blob: e7dcf07499e7cac3e3230382da86a7b649b802d7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
248static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
249static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100255
256#if defined(EBCDIC) || defined(PROTO)
257/*
258 * Compare struct fst by function name.
259 */
260 static int
261compare_func_name(const void *s1, const void *s2)
262{
263 struct fst *p1 = (struct fst *)s1;
264 struct fst *p2 = (struct fst *)s2;
265
266 return STRCMP(p1->f_name, p2->f_name);
267}
268
269/*
270 * Sort the function table by function name.
271 * The sorting of the table above is ASCII dependant.
272 * On machines using EBCDIC we have to sort it.
273 */
274 static void
275sortFunctions(void)
276{
277 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
278
279 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
280}
281#endif
282
283
Bram Moolenaar33570922005-01-25 22:26:29 +0000284/*
285 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000286 */
287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100288eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000289{
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 int i;
291 struct vimvar *p;
292
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200293 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
294 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200295 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000296 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299 for (i = 0; i < VV_LEN; ++i)
300 {
301 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 if (STRLEN(p->vv_name) > 16)
303 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100304 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200305 getout(1);
306 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 STRCPY(p->vv_di.di_key, p->vv_name);
308 if (p->vv_flags & VV_RO)
309 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
310 else if (p->vv_flags & VV_RO_SBX)
311 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
312 else
313 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000314
315 /* add to v: scope dict, unless the value is not always available */
316 if (p->vv_type != VAR_UNKNOWN)
317 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000319 /* add to compat scope dict */
320 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000321 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100322 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
323
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000324 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100325 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100326 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100327 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100328 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329
330 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
331 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
332 set_vim_var_nr(VV_NONE, VVAL_NONE);
333 set_vim_var_nr(VV_NULL, VVAL_NULL);
334
Bram Moolenaarf562e722016-07-19 17:25:25 +0200335 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
336 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
337 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
338 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
339 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
340 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
341 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
342 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
343 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
344 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100345 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200346
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200347 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348
349#ifdef EBCDIC
350 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100351 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200352 */
353 sortFunctions();
354#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000355}
356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000357#if defined(EXITFREE) || defined(PROTO)
358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100359eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000360{
361 int i;
362 struct vimvar *p;
363
364 for (i = 0; i < VV_LEN; ++i)
365 {
366 p = &vimvars[i];
367 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100368 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000369 else if (p->vv_di.di_tv.v_type == VAR_LIST)
370 {
371 list_unref(p->vv_list);
372 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 }
375 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000376 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377 hash_clear(&compat_hashtab);
378
Bram Moolenaard9fba312005-06-26 22:34:35 +0000379 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100380# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200381 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100382# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000383
384 /* global variables */
385 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000386
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000387 /* autoloaded script names */
388 ga_clear_strings(&ga_loaded);
389
Bram Moolenaarcca74132013-09-25 21:00:28 +0200390 /* Script-local variables. First clear all the variables and in a second
391 * loop free the scriptvar_T, because a variable in one script might hold
392 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200395 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200396 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 ga_clear(&ga_scripts);
398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000399 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200400 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000401
402 /* functions */
403 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000404}
405#endif
406
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * Set an internal variable to a string value. Creates the variable if it does
410 * not already exist.
411 */
412 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100413set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000415 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000416 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418 val = vim_strsave(value);
419 if (val != NULL)
420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000424 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427 }
428}
429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200431#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000432static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433static char_u *redir_endp = NULL;
434static char_u *redir_varname = NULL;
435
436/*
437 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439 * Returns OK if successfully completed the setup. FAIL otherwise.
440 */
441 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100442var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000443{
444 int save_emsg;
445 int err;
446 typval_T tv;
447
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000448 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000449 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 {
451 EMSG(_(e_invarg));
452 return FAIL;
453 }
454
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000455 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000456 redir_varname = vim_strsave(name);
457 if (redir_varname == NULL)
458 return FAIL;
459
460 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
461 if (redir_lval == NULL)
462 {
463 var_redir_stop();
464 return FAIL;
465 }
466
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000467 /* The output is stored in growarray "redir_ga" until redirection ends. */
468 ga_init2(&redir_ga, (int)sizeof(char), 500);
469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100471 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000472 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
474 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200475 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 if (redir_endp != NULL && *redir_endp != NUL)
477 /* Trailing characters are present after the variable name */
478 EMSG(_(e_trailing));
479 else
480 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000481 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482 var_redir_stop();
483 return FAIL;
484 }
485
486 /* check if we can write to the variable: set it to or append an empty
487 * string */
488 save_emsg = did_emsg;
489 did_emsg = FALSE;
490 tv.v_type = VAR_STRING;
491 tv.vval.v_string = (char_u *)"";
492 if (append)
493 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
494 else
495 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200496 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000498 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 if (err)
500 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000501 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 var_redir_stop();
503 return FAIL;
504 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000505
506 return OK;
507}
508
509/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000510 * Append "value[value_len]" to the variable set by var_redir_start().
511 * The actual appending is postponed until redirection ends, because the value
512 * appended may in fact be the string we write to, changing it may cause freed
513 * memory to be used:
514 * :redir => foo
515 * :let foo
516 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 */
518 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100519var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522
523 if (redir_lval == NULL)
524 return;
525
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000530
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 {
533 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000534 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000535 }
536 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538}
539
540/*
541 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547 typval_T tv;
548
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200549 if (EVALCMD_BUSY)
550 {
551 redir_lval = NULL;
552 return;
553 }
554
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000555 if (redir_lval != NULL)
556 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000557 /* If there was no error: assign the text to the variable. */
558 if (redir_endp != NULL)
559 {
560 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
561 tv.v_type = VAR_STRING;
562 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 /* Call get_lval() again, if it's inside a Dict or List it may
564 * have changed. */
565 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100566 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200567 if (redir_endp != NULL && redir_lval->ll_name != NULL)
568 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
569 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000570 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000572 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574
Bram Moolenaard23a8232018-02-10 18:45:26 +0100575 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100577 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100689 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaar48570482017-10-30 21:48:41 +0100699 static int
700eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
701{
702 char_u *s;
703 int dummy;
704 char_u buf[NUMBUFLEN];
705
706 if (expr->v_type == VAR_FUNC)
707 {
708 s = expr->vval.v_string;
709 if (s == NULL || *s == NUL)
710 return FAIL;
711 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
712 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
713 return FAIL;
714 }
715 else if (expr->v_type == VAR_PARTIAL)
716 {
717 partial_T *partial = expr->vval.v_partial;
718
719 s = partial_name(partial);
720 if (s == NULL || *s == NUL)
721 return FAIL;
722 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
723 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
724 return FAIL;
725 }
726 else
727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100728 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100729 if (s == NULL)
730 return FAIL;
731 s = skipwhite(s);
732 if (eval1(&s, rettv, TRUE) == FAIL)
733 return FAIL;
734 if (*s != NUL) /* check for trailing chars after expr */
735 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200736 clear_tv(rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100737 EMSG2(_(e_invexpr2), s);
738 return FAIL;
739 }
740 }
741 return OK;
742}
743
744/*
745 * Like eval_to_bool() but using a typval_T instead of a string.
746 * Works for string, funcref and partial.
747 */
748 int
749eval_expr_to_bool(typval_T *expr, int *error)
750{
751 typval_T rettv;
752 int res;
753
754 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
755 {
756 *error = TRUE;
757 return FALSE;
758 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100759 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100760 clear_tv(&rettv);
761 return res;
762}
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764/*
765 * Top level evaluation function, returning a string. If "skip" is TRUE,
766 * only parsing to "nextcmd" is done, without reporting errors. Return
767 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
768 */
769 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100770eval_to_string_skip(
771 char_u *arg,
772 char_u **nextcmd,
773 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 char_u *retval;
777
778 if (skip)
779 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000780 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 retval = NULL;
782 else
783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100784 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 if (skip)
788 --emsg_skip;
789
790 return retval;
791}
792
793/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000794 * Skip over an expression at "*pp".
795 * Return FAIL for an error, OK otherwise.
796 */
797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100798skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000801
802 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000804}
805
806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000808 * When "convert" is TRUE convert a List into a sequence of lines and convert
809 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * Return pointer to allocated memory, or NULL for failure.
811 */
812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813eval_to_string(
814 char_u *arg,
815 char_u **nextcmd,
816 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000820 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000821#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000822 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 retval = NULL;
827 else
828 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000829 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000830 {
831 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000832 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200833 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200834 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200835 if (tv.vval.v_list->lv_len > 0)
836 ga_append(&ga, NL);
837 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838 ga_append(&ga, NUL);
839 retval = (char_u *)ga.ga_data;
840 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000841#ifdef FEAT_FLOAT
842 else if (convert && tv.v_type == VAR_FLOAT)
843 {
844 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
845 retval = vim_strsave(numbuf);
846 }
847#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000848 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100849 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852
853 return retval;
854}
855
856/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000857 * Call eval_to_string() without using current local variables and using
858 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100861eval_to_string_safe(
862 char_u *arg,
863 char_u **nextcmd,
864 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200867 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200869 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000870 if (use_sandbox)
871 ++sandbox;
872 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000873 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000874 if (use_sandbox)
875 --sandbox;
876 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200877 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 return retval;
879}
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881/*
882 * Top level evaluation function, returning a number.
883 * Evaluates "expr" silently.
884 * Returns -1 for an error.
885 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200886 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100887eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
Bram Moolenaar33570922005-01-25 22:26:29 +0000889 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200890 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000891 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 ++emsg_off;
894
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 retval = -1;
897 else
898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100899 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 --emsg_off;
903
904 return retval;
905}
906
Bram Moolenaara40058a2005-07-11 22:42:07 +0000907/*
908 * Prepare v: variable "idx" to be used.
909 * Save the current typeval in "save_tv".
910 * When not used yet add the variable to the v: hashtable.
911 */
912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100913prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000914{
915 *save_tv = vimvars[idx].vv_tv;
916 if (vimvars[idx].vv_type == VAR_UNKNOWN)
917 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
918}
919
920/*
921 * Restore v: variable "idx" to typeval "save_tv".
922 * When no longer defined, remove the variable from the v: hashtable.
923 */
924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000926{
927 hashitem_T *hi;
928
Bram Moolenaara40058a2005-07-11 22:42:07 +0000929 vimvars[idx].vv_tv = *save_tv;
930 if (vimvars[idx].vv_type == VAR_UNKNOWN)
931 {
932 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
933 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100934 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000935 else
936 hash_remove(&vimvarht, hi);
937 }
938}
939
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000940#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000941/*
942 * Evaluate an expression to a list with suggestions.
943 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000944 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000945 */
946 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100947eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000948{
949 typval_T save_val;
950 typval_T rettv;
951 list_T *list = NULL;
952 char_u *p = skipwhite(expr);
953
954 /* Set "v:val" to the bad word. */
955 prepare_vimvar(VV_VAL, &save_val);
956 vimvars[VV_VAL].vv_type = VAR_STRING;
957 vimvars[VV_VAL].vv_str = badword;
958 if (p_verbose == 0)
959 ++emsg_off;
960
961 if (eval1(&p, &rettv, TRUE) == OK)
962 {
963 if (rettv.v_type != VAR_LIST)
964 clear_tv(&rettv);
965 else
966 list = rettv.vval.v_list;
967 }
968
969 if (p_verbose == 0)
970 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000971 restore_vimvar(VV_VAL, &save_val);
972
973 return list;
974}
975
976/*
977 * "list" is supposed to contain two items: a word and a number. Return the
978 * word in "pp" and the number as the return value.
979 * Return -1 if anything isn't right.
980 * Used to get the good word and score from the eval_spell_expr() result.
981 */
982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000984{
985 listitem_T *li;
986
987 li = list->lv_first;
988 if (li == NULL)
989 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100990 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000991
992 li = li->li_next;
993 if (li == NULL)
994 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000996}
997#endif
998
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000999/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001000 * Top level evaluation function.
1001 * Returns an allocated typval_T with the result.
1002 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001003 */
1004 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001006{
1007 typval_T *tv;
1008
1009 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001010 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001011 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001012
1013 return tv;
1014}
1015
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001018 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001019 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1020 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001021 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001024call_vim_function(
1025 char_u *func,
1026 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001027 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001028 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001031 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001033 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001034 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001036 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001037 if (ret == FAIL)
1038 clear_tv(rettv);
1039
1040 return ret;
1041}
1042
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001044 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001045 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001046 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1047 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001048 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001049 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050call_func_retnr(
1051 char_u *func,
1052 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001053 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001054{
1055 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001056 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001057
Bram Moolenaarded27a12018-08-01 19:06:03 +02001058 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001059 return -1;
1060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001062 clear_tv(&rettv);
1063 return retval;
1064}
1065
1066#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1067 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1068
Bram Moolenaar4f688582007-07-24 12:34:30 +00001069# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001070/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001071 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001072 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001073 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1074 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001075 */
1076 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001077call_func_retstr(
1078 char_u *func,
1079 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001080 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081{
1082 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001083 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084
Bram Moolenaarded27a12018-08-01 19:06:03 +02001085 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 return NULL;
1087
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001088 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 return retval;
1091}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001092# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001094/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001095 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001096 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1097 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001098 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 */
1100 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001101call_func_retlist(
1102 char_u *func,
1103 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001104 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001105{
1106 typval_T rettv;
1107
Bram Moolenaarded27a12018-08-01 19:06:03 +02001108 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109 return NULL;
1110
1111 if (rettv.v_type != VAR_LIST)
1112 {
1113 clear_tv(&rettv);
1114 return NULL;
1115 }
1116
1117 return rettv.vval.v_list;
1118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
1120
Bram Moolenaar05159a02005-02-26 23:04:13 +00001121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#ifdef FEAT_FOLDING
1123/*
1124 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1125 * it in "*cp". Doesn't give error messages.
1126 */
1127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001128eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001131 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001133 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1134 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001137 if (use_sandbox)
1138 ++sandbox;
1139 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 retval = 0;
1143 else
1144 {
1145 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 if (tv.v_type == VAR_NUMBER)
1147 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001148 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 retval = 0;
1150 else
1151 {
1152 /* If the result is a string, check if there is a non-digit before
1153 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!VIM_ISDIGIT(*s) && *s != '-')
1156 *cp = *s++;
1157 retval = atol((char *)s);
1158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001162 if (use_sandbox)
1163 --sandbox;
1164 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001166 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167}
1168#endif
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 * ":let" list all variable values
1172 * ":let var1 var2" list variable values
1173 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001174 * ":let var += expr" assignment command.
1175 * ":let var -= expr" assignment command.
1176 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 */
1179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001180ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001186 int var_count = 0;
1187 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001189 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001190 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaardb552d602006-03-23 22:59:57 +00001192 argend = skip_var_list(arg, &var_count, &semicolon);
1193 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001194 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001195 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1196 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001197 expr = skipwhite(argend);
1198 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1199 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001201 /*
1202 * ":let" without "=": list variables
1203 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001204 if (*arg == '[')
1205 EMSG(_(e_invarg));
1206 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001208 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001212 list_glob_vars(&first);
1213 list_buf_vars(&first);
1214 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001215 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001216 list_script_vars(&first);
1217 list_func_vars(&first);
1218 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 eap->nextcmd = check_nextcmd(arg);
1221 }
1222 else
1223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 op[0] = '=';
1225 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001226 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001228 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1229 op[0] = *expr; /* +=, -= or .= */
1230 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001231 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001232 else
1233 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (eap->skip)
1236 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (eap->skip)
1239 {
1240 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 --emsg_skip;
1243 }
1244 else if (i != FAIL)
1245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001246 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001248 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 }
1251}
1252
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001253/*
1254 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1255 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001256 * When "nextchars" is not NULL it points to a string with characters that
1257 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1258 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259 * Returns OK or FAIL;
1260 */
1261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001262ex_let_vars(
1263 char_u *arg_start,
1264 typval_T *tv,
1265 int copy, /* copy values from "tv", don't move */
1266 int semicolon, /* from skip_var_list() */
1267 int var_count, /* from skip_var_list() */
1268 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001269{
1270 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001272 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 listitem_T *item;
1274 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001275
1276 if (*arg != '[')
1277 {
1278 /*
1279 * ":let var = expr" or ":for var in list"
1280 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001282 return FAIL;
1283 return OK;
1284 }
1285
1286 /*
1287 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1288 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001289 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 {
1291 EMSG(_(e_listreq));
1292 return FAIL;
1293 }
1294
1295 i = list_len(l);
1296 if (semicolon == 0 && var_count < i)
1297 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001298 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 return FAIL;
1300 }
1301 if (var_count - semicolon > i)
1302 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001303 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001304 return FAIL;
1305 }
1306
1307 item = l->lv_first;
1308 while (*arg != ']')
1309 {
1310 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001311 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001312 item = item->li_next;
1313 if (arg == NULL)
1314 return FAIL;
1315
1316 arg = skipwhite(arg);
1317 if (*arg == ';')
1318 {
1319 /* Put the rest of the list (may be empty) in the var after ';'.
1320 * Create a new list for this. */
1321 l = list_alloc();
1322 if (l == NULL)
1323 return FAIL;
1324 while (item != NULL)
1325 {
1326 list_append_tv(l, &item->li_tv);
1327 item = item->li_next;
1328 }
1329
1330 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001331 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332 ltv.vval.v_list = l;
1333 l->lv_refcount = 1;
1334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1336 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 clear_tv(&ltv);
1338 if (arg == NULL)
1339 return FAIL;
1340 break;
1341 }
1342 else if (*arg != ',' && *arg != ']')
1343 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001344 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001345 return FAIL;
1346 }
1347 }
1348
1349 return OK;
1350}
1351
1352/*
1353 * Skip over assignable variable "var" or list of variables "[var, var]".
1354 * Used for ":let varvar = expr" and ":for varvar in expr".
1355 * For "[var, var]" increment "*var_count" for each variable.
1356 * for "[var, var; var]" set "semicolon".
1357 * Return NULL for an error.
1358 */
1359 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360skip_var_list(
1361 char_u *arg,
1362 int *var_count,
1363 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364{
1365 char_u *p, *s;
1366
1367 if (*arg == '[')
1368 {
1369 /* "[var, var]": find the matching ']'. */
1370 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001371 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 {
1373 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1374 s = skip_var_one(p);
1375 if (s == p)
1376 {
1377 EMSG2(_(e_invarg2), p);
1378 return NULL;
1379 }
1380 ++*var_count;
1381
1382 p = skipwhite(s);
1383 if (*p == ']')
1384 break;
1385 else if (*p == ';')
1386 {
1387 if (*semicolon == 1)
1388 {
1389 EMSG(_("Double ; in list of variables"));
1390 return NULL;
1391 }
1392 *semicolon = 1;
1393 }
1394 else if (*p != ',')
1395 {
1396 EMSG2(_(e_invarg2), p);
1397 return NULL;
1398 }
1399 }
1400 return p + 1;
1401 }
1402 else
1403 return skip_var_one(arg);
1404}
1405
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001406/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001407 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001408 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001409 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001411skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001413 if (*arg == '@' && arg[1] != NUL)
1414 return arg + 2;
1415 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1416 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417}
1418
Bram Moolenaara7043832005-01-21 11:56:39 +00001419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 * List variables for hashtab "ht" with prefix "prefix".
1421 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001422 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424list_hashtable_vars(
1425 hashtab_T *ht,
1426 char_u *prefix,
1427 int empty,
1428 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001429{
Bram Moolenaar33570922005-01-25 22:26:29 +00001430 hashitem_T *hi;
1431 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001432 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001433 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001434
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001435 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001436 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1437 {
1438 if (!HASHITEM_EMPTY(hi))
1439 {
1440 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001442
1443 // apply :filter /pat/ to variable name
1444 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1445 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1446 if (message_filtered(buf))
1447 continue;
1448
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 if (empty || di->di_tv.v_type != VAR_STRING
1450 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001451 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001452 }
1453 }
1454}
1455
1456/*
1457 * List global variables.
1458 */
1459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001461{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001462 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001463}
1464
1465/*
1466 * List buffer variables.
1467 */
1468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001470{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001471 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001472 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001473}
1474
1475/*
1476 * List window variables.
1477 */
1478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001479list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001480{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001481 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001482 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001483}
1484
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001485/*
1486 * List tab page variables.
1487 */
1488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001490{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001491 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001492 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001493}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001494
Bram Moolenaara7043832005-01-21 11:56:39 +00001495/*
1496 * List Vim variables.
1497 */
1498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001501 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502}
1503
1504/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001505 * List script-local variables, if there is a script.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001509{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001510 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1511 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001512 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001513}
1514
1515/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 * List variables in "arg".
1517 */
1518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520{
1521 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001522 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524 char_u *name_start;
1525 char_u *arg_subsc;
1526 char_u *tofree;
1527 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528
1529 while (!ends_excmd(*arg) && !got_int)
1530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001533 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001534 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 {
1536 emsg_severe = TRUE;
1537 EMSG(_(e_trailing));
1538 break;
1539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 /* get_name_len() takes care of expanding curly braces */
1544 name_start = name = arg;
1545 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1546 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001548 /* This is mainly to keep test 49 working: when expanding
1549 * curly braces fails overrule the exception error message. */
1550 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 emsg_severe = TRUE;
1553 EMSG2(_(e_invarg2), arg);
1554 break;
1555 }
1556 error = TRUE;
1557 }
1558 else
1559 {
1560 if (tofree != NULL)
1561 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001562 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001563 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 else
1565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 /* handle d.key, l[idx], f(expr) */
1567 arg_subsc = arg;
1568 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001574 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001576 case 'g': list_glob_vars(first); break;
1577 case 'b': list_buf_vars(first); break;
1578 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001579 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001580 case 'v': list_vim_vars(first); break;
1581 case 's': list_script_vars(first); break;
1582 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583 default:
1584 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001585 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001586 }
1587 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 {
1589 char_u numbuf[NUMBUFLEN];
1590 char_u *tf;
1591 int c;
1592 char_u *s;
1593
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001594 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595 c = *arg;
1596 *arg = NUL;
1597 list_one_var_a((char_u *)"",
1598 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001599 tv.v_type,
1600 s == NULL ? (char_u *)"" : s,
1601 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001602 *arg = c;
1603 vim_free(tf);
1604 }
1605 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 }
1608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001609
1610 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001612
1613 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 }
1615
1616 return arg;
1617}
1618
1619/*
1620 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1621 * Returns a pointer to the char just after the var name.
1622 * Returns NULL if there is an error.
1623 */
1624 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001625ex_let_one(
1626 char_u *arg, /* points to variable name */
1627 typval_T *tv, /* value to assign to variable */
1628 int copy, /* copy value from "tv" */
1629 char_u *endchars, /* valid chars after variable name or NULL */
1630 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631{
1632 int c1;
1633 char_u *name;
1634 char_u *p;
1635 char_u *arg_end = NULL;
1636 int len;
1637 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639
1640 /*
1641 * ":let $VAR = expr": Set environment variable.
1642 */
1643 if (*arg == '$')
1644 {
1645 /* Find the end of the name. */
1646 ++arg;
1647 name = arg;
1648 len = get_env_len(&arg);
1649 if (len == 0)
1650 EMSG2(_(e_invarg2), name - 1);
1651 else
1652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 if (op != NULL && (*op == '+' || *op == '-'))
1654 EMSG2(_(e_letwrong), op);
1655 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001658 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 {
1660 c1 = name[len];
1661 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001662 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 {
1665 int mustfree = FALSE;
1666 char_u *s = vim_getenv(name, &mustfree);
1667
1668 if (s != NULL)
1669 {
1670 p = tofree = concat_str(s, p);
1671 if (mustfree)
1672 vim_free(s);
1673 }
1674 }
1675 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001678 if (STRICMP(name, "HOME") == 0)
1679 init_homedir();
1680 else if (didset_vim && STRICMP(name, "VIM") == 0)
1681 didset_vim = FALSE;
1682 else if (didset_vimruntime
1683 && STRICMP(name, "VIMRUNTIME") == 0)
1684 didset_vimruntime = FALSE;
1685 arg_end = arg;
1686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 }
1690 }
1691 }
1692
1693 /*
1694 * ":let &option = expr": Set option value.
1695 * ":let &l:option = expr": Set local option value.
1696 * ":let &g:option = expr": Set global option value.
1697 */
1698 else if (*arg == '&')
1699 {
1700 /* Find the end of the name. */
1701 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 if (p == NULL || (endchars != NULL
1703 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 EMSG(_(e_letunexp));
1705 else
1706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707 long n;
1708 int opt_type;
1709 long numval;
1710 char_u *stringval = NULL;
1711 char_u *s;
1712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 c1 = *p;
1714 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001716 n = (long)tv_get_number(tv);
1717 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 opt_type = get_option_value(arg, &numval,
1721 &stringval, opt_flags);
1722 if ((opt_type == 1 && *op == '.')
1723 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001726 s = NULL; /* don't set the value */
1727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 else
1729 {
1730 if (opt_type == 1) /* number */
1731 {
1732 if (*op == '+')
1733 n = numval + n;
1734 else
1735 n = numval - n;
1736 }
1737 else if (opt_type == 0 && stringval != NULL) /* string */
1738 {
1739 s = concat_str(stringval, s);
1740 vim_free(stringval);
1741 stringval = s;
1742 }
1743 }
1744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001745 if (s != NULL)
1746 {
1747 set_option_value(arg, n, s, opt_flags);
1748 arg_end = p;
1749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 }
1753 }
1754
1755 /*
1756 * ":let @r = expr": Set register contents.
1757 */
1758 else if (*arg == '@')
1759 {
1760 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 if (op != NULL && (*op == '+' || *op == '-'))
1762 EMSG2(_(e_letwrong), op);
1763 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 EMSG(_(e_letunexp));
1766 else
1767 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 char_u *s;
1770
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001771 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001774 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 if (s != NULL)
1776 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001777 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 vim_free(s);
1779 }
1780 }
1781 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001784 arg_end = arg + 1;
1785 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001786 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 }
1788 }
1789
1790 /*
1791 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001794 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001798 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001799 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1802 EMSG(_(e_letunexp));
1803 else
1804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 arg_end = p;
1807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001809 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 }
1811
1812 else
1813 EMSG2(_(e_invarg2), arg);
1814
1815 return arg_end;
1816}
1817
1818/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 * Get an lval: variable, Dict item or List item that can be assigned a value
1820 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1821 * "name.key", "name.key[expr]" etc.
1822 * Indexing only works if "name" is an existing List or Dictionary.
1823 * "name" points to the start of the name.
1824 * If "rettv" is not NULL it points to the value to be assigned.
1825 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1826 * wrong; must end in space or cmd separator.
1827 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001828 * flags:
1829 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001830 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001831 * GLV_NO_AUTOLOAD: do not use script autoloading
1832 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001834 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838get_lval(
1839 char_u *name,
1840 typval_T *rettv,
1841 lval_T *lp,
1842 int unlet,
1843 int skip,
1844 int flags, /* GLV_ values */
1845 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 char_u *expr_start, *expr_end;
1849 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 dictitem_T *v;
1851 typval_T var1;
1852 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001855 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001856 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001858 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862
1863 if (skip)
1864 {
1865 /* When skipping just find the end of the name. */
1866 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001867 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 }
1869
1870 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001871 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 if (expr_start != NULL)
1873 {
1874 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001875 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001876 && *p != '[' && *p != '.')
1877 {
1878 EMSG(_(e_trailing));
1879 return NULL;
1880 }
1881
1882 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1883 if (lp->ll_exp_name == NULL)
1884 {
1885 /* Report an invalid expression in braces, unless the
1886 * expression evaluation has been cancelled due to an
1887 * aborting error, an interrupt, or an exception. */
1888 if (!aborting() && !quiet)
1889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001890 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 EMSG2(_(e_invarg2), name);
1892 return NULL;
1893 }
1894 }
1895 lp->ll_name = lp->ll_exp_name;
1896 }
1897 else
1898 lp->ll_name = name;
1899
1900 /* Without [idx] or .key we are done. */
1901 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1902 return p;
1903
1904 cc = *p;
1905 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001906 /* Only pass &ht when we would write to the variable, it prevents autoload
1907 * as well. */
1908 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1909 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (v == NULL && !quiet)
1911 EMSG2(_(e_undefvar), lp->ll_name);
1912 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 if (v == NULL)
1914 return NULL;
1915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 /*
1917 * Loop until no more [idx] or .key is following.
1918 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001920 var1.v_type = VAR_UNKNOWN;
1921 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1925 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001926 && lp->ll_tv->vval.v_dict != NULL)
1927 && !(lp->ll_tv->v_type == VAR_BLOB
1928 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 if (!quiet)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001931 EMSG(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 if (!quiet)
1937 EMSG(_("E708: [:] must come last"));
1938 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001940
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 len = -1;
1942 if (*p == '.')
1943 {
1944 key = p + 1;
1945 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1946 ;
1947 if (len == 0)
1948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 if (!quiet)
1950 EMSG(_(e_emptykey));
1951 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001952 }
1953 p = key + len;
1954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955 else
1956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001959 if (*p == ':')
1960 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001961 else
1962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 empty1 = FALSE;
1964 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001966 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 {
1968 /* not a number or string */
1969 clear_tv(&var1);
1970 return NULL;
1971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 }
1973
1974 /* Optionally get the second index [ :expr]. */
1975 if (*p == ':')
1976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001984 if (rettv != NULL
1985 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001986 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001988 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 if (!quiet)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001991 EMSG(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 }
1995 p = skipwhite(p + 1);
1996 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 else
1999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2002 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002003 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002006 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 {
2008 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002009 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 clear_tv(&var2);
2011 return NULL;
2012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002018
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (!quiet)
2022 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002023 clear_tv(&var1);
2024 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 }
2027
2028 /* Skip to past ']'. */
2029 ++p;
2030 }
2031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 {
2034 if (len == -1)
2035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002037 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002038 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 }
2043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 lp->ll_list = NULL;
2045 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002046 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002048 /* When assigning to a scope dictionary check that a function and
2049 * variable name is valid (only variable name unless it is l: or
2050 * g: dictionary). Disallow overwriting a builtin function. */
2051 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002053 int prevval;
2054 int wrong;
2055
2056 if (len != -1)
2057 {
2058 prevval = key[len];
2059 key[len] = NUL;
2060 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002061 else
2062 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002063 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2064 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002065 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002066 || !valid_varname(key);
2067 if (len != -1)
2068 key[len] = prevval;
2069 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002070 return NULL;
2071 }
2072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075 /* Can't add "v:" variable. */
2076 if (lp->ll_dict == &vimvardict)
2077 {
2078 EMSG2(_(e_illvar), name);
2079 return NULL;
2080 }
2081
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002082 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002087 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 }
2090 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002094 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002096 p = NULL;
2097 break;
2098 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002100 else if ((flags & GLV_READ_ONLY) == 0
2101 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002102 {
2103 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002104 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002105 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002106
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002110 else if (lp->ll_tv->v_type == VAR_BLOB)
2111 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002112 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2113
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002114 /*
2115 * Get the number and item for the only or first index of the List.
2116 */
2117 if (empty1)
2118 lp->ll_n1 = 0;
2119 else
2120 // is number or string
2121 lp->ll_n1 = (long)tv_get_number(&var1);
2122 clear_tv(&var1);
2123
2124 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002125 || lp->ll_n1 > bloblen
2126 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002127 {
2128 if (!quiet)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002129 EMSGN(_(e_blobidx), lp->ll_n1);
2130 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002131 return NULL;
2132 }
2133 if (lp->ll_range && !lp->ll_empty2)
2134 {
2135 lp->ll_n2 = (long)tv_get_number(&var2);
2136 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002137 if (lp->ll_n2 < 0
2138 || lp->ll_n2 >= bloblen
2139 || lp->ll_n2 < lp->ll_n1)
2140 {
2141 if (!quiet)
2142 EMSGN(_(e_blobidx), lp->ll_n2);
2143 return NULL;
2144 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002145 }
2146 lp->ll_blob = lp->ll_tv->vval.v_blob;
2147 lp->ll_tv = NULL;
2148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 else
2150 {
2151 /*
2152 * Get the number and item for the only or first index of the List.
2153 */
2154 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002157 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002159 clear_tv(&var1);
2160
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 lp->ll_list = lp->ll_tv->vval.v_list;
2163 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2164 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002165 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002166 if (lp->ll_n1 < 0)
2167 {
2168 lp->ll_n1 = 0;
2169 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2170 }
2171 }
2172 if (lp->ll_li == NULL)
2173 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002174 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002175 if (!quiet)
2176 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 }
2179
2180 /*
2181 * May need to find the item or absolute index for the second
2182 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 * When no index given: "lp->ll_empty2" is TRUE.
2184 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002188 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002189 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002195 {
2196 if (!quiet)
2197 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 }
2202
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2204 if (lp->ll_n1 < 0)
2205 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2206 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002207 {
2208 if (!quiet)
2209 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002211 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002212 }
2213
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
2217
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002218 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 return p;
2220}
2221
2222/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002223 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227{
2228 vim_free(lp->ll_exp_name);
2229 vim_free(lp->ll_newkey);
2230}
2231
2232/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002233 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002235 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238set_var_lval(
2239 lval_T *lp,
2240 char_u *endp,
2241 typval_T *rettv,
2242 int copy,
2243 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244{
2245 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 listitem_T *ri;
2247 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248
2249 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002251 cc = *endp;
2252 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002253 if (lp->ll_blob != NULL)
2254 {
2255 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002256
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002257 if (op != NULL && *op != '=')
2258 {
2259 EMSG2(_(e_letwrong), op);
2260 return;
2261 }
2262
2263 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2264 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002265 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002266
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002267 if (lp->ll_empty2)
2268 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2269
2270 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002271 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002272 EMSG(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002273 return;
2274 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002275 if (lp->ll_empty2)
2276 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002277
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002278 ir = 0;
2279 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2280 blob_set(lp->ll_blob, il,
2281 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 }
2283 else
2284 {
2285 val = (int)tv_get_number_chk(rettv, &error);
2286 if (!error)
2287 {
2288 garray_T *gap = &lp->ll_blob->bv_ga;
2289
2290 // Allow for appending a byte. Setting a byte beyond
2291 // the end is an error otherwise.
2292 if (lp->ll_n1 < gap->ga_len
2293 || (lp->ll_n1 == gap->ga_len
2294 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2295 {
2296 blob_set(lp->ll_blob, lp->ll_n1, val);
2297 if (lp->ll_n1 == gap->ga_len)
2298 ++gap->ga_len;
2299 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002301 }
2302 }
2303 }
2304 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002306 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307
Bram Moolenaar79518e22017-02-17 16:31:35 +01002308 /* handle +=, -= and .= */
2309 di = NULL;
2310 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2311 &tv, &di, TRUE, FALSE) == OK)
2312 {
2313 if ((di == NULL
2314 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2315 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2316 FALSE)))
2317 && tv_op(&tv, rettv, op) == OK)
2318 set_var(lp->ll_name, &tv, FALSE);
2319 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002322 else
2323 set_var(lp->ll_name, rettv, copy);
2324 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002326 else if (tv_check_lock(lp->ll_newkey == NULL
2327 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002328 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002329 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 else if (lp->ll_range)
2331 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002332 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002333 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002334
2335 /*
2336 * Check whether any of the list items is locked
2337 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002338 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002339 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002340 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002341 return;
2342 ri = ri->li_next;
2343 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2344 break;
2345 ll_li = ll_li->li_next;
2346 ++ll_n1;
2347 }
2348
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 /*
2350 * Assign the List values to the list items.
2351 */
2352 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 if (op != NULL && *op != '=')
2355 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2356 else
2357 {
2358 clear_tv(&lp->ll_li->li_tv);
2359 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2360 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 ri = ri->li_next;
2362 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2363 break;
2364 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002367 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 ri = NULL;
2370 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 lp->ll_li = lp->ll_li->li_next;
2374 ++lp->ll_n1;
2375 }
2376 if (ri != NULL)
2377 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 else if (lp->ll_empty2
2379 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 : lp->ll_n1 != lp->ll_n2)
2381 EMSG(_("E711: List value has not enough items"));
2382 }
2383 else
2384 {
2385 /*
2386 * Assign to a List or Dictionary item.
2387 */
2388 if (lp->ll_newkey != NULL)
2389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 if (op != NULL && *op != '=')
2391 {
2392 EMSG2(_(e_letwrong), op);
2393 return;
2394 }
2395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002397 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 if (di == NULL)
2399 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002400 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2401 {
2402 vim_free(di);
2403 return;
2404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002406 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 else if (op != NULL && *op != '=')
2408 {
2409 tv_op(lp->ll_tv, rettv, op);
2410 return;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 /*
2416 * Assign the value to the variable or list item.
2417 */
2418 if (copy)
2419 copy_tv(rettv, lp->ll_tv);
2420 else
2421 {
2422 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002423 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427}
2428
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2431 * Returns OK or FAIL.
2432 */
2433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002436 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u numbuf[NUMBUFLEN];
2438 char_u *s;
2439
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002440 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2441 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2442 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 switch (tv1->v_type)
2445 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002446 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 case VAR_DICT:
2448 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002449 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002450 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002451 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002452 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 break;
2454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002455 case VAR_BLOB:
2456 if (*op != '+' || tv2->v_type != VAR_BLOB)
2457 break;
2458 // BLOB += BLOB
2459 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2460 {
2461 blob_T *b1 = tv1->vval.v_blob;
2462 blob_T *b2 = tv2->vval.v_blob;
2463 int i, len = blob_len(b2);
2464 for (i = 0; i < len; i++)
2465 ga_append(&b1->bv_ga, blob_get(b2, i));
2466 }
2467 return OK;
2468
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 case VAR_LIST:
2470 if (*op != '+' || tv2->v_type != VAR_LIST)
2471 break;
2472 /* List += List */
2473 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2474 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2475 return OK;
2476
2477 case VAR_NUMBER:
2478 case VAR_STRING:
2479 if (tv2->v_type == VAR_LIST)
2480 break;
2481 if (*op == '+' || *op == '-')
2482 {
2483 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002484 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002485#ifdef FEAT_FLOAT
2486 if (tv2->v_type == VAR_FLOAT)
2487 {
2488 float_T f = n;
2489
2490 if (*op == '+')
2491 f += tv2->vval.v_float;
2492 else
2493 f -= tv2->vval.v_float;
2494 clear_tv(tv1);
2495 tv1->v_type = VAR_FLOAT;
2496 tv1->vval.v_float = f;
2497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002499#endif
2500 {
2501 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002502 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002503 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505 clear_tv(tv1);
2506 tv1->v_type = VAR_NUMBER;
2507 tv1->vval.v_number = n;
2508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 }
2510 else
2511 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002512 if (tv2->v_type == VAR_FLOAT)
2513 break;
2514
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002516 s = tv_get_string(tv1);
2517 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 clear_tv(tv1);
2519 tv1->v_type = VAR_STRING;
2520 tv1->vval.v_string = s;
2521 }
2522 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002523
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002525#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002526 {
2527 float_T f;
2528
2529 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2530 && tv2->v_type != VAR_NUMBER
2531 && tv2->v_type != VAR_STRING))
2532 break;
2533 if (tv2->v_type == VAR_FLOAT)
2534 f = tv2->vval.v_float;
2535 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 if (*op == '+')
2538 tv1->vval.v_float += f;
2539 else
2540 tv1->vval.v_float -= f;
2541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002542#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002543 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 }
2545 }
2546
2547 EMSG2(_(e_letwrong), op);
2548 return FAIL;
2549}
2550
2551/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 * Evaluate the expression used in a ":for var in expr" command.
2553 * "arg" points to "var".
2554 * Set "*errp" to TRUE for an error, FALSE otherwise;
2555 * Return a pointer that holds the info. Null when there is an error.
2556 */
2557 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002558eval_for_line(
2559 char_u *arg,
2560 int *errp,
2561 char_u **nextcmdp,
2562 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002563{
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002565 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 typval_T tv;
2567 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002568 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002569
2570 *errp = TRUE; /* default: there is an error */
2571
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573 if (fi == NULL)
2574 return NULL;
2575
2576 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2577 if (expr == NULL)
2578 return fi;
2579
2580 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002581 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002582 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002583 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002584 return fi;
2585 }
2586
2587 if (skip)
2588 ++emsg_skip;
2589 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2590 {
2591 *errp = FALSE;
2592 if (!skip)
2593 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002594 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002595 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002596 l = tv.vval.v_list;
2597 if (l == NULL)
2598 {
2599 // a null list is like an empty list: do nothing
2600 clear_tv(&tv);
2601 }
2602 else
2603 {
2604 // No need to increment the refcount, it's already set for
2605 // the list being used in "tv".
2606 fi->fi_list = l;
2607 list_add_watch(l, &fi->fi_lw);
2608 fi->fi_lw.lw_item = l->lv_first;
2609 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002610 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002611 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002612 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002613 b = tv.vval.v_blob;
2614 if (b == NULL)
2615 clear_tv(&tv);
2616 else
2617 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002618 // No need to increment the refcount, it's already set for
2619 // the blob being used in "tv".
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002620 fi->fi_blob = b;
2621 fi->fi_bi = 0;
2622 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002623 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002624 else
2625 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002626 EMSG(_(e_listreq));
2627 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002628 }
2629 }
2630 }
2631 if (skip)
2632 --emsg_skip;
2633
2634 return fi;
2635}
2636
2637/*
2638 * Use the first item in a ":for" list. Advance to the next.
2639 * Assign the values to the variable (list). "arg" points to the first one.
2640 * Return TRUE when a valid item was found, FALSE when at end of list or
2641 * something wrong.
2642 */
2643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002644next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002646 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002647 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002650 if (fi->fi_blob != NULL)
2651 {
2652 typval_T tv;
2653
2654 if (fi->fi_bi >= blob_len(fi->fi_blob))
2655 return FALSE;
2656 tv.v_type = VAR_NUMBER;
2657 tv.v_lock = VAR_FIXED;
2658 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2659 ++fi->fi_bi;
2660 return ex_let_vars(arg, &tv, TRUE,
2661 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2662 }
2663
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002664 item = fi->fi_lw.lw_item;
2665 if (item == NULL)
2666 result = FALSE;
2667 else
2668 {
2669 fi->fi_lw.lw_item = item->li_next;
2670 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2671 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2672 }
2673 return result;
2674}
2675
2676/*
2677 * Free the structure used to store info used by ":for".
2678 */
2679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681{
Bram Moolenaar33570922005-01-25 22:26:29 +00002682 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002683
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002684 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002685 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002686 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002687 list_unref(fi->fi_list);
2688 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002689 if (fi != NULL && fi->fi_blob != NULL)
2690 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 vim_free(fi);
2692}
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2695
2696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002697set_context_for_expression(
2698 expand_T *xp,
2699 char_u *arg,
2700 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 int got_eq = FALSE;
2703 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706 if (cmdidx == CMD_let)
2707 {
2708 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002709 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710 {
2711 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002712 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002713 {
2714 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002715 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002716 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 break;
2718 }
2719 return;
2720 }
2721 }
2722 else
2723 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2724 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 while ((xp->xp_pattern = vim_strpbrk(arg,
2726 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2727 {
2728 c = *xp->xp_pattern;
2729 if (c == '&')
2730 {
2731 c = xp->xp_pattern[1];
2732 if (c == '&')
2733 {
2734 ++xp->xp_pattern;
2735 xp->xp_context = cmdidx != CMD_let || got_eq
2736 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2737 }
2738 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002741 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2742 xp->xp_pattern += 2;
2743
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 else if (c == '$')
2747 {
2748 /* environment variable */
2749 xp->xp_context = EXPAND_ENV_VARS;
2750 }
2751 else if (c == '=')
2752 {
2753 got_eq = TRUE;
2754 xp->xp_context = EXPAND_EXPRESSION;
2755 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002756 else if (c == '#'
2757 && xp->xp_context == EXPAND_EXPRESSION)
2758 {
2759 /* Autoload function/variable contains '#'. */
2760 break;
2761 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002762 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 && xp->xp_context == EXPAND_FUNCTIONS
2764 && vim_strchr(xp->xp_pattern, '(') == NULL)
2765 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002766 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 break;
2768 }
2769 else if (cmdidx != CMD_let || got_eq)
2770 {
2771 if (c == '"') /* string */
2772 {
2773 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2774 if (c == '\\' && xp->xp_pattern[1] != NUL)
2775 ++xp->xp_pattern;
2776 xp->xp_context = EXPAND_NOTHING;
2777 }
2778 else if (c == '\'') /* literal string */
2779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2782 /* skip */ ;
2783 xp->xp_context = EXPAND_NOTHING;
2784 }
2785 else if (c == '|')
2786 {
2787 if (xp->xp_pattern[1] == '|')
2788 {
2789 ++xp->xp_pattern;
2790 xp->xp_context = EXPAND_EXPRESSION;
2791 }
2792 else
2793 xp->xp_context = EXPAND_COMMANDS;
2794 }
2795 else
2796 xp->xp_context = EXPAND_EXPRESSION;
2797 }
2798 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 /* Doesn't look like something valid, expand as an expression
2800 * anyway. */
2801 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 arg = xp->xp_pattern;
2803 if (*arg != NUL)
2804 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2805 /* skip */ ;
2806 }
2807 xp->xp_pattern = arg;
2808}
2809
2810#endif /* FEAT_CMDL_COMPL */
2811
2812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 * ":unlet[!] var1 ... " command.
2814 */
2815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 ex_unletlock(eap, eap->arg, 0);
2819}
2820
2821/*
2822 * ":lockvar" and ":unlockvar" commands
2823 */
2824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002825ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002826{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002828 int deep = 2;
2829
2830 if (eap->forceit)
2831 deep = -1;
2832 else if (vim_isdigit(*arg))
2833 {
2834 deep = getdigits(&arg);
2835 arg = skipwhite(arg);
2836 }
2837
2838 ex_unletlock(eap, arg, deep);
2839}
2840
2841/*
2842 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2843 */
2844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845ex_unletlock(
2846 exarg_T *eap,
2847 char_u *argstart,
2848 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849{
2850 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002853 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 do
2856 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002857 if (*arg == '$')
2858 {
2859 char_u *name = ++arg;
2860
2861 if (get_env_len(&arg) == 0)
2862 {
2863 EMSG2(_(e_invarg2), name - 1);
2864 return;
2865 }
2866 vim_unsetenv(name);
2867 arg = skipwhite(arg);
2868 continue;
2869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002872 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002873 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 if (lv.ll_name == NULL)
2875 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002876 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (name_end != NULL)
2880 {
2881 emsg_severe = TRUE;
2882 EMSG(_(e_trailing));
2883 }
2884 if (!(eap->skip || error))
2885 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 break;
2887 }
2888
2889 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002890 {
2891 if (eap->cmdidx == CMD_unlet)
2892 {
2893 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2894 error = TRUE;
2895 }
2896 else
2897 {
2898 if (do_lock_var(&lv, name_end, deep,
2899 eap->cmdidx == CMD_lockvar) == FAIL)
2900 error = TRUE;
2901 }
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!eap->skip)
2905 clear_lval(&lv);
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 arg = skipwhite(name_end);
2908 } while (!ends_excmd(*arg));
2909
2910 eap->nextcmd = check_nextcmd(arg);
2911}
2912
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914do_unlet_var(
2915 lval_T *lp,
2916 char_u *name_end,
2917 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int ret = OK;
2920 int cc;
2921
2922 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 cc = *name_end;
2925 *name_end = NUL;
2926
2927 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002928 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002932 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002933 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002934 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002935 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 else if (lp->ll_range)
2938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002940 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002941 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002942
2943 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2944 {
2945 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002946 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002947 return FAIL;
2948 ll_li = li;
2949 ++ll_n1;
2950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951
2952 /* Delete a range of List items. */
2953 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2954 {
2955 li = lp->ll_li->li_next;
2956 listitem_remove(lp->ll_list, lp->ll_li);
2957 lp->ll_li = li;
2958 ++lp->ll_n1;
2959 }
2960 }
2961 else
2962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* unlet a List item. */
2965 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 }
2970
2971 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972}
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974/*
2975 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 */
2978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002979do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 hashtab_T *ht;
2982 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002983 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002984 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002985 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 ht = find_var_ht(name, &varname);
2988 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002991 if (d == NULL)
2992 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 if (ht == &globvarht)
2994 d = &globvardict;
2995 else if (ht == &compat_hashtab)
2996 d = &vimvardict;
2997 else
2998 {
2999 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3000 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3001 }
3002 if (d == NULL)
3003 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003004 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 return FAIL;
3006 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003009 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003010 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003011 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003012 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003013 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003014 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003015 || var_check_ro(di->di_flags, name, FALSE)
3016 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003017 return FAIL;
3018
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 delete_var(ht, hi);
3020 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 if (forceit)
3024 return OK;
3025 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 return FAIL;
3027}
3028
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003029/*
3030 * Lock or unlock variable indicated by "lp".
3031 * "deep" is the levels to go (-1 for unlimited);
3032 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3033 */
3034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003035do_lock_var(
3036 lval_T *lp,
3037 char_u *name_end,
3038 int deep,
3039 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003040{
3041 int ret = OK;
3042 int cc;
3043 dictitem_T *di;
3044
3045 if (deep == 0) /* nothing to do */
3046 return OK;
3047
3048 if (lp->ll_tv == NULL)
3049 {
3050 cc = *name_end;
3051 *name_end = NUL;
3052
3053 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003054 di = find_var(lp->ll_name, NULL, TRUE);
3055 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003056 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003057 else if ((di->di_flags & DI_FLAGS_FIX)
3058 && di->di_tv.v_type != VAR_DICT
3059 && di->di_tv.v_type != VAR_LIST)
3060 /* For historic reasons this error is not given for a list or dict.
3061 * E.g., the b: dict could be locked/unlocked. */
3062 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003063 else
3064 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003065 if (lock)
3066 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003067 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003068 di->di_flags &= ~DI_FLAGS_LOCK;
3069 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003070 }
3071 *name_end = cc;
3072 }
3073 else if (lp->ll_range)
3074 {
3075 listitem_T *li = lp->ll_li;
3076
3077 /* (un)lock a range of List items. */
3078 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3079 {
3080 item_lock(&li->li_tv, deep, lock);
3081 li = li->li_next;
3082 ++lp->ll_n1;
3083 }
3084 }
3085 else if (lp->ll_list != NULL)
3086 /* (un)lock a List item. */
3087 item_lock(&lp->ll_li->li_tv, deep, lock);
3088 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003089 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003090 item_lock(&lp->ll_di->di_tv, deep, lock);
3091
3092 return ret;
3093}
3094
3095/*
3096 * Lock or unlock an item. "deep" is nr of levels to go.
3097 */
3098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003100{
3101 static int recurse = 0;
3102 list_T *l;
3103 listitem_T *li;
3104 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003105 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 hashitem_T *hi;
3107 int todo;
3108
3109 if (recurse >= DICT_MAXNEST)
3110 {
3111 EMSG(_("E743: variable nested too deep for (un)lock"));
3112 return;
3113 }
3114 if (deep == 0)
3115 return;
3116 ++recurse;
3117
3118 /* lock/unlock the item itself */
3119 if (lock)
3120 tv->v_lock |= VAR_LOCKED;
3121 else
3122 tv->v_lock &= ~VAR_LOCKED;
3123
3124 switch (tv->v_type)
3125 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003126 case VAR_UNKNOWN:
3127 case VAR_NUMBER:
3128 case VAR_STRING:
3129 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003130 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003131 case VAR_FLOAT:
3132 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003133 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003134 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003135 break;
3136
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003137 case VAR_BLOB:
3138 if ((b = tv->vval.v_blob) != NULL)
3139 {
3140 if (lock)
3141 b->bv_lock |= VAR_LOCKED;
3142 else
3143 b->bv_lock &= ~VAR_LOCKED;
3144 }
3145 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003146 case VAR_LIST:
3147 if ((l = tv->vval.v_list) != NULL)
3148 {
3149 if (lock)
3150 l->lv_lock |= VAR_LOCKED;
3151 else
3152 l->lv_lock &= ~VAR_LOCKED;
3153 if (deep < 0 || deep > 1)
3154 /* recursive: lock/unlock the items the List contains */
3155 for (li = l->lv_first; li != NULL; li = li->li_next)
3156 item_lock(&li->li_tv, deep - 1, lock);
3157 }
3158 break;
3159 case VAR_DICT:
3160 if ((d = tv->vval.v_dict) != NULL)
3161 {
3162 if (lock)
3163 d->dv_lock |= VAR_LOCKED;
3164 else
3165 d->dv_lock &= ~VAR_LOCKED;
3166 if (deep < 0 || deep > 1)
3167 {
3168 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003169 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3171 {
3172 if (!HASHITEM_EMPTY(hi))
3173 {
3174 --todo;
3175 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3176 }
3177 }
3178 }
3179 }
3180 }
3181 --recurse;
3182}
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3185/*
3186 * Delete all "menutrans_" variables.
3187 */
3188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003189del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003192 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003195 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003197 {
3198 if (!HASHITEM_EMPTY(hi))
3199 {
3200 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3202 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003203 }
3204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206}
3207#endif
3208
3209#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3210
3211/*
3212 * Local string buffer for the next two functions to store a variable name
3213 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3214 * get_user_var_name().
3215 */
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217static char_u *varnamebuf = NULL;
3218static int varnamebuflen = 0;
3219
3220/*
3221 * Function to concatenate a prefix and a variable name.
3222 */
3223 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 int len;
3227
3228 len = (int)STRLEN(name) + 3;
3229 if (len > varnamebuflen)
3230 {
3231 vim_free(varnamebuf);
3232 len += 10; /* some additional space */
3233 varnamebuf = alloc(len);
3234 if (varnamebuf == NULL)
3235 {
3236 varnamebuflen = 0;
3237 return NULL;
3238 }
3239 varnamebuflen = len;
3240 }
3241 *varnamebuf = prefix;
3242 varnamebuf[1] = ':';
3243 STRCPY(varnamebuf + 2, name);
3244 return varnamebuf;
3245}
3246
3247/*
3248 * Function given to ExpandGeneric() to obtain the list of user defined
3249 * (global/buffer/window/built-in) variable names.
3250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003254 static long_u gdone;
3255 static long_u bdone;
3256 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003257 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003258 static int vidx;
3259 static hashitem_T *hi;
3260 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003263 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003265 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003266 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003267
3268 /* Global variables */
3269 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003273 else
3274 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 while (HASHITEM_EMPTY(hi))
3276 ++hi;
3277 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3278 return cat_prefix_varname('g', hi->hi_key);
3279 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003281
3282 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003283 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003288 else
3289 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003290 while (HASHITEM_EMPTY(hi))
3291 ++hi;
3292 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003294
3295 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003296 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003299 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003301 else
3302 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003303 while (HASHITEM_EMPTY(hi))
3304 ++hi;
3305 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003307
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003309 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310 if (tdone < ht->ht_used)
3311 {
3312 if (tdone++ == 0)
3313 hi = ht->ht_array;
3314 else
3315 ++hi;
3316 while (HASHITEM_EMPTY(hi))
3317 ++hi;
3318 return cat_prefix_varname('t', hi->hi_key);
3319 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003320
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 /* v: variables */
3322 if (vidx < VV_LEN)
3323 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaard23a8232018-02-10 18:45:26 +01003325 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 varnamebuflen = 0;
3327 return NULL;
3328}
3329
3330#endif /* FEAT_CMDL_COMPL */
3331
3332/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003333 * Return TRUE if "pat" matches "text".
3334 * Does not use 'cpo' and always uses 'magic'.
3335 */
3336 static int
3337pattern_match(char_u *pat, char_u *text, int ic)
3338{
3339 int matches = FALSE;
3340 char_u *save_cpo;
3341 regmatch_T regmatch;
3342
3343 /* avoid 'l' flag in 'cpoptions' */
3344 save_cpo = p_cpo;
3345 p_cpo = (char_u *)"";
3346 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3347 if (regmatch.regprog != NULL)
3348 {
3349 regmatch.rm_ic = ic;
3350 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3351 vim_regfree(regmatch.regprog);
3352 }
3353 p_cpo = save_cpo;
3354 return matches;
3355}
3356
3357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3361 */
3362
3363/*
3364 * Handle zero level expression.
3365 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003367 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Return OK or FAIL.
3369 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371eval0(
3372 char_u *arg,
3373 typval_T *rettv,
3374 char_u **nextcmd,
3375 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 int ret;
3378 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003379 int did_emsg_before = did_emsg;
3380 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (ret == FAIL || !ends_excmd(*p))
3385 {
3386 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 /*
3389 * Report the invalid expression unless the expression evaluation has
3390 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003391 * exception, or we already gave a more specific error.
3392 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003394 if (!aborting() && did_emsg == did_emsg_before
3395 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 EMSG2(_(e_invexpr2), arg);
3397 ret = FAIL;
3398 }
3399 if (nextcmd != NULL)
3400 *nextcmd = check_nextcmd(p);
3401
3402 return ret;
3403}
3404
3405/*
3406 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003407 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 *
3409 * "arg" must point to the first non-white of the expression.
3410 * "arg" is advanced to the next non-white after the recognized expression.
3411 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003412 * Note: "rettv.v_lock" is not set.
3413 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Return OK or FAIL.
3415 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003417eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003420 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /*
3423 * Get the first variable.
3424 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 return FAIL;
3427
3428 if ((*arg)[0] == '?')
3429 {
3430 result = FALSE;
3431 if (evaluate)
3432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 int error = FALSE;
3434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003435 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003438 if (error)
3439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441
3442 /*
3443 * Get the second variable.
3444 */
3445 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return FAIL;
3448
3449 /*
3450 * Check for the ":".
3451 */
3452 if ((*arg)[0] != ':')
3453 {
3454 EMSG(_("E109: Missing ':' after '?'"));
3455 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 return FAIL;
3458 }
3459
3460 /*
3461 * Get the third variable.
3462 */
3463 *arg = skipwhite(*arg + 1);
3464 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3465 {
3466 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return FAIL;
3469 }
3470 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473
3474 return OK;
3475}
3476
3477/*
3478 * Handle first level expression:
3479 * expr2 || expr2 || expr2 logical OR
3480 *
3481 * "arg" must point to the first non-white of the expression.
3482 * "arg" is advanced to the next non-white after the recognized expression.
3483 *
3484 * Return OK or FAIL.
3485 */
3486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003487eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 long result;
3491 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 /*
3495 * Get the first variable.
3496 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return FAIL;
3499
3500 /*
3501 * Repeat until there is no following "||".
3502 */
3503 first = TRUE;
3504 result = FALSE;
3505 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3506 {
3507 if (evaluate && first)
3508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003511 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003512 if (error)
3513 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 first = FALSE;
3515 }
3516
3517 /*
3518 * Get the second variable.
3519 */
3520 *arg = skipwhite(*arg + 2);
3521 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3522 return FAIL;
3523
3524 /*
3525 * Compute the result.
3526 */
3527 if (evaluate && !result)
3528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 if (error)
3533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 if (evaluate)
3536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 rettv->v_type = VAR_NUMBER;
3538 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540 }
3541
3542 return OK;
3543}
3544
3545/*
3546 * Handle second level expression:
3547 * expr3 && expr3 && expr3 logical AND
3548 *
3549 * "arg" must point to the first non-white of the expression.
3550 * "arg" is advanced to the next non-white after the recognized expression.
3551 *
3552 * Return OK or FAIL.
3553 */
3554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 long result;
3559 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 /*
3563 * Get the first variable.
3564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 return FAIL;
3567
3568 /*
3569 * Repeat until there is no following "&&".
3570 */
3571 first = TRUE;
3572 result = TRUE;
3573 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3574 {
3575 if (evaluate && first)
3576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003577 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 if (error)
3581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 first = FALSE;
3583 }
3584
3585 /*
3586 * Get the second variable.
3587 */
3588 *arg = skipwhite(*arg + 2);
3589 if (eval4(arg, &var2, evaluate && result) == FAIL)
3590 return FAIL;
3591
3592 /*
3593 * Compute the result.
3594 */
3595 if (evaluate && result)
3596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 if (error)
3601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 if (evaluate)
3604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003605 rettv->v_type = VAR_NUMBER;
3606 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 }
3609
3610 return OK;
3611}
3612
3613/*
3614 * Handle third level expression:
3615 * var1 == var2
3616 * var1 =~ var2
3617 * var1 != var2
3618 * var1 !~ var2
3619 * var1 > var2
3620 * var1 >= var2
3621 * var1 < var2
3622 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003623 * var1 is var2
3624 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 *
3626 * "arg" must point to the first non-white of the expression.
3627 * "arg" is advanced to the next non-white after the recognized expression.
3628 *
3629 * Return OK or FAIL.
3630 */
3631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 char_u *p;
3636 int i;
3637 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003638 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
3643 * Get the first variable.
3644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003645 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return FAIL;
3647
3648 p = *arg;
3649 switch (p[0])
3650 {
3651 case '=': if (p[1] == '=')
3652 type = TYPE_EQUAL;
3653 else if (p[1] == '~')
3654 type = TYPE_MATCH;
3655 break;
3656 case '!': if (p[1] == '=')
3657 type = TYPE_NEQUAL;
3658 else if (p[1] == '~')
3659 type = TYPE_NOMATCH;
3660 break;
3661 case '>': if (p[1] != '=')
3662 {
3663 type = TYPE_GREATER;
3664 len = 1;
3665 }
3666 else
3667 type = TYPE_GEQUAL;
3668 break;
3669 case '<': if (p[1] != '=')
3670 {
3671 type = TYPE_SMALLER;
3672 len = 1;
3673 }
3674 else
3675 type = TYPE_SEQUAL;
3676 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003677 case 'i': if (p[1] == 's')
3678 {
3679 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3680 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003681 i = p[len];
3682 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003683 {
3684 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3685 type_is = TRUE;
3686 }
3687 }
3688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003692 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
3694 if (type != TYPE_UNKNOWN)
3695 {
3696 /* extra question mark appended: ignore case */
3697 if (p[len] == '?')
3698 {
3699 ic = TRUE;
3700 ++len;
3701 }
3702 /* extra '#' appended: match case */
3703 else if (p[len] == '#')
3704 {
3705 ic = FALSE;
3706 ++len;
3707 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003708 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else
3710 ic = p_ic;
3711
3712 /*
3713 * Get the second variable.
3714 */
3715 *arg = skipwhite(p + len);
3716 if (eval5(arg, &var2, evaluate) == FAIL)
3717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003721 if (evaluate)
3722 {
3723 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3724
3725 clear_tv(&var2);
3726 return ret;
3727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
3730 return OK;
3731}
3732
3733/*
3734 * Handle fourth level expression:
3735 * + number addition
3736 * - number subtraction
3737 * . string concatenation
3738 *
3739 * "arg" must point to the first non-white of the expression.
3740 * "arg" is advanced to the next non-white after the recognized expression.
3741 *
3742 * Return OK or FAIL.
3743 */
3744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 typval_T var2;
3748 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003750 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003751#ifdef FEAT_FLOAT
3752 float_T f1 = 0, f2 = 0;
3753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 char_u *s1, *s2;
3755 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3756 char_u *p;
3757
3758 /*
3759 * Get the first variable.
3760 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003761 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return FAIL;
3763
3764 /*
3765 * Repeat computing, until no '+', '-' or '.' is following.
3766 */
3767 for (;;)
3768 {
3769 op = **arg;
3770 if (op != '+' && op != '-' && op != '.')
3771 break;
3772
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003773 if ((op != '+' || (rettv->v_type != VAR_LIST
3774 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775#ifdef FEAT_FLOAT
3776 && (op == '.' || rettv->v_type != VAR_FLOAT)
3777#endif
3778 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 {
3780 /* For "list + ...", an illegal use of the first operand as
3781 * a number cannot be determined before evaluating the 2nd
3782 * operand: if this is also a list, all is ok.
3783 * For "something . ...", "something - ..." or "non-list + ...",
3784 * we know that the first operand needs to be a string or number
3785 * without evaluating the 2nd operand. So check before to avoid
3786 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 {
3789 clear_tv(rettv);
3790 return FAIL;
3791 }
3792 }
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 /*
3795 * Get the second variable.
3796 */
3797 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003798 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802 }
3803
3804 if (evaluate)
3805 {
3806 /*
3807 * Compute the result.
3808 */
3809 if (op == '.')
3810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003811 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3812 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 if (s2 == NULL) /* type error ? */
3814 {
3815 clear_tv(rettv);
3816 clear_tv(&var2);
3817 return FAIL;
3818 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003819 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
3821 rettv->v_type = VAR_STRING;
3822 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003824 else if (op == '+' && rettv->v_type == VAR_BLOB
3825 && var2.v_type == VAR_BLOB)
3826 {
3827 blob_T *b1 = rettv->vval.v_blob;
3828 blob_T *b2 = var2.vval.v_blob;
3829 blob_T *b = blob_alloc();
3830 int i;
3831
3832 if (b != NULL)
3833 {
3834 for (i = 0; i < blob_len(b1); i++)
3835 ga_append(&b->bv_ga, blob_get(b1, i));
3836 for (i = 0; i < blob_len(b2); i++)
3837 ga_append(&b->bv_ga, blob_get(b2, i));
3838
3839 clear_tv(rettv);
3840 rettv_blob_set(rettv, b);
3841 }
3842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003843 else if (op == '+' && rettv->v_type == VAR_LIST
3844 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003845 {
3846 /* concatenate Lists */
3847 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3848 &var3) == FAIL)
3849 {
3850 clear_tv(rettv);
3851 clear_tv(&var2);
3852 return FAIL;
3853 }
3854 clear_tv(rettv);
3855 *rettv = var3;
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else
3858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 int error = FALSE;
3860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861#ifdef FEAT_FLOAT
3862 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864 f1 = rettv->vval.v_float;
3865 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 else
3868#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003870 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 if (error)
3872 {
3873 /* This can only happen for "list + non-list". For
3874 * "non-list + ..." or "something - ...", we returned
3875 * before evaluating the 2nd operand. */
3876 clear_tv(rettv);
3877 return FAIL;
3878 }
3879#ifdef FEAT_FLOAT
3880 if (var2.v_type == VAR_FLOAT)
3881 f1 = n1;
3882#endif
3883 }
3884#ifdef FEAT_FLOAT
3885 if (var2.v_type == VAR_FLOAT)
3886 {
3887 f2 = var2.vval.v_float;
3888 n2 = 0;
3889 }
3890 else
3891#endif
3892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003894 if (error)
3895 {
3896 clear_tv(rettv);
3897 clear_tv(&var2);
3898 return FAIL;
3899 }
3900#ifdef FEAT_FLOAT
3901 if (rettv->v_type == VAR_FLOAT)
3902 f2 = n2;
3903#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003906
3907#ifdef FEAT_FLOAT
3908 /* If there is a float on either side the result is a float. */
3909 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3910 {
3911 if (op == '+')
3912 f1 = f1 + f2;
3913 else
3914 f1 = f1 - f2;
3915 rettv->v_type = VAR_FLOAT;
3916 rettv->vval.v_float = f1;
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003919#endif
3920 {
3921 if (op == '+')
3922 n1 = n1 + n2;
3923 else
3924 n1 = n1 - n2;
3925 rettv->v_type = VAR_NUMBER;
3926 rettv->vval.v_number = n1;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003929 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 }
3932 return OK;
3933}
3934
3935/*
3936 * Handle fifth level expression:
3937 * * number multiplication
3938 * / number division
3939 * % number modulo
3940 *
3941 * "arg" must point to the first non-white of the expression.
3942 * "arg" is advanced to the next non-white after the recognized expression.
3943 *
3944 * Return OK or FAIL.
3945 */
3946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003947eval6(
3948 char_u **arg,
3949 typval_T *rettv,
3950 int evaluate,
3951 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003955 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003956#ifdef FEAT_FLOAT
3957 int use_float = FALSE;
3958 float_T f1 = 0, f2;
3959#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003960 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /*
3963 * Get the first variable.
3964 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003965 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FAIL;
3967
3968 /*
3969 * Repeat computing, until no '*', '/' or '%' is following.
3970 */
3971 for (;;)
3972 {
3973 op = **arg;
3974 if (op != '*' && op != '/' && op != '%')
3975 break;
3976
3977 if (evaluate)
3978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003979#ifdef FEAT_FLOAT
3980 if (rettv->v_type == VAR_FLOAT)
3981 {
3982 f1 = rettv->vval.v_float;
3983 use_float = TRUE;
3984 n1 = 0;
3985 }
3986 else
3987#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003988 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 if (error)
3991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
3994 n1 = 0;
3995
3996 /*
3997 * Get the second variable.
3998 */
3999 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004000 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return FAIL;
4002
4003 if (evaluate)
4004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004005#ifdef FEAT_FLOAT
4006 if (var2.v_type == VAR_FLOAT)
4007 {
4008 if (!use_float)
4009 {
4010 f1 = n1;
4011 use_float = TRUE;
4012 }
4013 f2 = var2.vval.v_float;
4014 n2 = 0;
4015 }
4016 else
4017#endif
4018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004020 clear_tv(&var2);
4021 if (error)
4022 return FAIL;
4023#ifdef FEAT_FLOAT
4024 if (use_float)
4025 f2 = n2;
4026#endif
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
4030 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004031 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004033#ifdef FEAT_FLOAT
4034 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004036 if (op == '*')
4037 f1 = f1 * f2;
4038 else if (op == '/')
4039 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004040# ifdef VMS
4041 /* VMS crashes on divide by zero, work around it */
4042 if (f2 == 0.0)
4043 {
4044 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004045 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004047 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004049 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004050 }
4051 else
4052 f1 = f1 / f2;
4053# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004054 /* We rely on the floating point library to handle divide
4055 * by zero to result in "inf" and not a crash. */
4056 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004057# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004061 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 return FAIL;
4063 }
4064 rettv->v_type = VAR_FLOAT;
4065 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070 if (op == '*')
4071 n1 = n1 * n2;
4072 else if (op == '/')
4073 {
4074 if (n2 == 0) /* give an error message? */
4075 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004076 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004077 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004078 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004079 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004080 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004081 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 }
4083 else
4084 n1 = n1 / n2;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 {
4088 if (n2 == 0) /* give an error message? */
4089 n1 = 0;
4090 else
4091 n1 = n1 % n2;
4092 }
4093 rettv->v_type = VAR_NUMBER;
4094 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 }
4098
4099 return OK;
4100}
4101
4102/*
4103 * Handle sixth level expression:
4104 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004105 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004106 * "string" string constant
4107 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * &option-name option value
4109 * @r register contents
4110 * identifier variable value
4111 * function() function call
4112 * $VAR environment variable
4113 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004114 * [expr, expr] List
4115 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * Also handle:
4118 * ! in front logical NOT
4119 * - in front unary minus
4120 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 * trailing [] subscript in String or List
4122 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * "arg" must point to the first non-white of the expression.
4125 * "arg" is advanced to the next non-white after the recognized expression.
4126 *
4127 * Return OK or FAIL.
4128 */
4129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004130eval7(
4131 char_u **arg,
4132 typval_T *rettv,
4133 int evaluate,
4134 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004136 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 int len;
4138 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u *start_leader, *end_leader;
4140 int ret = OK;
4141 char_u *alias;
4142
4143 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004150 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 */
4152 start_leader = *arg;
4153 while (**arg == '!' || **arg == '-' || **arg == '+')
4154 *arg = skipwhite(*arg + 1);
4155 end_leader = *arg;
4156
4157 switch (**arg)
4158 {
4159 /*
4160 * Number constant.
4161 */
4162 case '0':
4163 case '1':
4164 case '2':
4165 case '3':
4166 case '4':
4167 case '5':
4168 case '6':
4169 case '7':
4170 case '8':
4171 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004172 {
4173#ifdef FEAT_FLOAT
4174 char_u *p = skipdigits(*arg + 1);
4175 int get_float = FALSE;
4176
4177 /* We accept a float when the format matches
4178 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004179 * strict to avoid backwards compatibility problems.
4180 * Don't look for a float after the "." operator, so that
4181 * ":let vers = 1.2.3" doesn't fail. */
4182 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004184 get_float = TRUE;
4185 p = skipdigits(p + 2);
4186 if (*p == 'e' || *p == 'E')
4187 {
4188 ++p;
4189 if (*p == '-' || *p == '+')
4190 ++p;
4191 if (!vim_isdigit(*p))
4192 get_float = FALSE;
4193 else
4194 p = skipdigits(p + 1);
4195 }
4196 if (ASCII_ISALPHA(*p) || *p == '.')
4197 get_float = FALSE;
4198 }
4199 if (get_float)
4200 {
4201 float_T f;
4202
4203 *arg += string2float(*arg, &f);
4204 if (evaluate)
4205 {
4206 rettv->v_type = VAR_FLOAT;
4207 rettv->vval.v_float = f;
4208 }
4209 }
4210 else
4211#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004212 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004213 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004214 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004215 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004216
4217 // Blob constant: 0z0123456789abcdef
4218 if (evaluate)
4219 blob = blob_alloc();
4220 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4221 {
4222 if (!vim_isxdigit(bp[1]))
4223 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004224 if (blob != NULL)
4225 {
4226 EMSG(_("E973: Blob literal should have an even number of hex characters"));
4227 ga_clear(&blob->bv_ga);
4228 VIM_CLEAR(blob);
4229 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004230 ret = FAIL;
4231 break;
4232 }
4233 if (blob != NULL)
4234 ga_append(&blob->bv_ga,
4235 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4236 }
4237 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004238 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004239 *arg = bp;
4240 }
4241 else
4242 {
4243 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004244 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 *arg += len;
4246 if (evaluate)
4247 {
4248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = n;
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /*
4256 * String constant: "string".
4257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 break;
4260
4261 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 break;
4266
4267 /*
4268 * List: [expr, expr]
4269 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272
4273 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004274 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004275 * Dictionary: {key: val, key: val}
4276 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004277 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4278 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004279 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004280 break;
4281
4282 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004283 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004285 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 break;
4287
4288 /*
4289 * Environment variable: $VAR.
4290 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293
4294 /*
4295 * Register contents: @r.
4296 */
4297 case '@': ++*arg;
4298 if (evaluate)
4299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004301 rettv->vval.v_string = get_reg_contents(**arg,
4302 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 if (**arg != NUL)
4305 ++*arg;
4306 break;
4307
4308 /*
4309 * nested expression: (expression).
4310 */
4311 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (**arg == ')')
4314 ++*arg;
4315 else if (ret == OK)
4316 {
4317 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ret = FAIL;
4320 }
4321 break;
4322
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326
4327 if (ret == NOTDONE)
4328 {
4329 /*
4330 * Must be a variable or function name.
4331 * Can also be a curly-braces kind of name: {expr}.
4332 */
4333 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004334 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335 if (alias != NULL)
4336 s = alias;
4337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004338 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 ret = FAIL;
4340 else
4341 {
4342 if (**arg == '(') /* recursive! */
4343 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004344 partial_T *partial;
4345
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004346 if (!evaluate)
4347 check_vars(s, len);
4348
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 /* If "s" is the name of a variable of type VAR_FUNC
4350 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004351 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004353 /* Need to make a copy, in case evaluating the arguments makes
4354 * the name invalid. */
4355 s = vim_strsave(s);
4356 if (s == NULL)
4357 ret = FAIL;
4358 else
4359 /* Invoke the function. */
4360 ret = get_func_tv(s, len, rettv, arg,
4361 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4362 &len, evaluate, partial, NULL);
4363 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004364
4365 /* If evaluate is FALSE rettv->v_type was not set in
4366 * get_func_tv, but it's needed in handle_subscript() to parse
4367 * what follows. So set it here. */
4368 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4369 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004370 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004371 rettv->v_type = VAR_FUNC;
4372 }
4373
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 /* Stop the expression evaluation when immediately
4375 * aborting on error, or when an interrupt occurred or
4376 * an exception was thrown but not caught. */
4377 if (aborting())
4378 {
4379 if (ret == OK)
4380 clear_tv(rettv);
4381 ret = FAIL;
4382 }
4383 }
4384 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004385 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004386 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004387 {
4388 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004389 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004391 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004392 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393 }
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 *arg = skipwhite(*arg);
4396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004397 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4398 * expr(expr). */
4399 if (ret == OK)
4400 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /*
4403 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4404 */
4405 if (ret == OK && evaluate && end_leader > start_leader)
4406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004408 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004409#ifdef FEAT_FLOAT
4410 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412 if (rettv->v_type == VAR_FLOAT)
4413 f = rettv->vval.v_float;
4414 else
4415#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004416 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 clear_tv(rettv);
4420 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 else
4423 {
4424 while (end_leader > start_leader)
4425 {
4426 --end_leader;
4427 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428 {
4429#ifdef FEAT_FLOAT
4430 if (rettv->v_type == VAR_FLOAT)
4431 f = !f;
4432 else
4433#endif
4434 val = !val;
4435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004436 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 {
4438#ifdef FEAT_FLOAT
4439 if (rettv->v_type == VAR_FLOAT)
4440 f = -f;
4441 else
4442#endif
4443 val = -val;
4444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004446#ifdef FEAT_FLOAT
4447 if (rettv->v_type == VAR_FLOAT)
4448 {
4449 clear_tv(rettv);
4450 rettv->vval.v_float = f;
4451 }
4452 else
4453#endif
4454 {
4455 clear_tv(rettv);
4456 rettv->v_type = VAR_NUMBER;
4457 rettv->vval.v_number = val;
4458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 return ret;
4463}
4464
4465/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004466 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4467 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4469 */
4470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004471eval_index(
4472 char_u **arg,
4473 typval_T *rettv,
4474 int evaluate,
4475 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476{
4477 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004479 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 long len = -1;
4482 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485
Bram Moolenaara03f2332016-02-06 18:09:59 +01004486 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004488 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004489 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004490 if (verbose)
4491 EMSG(_("E695: Cannot index a Funcref"));
4492 return FAIL;
4493 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004494#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004495 if (verbose)
4496 EMSG(_(e_float_as_string));
4497 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004498#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004499 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004500 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004501 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004502 if (verbose)
4503 EMSG(_("E909: Cannot index a special variable"));
4504 return FAIL;
4505 case VAR_UNKNOWN:
4506 if (evaluate)
4507 return FAIL;
4508 /* FALLTHROUGH */
4509
4510 case VAR_STRING:
4511 case VAR_NUMBER:
4512 case VAR_LIST:
4513 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004514 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004518 init_tv(&var1);
4519 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 /*
4523 * dict.name
4524 */
4525 key = *arg + 1;
4526 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4527 ;
4528 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 }
4532 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 /*
4535 * something[idx]
4536 *
4537 * Get the (first) variable from inside the [].
4538 */
4539 *arg = skipwhite(*arg + 1);
4540 if (**arg == ':')
4541 empty1 = TRUE;
4542 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4543 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004544 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 {
4546 /* not a number or string */
4547 clear_tv(&var1);
4548 return FAIL;
4549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550
4551 /*
4552 * Get the second variable from inside the [:].
4553 */
4554 if (**arg == ':')
4555 {
4556 range = TRUE;
4557 *arg = skipwhite(*arg + 1);
4558 if (**arg == ']')
4559 empty2 = TRUE;
4560 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 if (!empty1)
4563 clear_tv(&var1);
4564 return FAIL;
4565 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004566 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 {
4568 /* not a number or string */
4569 if (!empty1)
4570 clear_tv(&var1);
4571 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 return FAIL;
4573 }
4574 }
4575
4576 /* Check for the ']'. */
4577 if (**arg != ']')
4578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004579 if (verbose)
4580 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 clear_tv(&var1);
4582 if (range)
4583 clear_tv(&var2);
4584 return FAIL;
4585 }
4586 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 }
4588
4589 if (evaluate)
4590 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 n1 = 0;
4592 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 }
4597 if (range)
4598 {
4599 if (empty2)
4600 n2 = -1;
4601 else
4602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004603 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 }
4606 }
4607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004610 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004611 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004612 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004613 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004614 case VAR_SPECIAL:
4615 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004616 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004617 break; /* not evaluating, skipping over subscript */
4618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 case VAR_NUMBER:
4620 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 len = (long)STRLEN(s);
4623 if (range)
4624 {
4625 /* The resulting variable is a substring. If the indexes
4626 * are out of range the result is empty. */
4627 if (n1 < 0)
4628 {
4629 n1 = len + n1;
4630 if (n1 < 0)
4631 n1 = 0;
4632 }
4633 if (n2 < 0)
4634 n2 = len + n2;
4635 else if (n2 >= len)
4636 n2 = len;
4637 if (n1 >= len || n2 < 0 || n1 > n2)
4638 s = NULL;
4639 else
4640 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4641 }
4642 else
4643 {
4644 /* The resulting variable is a string of a single
4645 * character. If the index is too big or negative the
4646 * result is empty. */
4647 if (n1 >= len || n1 < 0)
4648 s = NULL;
4649 else
4650 s = vim_strnsave(s + n1, 1);
4651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(rettv);
4653 rettv->v_type = VAR_STRING;
4654 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 break;
4656
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004657 case VAR_BLOB:
4658 len = blob_len(rettv->vval.v_blob);
4659 if (range)
4660 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004661 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004662 // are out of range the result is empty.
4663 if (n1 < 0)
4664 {
4665 n1 = len + n1;
4666 if (n1 < 0)
4667 n1 = 0;
4668 }
4669 if (n2 < 0)
4670 n2 = len + n2;
4671 else if (n2 >= len)
4672 n2 = len - 1;
4673 if (n1 >= len || n2 < 0 || n1 > n2)
4674 {
4675 clear_tv(rettv);
4676 rettv->v_type = VAR_BLOB;
4677 rettv->vval.v_blob = NULL;
4678 }
4679 else
4680 {
4681 blob_T *blob = blob_alloc();
4682
4683 if (blob != NULL)
4684 {
4685 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4686 {
4687 blob_free(blob);
4688 return FAIL;
4689 }
4690 blob->bv_ga.ga_len = n2 - n1 + 1;
4691 for (i = n1; i <= n2; i++)
4692 blob_set(blob, i - n1,
4693 blob_get(rettv->vval.v_blob, i));
4694
4695 clear_tv(rettv);
4696 rettv_blob_set(rettv, blob);
4697 }
4698 }
4699 }
4700 else
4701 {
4702 // The resulting variable is a string of a single
4703 // character. If the index is too big or negative the
4704 // result is empty.
4705 if (n1 < len && n1 >= 0)
4706 {
4707 int v = (int)blob_get(rettv->vval.v_blob, n1);
4708
4709 clear_tv(rettv);
4710 rettv->v_type = VAR_NUMBER;
4711 rettv->vval.v_number = v;
4712 }
4713 else
4714 EMSGN(_(e_blobidx), n1);
4715 }
4716 break;
4717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 if (n1 < 0)
4721 n1 = len + n1;
4722 if (!empty1 && (n1 < 0 || n1 >= len))
4723 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004724 /* For a range we allow invalid values and return an empty
4725 * list. A list index out of range is an error. */
4726 if (!range)
4727 {
4728 if (verbose)
4729 EMSGN(_(e_listidx), n1);
4730 return FAIL;
4731 }
4732 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 }
4734 if (range)
4735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 list_T *l;
4737 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738
4739 if (n2 < 0)
4740 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004741 else if (n2 >= len)
4742 n2 = len - 1;
4743 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004744 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 l = list_alloc();
4746 if (l == NULL)
4747 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 n1 <= n2; ++n1)
4750 {
4751 if (list_append_tv(l, &item->li_tv) == FAIL)
4752 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004753 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 return FAIL;
4755 }
4756 item = item->li_next;
4757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004759 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761 else
4762 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004763 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 }
4767 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768
4769 case VAR_DICT:
4770 if (range)
4771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 if (verbose)
4773 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (len == -1)
4775 clear_tv(&var1);
4776 return FAIL;
4777 }
4778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780
4781 if (len == -1)
4782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004784 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 clear_tv(&var1);
4787 return FAIL;
4788 }
4789 }
4790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004791 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004793 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795 if (len == -1)
4796 clear_tv(&var1);
4797 if (item == NULL)
4798 return FAIL;
4799
4800 copy_tv(&item->di_tv, &var1);
4801 clear_tv(rettv);
4802 *rettv = var1;
4803 }
4804 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806 }
4807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 return OK;
4809}
4810
4811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 * Get an option value.
4813 * "arg" points to the '&' or '+' before the option name.
4814 * "arg" is advanced to character after the option name.
4815 * Return OK or FAIL.
4816 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004818get_option_tv(
4819 char_u **arg,
4820 typval_T *rettv, /* when NULL, only check if option exists */
4821 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 char_u *option_end;
4824 long numval;
4825 char_u *stringval;
4826 int opt_type;
4827 int c;
4828 int working = (**arg == '+'); /* has("+option") */
4829 int ret = OK;
4830 int opt_flags;
4831
4832 /*
4833 * Isolate the option name and find its value.
4834 */
4835 option_end = find_option_end(arg, &opt_flags);
4836 if (option_end == NULL)
4837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 EMSG2(_("E112: Option name missing: %s"), *arg);
4840 return FAIL;
4841 }
4842
4843 if (!evaluate)
4844 {
4845 *arg = option_end;
4846 return OK;
4847 }
4848
4849 c = *option_end;
4850 *option_end = NUL;
4851 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 if (opt_type == -3) /* invalid name */
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 EMSG2(_("E113: Unknown option: %s"), *arg);
4858 ret = FAIL;
4859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 if (opt_type == -2) /* hidden string option */
4863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 rettv->v_type = VAR_STRING;
4865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 else if (opt_type == -1) /* hidden number option */
4868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 rettv->v_type = VAR_NUMBER;
4870 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 else if (opt_type == 1) /* number option */
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 rettv->v_type = VAR_NUMBER;
4875 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 else /* string option */
4878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 rettv->v_type = VAR_STRING;
4880 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 }
4883 else if (working && (opt_type == -2 || opt_type == -1))
4884 ret = FAIL;
4885
4886 *option_end = c; /* put back for error messages */
4887 *arg = option_end;
4888
4889 return ret;
4890}
4891
4892/*
4893 * Allocate a variable for a string constant.
4894 * Return OK or FAIL.
4895 */
4896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004897get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 char_u *p;
4900 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int extra = 0;
4902
4903 /*
4904 * Find the end of the string, skipping backslashed characters.
4905 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004906 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 if (*p == '\\' && p[1] != NUL)
4909 {
4910 ++p;
4911 /* A "\<x>" form occupies at least 4 characters, and produces up
4912 * to 6 characters: reserve space for 2 extra */
4913 if (*p == '<')
4914 extra += 2;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917
4918 if (*p != '"')
4919 {
4920 EMSG2(_("E114: Missing quote: %s"), *arg);
4921 return FAIL;
4922 }
4923
4924 /* If only parsing, set *arg and return here */
4925 if (!evaluate)
4926 {
4927 *arg = p + 1;
4928 return OK;
4929 }
4930
4931 /*
4932 * Copy the string into allocated memory, handling backslashed
4933 * characters.
4934 */
4935 name = alloc((unsigned)(p - *arg + extra));
4936 if (name == NULL)
4937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 rettv->v_type = VAR_STRING;
4939 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 if (*p == '\\')
4944 {
4945 switch (*++p)
4946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 case 'b': *name++ = BS; ++p; break;
4948 case 'e': *name++ = ESC; ++p; break;
4949 case 'f': *name++ = FF; ++p; break;
4950 case 'n': *name++ = NL; ++p; break;
4951 case 'r': *name++ = CAR; ++p; break;
4952 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 case 'X': /* hex: "\x1", "\x12" */
4955 case 'x':
4956 case 'u': /* Unicode: "\u0023" */
4957 case 'U':
4958 if (vim_isxdigit(p[1]))
4959 {
4960 int n, nr;
4961 int c = toupper(*p);
4962
4963 if (c == 'X')
4964 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004965 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004967 else
4968 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 nr = 0;
4970 while (--n >= 0 && vim_isxdigit(p[1]))
4971 {
4972 ++p;
4973 nr = (nr << 4) + hex2nr(*p);
4974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#ifdef FEAT_MBYTE
4977 /* For "\u" store the number according to
4978 * 'encoding'. */
4979 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
4982#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
4986
4987 /* octal: "\1", "\12", "\123" */
4988 case '0':
4989 case '1':
4990 case '2':
4991 case '3':
4992 case '4':
4993 case '5':
4994 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 case '7': *name = *p++ - '0';
4996 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 *name = (*name << 3) + *p++ - '0';
4999 if (*p >= '0' && *p <= '7')
5000 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004
5005 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005006 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (extra != 0)
5008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 break;
5011 }
5012 /* FALLTHROUGH */
5013
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 break;
5016 }
5017 }
5018 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005023 if (*p != NUL) /* just in case */
5024 ++p;
5025 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 return OK;
5028}
5029
5030/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * Return OK or FAIL.
5033 */
5034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005035get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005038 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 int reduce = 0;
5040
5041 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005044 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 break;
5050 ++reduce;
5051 ++p;
5052 }
5053 }
5054
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 return FAIL;
5059 }
5060
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 if (!evaluate)
5063 {
5064 *arg = p + 1;
5065 return OK;
5066 }
5067
5068 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 */
5071 str = alloc((unsigned)((p - *arg) - reduce));
5072 if (str == NULL)
5073 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 rettv->v_type = VAR_STRING;
5075 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 break;
5083 ++p;
5084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 *arg = p + 1;
5089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 return OK;
5091}
5092
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005093/*
5094 * Return the function name of the partial.
5095 */
5096 char_u *
5097partial_name(partial_T *pt)
5098{
5099 if (pt->pt_name != NULL)
5100 return pt->pt_name;
5101 return pt->pt_func->uf_name;
5102}
5103
Bram Moolenaarddecc252016-04-06 22:59:37 +02005104 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005106{
5107 int i;
5108
5109 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005111 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005113 if (pt->pt_name != NULL)
5114 {
5115 func_unref(pt->pt_name);
5116 vim_free(pt->pt_name);
5117 }
5118 else
5119 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005120 vim_free(pt);
5121}
5122
5123/*
5124 * Unreference a closure: decrement the reference count and free it when it
5125 * becomes zero.
5126 */
5127 void
5128partial_unref(partial_T *pt)
5129{
5130 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005131 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005132}
5133
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005134static int tv_equal_recurse_limit;
5135
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005136 static int
5137func_equal(
5138 typval_T *tv1,
5139 typval_T *tv2,
5140 int ic) /* ignore case */
5141{
5142 char_u *s1, *s2;
5143 dict_T *d1, *d2;
5144 int a1, a2;
5145 int i;
5146
5147 /* empty and NULL function name considered the same */
5148 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005149 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005150 if (s1 != NULL && *s1 == NUL)
5151 s1 = NULL;
5152 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005153 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005154 if (s2 != NULL && *s2 == NUL)
5155 s2 = NULL;
5156 if (s1 == NULL || s2 == NULL)
5157 {
5158 if (s1 != s2)
5159 return FALSE;
5160 }
5161 else if (STRCMP(s1, s2) != 0)
5162 return FALSE;
5163
5164 /* empty dict and NULL dict is different */
5165 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5166 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5167 if (d1 == NULL || d2 == NULL)
5168 {
5169 if (d1 != d2)
5170 return FALSE;
5171 }
5172 else if (!dict_equal(d1, d2, ic, TRUE))
5173 return FALSE;
5174
5175 /* empty list and no list considered the same */
5176 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5177 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5178 if (a1 != a2)
5179 return FALSE;
5180 for (i = 0; i < a1; ++i)
5181 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5182 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5183 return FALSE;
5184
5185 return TRUE;
5186}
5187
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005188/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005190 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005191 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005194tv_equal(
5195 typval_T *tv1,
5196 typval_T *tv2,
5197 int ic, /* ignore case */
5198 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005199{
5200 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005202 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005203 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005204
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005205 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005206 * recursiveness to a limit. We guess they are equal then.
5207 * A fixed limit has the problem of still taking an awful long time.
5208 * Reduce the limit every time running into it. That should work fine for
5209 * deeply linked structures that are not recursively linked and catch
5210 * recursiveness quickly. */
5211 if (!recursive)
5212 tv_equal_recurse_limit = 1000;
5213 if (recursive_cnt >= tv_equal_recurse_limit)
5214 {
5215 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005216 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005217 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005218
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005219 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5220 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005221 if ((tv1->v_type == VAR_FUNC
5222 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5223 && (tv2->v_type == VAR_FUNC
5224 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5225 {
5226 ++recursive_cnt;
5227 r = func_equal(tv1, tv2, ic);
5228 --recursive_cnt;
5229 return r;
5230 }
5231
5232 if (tv1->v_type != tv2->v_type)
5233 return FALSE;
5234
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005235 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005236 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005237 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005238 ++recursive_cnt;
5239 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005242
5243 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005244 ++recursive_cnt;
5245 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5246 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005247 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005248
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005249 case VAR_BLOB:
5250 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5251
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005252 case VAR_NUMBER:
5253 return tv1->vval.v_number == tv2->vval.v_number;
5254
5255 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005256 s1 = tv_get_string_buf(tv1, buf1);
5257 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005258 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005259
5260 case VAR_SPECIAL:
5261 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005262
5263 case VAR_FLOAT:
5264#ifdef FEAT_FLOAT
5265 return tv1->vval.v_float == tv2->vval.v_float;
5266#endif
5267 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005269 return tv1->vval.v_job == tv2->vval.v_job;
5270#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005273 return tv1->vval.v_channel == tv2->vval.v_channel;
5274#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005275 case VAR_FUNC:
5276 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005277 case VAR_UNKNOWN:
5278 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005279 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005280
Bram Moolenaara03f2332016-02-06 18:09:59 +01005281 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5282 * does not equal anything, not even itself. */
5283 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005284}
5285
5286/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005287 * Return the next (unique) copy ID.
5288 * Used for serializing nested structures.
5289 */
5290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005292{
5293 current_copyID += COPYID_INC;
5294 return current_copyID;
5295}
5296
5297/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298 * Garbage collection for lists and dictionaries.
5299 *
5300 * We use reference counts to be able to free most items right away when they
5301 * are no longer used. But for composite items it's possible that it becomes
5302 * unused while the reference count is > 0: When there is a recursive
5303 * reference. Example:
5304 * :let l = [1, 2, 3]
5305 * :let d = {9: l}
5306 * :let l[1] = d
5307 *
5308 * Since this is quite unusual we handle this with garbage collection: every
5309 * once in a while find out which lists and dicts are not referenced from any
5310 * variable.
5311 *
5312 * Here is a good reference text about garbage collection (refers to Python
5313 * but it applies to all reference-counting mechanisms):
5314 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005316
5317/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005318 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005319 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005321 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005323garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005324{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005325 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327 buf_T *buf;
5328 win_T *wp;
5329 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005330 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005331 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005332
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005333 if (!testing)
5334 {
5335 /* Only do this once. */
5336 want_garbage_collect = FALSE;
5337 may_garbage_collect = FALSE;
5338 garbage_collect_at_exit = FALSE;
5339 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005340
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005341 /* We advance by two because we add one for items referenced through
5342 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005343 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345 /*
5346 * 1. Go through all accessible variables and mark all lists and dicts
5347 * with copyID.
5348 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005349
5350 /* Don't free variables in the previous_funccal list unless they are only
5351 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005352 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005354
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355 /* script-local variables */
5356 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005358
5359 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005360 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005361 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5362 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005363
5364 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5367 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005368 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005369 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5370 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005372 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005373 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5375 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005377 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005378
5379 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005382 /* named functions (matters for closures) */
5383 abort = abort || set_ref_in_functions(copyID);
5384
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005385 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005387
Bram Moolenaard812df62008-11-09 12:46:09 +00005388 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005390
Bram Moolenaar1dced572012-04-05 16:54:08 +02005391#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005392 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005393#endif
5394
Bram Moolenaardb913952012-06-29 12:54:53 +02005395#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005397#endif
5398
5399#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005400 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005401#endif
5402
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005403#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005404 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005405 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005406#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005407#ifdef FEAT_NETBEANS_INTG
5408 abort = abort || set_ref_in_nb_channel(copyID);
5409#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005410
Bram Moolenaare3188e22016-05-31 21:13:04 +02005411#ifdef FEAT_TIMERS
5412 abort = abort || set_ref_in_timer(copyID);
5413#endif
5414
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005415#ifdef FEAT_QUICKFIX
5416 abort = abort || set_ref_in_quickfix(copyID);
5417#endif
5418
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005419#ifdef FEAT_TERMINAL
5420 abort = abort || set_ref_in_term(copyID);
5421#endif
5422
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005424 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 /*
5426 * 2. Free lists and dictionaries that are not referenced.
5427 */
5428 did_free = free_unref_items(copyID);
5429
5430 /*
5431 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005432 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005435 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005436 else if (p_verbose > 0)
5437 {
5438 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5439 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005440
5441 return did_free;
5442}
5443
5444/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005445 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005446 */
5447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005448free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005449{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005450 int did_free = FALSE;
5451
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 /* Let all "free" functions know that we are here. This means no
5453 * dictionaries, lists, channels or jobs are to be freed, because we will
5454 * do that here. */
5455 in_free_unref_items = TRUE;
5456
5457 /*
5458 * PASS 1: free the contents of the items. We don't free the items
5459 * themselves yet, so that it is possible to decrement refcount counters
5460 */
5461
Bram Moolenaarcd524592016-07-17 14:57:05 +02005462 /* Go through the list of dicts and free items without the copyID. */
5463 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464
Bram Moolenaarda861d62016-07-17 15:46:27 +02005465 /* Go through the list of lists and free items without the copyID. */
5466 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005467
5468#ifdef FEAT_JOB_CHANNEL
5469 /* Go through the list of jobs and free items without the copyID. This
5470 * must happen before doing channels, because jobs refer to channels, but
5471 * the reference from the channel to the job isn't tracked. */
5472 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5473
5474 /* Go through the list of channels and free items without the copyID. */
5475 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5476#endif
5477
5478 /*
5479 * PASS 2: free the items themselves.
5480 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005481 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005482 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005484#ifdef FEAT_JOB_CHANNEL
5485 /* Go through the list of jobs and free items without the copyID. This
5486 * must happen before doing channels, because jobs refer to channels, but
5487 * the reference from the channel to the job isn't tracked. */
5488 free_unused_jobs(copyID, COPYID_MASK);
5489
5490 /* Go through the list of channels and free items without the copyID. */
5491 free_unused_channels(copyID, COPYID_MASK);
5492#endif
5493
5494 in_free_unref_items = FALSE;
5495
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005496 return did_free;
5497}
5498
5499/*
5500 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 * "list_stack" is used to add lists to be marked. Can be NULL.
5502 *
5503 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005504 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005506set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005507{
5508 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005509 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 hashtab_T *cur_ht;
5512 ht_stack_T *ht_stack = NULL;
5513 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005514
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 cur_ht = ht;
5516 for (;;)
5517 {
5518 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005519 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005520 /* Mark each item in the hashtab. If the item contains a hashtab
5521 * it is added to ht_stack, if it contains a list it is added to
5522 * list_stack. */
5523 todo = (int)cur_ht->ht_used;
5524 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5525 if (!HASHITEM_EMPTY(hi))
5526 {
5527 --todo;
5528 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5529 &ht_stack, list_stack);
5530 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532
5533 if (ht_stack == NULL)
5534 break;
5535
5536 /* take an item from the stack */
5537 cur_ht = ht_stack->ht;
5538 tempitem = ht_stack;
5539 ht_stack = ht_stack->prev;
5540 free(tempitem);
5541 }
5542
5543 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005544}
5545
5546/*
5547 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005548 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5549 *
5550 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005554{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005555 listitem_T *li;
5556 int abort = FALSE;
5557 list_T *cur_l;
5558 list_stack_T *list_stack = NULL;
5559 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005560
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005561 cur_l = l;
5562 for (;;)
5563 {
5564 if (!abort)
5565 /* Mark each item in the list. If the item contains a hashtab
5566 * it is added to ht_stack, if it contains a list it is added to
5567 * list_stack. */
5568 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5569 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5570 ht_stack, &list_stack);
5571 if (list_stack == NULL)
5572 break;
5573
5574 /* take an item from the stack */
5575 cur_l = list_stack->list;
5576 tempitem = list_stack;
5577 list_stack = list_stack->prev;
5578 free(tempitem);
5579 }
5580
5581 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582}
5583
5584/*
5585 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 * "list_stack" is used to add lists to be marked. Can be NULL.
5587 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5588 *
5589 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005590 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005592set_ref_in_item(
5593 typval_T *tv,
5594 int copyID,
5595 ht_stack_T **ht_stack,
5596 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005597{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005598 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005599
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005600 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005601 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005602 dict_T *dd = tv->vval.v_dict;
5603
Bram Moolenaara03f2332016-02-06 18:09:59 +01005604 if (dd != NULL && dd->dv_copyID != copyID)
5605 {
5606 /* Didn't see this dict yet. */
5607 dd->dv_copyID = copyID;
5608 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005609 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005610 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5611 }
5612 else
5613 {
5614 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5615 if (newitem == NULL)
5616 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 else
5618 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005619 newitem->ht = &dd->dv_hashtab;
5620 newitem->prev = *ht_stack;
5621 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005622 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005623 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005624 }
5625 }
5626 else if (tv->v_type == VAR_LIST)
5627 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005628 list_T *ll = tv->vval.v_list;
5629
Bram Moolenaara03f2332016-02-06 18:09:59 +01005630 if (ll != NULL && ll->lv_copyID != copyID)
5631 {
5632 /* Didn't see this list yet. */
5633 ll->lv_copyID = copyID;
5634 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005635 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005636 abort = set_ref_in_list(ll, copyID, ht_stack);
5637 }
5638 else
5639 {
5640 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005641 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005642 if (newitem == NULL)
5643 abort = TRUE;
5644 else
5645 {
5646 newitem->list = ll;
5647 newitem->prev = *list_stack;
5648 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005650 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005651 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005652 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005653 else if (tv->v_type == VAR_FUNC)
5654 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005655 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005656 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005657 else if (tv->v_type == VAR_PARTIAL)
5658 {
5659 partial_T *pt = tv->vval.v_partial;
5660 int i;
5661
5662 /* A partial does not have a copyID, because it cannot contain itself.
5663 */
5664 if (pt != NULL)
5665 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005666 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005667
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005668 if (pt->pt_dict != NULL)
5669 {
5670 typval_T dtv;
5671
5672 dtv.v_type = VAR_DICT;
5673 dtv.vval.v_dict = pt->pt_dict;
5674 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5675 }
5676
5677 for (i = 0; i < pt->pt_argc; ++i)
5678 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5679 ht_stack, list_stack);
5680 }
5681 }
5682#ifdef FEAT_JOB_CHANNEL
5683 else if (tv->v_type == VAR_JOB)
5684 {
5685 job_T *job = tv->vval.v_job;
5686 typval_T dtv;
5687
5688 if (job != NULL && job->jv_copyID != copyID)
5689 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005690 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005691 if (job->jv_channel != NULL)
5692 {
5693 dtv.v_type = VAR_CHANNEL;
5694 dtv.vval.v_channel = job->jv_channel;
5695 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5696 }
5697 if (job->jv_exit_partial != NULL)
5698 {
5699 dtv.v_type = VAR_PARTIAL;
5700 dtv.vval.v_partial = job->jv_exit_partial;
5701 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5702 }
5703 }
5704 }
5705 else if (tv->v_type == VAR_CHANNEL)
5706 {
5707 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005708 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005709 typval_T dtv;
5710 jsonq_T *jq;
5711 cbq_T *cq;
5712
5713 if (ch != NULL && ch->ch_copyID != copyID)
5714 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005715 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005716 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005717 {
5718 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5719 jq = jq->jq_next)
5720 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5721 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5722 cq = cq->cq_next)
5723 if (cq->cq_partial != NULL)
5724 {
5725 dtv.v_type = VAR_PARTIAL;
5726 dtv.vval.v_partial = cq->cq_partial;
5727 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5728 }
5729 if (ch->ch_part[part].ch_partial != NULL)
5730 {
5731 dtv.v_type = VAR_PARTIAL;
5732 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5733 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5734 }
5735 }
5736 if (ch->ch_partial != NULL)
5737 {
5738 dtv.v_type = VAR_PARTIAL;
5739 dtv.vval.v_partial = ch->ch_partial;
5740 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5741 }
5742 if (ch->ch_close_partial != NULL)
5743 {
5744 dtv.v_type = VAR_PARTIAL;
5745 dtv.vval.v_partial = ch->ch_close_partial;
5746 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5747 }
5748 }
5749 }
5750#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005751 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005752}
5753
Bram Moolenaar17a13432016-01-24 14:22:10 +01005754 static char *
5755get_var_special_name(int nr)
5756{
5757 switch (nr)
5758 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005759 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005760 case VVAL_TRUE: return "v:true";
5761 case VVAL_NONE: return "v:none";
5762 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005763 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005764 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005765 return "42";
5766}
5767
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 * Return a string with the string representation of a variable.
5770 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005772 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005773 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5774 * stings as "string()", otherwise does not put quotes around strings, as
5775 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005776 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5777 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005778 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005780 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005781echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005782 typval_T *tv,
5783 char_u **tofree,
5784 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005785 int copyID,
5786 int echo_style,
5787 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005788 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 static int recurse = 0;
5791 char_u *r = NULL;
5792
Bram Moolenaar33570922005-01-25 22:26:29 +00005793 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005794 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005795 if (!did_echo_string_emsg)
5796 {
5797 /* Only give this message once for a recursive call to avoid
5798 * flooding the user with errors. And stop iterating over lists
5799 * and dicts. */
5800 did_echo_string_emsg = TRUE;
5801 EMSG(_("E724: variable nested too deep for displaying"));
5802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005804 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005805 }
5806 ++recurse;
5807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 switch (tv->v_type)
5809 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005810 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005811 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005812 {
5813 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005814 r = tv->vval.v_string;
5815 if (r == NULL)
5816 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005817 }
5818 else
5819 {
5820 *tofree = string_quote(tv->vval.v_string, FALSE);
5821 r = *tofree;
5822 }
5823 break;
5824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005826 if (echo_style)
5827 {
5828 *tofree = NULL;
5829 r = tv->vval.v_string;
5830 }
5831 else
5832 {
5833 *tofree = string_quote(tv->vval.v_string, TRUE);
5834 r = *tofree;
5835 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005837
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005838 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005839 {
5840 partial_T *pt = tv->vval.v_partial;
5841 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005842 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005843 garray_T ga;
5844 int i;
5845 char_u *tf;
5846
5847 ga_init2(&ga, 1, 100);
5848 ga_concat(&ga, (char_u *)"function(");
5849 if (fname != NULL)
5850 {
5851 ga_concat(&ga, fname);
5852 vim_free(fname);
5853 }
5854 if (pt != NULL && pt->pt_argc > 0)
5855 {
5856 ga_concat(&ga, (char_u *)", [");
5857 for (i = 0; i < pt->pt_argc; ++i)
5858 {
5859 if (i > 0)
5860 ga_concat(&ga, (char_u *)", ");
5861 ga_concat(&ga,
5862 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5863 vim_free(tf);
5864 }
5865 ga_concat(&ga, (char_u *)"]");
5866 }
5867 if (pt != NULL && pt->pt_dict != NULL)
5868 {
5869 typval_T dtv;
5870
5871 ga_concat(&ga, (char_u *)", ");
5872 dtv.v_type = VAR_DICT;
5873 dtv.vval.v_dict = pt->pt_dict;
5874 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5875 vim_free(tf);
5876 }
5877 ga_concat(&ga, (char_u *)")");
5878
5879 *tofree = ga.ga_data;
5880 r = *tofree;
5881 break;
5882 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005883
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005884 case VAR_BLOB:
5885 if (tv->vval.v_blob == NULL)
5886 {
5887 *tofree = NULL;
5888 r = (char_u *)"[]";
5889 }
5890 else
5891 {
5892 blob_T *b;
5893 int i;
5894 garray_T ga;
5895
5896 // Store bytes in the growarray.
5897 ga_init2(&ga, 1, 4000);
5898 b = tv->vval.v_blob;
5899 ga_append(&ga, '[');
5900 for (i = 0; i < blob_len(b); i++)
5901 {
5902 if (i > 0)
5903 ga_concat(&ga, (char_u *)",");
5904 vim_snprintf((char *)numbuf, NUMBUFLEN, "0x%02X",
5905 (int)blob_get(b, i));
5906 ga_concat(&ga, numbuf);
5907 }
5908 ga_append(&ga, ']');
5909 *tofree = ga.ga_data;
5910 r = *tofree;
5911 }
5912 break;
5913
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005915 if (tv->vval.v_list == NULL)
5916 {
5917 *tofree = NULL;
5918 r = NULL;
5919 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005920 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5921 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005922 {
5923 *tofree = NULL;
5924 r = (char_u *)"[...]";
5925 }
5926 else
5927 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005928 int old_copyID = tv->vval.v_list->lv_copyID;
5929
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005931 *tofree = list2string(tv, copyID, restore_copyID);
5932 if (restore_copyID)
5933 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005934 r = *tofree;
5935 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005937
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005939 if (tv->vval.v_dict == NULL)
5940 {
5941 *tofree = NULL;
5942 r = NULL;
5943 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005944 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5945 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005946 {
5947 *tofree = NULL;
5948 r = (char_u *)"{...}";
5949 }
5950 else
5951 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005952 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005954 *tofree = dict2string(tv, copyID, restore_copyID);
5955 if (restore_copyID)
5956 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005957 r = *tofree;
5958 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005959 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005960
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005962 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005963 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005964 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005965 break;
5966
Bram Moolenaar835dc632016-02-07 14:27:38 +01005967 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005968 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005970 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005971 if (composite_val)
5972 {
5973 *tofree = string_quote(r, FALSE);
5974 r = *tofree;
5975 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005977
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005978 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005979#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005980 *tofree = NULL;
5981 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5982 r = numbuf;
5983 break;
5984#endif
5985
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005986 case VAR_SPECIAL:
5987 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005988 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005989 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991
Bram Moolenaar8502c702014-06-17 12:51:16 +02005992 if (--recurse == 0)
5993 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995}
5996
5997/*
5998 * Return a string with the string representation of a variable.
5999 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6000 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006001 * Does not put quotes around strings, as ":echo" displays values.
6002 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6003 * May return NULL.
6004 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006006echo_string(
6007 typval_T *tv,
6008 char_u **tofree,
6009 char_u *numbuf,
6010 int copyID)
6011{
6012 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6013}
6014
6015/*
6016 * Return a string with the string representation of a variable.
6017 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6018 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006019 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006020 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023tv2string(
6024 typval_T *tv,
6025 char_u **tofree,
6026 char_u *numbuf,
6027 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006028{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006029 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030}
6031
6032/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 * Return string "str" in ' quotes, doubling ' characters.
6034 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006038string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039{
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006041 char_u *p, *r, *s;
6042
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 len = (function ? 13 : 3);
6044 if (str != NULL)
6045 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006046 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006047 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 if (*p == '\'')
6049 ++len;
6050 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006051 s = r = alloc(len);
6052 if (r != NULL)
6053 {
6054 if (function)
6055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006056 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 r += 10;
6058 }
6059 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006060 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 if (str != NULL)
6062 for (p = str; *p != NUL; )
6063 {
6064 if (*p == '\'')
6065 *r++ = '\'';
6066 MB_COPY_CHAR(p, r);
6067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069 if (function)
6070 *r++ = ')';
6071 *r++ = NUL;
6072 }
6073 return s;
6074}
6075
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006076#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077/*
6078 * Convert the string "text" to a floating point number.
6079 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6080 * this always uses a decimal point.
6081 * Returns the length of the text that was consumed.
6082 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006083 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084string2float(
6085 char_u *text,
6086 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006087{
6088 char *s = (char *)text;
6089 float_T f;
6090
Bram Moolenaar62473612017-01-08 19:25:40 +01006091 /* MS-Windows does not deal with "inf" and "nan" properly. */
6092 if (STRNICMP(text, "inf", 3) == 0)
6093 {
6094 *value = INFINITY;
6095 return 3;
6096 }
6097 if (STRNICMP(text, "-inf", 3) == 0)
6098 {
6099 *value = -INFINITY;
6100 return 4;
6101 }
6102 if (STRNICMP(text, "nan", 3) == 0)
6103 {
6104 *value = NAN;
6105 return 3;
6106 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006107 f = strtod(s, &s);
6108 *value = f;
6109 return (int)((char_u *)s - text);
6110}
6111#endif
6112
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 * Get the value of an environment variable.
6115 * "arg" is pointing to the '$'. It is advanced to after the name.
6116 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006117 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
6119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 char_u *string = NULL;
6123 int len;
6124 int cc;
6125 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006126 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 ++*arg;
6129 name = *arg;
6130 len = get_env_len(arg);
6131 if (evaluate)
6132 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006133 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006134 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006135
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006136 cc = name[len];
6137 name[len] = NUL;
6138 /* first try vim_getenv(), fast for normal environment vars */
6139 string = vim_getenv(name, &mustfree);
6140 if (string != NULL && *string != NUL)
6141 {
6142 if (!mustfree)
6143 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006145 else
6146 {
6147 if (mustfree)
6148 vim_free(string);
6149
6150 /* next try expanding things like $VIM and ${HOME} */
6151 string = expand_env_save(name - 1);
6152 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006153 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006154 }
6155 name[len] = cc;
6156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157 rettv->v_type = VAR_STRING;
6158 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 }
6160
6161 return OK;
6162}
6163
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006164
6165
6166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006168 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006170 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006171var2fpos(
6172 typval_T *varp,
6173 int dollar_lnum, /* TRUE when $ is last line */
6174 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006176 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006178 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
Bram Moolenaara5525202006-03-02 22:52:09 +00006180 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006181 if (varp->v_type == VAR_LIST)
6182 {
6183 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006184 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006185 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006186 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006187
6188 l = varp->vval.v_list;
6189 if (l == NULL)
6190 return NULL;
6191
6192 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006193 pos.lnum = list_find_nr(l, 0L, &error);
6194 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006195 return NULL; /* invalid line number */
6196
6197 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006198 pos.col = list_find_nr(l, 1L, &error);
6199 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006200 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006201 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006202
6203 /* We accept "$" for the column number: last column. */
6204 li = list_find(l, 1L);
6205 if (li != NULL && li->li_tv.v_type == VAR_STRING
6206 && li->li_tv.vval.v_string != NULL
6207 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6208 pos.col = len + 1;
6209
Bram Moolenaara5525202006-03-02 22:52:09 +00006210 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006211 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006212 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006213 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006214
Bram Moolenaara5525202006-03-02 22:52:09 +00006215#ifdef FEAT_VIRTUALEDIT
6216 /* Get the virtual offset. Defaults to zero. */
6217 pos.coladd = list_find_nr(l, 2L, &error);
6218 if (error)
6219 pos.coladd = 0;
6220#endif
6221
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006222 return &pos;
6223 }
6224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006225 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006226 if (name == NULL)
6227 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006228 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006230 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6231 {
6232 if (VIsual_active)
6233 return &VIsual;
6234 return &curwin->w_cursor;
6235 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006236 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006238 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6240 return NULL;
6241 return pp;
6242 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006243
6244#ifdef FEAT_VIRTUALEDIT
6245 pos.coladd = 0;
6246#endif
6247
Bram Moolenaar477933c2007-07-17 14:32:23 +00006248 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006249 {
6250 pos.col = 0;
6251 if (name[1] == '0') /* "w0": first visible line */
6252 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006253 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006254 /* In silent Ex mode topline is zero, but that's not a valid line
6255 * number; use one instead. */
6256 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006257 return &pos;
6258 }
6259 else if (name[1] == '$') /* "w$": last visible line */
6260 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006261 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006262 /* In silent Ex mode botline is zero, return zero then. */
6263 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006264 return &pos;
6265 }
6266 }
6267 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006269 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 pos.lnum = curbuf->b_ml.ml_line_count;
6272 pos.col = 0;
6273 }
6274 else
6275 {
6276 pos.lnum = curwin->w_cursor.lnum;
6277 pos.col = (colnr_T)STRLEN(ml_get_curline());
6278 }
6279 return &pos;
6280 }
6281 return NULL;
6282}
6283
6284/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006285 * Convert list in "arg" into a position and optional file number.
6286 * When "fnump" is NULL there is no file number, only 3 items.
6287 * Note that the column is passed on as-is, the caller may want to decrement
6288 * it to use 1 for the first column.
6289 * Return FAIL when conversion is not possible, doesn't check the position for
6290 * validity.
6291 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006293list2fpos(
6294 typval_T *arg,
6295 pos_T *posp,
6296 int *fnump,
6297 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006298{
6299 list_T *l = arg->vval.v_list;
6300 long i = 0;
6301 long n;
6302
Bram Moolenaar493c1782014-05-28 14:34:46 +02006303 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6304 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006305 if (arg->v_type != VAR_LIST
6306 || l == NULL
6307 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006308 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006309 return FAIL;
6310
6311 if (fnump != NULL)
6312 {
6313 n = list_find_nr(l, i++, NULL); /* fnum */
6314 if (n < 0)
6315 return FAIL;
6316 if (n == 0)
6317 n = curbuf->b_fnum; /* current buffer */
6318 *fnump = n;
6319 }
6320
6321 n = list_find_nr(l, i++, NULL); /* lnum */
6322 if (n < 0)
6323 return FAIL;
6324 posp->lnum = n;
6325
6326 n = list_find_nr(l, i++, NULL); /* col */
6327 if (n < 0)
6328 return FAIL;
6329 posp->col = n;
6330
6331#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006332 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006333 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006334 posp->coladd = 0;
6335 else
6336 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006337#endif
6338
Bram Moolenaar493c1782014-05-28 14:34:46 +02006339 if (curswantp != NULL)
6340 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6341
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006342 return OK;
6343}
6344
6345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 * Get the length of an environment variable name.
6347 * Advance "arg" to the first character after the name.
6348 * Return 0 for error.
6349 */
6350 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006351get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 char_u *p;
6354 int len;
6355
6356 for (p = *arg; vim_isIDc(*p); ++p)
6357 ;
6358 if (p == *arg) /* no name found */
6359 return 0;
6360
6361 len = (int)(p - *arg);
6362 *arg = p;
6363 return len;
6364}
6365
6366/*
6367 * Get the length of the name of a function or internal variable.
6368 * "arg" is advanced to the first non-white character after the name.
6369 * Return 0 if something is wrong.
6370 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 char_u *p;
6375 int len;
6376
6377 /* Find the end of the name. */
6378 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006379 {
6380 if (*p == ':')
6381 {
6382 /* "s:" is start of "s:var", but "n:" is not and can be used in
6383 * slice "[n:]". Also "xx:" is not a namespace. */
6384 len = (int)(p - *arg);
6385 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6386 || len > 1)
6387 break;
6388 }
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (p == *arg) /* no name found */
6391 return 0;
6392
6393 len = (int)(p - *arg);
6394 *arg = skipwhite(p);
6395
6396 return len;
6397}
6398
6399/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006400 * Get the length of the name of a variable or function.
6401 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006403 * Return -1 if curly braces expansion failed.
6404 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 * If the name contains 'magic' {}'s, expand them and return the
6406 * expanded name in an allocated string via 'alias' - caller must free.
6407 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409get_name_len(
6410 char_u **arg,
6411 char_u **alias,
6412 int evaluate,
6413 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 char_u *p;
6417 char_u *expr_start;
6418 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 *alias = NULL; /* default to no alias */
6421
6422 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6423 && (*arg)[2] == (int)KE_SNR)
6424 {
6425 /* hard coded <SNR>, already translated */
6426 *arg += 3;
6427 return get_id_len(arg) + 3;
6428 }
6429 len = eval_fname_script(*arg);
6430 if (len > 0)
6431 {
6432 /* literal "<SID>", "s:" or "<SNR>" */
6433 *arg += len;
6434 }
6435
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006437 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006439 p = find_name_end(*arg, &expr_start, &expr_end,
6440 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (expr_start != NULL)
6442 {
6443 char_u *temp_string;
6444
6445 if (!evaluate)
6446 {
6447 len += (int)(p - *arg);
6448 *arg = skipwhite(p);
6449 return len;
6450 }
6451
6452 /*
6453 * Include any <SID> etc in the expanded string:
6454 * Thus the -len here.
6455 */
6456 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6457 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006458 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 *alias = temp_string;
6460 *arg = skipwhite(p);
6461 return (int)STRLEN(temp_string);
6462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006465 // Only give an error when there is something, otherwise it will be
6466 // reported at a higher level.
6467 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 EMSG2(_(e_invexpr2), *arg);
6469
6470 return len;
6471}
6472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006473/*
6474 * Find the end of a variable or function name, taking care of magic braces.
6475 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6476 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006477 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006478 * Return a pointer to just after the name. Equal to "arg" if there is no
6479 * valid name.
6480 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006481 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006482find_name_end(
6483 char_u *arg,
6484 char_u **expr_start,
6485 char_u **expr_end,
6486 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006488 int mb_nest = 0;
6489 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006491 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495 *expr_start = NULL;
6496 *expr_end = NULL;
6497 }
6498
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006499 /* Quick check for valid starting character. */
6500 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6501 return arg;
6502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 for (p = arg; *p != NUL
6504 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006506 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006508 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006509 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006510 if (*p == '\'')
6511 {
6512 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006513 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006514 ;
6515 if (*p == NUL)
6516 break;
6517 }
6518 else if (*p == '"')
6519 {
6520 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006521 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006522 if (*p == '\\' && p[1] != NUL)
6523 ++p;
6524 if (*p == NUL)
6525 break;
6526 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006527 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6528 {
6529 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006530 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006531 len = (int)(p - arg);
6532 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006533 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006534 break;
6535 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006537 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006539 if (*p == '[')
6540 ++br_nest;
6541 else if (*p == ']')
6542 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006544
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006545 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 if (*p == '{')
6548 {
6549 mb_nest++;
6550 if (expr_start != NULL && *expr_start == NULL)
6551 *expr_start = p;
6552 }
6553 else if (*p == '}')
6554 {
6555 mb_nest--;
6556 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6557 *expr_end = p;
6558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 }
6561
6562 return p;
6563}
6564
6565/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006566 * Expands out the 'magic' {}'s in a variable/function name.
6567 * Note that this can call itself recursively, to deal with
6568 * constructs like foo{bar}{baz}{bam}
6569 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6570 * "in_start" ^
6571 * "expr_start" ^
6572 * "expr_end" ^
6573 * "in_end" ^
6574 *
6575 * Returns a new allocated string, which the caller must free.
6576 * Returns NULL for failure.
6577 */
6578 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579make_expanded_name(
6580 char_u *in_start,
6581 char_u *expr_start,
6582 char_u *expr_end,
6583 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006584{
6585 char_u c1;
6586 char_u *retval = NULL;
6587 char_u *temp_result;
6588 char_u *nextcmd = NULL;
6589
6590 if (expr_end == NULL || in_end == NULL)
6591 return NULL;
6592 *expr_start = NUL;
6593 *expr_end = NUL;
6594 c1 = *in_end;
6595 *in_end = NUL;
6596
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006597 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006598 if (temp_result != NULL && nextcmd == NULL)
6599 {
6600 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6601 + (in_end - expr_end) + 1));
6602 if (retval != NULL)
6603 {
6604 STRCPY(retval, in_start);
6605 STRCAT(retval, temp_result);
6606 STRCAT(retval, expr_end + 1);
6607 }
6608 }
6609 vim_free(temp_result);
6610
6611 *in_end = c1; /* put char back for error messages */
6612 *expr_start = '{';
6613 *expr_end = '}';
6614
6615 if (retval != NULL)
6616 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006617 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006618 if (expr_start != NULL)
6619 {
6620 /* Further expansion! */
6621 temp_result = make_expanded_name(retval, expr_start,
6622 expr_end, temp_result);
6623 vim_free(retval);
6624 retval = temp_result;
6625 }
6626 }
6627
6628 return retval;
6629}
6630
6631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006633 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006638 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6639}
6640
6641/*
6642 * Return TRUE if character "c" can be used as the first character in a
6643 * variable or function name (excluding '{' and '}').
6644 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006645 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006646eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006647{
6648 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649}
6650
6651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 * Set number v: variable to "val".
6653 */
6654 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006655set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658}
6659
6660/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006661 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006663 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006664get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667}
6668
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006669/*
6670 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006671 * If the String variable has never been set, return an empty string.
6672 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006673 */
6674 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006676{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006677 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006678}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006679
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006681 * Get List v: variable value. Caller must take care of reference count when
6682 * needed.
6683 */
6684 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006686{
6687 return vimvars[idx].vv_list;
6688}
6689
6690/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006691 * Get Dict v: variable value. Caller must take care of reference count when
6692 * needed.
6693 */
6694 dict_T *
6695get_vim_var_dict(int idx)
6696{
6697 return vimvars[idx].vv_dict;
6698}
6699
6700/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006701 * Set v:char to character "c".
6702 */
6703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006705{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006706 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006707
6708#ifdef FEAT_MBYTE
6709 if (has_mbyte)
6710 buf[(*mb_char2bytes)(c, buf)] = NUL;
6711 else
6712#endif
6713 {
6714 buf[0] = c;
6715 buf[1] = NUL;
6716 }
6717 set_vim_var_string(VV_CHAR, buf, -1);
6718}
6719
6720/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006721 * Set v:count to "count" and v:count1 to "count1".
6722 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 */
6724 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006725set_vcount(
6726 long count,
6727 long count1,
6728 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006730 if (set_prevcount)
6731 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006732 vimvars[VV_COUNT].vv_nr = count;
6733 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
6736/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006737 * Save variables that might be changed as a side effect. Used when executing
6738 * a timer callback.
6739 */
6740 void
6741save_vimvars(vimvars_save_T *vvsave)
6742{
6743 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6744 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6745 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6746}
6747
6748/*
6749 * Restore variables saved by save_vimvars().
6750 */
6751 void
6752restore_vimvars(vimvars_save_T *vvsave)
6753{
6754 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6755 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6756 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6757}
6758
6759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 * Set string v: variable to a copy of "val".
6761 */
6762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006763set_vim_var_string(
6764 int idx,
6765 char_u *val,
6766 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
Bram Moolenaara542c682016-01-31 16:28:04 +01006768 clear_tv(&vimvars[idx].vv_di.di_tv);
6769 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006773 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006775 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776}
6777
6778/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006779 * Set List v: variable to "val".
6780 */
6781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006783{
Bram Moolenaara542c682016-01-31 16:28:04 +01006784 clear_tv(&vimvars[idx].vv_di.di_tv);
6785 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006786 vimvars[idx].vv_list = val;
6787 if (val != NULL)
6788 ++val->lv_refcount;
6789}
6790
6791/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006792 * Set Dictionary v: variable to "val".
6793 */
6794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006795set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006796{
Bram Moolenaara542c682016-01-31 16:28:04 +01006797 clear_tv(&vimvars[idx].vv_di.di_tv);
6798 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006799 vimvars[idx].vv_dict = val;
6800 if (val != NULL)
6801 {
6802 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006803 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006804 }
6805}
6806
6807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 * Set v:register if needed.
6809 */
6810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813 char_u regname;
6814
6815 if (c == 0 || c == ' ')
6816 regname = '"';
6817 else
6818 regname = c;
6819 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006820 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 set_vim_var_string(VV_REG, &regname, 1);
6822}
6823
6824/*
6825 * Get or set v:exception. If "oldval" == NULL, return the current value.
6826 * Otherwise, restore the value to "oldval" and return NULL.
6827 * Must always be called in pairs to save and restore v:exception! Does not
6828 * take care of memory allocations.
6829 */
6830 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
Bram Moolenaare9a41262005-01-15 22:18:47 +00006836 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 return NULL;
6838}
6839
6840/*
6841 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6842 * Otherwise, restore the value to "oldval" and return NULL.
6843 * Must always be called in pairs to save and restore v:throwpoint! Does not
6844 * take care of memory allocations.
6845 */
6846 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006847v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
Bram Moolenaare9a41262005-01-15 22:18:47 +00006852 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 return NULL;
6854}
6855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856/*
6857 * Set v:cmdarg.
6858 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6859 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6860 * Must always be called in pairs!
6861 */
6862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006863set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
6865 char_u *oldval;
6866 char_u *newval;
6867 unsigned len;
6868
Bram Moolenaare9a41262005-01-15 22:18:47 +00006869 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006870 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006872 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006873 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006874 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006877 if (eap->force_bin == FORCE_BIN)
6878 len = 6;
6879 else if (eap->force_bin == FORCE_NOBIN)
6880 len = 8;
6881 else
6882 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006883
6884 if (eap->read_edit)
6885 len += 7;
6886
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006887 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006888 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006889# ifdef FEAT_MBYTE
6890 if (eap->force_enc != 0)
6891 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006892 if (eap->bad_char != 0)
6893 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006894# endif
6895
6896 newval = alloc(len + 1);
6897 if (newval == NULL)
6898 return NULL;
6899
6900 if (eap->force_bin == FORCE_BIN)
6901 sprintf((char *)newval, " ++bin");
6902 else if (eap->force_bin == FORCE_NOBIN)
6903 sprintf((char *)newval, " ++nobin");
6904 else
6905 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006906
6907 if (eap->read_edit)
6908 STRCAT(newval, " ++edit");
6909
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006910 if (eap->force_ff != 0)
6911 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006912 eap->force_ff == 'u' ? "unix"
6913 : eap->force_ff == 'd' ? "dos"
6914 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006915#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006916 if (eap->force_enc != 0)
6917 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6918 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006919 if (eap->bad_char == BAD_KEEP)
6920 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6921 else if (eap->bad_char == BAD_DROP)
6922 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6923 else if (eap->bad_char != 0)
6924 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006925#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006927 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930/*
6931 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006932 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006935get_var_tv(
6936 char_u *name,
6937 int len, /* length of "name" */
6938 typval_T *rettv, /* NULL when only checking existence */
6939 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6940 int verbose, /* may give error message */
6941 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 /* truncate the name, so that we can use strcmp() */
6949 cc = name[len];
6950 name[len] = NUL;
6951
6952 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 * Check for user-defined variables.
6954 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006955 v = find_var(name, NULL, no_autoload);
6956 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006958 tv = &v->di_tv;
6959 if (dip != NULL)
6960 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962
Bram Moolenaare9a41262005-01-15 22:18:47 +00006963 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006965 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006966 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 ret = FAIL;
6968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006969 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 name[len] = cc;
6973
6974 return ret;
6975}
6976
6977/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006978 * Check if variable "name[len]" is a local variable or an argument.
6979 * If so, "*eval_lavars_used" is set to TRUE.
6980 */
6981 static void
6982check_vars(char_u *name, int len)
6983{
6984 int cc;
6985 char_u *varname;
6986 hashtab_T *ht;
6987
6988 if (eval_lavars_used == NULL)
6989 return;
6990
6991 /* truncate the name, so that we can use strcmp() */
6992 cc = name[len];
6993 name[len] = NUL;
6994
6995 ht = find_var_ht(name, &varname);
6996 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6997 {
6998 if (find_var(name, NULL, TRUE) != NULL)
6999 *eval_lavars_used = TRUE;
7000 }
7001
7002 name[len] = cc;
7003}
7004
7005/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007006 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7007 * Also handle function call with Funcref variable: func(expr)
7008 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007011handle_subscript(
7012 char_u **arg,
7013 typval_T *rettv,
7014 int evaluate, /* do more than finding the end */
7015 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007016{
7017 int ret = OK;
7018 dict_T *selfdict = NULL;
7019 char_u *s;
7020 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007021 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007022
7023 while (ret == OK
7024 && (**arg == '['
7025 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007026 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7027 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007028 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007029 {
7030 if (**arg == '(')
7031 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007032 partial_T *pt = NULL;
7033
Bram Moolenaard9fba312005-06-26 22:34:35 +00007034 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007035 if (evaluate)
7036 {
7037 functv = *rettv;
7038 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007039
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007040 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007041 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007042 {
7043 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007044 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007045 }
7046 else
7047 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007048 }
7049 else
7050 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007051 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007053 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054
7055 /* Clear the funcref afterwards, so that deleting it while
7056 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007057 if (evaluate)
7058 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007059
7060 /* Stop the expression evaluation when immediately aborting on
7061 * error, or when an interrupt occurred or an exception was thrown
7062 * but not caught. */
7063 if (aborting())
7064 {
7065 if (ret == OK)
7066 clear_tv(rettv);
7067 ret = FAIL;
7068 }
7069 dict_unref(selfdict);
7070 selfdict = NULL;
7071 }
7072 else /* **arg == '[' || **arg == '.' */
7073 {
7074 dict_unref(selfdict);
7075 if (rettv->v_type == VAR_DICT)
7076 {
7077 selfdict = rettv->vval.v_dict;
7078 if (selfdict != NULL)
7079 ++selfdict->dv_refcount;
7080 }
7081 else
7082 selfdict = NULL;
7083 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7084 {
7085 clear_tv(rettv);
7086 ret = FAIL;
7087 }
7088 }
7089 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007090
Bram Moolenaar1d429612016-05-24 15:44:17 +02007091 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7092 * Don't do this when "Func" is already a partial that was bound
7093 * explicitly (pt_auto is FALSE). */
7094 if (selfdict != NULL
7095 && (rettv->v_type == VAR_FUNC
7096 || (rettv->v_type == VAR_PARTIAL
7097 && (rettv->vval.v_partial->pt_auto
7098 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007099 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007100
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007101 dict_unref(selfdict);
7102 return ret;
7103}
7104
7105/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007106 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007107 * value).
7108 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007109 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007111{
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113}
7114
7115/*
7116 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 * The string "s" must have been allocated, it is consumed.
7118 * Return NULL for out of memory, the variable otherwise.
7119 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007121alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007125 rettv = alloc_tv();
7126 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007128 rettv->v_type = VAR_STRING;
7129 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
7131 else
7132 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007133 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134}
7135
7136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007137 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007140free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
7142 if (varp != NULL)
7143 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007144 switch (varp->v_type)
7145 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007146 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007147 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007148 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007149 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007150 vim_free(varp->vval.v_string);
7151 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007152 case VAR_PARTIAL:
7153 partial_unref(varp->vval.v_partial);
7154 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007155 case VAR_BLOB:
7156 blob_unref(varp->vval.v_blob);
7157 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007158 case VAR_LIST:
7159 list_unref(varp->vval.v_list);
7160 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007161 case VAR_DICT:
7162 dict_unref(varp->vval.v_dict);
7163 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007164 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007165#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007166 job_unref(varp->vval.v_job);
7167 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007168#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007169 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007170#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007171 channel_unref(varp->vval.v_channel);
7172 break;
7173#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007174 case VAR_NUMBER:
7175 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007176 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007177 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007178 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 vim_free(varp);
7181 }
7182}
7183
7184/*
7185 * Free the memory for a variable value and set the value to NULL or 0.
7186 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007188clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 if (varp != NULL)
7191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007194 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007195 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007196 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007197 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007198 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007200 case VAR_PARTIAL:
7201 partial_unref(varp->vval.v_partial);
7202 varp->vval.v_partial = NULL;
7203 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007204 case VAR_BLOB:
7205 blob_unref(varp->vval.v_blob);
7206 varp->vval.v_blob = NULL;
7207 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007208 case VAR_LIST:
7209 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007211 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 case VAR_DICT:
7213 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007216 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007217 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 varp->vval.v_number = 0;
7219 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007220 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007221#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007222 varp->vval.v_float = 0.0;
7223 break;
7224#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007225 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007226#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007227 job_unref(varp->vval.v_job);
7228 varp->vval.v_job = NULL;
7229#endif
7230 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007231 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007233 channel_unref(varp->vval.v_channel);
7234 varp->vval.v_channel = NULL;
7235#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 case VAR_UNKNOWN:
7237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007239 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241}
7242
7243/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007244 * Set the value of a variable to NULL without freeing items.
7245 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007247init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248{
7249 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007251}
7252
7253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 * Get the number value of a variable.
7255 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007257 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 * caller of incompatible types: it sets *denote to TRUE if "denote"
7259 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007261 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007262tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007264 int error = FALSE;
7265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007266 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007267}
7268
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007269 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007270tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007271{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007272 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007276 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007277 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007278 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007279#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007280 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007281 break;
7282#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007283 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007284 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007285 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007286 break;
7287 case VAR_STRING:
7288 if (varp->vval.v_string != NULL)
7289 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007290 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007291 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007292 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007293 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007294 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007295 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007296 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007297 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007298 case VAR_SPECIAL:
7299 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7300 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007301 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007302#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007303 EMSG(_("E910: Using a Job as a Number"));
7304 break;
7305#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007306 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007307#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007308 EMSG(_("E913: Using a Channel as a Number"));
7309 break;
7310#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007311 case VAR_BLOB:
7312 EMSG(_("E974: Using a Blob as a Number"));
7313 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007315 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 if (denote == NULL) /* useful for values that must be unsigned */
7319 n = -1;
7320 else
7321 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007322 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323}
7324
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007325#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007328{
7329 switch (varp->v_type)
7330 {
7331 case VAR_NUMBER:
7332 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007333 case VAR_FLOAT:
7334 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007335 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007336 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007337 EMSG(_("E891: Using a Funcref as a Float"));
7338 break;
7339 case VAR_STRING:
7340 EMSG(_("E892: Using a String as a Float"));
7341 break;
7342 case VAR_LIST:
7343 EMSG(_("E893: Using a List as a Float"));
7344 break;
7345 case VAR_DICT:
7346 EMSG(_("E894: Using a Dictionary as a Float"));
7347 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007348 case VAR_SPECIAL:
7349 EMSG(_("E907: Using a special value as a Float"));
7350 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007351 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007352# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007353 EMSG(_("E911: Using a Job as a Float"));
7354 break;
7355# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007356 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007357# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007358 EMSG(_("E914: Using a Channel as a Float"));
7359 break;
7360# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007361 case VAR_BLOB:
7362 EMSG(_("E975: Using a Blob as a Float"));
7363 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007364 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007366 break;
7367 }
7368 return 0;
7369}
7370#endif
7371
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 * Get the string value of a variable.
7374 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007375 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7376 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 * If the String variable has never been set, return an empty string.
7378 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007379 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007380 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007382 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007383tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
7385 static char_u mybuf[NUMBUFLEN];
7386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007387 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388}
7389
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007390 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007391tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007393 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007394
7395 return res != NULL ? res : (char_u *)"";
7396}
7397
Bram Moolenaar7d647822014-04-05 21:28:56 +02007398/*
7399 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7400 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007401 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007402tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007403{
7404 static char_u mybuf[NUMBUFLEN];
7405
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007406 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407}
7408
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007409 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007410tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007411{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007412 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007414 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007415 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007416 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007417 return buf;
7418 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007419 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007420 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007421 break;
7422 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007423 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 break;
7425 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007426 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007427 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007428 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007429#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007430 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007431 break;
7432#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007433 case VAR_STRING:
7434 if (varp->vval.v_string != NULL)
7435 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007436 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007437 case VAR_SPECIAL:
7438 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7439 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007440 case VAR_BLOB:
7441 EMSG(_("E976: using Blob as a String"));
7442 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007443 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007444#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007445 {
7446 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007447 char *status;
7448
7449 if (job == NULL)
7450 return (char_u *)"no process";
7451 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007452 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007453 : "run";
7454# ifdef UNIX
7455 vim_snprintf((char *)buf, NUMBUFLEN,
7456 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007457# elif defined(WIN32)
7458 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007459 "process %ld %s",
7460 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007461 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007462# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007463 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007464 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7465# endif
7466 return buf;
7467 }
7468#endif
7469 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007470 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007471#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007472 {
7473 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007474 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007475
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007476 if (channel == NULL)
7477 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7478 else
7479 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007480 "channel %d %s", channel->ch_id, status);
7481 return buf;
7482 }
7483#endif
7484 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007485 case VAR_UNKNOWN:
7486 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007489 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490}
7491
7492/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007493 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7494 * string() on Dict, List, etc.
7495 */
7496 char_u *
7497tv_stringify(typval_T *varp, char_u *buf)
7498{
7499 if (varp->v_type == VAR_LIST
7500 || varp->v_type == VAR_DICT
7501 || varp->v_type == VAR_FUNC
7502 || varp->v_type == VAR_PARTIAL
7503 || varp->v_type == VAR_FLOAT)
7504 {
7505 typval_T tmp;
7506
7507 f_string(varp, &tmp);
7508 tv_get_string_buf(&tmp, buf);
7509 clear_tv(varp);
7510 *varp = tmp;
7511 return tmp.vval.v_string;
7512 }
7513 return tv_get_string_buf(varp, buf);
7514}
7515
7516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 * Find variable "name" in the list of variables.
7518 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007519 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007520 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007523 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007524find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007528 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529
Bram Moolenaara7043832005-01-21 11:56:39 +00007530 ht = find_var_ht(name, &varname);
7531 if (htp != NULL)
7532 *htp = ht;
7533 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007535 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7536 if (ret != NULL)
7537 return ret;
7538
7539 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007540 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541}
7542
7543/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007544 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007545 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548find_var_in_ht(
7549 hashtab_T *ht,
7550 int htname,
7551 char_u *varname,
7552 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007553{
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hashitem_T *hi;
7555
7556 if (*varname == NUL)
7557 {
7558 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007559 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007561 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 case 'g': return &globvars_var;
7563 case 'v': return &vimvars_var;
7564 case 'b': return &curbuf->b_bufvar;
7565 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007566 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007567 case 'l': return get_funccal_local_var();
7568 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 }
7570 return NULL;
7571 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007572
7573 hi = hash_find(ht, varname);
7574 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007575 {
7576 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007577 * worked find the variable again. Don't auto-load a script if it was
7578 * loaded already, otherwise it would be loaded every time when
7579 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007580 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007581 {
7582 /* Note: script_autoload() may make "hi" invalid. It must either
7583 * be obtained again or not used. */
7584 if (!script_autoload(varname, FALSE) || aborting())
7585 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007587 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007588 if (HASHITEM_EMPTY(hi))
7589 return NULL;
7590 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007592}
7593
7594/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007596 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007597 * Set "varname" to the start of name without ':'.
7598 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007599 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007600find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007602 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007603 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007604
Bram Moolenaar73627d02015-08-11 15:46:09 +02007605 if (name[0] == NUL)
7606 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (name[1] != ':')
7608 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007609 /* The name must not start with a colon or #. */
7610 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 return NULL;
7612 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007613
7614 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007615 hi = hash_find(&compat_hashtab, name);
7616 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007617 return &compat_hashtab;
7618
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007619 ht = get_funccal_local_ht();
7620 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007622 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007625 if (*name == 'g') /* global variable */
7626 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007627 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7628 */
7629 if (vim_strchr(name + 2, ':') != NULL
7630 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007633 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007635 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007636 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007637 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 if (*name == 'v') /* v: variable */
7639 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007640 if (*name == 'a') /* a: function argument */
7641 return get_funccal_args_ht();
7642 if (*name == 'l') /* l: local function variable */
7643 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007645 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7646 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 return NULL;
7648}
7649
7650/*
7651 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007652 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 * Returns NULL when it doesn't exist.
7654 */
7655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007656get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007660 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 if (v == NULL)
7662 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007663 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
7665
7666/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 * sourcing this script and when executing functions defined in the script.
7669 */
7670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaara7043832005-01-21 11:56:39 +00007673 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 hashtab_T *ht;
7675 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007676
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7678 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007679 /* Re-allocating ga_data means that an ht_array pointing to
7680 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007682 for (i = 1; i <= ga_scripts.ga_len; ++i)
7683 {
7684 ht = &SCRIPT_VARS(i);
7685 if (ht->ht_mask == HT_INIT_SIZE - 1)
7686 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007687 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007689 }
7690
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 while (ga_scripts.ga_len < id)
7692 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007693 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007694 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007695 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 }
7699}
7700
7701/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7703 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 */
7705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007706init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007709 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007710 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007711 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007712 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 dict_var->di_tv.vval.v_dict = dict;
7714 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007715 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7717 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
7720/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007721 * Unreference a dictionary initialized by init_var_dict().
7722 */
7723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007725{
7726 /* Now the dict needs to be freed if no one else is using it, go back to
7727 * normal reference counting. */
7728 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7729 dict_unref(dict);
7730}
7731
7732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 * Frees all allocated variables and the value they contain.
7735 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 */
7737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007738vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007739{
7740 vars_clear_ext(ht, TRUE);
7741}
7742
7743/*
7744 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7745 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007747vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
Bram Moolenaara7043832005-01-21 11:56:39 +00007749 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 hashitem_T *hi;
7751 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007754 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007755 for (hi = ht->ht_array; todo > 0; ++hi)
7756 {
7757 if (!HASHITEM_EMPTY(hi))
7758 {
7759 --todo;
7760
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007762 * ht_array might change then. hash_clear() takes care of it
7763 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 v = HI2DI(hi);
7765 if (free_val)
7766 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007767 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007769 }
7770 }
7771 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007772 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
Bram Moolenaara7043832005-01-21 11:56:39 +00007775/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 * Delete a variable from hashtab "ht" at item "hi".
7777 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007780delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007783
7784 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 clear_tv(&di->di_tv);
7786 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788
7789/*
7790 * List the value of one internal variable.
7791 */
7792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007793list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 char_u *tofree;
7796 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007799 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007801 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007806list_one_var_a(
7807 char_u *prefix,
7808 char_u *name,
7809 int type,
7810 char_u *string,
7811 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
Bram Moolenaar31859182007-08-14 20:41:13 +00007813 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7814 msg_start();
7815 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (name != NULL) /* "a:" vars don't have a name stored */
7817 msg_puts(name);
7818 msg_putchar(' ');
7819 msg_advance(22);
7820 if (type == VAR_NUMBER)
7821 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007822 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 msg_putchar('*');
7824 else if (type == VAR_LIST)
7825 {
7826 msg_putchar('[');
7827 if (*string == '[')
7828 ++string;
7829 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007830 else if (type == VAR_DICT)
7831 {
7832 msg_putchar('{');
7833 if (*string == '{')
7834 ++string;
7835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 else
7837 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007841 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007842 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007843 if (*first)
7844 {
7845 msg_clr_eos();
7846 *first = FALSE;
7847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 * If the variable already exists, the value is updated.
7853 * Otherwise the variable is created.
7854 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856set_var(
7857 char_u *name,
7858 typval_T *tv,
7859 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007865 ht = find_var_ht(name, &varname);
7866 if (ht == NULL || *varname == NUL)
7867 {
7868 EMSG2(_(e_illvar), name);
7869 return;
7870 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007871 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007872
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007873 /* Search in parent scope which is possible to reference from lambda */
7874 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007875 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007876
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007877 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7878 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007879 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007884 if (var_check_ro(v->di_flags, name, FALSE)
7885 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007887
7888 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007889 * Handle setting internal v: variables separately where needed to
7890 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 */
7892 if (ht == &vimvarht)
7893 {
7894 if (v->di_tv.v_type == VAR_STRING)
7895 {
7896 vim_free(v->di_tv.vval.v_string);
7897 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007898 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 else
7900 {
7901 /* Take over the string to avoid an extra alloc/free. */
7902 v->di_tv.vval.v_string = tv->vval.v_string;
7903 tv->vval.v_string = NULL;
7904 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007905 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007907 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007909 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910 if (STRCMP(varname, "searchforward") == 0)
7911 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007912#ifdef FEAT_SEARCH_EXTRA
7913 else if (STRCMP(varname, "hlsearch") == 0)
7914 {
7915 no_hlsearch = !v->di_tv.vval.v_number;
7916 redraw_all_later(SOME_VALID);
7917 }
7918#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007919 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007920 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007921 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007922 {
Bram Moolenaar74ea88c2018-12-04 22:37:49 +01007923 EMSG2(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007924 return;
7925 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 }
7927
7928 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 else /* add a new variable */
7931 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007932 /* Can't add "v:" variable. */
7933 if (ht == &vimvarht)
7934 {
7935 EMSG2(_(e_illvar), name);
7936 return;
7937 }
7938
Bram Moolenaar92124a32005-06-17 22:03:40 +00007939 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007940 if (!valid_varname(varname))
7941 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007942
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007943 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7944 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007945 if (v == NULL)
7946 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007950 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 return;
7952 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007953 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007955
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007956 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007957 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007958 else
7959 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007961 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964}
7965
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007966/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007967 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 * Also give an error message.
7969 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007971var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007972{
7973 if (flags & DI_FLAGS_RO)
7974 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007975 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 return TRUE;
7977 }
7978 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7979 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007980 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 return TRUE;
7982 }
7983 return FALSE;
7984}
7985
7986/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007987 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7988 * Also give an error message.
7989 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007991var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007992{
7993 if (flags & DI_FLAGS_FIX)
7994 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007995 EMSG2(_("E795: Cannot delete variable %s"),
7996 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007997 return TRUE;
7998 }
7999 return FALSE;
8000}
8001
8002/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008003 * Check if a funcref is assigned to a valid variable name.
8004 * Return TRUE and give an error if not.
8005 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008007var_check_func_name(
8008 char_u *name, /* points to start of variable name */
8009 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008010{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008011 /* Allow for w: b: s: and t:. */
8012 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008013 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8014 ? name[2] : name[0]))
8015 {
8016 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
8017 name);
8018 return TRUE;
8019 }
8020 /* Don't allow hiding a function. When "v" is not NULL we might be
8021 * assigning another function to the same var, the type is checked
8022 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008023 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008024 {
8025 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
8026 name);
8027 return TRUE;
8028 }
8029 return FALSE;
8030}
8031
8032/*
8033 * Check if a variable name is valid.
8034 * Return FALSE and give an error if not.
8035 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008037valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008038{
8039 char_u *p;
8040
8041 for (p = varname; *p != NUL; ++p)
8042 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8043 && *p != AUTOLOAD_CHAR)
8044 {
8045 EMSG2(_(e_illvar), varname);
8046 return FALSE;
8047 }
8048 return TRUE;
8049}
8050
8051/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008052 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008053 * Also give an error message, using "name" or _("name") when use_gettext is
8054 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008055 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008057tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008058{
8059 if (lock & VAR_LOCKED)
8060 {
8061 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008062 name == NULL ? (char_u *)_("Unknown")
8063 : use_gettext ? (char_u *)_(name)
8064 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008065 return TRUE;
8066 }
8067 if (lock & VAR_FIXED)
8068 {
8069 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008070 name == NULL ? (char_u *)_("Unknown")
8071 : use_gettext ? (char_u *)_(name)
8072 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008073 return TRUE;
8074 }
8075 return FALSE;
8076}
8077
8078/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008079 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008080 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008081 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008082 * It is OK for "from" and "to" to point to the same item. This is used to
8083 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008086copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008089 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008090 switch (from->v_type)
8091 {
8092 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008093 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 to->vval.v_number = from->vval.v_number;
8095 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008097#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098 to->vval.v_float = from->vval.v_float;
8099 break;
8100#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008101 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008102#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008103 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008104 if (to->vval.v_job != NULL)
8105 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008106 break;
8107#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008108 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008109#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008110 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008111 if (to->vval.v_channel != NULL)
8112 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008113 break;
8114#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008115 case VAR_STRING:
8116 case VAR_FUNC:
8117 if (from->vval.v_string == NULL)
8118 to->vval.v_string = NULL;
8119 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008122 if (from->v_type == VAR_FUNC)
8123 func_ref(to->vval.v_string);
8124 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008126 case VAR_PARTIAL:
8127 if (from->vval.v_partial == NULL)
8128 to->vval.v_partial = NULL;
8129 else
8130 {
8131 to->vval.v_partial = from->vval.v_partial;
8132 ++to->vval.v_partial->pt_refcount;
8133 }
8134 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008135 case VAR_BLOB:
8136 if (from->vval.v_blob == NULL)
8137 to->vval.v_blob = NULL;
8138 else
8139 {
8140 to->vval.v_blob = from->vval.v_blob;
8141 ++to->vval.v_blob->bv_refcount;
8142 }
8143 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144 case VAR_LIST:
8145 if (from->vval.v_list == NULL)
8146 to->vval.v_list = NULL;
8147 else
8148 {
8149 to->vval.v_list = from->vval.v_list;
8150 ++to->vval.v_list->lv_refcount;
8151 }
8152 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008153 case VAR_DICT:
8154 if (from->vval.v_dict == NULL)
8155 to->vval.v_dict = NULL;
8156 else
8157 {
8158 to->vval.v_dict = from->vval.v_dict;
8159 ++to->vval.v_dict->dv_refcount;
8160 }
8161 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008162 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008163 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008164 break;
8165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166}
8167
8168/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008169 * Make a copy of an item.
8170 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008171 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8172 * reference to an already copied list/dict can be used.
8173 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008174 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008175 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008176item_copy(
8177 typval_T *from,
8178 typval_T *to,
8179 int deep,
8180 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008181{
8182 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008183 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184
Bram Moolenaar33570922005-01-25 22:26:29 +00008185 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 {
8187 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008188 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 }
8190 ++recurse;
8191
8192 switch (from->v_type)
8193 {
8194 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008195 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 case VAR_STRING:
8197 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008198 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008199 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008200 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008201 case VAR_CHANNEL:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008202 case VAR_BLOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 copy_tv(from, to);
8204 break;
8205 case VAR_LIST:
8206 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008207 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 if (from->vval.v_list == NULL)
8209 to->vval.v_list = NULL;
8210 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8211 {
8212 /* use the copy made earlier */
8213 to->vval.v_list = from->vval.v_list->lv_copylist;
8214 ++to->vval.v_list->lv_refcount;
8215 }
8216 else
8217 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8218 if (to->vval.v_list == NULL)
8219 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 break;
8221 case VAR_DICT:
8222 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008223 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008224 if (from->vval.v_dict == NULL)
8225 to->vval.v_dict = NULL;
8226 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8227 {
8228 /* use the copy made earlier */
8229 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8230 ++to->vval.v_dict->dv_refcount;
8231 }
8232 else
8233 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8234 if (to->vval.v_dict == NULL)
8235 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008236 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008237 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008238 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008239 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008240 }
8241 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008242 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008243}
8244
8245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 * This function is used by f_input() and f_inputdialog() functions. The third
8247 * argument to f_input() specifies the type of completion to use at the
8248 * prompt. The third argument to f_inputdialog() specifies the value to return
8249 * when the user cancels the prompt.
8250 */
8251 void
8252get_user_input(
8253 typval_T *argvars,
8254 typval_T *rettv,
8255 int inputdialog,
8256 int secret)
8257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008258 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 char_u *p = NULL;
8260 int c;
8261 char_u buf[NUMBUFLEN];
8262 int cmd_silent_save = cmd_silent;
8263 char_u *defstr = (char_u *)"";
8264 int xp_type = EXPAND_NOTHING;
8265 char_u *xp_arg = NULL;
8266
8267 rettv->v_type = VAR_STRING;
8268 rettv->vval.v_string = NULL;
8269
8270#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008271 /* While starting up, there is no place to enter text. When running tests
8272 * with --not-a-term we assume feedkeys() will be used. */
8273 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 return;
8275#endif
8276
8277 cmd_silent = FALSE; /* Want to see the prompt. */
8278 if (prompt != NULL)
8279 {
8280 /* Only the part of the message after the last NL is considered as
8281 * prompt for the command line */
8282 p = vim_strrchr(prompt, '\n');
8283 if (p == NULL)
8284 p = prompt;
8285 else
8286 {
8287 ++p;
8288 c = *p;
8289 *p = NUL;
8290 msg_start();
8291 msg_clr_eos();
8292 msg_puts_attr(prompt, echo_attr);
8293 msg_didout = FALSE;
8294 msg_starthere();
8295 *p = c;
8296 }
8297 cmdline_row = msg_row;
8298
8299 if (argvars[1].v_type != VAR_UNKNOWN)
8300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008301 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 if (defstr != NULL)
8303 stuffReadbuffSpec(defstr);
8304
8305 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8306 {
8307 char_u *xp_name;
8308 int xp_namelen;
8309 long argt;
8310
8311 /* input() with a third argument: completion */
8312 rettv->vval.v_string = NULL;
8313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 if (xp_name == NULL)
8316 return;
8317
8318 xp_namelen = (int)STRLEN(xp_name);
8319
8320 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8321 &xp_arg) == FAIL)
8322 return;
8323 }
8324 }
8325
8326 if (defstr != NULL)
8327 {
8328 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330 ex_normal_busy = 0;
8331 rettv->vval.v_string =
8332 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8333 xp_type, xp_arg);
8334 ex_normal_busy = save_ex_normal_busy;
8335 }
8336 if (inputdialog && rettv->vval.v_string == NULL
8337 && argvars[1].v_type != VAR_UNKNOWN
8338 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008339 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 &argvars[2], buf));
8341
8342 vim_free(xp_arg);
8343
8344 /* since the user typed this, no need to wait for return */
8345 need_wait_return = FALSE;
8346 msg_didout = FALSE;
8347 }
8348 cmd_silent = cmd_silent_save;
8349}
8350
8351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 * ":echo expr1 ..." print each argument separated with a space, add a
8353 * newline at the end.
8354 * ":echon expr1 ..." print each argument plain.
8355 */
8356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008357ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
8359 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 char_u *p;
8363 int needclr = TRUE;
8364 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008366 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008367 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
8369 if (eap->skip)
8370 ++emsg_skip;
8371 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8372 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008373 /* If eval1() causes an error message the text from the command may
8374 * still need to be cleared. E.g., "echo 22,44". */
8375 need_clr_eos = needclr;
8376
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008378 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {
8380 /*
8381 * Report the invalid expression unless the expression evaluation
8382 * has been cancelled due to an aborting error, an interrupt, or an
8383 * exception.
8384 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008385 if (!aborting() && did_emsg == did_emsg_before
8386 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008388 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 break;
8390 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008391 need_clr_eos = FALSE;
8392
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 if (!eap->skip)
8394 {
8395 if (atstart)
8396 {
8397 atstart = FALSE;
8398 /* Call msg_start() after eval1(), evaluating the expression
8399 * may cause a message to appear. */
8400 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008401 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008402 /* Mark the saved text as finishing the line, so that what
8403 * follows is displayed on a new line when scrolling back
8404 * at the more prompt. */
8405 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 }
8409 else if (eap->cmdidx == CMD_echo)
8410 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008411 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008412 if (p != NULL)
8413 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008417 if (*p != TAB && needclr)
8418 {
8419 /* remove any text still there from the command */
8420 msg_clr_eos();
8421 needclr = FALSE;
8422 }
8423 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008426 {
8427#ifdef FEAT_MBYTE
8428 if (has_mbyte)
8429 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008430 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008431
8432 (void)msg_outtrans_len_attr(p, i, echo_attr);
8433 p += i - 1;
8434 }
8435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008437 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008442 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 arg = skipwhite(arg);
8444 }
8445 eap->nextcmd = check_nextcmd(arg);
8446
8447 if (eap->skip)
8448 --emsg_skip;
8449 else
8450 {
8451 /* remove text that may still be there from the command */
8452 if (needclr)
8453 msg_clr_eos();
8454 if (eap->cmdidx == CMD_echo)
8455 msg_end();
8456 }
8457}
8458
8459/*
8460 * ":echohl {name}".
8461 */
8462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008463ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008465 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466}
8467
8468/*
8469 * ":execute expr1 ..." execute the result of an expression.
8470 * ":echomsg expr1 ..." Print a message
8471 * ":echoerr expr1 ..." Print an error
8472 * Each gets spaces around each argument and a newline at the end for
8473 * echo commands
8474 */
8475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008476ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 int ret = OK;
8481 char_u *p;
8482 garray_T ga;
8483 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008484 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485
8486 ga_init2(&ga, 1, 80);
8487
8488 if (eap->skip)
8489 ++emsg_skip;
8490 while (*arg != NUL && *arg != '|' && *arg != '\n')
8491 {
8492 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {
8495 /*
8496 * Report the invalid expression unless the expression evaluation
8497 * has been cancelled due to an aborting error, an interrupt, or an
8498 * exception.
8499 */
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008500 if (!aborting() && did_emsg == save_did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 EMSG2(_(e_invexpr2), p);
8502 ret = FAIL;
8503 break;
8504 }
8505
8506 if (!eap->skip)
8507 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008508 char_u buf[NUMBUFLEN];
8509
8510 if (eap->cmdidx == CMD_execute)
8511 p = tv_get_string_buf(&rettv, buf);
8512 else
8513 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 len = (int)STRLEN(p);
8515 if (ga_grow(&ga, len + 2) == FAIL)
8516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008517 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 ret = FAIL;
8519 break;
8520 }
8521 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 ga.ga_len += len;
8525 }
8526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 arg = skipwhite(arg);
8529 }
8530
8531 if (ret != FAIL && ga.ga_data != NULL)
8532 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008533 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8534 {
8535 /* Mark the already saved text as finishing the line, so that what
8536 * follows is displayed on a new line when scrolling back at the
8537 * more prompt. */
8538 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008539 }
8540
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008544 out_flush();
8545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 else if (eap->cmdidx == CMD_echoerr)
8547 {
8548 /* We don't want to abort following commands, restore did_emsg. */
8549 save_did_emsg = did_emsg;
8550 EMSG((char_u *)ga.ga_data);
8551 if (!force_abort)
8552 did_emsg = save_did_emsg;
8553 }
8554 else if (eap->cmdidx == CMD_execute)
8555 do_cmdline((char_u *)ga.ga_data,
8556 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8557 }
8558
8559 ga_clear(&ga);
8560
8561 if (eap->skip)
8562 --emsg_skip;
8563
8564 eap->nextcmd = check_nextcmd(arg);
8565}
8566
8567/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 * Find window specified by "vp" in tabpage "tp".
8569 */
8570 win_T *
8571find_win_by_nr(
8572 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008573 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008576 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 if (nr < 0)
8579 return NULL;
8580 if (nr == 0)
8581 return curwin;
8582
Bram Moolenaar29323592016-07-24 22:04:11 +02008583 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8584 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 if (nr >= LOWEST_WIN_ID)
8586 {
8587 if (wp->w_id == nr)
8588 return wp;
8589 }
8590 else if (--nr <= 0)
8591 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 if (nr >= LOWEST_WIN_ID)
8594 return NULL;
8595 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596}
8597
8598/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008599 * Find a window: When using a Window ID in any tab page, when using a number
8600 * in the current tab page.
8601 */
8602 win_T *
8603find_win_by_nr_or_id(typval_T *vp)
8604{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008605 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008606
8607 if (nr >= LOWEST_WIN_ID)
8608 return win_id2wp(vp);
8609 return find_win_by_nr(vp, NULL);
8610}
8611
8612/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 * Find window specified by "wvp" in tabpage "tvp".
8614 */
8615 win_T *
8616find_tabwin(
8617 typval_T *wvp, /* VAR_UNKNOWN for current window */
8618 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8619{
8620 win_T *wp = NULL;
8621 tabpage_T *tp = NULL;
8622 long n;
8623
8624 if (wvp->v_type != VAR_UNKNOWN)
8625 {
8626 if (tvp->v_type != VAR_UNKNOWN)
8627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008628 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 if (n >= 0)
8630 tp = find_tabpage(n);
8631 }
8632 else
8633 tp = curtab;
8634
8635 if (tp != NULL)
8636 wp = find_win_by_nr(wvp, tp);
8637 }
8638 else
8639 wp = curwin;
8640
8641 return wp;
8642}
8643
8644/*
8645 * getwinvar() and gettabwinvar()
8646 */
8647 void
8648getwinvar(
8649 typval_T *argvars,
8650 typval_T *rettv,
8651 int off) /* 1 for gettabwinvar() */
8652{
8653 win_T *win;
8654 char_u *varname;
8655 dictitem_T *v;
8656 tabpage_T *tp = NULL;
8657 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 win_T *oldcurwin;
8659 tabpage_T *oldtabpage;
8660 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008663 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 else
8665 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008667 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 ++emsg_off;
8669
8670 rettv->v_type = VAR_STRING;
8671 rettv->vval.v_string = NULL;
8672
8673 if (win != NULL && varname != NULL)
8674 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 /* Set curwin to be our win, temporarily. Also set the tabpage,
8676 * otherwise the window is not valid. Only do this when needed,
8677 * autocommands get blocked. */
8678 need_switch_win = !(tp == curtab && win == curwin);
8679 if (!need_switch_win
8680 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008682 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008684 if (varname[1] == NUL)
8685 {
8686 /* get all window-local options in a dict */
8687 dict_T *opts = get_winbuf_options(FALSE);
8688
8689 if (opts != NULL)
8690 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008691 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008692 done = TRUE;
8693 }
8694 }
8695 else if (get_option_tv(&varname, rettv, 1) == OK)
8696 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 done = TRUE;
8698 }
8699 else
8700 {
8701 /* Look up the variable. */
8702 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8703 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8704 varname, FALSE);
8705 if (v != NULL)
8706 {
8707 copy_tv(&v->di_tv, rettv);
8708 done = TRUE;
8709 }
8710 }
8711 }
8712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713 if (need_switch_win)
8714 /* restore previous notion of curwin */
8715 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 }
8717
8718 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8719 /* use the default return value */
8720 copy_tv(&argvars[off + 2], rettv);
8721
8722 --emsg_off;
8723}
8724
8725/*
8726 * "setwinvar()" and "settabwinvar()" functions
8727 */
8728 void
8729setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8730{
8731 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 win_T *save_curwin;
8733 tabpage_T *save_curtab;
8734 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 char_u *varname, *winvarname;
8736 typval_T *varp;
8737 char_u nbuf[NUMBUFLEN];
8738 tabpage_T *tp = NULL;
8739
8740 if (check_restricted() || check_secure())
8741 return;
8742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008744 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 else
8746 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008748 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 varp = &argvars[off + 2];
8750
8751 if (win != NULL && varname != NULL && varp != NULL)
8752 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 need_switch_win = !(tp == curtab && win == curwin);
8754 if (!need_switch_win
8755 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 {
8757 if (*varname == '&')
8758 {
8759 long numval;
8760 char_u *strval;
8761 int error = FALSE;
8762
8763 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008764 numval = (long)tv_get_number_chk(varp, &error);
8765 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 if (!error && strval != NULL)
8767 set_option_value(varname, numval, strval, OPT_LOCAL);
8768 }
8769 else
8770 {
8771 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8772 if (winvarname != NULL)
8773 {
8774 STRCPY(winvarname, "w:");
8775 STRCPY(winvarname + 2, varname);
8776 set_var(winvarname, varp, TRUE);
8777 vim_free(winvarname);
8778 }
8779 }
8780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 if (need_switch_win)
8782 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783 }
8784}
8785
8786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8788 * "arg" points to the "&" or '+' when called, to "option" when returning.
8789 * Returns NULL when no option name found. Otherwise pointer to the char
8790 * after the option name.
8791 */
8792 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008793find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 char_u *p = *arg;
8796
8797 ++p;
8798 if (*p == 'g' && p[1] == ':')
8799 {
8800 *opt_flags = OPT_GLOBAL;
8801 p += 2;
8802 }
8803 else if (*p == 'l' && p[1] == ':')
8804 {
8805 *opt_flags = OPT_LOCAL;
8806 p += 2;
8807 }
8808 else
8809 *opt_flags = 0;
8810
8811 if (!ASCII_ISALPHA(*p))
8812 return NULL;
8813 *arg = p;
8814
8815 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8816 p += 4; /* termcap option */
8817 else
8818 while (ASCII_ISALPHA(*p))
8819 ++p;
8820 return p;
8821}
8822
8823/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008824 * Return the autoload script name for a function or variable name.
8825 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008827 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008828autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008829{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008830 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008831 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008832
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008833 /* Get the script file name: replace '#' with '/', append ".vim". */
8834 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8835 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008836 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008837 STRCPY(scriptname, "autoload/");
8838 STRCAT(scriptname, name);
8839 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8840 STRCAT(scriptname, ".vim");
8841 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8842 *p = '/';
8843 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844}
8845
8846/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008847 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848 * Return TRUE if a package was loaded.
8849 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008851script_autoload(
8852 char_u *name,
8853 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008854{
8855 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008856 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008857 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008858 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008859
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008860 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008861 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008862 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008863 return FALSE;
8864
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008865 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008866
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008867 /* Find the name in the list of previously loaded package names. Skip
8868 * "autoload/", it's always the same. */
8869 for (i = 0; i < ga_loaded.ga_len; ++i)
8870 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8871 break;
8872 if (!reload && i < ga_loaded.ga_len)
8873 ret = FALSE; /* was loaded already */
8874 else
8875 {
8876 /* Remember the name if it wasn't loaded already. */
8877 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8878 {
8879 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8880 tofree = NULL;
8881 }
8882
8883 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008884 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008885 ret = TRUE;
8886 }
8887
8888 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008889 return ret;
8890}
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8893typedef enum
8894{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008895 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8896 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8897 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898} var_flavour_T;
8899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008901var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903 char_u *p = varname;
8904
8905 if (ASCII_ISUPPER(*p))
8906 {
8907 while (*(++p))
8908 if (ASCII_ISLOWER(*p))
8909 return VAR_FLAVOUR_SESSION;
8910 return VAR_FLAVOUR_VIMINFO;
8911 }
8912 else
8913 return VAR_FLAVOUR_DEFAULT;
8914}
8915#endif
8916
8917#if defined(FEAT_VIMINFO) || defined(PROTO)
8918/*
8919 * Restore global vars that start with a capital from the viminfo file
8920 */
8921 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008922read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008925 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008926 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008927 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
8929 if (!writing && (find_viminfo_parameter('!') != NULL))
8930 {
8931 tab = vim_strchr(virp->vir_line + 1, '\t');
8932 if (tab != NULL)
8933 {
8934 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008935 switch (*tab)
8936 {
8937 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008939 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008940#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008941 case 'D': type = VAR_DICT; break;
8942 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008943 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008944 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
8947 tab = vim_strchr(tab, '\t');
8948 if (tab != NULL)
8949 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950 tv.v_type = type;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008951 if (type == VAR_STRING || type == VAR_DICT ||
8952 type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008953 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008955#ifdef FEAT_FLOAT
8956 else if (type == VAR_FLOAT)
8957 (void)string2float(tab + 1, &tv.vval.v_float);
8958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008960 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008961 if (type == VAR_DICT || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008962 {
8963 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8964
8965 if (etv == NULL)
8966 /* Failed to parse back the dict or list, use it as a
8967 * string. */
8968 tv.v_type = VAR_STRING;
8969 else
8970 {
8971 vim_free(tv.vval.v_string);
8972 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008973 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008974 }
8975 }
8976
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008977 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008978 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008979 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008980 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008981
8982 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008983 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008984 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8985 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008986 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
8988 }
8989 }
8990
8991 return viminfo_readline(virp);
8992}
8993
8994/*
8995 * Write global vars that start with a capital to the viminfo file
8996 */
8997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008998write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 hashitem_T *hi;
9001 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009002 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009003 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009004 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 if (find_viminfo_parameter('!') == NULL)
9009 return;
9010
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009011 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009013 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009014 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009018 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 this_var = HI2DI(hi);
9020 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009022 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009023 {
9024 case VAR_STRING: s = "STR"; break;
9025 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009026 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009027 case VAR_DICT: s = "DIC"; break;
9028 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009029 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009030 case VAR_SPECIAL: s = "XPL"; break;
9031
9032 case VAR_UNKNOWN:
9033 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009034 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009035 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009036 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009037 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009040 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009041 if (p != NULL)
9042 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009043 vim_free(tofree);
9044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
9046 }
9047}
9048#endif
9049
9050#if defined(FEAT_SESSION) || defined(PROTO)
9051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009052store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 hashitem_T *hi;
9055 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009056 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 char_u *p, *t;
9058
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009059 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009060 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009062 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009064 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 this_var = HI2DI(hi);
9066 if ((this_var->di_tv.v_type == VAR_NUMBER
9067 || this_var->di_tv.v_type == VAR_STRING)
9068 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009069 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009070 /* Escape special characters with a backslash. Turn a LF and
9071 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009073 (char_u *)"\\\"\n\r");
9074 if (p == NULL) /* out of memory */
9075 break;
9076 for (t = p; *t != NUL; ++t)
9077 if (*t == '\n')
9078 *t = 'n';
9079 else if (*t == '\r')
9080 *t = 'r';
9081 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 this_var->di_key,
9083 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9084 : ' ',
9085 p,
9086 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9087 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009088 || put_eol(fd) == FAIL)
9089 {
9090 vim_free(p);
9091 return FAIL;
9092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 vim_free(p);
9094 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009095#ifdef FEAT_FLOAT
9096 else if (this_var->di_tv.v_type == VAR_FLOAT
9097 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9098 {
9099 float_T f = this_var->di_tv.vval.v_float;
9100 int sign = ' ';
9101
9102 if (f < 0)
9103 {
9104 f = -f;
9105 sign = '-';
9106 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009107 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009108 this_var->di_key, sign, f) < 0)
9109 || put_eol(fd) == FAIL)
9110 return FAIL;
9111 }
9112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 }
9114 }
9115 return OK;
9116}
9117#endif
9118
Bram Moolenaar661b1822005-07-28 22:36:45 +00009119/*
9120 * Display script name where an item was last set.
9121 * Should only be invoked when 'verbose' is non-zero.
9122 */
9123 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009124last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009125{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009126 char_u *p;
9127
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009128 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009129 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009130 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009131 if (p != NULL)
9132 {
9133 verbose_enter();
9134 MSG_PUTS(_("\n\tLast set from "));
9135 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009136 if (script_ctx.sc_lnum > 0)
9137 {
9138 MSG_PUTS(_(" line "));
9139 msg_outnum((long)script_ctx.sc_lnum);
9140 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009141 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009142 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009143 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009144 }
9145}
9146
Bram Moolenaar53744302015-07-17 17:38:22 +02009147/* reset v:option_new, v:option_old and v:option_type */
9148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009149reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009150{
9151 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9152 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9153 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9154}
9155
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156/*
9157 * Prepare "gap" for an assert error and add the sourcing position.
9158 */
9159 void
9160prepare_assert_error(garray_T *gap)
9161{
9162 char buf[NUMBUFLEN];
9163
9164 ga_init2(gap, 1, 100);
9165 if (sourcing_name != NULL)
9166 {
9167 ga_concat(gap, sourcing_name);
9168 if (sourcing_lnum > 0)
9169 ga_concat(gap, (char_u *)" ");
9170 }
9171 if (sourcing_lnum > 0)
9172 {
9173 sprintf(buf, "line %ld", (long)sourcing_lnum);
9174 ga_concat(gap, (char_u *)buf);
9175 }
9176 if (sourcing_name != NULL || sourcing_lnum > 0)
9177 ga_concat(gap, (char_u *)": ");
9178}
9179
9180/*
9181 * Add an assert error to v:errors.
9182 */
9183 void
9184assert_error(garray_T *gap)
9185{
9186 struct vimvar *vp = &vimvars[VV_ERRORS];
9187
9188 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9189 /* Make sure v:errors is a list. */
9190 set_vim_var_list(VV_ERRORS, list_alloc());
9191 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9192}
9193
Bram Moolenaar65a54642018-04-28 16:56:53 +02009194 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195assert_equal_common(typval_T *argvars, assert_type_T atype)
9196{
9197 garray_T ga;
9198
9199 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9200 != (atype == ASSERT_EQUAL))
9201 {
9202 prepare_assert_error(&ga);
9203 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9204 atype);
9205 assert_error(&ga);
9206 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009207 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009209 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009210}
9211
Bram Moolenaar65a54642018-04-28 16:56:53 +02009212 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009213assert_equalfile(typval_T *argvars)
9214{
9215 char_u buf1[NUMBUFLEN];
9216 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009217 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9218 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009219 garray_T ga;
9220 FILE *fd1;
9221 FILE *fd2;
9222
9223 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009224 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009225
9226 IObuff[0] = NUL;
9227 fd1 = mch_fopen((char *)fname1, READBIN);
9228 if (fd1 == NULL)
9229 {
9230 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9231 }
9232 else
9233 {
9234 fd2 = mch_fopen((char *)fname2, READBIN);
9235 if (fd2 == NULL)
9236 {
9237 fclose(fd1);
9238 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9239 }
9240 else
9241 {
9242 int c1, c2;
9243 long count = 0;
9244
9245 for (;;)
9246 {
9247 c1 = fgetc(fd1);
9248 c2 = fgetc(fd2);
9249 if (c1 == EOF)
9250 {
9251 if (c2 != EOF)
9252 STRCPY(IObuff, "first file is shorter");
9253 break;
9254 }
9255 else if (c2 == EOF)
9256 {
9257 STRCPY(IObuff, "second file is shorter");
9258 break;
9259 }
9260 else if (c1 != c2)
9261 {
9262 vim_snprintf((char *)IObuff, IOSIZE,
9263 "difference at byte %ld", count);
9264 break;
9265 }
9266 ++count;
9267 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009268 fclose(fd1);
9269 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009270 }
9271 }
9272 if (IObuff[0] != NUL)
9273 {
9274 prepare_assert_error(&ga);
9275 ga_concat(&ga, IObuff);
9276 assert_error(&ga);
9277 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009278 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009279 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009280 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009281}
9282
Bram Moolenaar65a54642018-04-28 16:56:53 +02009283 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284assert_match_common(typval_T *argvars, assert_type_T atype)
9285{
9286 garray_T ga;
9287 char_u buf1[NUMBUFLEN];
9288 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009289 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9290 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291
9292 if (pat == NULL || text == NULL)
9293 EMSG(_(e_invarg));
9294 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9295 {
9296 prepare_assert_error(&ga);
9297 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9298 atype);
9299 assert_error(&ga);
9300 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009301 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009303 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304}
9305
Bram Moolenaar65a54642018-04-28 16:56:53 +02009306 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009307assert_inrange(typval_T *argvars)
9308{
9309 garray_T ga;
9310 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009311 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9312 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9313 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009314 char_u *tofree;
9315 char msg[200];
9316 char_u numbuf[NUMBUFLEN];
9317
9318 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009319 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009320 if (actual < lower || actual > upper)
9321 {
9322 prepare_assert_error(&ga);
9323 if (argvars[3].v_type != VAR_UNKNOWN)
9324 {
9325 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9326 vim_free(tofree);
9327 }
9328 else
9329 {
9330 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9331 (long)lower, (long)upper, (long)actual);
9332 ga_concat(&ga, (char_u *)msg);
9333 }
9334 assert_error(&ga);
9335 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009336 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009337 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009338 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009339}
9340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341/*
9342 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009343 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009345 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346assert_bool(typval_T *argvars, int isTrue)
9347{
9348 int error = FALSE;
9349 garray_T ga;
9350
9351 if (argvars[0].v_type == VAR_SPECIAL
9352 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009353 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009355 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009356 || error)
9357 {
9358 prepare_assert_error(&ga);
9359 fill_assert_error(&ga, &argvars[1],
9360 (char_u *)(isTrue ? "True" : "False"),
9361 NULL, &argvars[0], ASSERT_OTHER);
9362 assert_error(&ga);
9363 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009364 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009366 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367}
9368
Bram Moolenaar65a54642018-04-28 16:56:53 +02009369 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009370assert_report(typval_T *argvars)
9371{
9372 garray_T ga;
9373
9374 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009375 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009376 assert_error(&ga);
9377 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009378 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009379}
9380
Bram Moolenaar65a54642018-04-28 16:56:53 +02009381 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382assert_exception(typval_T *argvars)
9383{
9384 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009385 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009386
9387 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9388 {
9389 prepare_assert_error(&ga);
9390 ga_concat(&ga, (char_u *)"v:exception is not set");
9391 assert_error(&ga);
9392 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009393 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009394 }
9395 else if (error != NULL
9396 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9397 {
9398 prepare_assert_error(&ga);
9399 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9400 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9401 assert_error(&ga);
9402 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009403 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009405 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009406}
9407
Bram Moolenaar65a54642018-04-28 16:56:53 +02009408 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009409assert_beeps(typval_T *argvars)
9410{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009411 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009412 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009413 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009414
9415 called_vim_beep = FALSE;
9416 suppress_errthrow = TRUE;
9417 emsg_silent = FALSE;
9418 do_cmdline_cmd(cmd);
9419 if (!called_vim_beep)
9420 {
9421 prepare_assert_error(&ga);
9422 ga_concat(&ga, (char_u *)"command did not beep: ");
9423 ga_concat(&ga, cmd);
9424 assert_error(&ga);
9425 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009426 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009427 }
9428
9429 suppress_errthrow = FALSE;
9430 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009431 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009432}
9433
Bram Moolenaar65a54642018-04-28 16:56:53 +02009434 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009435assert_fails(typval_T *argvars)
9436{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009437 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009439 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009440 char_u numbuf[NUMBUFLEN];
9441 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009442
9443 called_emsg = FALSE;
9444 suppress_errthrow = TRUE;
9445 emsg_silent = TRUE;
9446 do_cmdline_cmd(cmd);
9447 if (!called_emsg)
9448 {
9449 prepare_assert_error(&ga);
9450 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009451 if (argvars[1].v_type != VAR_UNKNOWN
9452 && argvars[2].v_type != VAR_UNKNOWN)
9453 {
9454 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9455 vim_free(tofree);
9456 }
9457 else
9458 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 assert_error(&ga);
9460 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009461 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 }
9463 else if (argvars[1].v_type != VAR_UNKNOWN)
9464 {
9465 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009466 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467
9468 if (error == NULL
9469 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9470 {
9471 prepare_assert_error(&ga);
9472 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9473 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9474 assert_error(&ga);
9475 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009476 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477 }
9478 }
9479
9480 called_emsg = FALSE;
9481 suppress_errthrow = FALSE;
9482 emsg_silent = FALSE;
9483 emsg_on_display = FALSE;
9484 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009485 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486}
9487
9488/*
9489 * Append "str" to "gap", escaping unprintable characters.
9490 * Changes NL to \n, CR to \r, etc.
9491 */
9492 static void
9493ga_concat_esc(garray_T *gap, char_u *str)
9494{
9495 char_u *p;
9496 char_u buf[NUMBUFLEN];
9497
9498 if (str == NULL)
9499 {
9500 ga_concat(gap, (char_u *)"NULL");
9501 return;
9502 }
9503
9504 for (p = str; *p != NUL; ++p)
9505 switch (*p)
9506 {
9507 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9508 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9509 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9510 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9511 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9512 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9513 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9514 default:
9515 if (*p < ' ')
9516 {
9517 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9518 ga_concat(gap, buf);
9519 }
9520 else
9521 ga_append(gap, *p);
9522 break;
9523 }
9524}
9525
9526/*
9527 * Fill "gap" with information about an assert error.
9528 */
9529 void
9530fill_assert_error(
9531 garray_T *gap,
9532 typval_T *opt_msg_tv,
9533 char_u *exp_str,
9534 typval_T *exp_tv,
9535 typval_T *got_tv,
9536 assert_type_T atype)
9537{
9538 char_u numbuf[NUMBUFLEN];
9539 char_u *tofree;
9540
9541 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9542 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009543 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9544 vim_free(tofree);
9545 ga_concat(gap, (char_u *)": ");
9546 }
9547
9548 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9549 ga_concat(gap, (char_u *)"Pattern ");
9550 else if (atype == ASSERT_NOTEQUAL)
9551 ga_concat(gap, (char_u *)"Expected not equal to ");
9552 else
9553 ga_concat(gap, (char_u *)"Expected ");
9554 if (exp_str == NULL)
9555 {
9556 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 vim_free(tofree);
9558 }
9559 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009560 ga_concat_esc(gap, exp_str);
9561 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009562 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009563 if (atype == ASSERT_MATCH)
9564 ga_concat(gap, (char_u *)" does not match ");
9565 else if (atype == ASSERT_NOTMATCH)
9566 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009567 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009568 ga_concat(gap, (char_u *)" but got ");
9569 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9570 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 }
9572}
9573
Bram Moolenaar31988702018-02-13 12:57:42 +01009574/*
9575 * Compare "typ1" and "typ2". Put the result in "typ1".
9576 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009577 int
9578typval_compare(
9579 typval_T *typ1, /* first operand */
9580 typval_T *typ2, /* second operand */
9581 exptype_T type, /* operator */
9582 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009583 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009584{
9585 int i;
9586 varnumber_T n1, n2;
9587 char_u *s1, *s2;
9588 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9589
Bram Moolenaar31988702018-02-13 12:57:42 +01009590 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009591 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009592 /* For "is" a different type always means FALSE, for "notis"
9593 * it means TRUE. */
9594 n1 = (type == TYPE_NEQUAL);
9595 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009596 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9597 {
9598 if (type_is)
9599 {
9600 n1 = (typ1->v_type == typ2->v_type
9601 && typ1->vval.v_blob == typ2->vval.v_blob);
9602 if (type == TYPE_NEQUAL)
9603 n1 = !n1;
9604 }
9605 else if (typ1->v_type != typ2->v_type
9606 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9607 {
9608 if (typ1->v_type != typ2->v_type)
9609 EMSG(_("E977: Can only compare Blob with Blob"));
9610 else
9611 EMSG(_(e_invalblob));
9612 clear_tv(typ1);
9613 return FAIL;
9614 }
9615 else
9616 {
9617 // Compare two Blobs for being equal or unequal.
9618 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9619 if (type == TYPE_NEQUAL)
9620 n1 = !n1;
9621 }
9622 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009623 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9624 {
9625 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009626 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009627 n1 = (typ1->v_type == typ2->v_type
9628 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009629 if (type == TYPE_NEQUAL)
9630 n1 = !n1;
9631 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009632 else if (typ1->v_type != typ2->v_type
9633 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009634 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009635 if (typ1->v_type != typ2->v_type)
9636 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009637 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009638 EMSG(_("E692: Invalid operation for List"));
9639 clear_tv(typ1);
9640 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009641 }
9642 else
9643 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009644 /* Compare two Lists for being equal or unequal. */
9645 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9646 ic, FALSE);
9647 if (type == TYPE_NEQUAL)
9648 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009649 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009650 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009651
9652 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9653 {
9654 if (type_is)
9655 {
9656 n1 = (typ1->v_type == typ2->v_type
9657 && typ1->vval.v_dict == typ2->vval.v_dict);
9658 if (type == TYPE_NEQUAL)
9659 n1 = !n1;
9660 }
9661 else if (typ1->v_type != typ2->v_type
9662 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9663 {
9664 if (typ1->v_type != typ2->v_type)
9665 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9666 else
9667 EMSG(_("E736: Invalid operation for Dictionary"));
9668 clear_tv(typ1);
9669 return FAIL;
9670 }
9671 else
9672 {
9673 /* Compare two Dictionaries for being equal or unequal. */
9674 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9675 ic, FALSE);
9676 if (type == TYPE_NEQUAL)
9677 n1 = !n1;
9678 }
9679 }
9680
9681 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9682 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9683 {
9684 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9685 {
9686 EMSG(_("E694: Invalid operation for Funcrefs"));
9687 clear_tv(typ1);
9688 return FAIL;
9689 }
9690 if ((typ1->v_type == VAR_PARTIAL
9691 && typ1->vval.v_partial == NULL)
9692 || (typ2->v_type == VAR_PARTIAL
9693 && typ2->vval.v_partial == NULL))
9694 /* when a partial is NULL assume not equal */
9695 n1 = FALSE;
9696 else if (type_is)
9697 {
9698 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9699 /* strings are considered the same if their value is
9700 * the same */
9701 n1 = tv_equal(typ1, typ2, ic, FALSE);
9702 else if (typ1->v_type == VAR_PARTIAL
9703 && typ2->v_type == VAR_PARTIAL)
9704 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9705 else
9706 n1 = FALSE;
9707 }
9708 else
9709 n1 = tv_equal(typ1, typ2, ic, FALSE);
9710 if (type == TYPE_NEQUAL)
9711 n1 = !n1;
9712 }
9713
9714#ifdef FEAT_FLOAT
9715 /*
9716 * If one of the two variables is a float, compare as a float.
9717 * When using "=~" or "!~", always compare as string.
9718 */
9719 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9720 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9721 {
9722 float_T f1, f2;
9723
9724 if (typ1->v_type == VAR_FLOAT)
9725 f1 = typ1->vval.v_float;
9726 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009727 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009728 if (typ2->v_type == VAR_FLOAT)
9729 f2 = typ2->vval.v_float;
9730 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009731 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009732 n1 = FALSE;
9733 switch (type)
9734 {
9735 case TYPE_EQUAL: n1 = (f1 == f2); break;
9736 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9737 case TYPE_GREATER: n1 = (f1 > f2); break;
9738 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9739 case TYPE_SMALLER: n1 = (f1 < f2); break;
9740 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9741 case TYPE_UNKNOWN:
9742 case TYPE_MATCH:
9743 case TYPE_NOMATCH: break; /* avoid gcc warning */
9744 }
9745 }
9746#endif
9747
9748 /*
9749 * If one of the two variables is a number, compare as a number.
9750 * When using "=~" or "!~", always compare as string.
9751 */
9752 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9753 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009755 n1 = tv_get_number(typ1);
9756 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009757 switch (type)
9758 {
9759 case TYPE_EQUAL: n1 = (n1 == n2); break;
9760 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9761 case TYPE_GREATER: n1 = (n1 > n2); break;
9762 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9763 case TYPE_SMALLER: n1 = (n1 < n2); break;
9764 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9765 case TYPE_UNKNOWN:
9766 case TYPE_MATCH:
9767 case TYPE_NOMATCH: break; /* avoid gcc warning */
9768 }
9769 }
9770 else
9771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009772 s1 = tv_get_string_buf(typ1, buf1);
9773 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009774 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9775 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9776 else
9777 i = 0;
9778 n1 = FALSE;
9779 switch (type)
9780 {
9781 case TYPE_EQUAL: n1 = (i == 0); break;
9782 case TYPE_NEQUAL: n1 = (i != 0); break;
9783 case TYPE_GREATER: n1 = (i > 0); break;
9784 case TYPE_GEQUAL: n1 = (i >= 0); break;
9785 case TYPE_SMALLER: n1 = (i < 0); break;
9786 case TYPE_SEQUAL: n1 = (i <= 0); break;
9787
9788 case TYPE_MATCH:
9789 case TYPE_NOMATCH:
9790 n1 = pattern_match(s2, s1, ic);
9791 if (type == TYPE_NOMATCH)
9792 n1 = !n1;
9793 break;
9794
9795 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9796 }
9797 }
9798 clear_tv(typ1);
9799 typ1->v_type = VAR_NUMBER;
9800 typ1->vval.v_number = n1;
9801
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009802 return OK;
9803}
9804
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009805 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009806typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009807{
9808 char_u *tofree;
9809 char_u numbuf[NUMBUFLEN];
9810 char_u *ret = NULL;
9811
9812 if (arg == NULL)
9813 return vim_strsave((char_u *)"(does not exist)");
9814 ret = tv2string(arg, &tofree, numbuf, 0);
9815 /* Make a copy if we have a value but it's not in allocated memory. */
9816 if (ret != NULL && tofree == NULL)
9817 ret = vim_strsave(ret);
9818 return ret;
9819}
9820
9821 int
9822var_exists(char_u *var)
9823{
9824 char_u *name;
9825 char_u *tofree;
9826 typval_T tv;
9827 int len = 0;
9828 int n = FALSE;
9829
9830 /* get_name_len() takes care of expanding curly braces */
9831 name = var;
9832 len = get_name_len(&var, &tofree, TRUE, FALSE);
9833 if (len > 0)
9834 {
9835 if (tofree != NULL)
9836 name = tofree;
9837 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9838 if (n)
9839 {
9840 /* handle d.key, l[idx], f(expr) */
9841 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9842 if (n)
9843 clear_tv(&tv);
9844 }
9845 }
9846 if (*var != NUL)
9847 n = FALSE;
9848
9849 vim_free(tofree);
9850 return n;
9851}
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853#endif /* FEAT_EVAL */
9854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009856#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857
9858#ifdef WIN3264
9859/*
9860 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862
9863/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009864 * Get the short path (8.3) for the filename in "fnamep".
9865 * Only works for a valid file name.
9866 * When the path gets longer "fnamep" is changed and the allocated buffer
9867 * is put in "bufp".
9868 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9869 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 */
9871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009872get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009874 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 char_u *newbuf;
9876
9877 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009878 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 if (l > len - 1)
9880 {
9881 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009882 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 newbuf = vim_strnsave(*fnamep, l);
9884 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009885 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
9887 vim_free(*bufp);
9888 *fnamep = *bufp = newbuf;
9889
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009890 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009891 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 }
9893
9894 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009895 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896}
9897
9898/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009899 * Get the short path (8.3) for the filename in "fname". The converted
9900 * path is returned in "bufp".
9901 *
9902 * Some of the directories specified in "fname" may not exist. This function
9903 * will shorten the existing directories at the beginning of the path and then
9904 * append the remaining non-existing path.
9905 *
9906 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009907 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009908 * bufp - Pointer to an allocated buffer for the filename.
9909 * fnamelen - Length of the filename pointed to by fname
9910 *
9911 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 */
9913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914shortpath_for_invalid_fname(
9915 char_u **fname,
9916 char_u **bufp,
9917 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009919 char_u *short_fname, *save_fname, *pbuf_unused;
9920 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009922 int old_len, len;
9923 int new_len, sfx_len;
9924 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
9926 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009927 old_len = *fnamelen;
9928 save_fname = vim_strnsave(*fname, old_len);
9929 pbuf_unused = NULL;
9930 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009932 endp = save_fname + old_len - 1; /* Find the end of the copy */
9933 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009935 /*
9936 * Try shortening the supplied path till it succeeds by removing one
9937 * directory at a time from the tail of the path.
9938 */
9939 len = 0;
9940 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009942 /* go back one path-separator */
9943 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9944 --endp;
9945 if (endp <= save_fname)
9946 break; /* processed the complete path */
9947
9948 /*
9949 * Replace the path separator with a NUL and try to shorten the
9950 * resulting path.
9951 */
9952 ch = *endp;
9953 *endp = 0;
9954 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009955 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009956 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9957 {
9958 retval = FAIL;
9959 goto theend;
9960 }
9961 *endp = ch; /* preserve the string */
9962
9963 if (len > 0)
9964 break; /* successfully shortened the path */
9965
9966 /* failed to shorten the path. Skip the path separator */
9967 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 }
9969
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009970 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009972 /*
9973 * Succeeded in shortening the path. Now concatenate the shortened
9974 * path with the remaining path at the tail.
9975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009977 /* Compute the length of the new path. */
9978 sfx_len = (int)(save_endp - endp) + 1;
9979 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009981 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009983 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009985 /* There is not enough space in the currently allocated string,
9986 * copy it to a buffer big enough. */
9987 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009989 {
9990 retval = FAIL;
9991 goto theend;
9992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994 else
9995 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009996 /* Transfer short_fname to the main buffer (it's big enough),
9997 * unless get_short_pathname() did its work in-place. */
9998 *fname = *bufp = save_fname;
9999 if (short_fname != save_fname)
10000 vim_strncpy(save_fname, short_fname, len);
10001 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010003
10004 /* concat the not-shortened part of the path */
10005 vim_strncpy(*fname + len, endp, sfx_len);
10006 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010008
10009theend:
10010 vim_free(pbuf_unused);
10011 vim_free(save_fname);
10012
10013 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014}
10015
10016/*
10017 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010018 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 */
10020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010021shortpath_for_partial(
10022 char_u **fnamep,
10023 char_u **bufp,
10024 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
10026 int sepcount, len, tflen;
10027 char_u *p;
10028 char_u *pbuf, *tfname;
10029 int hasTilde;
10030
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010031 /* Count up the path separators from the RHS.. so we know which part
10032 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010034 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (vim_ispathsep(*p))
10036 ++sepcount;
10037
10038 /* Need full path first (use expand_env() to remove a "~/") */
10039 hasTilde = (**fnamep == '~');
10040 if (hasTilde)
10041 pbuf = tfname = expand_env_save(*fnamep);
10042 else
10043 pbuf = tfname = FullName_save(*fnamep, FALSE);
10044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010045 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010047 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10048 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 if (len == 0)
10051 {
10052 /* Don't have a valid filename, so shorten the rest of the
10053 * path if we can. This CAN give us invalid 8.3 filenames, but
10054 * there's not a lot of point in guessing what it might be.
10055 */
10056 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010057 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060
10061 /* Count the paths backward to find the beginning of the desired string. */
10062 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010063 {
10064#ifdef FEAT_MBYTE
10065 if (has_mbyte)
10066 p -= mb_head_off(tfname, p);
10067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 if (vim_ispathsep(*p))
10069 {
10070 if (sepcount == 0 || (hasTilde && sepcount == 1))
10071 break;
10072 else
10073 sepcount --;
10074 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (hasTilde)
10077 {
10078 --p;
10079 if (p >= tfname)
10080 *p = '~';
10081 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010082 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 }
10084 else
10085 ++p;
10086
10087 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10088 vim_free(*bufp);
10089 *fnamelen = (int)STRLEN(p);
10090 *bufp = pbuf;
10091 *fnamep = p;
10092
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010093 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094}
10095#endif /* WIN3264 */
10096
10097/*
10098 * Adjust a filename, according to a string of modifiers.
10099 * *fnamep must be NUL terminated when called. When returning, the length is
10100 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010101 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 * When there is an error, *fnamep is set to NULL.
10103 */
10104 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010106 char_u *src, // string with modifiers
10107 int tilde_file, // "~" is a file name, not $HOME
10108 int *usedlen, // characters after src that are used
10109 char_u **fnamep, // file name so far
10110 char_u **bufp, // buffer for allocated file name or NULL
10111 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
10113 int valid = 0;
10114 char_u *tail;
10115 char_u *s, *p, *pbuf;
10116 char_u dirname[MAXPATHL];
10117 int c;
10118 int has_fullname = 0;
10119#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010120 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 int has_shortname = 0;
10122#endif
10123
10124repeat:
10125 /* ":p" - full path/file_name */
10126 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10127 {
10128 has_fullname = 1;
10129
10130 valid |= VALID_PATH;
10131 *usedlen += 2;
10132
10133 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10134 if ((*fnamep)[0] == '~'
10135#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10136 && ((*fnamep)[1] == '/'
10137# ifdef BACKSLASH_IN_FILENAME
10138 || (*fnamep)[1] == '\\'
10139# endif
10140 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010142 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 )
10144 {
10145 *fnamep = expand_env_save(*fnamep);
10146 vim_free(*bufp); /* free any allocated file name */
10147 *bufp = *fnamep;
10148 if (*fnamep == NULL)
10149 return -1;
10150 }
10151
10152 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010153 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 {
10155 if (vim_ispathsep(*p)
10156 && p[1] == '.'
10157 && (p[2] == NUL
10158 || vim_ispathsep(p[2])
10159 || (p[2] == '.'
10160 && (p[3] == NUL || vim_ispathsep(p[3])))))
10161 break;
10162 }
10163
10164 /* FullName_save() is slow, don't use it when not needed. */
10165 if (*p != NUL || !vim_isAbsName(*fnamep))
10166 {
10167 *fnamep = FullName_save(*fnamep, *p != NUL);
10168 vim_free(*bufp); /* free any allocated file name */
10169 *bufp = *fnamep;
10170 if (*fnamep == NULL)
10171 return -1;
10172 }
10173
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010174#ifdef WIN3264
10175# if _WIN32_WINNT >= 0x0500
10176 if (vim_strchr(*fnamep, '~') != NULL)
10177 {
10178 /* Expand 8.3 filename to full path. Needed to make sure the same
10179 * file does not have two different names.
10180 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10181 p = alloc(_MAX_PATH + 1);
10182 if (p != NULL)
10183 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010184 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010185 {
10186 vim_free(*bufp);
10187 *bufp = *fnamep = p;
10188 }
10189 else
10190 vim_free(p);
10191 }
10192 }
10193# endif
10194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 /* Append a path separator to a directory. */
10196 if (mch_isdir(*fnamep))
10197 {
10198 /* Make room for one or two extra characters. */
10199 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10200 vim_free(*bufp); /* free any allocated file name */
10201 *bufp = *fnamep;
10202 if (*fnamep == NULL)
10203 return -1;
10204 add_pathsep(*fnamep);
10205 }
10206 }
10207
10208 /* ":." - path relative to the current directory */
10209 /* ":~" - path relative to the home directory */
10210 /* ":8" - shortname path - postponed till after */
10211 while (src[*usedlen] == ':'
10212 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10213 {
10214 *usedlen += 2;
10215 if (c == '8')
10216 {
10217#ifdef WIN3264
10218 has_shortname = 1; /* Postpone this. */
10219#endif
10220 continue;
10221 }
10222 pbuf = NULL;
10223 /* Need full path first (use expand_env() to remove a "~/") */
10224 if (!has_fullname)
10225 {
10226 if (c == '.' && **fnamep == '~')
10227 p = pbuf = expand_env_save(*fnamep);
10228 else
10229 p = pbuf = FullName_save(*fnamep, FALSE);
10230 }
10231 else
10232 p = *fnamep;
10233
10234 has_fullname = 0;
10235
10236 if (p != NULL)
10237 {
10238 if (c == '.')
10239 {
10240 mch_dirname(dirname, MAXPATHL);
10241 s = shorten_fname(p, dirname);
10242 if (s != NULL)
10243 {
10244 *fnamep = s;
10245 if (pbuf != NULL)
10246 {
10247 vim_free(*bufp); /* free any allocated file name */
10248 *bufp = pbuf;
10249 pbuf = NULL;
10250 }
10251 }
10252 }
10253 else
10254 {
10255 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10256 /* Only replace it when it starts with '~' */
10257 if (*dirname == '~')
10258 {
10259 s = vim_strsave(dirname);
10260 if (s != NULL)
10261 {
10262 *fnamep = s;
10263 vim_free(*bufp);
10264 *bufp = s;
10265 }
10266 }
10267 }
10268 vim_free(pbuf);
10269 }
10270 }
10271
10272 tail = gettail(*fnamep);
10273 *fnamelen = (int)STRLEN(*fnamep);
10274
10275 /* ":h" - head, remove "/file_name", can be repeated */
10276 /* Don't remove the first "/" or "c:\" */
10277 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10278 {
10279 valid |= VALID_HEAD;
10280 *usedlen += 2;
10281 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010282 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010283 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 *fnamelen = (int)(tail - *fnamep);
10285#ifdef VMS
10286 if (*fnamelen > 0)
10287 *fnamelen += 1; /* the path separator is part of the path */
10288#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010289 if (*fnamelen == 0)
10290 {
10291 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10292 p = vim_strsave((char_u *)".");
10293 if (p == NULL)
10294 return -1;
10295 vim_free(*bufp);
10296 *bufp = *fnamep = tail = p;
10297 *fnamelen = 1;
10298 }
10299 else
10300 {
10301 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010302 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 }
10305
10306 /* ":8" - shortname */
10307 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10308 {
10309 *usedlen += 2;
10310#ifdef WIN3264
10311 has_shortname = 1;
10312#endif
10313 }
10314
10315#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010316 /*
10317 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 */
10319 if (has_shortname)
10320 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010321 /* Copy the string if it is shortened by :h and when it wasn't copied
10322 * yet, because we are going to change it in place. Avoids changing
10323 * the buffer name for "%:8". */
10324 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 {
10326 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010327 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 return -1;
10329 vim_free(*bufp);
10330 *bufp = *fnamep = p;
10331 }
10332
10333 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010334 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 if (!has_fullname && !vim_isAbsName(*fnamep))
10336 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010337 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 return -1;
10339 }
10340 else
10341 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010342 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343
Bram Moolenaardc935552011-08-17 15:23:23 +020010344 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010346 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 return -1;
10348
10349 if (l == 0)
10350 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010351 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010353 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 return -1;
10355 }
10356 *fnamelen = l;
10357 }
10358 }
10359#endif /* WIN3264 */
10360
10361 /* ":t" - tail, just the basename */
10362 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10363 {
10364 *usedlen += 2;
10365 *fnamelen -= (int)(tail - *fnamep);
10366 *fnamep = tail;
10367 }
10368
10369 /* ":e" - extension, can be repeated */
10370 /* ":r" - root, without extension, can be repeated */
10371 while (src[*usedlen] == ':'
10372 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10373 {
10374 /* find a '.' in the tail:
10375 * - for second :e: before the current fname
10376 * - otherwise: The last '.'
10377 */
10378 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10379 s = *fnamep - 2;
10380 else
10381 s = *fnamep + *fnamelen - 1;
10382 for ( ; s > tail; --s)
10383 if (s[0] == '.')
10384 break;
10385 if (src[*usedlen + 1] == 'e') /* :e */
10386 {
10387 if (s > tail)
10388 {
10389 *fnamelen += (int)(*fnamep - (s + 1));
10390 *fnamep = s + 1;
10391#ifdef VMS
10392 /* cut version from the extension */
10393 s = *fnamep + *fnamelen - 1;
10394 for ( ; s > *fnamep; --s)
10395 if (s[0] == ';')
10396 break;
10397 if (s > *fnamep)
10398 *fnamelen = s - *fnamep;
10399#endif
10400 }
10401 else if (*fnamep <= tail)
10402 *fnamelen = 0;
10403 }
10404 else /* :r */
10405 {
10406 if (s > tail) /* remove one extension */
10407 *fnamelen = (int)(s - *fnamep);
10408 }
10409 *usedlen += 2;
10410 }
10411
10412 /* ":s?pat?foo?" - substitute */
10413 /* ":gs?pat?foo?" - global substitute */
10414 if (src[*usedlen] == ':'
10415 && (src[*usedlen + 1] == 's'
10416 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10417 {
10418 char_u *str;
10419 char_u *pat;
10420 char_u *sub;
10421 int sep;
10422 char_u *flags;
10423 int didit = FALSE;
10424
10425 flags = (char_u *)"";
10426 s = src + *usedlen + 2;
10427 if (src[*usedlen + 1] == 'g')
10428 {
10429 flags = (char_u *)"g";
10430 ++s;
10431 }
10432
10433 sep = *s++;
10434 if (sep)
10435 {
10436 /* find end of pattern */
10437 p = vim_strchr(s, sep);
10438 if (p != NULL)
10439 {
10440 pat = vim_strnsave(s, (int)(p - s));
10441 if (pat != NULL)
10442 {
10443 s = p + 1;
10444 /* find end of substitution */
10445 p = vim_strchr(s, sep);
10446 if (p != NULL)
10447 {
10448 sub = vim_strnsave(s, (int)(p - s));
10449 str = vim_strnsave(*fnamep, *fnamelen);
10450 if (sub != NULL && str != NULL)
10451 {
10452 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010453 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 if (s != NULL)
10455 {
10456 *fnamep = s;
10457 *fnamelen = (int)STRLEN(s);
10458 vim_free(*bufp);
10459 *bufp = s;
10460 didit = TRUE;
10461 }
10462 }
10463 vim_free(sub);
10464 vim_free(str);
10465 }
10466 vim_free(pat);
10467 }
10468 }
10469 /* after using ":s", repeat all the modifiers */
10470 if (didit)
10471 goto repeat;
10472 }
10473 }
10474
Bram Moolenaar26df0922014-02-23 23:39:13 +010010475 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10476 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010477 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010478 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010479 if (c != NUL)
10480 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010481 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010482 if (c != NUL)
10483 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010484 if (p == NULL)
10485 return -1;
10486 vim_free(*bufp);
10487 *bufp = *fnamep = p;
10488 *fnamelen = (int)STRLEN(p);
10489 *usedlen += 2;
10490 }
10491
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 return valid;
10493}
10494
10495/*
10496 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010497 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 * "flags" can be "g" to do a global substitute.
10499 * Returns an allocated string, NULL for error.
10500 */
10501 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010502do_string_sub(
10503 char_u *str,
10504 char_u *pat,
10505 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010506 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010507 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 int sublen;
10510 regmatch_T regmatch;
10511 int i;
10512 int do_all;
10513 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010514 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 garray_T ga;
10516 char_u *ret;
10517 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010518 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
10520 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10521 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010522 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523
10524 ga_init2(&ga, 1, 200);
10525
10526 do_all = (flags[0] == 'g');
10527
10528 regmatch.rm_ic = p_ic;
10529 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10530 if (regmatch.regprog != NULL)
10531 {
10532 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010533 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10535 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010536 /* Skip empty match except for first match. */
10537 if (regmatch.startp[0] == regmatch.endp[0])
10538 {
10539 if (zero_width == regmatch.startp[0])
10540 {
10541 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010542 i = MB_PTR2LEN(tail);
10543 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10544 (size_t)i);
10545 ga.ga_len += i;
10546 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010547 continue;
10548 }
10549 zero_width = regmatch.startp[0];
10550 }
10551
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 /*
10553 * Get some space for a temporary buffer to do the substitution
10554 * into. It will contain:
10555 * - The text up to where the match is.
10556 * - The substituted text.
10557 * - The text after the match.
10558 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010559 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010560 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10562 {
10563 ga_clear(&ga);
10564 break;
10565 }
10566
10567 /* copy the text up to where the match is */
10568 i = (int)(regmatch.startp[0] - tail);
10569 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10570 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010571 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 + ga.ga_len + i, TRUE, TRUE, FALSE);
10573 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010574 tail = regmatch.endp[0];
10575 if (*tail == NUL)
10576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 if (!do_all)
10578 break;
10579 }
10580
10581 if (ga.ga_data != NULL)
10582 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10583
Bram Moolenaar473de612013-06-08 18:19:48 +020010584 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
10586
10587 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10588 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010589 if (p_cpo == empty_option)
10590 p_cpo = save_cpo;
10591 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010592 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010593 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594
10595 return ret;
10596}
10597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010598 static int
10599filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10600{
10601 typval_T rettv;
10602 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010603 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604
10605 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10606 argv[0] = vimvars[VV_KEY].vv_tv;
10607 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010608 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10609 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010610 if (map)
10611 {
10612 /* map(): replace the list item value */
10613 clear_tv(tv);
10614 rettv.v_lock = 0;
10615 *tv = rettv;
10616 }
10617 else
10618 {
10619 int error = FALSE;
10620
10621 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010622 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010623 clear_tv(&rettv);
10624 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010625 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 if (error)
10627 goto theend;
10628 }
10629 retval = OK;
10630theend:
10631 clear_tv(&vimvars[VV_VAL].vv_tv);
10632 return retval;
10633}
10634
10635
10636/*
10637 * Implementation of map() and filter().
10638 */
10639 void
10640filter_map(typval_T *argvars, typval_T *rettv, int map)
10641{
10642 typval_T *expr;
10643 listitem_T *li, *nli;
10644 list_T *l = NULL;
10645 dictitem_T *di;
10646 hashtab_T *ht;
10647 hashitem_T *hi;
10648 dict_T *d = NULL;
10649 typval_T save_val;
10650 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010651 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 int rem;
10653 int todo;
10654 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10655 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10656 : N_("filter() argument"));
10657 int save_did_emsg;
10658 int idx = 0;
10659
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010660 if (argvars[0].v_type == VAR_BLOB)
10661 {
10662 if ((b = argvars[0].vval.v_blob) == NULL)
10663 return;
10664 }
10665 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 {
10667 if ((l = argvars[0].vval.v_list) == NULL
10668 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10669 return;
10670 }
10671 else if (argvars[0].v_type == VAR_DICT)
10672 {
10673 if ((d = argvars[0].vval.v_dict) == NULL
10674 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10675 return;
10676 }
10677 else
10678 {
10679 EMSG2(_(e_listdictarg), ermsg);
10680 return;
10681 }
10682
10683 expr = &argvars[1];
10684 /* On type errors, the preceding call has already displayed an error
10685 * message. Avoid a misleading error message for an empty string that
10686 * was not passed as argument. */
10687 if (expr->v_type != VAR_UNKNOWN)
10688 {
10689 prepare_vimvar(VV_VAL, &save_val);
10690
10691 /* We reset "did_emsg" to be able to detect whether an error
10692 * occurred during evaluation of the expression. */
10693 save_did_emsg = did_emsg;
10694 did_emsg = FALSE;
10695
10696 prepare_vimvar(VV_KEY, &save_key);
10697 if (argvars[0].v_type == VAR_DICT)
10698 {
10699 vimvars[VV_KEY].vv_type = VAR_STRING;
10700
10701 ht = &d->dv_hashtab;
10702 hash_lock(ht);
10703 todo = (int)ht->ht_used;
10704 for (hi = ht->ht_array; todo > 0; ++hi)
10705 {
10706 if (!HASHITEM_EMPTY(hi))
10707 {
10708 int r;
10709
10710 --todo;
10711 di = HI2DI(hi);
10712 if (map &&
10713 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10714 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10715 break;
10716 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10717 r = filter_map_one(&di->di_tv, expr, map, &rem);
10718 clear_tv(&vimvars[VV_KEY].vv_tv);
10719 if (r == FAIL || did_emsg)
10720 break;
10721 if (!map && rem)
10722 {
10723 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10724 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10725 break;
10726 dictitem_remove(d, di);
10727 }
10728 }
10729 }
10730 hash_unlock(ht);
10731 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010732 else if (argvars[0].v_type == VAR_BLOB)
10733 {
10734 int i;
10735 typval_T tv;
10736
10737 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10738 for (i = 0; i < b->bv_ga.ga_len; i++)
10739 {
10740 tv.v_type = VAR_NUMBER;
10741 tv.vval.v_number = blob_get(b, i);
10742 vimvars[VV_KEY].vv_nr = idx;
10743 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10744 break;
10745 if (tv.v_type != VAR_NUMBER)
10746 {
10747 EMSG(_(e_invalblob));
10748 return;
10749 }
10750 tv.v_type = VAR_NUMBER;
10751 blob_set(b, i, tv.vval.v_number);
10752 if (!map && rem)
10753 {
10754 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10755
10756 mch_memmove(p + idx, p + i + 1,
10757 (size_t)b->bv_ga.ga_len - i - 1);
10758 --b->bv_ga.ga_len;
10759 --i;
10760 }
10761 }
10762 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 else
10764 {
10765 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10766
10767 for (li = l->lv_first; li != NULL; li = nli)
10768 {
10769 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10770 break;
10771 nli = li->li_next;
10772 vimvars[VV_KEY].vv_nr = idx;
10773 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10774 || did_emsg)
10775 break;
10776 if (!map && rem)
10777 listitem_remove(l, li);
10778 ++idx;
10779 }
10780 }
10781
10782 restore_vimvar(VV_KEY, &save_key);
10783 restore_vimvar(VV_VAL, &save_val);
10784
10785 did_emsg |= save_did_emsg;
10786 }
10787
10788 copy_tv(&argvars[0], rettv);
10789}
10790
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */