blob: 993a5bc73b065b50681455835abd89950212cc83 [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 {
2618 fi->fi_blob = b;
2619 fi->fi_bi = 0;
2620 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002621 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002622 else
2623 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002624 EMSG(_(e_listreq));
2625 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002626 }
2627 }
2628 }
2629 if (skip)
2630 --emsg_skip;
2631
2632 return fi;
2633}
2634
2635/*
2636 * Use the first item in a ":for" list. Advance to the next.
2637 * Assign the values to the variable (list). "arg" points to the first one.
2638 * Return TRUE when a valid item was found, FALSE when at end of list or
2639 * something wrong.
2640 */
2641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002642next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002643{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002644 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002647
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002648 if (fi->fi_blob != NULL)
2649 {
2650 typval_T tv;
2651
2652 if (fi->fi_bi >= blob_len(fi->fi_blob))
2653 return FALSE;
2654 tv.v_type = VAR_NUMBER;
2655 tv.v_lock = VAR_FIXED;
2656 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2657 ++fi->fi_bi;
2658 return ex_let_vars(arg, &tv, TRUE,
2659 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2660 }
2661
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002662 item = fi->fi_lw.lw_item;
2663 if (item == NULL)
2664 result = FALSE;
2665 else
2666 {
2667 fi->fi_lw.lw_item = item->li_next;
2668 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2669 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2670 }
2671 return result;
2672}
2673
2674/*
2675 * Free the structure used to store info used by ":for".
2676 */
2677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002679{
Bram Moolenaar33570922005-01-25 22:26:29 +00002680 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002682 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002683 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002684 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002685 list_unref(fi->fi_list);
2686 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002687 vim_free(fi);
2688}
2689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2691
2692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002693set_context_for_expression(
2694 expand_T *xp,
2695 char_u *arg,
2696 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 int got_eq = FALSE;
2699 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002700 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002702 if (cmdidx == CMD_let)
2703 {
2704 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002705 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706 {
2707 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002708 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002709 {
2710 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002711 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002712 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002713 break;
2714 }
2715 return;
2716 }
2717 }
2718 else
2719 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2720 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 while ((xp->xp_pattern = vim_strpbrk(arg,
2722 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2723 {
2724 c = *xp->xp_pattern;
2725 if (c == '&')
2726 {
2727 c = xp->xp_pattern[1];
2728 if (c == '&')
2729 {
2730 ++xp->xp_pattern;
2731 xp->xp_context = cmdidx != CMD_let || got_eq
2732 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2733 }
2734 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002737 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2738 xp->xp_pattern += 2;
2739
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 else if (c == '$')
2743 {
2744 /* environment variable */
2745 xp->xp_context = EXPAND_ENV_VARS;
2746 }
2747 else if (c == '=')
2748 {
2749 got_eq = TRUE;
2750 xp->xp_context = EXPAND_EXPRESSION;
2751 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002752 else if (c == '#'
2753 && xp->xp_context == EXPAND_EXPRESSION)
2754 {
2755 /* Autoload function/variable contains '#'. */
2756 break;
2757 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002758 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 && xp->xp_context == EXPAND_FUNCTIONS
2760 && vim_strchr(xp->xp_pattern, '(') == NULL)
2761 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002762 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 break;
2764 }
2765 else if (cmdidx != CMD_let || got_eq)
2766 {
2767 if (c == '"') /* string */
2768 {
2769 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2770 if (c == '\\' && xp->xp_pattern[1] != NUL)
2771 ++xp->xp_pattern;
2772 xp->xp_context = EXPAND_NOTHING;
2773 }
2774 else if (c == '\'') /* literal string */
2775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2778 /* skip */ ;
2779 xp->xp_context = EXPAND_NOTHING;
2780 }
2781 else if (c == '|')
2782 {
2783 if (xp->xp_pattern[1] == '|')
2784 {
2785 ++xp->xp_pattern;
2786 xp->xp_context = EXPAND_EXPRESSION;
2787 }
2788 else
2789 xp->xp_context = EXPAND_COMMANDS;
2790 }
2791 else
2792 xp->xp_context = EXPAND_EXPRESSION;
2793 }
2794 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002795 /* Doesn't look like something valid, expand as an expression
2796 * anyway. */
2797 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 arg = xp->xp_pattern;
2799 if (*arg != NUL)
2800 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2801 /* skip */ ;
2802 }
2803 xp->xp_pattern = arg;
2804}
2805
2806#endif /* FEAT_CMDL_COMPL */
2807
2808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 * ":unlet[!] var1 ... " command.
2810 */
2811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002812ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002814 ex_unletlock(eap, eap->arg, 0);
2815}
2816
2817/*
2818 * ":lockvar" and ":unlockvar" commands
2819 */
2820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002821ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002822{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002824 int deep = 2;
2825
2826 if (eap->forceit)
2827 deep = -1;
2828 else if (vim_isdigit(*arg))
2829 {
2830 deep = getdigits(&arg);
2831 arg = skipwhite(arg);
2832 }
2833
2834 ex_unletlock(eap, arg, deep);
2835}
2836
2837/*
2838 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2839 */
2840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841ex_unletlock(
2842 exarg_T *eap,
2843 char_u *argstart,
2844 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002845{
2846 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 do
2852 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002853 if (*arg == '$')
2854 {
2855 char_u *name = ++arg;
2856
2857 if (get_env_len(&arg) == 0)
2858 {
2859 EMSG2(_(e_invarg2), name - 1);
2860 return;
2861 }
2862 vim_unsetenv(name);
2863 arg = skipwhite(arg);
2864 continue;
2865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002868 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002869 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lv.ll_name == NULL)
2871 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002872 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (name_end != NULL)
2876 {
2877 emsg_severe = TRUE;
2878 EMSG(_(e_trailing));
2879 }
2880 if (!(eap->skip || error))
2881 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 break;
2883 }
2884
2885 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 {
2887 if (eap->cmdidx == CMD_unlet)
2888 {
2889 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2890 error = TRUE;
2891 }
2892 else
2893 {
2894 if (do_lock_var(&lv, name_end, deep,
2895 eap->cmdidx == CMD_lockvar) == FAIL)
2896 error = TRUE;
2897 }
2898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 if (!eap->skip)
2901 clear_lval(&lv);
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 arg = skipwhite(name_end);
2904 } while (!ends_excmd(*arg));
2905
2906 eap->nextcmd = check_nextcmd(arg);
2907}
2908
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910do_unlet_var(
2911 lval_T *lp,
2912 char_u *name_end,
2913 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 int ret = OK;
2916 int cc;
2917
2918 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 cc = *name_end;
2921 *name_end = NUL;
2922
2923 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002924 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002928 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002929 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002930 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002931 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 else if (lp->ll_range)
2934 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002936 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002937 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002938
2939 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2940 {
2941 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002942 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002943 return FAIL;
2944 ll_li = li;
2945 ++ll_n1;
2946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947
2948 /* Delete a range of List items. */
2949 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2950 {
2951 li = lp->ll_li->li_next;
2952 listitem_remove(lp->ll_list, lp->ll_li);
2953 lp->ll_li = li;
2954 ++lp->ll_n1;
2955 }
2956 }
2957 else
2958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 /* unlet a List item. */
2961 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002964 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 }
2966
2967 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968}
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
2971 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 */
2974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002975do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 hashtab_T *ht;
2978 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002979 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002980 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002981 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaar33570922005-01-25 22:26:29 +00002983 ht = find_var_ht(name, &varname);
2984 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002986 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002987 if (d == NULL)
2988 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989 if (ht == &globvarht)
2990 d = &globvardict;
2991 else if (ht == &compat_hashtab)
2992 d = &vimvardict;
2993 else
2994 {
2995 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2996 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2997 }
2998 if (d == NULL)
2999 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003000 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 return FAIL;
3002 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003003 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003004 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003005 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003006 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003007 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003008 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003009 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003010 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003011 || var_check_ro(di->di_flags, name, FALSE)
3012 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003013 return FAIL;
3014
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003015 delete_var(ht, hi);
3016 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 if (forceit)
3020 return OK;
3021 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 return FAIL;
3023}
3024
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003025/*
3026 * Lock or unlock variable indicated by "lp".
3027 * "deep" is the levels to go (-1 for unlimited);
3028 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3029 */
3030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003031do_lock_var(
3032 lval_T *lp,
3033 char_u *name_end,
3034 int deep,
3035 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003036{
3037 int ret = OK;
3038 int cc;
3039 dictitem_T *di;
3040
3041 if (deep == 0) /* nothing to do */
3042 return OK;
3043
3044 if (lp->ll_tv == NULL)
3045 {
3046 cc = *name_end;
3047 *name_end = NUL;
3048
3049 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003050 di = find_var(lp->ll_name, NULL, TRUE);
3051 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003052 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003053 else if ((di->di_flags & DI_FLAGS_FIX)
3054 && di->di_tv.v_type != VAR_DICT
3055 && di->di_tv.v_type != VAR_LIST)
3056 /* For historic reasons this error is not given for a list or dict.
3057 * E.g., the b: dict could be locked/unlocked. */
3058 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003059 else
3060 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003061 if (lock)
3062 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003063 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003064 di->di_flags &= ~DI_FLAGS_LOCK;
3065 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003066 }
3067 *name_end = cc;
3068 }
3069 else if (lp->ll_range)
3070 {
3071 listitem_T *li = lp->ll_li;
3072
3073 /* (un)lock a range of List items. */
3074 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3075 {
3076 item_lock(&li->li_tv, deep, lock);
3077 li = li->li_next;
3078 ++lp->ll_n1;
3079 }
3080 }
3081 else if (lp->ll_list != NULL)
3082 /* (un)lock a List item. */
3083 item_lock(&lp->ll_li->li_tv, deep, lock);
3084 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003085 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003086 item_lock(&lp->ll_di->di_tv, deep, lock);
3087
3088 return ret;
3089}
3090
3091/*
3092 * Lock or unlock an item. "deep" is nr of levels to go.
3093 */
3094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003096{
3097 static int recurse = 0;
3098 list_T *l;
3099 listitem_T *li;
3100 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003101 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003102 hashitem_T *hi;
3103 int todo;
3104
3105 if (recurse >= DICT_MAXNEST)
3106 {
3107 EMSG(_("E743: variable nested too deep for (un)lock"));
3108 return;
3109 }
3110 if (deep == 0)
3111 return;
3112 ++recurse;
3113
3114 /* lock/unlock the item itself */
3115 if (lock)
3116 tv->v_lock |= VAR_LOCKED;
3117 else
3118 tv->v_lock &= ~VAR_LOCKED;
3119
3120 switch (tv->v_type)
3121 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003122 case VAR_UNKNOWN:
3123 case VAR_NUMBER:
3124 case VAR_STRING:
3125 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003126 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003127 case VAR_FLOAT:
3128 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003129 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003130 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003131 break;
3132
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003133 case VAR_BLOB:
3134 if ((b = tv->vval.v_blob) != NULL)
3135 {
3136 if (lock)
3137 b->bv_lock |= VAR_LOCKED;
3138 else
3139 b->bv_lock &= ~VAR_LOCKED;
3140 }
3141 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003142 case VAR_LIST:
3143 if ((l = tv->vval.v_list) != NULL)
3144 {
3145 if (lock)
3146 l->lv_lock |= VAR_LOCKED;
3147 else
3148 l->lv_lock &= ~VAR_LOCKED;
3149 if (deep < 0 || deep > 1)
3150 /* recursive: lock/unlock the items the List contains */
3151 for (li = l->lv_first; li != NULL; li = li->li_next)
3152 item_lock(&li->li_tv, deep - 1, lock);
3153 }
3154 break;
3155 case VAR_DICT:
3156 if ((d = tv->vval.v_dict) != NULL)
3157 {
3158 if (lock)
3159 d->dv_lock |= VAR_LOCKED;
3160 else
3161 d->dv_lock &= ~VAR_LOCKED;
3162 if (deep < 0 || deep > 1)
3163 {
3164 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003165 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003166 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3167 {
3168 if (!HASHITEM_EMPTY(hi))
3169 {
3170 --todo;
3171 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3172 }
3173 }
3174 }
3175 }
3176 }
3177 --recurse;
3178}
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3181/*
3182 * Delete all "menutrans_" variables.
3183 */
3184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003185del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003188 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003191 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003193 {
3194 if (!HASHITEM_EMPTY(hi))
3195 {
3196 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3198 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003199 }
3200 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202}
3203#endif
3204
3205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3206
3207/*
3208 * Local string buffer for the next two functions to store a variable name
3209 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3210 * get_user_var_name().
3211 */
3212
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213static char_u *varnamebuf = NULL;
3214static int varnamebuflen = 0;
3215
3216/*
3217 * Function to concatenate a prefix and a variable name.
3218 */
3219 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 int len;
3223
3224 len = (int)STRLEN(name) + 3;
3225 if (len > varnamebuflen)
3226 {
3227 vim_free(varnamebuf);
3228 len += 10; /* some additional space */
3229 varnamebuf = alloc(len);
3230 if (varnamebuf == NULL)
3231 {
3232 varnamebuflen = 0;
3233 return NULL;
3234 }
3235 varnamebuflen = len;
3236 }
3237 *varnamebuf = prefix;
3238 varnamebuf[1] = ':';
3239 STRCPY(varnamebuf + 2, name);
3240 return varnamebuf;
3241}
3242
3243/*
3244 * Function given to ExpandGeneric() to obtain the list of user defined
3245 * (global/buffer/window/built-in) variable names.
3246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003250 static long_u gdone;
3251 static long_u bdone;
3252 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003253 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003254 static int vidx;
3255 static hashitem_T *hi;
3256 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003259 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003260 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003261 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003262 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003263
3264 /* Global variables */
3265 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003267 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003268 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003269 else
3270 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 while (HASHITEM_EMPTY(hi))
3272 ++hi;
3273 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3274 return cat_prefix_varname('g', hi->hi_key);
3275 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003277
3278 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003279 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003282 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003284 else
3285 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 while (HASHITEM_EMPTY(hi))
3287 ++hi;
3288 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003290
3291 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003292 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003295 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003297 else
3298 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003299 while (HASHITEM_EMPTY(hi))
3300 ++hi;
3301 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003303
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003304 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003305 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003306 if (tdone < ht->ht_used)
3307 {
3308 if (tdone++ == 0)
3309 hi = ht->ht_array;
3310 else
3311 ++hi;
3312 while (HASHITEM_EMPTY(hi))
3313 ++hi;
3314 return cat_prefix_varname('t', hi->hi_key);
3315 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003316
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 /* v: variables */
3318 if (vidx < VV_LEN)
3319 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaard23a8232018-02-10 18:45:26 +01003321 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 varnamebuflen = 0;
3323 return NULL;
3324}
3325
3326#endif /* FEAT_CMDL_COMPL */
3327
3328/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003329 * Return TRUE if "pat" matches "text".
3330 * Does not use 'cpo' and always uses 'magic'.
3331 */
3332 static int
3333pattern_match(char_u *pat, char_u *text, int ic)
3334{
3335 int matches = FALSE;
3336 char_u *save_cpo;
3337 regmatch_T regmatch;
3338
3339 /* avoid 'l' flag in 'cpoptions' */
3340 save_cpo = p_cpo;
3341 p_cpo = (char_u *)"";
3342 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3343 if (regmatch.regprog != NULL)
3344 {
3345 regmatch.rm_ic = ic;
3346 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3347 vim_regfree(regmatch.regprog);
3348 }
3349 p_cpo = save_cpo;
3350 return matches;
3351}
3352
3353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3357 */
3358
3359/*
3360 * Handle zero level expression.
3361 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003363 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * Return OK or FAIL.
3365 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367eval0(
3368 char_u *arg,
3369 typval_T *rettv,
3370 char_u **nextcmd,
3371 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 int ret;
3374 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003375 int did_emsg_before = did_emsg;
3376 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003379 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 if (ret == FAIL || !ends_excmd(*p))
3381 {
3382 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /*
3385 * Report the invalid expression unless the expression evaluation has
3386 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003387 * exception, or we already gave a more specific error.
3388 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003390 if (!aborting() && did_emsg == did_emsg_before
3391 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 EMSG2(_(e_invexpr2), arg);
3393 ret = FAIL;
3394 }
3395 if (nextcmd != NULL)
3396 *nextcmd = check_nextcmd(p);
3397
3398 return ret;
3399}
3400
3401/*
3402 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003403 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 *
3405 * "arg" must point to the first non-white of the expression.
3406 * "arg" is advanced to the next non-white after the recognized expression.
3407 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003408 * Note: "rettv.v_lock" is not set.
3409 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * Return OK or FAIL.
3411 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003413eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
3418 /*
3419 * Get the first variable.
3420 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 return FAIL;
3423
3424 if ((*arg)[0] == '?')
3425 {
3426 result = FALSE;
3427 if (evaluate)
3428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 int error = FALSE;
3430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003431 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003434 if (error)
3435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437
3438 /*
3439 * Get the second variable.
3440 */
3441 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003442 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 return FAIL;
3444
3445 /*
3446 * Check for the ":".
3447 */
3448 if ((*arg)[0] != ':')
3449 {
3450 EMSG(_("E109: Missing ':' after '?'"));
3451 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003452 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 return FAIL;
3454 }
3455
3456 /*
3457 * Get the third variable.
3458 */
3459 *arg = skipwhite(*arg + 1);
3460 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3461 {
3462 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003463 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 return FAIL;
3465 }
3466 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469
3470 return OK;
3471}
3472
3473/*
3474 * Handle first level expression:
3475 * expr2 || expr2 || expr2 logical OR
3476 *
3477 * "arg" must point to the first non-white of the expression.
3478 * "arg" is advanced to the next non-white after the recognized expression.
3479 *
3480 * Return OK or FAIL.
3481 */
3482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003483eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
Bram Moolenaar33570922005-01-25 22:26:29 +00003485 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 long result;
3487 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003488 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 /*
3491 * Get the first variable.
3492 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 return FAIL;
3495
3496 /*
3497 * Repeat until there is no following "||".
3498 */
3499 first = TRUE;
3500 result = FALSE;
3501 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3502 {
3503 if (evaluate && first)
3504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003507 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003508 if (error)
3509 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 first = FALSE;
3511 }
3512
3513 /*
3514 * Get the second variable.
3515 */
3516 *arg = skipwhite(*arg + 2);
3517 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3518 return FAIL;
3519
3520 /*
3521 * Compute the result.
3522 */
3523 if (evaluate && !result)
3524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003525 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528 if (error)
3529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531 if (evaluate)
3532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 rettv->v_type = VAR_NUMBER;
3534 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 }
3537
3538 return OK;
3539}
3540
3541/*
3542 * Handle second level expression:
3543 * expr3 && expr3 && expr3 logical AND
3544 *
3545 * "arg" must point to the first non-white of the expression.
3546 * "arg" is advanced to the next non-white after the recognized expression.
3547 *
3548 * Return OK or FAIL.
3549 */
3550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003551eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 long result;
3555 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558 /*
3559 * Get the first variable.
3560 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003561 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 return FAIL;
3563
3564 /*
3565 * Repeat until there is no following "&&".
3566 */
3567 first = TRUE;
3568 result = TRUE;
3569 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3570 {
3571 if (evaluate && first)
3572 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003573 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003575 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003576 if (error)
3577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 first = FALSE;
3579 }
3580
3581 /*
3582 * Get the second variable.
3583 */
3584 *arg = skipwhite(*arg + 2);
3585 if (eval4(arg, &var2, evaluate && result) == FAIL)
3586 return FAIL;
3587
3588 /*
3589 * Compute the result.
3590 */
3591 if (evaluate && result)
3592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003593 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003595 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003596 if (error)
3597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 if (evaluate)
3600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003601 rettv->v_type = VAR_NUMBER;
3602 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
3604 }
3605
3606 return OK;
3607}
3608
3609/*
3610 * Handle third level expression:
3611 * var1 == var2
3612 * var1 =~ var2
3613 * var1 != var2
3614 * var1 !~ var2
3615 * var1 > var2
3616 * var1 >= var2
3617 * var1 < var2
3618 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003619 * var1 is var2
3620 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 *
3622 * "arg" must point to the first non-white of the expression.
3623 * "arg" is advanced to the next non-white after the recognized expression.
3624 *
3625 * Return OK or FAIL.
3626 */
3627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003628eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
Bram Moolenaar33570922005-01-25 22:26:29 +00003630 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 char_u *p;
3632 int i;
3633 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003634 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 /*
3639 * Get the first variable.
3640 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 return FAIL;
3643
3644 p = *arg;
3645 switch (p[0])
3646 {
3647 case '=': if (p[1] == '=')
3648 type = TYPE_EQUAL;
3649 else if (p[1] == '~')
3650 type = TYPE_MATCH;
3651 break;
3652 case '!': if (p[1] == '=')
3653 type = TYPE_NEQUAL;
3654 else if (p[1] == '~')
3655 type = TYPE_NOMATCH;
3656 break;
3657 case '>': if (p[1] != '=')
3658 {
3659 type = TYPE_GREATER;
3660 len = 1;
3661 }
3662 else
3663 type = TYPE_GEQUAL;
3664 break;
3665 case '<': if (p[1] != '=')
3666 {
3667 type = TYPE_SMALLER;
3668 len = 1;
3669 }
3670 else
3671 type = TYPE_SEQUAL;
3672 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003673 case 'i': if (p[1] == 's')
3674 {
3675 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3676 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003677 i = p[len];
3678 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003679 {
3680 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3681 type_is = TRUE;
3682 }
3683 }
3684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686
3687 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003688 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 */
3690 if (type != TYPE_UNKNOWN)
3691 {
3692 /* extra question mark appended: ignore case */
3693 if (p[len] == '?')
3694 {
3695 ic = TRUE;
3696 ++len;
3697 }
3698 /* extra '#' appended: match case */
3699 else if (p[len] == '#')
3700 {
3701 ic = FALSE;
3702 ++len;
3703 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003704 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 else
3706 ic = p_ic;
3707
3708 /*
3709 * Get the second variable.
3710 */
3711 *arg = skipwhite(p + len);
3712 if (eval5(arg, &var2, evaluate) == FAIL)
3713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 return FAIL;
3716 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003717 if (evaluate)
3718 {
3719 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3720
3721 clear_tv(&var2);
3722 return ret;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725
3726 return OK;
3727}
3728
3729/*
3730 * Handle fourth level expression:
3731 * + number addition
3732 * - number subtraction
3733 * . string concatenation
3734 *
3735 * "arg" must point to the first non-white of the expression.
3736 * "arg" is advanced to the next non-white after the recognized expression.
3737 *
3738 * Return OK or FAIL.
3739 */
3740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003741eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar33570922005-01-25 22:26:29 +00003743 typval_T var2;
3744 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003746 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003747#ifdef FEAT_FLOAT
3748 float_T f1 = 0, f2 = 0;
3749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 char_u *s1, *s2;
3751 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3752 char_u *p;
3753
3754 /*
3755 * Get the first variable.
3756 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003757 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 return FAIL;
3759
3760 /*
3761 * Repeat computing, until no '+', '-' or '.' is following.
3762 */
3763 for (;;)
3764 {
3765 op = **arg;
3766 if (op != '+' && op != '-' && op != '.')
3767 break;
3768
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003769 if ((op != '+' || (rettv->v_type != VAR_LIST
3770 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003771#ifdef FEAT_FLOAT
3772 && (op == '.' || rettv->v_type != VAR_FLOAT)
3773#endif
3774 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775 {
3776 /* For "list + ...", an illegal use of the first operand as
3777 * a number cannot be determined before evaluating the 2nd
3778 * operand: if this is also a list, all is ok.
3779 * For "something . ...", "something - ..." or "non-list + ...",
3780 * we know that the first operand needs to be a string or number
3781 * without evaluating the 2nd operand. So check before to avoid
3782 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003783 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 {
3785 clear_tv(rettv);
3786 return FAIL;
3787 }
3788 }
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 /*
3791 * Get the second variable.
3792 */
3793 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003794 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
3798 }
3799
3800 if (evaluate)
3801 {
3802 /*
3803 * Compute the result.
3804 */
3805 if (op == '.')
3806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003807 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3808 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 if (s2 == NULL) /* type error ? */
3810 {
3811 clear_tv(rettv);
3812 clear_tv(&var2);
3813 return FAIL;
3814 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003815 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 clear_tv(rettv);
3817 rettv->v_type = VAR_STRING;
3818 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003820 else if (op == '+' && rettv->v_type == VAR_BLOB
3821 && var2.v_type == VAR_BLOB)
3822 {
3823 blob_T *b1 = rettv->vval.v_blob;
3824 blob_T *b2 = var2.vval.v_blob;
3825 blob_T *b = blob_alloc();
3826 int i;
3827
3828 if (b != NULL)
3829 {
3830 for (i = 0; i < blob_len(b1); i++)
3831 ga_append(&b->bv_ga, blob_get(b1, i));
3832 for (i = 0; i < blob_len(b2); i++)
3833 ga_append(&b->bv_ga, blob_get(b2, i));
3834
3835 clear_tv(rettv);
3836 rettv_blob_set(rettv, b);
3837 }
3838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003839 else if (op == '+' && rettv->v_type == VAR_LIST
3840 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003841 {
3842 /* concatenate Lists */
3843 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3844 &var3) == FAIL)
3845 {
3846 clear_tv(rettv);
3847 clear_tv(&var2);
3848 return FAIL;
3849 }
3850 clear_tv(rettv);
3851 *rettv = var3;
3852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 else
3854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 int error = FALSE;
3856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003857#ifdef FEAT_FLOAT
3858 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003860 f1 = rettv->vval.v_float;
3861 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003863 else
3864#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003866 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 if (error)
3868 {
3869 /* This can only happen for "list + non-list". For
3870 * "non-list + ..." or "something - ...", we returned
3871 * before evaluating the 2nd operand. */
3872 clear_tv(rettv);
3873 return FAIL;
3874 }
3875#ifdef FEAT_FLOAT
3876 if (var2.v_type == VAR_FLOAT)
3877 f1 = n1;
3878#endif
3879 }
3880#ifdef FEAT_FLOAT
3881 if (var2.v_type == VAR_FLOAT)
3882 {
3883 f2 = var2.vval.v_float;
3884 n2 = 0;
3885 }
3886 else
3887#endif
3888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003889 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003890 if (error)
3891 {
3892 clear_tv(rettv);
3893 clear_tv(&var2);
3894 return FAIL;
3895 }
3896#ifdef FEAT_FLOAT
3897 if (rettv->v_type == VAR_FLOAT)
3898 f2 = n2;
3899#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003902
3903#ifdef FEAT_FLOAT
3904 /* If there is a float on either side the result is a float. */
3905 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3906 {
3907 if (op == '+')
3908 f1 = f1 + f2;
3909 else
3910 f1 = f1 - f2;
3911 rettv->v_type = VAR_FLOAT;
3912 rettv->vval.v_float = f1;
3913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003915#endif
3916 {
3917 if (op == '+')
3918 n1 = n1 + n2;
3919 else
3920 n1 = n1 - n2;
3921 rettv->v_type = VAR_NUMBER;
3922 rettv->vval.v_number = n1;
3923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 }
3928 return OK;
3929}
3930
3931/*
3932 * Handle fifth level expression:
3933 * * number multiplication
3934 * / number division
3935 * % number modulo
3936 *
3937 * "arg" must point to the first non-white of the expression.
3938 * "arg" is advanced to the next non-white after the recognized expression.
3939 *
3940 * Return OK or FAIL.
3941 */
3942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003943eval6(
3944 char_u **arg,
3945 typval_T *rettv,
3946 int evaluate,
3947 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003951 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003952#ifdef FEAT_FLOAT
3953 int use_float = FALSE;
3954 float_T f1 = 0, f2;
3955#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003956 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
3958 /*
3959 * Get the first variable.
3960 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003961 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 return FAIL;
3963
3964 /*
3965 * Repeat computing, until no '*', '/' or '%' is following.
3966 */
3967 for (;;)
3968 {
3969 op = **arg;
3970 if (op != '*' && op != '/' && op != '%')
3971 break;
3972
3973 if (evaluate)
3974 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003975#ifdef FEAT_FLOAT
3976 if (rettv->v_type == VAR_FLOAT)
3977 {
3978 f1 = rettv->vval.v_float;
3979 use_float = TRUE;
3980 n1 = 0;
3981 }
3982 else
3983#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003984 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003985 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986 if (error)
3987 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 else
3990 n1 = 0;
3991
3992 /*
3993 * Get the second variable.
3994 */
3995 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003996 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return FAIL;
3998
3999 if (evaluate)
4000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004001#ifdef FEAT_FLOAT
4002 if (var2.v_type == VAR_FLOAT)
4003 {
4004 if (!use_float)
4005 {
4006 f1 = n1;
4007 use_float = TRUE;
4008 }
4009 f2 = var2.vval.v_float;
4010 n2 = 0;
4011 }
4012 else
4013#endif
4014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004015 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004016 clear_tv(&var2);
4017 if (error)
4018 return FAIL;
4019#ifdef FEAT_FLOAT
4020 if (use_float)
4021 f2 = n2;
4022#endif
4023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 /*
4026 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004027 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004029#ifdef FEAT_FLOAT
4030 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004032 if (op == '*')
4033 f1 = f1 * f2;
4034 else if (op == '/')
4035 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004036# ifdef VMS
4037 /* VMS crashes on divide by zero, work around it */
4038 if (f2 == 0.0)
4039 {
4040 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004041 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004042 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004043 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004044 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004045 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 }
4047 else
4048 f1 = f1 / f2;
4049# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004050 /* We rely on the floating point library to handle divide
4051 * by zero to result in "inf" and not a crash. */
4052 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004053# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004057 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 return FAIL;
4059 }
4060 rettv->v_type = VAR_FLOAT;
4061 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066 if (op == '*')
4067 n1 = n1 * n2;
4068 else if (op == '/')
4069 {
4070 if (n2 == 0) /* give an error message? */
4071 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004072 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004073 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004074 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004075 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004076 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004077 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004078 }
4079 else
4080 n1 = n1 / n2;
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004083 {
4084 if (n2 == 0) /* give an error message? */
4085 n1 = 0;
4086 else
4087 n1 = n1 % n2;
4088 }
4089 rettv->v_type = VAR_NUMBER;
4090 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 }
4094
4095 return OK;
4096}
4097
4098/*
4099 * Handle sixth level expression:
4100 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004101 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004102 * "string" string constant
4103 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * &option-name option value
4105 * @r register contents
4106 * identifier variable value
4107 * function() function call
4108 * $VAR environment variable
4109 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004110 * [expr, expr] List
4111 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 *
4113 * Also handle:
4114 * ! in front logical NOT
4115 * - in front unary minus
4116 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 * trailing [] subscript in String or List
4118 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * "arg" must point to the first non-white of the expression.
4121 * "arg" is advanced to the next non-white after the recognized expression.
4122 *
4123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126eval7(
4127 char_u **arg,
4128 typval_T *rettv,
4129 int evaluate,
4130 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004132 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 int len;
4134 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 char_u *start_leader, *end_leader;
4136 int ret = OK;
4137 char_u *alias;
4138
4139 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004146 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 start_leader = *arg;
4149 while (**arg == '!' || **arg == '-' || **arg == '+')
4150 *arg = skipwhite(*arg + 1);
4151 end_leader = *arg;
4152
4153 switch (**arg)
4154 {
4155 /*
4156 * Number constant.
4157 */
4158 case '0':
4159 case '1':
4160 case '2':
4161 case '3':
4162 case '4':
4163 case '5':
4164 case '6':
4165 case '7':
4166 case '8':
4167 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004168 {
4169#ifdef FEAT_FLOAT
4170 char_u *p = skipdigits(*arg + 1);
4171 int get_float = FALSE;
4172
4173 /* We accept a float when the format matches
4174 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004175 * strict to avoid backwards compatibility problems.
4176 * Don't look for a float after the "." operator, so that
4177 * ":let vers = 1.2.3" doesn't fail. */
4178 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004180 get_float = TRUE;
4181 p = skipdigits(p + 2);
4182 if (*p == 'e' || *p == 'E')
4183 {
4184 ++p;
4185 if (*p == '-' || *p == '+')
4186 ++p;
4187 if (!vim_isdigit(*p))
4188 get_float = FALSE;
4189 else
4190 p = skipdigits(p + 1);
4191 }
4192 if (ASCII_ISALPHA(*p) || *p == '.')
4193 get_float = FALSE;
4194 }
4195 if (get_float)
4196 {
4197 float_T f;
4198
4199 *arg += string2float(*arg, &f);
4200 if (evaluate)
4201 {
4202 rettv->v_type = VAR_FLOAT;
4203 rettv->vval.v_float = f;
4204 }
4205 }
4206 else
4207#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004208 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004209 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004210 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004211 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004212
4213 // Blob constant: 0z0123456789abcdef
4214 if (evaluate)
4215 blob = blob_alloc();
4216 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4217 {
4218 if (!vim_isxdigit(bp[1]))
4219 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004220 EMSG(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004221 vim_free(blob);
4222 ret = FAIL;
4223 break;
4224 }
4225 if (blob != NULL)
4226 ga_append(&blob->bv_ga,
4227 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4228 }
4229 if (blob != NULL)
4230 {
4231 ++blob->bv_refcount;
4232 rettv->v_type = VAR_BLOB;
4233 rettv->vval.v_blob = blob;
4234 }
4235 *arg = bp;
4236 }
4237 else
4238 {
4239 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004240 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004241 *arg += len;
4242 if (evaluate)
4243 {
4244 rettv->v_type = VAR_NUMBER;
4245 rettv->vval.v_number = n;
4246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * String constant: "string".
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 break;
4256
4257 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004258 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 break;
4262
4263 /*
4264 * List: [expr, expr]
4265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268
4269 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004270 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004271 * Dictionary: {key: val, key: val}
4272 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004273 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4274 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004275 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004276 break;
4277
4278 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004279 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004281 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 break;
4283
4284 /*
4285 * Environment variable: $VAR.
4286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 break;
4289
4290 /*
4291 * Register contents: @r.
4292 */
4293 case '@': ++*arg;
4294 if (evaluate)
4295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004297 rettv->vval.v_string = get_reg_contents(**arg,
4298 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 if (**arg != NUL)
4301 ++*arg;
4302 break;
4303
4304 /*
4305 * nested expression: (expression).
4306 */
4307 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if (**arg == ')')
4310 ++*arg;
4311 else if (ret == OK)
4312 {
4313 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 ret = FAIL;
4316 }
4317 break;
4318
Bram Moolenaar8c711452005-01-14 21:53:12 +00004319 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 break;
4321 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004322
4323 if (ret == NOTDONE)
4324 {
4325 /*
4326 * Must be a variable or function name.
4327 * Can also be a curly-braces kind of name: {expr}.
4328 */
4329 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004330 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004331 if (alias != NULL)
4332 s = alias;
4333
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004334 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335 ret = FAIL;
4336 else
4337 {
4338 if (**arg == '(') /* recursive! */
4339 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004340 partial_T *partial;
4341
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004342 if (!evaluate)
4343 check_vars(s, len);
4344
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345 /* If "s" is the name of a variable of type VAR_FUNC
4346 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004347 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004349 /* Need to make a copy, in case evaluating the arguments makes
4350 * the name invalid. */
4351 s = vim_strsave(s);
4352 if (s == NULL)
4353 ret = FAIL;
4354 else
4355 /* Invoke the function. */
4356 ret = get_func_tv(s, len, rettv, arg,
4357 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4358 &len, evaluate, partial, NULL);
4359 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004360
4361 /* If evaluate is FALSE rettv->v_type was not set in
4362 * get_func_tv, but it's needed in handle_subscript() to parse
4363 * what follows. So set it here. */
4364 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4365 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004366 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004367 rettv->v_type = VAR_FUNC;
4368 }
4369
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 /* Stop the expression evaluation when immediately
4371 * aborting on error, or when an interrupt occurred or
4372 * an exception was thrown but not caught. */
4373 if (aborting())
4374 {
4375 if (ret == OK)
4376 clear_tv(rettv);
4377 ret = FAIL;
4378 }
4379 }
4380 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004381 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004382 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004383 {
4384 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004385 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004386 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004388 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 }
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *arg = skipwhite(*arg);
4392
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004393 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4394 * expr(expr). */
4395 if (ret == OK)
4396 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 /*
4399 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4400 */
4401 if (ret == OK && evaluate && end_leader > start_leader)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004404 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004405#ifdef FEAT_FLOAT
4406 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408 if (rettv->v_type == VAR_FLOAT)
4409 f = rettv->vval.v_float;
4410 else
4411#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004412 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004415 clear_tv(rettv);
4416 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004418 else
4419 {
4420 while (end_leader > start_leader)
4421 {
4422 --end_leader;
4423 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 {
4425#ifdef FEAT_FLOAT
4426 if (rettv->v_type == VAR_FLOAT)
4427 f = !f;
4428 else
4429#endif
4430 val = !val;
4431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004432 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004433 {
4434#ifdef FEAT_FLOAT
4435 if (rettv->v_type == VAR_FLOAT)
4436 f = -f;
4437 else
4438#endif
4439 val = -val;
4440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004442#ifdef FEAT_FLOAT
4443 if (rettv->v_type == VAR_FLOAT)
4444 {
4445 clear_tv(rettv);
4446 rettv->vval.v_float = f;
4447 }
4448 else
4449#endif
4450 {
4451 clear_tv(rettv);
4452 rettv->v_type = VAR_NUMBER;
4453 rettv->vval.v_number = val;
4454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457
4458 return ret;
4459}
4460
4461/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004462 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4463 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4465 */
4466 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004467eval_index(
4468 char_u **arg,
4469 typval_T *rettv,
4470 int evaluate,
4471 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004472{
4473 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004474 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004475 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004477 long len = -1;
4478 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004479 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004480 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481
Bram Moolenaara03f2332016-02-06 18:09:59 +01004482 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004484 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004485 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004486 if (verbose)
4487 EMSG(_("E695: Cannot index a Funcref"));
4488 return FAIL;
4489 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004490#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004491 if (verbose)
4492 EMSG(_(e_float_as_string));
4493 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004494#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004495 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004496 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004497 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004498 if (verbose)
4499 EMSG(_("E909: Cannot index a special variable"));
4500 return FAIL;
4501 case VAR_UNKNOWN:
4502 if (evaluate)
4503 return FAIL;
4504 /* FALLTHROUGH */
4505
4506 case VAR_STRING:
4507 case VAR_NUMBER:
4508 case VAR_LIST:
4509 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004510 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004511 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004512 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004514 init_tv(&var1);
4515 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 /*
4519 * dict.name
4520 */
4521 key = *arg + 1;
4522 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4523 ;
4524 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004525 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004526 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004527 }
4528 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 /*
4531 * something[idx]
4532 *
4533 * Get the (first) variable from inside the [].
4534 */
4535 *arg = skipwhite(*arg + 1);
4536 if (**arg == ':')
4537 empty1 = TRUE;
4538 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4539 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004540 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004541 {
4542 /* not a number or string */
4543 clear_tv(&var1);
4544 return FAIL;
4545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546
4547 /*
4548 * Get the second variable from inside the [:].
4549 */
4550 if (**arg == ':')
4551 {
4552 range = TRUE;
4553 *arg = skipwhite(*arg + 1);
4554 if (**arg == ']')
4555 empty2 = TRUE;
4556 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 if (!empty1)
4559 clear_tv(&var1);
4560 return FAIL;
4561 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004562 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 {
4564 /* not a number or string */
4565 if (!empty1)
4566 clear_tv(&var1);
4567 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 return FAIL;
4569 }
4570 }
4571
4572 /* Check for the ']'. */
4573 if (**arg != ']')
4574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004575 if (verbose)
4576 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 clear_tv(&var1);
4578 if (range)
4579 clear_tv(&var2);
4580 return FAIL;
4581 }
4582 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
4584
4585 if (evaluate)
4586 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 n1 = 0;
4588 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004590 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 }
4593 if (range)
4594 {
4595 if (empty2)
4596 n2 = -1;
4597 else
4598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004599 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 }
4602 }
4603
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004606 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004607 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004608 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004609 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004610 case VAR_SPECIAL:
4611 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004612 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004613 break; /* not evaluating, skipping over subscript */
4614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 case VAR_NUMBER:
4616 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004617 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 len = (long)STRLEN(s);
4619 if (range)
4620 {
4621 /* The resulting variable is a substring. If the indexes
4622 * are out of range the result is empty. */
4623 if (n1 < 0)
4624 {
4625 n1 = len + n1;
4626 if (n1 < 0)
4627 n1 = 0;
4628 }
4629 if (n2 < 0)
4630 n2 = len + n2;
4631 else if (n2 >= len)
4632 n2 = len;
4633 if (n1 >= len || n2 < 0 || n1 > n2)
4634 s = NULL;
4635 else
4636 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4637 }
4638 else
4639 {
4640 /* The resulting variable is a string of a single
4641 * character. If the index is too big or negative the
4642 * result is empty. */
4643 if (n1 >= len || n1 < 0)
4644 s = NULL;
4645 else
4646 s = vim_strnsave(s + n1, 1);
4647 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004648 clear_tv(rettv);
4649 rettv->v_type = VAR_STRING;
4650 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651 break;
4652
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004653 case VAR_BLOB:
4654 len = blob_len(rettv->vval.v_blob);
4655 if (range)
4656 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004657 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004658 // are out of range the result is empty.
4659 if (n1 < 0)
4660 {
4661 n1 = len + n1;
4662 if (n1 < 0)
4663 n1 = 0;
4664 }
4665 if (n2 < 0)
4666 n2 = len + n2;
4667 else if (n2 >= len)
4668 n2 = len - 1;
4669 if (n1 >= len || n2 < 0 || n1 > n2)
4670 {
4671 clear_tv(rettv);
4672 rettv->v_type = VAR_BLOB;
4673 rettv->vval.v_blob = NULL;
4674 }
4675 else
4676 {
4677 blob_T *blob = blob_alloc();
4678
4679 if (blob != NULL)
4680 {
4681 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4682 {
4683 blob_free(blob);
4684 return FAIL;
4685 }
4686 blob->bv_ga.ga_len = n2 - n1 + 1;
4687 for (i = n1; i <= n2; i++)
4688 blob_set(blob, i - n1,
4689 blob_get(rettv->vval.v_blob, i));
4690
4691 clear_tv(rettv);
4692 rettv_blob_set(rettv, blob);
4693 }
4694 }
4695 }
4696 else
4697 {
4698 // The resulting variable is a string of a single
4699 // character. If the index is too big or negative the
4700 // result is empty.
4701 if (n1 < len && n1 >= 0)
4702 {
4703 int v = (int)blob_get(rettv->vval.v_blob, n1);
4704
4705 clear_tv(rettv);
4706 rettv->v_type = VAR_NUMBER;
4707 rettv->vval.v_number = v;
4708 }
4709 else
4710 EMSGN(_(e_blobidx), n1);
4711 }
4712 break;
4713
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 if (n1 < 0)
4717 n1 = len + n1;
4718 if (!empty1 && (n1 < 0 || n1 >= len))
4719 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004720 /* For a range we allow invalid values and return an empty
4721 * list. A list index out of range is an error. */
4722 if (!range)
4723 {
4724 if (verbose)
4725 EMSGN(_(e_listidx), n1);
4726 return FAIL;
4727 }
4728 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 }
4730 if (range)
4731 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004732 list_T *l;
4733 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734
4735 if (n2 < 0)
4736 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004737 else if (n2 >= len)
4738 n2 = len - 1;
4739 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004740 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 l = list_alloc();
4742 if (l == NULL)
4743 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 n1 <= n2; ++n1)
4746 {
4747 if (list_append_tv(l, &item->li_tv) == FAIL)
4748 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004749 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 return FAIL;
4751 }
4752 item = item->li_next;
4753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004755 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 }
4757 else
4758 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004759 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
4761 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 }
4763 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764
4765 case VAR_DICT:
4766 if (range)
4767 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004768 if (verbose)
4769 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004770 if (len == -1)
4771 clear_tv(&var1);
4772 return FAIL;
4773 }
4774 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776
4777 if (len == -1)
4778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004779 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004780 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004782 clear_tv(&var1);
4783 return FAIL;
4784 }
4785 }
4786
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004787 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004789 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004790 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 if (len == -1)
4792 clear_tv(&var1);
4793 if (item == NULL)
4794 return FAIL;
4795
4796 copy_tv(&item->di_tv, &var1);
4797 clear_tv(rettv);
4798 *rettv = var1;
4799 }
4800 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 }
4802 }
4803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 return OK;
4805}
4806
4807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 * Get an option value.
4809 * "arg" points to the '&' or '+' before the option name.
4810 * "arg" is advanced to character after the option name.
4811 * Return OK or FAIL.
4812 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004814get_option_tv(
4815 char_u **arg,
4816 typval_T *rettv, /* when NULL, only check if option exists */
4817 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 char_u *option_end;
4820 long numval;
4821 char_u *stringval;
4822 int opt_type;
4823 int c;
4824 int working = (**arg == '+'); /* has("+option") */
4825 int ret = OK;
4826 int opt_flags;
4827
4828 /*
4829 * Isolate the option name and find its value.
4830 */
4831 option_end = find_option_end(arg, &opt_flags);
4832 if (option_end == NULL)
4833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 EMSG2(_("E112: Option name missing: %s"), *arg);
4836 return FAIL;
4837 }
4838
4839 if (!evaluate)
4840 {
4841 *arg = option_end;
4842 return OK;
4843 }
4844
4845 c = *option_end;
4846 *option_end = NUL;
4847 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 if (opt_type == -3) /* invalid name */
4851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 EMSG2(_("E113: Unknown option: %s"), *arg);
4854 ret = FAIL;
4855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
4858 if (opt_type == -2) /* hidden string option */
4859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 rettv->v_type = VAR_STRING;
4861 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 else if (opt_type == -1) /* hidden number option */
4864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 rettv->v_type = VAR_NUMBER;
4866 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 else if (opt_type == 1) /* number option */
4869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 rettv->v_type = VAR_NUMBER;
4871 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 else /* string option */
4874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 rettv->v_type = VAR_STRING;
4876 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 }
4879 else if (working && (opt_type == -2 || opt_type == -1))
4880 ret = FAIL;
4881
4882 *option_end = c; /* put back for error messages */
4883 *arg = option_end;
4884
4885 return ret;
4886}
4887
4888/*
4889 * Allocate a variable for a string constant.
4890 * Return OK or FAIL.
4891 */
4892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004893get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 char_u *p;
4896 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 int extra = 0;
4898
4899 /*
4900 * Find the end of the string, skipping backslashed characters.
4901 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004902 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
4904 if (*p == '\\' && p[1] != NUL)
4905 {
4906 ++p;
4907 /* A "\<x>" form occupies at least 4 characters, and produces up
4908 * to 6 characters: reserve space for 2 extra */
4909 if (*p == '<')
4910 extra += 2;
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913
4914 if (*p != '"')
4915 {
4916 EMSG2(_("E114: Missing quote: %s"), *arg);
4917 return FAIL;
4918 }
4919
4920 /* If only parsing, set *arg and return here */
4921 if (!evaluate)
4922 {
4923 *arg = p + 1;
4924 return OK;
4925 }
4926
4927 /*
4928 * Copy the string into allocated memory, handling backslashed
4929 * characters.
4930 */
4931 name = alloc((unsigned)(p - *arg + extra));
4932 if (name == NULL)
4933 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 rettv->v_type = VAR_STRING;
4935 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
4939 if (*p == '\\')
4940 {
4941 switch (*++p)
4942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943 case 'b': *name++ = BS; ++p; break;
4944 case 'e': *name++ = ESC; ++p; break;
4945 case 'f': *name++ = FF; ++p; break;
4946 case 'n': *name++ = NL; ++p; break;
4947 case 'r': *name++ = CAR; ++p; break;
4948 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 case 'X': /* hex: "\x1", "\x12" */
4951 case 'x':
4952 case 'u': /* Unicode: "\u0023" */
4953 case 'U':
4954 if (vim_isxdigit(p[1]))
4955 {
4956 int n, nr;
4957 int c = toupper(*p);
4958
4959 if (c == 'X')
4960 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004961 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004963 else
4964 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 nr = 0;
4966 while (--n >= 0 && vim_isxdigit(p[1]))
4967 {
4968 ++p;
4969 nr = (nr << 4) + hex2nr(*p);
4970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#ifdef FEAT_MBYTE
4973 /* For "\u" store the number according to
4974 * 'encoding'. */
4975 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 else
4978#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 break;
4982
4983 /* octal: "\1", "\12", "\123" */
4984 case '0':
4985 case '1':
4986 case '2':
4987 case '3':
4988 case '4':
4989 case '5':
4990 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 case '7': *name = *p++ - '0';
4992 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 *name = (*name << 3) + *p++ - '0';
4995 if (*p >= '0' && *p <= '7')
4996 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000
5001 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005002 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (extra != 0)
5004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007 }
5008 /* FALLTHROUGH */
5009
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 break;
5012 }
5013 }
5014 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005019 if (*p != NUL) /* just in case */
5020 ++p;
5021 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 return OK;
5024}
5025
5026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 * Return OK or FAIL.
5029 */
5030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005031get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005034 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035 int reduce = 0;
5036
5037 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005040 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005045 break;
5046 ++reduce;
5047 ++p;
5048 }
5049 }
5050
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054 return FAIL;
5055 }
5056
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 if (!evaluate)
5059 {
5060 *arg = p + 1;
5061 return OK;
5062 }
5063
5064 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 */
5067 str = alloc((unsigned)((p - *arg) - reduce));
5068 if (str == NULL)
5069 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 rettv->v_type = VAR_STRING;
5071 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 break;
5079 ++p;
5080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 *arg = p + 1;
5085
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 return OK;
5087}
5088
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005089/*
5090 * Return the function name of the partial.
5091 */
5092 char_u *
5093partial_name(partial_T *pt)
5094{
5095 if (pt->pt_name != NULL)
5096 return pt->pt_name;
5097 return pt->pt_func->uf_name;
5098}
5099
Bram Moolenaarddecc252016-04-06 22:59:37 +02005100 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005101partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005102{
5103 int i;
5104
5105 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005106 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005107 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005108 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005109 if (pt->pt_name != NULL)
5110 {
5111 func_unref(pt->pt_name);
5112 vim_free(pt->pt_name);
5113 }
5114 else
5115 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005116 vim_free(pt);
5117}
5118
5119/*
5120 * Unreference a closure: decrement the reference count and free it when it
5121 * becomes zero.
5122 */
5123 void
5124partial_unref(partial_T *pt)
5125{
5126 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005127 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005128}
5129
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005130static int tv_equal_recurse_limit;
5131
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005132 static int
5133func_equal(
5134 typval_T *tv1,
5135 typval_T *tv2,
5136 int ic) /* ignore case */
5137{
5138 char_u *s1, *s2;
5139 dict_T *d1, *d2;
5140 int a1, a2;
5141 int i;
5142
5143 /* empty and NULL function name considered the same */
5144 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005145 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005146 if (s1 != NULL && *s1 == NUL)
5147 s1 = NULL;
5148 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005149 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005150 if (s2 != NULL && *s2 == NUL)
5151 s2 = NULL;
5152 if (s1 == NULL || s2 == NULL)
5153 {
5154 if (s1 != s2)
5155 return FALSE;
5156 }
5157 else if (STRCMP(s1, s2) != 0)
5158 return FALSE;
5159
5160 /* empty dict and NULL dict is different */
5161 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5162 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5163 if (d1 == NULL || d2 == NULL)
5164 {
5165 if (d1 != d2)
5166 return FALSE;
5167 }
5168 else if (!dict_equal(d1, d2, ic, TRUE))
5169 return FALSE;
5170
5171 /* empty list and no list considered the same */
5172 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5173 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5174 if (a1 != a2)
5175 return FALSE;
5176 for (i = 0; i < a1; ++i)
5177 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5178 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5179 return FALSE;
5180
5181 return TRUE;
5182}
5183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005184/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005185 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005186 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005187 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005188 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005190tv_equal(
5191 typval_T *tv1,
5192 typval_T *tv2,
5193 int ic, /* ignore case */
5194 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005195{
5196 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005198 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005199 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005201 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005202 * recursiveness to a limit. We guess they are equal then.
5203 * A fixed limit has the problem of still taking an awful long time.
5204 * Reduce the limit every time running into it. That should work fine for
5205 * deeply linked structures that are not recursively linked and catch
5206 * recursiveness quickly. */
5207 if (!recursive)
5208 tv_equal_recurse_limit = 1000;
5209 if (recursive_cnt >= tv_equal_recurse_limit)
5210 {
5211 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005212 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005213 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005214
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005215 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5216 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005217 if ((tv1->v_type == VAR_FUNC
5218 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5219 && (tv2->v_type == VAR_FUNC
5220 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5221 {
5222 ++recursive_cnt;
5223 r = func_equal(tv1, tv2, ic);
5224 --recursive_cnt;
5225 return r;
5226 }
5227
5228 if (tv1->v_type != tv2->v_type)
5229 return FALSE;
5230
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005231 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005232 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005233 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005234 ++recursive_cnt;
5235 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5236 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005237 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005238
5239 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005240 ++recursive_cnt;
5241 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5242 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005243 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005244
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005245 case VAR_BLOB:
5246 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5247
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005248 case VAR_NUMBER:
5249 return tv1->vval.v_number == tv2->vval.v_number;
5250
5251 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005252 s1 = tv_get_string_buf(tv1, buf1);
5253 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005254 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005255
5256 case VAR_SPECIAL:
5257 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005258
5259 case VAR_FLOAT:
5260#ifdef FEAT_FLOAT
5261 return tv1->vval.v_float == tv2->vval.v_float;
5262#endif
5263 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005264#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005265 return tv1->vval.v_job == tv2->vval.v_job;
5266#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005267 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005269 return tv1->vval.v_channel == tv2->vval.v_channel;
5270#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005271 case VAR_FUNC:
5272 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005273 case VAR_UNKNOWN:
5274 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005275 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005276
Bram Moolenaara03f2332016-02-06 18:09:59 +01005277 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5278 * does not equal anything, not even itself. */
5279 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005280}
5281
5282/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005283 * Return the next (unique) copy ID.
5284 * Used for serializing nested structures.
5285 */
5286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005287get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005288{
5289 current_copyID += COPYID_INC;
5290 return current_copyID;
5291}
5292
5293/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005294 * Garbage collection for lists and dictionaries.
5295 *
5296 * We use reference counts to be able to free most items right away when they
5297 * are no longer used. But for composite items it's possible that it becomes
5298 * unused while the reference count is > 0: When there is a recursive
5299 * reference. Example:
5300 * :let l = [1, 2, 3]
5301 * :let d = {9: l}
5302 * :let l[1] = d
5303 *
5304 * Since this is quite unusual we handle this with garbage collection: every
5305 * once in a while find out which lists and dicts are not referenced from any
5306 * variable.
5307 *
5308 * Here is a good reference text about garbage collection (refers to Python
5309 * but it applies to all reference-counting mechanisms):
5310 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005311 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005312
5313/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005315 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005316 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005317 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005318 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005319garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005320{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005321 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005322 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005323 buf_T *buf;
5324 win_T *wp;
5325 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005326 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005327 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005328
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005329 if (!testing)
5330 {
5331 /* Only do this once. */
5332 want_garbage_collect = FALSE;
5333 may_garbage_collect = FALSE;
5334 garbage_collect_at_exit = FALSE;
5335 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005336
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005337 /* We advance by two because we add one for items referenced through
5338 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005339 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005340
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005341 /*
5342 * 1. Go through all accessible variables and mark all lists and dicts
5343 * with copyID.
5344 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005345
5346 /* Don't free variables in the previous_funccal list unless they are only
5347 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005348 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005350
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005351 /* script-local variables */
5352 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354
5355 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005356 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5358 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005359
5360 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005361 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5363 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005364 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005365 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5366 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005367
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005368 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005369 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5371 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005373 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374
5375 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005376 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005377
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005378 /* named functions (matters for closures) */
5379 abort = abort || set_ref_in_functions(copyID);
5380
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005381 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005382 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005383
Bram Moolenaard812df62008-11-09 12:46:09 +00005384 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005385 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005386
Bram Moolenaar1dced572012-04-05 16:54:08 +02005387#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005388 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005389#endif
5390
Bram Moolenaardb913952012-06-29 12:54:53 +02005391#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005392 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005393#endif
5394
5395#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005397#endif
5398
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005399#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005400 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005401 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005402#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005403#ifdef FEAT_NETBEANS_INTG
5404 abort = abort || set_ref_in_nb_channel(copyID);
5405#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005406
Bram Moolenaare3188e22016-05-31 21:13:04 +02005407#ifdef FEAT_TIMERS
5408 abort = abort || set_ref_in_timer(copyID);
5409#endif
5410
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005411#ifdef FEAT_QUICKFIX
5412 abort = abort || set_ref_in_quickfix(copyID);
5413#endif
5414
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005415#ifdef FEAT_TERMINAL
5416 abort = abort || set_ref_in_term(copyID);
5417#endif
5418
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005420 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 /*
5422 * 2. Free lists and dictionaries that are not referenced.
5423 */
5424 did_free = free_unref_items(copyID);
5425
5426 /*
5427 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005429 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005430 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005431 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005432 else if (p_verbose > 0)
5433 {
5434 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5435 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005436
5437 return did_free;
5438}
5439
5440/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005442 */
5443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005444free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005445{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005446 int did_free = FALSE;
5447
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005448 /* Let all "free" functions know that we are here. This means no
5449 * dictionaries, lists, channels or jobs are to be freed, because we will
5450 * do that here. */
5451 in_free_unref_items = TRUE;
5452
5453 /*
5454 * PASS 1: free the contents of the items. We don't free the items
5455 * themselves yet, so that it is possible to decrement refcount counters
5456 */
5457
Bram Moolenaarcd524592016-07-17 14:57:05 +02005458 /* Go through the list of dicts and free items without the copyID. */
5459 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005460
Bram Moolenaarda861d62016-07-17 15:46:27 +02005461 /* Go through the list of lists and free items without the copyID. */
5462 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005463
5464#ifdef FEAT_JOB_CHANNEL
5465 /* Go through the list of jobs and free items without the copyID. This
5466 * must happen before doing channels, because jobs refer to channels, but
5467 * the reference from the channel to the job isn't tracked. */
5468 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5469
5470 /* Go through the list of channels and free items without the copyID. */
5471 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5472#endif
5473
5474 /*
5475 * PASS 2: free the items themselves.
5476 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005477 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005478 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005480#ifdef FEAT_JOB_CHANNEL
5481 /* Go through the list of jobs and free items without the copyID. This
5482 * must happen before doing channels, because jobs refer to channels, but
5483 * the reference from the channel to the job isn't tracked. */
5484 free_unused_jobs(copyID, COPYID_MASK);
5485
5486 /* Go through the list of channels and free items without the copyID. */
5487 free_unused_channels(copyID, COPYID_MASK);
5488#endif
5489
5490 in_free_unref_items = FALSE;
5491
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005492 return did_free;
5493}
5494
5495/*
5496 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005497 * "list_stack" is used to add lists to be marked. Can be NULL.
5498 *
5499 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005500 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005502set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005503{
5504 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005505 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005506 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005507 hashtab_T *cur_ht;
5508 ht_stack_T *ht_stack = NULL;
5509 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 cur_ht = ht;
5512 for (;;)
5513 {
5514 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005515 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005516 /* Mark each item in the hashtab. If the item contains a hashtab
5517 * it is added to ht_stack, if it contains a list it is added to
5518 * list_stack. */
5519 todo = (int)cur_ht->ht_used;
5520 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5521 if (!HASHITEM_EMPTY(hi))
5522 {
5523 --todo;
5524 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5525 &ht_stack, list_stack);
5526 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005527 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005528
5529 if (ht_stack == NULL)
5530 break;
5531
5532 /* take an item from the stack */
5533 cur_ht = ht_stack->ht;
5534 tempitem = ht_stack;
5535 ht_stack = ht_stack->prev;
5536 free(tempitem);
5537 }
5538
5539 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005540}
5541
5542/*
5543 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005544 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5545 *
5546 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005547 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005548 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005549set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005550{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005551 listitem_T *li;
5552 int abort = FALSE;
5553 list_T *cur_l;
5554 list_stack_T *list_stack = NULL;
5555 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005556
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005557 cur_l = l;
5558 for (;;)
5559 {
5560 if (!abort)
5561 /* Mark each item in the list. If the item contains a hashtab
5562 * it is added to ht_stack, if it contains a list it is added to
5563 * list_stack. */
5564 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5565 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5566 ht_stack, &list_stack);
5567 if (list_stack == NULL)
5568 break;
5569
5570 /* take an item from the stack */
5571 cur_l = list_stack->list;
5572 tempitem = list_stack;
5573 list_stack = list_stack->prev;
5574 free(tempitem);
5575 }
5576
5577 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005578}
5579
5580/*
5581 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 * "list_stack" is used to add lists to be marked. Can be NULL.
5583 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5584 *
5585 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005586 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005588set_ref_in_item(
5589 typval_T *tv,
5590 int copyID,
5591 ht_stack_T **ht_stack,
5592 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005593{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005594 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005595
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005596 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005597 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005598 dict_T *dd = tv->vval.v_dict;
5599
Bram Moolenaara03f2332016-02-06 18:09:59 +01005600 if (dd != NULL && dd->dv_copyID != copyID)
5601 {
5602 /* Didn't see this dict yet. */
5603 dd->dv_copyID = copyID;
5604 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005605 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005606 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5607 }
5608 else
5609 {
5610 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5611 if (newitem == NULL)
5612 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005613 else
5614 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005615 newitem->ht = &dd->dv_hashtab;
5616 newitem->prev = *ht_stack;
5617 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005618 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005619 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005620 }
5621 }
5622 else if (tv->v_type == VAR_LIST)
5623 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005624 list_T *ll = tv->vval.v_list;
5625
Bram Moolenaara03f2332016-02-06 18:09:59 +01005626 if (ll != NULL && ll->lv_copyID != copyID)
5627 {
5628 /* Didn't see this list yet. */
5629 ll->lv_copyID = copyID;
5630 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005631 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005632 abort = set_ref_in_list(ll, copyID, ht_stack);
5633 }
5634 else
5635 {
5636 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005637 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005638 if (newitem == NULL)
5639 abort = TRUE;
5640 else
5641 {
5642 newitem->list = ll;
5643 newitem->prev = *list_stack;
5644 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005645 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005646 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005647 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005648 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005649 else if (tv->v_type == VAR_FUNC)
5650 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005651 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005652 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005653 else if (tv->v_type == VAR_PARTIAL)
5654 {
5655 partial_T *pt = tv->vval.v_partial;
5656 int i;
5657
5658 /* A partial does not have a copyID, because it cannot contain itself.
5659 */
5660 if (pt != NULL)
5661 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005662 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005663
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005664 if (pt->pt_dict != NULL)
5665 {
5666 typval_T dtv;
5667
5668 dtv.v_type = VAR_DICT;
5669 dtv.vval.v_dict = pt->pt_dict;
5670 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5671 }
5672
5673 for (i = 0; i < pt->pt_argc; ++i)
5674 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5675 ht_stack, list_stack);
5676 }
5677 }
5678#ifdef FEAT_JOB_CHANNEL
5679 else if (tv->v_type == VAR_JOB)
5680 {
5681 job_T *job = tv->vval.v_job;
5682 typval_T dtv;
5683
5684 if (job != NULL && job->jv_copyID != copyID)
5685 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005686 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005687 if (job->jv_channel != NULL)
5688 {
5689 dtv.v_type = VAR_CHANNEL;
5690 dtv.vval.v_channel = job->jv_channel;
5691 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5692 }
5693 if (job->jv_exit_partial != NULL)
5694 {
5695 dtv.v_type = VAR_PARTIAL;
5696 dtv.vval.v_partial = job->jv_exit_partial;
5697 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5698 }
5699 }
5700 }
5701 else if (tv->v_type == VAR_CHANNEL)
5702 {
5703 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005704 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005705 typval_T dtv;
5706 jsonq_T *jq;
5707 cbq_T *cq;
5708
5709 if (ch != NULL && ch->ch_copyID != copyID)
5710 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005711 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005712 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005713 {
5714 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5715 jq = jq->jq_next)
5716 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5717 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5718 cq = cq->cq_next)
5719 if (cq->cq_partial != NULL)
5720 {
5721 dtv.v_type = VAR_PARTIAL;
5722 dtv.vval.v_partial = cq->cq_partial;
5723 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5724 }
5725 if (ch->ch_part[part].ch_partial != NULL)
5726 {
5727 dtv.v_type = VAR_PARTIAL;
5728 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5729 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5730 }
5731 }
5732 if (ch->ch_partial != NULL)
5733 {
5734 dtv.v_type = VAR_PARTIAL;
5735 dtv.vval.v_partial = ch->ch_partial;
5736 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5737 }
5738 if (ch->ch_close_partial != NULL)
5739 {
5740 dtv.v_type = VAR_PARTIAL;
5741 dtv.vval.v_partial = ch->ch_close_partial;
5742 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5743 }
5744 }
5745 }
5746#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005747 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005748}
5749
Bram Moolenaar17a13432016-01-24 14:22:10 +01005750 static char *
5751get_var_special_name(int nr)
5752{
5753 switch (nr)
5754 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005755 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005756 case VVAL_TRUE: return "v:true";
5757 case VVAL_NONE: return "v:none";
5758 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005759 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005760 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005761 return "42";
5762}
5763
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 * Return a string with the string representation of a variable.
5766 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005767 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005768 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005769 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5770 * stings as "string()", otherwise does not put quotes around strings, as
5771 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005772 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5773 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005774 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005776 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005777echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005778 typval_T *tv,
5779 char_u **tofree,
5780 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005781 int copyID,
5782 int echo_style,
5783 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005784 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005786 static int recurse = 0;
5787 char_u *r = NULL;
5788
Bram Moolenaar33570922005-01-25 22:26:29 +00005789 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005791 if (!did_echo_string_emsg)
5792 {
5793 /* Only give this message once for a recursive call to avoid
5794 * flooding the user with errors. And stop iterating over lists
5795 * and dicts. */
5796 did_echo_string_emsg = TRUE;
5797 EMSG(_("E724: variable nested too deep for displaying"));
5798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005799 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005800 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005801 }
5802 ++recurse;
5803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 switch (tv->v_type)
5805 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005806 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005807 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005808 {
5809 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005810 r = tv->vval.v_string;
5811 if (r == NULL)
5812 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005813 }
5814 else
5815 {
5816 *tofree = string_quote(tv->vval.v_string, FALSE);
5817 r = *tofree;
5818 }
5819 break;
5820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005822 if (echo_style)
5823 {
5824 *tofree = NULL;
5825 r = tv->vval.v_string;
5826 }
5827 else
5828 {
5829 *tofree = string_quote(tv->vval.v_string, TRUE);
5830 r = *tofree;
5831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005832 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005833
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005834 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005835 {
5836 partial_T *pt = tv->vval.v_partial;
5837 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005838 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005839 garray_T ga;
5840 int i;
5841 char_u *tf;
5842
5843 ga_init2(&ga, 1, 100);
5844 ga_concat(&ga, (char_u *)"function(");
5845 if (fname != NULL)
5846 {
5847 ga_concat(&ga, fname);
5848 vim_free(fname);
5849 }
5850 if (pt != NULL && pt->pt_argc > 0)
5851 {
5852 ga_concat(&ga, (char_u *)", [");
5853 for (i = 0; i < pt->pt_argc; ++i)
5854 {
5855 if (i > 0)
5856 ga_concat(&ga, (char_u *)", ");
5857 ga_concat(&ga,
5858 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5859 vim_free(tf);
5860 }
5861 ga_concat(&ga, (char_u *)"]");
5862 }
5863 if (pt != NULL && pt->pt_dict != NULL)
5864 {
5865 typval_T dtv;
5866
5867 ga_concat(&ga, (char_u *)", ");
5868 dtv.v_type = VAR_DICT;
5869 dtv.vval.v_dict = pt->pt_dict;
5870 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5871 vim_free(tf);
5872 }
5873 ga_concat(&ga, (char_u *)")");
5874
5875 *tofree = ga.ga_data;
5876 r = *tofree;
5877 break;
5878 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005879
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005880 case VAR_BLOB:
5881 if (tv->vval.v_blob == NULL)
5882 {
5883 *tofree = NULL;
5884 r = (char_u *)"[]";
5885 }
5886 else
5887 {
5888 blob_T *b;
5889 int i;
5890 garray_T ga;
5891
5892 // Store bytes in the growarray.
5893 ga_init2(&ga, 1, 4000);
5894 b = tv->vval.v_blob;
5895 ga_append(&ga, '[');
5896 for (i = 0; i < blob_len(b); i++)
5897 {
5898 if (i > 0)
5899 ga_concat(&ga, (char_u *)",");
5900 vim_snprintf((char *)numbuf, NUMBUFLEN, "0x%02X",
5901 (int)blob_get(b, i));
5902 ga_concat(&ga, numbuf);
5903 }
5904 ga_append(&ga, ']');
5905 *tofree = ga.ga_data;
5906 r = *tofree;
5907 }
5908 break;
5909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005911 if (tv->vval.v_list == NULL)
5912 {
5913 *tofree = NULL;
5914 r = NULL;
5915 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005916 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5917 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005918 {
5919 *tofree = NULL;
5920 r = (char_u *)"[...]";
5921 }
5922 else
5923 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005924 int old_copyID = tv->vval.v_list->lv_copyID;
5925
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005926 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005927 *tofree = list2string(tv, copyID, restore_copyID);
5928 if (restore_copyID)
5929 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 r = *tofree;
5931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005933
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935 if (tv->vval.v_dict == NULL)
5936 {
5937 *tofree = NULL;
5938 r = NULL;
5939 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005940 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5941 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005942 {
5943 *tofree = NULL;
5944 r = (char_u *)"{...}";
5945 }
5946 else
5947 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005948 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005949 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005950 *tofree = dict2string(tv, copyID, restore_copyID);
5951 if (restore_copyID)
5952 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953 r = *tofree;
5954 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005957 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005958 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005959 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005960 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005961 break;
5962
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005964 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005966 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005967 if (composite_val)
5968 {
5969 *tofree = string_quote(r, FALSE);
5970 r = *tofree;
5971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 *tofree = NULL;
5977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5978 r = numbuf;
5979 break;
5980#endif
5981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005982 case VAR_SPECIAL:
5983 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005984 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987
Bram Moolenaar8502c702014-06-17 12:51:16 +02005988 if (--recurse == 0)
5989 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991}
5992
5993/*
5994 * Return a string with the string representation of a variable.
5995 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5996 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005997 * Does not put quotes around strings, as ":echo" displays values.
5998 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5999 * May return NULL.
6000 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006001 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002echo_string(
6003 typval_T *tv,
6004 char_u **tofree,
6005 char_u *numbuf,
6006 int copyID)
6007{
6008 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6009}
6010
6011/*
6012 * Return a string with the string representation of a variable.
6013 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6014 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006016 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006018 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006019tv2string(
6020 typval_T *tv,
6021 char_u **tofree,
6022 char_u *numbuf,
6023 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006024{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 * Return string "str" in ' quotes, doubling ' characters.
6030 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006031 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006032 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037 char_u *p, *r, *s;
6038
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 len = (function ? 13 : 3);
6040 if (str != NULL)
6041 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006042 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006043 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 if (*p == '\'')
6045 ++len;
6046 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047 s = r = alloc(len);
6048 if (r != NULL)
6049 {
6050 if (function)
6051 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006053 r += 10;
6054 }
6055 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006056 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 if (str != NULL)
6058 for (p = str; *p != NUL; )
6059 {
6060 if (*p == '\'')
6061 *r++ = '\'';
6062 MB_COPY_CHAR(p, r);
6063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006065 if (function)
6066 *r++ = ')';
6067 *r++ = NUL;
6068 }
6069 return s;
6070}
6071
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006072#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006073/*
6074 * Convert the string "text" to a floating point number.
6075 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6076 * this always uses a decimal point.
6077 * Returns the length of the text that was consumed.
6078 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080string2float(
6081 char_u *text,
6082 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006083{
6084 char *s = (char *)text;
6085 float_T f;
6086
Bram Moolenaar62473612017-01-08 19:25:40 +01006087 /* MS-Windows does not deal with "inf" and "nan" properly. */
6088 if (STRNICMP(text, "inf", 3) == 0)
6089 {
6090 *value = INFINITY;
6091 return 3;
6092 }
6093 if (STRNICMP(text, "-inf", 3) == 0)
6094 {
6095 *value = -INFINITY;
6096 return 4;
6097 }
6098 if (STRNICMP(text, "nan", 3) == 0)
6099 {
6100 *value = NAN;
6101 return 3;
6102 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006103 f = strtod(s, &s);
6104 *value = f;
6105 return (int)((char_u *)s - text);
6106}
6107#endif
6108
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 * Get the value of an environment variable.
6111 * "arg" is pointing to the '$'. It is advanced to after the name.
6112 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006113 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 char_u *string = NULL;
6119 int len;
6120 int cc;
6121 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006122 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
6124 ++*arg;
6125 name = *arg;
6126 len = get_env_len(arg);
6127 if (evaluate)
6128 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006129 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006130 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006131
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006132 cc = name[len];
6133 name[len] = NUL;
6134 /* first try vim_getenv(), fast for normal environment vars */
6135 string = vim_getenv(name, &mustfree);
6136 if (string != NULL && *string != NUL)
6137 {
6138 if (!mustfree)
6139 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006141 else
6142 {
6143 if (mustfree)
6144 vim_free(string);
6145
6146 /* next try expanding things like $VIM and ${HOME} */
6147 string = expand_env_save(name - 1);
6148 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006149 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006150 }
6151 name[len] = cc;
6152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153 rettv->v_type = VAR_STRING;
6154 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 }
6156
6157 return OK;
6158}
6159
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006160
6161
6162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006164 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006166 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167var2fpos(
6168 typval_T *varp,
6169 int dollar_lnum, /* TRUE when $ is last line */
6170 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006172 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006174 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaara5525202006-03-02 22:52:09 +00006176 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006177 if (varp->v_type == VAR_LIST)
6178 {
6179 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006180 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006181 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006182 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006183
6184 l = varp->vval.v_list;
6185 if (l == NULL)
6186 return NULL;
6187
6188 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006189 pos.lnum = list_find_nr(l, 0L, &error);
6190 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006191 return NULL; /* invalid line number */
6192
6193 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006194 pos.col = list_find_nr(l, 1L, &error);
6195 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006197 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006198
6199 /* We accept "$" for the column number: last column. */
6200 li = list_find(l, 1L);
6201 if (li != NULL && li->li_tv.v_type == VAR_STRING
6202 && li->li_tv.vval.v_string != NULL
6203 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6204 pos.col = len + 1;
6205
Bram Moolenaara5525202006-03-02 22:52:09 +00006206 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006207 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006208 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006209 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006210
Bram Moolenaara5525202006-03-02 22:52:09 +00006211#ifdef FEAT_VIRTUALEDIT
6212 /* Get the virtual offset. Defaults to zero. */
6213 pos.coladd = list_find_nr(l, 2L, &error);
6214 if (error)
6215 pos.coladd = 0;
6216#endif
6217
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006218 return &pos;
6219 }
6220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006221 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006222 if (name == NULL)
6223 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006224 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006226 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6227 {
6228 if (VIsual_active)
6229 return &VIsual;
6230 return &curwin->w_cursor;
6231 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006232 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006234 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6236 return NULL;
6237 return pp;
6238 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006239
6240#ifdef FEAT_VIRTUALEDIT
6241 pos.coladd = 0;
6242#endif
6243
Bram Moolenaar477933c2007-07-17 14:32:23 +00006244 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006245 {
6246 pos.col = 0;
6247 if (name[1] == '0') /* "w0": first visible line */
6248 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006250 /* In silent Ex mode topline is zero, but that's not a valid line
6251 * number; use one instead. */
6252 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006253 return &pos;
6254 }
6255 else if (name[1] == '$') /* "w$": last visible line */
6256 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006257 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006258 /* In silent Ex mode botline is zero, return zero then. */
6259 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006260 return &pos;
6261 }
6262 }
6263 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006265 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 pos.lnum = curbuf->b_ml.ml_line_count;
6268 pos.col = 0;
6269 }
6270 else
6271 {
6272 pos.lnum = curwin->w_cursor.lnum;
6273 pos.col = (colnr_T)STRLEN(ml_get_curline());
6274 }
6275 return &pos;
6276 }
6277 return NULL;
6278}
6279
6280/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006281 * Convert list in "arg" into a position and optional file number.
6282 * When "fnump" is NULL there is no file number, only 3 items.
6283 * Note that the column is passed on as-is, the caller may want to decrement
6284 * it to use 1 for the first column.
6285 * Return FAIL when conversion is not possible, doesn't check the position for
6286 * validity.
6287 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006289list2fpos(
6290 typval_T *arg,
6291 pos_T *posp,
6292 int *fnump,
6293 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006294{
6295 list_T *l = arg->vval.v_list;
6296 long i = 0;
6297 long n;
6298
Bram Moolenaar493c1782014-05-28 14:34:46 +02006299 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6300 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006301 if (arg->v_type != VAR_LIST
6302 || l == NULL
6303 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006304 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006305 return FAIL;
6306
6307 if (fnump != NULL)
6308 {
6309 n = list_find_nr(l, i++, NULL); /* fnum */
6310 if (n < 0)
6311 return FAIL;
6312 if (n == 0)
6313 n = curbuf->b_fnum; /* current buffer */
6314 *fnump = n;
6315 }
6316
6317 n = list_find_nr(l, i++, NULL); /* lnum */
6318 if (n < 0)
6319 return FAIL;
6320 posp->lnum = n;
6321
6322 n = list_find_nr(l, i++, NULL); /* col */
6323 if (n < 0)
6324 return FAIL;
6325 posp->col = n;
6326
6327#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006328 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006329 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006330 posp->coladd = 0;
6331 else
6332 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006333#endif
6334
Bram Moolenaar493c1782014-05-28 14:34:46 +02006335 if (curswantp != NULL)
6336 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6337
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006338 return OK;
6339}
6340
6341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 * Get the length of an environment variable name.
6343 * Advance "arg" to the first character after the name.
6344 * Return 0 for error.
6345 */
6346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006347get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
6349 char_u *p;
6350 int len;
6351
6352 for (p = *arg; vim_isIDc(*p); ++p)
6353 ;
6354 if (p == *arg) /* no name found */
6355 return 0;
6356
6357 len = (int)(p - *arg);
6358 *arg = p;
6359 return len;
6360}
6361
6362/*
6363 * Get the length of the name of a function or internal variable.
6364 * "arg" is advanced to the first non-white character after the name.
6365 * Return 0 if something is wrong.
6366 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 char_u *p;
6371 int len;
6372
6373 /* Find the end of the name. */
6374 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006375 {
6376 if (*p == ':')
6377 {
6378 /* "s:" is start of "s:var", but "n:" is not and can be used in
6379 * slice "[n:]". Also "xx:" is not a namespace. */
6380 len = (int)(p - *arg);
6381 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6382 || len > 1)
6383 break;
6384 }
6385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (p == *arg) /* no name found */
6387 return 0;
6388
6389 len = (int)(p - *arg);
6390 *arg = skipwhite(p);
6391
6392 return len;
6393}
6394
6395/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006396 * Get the length of the name of a variable or function.
6397 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006399 * Return -1 if curly braces expansion failed.
6400 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * If the name contains 'magic' {}'s, expand them and return the
6402 * expanded name in an allocated string via 'alias' - caller must free.
6403 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006405get_name_len(
6406 char_u **arg,
6407 char_u **alias,
6408 int evaluate,
6409 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 char_u *p;
6413 char_u *expr_start;
6414 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415
6416 *alias = NULL; /* default to no alias */
6417
6418 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6419 && (*arg)[2] == (int)KE_SNR)
6420 {
6421 /* hard coded <SNR>, already translated */
6422 *arg += 3;
6423 return get_id_len(arg) + 3;
6424 }
6425 len = eval_fname_script(*arg);
6426 if (len > 0)
6427 {
6428 /* literal "<SID>", "s:" or "<SNR>" */
6429 *arg += len;
6430 }
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006433 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006435 p = find_name_end(*arg, &expr_start, &expr_end,
6436 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (expr_start != NULL)
6438 {
6439 char_u *temp_string;
6440
6441 if (!evaluate)
6442 {
6443 len += (int)(p - *arg);
6444 *arg = skipwhite(p);
6445 return len;
6446 }
6447
6448 /*
6449 * Include any <SID> etc in the expanded string:
6450 * Thus the -len here.
6451 */
6452 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6453 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006454 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 *alias = temp_string;
6456 *arg = skipwhite(p);
6457 return (int)STRLEN(temp_string);
6458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
6460 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006461 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 EMSG2(_(e_invexpr2), *arg);
6463
6464 return len;
6465}
6466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467/*
6468 * Find the end of a variable or function name, taking care of magic braces.
6469 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6470 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006471 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006472 * Return a pointer to just after the name. Equal to "arg" if there is no
6473 * valid name.
6474 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476find_name_end(
6477 char_u *arg,
6478 char_u **expr_start,
6479 char_u **expr_end,
6480 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006482 int mb_nest = 0;
6483 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006485 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006489 *expr_start = NULL;
6490 *expr_end = NULL;
6491 }
6492
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006493 /* Quick check for valid starting character. */
6494 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6495 return arg;
6496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006497 for (p = arg; *p != NUL
6498 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006500 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006501 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006502 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006504 if (*p == '\'')
6505 {
6506 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006507 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006508 ;
6509 if (*p == NUL)
6510 break;
6511 }
6512 else if (*p == '"')
6513 {
6514 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006515 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006516 if (*p == '\\' && p[1] != NUL)
6517 ++p;
6518 if (*p == NUL)
6519 break;
6520 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006521 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6522 {
6523 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006524 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006525 len = (int)(p - arg);
6526 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006527 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006528 break;
6529 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006531 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 if (*p == '[')
6534 ++br_nest;
6535 else if (*p == ']')
6536 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006539 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006541 if (*p == '{')
6542 {
6543 mb_nest++;
6544 if (expr_start != NULL && *expr_start == NULL)
6545 *expr_start = p;
6546 }
6547 else if (*p == '}')
6548 {
6549 mb_nest--;
6550 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6551 *expr_end = p;
6552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
6555
6556 return p;
6557}
6558
6559/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006560 * Expands out the 'magic' {}'s in a variable/function name.
6561 * Note that this can call itself recursively, to deal with
6562 * constructs like foo{bar}{baz}{bam}
6563 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6564 * "in_start" ^
6565 * "expr_start" ^
6566 * "expr_end" ^
6567 * "in_end" ^
6568 *
6569 * Returns a new allocated string, which the caller must free.
6570 * Returns NULL for failure.
6571 */
6572 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573make_expanded_name(
6574 char_u *in_start,
6575 char_u *expr_start,
6576 char_u *expr_end,
6577 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006578{
6579 char_u c1;
6580 char_u *retval = NULL;
6581 char_u *temp_result;
6582 char_u *nextcmd = NULL;
6583
6584 if (expr_end == NULL || in_end == NULL)
6585 return NULL;
6586 *expr_start = NUL;
6587 *expr_end = NUL;
6588 c1 = *in_end;
6589 *in_end = NUL;
6590
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006591 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006592 if (temp_result != NULL && nextcmd == NULL)
6593 {
6594 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6595 + (in_end - expr_end) + 1));
6596 if (retval != NULL)
6597 {
6598 STRCPY(retval, in_start);
6599 STRCAT(retval, temp_result);
6600 STRCAT(retval, expr_end + 1);
6601 }
6602 }
6603 vim_free(temp_result);
6604
6605 *in_end = c1; /* put char back for error messages */
6606 *expr_start = '{';
6607 *expr_end = '}';
6608
6609 if (retval != NULL)
6610 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006611 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006612 if (expr_start != NULL)
6613 {
6614 /* Further expansion! */
6615 temp_result = make_expanded_name(retval, expr_start,
6616 expr_end, temp_result);
6617 vim_free(retval);
6618 retval = temp_result;
6619 }
6620 }
6621
6622 return retval;
6623}
6624
6625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006630eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006632 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6633}
6634
6635/*
6636 * Return TRUE if character "c" can be used as the first character in a
6637 * variable or function name (excluding '{' and '}').
6638 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006639 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006641{
6642 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
6644
6645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 * Set number v: variable to "val".
6647 */
6648 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006649set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652}
6653
6654/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006655 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006657 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006658get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006660 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661}
6662
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006663/*
6664 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006665 * If the String variable has never been set, return an empty string.
6666 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006667 */
6668 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006670{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006671 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006672}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006673
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006675 * Get List v: variable value. Caller must take care of reference count when
6676 * needed.
6677 */
6678 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006679get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006680{
6681 return vimvars[idx].vv_list;
6682}
6683
6684/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006685 * Get Dict v: variable value. Caller must take care of reference count when
6686 * needed.
6687 */
6688 dict_T *
6689get_vim_var_dict(int idx)
6690{
6691 return vimvars[idx].vv_dict;
6692}
6693
6694/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006695 * Set v:char to character "c".
6696 */
6697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006699{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006700 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006701
6702#ifdef FEAT_MBYTE
6703 if (has_mbyte)
6704 buf[(*mb_char2bytes)(c, buf)] = NUL;
6705 else
6706#endif
6707 {
6708 buf[0] = c;
6709 buf[1] = NUL;
6710 }
6711 set_vim_var_string(VV_CHAR, buf, -1);
6712}
6713
6714/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006715 * Set v:count to "count" and v:count1 to "count1".
6716 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 */
6718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006719set_vcount(
6720 long count,
6721 long count1,
6722 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006724 if (set_prevcount)
6725 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006726 vimvars[VV_COUNT].vv_nr = count;
6727 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728}
6729
6730/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006731 * Save variables that might be changed as a side effect. Used when executing
6732 * a timer callback.
6733 */
6734 void
6735save_vimvars(vimvars_save_T *vvsave)
6736{
6737 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6738 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6739 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6740}
6741
6742/*
6743 * Restore variables saved by save_vimvars().
6744 */
6745 void
6746restore_vimvars(vimvars_save_T *vvsave)
6747{
6748 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6749 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6750 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6751}
6752
6753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 * Set string v: variable to a copy of "val".
6755 */
6756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006757set_vim_var_string(
6758 int idx,
6759 char_u *val,
6760 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
Bram Moolenaara542c682016-01-31 16:28:04 +01006762 clear_tv(&vimvars[idx].vv_di.di_tv);
6763 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006765 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006767 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770}
6771
6772/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006773 * Set List v: variable to "val".
6774 */
6775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006776set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006777{
Bram Moolenaara542c682016-01-31 16:28:04 +01006778 clear_tv(&vimvars[idx].vv_di.di_tv);
6779 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006780 vimvars[idx].vv_list = val;
6781 if (val != NULL)
6782 ++val->lv_refcount;
6783}
6784
6785/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006786 * Set Dictionary v: variable to "val".
6787 */
6788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006789set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006790{
Bram Moolenaara542c682016-01-31 16:28:04 +01006791 clear_tv(&vimvars[idx].vv_di.di_tv);
6792 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006793 vimvars[idx].vv_dict = val;
6794 if (val != NULL)
6795 {
6796 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006797 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006798 }
6799}
6800
6801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 * Set v:register if needed.
6803 */
6804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006805set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 char_u regname;
6808
6809 if (c == 0 || c == ' ')
6810 regname = '"';
6811 else
6812 regname = c;
6813 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006814 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 set_vim_var_string(VV_REG, &regname, 1);
6816}
6817
6818/*
6819 * Get or set v:exception. If "oldval" == NULL, return the current value.
6820 * Otherwise, restore the value to "oldval" and return NULL.
6821 * Must always be called in pairs to save and restore v:exception! Does not
6822 * take care of memory allocations.
6823 */
6824 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006825v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
6827 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006828 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
Bram Moolenaare9a41262005-01-15 22:18:47 +00006830 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 return NULL;
6832}
6833
6834/*
6835 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6836 * Otherwise, restore the value to "oldval" and return NULL.
6837 * Must always be called in pairs to save and restore v:throwpoint! Does not
6838 * take care of memory allocations.
6839 */
6840 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 return NULL;
6848}
6849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850/*
6851 * Set v:cmdarg.
6852 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6853 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6854 * Must always be called in pairs!
6855 */
6856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006857set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 char_u *oldval;
6860 char_u *newval;
6861 unsigned len;
6862
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006864 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006866 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006868 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006871 if (eap->force_bin == FORCE_BIN)
6872 len = 6;
6873 else if (eap->force_bin == FORCE_NOBIN)
6874 len = 8;
6875 else
6876 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006877
6878 if (eap->read_edit)
6879 len += 7;
6880
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006881 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006882 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006883# ifdef FEAT_MBYTE
6884 if (eap->force_enc != 0)
6885 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006886 if (eap->bad_char != 0)
6887 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006888# endif
6889
6890 newval = alloc(len + 1);
6891 if (newval == NULL)
6892 return NULL;
6893
6894 if (eap->force_bin == FORCE_BIN)
6895 sprintf((char *)newval, " ++bin");
6896 else if (eap->force_bin == FORCE_NOBIN)
6897 sprintf((char *)newval, " ++nobin");
6898 else
6899 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900
6901 if (eap->read_edit)
6902 STRCAT(newval, " ++edit");
6903
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006904 if (eap->force_ff != 0)
6905 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006906 eap->force_ff == 'u' ? "unix"
6907 : eap->force_ff == 'd' ? "dos"
6908 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006909#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006910 if (eap->force_enc != 0)
6911 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6912 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006913 if (eap->bad_char == BAD_KEEP)
6914 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6915 else if (eap->bad_char == BAD_DROP)
6916 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6917 else if (eap->bad_char != 0)
6918 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006919#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006920 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006921 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924/*
6925 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006926 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006929get_var_tv(
6930 char_u *name,
6931 int len, /* length of "name" */
6932 typval_T *rettv, /* NULL when only checking existence */
6933 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6934 int verbose, /* may give error message */
6935 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 /* truncate the name, so that we can use strcmp() */
6943 cc = name[len];
6944 name[len] = NUL;
6945
6946 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 * Check for user-defined variables.
6948 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006949 v = find_var(name, NULL, no_autoload);
6950 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006952 tv = &v->di_tv;
6953 if (dip != NULL)
6954 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 }
6956
Bram Moolenaare9a41262005-01-15 22:18:47 +00006957 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006959 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006960 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 ret = FAIL;
6962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006963 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006964 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
6966 name[len] = cc;
6967
6968 return ret;
6969}
6970
6971/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006972 * Check if variable "name[len]" is a local variable or an argument.
6973 * If so, "*eval_lavars_used" is set to TRUE.
6974 */
6975 static void
6976check_vars(char_u *name, int len)
6977{
6978 int cc;
6979 char_u *varname;
6980 hashtab_T *ht;
6981
6982 if (eval_lavars_used == NULL)
6983 return;
6984
6985 /* truncate the name, so that we can use strcmp() */
6986 cc = name[len];
6987 name[len] = NUL;
6988
6989 ht = find_var_ht(name, &varname);
6990 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6991 {
6992 if (find_var(name, NULL, TRUE) != NULL)
6993 *eval_lavars_used = TRUE;
6994 }
6995
6996 name[len] = cc;
6997}
6998
6999/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007000 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7001 * Also handle function call with Funcref variable: func(expr)
7002 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7003 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005handle_subscript(
7006 char_u **arg,
7007 typval_T *rettv,
7008 int evaluate, /* do more than finding the end */
7009 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007010{
7011 int ret = OK;
7012 dict_T *selfdict = NULL;
7013 char_u *s;
7014 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007015 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007016
7017 while (ret == OK
7018 && (**arg == '['
7019 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007020 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7021 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007022 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007023 {
7024 if (**arg == '(')
7025 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007026 partial_T *pt = NULL;
7027
Bram Moolenaard9fba312005-06-26 22:34:35 +00007028 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007029 if (evaluate)
7030 {
7031 functv = *rettv;
7032 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007033
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007034 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007035 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007036 {
7037 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007038 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007039 }
7040 else
7041 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007042 }
7043 else
7044 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007045 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007047 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007048
7049 /* Clear the funcref afterwards, so that deleting it while
7050 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007051 if (evaluate)
7052 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007053
7054 /* Stop the expression evaluation when immediately aborting on
7055 * error, or when an interrupt occurred or an exception was thrown
7056 * but not caught. */
7057 if (aborting())
7058 {
7059 if (ret == OK)
7060 clear_tv(rettv);
7061 ret = FAIL;
7062 }
7063 dict_unref(selfdict);
7064 selfdict = NULL;
7065 }
7066 else /* **arg == '[' || **arg == '.' */
7067 {
7068 dict_unref(selfdict);
7069 if (rettv->v_type == VAR_DICT)
7070 {
7071 selfdict = rettv->vval.v_dict;
7072 if (selfdict != NULL)
7073 ++selfdict->dv_refcount;
7074 }
7075 else
7076 selfdict = NULL;
7077 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7078 {
7079 clear_tv(rettv);
7080 ret = FAIL;
7081 }
7082 }
7083 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007084
Bram Moolenaar1d429612016-05-24 15:44:17 +02007085 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7086 * Don't do this when "Func" is already a partial that was bound
7087 * explicitly (pt_auto is FALSE). */
7088 if (selfdict != NULL
7089 && (rettv->v_type == VAR_FUNC
7090 || (rettv->v_type == VAR_PARTIAL
7091 && (rettv->vval.v_partial->pt_auto
7092 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007093 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007094
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007095 dict_unref(selfdict);
7096 return ret;
7097}
7098
7099/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007100 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007101 * value).
7102 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007103 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007107}
7108
7109/*
7110 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 * The string "s" must have been allocated, it is consumed.
7112 * Return NULL for out of memory, the variable otherwise.
7113 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007115alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 rettv = alloc_tv();
7120 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007122 rettv->v_type = VAR_STRING;
7123 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 }
7125 else
7126 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007127 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128}
7129
7130/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007131 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007134free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 if (varp != NULL)
7137 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007138 switch (varp->v_type)
7139 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007140 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007141 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007142 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007143 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007144 vim_free(varp->vval.v_string);
7145 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007146 case VAR_PARTIAL:
7147 partial_unref(varp->vval.v_partial);
7148 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007149 case VAR_BLOB:
7150 blob_unref(varp->vval.v_blob);
7151 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007152 case VAR_LIST:
7153 list_unref(varp->vval.v_list);
7154 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007155 case VAR_DICT:
7156 dict_unref(varp->vval.v_dict);
7157 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007158 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007159#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007160 job_unref(varp->vval.v_job);
7161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007162#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007163 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007164#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007165 channel_unref(varp->vval.v_channel);
7166 break;
7167#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007168 case VAR_NUMBER:
7169 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007170 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007171 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007172 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 vim_free(varp);
7175 }
7176}
7177
7178/*
7179 * Free the memory for a variable value and set the value to NULL or 0.
7180 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
7184 if (varp != NULL)
7185 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007186 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007188 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007189 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007190 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007191 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007192 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007193 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007194 case VAR_PARTIAL:
7195 partial_unref(varp->vval.v_partial);
7196 varp->vval.v_partial = NULL;
7197 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007198 case VAR_BLOB:
7199 blob_unref(varp->vval.v_blob);
7200 varp->vval.v_blob = NULL;
7201 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007202 case VAR_LIST:
7203 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007205 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 case VAR_DICT:
7207 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007210 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007211 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007212 varp->vval.v_number = 0;
7213 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007214 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007215#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007216 varp->vval.v_float = 0.0;
7217 break;
7218#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007219 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007220#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007221 job_unref(varp->vval.v_job);
7222 varp->vval.v_job = NULL;
7223#endif
7224 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007225 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007226#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007227 channel_unref(varp->vval.v_channel);
7228 varp->vval.v_channel = NULL;
7229#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007230 case VAR_UNKNOWN:
7231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007233 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235}
7236
7237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007238 * Set the value of a variable to NULL without freeing items.
7239 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007241init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242{
7243 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007245}
7246
7247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 * Get the number value of a variable.
7249 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007250 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007251 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252 * caller of incompatible types: it sets *denote to TRUE if "denote"
7253 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007255 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007256tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 int error = FALSE;
7259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007260 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007261}
7262
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007263 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007264tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007266 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007268 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007270 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007271 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007272 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007273#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007274 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007275 break;
7276#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007277 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007278 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007279 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280 break;
7281 case VAR_STRING:
7282 if (varp->vval.v_string != NULL)
7283 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007284 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007285 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007286 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007287 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007288 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007289 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007290 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007291 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007292 case VAR_SPECIAL:
7293 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7294 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007295 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007297 EMSG(_("E910: Using a Job as a Number"));
7298 break;
7299#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007300 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007302 EMSG(_("E913: Using a Channel as a Number"));
7303 break;
7304#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007305 case VAR_BLOB:
7306 EMSG(_("E974: Using a Blob as a Number"));
7307 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007308 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007309 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 if (denote == NULL) /* useful for values that must be unsigned */
7313 n = -1;
7314 else
7315 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317}
7318
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007319#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007321tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007322{
7323 switch (varp->v_type)
7324 {
7325 case VAR_NUMBER:
7326 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007327 case VAR_FLOAT:
7328 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007329 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007330 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007331 EMSG(_("E891: Using a Funcref as a Float"));
7332 break;
7333 case VAR_STRING:
7334 EMSG(_("E892: Using a String as a Float"));
7335 break;
7336 case VAR_LIST:
7337 EMSG(_("E893: Using a List as a Float"));
7338 break;
7339 case VAR_DICT:
7340 EMSG(_("E894: Using a Dictionary as a Float"));
7341 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007342 case VAR_SPECIAL:
7343 EMSG(_("E907: Using a special value as a Float"));
7344 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007345 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007346# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007347 EMSG(_("E911: Using a Job as a Float"));
7348 break;
7349# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007350 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007351# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007352 EMSG(_("E914: Using a Channel as a Float"));
7353 break;
7354# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007355 case VAR_BLOB:
7356 EMSG(_("E975: Using a Blob as a Float"));
7357 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007358 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007360 break;
7361 }
7362 return 0;
7363}
7364#endif
7365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 * Get the string value of a variable.
7368 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007369 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7370 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 * If the String variable has never been set, return an empty string.
7372 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007373 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007374 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007376 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007377tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 static char_u mybuf[NUMBUFLEN];
7380
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007381 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382}
7383
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007384 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007387 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007388
7389 return res != NULL ? res : (char_u *)"";
7390}
7391
Bram Moolenaar7d647822014-04-05 21:28:56 +02007392/*
7393 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7394 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007395 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007396tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007397{
7398 static char_u mybuf[NUMBUFLEN];
7399
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007400 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007401}
7402
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007403 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007404tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007406 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007408 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007409 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007410 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 return buf;
7412 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007413 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007414 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 break;
7416 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007417 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418 break;
7419 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007420 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007421 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007422 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007423#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007424 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007425 break;
7426#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007427 case VAR_STRING:
7428 if (varp->vval.v_string != NULL)
7429 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007431 case VAR_SPECIAL:
7432 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7433 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007434 case VAR_BLOB:
7435 EMSG(_("E976: using Blob as a String"));
7436 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007437 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007438#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007439 {
7440 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007441 char *status;
7442
7443 if (job == NULL)
7444 return (char_u *)"no process";
7445 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007446 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007447 : "run";
7448# ifdef UNIX
7449 vim_snprintf((char *)buf, NUMBUFLEN,
7450 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007451# elif defined(WIN32)
7452 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007453 "process %ld %s",
7454 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007455 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007456# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007457 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007458 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7459# endif
7460 return buf;
7461 }
7462#endif
7463 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007464 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007465#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007466 {
7467 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007468 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007469
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007470 if (channel == NULL)
7471 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7472 else
7473 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007474 "channel %d %s", channel->ch_id, status);
7475 return buf;
7476 }
7477#endif
7478 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007479 case VAR_UNKNOWN:
7480 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007483 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
7485
7486/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007487 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7488 * string() on Dict, List, etc.
7489 */
7490 char_u *
7491tv_stringify(typval_T *varp, char_u *buf)
7492{
7493 if (varp->v_type == VAR_LIST
7494 || varp->v_type == VAR_DICT
7495 || varp->v_type == VAR_FUNC
7496 || varp->v_type == VAR_PARTIAL
7497 || varp->v_type == VAR_FLOAT)
7498 {
7499 typval_T tmp;
7500
7501 f_string(varp, &tmp);
7502 tv_get_string_buf(&tmp, buf);
7503 clear_tv(varp);
7504 *varp = tmp;
7505 return tmp.vval.v_string;
7506 }
7507 return tv_get_string_buf(varp, buf);
7508}
7509
7510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 * Find variable "name" in the list of variables.
7512 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007513 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007514 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007517 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007518find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007522 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523
Bram Moolenaara7043832005-01-21 11:56:39 +00007524 ht = find_var_ht(name, &varname);
7525 if (htp != NULL)
7526 *htp = ht;
7527 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007529 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7530 if (ret != NULL)
7531 return ret;
7532
7533 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007534 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535}
7536
7537/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007538 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007539 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542find_var_in_ht(
7543 hashtab_T *ht,
7544 int htname,
7545 char_u *varname,
7546 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007547{
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 hashitem_T *hi;
7549
7550 if (*varname == NUL)
7551 {
7552 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007553 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007555 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007556 case 'g': return &globvars_var;
7557 case 'v': return &vimvars_var;
7558 case 'b': return &curbuf->b_bufvar;
7559 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007560 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007561 case 'l': return get_funccal_local_var();
7562 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 }
7564 return NULL;
7565 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007566
7567 hi = hash_find(ht, varname);
7568 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007569 {
7570 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007571 * worked find the variable again. Don't auto-load a script if it was
7572 * loaded already, otherwise it would be loaded every time when
7573 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007574 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007575 {
7576 /* Note: script_autoload() may make "hi" invalid. It must either
7577 * be obtained again or not used. */
7578 if (!script_autoload(varname, FALSE) || aborting())
7579 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007580 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007581 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 if (HASHITEM_EMPTY(hi))
7583 return NULL;
7584 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007586}
7587
7588/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007590 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007591 * Set "varname" to the start of name without ':'.
7592 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007593 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007596 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007597 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007598
Bram Moolenaar73627d02015-08-11 15:46:09 +02007599 if (name[0] == NUL)
7600 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 if (name[1] != ':')
7602 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007603 /* The name must not start with a colon or #. */
7604 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 return NULL;
7606 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007607
7608 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007609 hi = hash_find(&compat_hashtab, name);
7610 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007611 return &compat_hashtab;
7612
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007613 ht = get_funccal_local_ht();
7614 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007616 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007619 if (*name == 'g') /* global variable */
7620 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007621 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7622 */
7623 if (vim_strchr(name + 2, ':') != NULL
7624 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007625 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007627 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007629 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007630 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007631 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 if (*name == 'v') /* v: variable */
7633 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007634 if (*name == 'a') /* a: function argument */
7635 return get_funccal_args_ht();
7636 if (*name == 'l') /* l: local function variable */
7637 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007639 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7640 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 return NULL;
7642}
7643
7644/*
7645 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007646 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 * Returns NULL when it doesn't exist.
7648 */
7649 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007650get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007654 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 if (v == NULL)
7656 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007657 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
7660/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 * sourcing this script and when executing functions defined in the script.
7663 */
7664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
Bram Moolenaara7043832005-01-21 11:56:39 +00007667 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 hashtab_T *ht;
7669 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7672 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007673 /* Re-allocating ga_data means that an ht_array pointing to
7674 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007676 for (i = 1; i <= ga_scripts.ga_len; ++i)
7677 {
7678 ht = &SCRIPT_VARS(i);
7679 if (ht->ht_mask == HT_INIT_SIZE - 1)
7680 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007681 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007683 }
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 while (ga_scripts.ga_len < id)
7686 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007687 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007688 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007689 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
7692 }
7693}
7694
7695/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7697 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 */
7699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007700init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007703 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007704 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007705 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007706 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 dict_var->di_tv.vval.v_dict = dict;
7708 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007709 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7711 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712}
7713
7714/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007715 * Unreference a dictionary initialized by init_var_dict().
7716 */
7717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007718unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007719{
7720 /* Now the dict needs to be freed if no one else is using it, go back to
7721 * normal reference counting. */
7722 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7723 dict_unref(dict);
7724}
7725
7726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 * Frees all allocated variables and the value they contain.
7729 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 */
7731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007732vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007733{
7734 vars_clear_ext(ht, TRUE);
7735}
7736
7737/*
7738 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7739 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007741vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
Bram Moolenaara7043832005-01-21 11:56:39 +00007743 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 hashitem_T *hi;
7745 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaar33570922005-01-25 22:26:29 +00007747 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007748 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007749 for (hi = ht->ht_array; todo > 0; ++hi)
7750 {
7751 if (!HASHITEM_EMPTY(hi))
7752 {
7753 --todo;
7754
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007756 * ht_array might change then. hash_clear() takes care of it
7757 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 v = HI2DI(hi);
7759 if (free_val)
7760 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007761 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007763 }
7764 }
7765 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007766 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767}
7768
Bram Moolenaara7043832005-01-21 11:56:39 +00007769/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 * Delete a variable from hashtab "ht" at item "hi".
7771 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007774delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007777
7778 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 clear_tv(&di->di_tv);
7780 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782
7783/*
7784 * List the value of one internal variable.
7785 */
7786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007787list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 char_u *tofree;
7790 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007793 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007795 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007800list_one_var_a(
7801 char_u *prefix,
7802 char_u *name,
7803 int type,
7804 char_u *string,
7805 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaar31859182007-08-14 20:41:13 +00007807 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7808 msg_start();
7809 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 if (name != NULL) /* "a:" vars don't have a name stored */
7811 msg_puts(name);
7812 msg_putchar(' ');
7813 msg_advance(22);
7814 if (type == VAR_NUMBER)
7815 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007816 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007817 msg_putchar('*');
7818 else if (type == VAR_LIST)
7819 {
7820 msg_putchar('[');
7821 if (*string == '[')
7822 ++string;
7823 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 else if (type == VAR_DICT)
7825 {
7826 msg_putchar('{');
7827 if (*string == '{')
7828 ++string;
7829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 else
7831 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007835 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007837 if (*first)
7838 {
7839 msg_clr_eos();
7840 *first = FALSE;
7841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842}
7843
7844/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 * If the variable already exists, the value is updated.
7847 * Otherwise the variable is created.
7848 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007850set_var(
7851 char_u *name,
7852 typval_T *tv,
7853 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007859 ht = find_var_ht(name, &varname);
7860 if (ht == NULL || *varname == NUL)
7861 {
7862 EMSG2(_(e_illvar), name);
7863 return;
7864 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007865 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007866
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007867 /* Search in parent scope which is possible to reference from lambda */
7868 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007869 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007870
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007871 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7872 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007873 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007878 if (var_check_ro(v->di_flags, name, FALSE)
7879 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007881
7882 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007883 * Handle setting internal v: variables separately where needed to
7884 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 */
7886 if (ht == &vimvarht)
7887 {
7888 if (v->di_tv.v_type == VAR_STRING)
7889 {
7890 vim_free(v->di_tv.vval.v_string);
7891 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007893 else
7894 {
7895 /* Take over the string to avoid an extra alloc/free. */
7896 v->di_tv.vval.v_string = tv->vval.v_string;
7897 tv->vval.v_string = NULL;
7898 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007899 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007901 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007903 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904 if (STRCMP(varname, "searchforward") == 0)
7905 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007906#ifdef FEAT_SEARCH_EXTRA
7907 else if (STRCMP(varname, "hlsearch") == 0)
7908 {
7909 no_hlsearch = !v->di_tv.vval.v_number;
7910 redraw_all_later(SOME_VALID);
7911 }
7912#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007913 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007915 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007916 {
Bram Moolenaar74ea88c2018-12-04 22:37:49 +01007917 EMSG2(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007918 return;
7919 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 }
7921
7922 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 }
7924 else /* add a new variable */
7925 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007926 /* Can't add "v:" variable. */
7927 if (ht == &vimvarht)
7928 {
7929 EMSG2(_(e_illvar), name);
7930 return;
7931 }
7932
Bram Moolenaar92124a32005-06-17 22:03:40 +00007933 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007934 if (!valid_varname(varname))
7935 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007936
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007937 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7938 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007939 if (v == NULL)
7940 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007944 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 return;
7946 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007947 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007949
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007950 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007952 else
7953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007955 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958}
7959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007960/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007961 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 * Also give an error message.
7963 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007965var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966{
7967 if (flags & DI_FLAGS_RO)
7968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007969 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 return TRUE;
7971 }
7972 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7973 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007974 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 return TRUE;
7976 }
7977 return FALSE;
7978}
7979
7980/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007981 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7982 * Also give an error message.
7983 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007985var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007986{
7987 if (flags & DI_FLAGS_FIX)
7988 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007989 EMSG2(_("E795: Cannot delete variable %s"),
7990 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007991 return TRUE;
7992 }
7993 return FALSE;
7994}
7995
7996/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007997 * Check if a funcref is assigned to a valid variable name.
7998 * Return TRUE and give an error if not.
7999 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001var_check_func_name(
8002 char_u *name, /* points to start of variable name */
8003 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008004{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008005 /* Allow for w: b: s: and t:. */
8006 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008007 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8008 ? name[2] : name[0]))
8009 {
8010 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
8011 name);
8012 return TRUE;
8013 }
8014 /* Don't allow hiding a function. When "v" is not NULL we might be
8015 * assigning another function to the same var, the type is checked
8016 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008017 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008018 {
8019 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
8020 name);
8021 return TRUE;
8022 }
8023 return FALSE;
8024}
8025
8026/*
8027 * Check if a variable name is valid.
8028 * Return FALSE and give an error if not.
8029 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008031valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008032{
8033 char_u *p;
8034
8035 for (p = varname; *p != NUL; ++p)
8036 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8037 && *p != AUTOLOAD_CHAR)
8038 {
8039 EMSG2(_(e_illvar), varname);
8040 return FALSE;
8041 }
8042 return TRUE;
8043}
8044
8045/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008046 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008047 * Also give an error message, using "name" or _("name") when use_gettext is
8048 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008049 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008051tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008052{
8053 if (lock & VAR_LOCKED)
8054 {
8055 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008056 name == NULL ? (char_u *)_("Unknown")
8057 : use_gettext ? (char_u *)_(name)
8058 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008059 return TRUE;
8060 }
8061 if (lock & VAR_FIXED)
8062 {
8063 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008064 name == NULL ? (char_u *)_("Unknown")
8065 : use_gettext ? (char_u *)_(name)
8066 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008067 return TRUE;
8068 }
8069 return FALSE;
8070}
8071
8072/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008075 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008076 * It is OK for "from" and "to" to point to the same item. This is used to
8077 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008078 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008080copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008082 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008083 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 switch (from->v_type)
8085 {
8086 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008087 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 to->vval.v_number = from->vval.v_number;
8089 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008090 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008091#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092 to->vval.v_float = from->vval.v_float;
8093 break;
8094#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008095 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008096#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008097 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008098 if (to->vval.v_job != NULL)
8099 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008100 break;
8101#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008102 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008103#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008104 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008105 if (to->vval.v_channel != NULL)
8106 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008107 break;
8108#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008109 case VAR_STRING:
8110 case VAR_FUNC:
8111 if (from->vval.v_string == NULL)
8112 to->vval.v_string = NULL;
8113 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008114 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008115 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008116 if (from->v_type == VAR_FUNC)
8117 func_ref(to->vval.v_string);
8118 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008120 case VAR_PARTIAL:
8121 if (from->vval.v_partial == NULL)
8122 to->vval.v_partial = NULL;
8123 else
8124 {
8125 to->vval.v_partial = from->vval.v_partial;
8126 ++to->vval.v_partial->pt_refcount;
8127 }
8128 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008129 case VAR_BLOB:
8130 if (from->vval.v_blob == NULL)
8131 to->vval.v_blob = NULL;
8132 else
8133 {
8134 to->vval.v_blob = from->vval.v_blob;
8135 ++to->vval.v_blob->bv_refcount;
8136 }
8137 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 case VAR_LIST:
8139 if (from->vval.v_list == NULL)
8140 to->vval.v_list = NULL;
8141 else
8142 {
8143 to->vval.v_list = from->vval.v_list;
8144 ++to->vval.v_list->lv_refcount;
8145 }
8146 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008147 case VAR_DICT:
8148 if (from->vval.v_dict == NULL)
8149 to->vval.v_dict = NULL;
8150 else
8151 {
8152 to->vval.v_dict = from->vval.v_dict;
8153 ++to->vval.v_dict->dv_refcount;
8154 }
8155 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008156 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008157 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 break;
8159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160}
8161
8162/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 * Make a copy of an item.
8164 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008165 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8166 * reference to an already copied list/dict can be used.
8167 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008169 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008170item_copy(
8171 typval_T *from,
8172 typval_T *to,
8173 int deep,
8174 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175{
8176 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008177 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008178
Bram Moolenaar33570922005-01-25 22:26:29 +00008179 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180 {
8181 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008182 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183 }
8184 ++recurse;
8185
8186 switch (from->v_type)
8187 {
8188 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008189 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190 case VAR_STRING:
8191 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008192 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008193 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008194 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008195 case VAR_CHANNEL:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008196 case VAR_BLOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 copy_tv(from, to);
8198 break;
8199 case VAR_LIST:
8200 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008201 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008202 if (from->vval.v_list == NULL)
8203 to->vval.v_list = NULL;
8204 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8205 {
8206 /* use the copy made earlier */
8207 to->vval.v_list = from->vval.v_list->lv_copylist;
8208 ++to->vval.v_list->lv_refcount;
8209 }
8210 else
8211 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8212 if (to->vval.v_list == NULL)
8213 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 break;
8215 case VAR_DICT:
8216 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008217 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008218 if (from->vval.v_dict == NULL)
8219 to->vval.v_dict = NULL;
8220 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8221 {
8222 /* use the copy made earlier */
8223 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8224 ++to->vval.v_dict->dv_refcount;
8225 }
8226 else
8227 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8228 if (to->vval.v_dict == NULL)
8229 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008230 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008231 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008232 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008233 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008234 }
8235 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008236 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008237}
8238
8239/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008240 * This function is used by f_input() and f_inputdialog() functions. The third
8241 * argument to f_input() specifies the type of completion to use at the
8242 * prompt. The third argument to f_inputdialog() specifies the value to return
8243 * when the user cancels the prompt.
8244 */
8245 void
8246get_user_input(
8247 typval_T *argvars,
8248 typval_T *rettv,
8249 int inputdialog,
8250 int secret)
8251{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008252 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 char_u *p = NULL;
8254 int c;
8255 char_u buf[NUMBUFLEN];
8256 int cmd_silent_save = cmd_silent;
8257 char_u *defstr = (char_u *)"";
8258 int xp_type = EXPAND_NOTHING;
8259 char_u *xp_arg = NULL;
8260
8261 rettv->v_type = VAR_STRING;
8262 rettv->vval.v_string = NULL;
8263
8264#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008265 /* While starting up, there is no place to enter text. When running tests
8266 * with --not-a-term we assume feedkeys() will be used. */
8267 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 return;
8269#endif
8270
8271 cmd_silent = FALSE; /* Want to see the prompt. */
8272 if (prompt != NULL)
8273 {
8274 /* Only the part of the message after the last NL is considered as
8275 * prompt for the command line */
8276 p = vim_strrchr(prompt, '\n');
8277 if (p == NULL)
8278 p = prompt;
8279 else
8280 {
8281 ++p;
8282 c = *p;
8283 *p = NUL;
8284 msg_start();
8285 msg_clr_eos();
8286 msg_puts_attr(prompt, echo_attr);
8287 msg_didout = FALSE;
8288 msg_starthere();
8289 *p = c;
8290 }
8291 cmdline_row = msg_row;
8292
8293 if (argvars[1].v_type != VAR_UNKNOWN)
8294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008295 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 if (defstr != NULL)
8297 stuffReadbuffSpec(defstr);
8298
8299 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8300 {
8301 char_u *xp_name;
8302 int xp_namelen;
8303 long argt;
8304
8305 /* input() with a third argument: completion */
8306 rettv->vval.v_string = NULL;
8307
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008308 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 if (xp_name == NULL)
8310 return;
8311
8312 xp_namelen = (int)STRLEN(xp_name);
8313
8314 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8315 &xp_arg) == FAIL)
8316 return;
8317 }
8318 }
8319
8320 if (defstr != NULL)
8321 {
8322 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 ex_normal_busy = 0;
8325 rettv->vval.v_string =
8326 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8327 xp_type, xp_arg);
8328 ex_normal_busy = save_ex_normal_busy;
8329 }
8330 if (inputdialog && rettv->vval.v_string == NULL
8331 && argvars[1].v_type != VAR_UNKNOWN
8332 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008333 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 &argvars[2], buf));
8335
8336 vim_free(xp_arg);
8337
8338 /* since the user typed this, no need to wait for return */
8339 need_wait_return = FALSE;
8340 msg_didout = FALSE;
8341 }
8342 cmd_silent = cmd_silent_save;
8343}
8344
8345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 * ":echo expr1 ..." print each argument separated with a space, add a
8347 * newline at the end.
8348 * ":echon expr1 ..." print each argument plain.
8349 */
8350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008351ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008355 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 char_u *p;
8357 int needclr = TRUE;
8358 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008359 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008360 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008361 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
8363 if (eap->skip)
8364 ++emsg_skip;
8365 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8366 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367 /* If eval1() causes an error message the text from the command may
8368 * still need to be cleared. E.g., "echo 22,44". */
8369 need_clr_eos = needclr;
8370
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
8374 /*
8375 * Report the invalid expression unless the expression evaluation
8376 * has been cancelled due to an aborting error, an interrupt, or an
8377 * exception.
8378 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008379 if (!aborting() && did_emsg == did_emsg_before
8380 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008382 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 break;
8384 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008385 need_clr_eos = FALSE;
8386
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 if (!eap->skip)
8388 {
8389 if (atstart)
8390 {
8391 atstart = FALSE;
8392 /* Call msg_start() after eval1(), evaluating the expression
8393 * may cause a message to appear. */
8394 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008395 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008396 /* Mark the saved text as finishing the line, so that what
8397 * follows is displayed on a new line when scrolling back
8398 * at the more prompt. */
8399 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 }
8403 else if (eap->cmdidx == CMD_echo)
8404 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008405 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008406 if (p != NULL)
8407 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008409 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008411 if (*p != TAB && needclr)
8412 {
8413 /* remove any text still there from the command */
8414 msg_clr_eos();
8415 needclr = FALSE;
8416 }
8417 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008420 {
8421#ifdef FEAT_MBYTE
8422 if (has_mbyte)
8423 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008424 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008425
8426 (void)msg_outtrans_len_attr(p, i, echo_attr);
8427 p += i - 1;
8428 }
8429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008431 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008436 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 arg = skipwhite(arg);
8438 }
8439 eap->nextcmd = check_nextcmd(arg);
8440
8441 if (eap->skip)
8442 --emsg_skip;
8443 else
8444 {
8445 /* remove text that may still be there from the command */
8446 if (needclr)
8447 msg_clr_eos();
8448 if (eap->cmdidx == CMD_echo)
8449 msg_end();
8450 }
8451}
8452
8453/*
8454 * ":echohl {name}".
8455 */
8456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008457ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008459 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460}
8461
8462/*
8463 * ":execute expr1 ..." execute the result of an expression.
8464 * ":echomsg expr1 ..." Print a message
8465 * ":echoerr expr1 ..." Print an error
8466 * Each gets spaces around each argument and a newline at the end for
8467 * echo commands
8468 */
8469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008470ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008473 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 int ret = OK;
8475 char_u *p;
8476 garray_T ga;
8477 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008478 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479
8480 ga_init2(&ga, 1, 80);
8481
8482 if (eap->skip)
8483 ++emsg_skip;
8484 while (*arg != NUL && *arg != '|' && *arg != '\n')
8485 {
8486 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008487 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {
8489 /*
8490 * Report the invalid expression unless the expression evaluation
8491 * has been cancelled due to an aborting error, an interrupt, or an
8492 * exception.
8493 */
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008494 if (!aborting() && did_emsg == save_did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 EMSG2(_(e_invexpr2), p);
8496 ret = FAIL;
8497 break;
8498 }
8499
8500 if (!eap->skip)
8501 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008502 char_u buf[NUMBUFLEN];
8503
8504 if (eap->cmdidx == CMD_execute)
8505 p = tv_get_string_buf(&rettv, buf);
8506 else
8507 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 len = (int)STRLEN(p);
8509 if (ga_grow(&ga, len + 2) == FAIL)
8510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 ret = FAIL;
8513 break;
8514 }
8515 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 ga.ga_len += len;
8519 }
8520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 arg = skipwhite(arg);
8523 }
8524
8525 if (ret != FAIL && ga.ga_data != NULL)
8526 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008527 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8528 {
8529 /* Mark the already saved text as finishing the line, so that what
8530 * follows is displayed on a new line when scrolling back at the
8531 * more prompt. */
8532 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008533 }
8534
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008538 out_flush();
8539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 else if (eap->cmdidx == CMD_echoerr)
8541 {
8542 /* We don't want to abort following commands, restore did_emsg. */
8543 save_did_emsg = did_emsg;
8544 EMSG((char_u *)ga.ga_data);
8545 if (!force_abort)
8546 did_emsg = save_did_emsg;
8547 }
8548 else if (eap->cmdidx == CMD_execute)
8549 do_cmdline((char_u *)ga.ga_data,
8550 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8551 }
8552
8553 ga_clear(&ga);
8554
8555 if (eap->skip)
8556 --emsg_skip;
8557
8558 eap->nextcmd = check_nextcmd(arg);
8559}
8560
8561/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 * Find window specified by "vp" in tabpage "tp".
8563 */
8564 win_T *
8565find_win_by_nr(
8566 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008567 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008570 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 if (nr < 0)
8573 return NULL;
8574 if (nr == 0)
8575 return curwin;
8576
Bram Moolenaar29323592016-07-24 22:04:11 +02008577 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8578 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 if (nr >= LOWEST_WIN_ID)
8580 {
8581 if (wp->w_id == nr)
8582 return wp;
8583 }
8584 else if (--nr <= 0)
8585 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008586 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 if (nr >= LOWEST_WIN_ID)
8588 return NULL;
8589 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590}
8591
8592/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008593 * Find a window: When using a Window ID in any tab page, when using a number
8594 * in the current tab page.
8595 */
8596 win_T *
8597find_win_by_nr_or_id(typval_T *vp)
8598{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008599 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008600
8601 if (nr >= LOWEST_WIN_ID)
8602 return win_id2wp(vp);
8603 return find_win_by_nr(vp, NULL);
8604}
8605
8606/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 * Find window specified by "wvp" in tabpage "tvp".
8608 */
8609 win_T *
8610find_tabwin(
8611 typval_T *wvp, /* VAR_UNKNOWN for current window */
8612 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8613{
8614 win_T *wp = NULL;
8615 tabpage_T *tp = NULL;
8616 long n;
8617
8618 if (wvp->v_type != VAR_UNKNOWN)
8619 {
8620 if (tvp->v_type != VAR_UNKNOWN)
8621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008622 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 if (n >= 0)
8624 tp = find_tabpage(n);
8625 }
8626 else
8627 tp = curtab;
8628
8629 if (tp != NULL)
8630 wp = find_win_by_nr(wvp, tp);
8631 }
8632 else
8633 wp = curwin;
8634
8635 return wp;
8636}
8637
8638/*
8639 * getwinvar() and gettabwinvar()
8640 */
8641 void
8642getwinvar(
8643 typval_T *argvars,
8644 typval_T *rettv,
8645 int off) /* 1 for gettabwinvar() */
8646{
8647 win_T *win;
8648 char_u *varname;
8649 dictitem_T *v;
8650 tabpage_T *tp = NULL;
8651 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 win_T *oldcurwin;
8653 tabpage_T *oldtabpage;
8654 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008657 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 else
8659 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 ++emsg_off;
8663
8664 rettv->v_type = VAR_STRING;
8665 rettv->vval.v_string = NULL;
8666
8667 if (win != NULL && varname != NULL)
8668 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 /* Set curwin to be our win, temporarily. Also set the tabpage,
8670 * otherwise the window is not valid. Only do this when needed,
8671 * autocommands get blocked. */
8672 need_switch_win = !(tp == curtab && win == curwin);
8673 if (!need_switch_win
8674 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008676 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008678 if (varname[1] == NUL)
8679 {
8680 /* get all window-local options in a dict */
8681 dict_T *opts = get_winbuf_options(FALSE);
8682
8683 if (opts != NULL)
8684 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008685 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008686 done = TRUE;
8687 }
8688 }
8689 else if (get_option_tv(&varname, rettv, 1) == OK)
8690 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691 done = TRUE;
8692 }
8693 else
8694 {
8695 /* Look up the variable. */
8696 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8697 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8698 varname, FALSE);
8699 if (v != NULL)
8700 {
8701 copy_tv(&v->di_tv, rettv);
8702 done = TRUE;
8703 }
8704 }
8705 }
8706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707 if (need_switch_win)
8708 /* restore previous notion of curwin */
8709 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 }
8711
8712 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8713 /* use the default return value */
8714 copy_tv(&argvars[off + 2], rettv);
8715
8716 --emsg_off;
8717}
8718
8719/*
8720 * "setwinvar()" and "settabwinvar()" functions
8721 */
8722 void
8723setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8724{
8725 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 win_T *save_curwin;
8727 tabpage_T *save_curtab;
8728 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 char_u *varname, *winvarname;
8730 typval_T *varp;
8731 char_u nbuf[NUMBUFLEN];
8732 tabpage_T *tp = NULL;
8733
8734 if (check_restricted() || check_secure())
8735 return;
8736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008738 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 else
8740 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008742 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 varp = &argvars[off + 2];
8744
8745 if (win != NULL && varname != NULL && varp != NULL)
8746 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 need_switch_win = !(tp == curtab && win == curwin);
8748 if (!need_switch_win
8749 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 {
8751 if (*varname == '&')
8752 {
8753 long numval;
8754 char_u *strval;
8755 int error = FALSE;
8756
8757 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008758 numval = (long)tv_get_number_chk(varp, &error);
8759 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 if (!error && strval != NULL)
8761 set_option_value(varname, numval, strval, OPT_LOCAL);
8762 }
8763 else
8764 {
8765 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8766 if (winvarname != NULL)
8767 {
8768 STRCPY(winvarname, "w:");
8769 STRCPY(winvarname + 2, varname);
8770 set_var(winvarname, varp, TRUE);
8771 vim_free(winvarname);
8772 }
8773 }
8774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775 if (need_switch_win)
8776 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777 }
8778}
8779
8780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8782 * "arg" points to the "&" or '+' when called, to "option" when returning.
8783 * Returns NULL when no option name found. Otherwise pointer to the char
8784 * after the option name.
8785 */
8786 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008787find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 char_u *p = *arg;
8790
8791 ++p;
8792 if (*p == 'g' && p[1] == ':')
8793 {
8794 *opt_flags = OPT_GLOBAL;
8795 p += 2;
8796 }
8797 else if (*p == 'l' && p[1] == ':')
8798 {
8799 *opt_flags = OPT_LOCAL;
8800 p += 2;
8801 }
8802 else
8803 *opt_flags = 0;
8804
8805 if (!ASCII_ISALPHA(*p))
8806 return NULL;
8807 *arg = p;
8808
8809 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8810 p += 4; /* termcap option */
8811 else
8812 while (ASCII_ISALPHA(*p))
8813 ++p;
8814 return p;
8815}
8816
8817/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008818 * Return the autoload script name for a function or variable name.
8819 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008821 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008822autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008823{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008824 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008825 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008826
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008827 /* Get the script file name: replace '#' with '/', append ".vim". */
8828 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8829 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008830 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008831 STRCPY(scriptname, "autoload/");
8832 STRCAT(scriptname, name);
8833 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8834 STRCAT(scriptname, ".vim");
8835 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8836 *p = '/';
8837 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838}
8839
8840/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008841 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008842 * Return TRUE if a package was loaded.
8843 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008845script_autoload(
8846 char_u *name,
8847 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848{
8849 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008850 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008851 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008852 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008853
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008854 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008855 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008856 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008857 return FALSE;
8858
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008859 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008860
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008861 /* Find the name in the list of previously loaded package names. Skip
8862 * "autoload/", it's always the same. */
8863 for (i = 0; i < ga_loaded.ga_len; ++i)
8864 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8865 break;
8866 if (!reload && i < ga_loaded.ga_len)
8867 ret = FALSE; /* was loaded already */
8868 else
8869 {
8870 /* Remember the name if it wasn't loaded already. */
8871 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8872 {
8873 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8874 tofree = NULL;
8875 }
8876
8877 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008878 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008879 ret = TRUE;
8880 }
8881
8882 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008883 return ret;
8884}
8885
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8887typedef enum
8888{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008889 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8890 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8891 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892} var_flavour_T;
8893
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008895var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 char_u *p = varname;
8898
8899 if (ASCII_ISUPPER(*p))
8900 {
8901 while (*(++p))
8902 if (ASCII_ISLOWER(*p))
8903 return VAR_FLAVOUR_SESSION;
8904 return VAR_FLAVOUR_VIMINFO;
8905 }
8906 else
8907 return VAR_FLAVOUR_DEFAULT;
8908}
8909#endif
8910
8911#if defined(FEAT_VIMINFO) || defined(PROTO)
8912/*
8913 * Restore global vars that start with a capital from the viminfo file
8914 */
8915 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008919 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008921 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
8923 if (!writing && (find_viminfo_parameter('!') != NULL))
8924 {
8925 tab = vim_strchr(virp->vir_line + 1, '\t');
8926 if (tab != NULL)
8927 {
8928 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008929 switch (*tab)
8930 {
8931 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008932#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008933 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008934#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008935 case 'D': type = VAR_DICT; break;
8936 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008937 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008938 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
8941 tab = vim_strchr(tab, '\t');
8942 if (tab != NULL)
8943 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944 tv.v_type = type;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008945 if (type == VAR_STRING || type == VAR_DICT ||
8946 type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008947 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008949#ifdef FEAT_FLOAT
8950 else if (type == VAR_FLOAT)
8951 (void)string2float(tab + 1, &tv.vval.v_float);
8952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008954 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008955 if (type == VAR_DICT || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008956 {
8957 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8958
8959 if (etv == NULL)
8960 /* Failed to parse back the dict or list, use it as a
8961 * string. */
8962 tv.v_type = VAR_STRING;
8963 else
8964 {
8965 vim_free(tv.vval.v_string);
8966 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008967 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008968 }
8969 }
8970
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008971 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008972 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008973 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008974 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008975
8976 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008977 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008978 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8979 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008980 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 }
8982 }
8983 }
8984
8985 return viminfo_readline(virp);
8986}
8987
8988/*
8989 * Write global vars that start with a capital to the viminfo file
8990 */
8991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008992write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 hashitem_T *hi;
8995 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008996 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008997 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008998 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008999 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009000 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001
9002 if (find_viminfo_parameter('!') == NULL)
9003 return;
9004
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009005 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009006
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009007 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009010 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009012 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 this_var = HI2DI(hi);
9014 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009016 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009017 {
9018 case VAR_STRING: s = "STR"; break;
9019 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009020 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009021 case VAR_DICT: s = "DIC"; break;
9022 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009023 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009024 case VAR_SPECIAL: s = "XPL"; break;
9025
9026 case VAR_UNKNOWN:
9027 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009028 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009029 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009030 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009031 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009034 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009035 if (p != NULL)
9036 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009037 vim_free(tofree);
9038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 }
9040 }
9041}
9042#endif
9043
9044#if defined(FEAT_SESSION) || defined(PROTO)
9045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 hashitem_T *hi;
9049 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009050 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 char_u *p, *t;
9052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009053 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009056 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009058 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 this_var = HI2DI(hi);
9060 if ((this_var->di_tv.v_type == VAR_NUMBER
9061 || this_var->di_tv.v_type == VAR_STRING)
9062 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009063 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009064 /* Escape special characters with a backslash. Turn a LF and
9065 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009066 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009067 (char_u *)"\\\"\n\r");
9068 if (p == NULL) /* out of memory */
9069 break;
9070 for (t = p; *t != NUL; ++t)
9071 if (*t == '\n')
9072 *t = 'n';
9073 else if (*t == '\r')
9074 *t = 'r';
9075 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 this_var->di_key,
9077 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9078 : ' ',
9079 p,
9080 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9081 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009082 || put_eol(fd) == FAIL)
9083 {
9084 vim_free(p);
9085 return FAIL;
9086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 vim_free(p);
9088 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009089#ifdef FEAT_FLOAT
9090 else if (this_var->di_tv.v_type == VAR_FLOAT
9091 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9092 {
9093 float_T f = this_var->di_tv.vval.v_float;
9094 int sign = ' ';
9095
9096 if (f < 0)
9097 {
9098 f = -f;
9099 sign = '-';
9100 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009101 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009102 this_var->di_key, sign, f) < 0)
9103 || put_eol(fd) == FAIL)
9104 return FAIL;
9105 }
9106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 }
9108 }
9109 return OK;
9110}
9111#endif
9112
Bram Moolenaar661b1822005-07-28 22:36:45 +00009113/*
9114 * Display script name where an item was last set.
9115 * Should only be invoked when 'verbose' is non-zero.
9116 */
9117 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009118last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009119{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009120 char_u *p;
9121
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009122 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009123 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009124 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009125 if (p != NULL)
9126 {
9127 verbose_enter();
9128 MSG_PUTS(_("\n\tLast set from "));
9129 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009130 if (script_ctx.sc_lnum > 0)
9131 {
9132 MSG_PUTS(_(" line "));
9133 msg_outnum((long)script_ctx.sc_lnum);
9134 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009135 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009136 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009137 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009138 }
9139}
9140
Bram Moolenaar53744302015-07-17 17:38:22 +02009141/* reset v:option_new, v:option_old and v:option_type */
9142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009143reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009144{
9145 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9146 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9147 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9148}
9149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009150/*
9151 * Prepare "gap" for an assert error and add the sourcing position.
9152 */
9153 void
9154prepare_assert_error(garray_T *gap)
9155{
9156 char buf[NUMBUFLEN];
9157
9158 ga_init2(gap, 1, 100);
9159 if (sourcing_name != NULL)
9160 {
9161 ga_concat(gap, sourcing_name);
9162 if (sourcing_lnum > 0)
9163 ga_concat(gap, (char_u *)" ");
9164 }
9165 if (sourcing_lnum > 0)
9166 {
9167 sprintf(buf, "line %ld", (long)sourcing_lnum);
9168 ga_concat(gap, (char_u *)buf);
9169 }
9170 if (sourcing_name != NULL || sourcing_lnum > 0)
9171 ga_concat(gap, (char_u *)": ");
9172}
9173
9174/*
9175 * Add an assert error to v:errors.
9176 */
9177 void
9178assert_error(garray_T *gap)
9179{
9180 struct vimvar *vp = &vimvars[VV_ERRORS];
9181
9182 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9183 /* Make sure v:errors is a list. */
9184 set_vim_var_list(VV_ERRORS, list_alloc());
9185 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9186}
9187
Bram Moolenaar65a54642018-04-28 16:56:53 +02009188 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189assert_equal_common(typval_T *argvars, assert_type_T atype)
9190{
9191 garray_T ga;
9192
9193 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9194 != (atype == ASSERT_EQUAL))
9195 {
9196 prepare_assert_error(&ga);
9197 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9198 atype);
9199 assert_error(&ga);
9200 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009201 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009203 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204}
9205
Bram Moolenaar65a54642018-04-28 16:56:53 +02009206 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009207assert_equalfile(typval_T *argvars)
9208{
9209 char_u buf1[NUMBUFLEN];
9210 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009211 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9212 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009213 garray_T ga;
9214 FILE *fd1;
9215 FILE *fd2;
9216
9217 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009218 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009219
9220 IObuff[0] = NUL;
9221 fd1 = mch_fopen((char *)fname1, READBIN);
9222 if (fd1 == NULL)
9223 {
9224 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9225 }
9226 else
9227 {
9228 fd2 = mch_fopen((char *)fname2, READBIN);
9229 if (fd2 == NULL)
9230 {
9231 fclose(fd1);
9232 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9233 }
9234 else
9235 {
9236 int c1, c2;
9237 long count = 0;
9238
9239 for (;;)
9240 {
9241 c1 = fgetc(fd1);
9242 c2 = fgetc(fd2);
9243 if (c1 == EOF)
9244 {
9245 if (c2 != EOF)
9246 STRCPY(IObuff, "first file is shorter");
9247 break;
9248 }
9249 else if (c2 == EOF)
9250 {
9251 STRCPY(IObuff, "second file is shorter");
9252 break;
9253 }
9254 else if (c1 != c2)
9255 {
9256 vim_snprintf((char *)IObuff, IOSIZE,
9257 "difference at byte %ld", count);
9258 break;
9259 }
9260 ++count;
9261 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009262 fclose(fd1);
9263 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009264 }
9265 }
9266 if (IObuff[0] != NUL)
9267 {
9268 prepare_assert_error(&ga);
9269 ga_concat(&ga, IObuff);
9270 assert_error(&ga);
9271 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009272 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009273 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009274 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009275}
9276
Bram Moolenaar65a54642018-04-28 16:56:53 +02009277 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278assert_match_common(typval_T *argvars, assert_type_T atype)
9279{
9280 garray_T ga;
9281 char_u buf1[NUMBUFLEN];
9282 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009283 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9284 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285
9286 if (pat == NULL || text == NULL)
9287 EMSG(_(e_invarg));
9288 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9289 {
9290 prepare_assert_error(&ga);
9291 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9292 atype);
9293 assert_error(&ga);
9294 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009295 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009296 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009297 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298}
9299
Bram Moolenaar65a54642018-04-28 16:56:53 +02009300 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009301assert_inrange(typval_T *argvars)
9302{
9303 garray_T ga;
9304 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009305 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9306 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9307 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009308 char_u *tofree;
9309 char msg[200];
9310 char_u numbuf[NUMBUFLEN];
9311
9312 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009313 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009314 if (actual < lower || actual > upper)
9315 {
9316 prepare_assert_error(&ga);
9317 if (argvars[3].v_type != VAR_UNKNOWN)
9318 {
9319 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9320 vim_free(tofree);
9321 }
9322 else
9323 {
9324 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9325 (long)lower, (long)upper, (long)actual);
9326 ga_concat(&ga, (char_u *)msg);
9327 }
9328 assert_error(&ga);
9329 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009330 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009331 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009332 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009333}
9334
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335/*
9336 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009337 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009339 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340assert_bool(typval_T *argvars, int isTrue)
9341{
9342 int error = FALSE;
9343 garray_T ga;
9344
9345 if (argvars[0].v_type == VAR_SPECIAL
9346 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009347 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009349 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 || error)
9351 {
9352 prepare_assert_error(&ga);
9353 fill_assert_error(&ga, &argvars[1],
9354 (char_u *)(isTrue ? "True" : "False"),
9355 NULL, &argvars[0], ASSERT_OTHER);
9356 assert_error(&ga);
9357 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009358 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009360 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361}
9362
Bram Moolenaar65a54642018-04-28 16:56:53 +02009363 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009364assert_report(typval_T *argvars)
9365{
9366 garray_T ga;
9367
9368 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009369 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009370 assert_error(&ga);
9371 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009372 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009373}
9374
Bram Moolenaar65a54642018-04-28 16:56:53 +02009375 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376assert_exception(typval_T *argvars)
9377{
9378 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009379 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380
9381 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9382 {
9383 prepare_assert_error(&ga);
9384 ga_concat(&ga, (char_u *)"v:exception is not set");
9385 assert_error(&ga);
9386 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009387 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 }
9389 else if (error != NULL
9390 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9391 {
9392 prepare_assert_error(&ga);
9393 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9394 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9395 assert_error(&ga);
9396 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009397 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009398 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009399 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400}
9401
Bram Moolenaar65a54642018-04-28 16:56:53 +02009402 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009403assert_beeps(typval_T *argvars)
9404{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009405 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009406 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009407 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009408
9409 called_vim_beep = FALSE;
9410 suppress_errthrow = TRUE;
9411 emsg_silent = FALSE;
9412 do_cmdline_cmd(cmd);
9413 if (!called_vim_beep)
9414 {
9415 prepare_assert_error(&ga);
9416 ga_concat(&ga, (char_u *)"command did not beep: ");
9417 ga_concat(&ga, cmd);
9418 assert_error(&ga);
9419 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009420 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009421 }
9422
9423 suppress_errthrow = FALSE;
9424 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009425 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009426}
9427
Bram Moolenaar65a54642018-04-28 16:56:53 +02009428 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009429assert_fails(typval_T *argvars)
9430{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009431 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009433 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009434 char_u numbuf[NUMBUFLEN];
9435 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436
9437 called_emsg = FALSE;
9438 suppress_errthrow = TRUE;
9439 emsg_silent = TRUE;
9440 do_cmdline_cmd(cmd);
9441 if (!called_emsg)
9442 {
9443 prepare_assert_error(&ga);
9444 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009445 if (argvars[1].v_type != VAR_UNKNOWN
9446 && argvars[2].v_type != VAR_UNKNOWN)
9447 {
9448 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9449 vim_free(tofree);
9450 }
9451 else
9452 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 assert_error(&ga);
9454 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009455 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456 }
9457 else if (argvars[1].v_type != VAR_UNKNOWN)
9458 {
9459 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009460 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461
9462 if (error == NULL
9463 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9464 {
9465 prepare_assert_error(&ga);
9466 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9467 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9468 assert_error(&ga);
9469 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009470 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 }
9472 }
9473
9474 called_emsg = FALSE;
9475 suppress_errthrow = FALSE;
9476 emsg_silent = FALSE;
9477 emsg_on_display = FALSE;
9478 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009479 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480}
9481
9482/*
9483 * Append "str" to "gap", escaping unprintable characters.
9484 * Changes NL to \n, CR to \r, etc.
9485 */
9486 static void
9487ga_concat_esc(garray_T *gap, char_u *str)
9488{
9489 char_u *p;
9490 char_u buf[NUMBUFLEN];
9491
9492 if (str == NULL)
9493 {
9494 ga_concat(gap, (char_u *)"NULL");
9495 return;
9496 }
9497
9498 for (p = str; *p != NUL; ++p)
9499 switch (*p)
9500 {
9501 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9502 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9503 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9504 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9505 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9506 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9507 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9508 default:
9509 if (*p < ' ')
9510 {
9511 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9512 ga_concat(gap, buf);
9513 }
9514 else
9515 ga_append(gap, *p);
9516 break;
9517 }
9518}
9519
9520/*
9521 * Fill "gap" with information about an assert error.
9522 */
9523 void
9524fill_assert_error(
9525 garray_T *gap,
9526 typval_T *opt_msg_tv,
9527 char_u *exp_str,
9528 typval_T *exp_tv,
9529 typval_T *got_tv,
9530 assert_type_T atype)
9531{
9532 char_u numbuf[NUMBUFLEN];
9533 char_u *tofree;
9534
9535 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9536 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009537 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9538 vim_free(tofree);
9539 ga_concat(gap, (char_u *)": ");
9540 }
9541
9542 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9543 ga_concat(gap, (char_u *)"Pattern ");
9544 else if (atype == ASSERT_NOTEQUAL)
9545 ga_concat(gap, (char_u *)"Expected not equal to ");
9546 else
9547 ga_concat(gap, (char_u *)"Expected ");
9548 if (exp_str == NULL)
9549 {
9550 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 vim_free(tofree);
9552 }
9553 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009554 ga_concat_esc(gap, exp_str);
9555 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009557 if (atype == ASSERT_MATCH)
9558 ga_concat(gap, (char_u *)" does not match ");
9559 else if (atype == ASSERT_NOTMATCH)
9560 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009562 ga_concat(gap, (char_u *)" but got ");
9563 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9564 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 }
9566}
9567
Bram Moolenaar31988702018-02-13 12:57:42 +01009568/*
9569 * Compare "typ1" and "typ2". Put the result in "typ1".
9570 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009571 int
9572typval_compare(
9573 typval_T *typ1, /* first operand */
9574 typval_T *typ2, /* second operand */
9575 exptype_T type, /* operator */
9576 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009577 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009578{
9579 int i;
9580 varnumber_T n1, n2;
9581 char_u *s1, *s2;
9582 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9583
Bram Moolenaar31988702018-02-13 12:57:42 +01009584 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009585 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009586 /* For "is" a different type always means FALSE, for "notis"
9587 * it means TRUE. */
9588 n1 = (type == TYPE_NEQUAL);
9589 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009590 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9591 {
9592 if (type_is)
9593 {
9594 n1 = (typ1->v_type == typ2->v_type
9595 && typ1->vval.v_blob == typ2->vval.v_blob);
9596 if (type == TYPE_NEQUAL)
9597 n1 = !n1;
9598 }
9599 else if (typ1->v_type != typ2->v_type
9600 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9601 {
9602 if (typ1->v_type != typ2->v_type)
9603 EMSG(_("E977: Can only compare Blob with Blob"));
9604 else
9605 EMSG(_(e_invalblob));
9606 clear_tv(typ1);
9607 return FAIL;
9608 }
9609 else
9610 {
9611 // Compare two Blobs for being equal or unequal.
9612 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9613 if (type == TYPE_NEQUAL)
9614 n1 = !n1;
9615 }
9616 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009617 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9618 {
9619 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009620 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009621 n1 = (typ1->v_type == typ2->v_type
9622 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009623 if (type == TYPE_NEQUAL)
9624 n1 = !n1;
9625 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009626 else if (typ1->v_type != typ2->v_type
9627 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009628 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009629 if (typ1->v_type != typ2->v_type)
9630 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009631 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009632 EMSG(_("E692: Invalid operation for List"));
9633 clear_tv(typ1);
9634 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009635 }
9636 else
9637 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009638 /* Compare two Lists for being equal or unequal. */
9639 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9640 ic, FALSE);
9641 if (type == TYPE_NEQUAL)
9642 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009643 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009644 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009645
9646 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9647 {
9648 if (type_is)
9649 {
9650 n1 = (typ1->v_type == typ2->v_type
9651 && typ1->vval.v_dict == typ2->vval.v_dict);
9652 if (type == TYPE_NEQUAL)
9653 n1 = !n1;
9654 }
9655 else if (typ1->v_type != typ2->v_type
9656 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9657 {
9658 if (typ1->v_type != typ2->v_type)
9659 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9660 else
9661 EMSG(_("E736: Invalid operation for Dictionary"));
9662 clear_tv(typ1);
9663 return FAIL;
9664 }
9665 else
9666 {
9667 /* Compare two Dictionaries for being equal or unequal. */
9668 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9669 ic, FALSE);
9670 if (type == TYPE_NEQUAL)
9671 n1 = !n1;
9672 }
9673 }
9674
9675 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9676 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9677 {
9678 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9679 {
9680 EMSG(_("E694: Invalid operation for Funcrefs"));
9681 clear_tv(typ1);
9682 return FAIL;
9683 }
9684 if ((typ1->v_type == VAR_PARTIAL
9685 && typ1->vval.v_partial == NULL)
9686 || (typ2->v_type == VAR_PARTIAL
9687 && typ2->vval.v_partial == NULL))
9688 /* when a partial is NULL assume not equal */
9689 n1 = FALSE;
9690 else if (type_is)
9691 {
9692 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9693 /* strings are considered the same if their value is
9694 * the same */
9695 n1 = tv_equal(typ1, typ2, ic, FALSE);
9696 else if (typ1->v_type == VAR_PARTIAL
9697 && typ2->v_type == VAR_PARTIAL)
9698 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9699 else
9700 n1 = FALSE;
9701 }
9702 else
9703 n1 = tv_equal(typ1, typ2, ic, FALSE);
9704 if (type == TYPE_NEQUAL)
9705 n1 = !n1;
9706 }
9707
9708#ifdef FEAT_FLOAT
9709 /*
9710 * If one of the two variables is a float, compare as a float.
9711 * When using "=~" or "!~", always compare as string.
9712 */
9713 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9714 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9715 {
9716 float_T f1, f2;
9717
9718 if (typ1->v_type == VAR_FLOAT)
9719 f1 = typ1->vval.v_float;
9720 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009721 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009722 if (typ2->v_type == VAR_FLOAT)
9723 f2 = typ2->vval.v_float;
9724 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009725 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009726 n1 = FALSE;
9727 switch (type)
9728 {
9729 case TYPE_EQUAL: n1 = (f1 == f2); break;
9730 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9731 case TYPE_GREATER: n1 = (f1 > f2); break;
9732 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9733 case TYPE_SMALLER: n1 = (f1 < f2); break;
9734 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9735 case TYPE_UNKNOWN:
9736 case TYPE_MATCH:
9737 case TYPE_NOMATCH: break; /* avoid gcc warning */
9738 }
9739 }
9740#endif
9741
9742 /*
9743 * If one of the two variables is a number, compare as a number.
9744 * When using "=~" or "!~", always compare as string.
9745 */
9746 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9747 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009749 n1 = tv_get_number(typ1);
9750 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009751 switch (type)
9752 {
9753 case TYPE_EQUAL: n1 = (n1 == n2); break;
9754 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9755 case TYPE_GREATER: n1 = (n1 > n2); break;
9756 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9757 case TYPE_SMALLER: n1 = (n1 < n2); break;
9758 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9759 case TYPE_UNKNOWN:
9760 case TYPE_MATCH:
9761 case TYPE_NOMATCH: break; /* avoid gcc warning */
9762 }
9763 }
9764 else
9765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009766 s1 = tv_get_string_buf(typ1, buf1);
9767 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009768 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9769 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9770 else
9771 i = 0;
9772 n1 = FALSE;
9773 switch (type)
9774 {
9775 case TYPE_EQUAL: n1 = (i == 0); break;
9776 case TYPE_NEQUAL: n1 = (i != 0); break;
9777 case TYPE_GREATER: n1 = (i > 0); break;
9778 case TYPE_GEQUAL: n1 = (i >= 0); break;
9779 case TYPE_SMALLER: n1 = (i < 0); break;
9780 case TYPE_SEQUAL: n1 = (i <= 0); break;
9781
9782 case TYPE_MATCH:
9783 case TYPE_NOMATCH:
9784 n1 = pattern_match(s2, s1, ic);
9785 if (type == TYPE_NOMATCH)
9786 n1 = !n1;
9787 break;
9788
9789 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9790 }
9791 }
9792 clear_tv(typ1);
9793 typ1->v_type = VAR_NUMBER;
9794 typ1->vval.v_number = n1;
9795
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009796 return OK;
9797}
9798
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009799 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009800typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009801{
9802 char_u *tofree;
9803 char_u numbuf[NUMBUFLEN];
9804 char_u *ret = NULL;
9805
9806 if (arg == NULL)
9807 return vim_strsave((char_u *)"(does not exist)");
9808 ret = tv2string(arg, &tofree, numbuf, 0);
9809 /* Make a copy if we have a value but it's not in allocated memory. */
9810 if (ret != NULL && tofree == NULL)
9811 ret = vim_strsave(ret);
9812 return ret;
9813}
9814
9815 int
9816var_exists(char_u *var)
9817{
9818 char_u *name;
9819 char_u *tofree;
9820 typval_T tv;
9821 int len = 0;
9822 int n = FALSE;
9823
9824 /* get_name_len() takes care of expanding curly braces */
9825 name = var;
9826 len = get_name_len(&var, &tofree, TRUE, FALSE);
9827 if (len > 0)
9828 {
9829 if (tofree != NULL)
9830 name = tofree;
9831 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9832 if (n)
9833 {
9834 /* handle d.key, l[idx], f(expr) */
9835 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9836 if (n)
9837 clear_tv(&tv);
9838 }
9839 }
9840 if (*var != NUL)
9841 n = FALSE;
9842
9843 vim_free(tofree);
9844 return n;
9845}
9846
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847#endif /* FEAT_EVAL */
9848
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009850#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
9852#ifdef WIN3264
9853/*
9854 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
9857/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009858 * Get the short path (8.3) for the filename in "fnamep".
9859 * Only works for a valid file name.
9860 * When the path gets longer "fnamep" is changed and the allocated buffer
9861 * is put in "bufp".
9862 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9863 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 */
9865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009868 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 char_u *newbuf;
9870
9871 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009872 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 if (l > len - 1)
9874 {
9875 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009876 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 newbuf = vim_strnsave(*fnamep, l);
9878 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009879 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
9881 vim_free(*bufp);
9882 *fnamep = *bufp = newbuf;
9883
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009884 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009885 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 }
9887
9888 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009889 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009893 * Get the short path (8.3) for the filename in "fname". The converted
9894 * path is returned in "bufp".
9895 *
9896 * Some of the directories specified in "fname" may not exist. This function
9897 * will shorten the existing directories at the beginning of the path and then
9898 * append the remaining non-existing path.
9899 *
9900 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009901 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009902 * bufp - Pointer to an allocated buffer for the filename.
9903 * fnamelen - Length of the filename pointed to by fname
9904 *
9905 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 */
9907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908shortpath_for_invalid_fname(
9909 char_u **fname,
9910 char_u **bufp,
9911 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009913 char_u *short_fname, *save_fname, *pbuf_unused;
9914 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009916 int old_len, len;
9917 int new_len, sfx_len;
9918 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
9920 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009921 old_len = *fnamelen;
9922 save_fname = vim_strnsave(*fname, old_len);
9923 pbuf_unused = NULL;
9924 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009926 endp = save_fname + old_len - 1; /* Find the end of the copy */
9927 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009929 /*
9930 * Try shortening the supplied path till it succeeds by removing one
9931 * directory at a time from the tail of the path.
9932 */
9933 len = 0;
9934 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009936 /* go back one path-separator */
9937 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9938 --endp;
9939 if (endp <= save_fname)
9940 break; /* processed the complete path */
9941
9942 /*
9943 * Replace the path separator with a NUL and try to shorten the
9944 * resulting path.
9945 */
9946 ch = *endp;
9947 *endp = 0;
9948 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009949 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009950 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9951 {
9952 retval = FAIL;
9953 goto theend;
9954 }
9955 *endp = ch; /* preserve the string */
9956
9957 if (len > 0)
9958 break; /* successfully shortened the path */
9959
9960 /* failed to shorten the path. Skip the path separator */
9961 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 }
9963
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009964 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009966 /*
9967 * Succeeded in shortening the path. Now concatenate the shortened
9968 * path with the remaining path at the tail.
9969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009971 /* Compute the length of the new path. */
9972 sfx_len = (int)(save_endp - endp) + 1;
9973 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009975 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009977 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009979 /* There is not enough space in the currently allocated string,
9980 * copy it to a buffer big enough. */
9981 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009983 {
9984 retval = FAIL;
9985 goto theend;
9986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 }
9988 else
9989 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009990 /* Transfer short_fname to the main buffer (it's big enough),
9991 * unless get_short_pathname() did its work in-place. */
9992 *fname = *bufp = save_fname;
9993 if (short_fname != save_fname)
9994 vim_strncpy(save_fname, short_fname, len);
9995 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009997
9998 /* concat the not-shortened part of the path */
9999 vim_strncpy(*fname + len, endp, sfx_len);
10000 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010002
10003theend:
10004 vim_free(pbuf_unused);
10005 vim_free(save_fname);
10006
10007 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008}
10009
10010/*
10011 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010012 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 */
10014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010015shortpath_for_partial(
10016 char_u **fnamep,
10017 char_u **bufp,
10018 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020 int sepcount, len, tflen;
10021 char_u *p;
10022 char_u *pbuf, *tfname;
10023 int hasTilde;
10024
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010025 /* Count up the path separators from the RHS.. so we know which part
10026 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010028 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 if (vim_ispathsep(*p))
10030 ++sepcount;
10031
10032 /* Need full path first (use expand_env() to remove a "~/") */
10033 hasTilde = (**fnamep == '~');
10034 if (hasTilde)
10035 pbuf = tfname = expand_env_save(*fnamep);
10036 else
10037 pbuf = tfname = FullName_save(*fnamep, FALSE);
10038
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010039 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010041 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10042 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 if (len == 0)
10045 {
10046 /* Don't have a valid filename, so shorten the rest of the
10047 * path if we can. This CAN give us invalid 8.3 filenames, but
10048 * there's not a lot of point in guessing what it might be.
10049 */
10050 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010051 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10052 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054
10055 /* Count the paths backward to find the beginning of the desired string. */
10056 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010057 {
10058#ifdef FEAT_MBYTE
10059 if (has_mbyte)
10060 p -= mb_head_off(tfname, p);
10061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (vim_ispathsep(*p))
10063 {
10064 if (sepcount == 0 || (hasTilde && sepcount == 1))
10065 break;
10066 else
10067 sepcount --;
10068 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (hasTilde)
10071 {
10072 --p;
10073 if (p >= tfname)
10074 *p = '~';
10075 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010076 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078 else
10079 ++p;
10080
10081 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10082 vim_free(*bufp);
10083 *fnamelen = (int)STRLEN(p);
10084 *bufp = pbuf;
10085 *fnamep = p;
10086
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010087 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088}
10089#endif /* WIN3264 */
10090
10091/*
10092 * Adjust a filename, according to a string of modifiers.
10093 * *fnamep must be NUL terminated when called. When returning, the length is
10094 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010095 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 * When there is an error, *fnamep is set to NULL.
10097 */
10098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010100 char_u *src, // string with modifiers
10101 int tilde_file, // "~" is a file name, not $HOME
10102 int *usedlen, // characters after src that are used
10103 char_u **fnamep, // file name so far
10104 char_u **bufp, // buffer for allocated file name or NULL
10105 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 int valid = 0;
10108 char_u *tail;
10109 char_u *s, *p, *pbuf;
10110 char_u dirname[MAXPATHL];
10111 int c;
10112 int has_fullname = 0;
10113#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010114 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 int has_shortname = 0;
10116#endif
10117
10118repeat:
10119 /* ":p" - full path/file_name */
10120 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10121 {
10122 has_fullname = 1;
10123
10124 valid |= VALID_PATH;
10125 *usedlen += 2;
10126
10127 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10128 if ((*fnamep)[0] == '~'
10129#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10130 && ((*fnamep)[1] == '/'
10131# ifdef BACKSLASH_IN_FILENAME
10132 || (*fnamep)[1] == '\\'
10133# endif
10134 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010136 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 )
10138 {
10139 *fnamep = expand_env_save(*fnamep);
10140 vim_free(*bufp); /* free any allocated file name */
10141 *bufp = *fnamep;
10142 if (*fnamep == NULL)
10143 return -1;
10144 }
10145
10146 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010147 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 {
10149 if (vim_ispathsep(*p)
10150 && p[1] == '.'
10151 && (p[2] == NUL
10152 || vim_ispathsep(p[2])
10153 || (p[2] == '.'
10154 && (p[3] == NUL || vim_ispathsep(p[3])))))
10155 break;
10156 }
10157
10158 /* FullName_save() is slow, don't use it when not needed. */
10159 if (*p != NUL || !vim_isAbsName(*fnamep))
10160 {
10161 *fnamep = FullName_save(*fnamep, *p != NUL);
10162 vim_free(*bufp); /* free any allocated file name */
10163 *bufp = *fnamep;
10164 if (*fnamep == NULL)
10165 return -1;
10166 }
10167
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010168#ifdef WIN3264
10169# if _WIN32_WINNT >= 0x0500
10170 if (vim_strchr(*fnamep, '~') != NULL)
10171 {
10172 /* Expand 8.3 filename to full path. Needed to make sure the same
10173 * file does not have two different names.
10174 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10175 p = alloc(_MAX_PATH + 1);
10176 if (p != NULL)
10177 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010178 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010179 {
10180 vim_free(*bufp);
10181 *bufp = *fnamep = p;
10182 }
10183 else
10184 vim_free(p);
10185 }
10186 }
10187# endif
10188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 /* Append a path separator to a directory. */
10190 if (mch_isdir(*fnamep))
10191 {
10192 /* Make room for one or two extra characters. */
10193 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10194 vim_free(*bufp); /* free any allocated file name */
10195 *bufp = *fnamep;
10196 if (*fnamep == NULL)
10197 return -1;
10198 add_pathsep(*fnamep);
10199 }
10200 }
10201
10202 /* ":." - path relative to the current directory */
10203 /* ":~" - path relative to the home directory */
10204 /* ":8" - shortname path - postponed till after */
10205 while (src[*usedlen] == ':'
10206 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10207 {
10208 *usedlen += 2;
10209 if (c == '8')
10210 {
10211#ifdef WIN3264
10212 has_shortname = 1; /* Postpone this. */
10213#endif
10214 continue;
10215 }
10216 pbuf = NULL;
10217 /* Need full path first (use expand_env() to remove a "~/") */
10218 if (!has_fullname)
10219 {
10220 if (c == '.' && **fnamep == '~')
10221 p = pbuf = expand_env_save(*fnamep);
10222 else
10223 p = pbuf = FullName_save(*fnamep, FALSE);
10224 }
10225 else
10226 p = *fnamep;
10227
10228 has_fullname = 0;
10229
10230 if (p != NULL)
10231 {
10232 if (c == '.')
10233 {
10234 mch_dirname(dirname, MAXPATHL);
10235 s = shorten_fname(p, dirname);
10236 if (s != NULL)
10237 {
10238 *fnamep = s;
10239 if (pbuf != NULL)
10240 {
10241 vim_free(*bufp); /* free any allocated file name */
10242 *bufp = pbuf;
10243 pbuf = NULL;
10244 }
10245 }
10246 }
10247 else
10248 {
10249 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10250 /* Only replace it when it starts with '~' */
10251 if (*dirname == '~')
10252 {
10253 s = vim_strsave(dirname);
10254 if (s != NULL)
10255 {
10256 *fnamep = s;
10257 vim_free(*bufp);
10258 *bufp = s;
10259 }
10260 }
10261 }
10262 vim_free(pbuf);
10263 }
10264 }
10265
10266 tail = gettail(*fnamep);
10267 *fnamelen = (int)STRLEN(*fnamep);
10268
10269 /* ":h" - head, remove "/file_name", can be repeated */
10270 /* Don't remove the first "/" or "c:\" */
10271 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10272 {
10273 valid |= VALID_HEAD;
10274 *usedlen += 2;
10275 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010276 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010277 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 *fnamelen = (int)(tail - *fnamep);
10279#ifdef VMS
10280 if (*fnamelen > 0)
10281 *fnamelen += 1; /* the path separator is part of the path */
10282#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010283 if (*fnamelen == 0)
10284 {
10285 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10286 p = vim_strsave((char_u *)".");
10287 if (p == NULL)
10288 return -1;
10289 vim_free(*bufp);
10290 *bufp = *fnamep = tail = p;
10291 *fnamelen = 1;
10292 }
10293 else
10294 {
10295 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010296 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 }
10299
10300 /* ":8" - shortname */
10301 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10302 {
10303 *usedlen += 2;
10304#ifdef WIN3264
10305 has_shortname = 1;
10306#endif
10307 }
10308
10309#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010310 /*
10311 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 */
10313 if (has_shortname)
10314 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010315 /* Copy the string if it is shortened by :h and when it wasn't copied
10316 * yet, because we are going to change it in place. Avoids changing
10317 * the buffer name for "%:8". */
10318 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 {
10320 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010321 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 return -1;
10323 vim_free(*bufp);
10324 *bufp = *fnamep = p;
10325 }
10326
10327 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010328 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (!has_fullname && !vim_isAbsName(*fnamep))
10330 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010331 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 return -1;
10333 }
10334 else
10335 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010336 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337
Bram Moolenaardc935552011-08-17 15:23:23 +020010338 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010340 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 return -1;
10342
10343 if (l == 0)
10344 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010345 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010347 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 return -1;
10349 }
10350 *fnamelen = l;
10351 }
10352 }
10353#endif /* WIN3264 */
10354
10355 /* ":t" - tail, just the basename */
10356 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10357 {
10358 *usedlen += 2;
10359 *fnamelen -= (int)(tail - *fnamep);
10360 *fnamep = tail;
10361 }
10362
10363 /* ":e" - extension, can be repeated */
10364 /* ":r" - root, without extension, can be repeated */
10365 while (src[*usedlen] == ':'
10366 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10367 {
10368 /* find a '.' in the tail:
10369 * - for second :e: before the current fname
10370 * - otherwise: The last '.'
10371 */
10372 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10373 s = *fnamep - 2;
10374 else
10375 s = *fnamep + *fnamelen - 1;
10376 for ( ; s > tail; --s)
10377 if (s[0] == '.')
10378 break;
10379 if (src[*usedlen + 1] == 'e') /* :e */
10380 {
10381 if (s > tail)
10382 {
10383 *fnamelen += (int)(*fnamep - (s + 1));
10384 *fnamep = s + 1;
10385#ifdef VMS
10386 /* cut version from the extension */
10387 s = *fnamep + *fnamelen - 1;
10388 for ( ; s > *fnamep; --s)
10389 if (s[0] == ';')
10390 break;
10391 if (s > *fnamep)
10392 *fnamelen = s - *fnamep;
10393#endif
10394 }
10395 else if (*fnamep <= tail)
10396 *fnamelen = 0;
10397 }
10398 else /* :r */
10399 {
10400 if (s > tail) /* remove one extension */
10401 *fnamelen = (int)(s - *fnamep);
10402 }
10403 *usedlen += 2;
10404 }
10405
10406 /* ":s?pat?foo?" - substitute */
10407 /* ":gs?pat?foo?" - global substitute */
10408 if (src[*usedlen] == ':'
10409 && (src[*usedlen + 1] == 's'
10410 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10411 {
10412 char_u *str;
10413 char_u *pat;
10414 char_u *sub;
10415 int sep;
10416 char_u *flags;
10417 int didit = FALSE;
10418
10419 flags = (char_u *)"";
10420 s = src + *usedlen + 2;
10421 if (src[*usedlen + 1] == 'g')
10422 {
10423 flags = (char_u *)"g";
10424 ++s;
10425 }
10426
10427 sep = *s++;
10428 if (sep)
10429 {
10430 /* find end of pattern */
10431 p = vim_strchr(s, sep);
10432 if (p != NULL)
10433 {
10434 pat = vim_strnsave(s, (int)(p - s));
10435 if (pat != NULL)
10436 {
10437 s = p + 1;
10438 /* find end of substitution */
10439 p = vim_strchr(s, sep);
10440 if (p != NULL)
10441 {
10442 sub = vim_strnsave(s, (int)(p - s));
10443 str = vim_strnsave(*fnamep, *fnamelen);
10444 if (sub != NULL && str != NULL)
10445 {
10446 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010447 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 if (s != NULL)
10449 {
10450 *fnamep = s;
10451 *fnamelen = (int)STRLEN(s);
10452 vim_free(*bufp);
10453 *bufp = s;
10454 didit = TRUE;
10455 }
10456 }
10457 vim_free(sub);
10458 vim_free(str);
10459 }
10460 vim_free(pat);
10461 }
10462 }
10463 /* after using ":s", repeat all the modifiers */
10464 if (didit)
10465 goto repeat;
10466 }
10467 }
10468
Bram Moolenaar26df0922014-02-23 23:39:13 +010010469 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10470 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010471 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010472 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010473 if (c != NUL)
10474 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010475 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010476 if (c != NUL)
10477 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010478 if (p == NULL)
10479 return -1;
10480 vim_free(*bufp);
10481 *bufp = *fnamep = p;
10482 *fnamelen = (int)STRLEN(p);
10483 *usedlen += 2;
10484 }
10485
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 return valid;
10487}
10488
10489/*
10490 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010491 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 * "flags" can be "g" to do a global substitute.
10493 * Returns an allocated string, NULL for error.
10494 */
10495 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010496do_string_sub(
10497 char_u *str,
10498 char_u *pat,
10499 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010500 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503 int sublen;
10504 regmatch_T regmatch;
10505 int i;
10506 int do_all;
10507 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010508 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 garray_T ga;
10510 char_u *ret;
10511 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010512 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513
10514 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10515 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010516 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
10518 ga_init2(&ga, 1, 200);
10519
10520 do_all = (flags[0] == 'g');
10521
10522 regmatch.rm_ic = p_ic;
10523 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10524 if (regmatch.regprog != NULL)
10525 {
10526 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010527 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10529 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010530 /* Skip empty match except for first match. */
10531 if (regmatch.startp[0] == regmatch.endp[0])
10532 {
10533 if (zero_width == regmatch.startp[0])
10534 {
10535 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010536 i = MB_PTR2LEN(tail);
10537 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10538 (size_t)i);
10539 ga.ga_len += i;
10540 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010541 continue;
10542 }
10543 zero_width = regmatch.startp[0];
10544 }
10545
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 /*
10547 * Get some space for a temporary buffer to do the substitution
10548 * into. It will contain:
10549 * - The text up to where the match is.
10550 * - The substituted text.
10551 * - The text after the match.
10552 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010553 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010554 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10556 {
10557 ga_clear(&ga);
10558 break;
10559 }
10560
10561 /* copy the text up to where the match is */
10562 i = (int)(regmatch.startp[0] - tail);
10563 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10564 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010565 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 + ga.ga_len + i, TRUE, TRUE, FALSE);
10567 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010568 tail = regmatch.endp[0];
10569 if (*tail == NUL)
10570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (!do_all)
10572 break;
10573 }
10574
10575 if (ga.ga_data != NULL)
10576 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10577
Bram Moolenaar473de612013-06-08 18:19:48 +020010578 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 }
10580
10581 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10582 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010583 if (p_cpo == empty_option)
10584 p_cpo = save_cpo;
10585 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010586 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010587 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588
10589 return ret;
10590}
10591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592 static int
10593filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10594{
10595 typval_T rettv;
10596 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010597 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010598
10599 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10600 argv[0] = vimvars[VV_KEY].vv_tv;
10601 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010602 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10603 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604 if (map)
10605 {
10606 /* map(): replace the list item value */
10607 clear_tv(tv);
10608 rettv.v_lock = 0;
10609 *tv = rettv;
10610 }
10611 else
10612 {
10613 int error = FALSE;
10614
10615 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010616 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 clear_tv(&rettv);
10618 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010619 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010620 if (error)
10621 goto theend;
10622 }
10623 retval = OK;
10624theend:
10625 clear_tv(&vimvars[VV_VAL].vv_tv);
10626 return retval;
10627}
10628
10629
10630/*
10631 * Implementation of map() and filter().
10632 */
10633 void
10634filter_map(typval_T *argvars, typval_T *rettv, int map)
10635{
10636 typval_T *expr;
10637 listitem_T *li, *nli;
10638 list_T *l = NULL;
10639 dictitem_T *di;
10640 hashtab_T *ht;
10641 hashitem_T *hi;
10642 dict_T *d = NULL;
10643 typval_T save_val;
10644 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010645 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010646 int rem;
10647 int todo;
10648 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10649 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10650 : N_("filter() argument"));
10651 int save_did_emsg;
10652 int idx = 0;
10653
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010654 if (argvars[0].v_type == VAR_BLOB)
10655 {
10656 if ((b = argvars[0].vval.v_blob) == NULL)
10657 return;
10658 }
10659 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660 {
10661 if ((l = argvars[0].vval.v_list) == NULL
10662 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10663 return;
10664 }
10665 else if (argvars[0].v_type == VAR_DICT)
10666 {
10667 if ((d = argvars[0].vval.v_dict) == NULL
10668 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10669 return;
10670 }
10671 else
10672 {
10673 EMSG2(_(e_listdictarg), ermsg);
10674 return;
10675 }
10676
10677 expr = &argvars[1];
10678 /* On type errors, the preceding call has already displayed an error
10679 * message. Avoid a misleading error message for an empty string that
10680 * was not passed as argument. */
10681 if (expr->v_type != VAR_UNKNOWN)
10682 {
10683 prepare_vimvar(VV_VAL, &save_val);
10684
10685 /* We reset "did_emsg" to be able to detect whether an error
10686 * occurred during evaluation of the expression. */
10687 save_did_emsg = did_emsg;
10688 did_emsg = FALSE;
10689
10690 prepare_vimvar(VV_KEY, &save_key);
10691 if (argvars[0].v_type == VAR_DICT)
10692 {
10693 vimvars[VV_KEY].vv_type = VAR_STRING;
10694
10695 ht = &d->dv_hashtab;
10696 hash_lock(ht);
10697 todo = (int)ht->ht_used;
10698 for (hi = ht->ht_array; todo > 0; ++hi)
10699 {
10700 if (!HASHITEM_EMPTY(hi))
10701 {
10702 int r;
10703
10704 --todo;
10705 di = HI2DI(hi);
10706 if (map &&
10707 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10708 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10709 break;
10710 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10711 r = filter_map_one(&di->di_tv, expr, map, &rem);
10712 clear_tv(&vimvars[VV_KEY].vv_tv);
10713 if (r == FAIL || did_emsg)
10714 break;
10715 if (!map && rem)
10716 {
10717 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10718 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10719 break;
10720 dictitem_remove(d, di);
10721 }
10722 }
10723 }
10724 hash_unlock(ht);
10725 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010726 else if (argvars[0].v_type == VAR_BLOB)
10727 {
10728 int i;
10729 typval_T tv;
10730
10731 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10732 for (i = 0; i < b->bv_ga.ga_len; i++)
10733 {
10734 tv.v_type = VAR_NUMBER;
10735 tv.vval.v_number = blob_get(b, i);
10736 vimvars[VV_KEY].vv_nr = idx;
10737 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10738 break;
10739 if (tv.v_type != VAR_NUMBER)
10740 {
10741 EMSG(_(e_invalblob));
10742 return;
10743 }
10744 tv.v_type = VAR_NUMBER;
10745 blob_set(b, i, tv.vval.v_number);
10746 if (!map && rem)
10747 {
10748 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10749
10750 mch_memmove(p + idx, p + i + 1,
10751 (size_t)b->bv_ga.ga_len - i - 1);
10752 --b->bv_ga.ga_len;
10753 --i;
10754 }
10755 }
10756 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 else
10758 {
10759 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10760
10761 for (li = l->lv_first; li != NULL; li = nli)
10762 {
10763 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10764 break;
10765 nli = li->li_next;
10766 vimvars[VV_KEY].vv_nr = idx;
10767 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10768 || did_emsg)
10769 break;
10770 if (!map && rem)
10771 listitem_remove(l, li);
10772 ++idx;
10773 }
10774 }
10775
10776 restore_vimvar(VV_KEY, &save_key);
10777 restore_vimvar(VV_VAL, &save_val);
10778
10779 did_emsg |= save_did_emsg;
10780 }
10781
10782 copy_tv(&argvars[0], rettv);
10783}
10784
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */