blob: 4f931a271485e08e55e588c36ca3381719ac7618 [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 Moolenaarf9e3e092019-01-13 23:38:42 +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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100451 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 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 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100478 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100480 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 Moolenaarce9d50d2019-01-14 22:22:29 +0100699/*
700 * Call eval1() and give an error message if not done at a lower level.
701 */
702 static int
703eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
704{
705 int ret;
706 int did_emsg_before = did_emsg;
707 int called_emsg_before = called_emsg;
708
709 ret = eval1(arg, rettv, evaluate);
710 if (ret == FAIL)
711 {
712 // Report the invalid expression unless the expression evaluation has
713 // been cancelled due to an aborting error, an interrupt, or an
714 // exception, or we already gave a more specific error.
715 // Also check called_emsg for when using assert_fails().
716 if (!aborting() && did_emsg == did_emsg_before
717 && called_emsg == called_emsg_before)
718 semsg(_(e_invexpr2), arg);
719 }
720 return ret;
721}
722
Bram Moolenaar48570482017-10-30 21:48:41 +0100723 static int
724eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
725{
726 char_u *s;
727 int dummy;
728 char_u buf[NUMBUFLEN];
729
730 if (expr->v_type == VAR_FUNC)
731 {
732 s = expr->vval.v_string;
733 if (s == NULL || *s == NUL)
734 return FAIL;
735 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
736 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
737 return FAIL;
738 }
739 else if (expr->v_type == VAR_PARTIAL)
740 {
741 partial_T *partial = expr->vval.v_partial;
742
743 s = partial_name(partial);
744 if (s == NULL || *s == NUL)
745 return FAIL;
746 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
747 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
748 return FAIL;
749 }
750 else
751 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100752 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100753 if (s == NULL)
754 return FAIL;
755 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100756 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100757 return FAIL;
758 if (*s != NUL) /* check for trailing chars after expr */
759 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200760 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100761 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100762 return FAIL;
763 }
764 }
765 return OK;
766}
767
768/*
769 * Like eval_to_bool() but using a typval_T instead of a string.
770 * Works for string, funcref and partial.
771 */
772 int
773eval_expr_to_bool(typval_T *expr, int *error)
774{
775 typval_T rettv;
776 int res;
777
778 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
779 {
780 *error = TRUE;
781 return FALSE;
782 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100783 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100784 clear_tv(&rettv);
785 return res;
786}
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788/*
789 * Top level evaluation function, returning a string. If "skip" is TRUE,
790 * only parsing to "nextcmd" is done, without reporting errors. Return
791 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
792 */
793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100794eval_to_string_skip(
795 char_u *arg,
796 char_u **nextcmd,
797 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 char_u *retval;
801
802 if (skip)
803 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000804 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 retval = NULL;
806 else
807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100808 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811 if (skip)
812 --emsg_skip;
813
814 return retval;
815}
816
817/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000818 * Skip over an expression at "*pp".
819 * Return FAIL for an error, OK otherwise.
820 */
821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100822skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000823{
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000825
826 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000827 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000828}
829
830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000832 * When "convert" is TRUE convert a List into a sequence of lines and convert
833 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Return pointer to allocated memory, or NULL for failure.
835 */
836 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100837eval_to_string(
838 char_u *arg,
839 char_u **nextcmd,
840 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000845#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000846 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 retval = NULL;
851 else
852 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000853 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000854 {
855 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000856 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200857 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200858 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200859 if (tv.vval.v_list->lv_len > 0)
860 ga_append(&ga, NL);
861 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000862 ga_append(&ga, NUL);
863 retval = (char_u *)ga.ga_data;
864 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000865#ifdef FEAT_FLOAT
866 else if (convert && tv.v_type == VAR_FLOAT)
867 {
868 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
869 retval = vim_strsave(numbuf);
870 }
871#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000872 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100873 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
877 return retval;
878}
879
880/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000881 * Call eval_to_string() without using current local variables and using
882 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 */
884 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100885eval_to_string_safe(
886 char_u *arg,
887 char_u **nextcmd,
888 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200891 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200893 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000894 if (use_sandbox)
895 ++sandbox;
896 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000897 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000898 if (use_sandbox)
899 --sandbox;
900 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200901 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 return retval;
903}
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905/*
906 * Top level evaluation function, returning a number.
907 * Evaluates "expr" silently.
908 * Returns -1 for an error.
909 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200910 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100911eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200914 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000915 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 ++emsg_off;
918
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000919 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = -1;
921 else
922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100923 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 --emsg_off;
927
928 return retval;
929}
930
Bram Moolenaara40058a2005-07-11 22:42:07 +0000931/*
932 * Prepare v: variable "idx" to be used.
933 * Save the current typeval in "save_tv".
934 * When not used yet add the variable to the v: hashtable.
935 */
936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000938{
939 *save_tv = vimvars[idx].vv_tv;
940 if (vimvars[idx].vv_type == VAR_UNKNOWN)
941 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
942}
943
944/*
945 * Restore v: variable "idx" to typeval "save_tv".
946 * When no longer defined, remove the variable from the v: hashtable.
947 */
948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100949restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000950{
951 hashitem_T *hi;
952
Bram Moolenaara40058a2005-07-11 22:42:07 +0000953 vimvars[idx].vv_tv = *save_tv;
954 if (vimvars[idx].vv_type == VAR_UNKNOWN)
955 {
956 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
957 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100958 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000959 else
960 hash_remove(&vimvarht, hi);
961 }
962}
963
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000964#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000965/*
966 * Evaluate an expression to a list with suggestions.
967 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000968 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000969 */
970 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000972{
973 typval_T save_val;
974 typval_T rettv;
975 list_T *list = NULL;
976 char_u *p = skipwhite(expr);
977
978 /* Set "v:val" to the bad word. */
979 prepare_vimvar(VV_VAL, &save_val);
980 vimvars[VV_VAL].vv_type = VAR_STRING;
981 vimvars[VV_VAL].vv_str = badword;
982 if (p_verbose == 0)
983 ++emsg_off;
984
985 if (eval1(&p, &rettv, TRUE) == OK)
986 {
987 if (rettv.v_type != VAR_LIST)
988 clear_tv(&rettv);
989 else
990 list = rettv.vval.v_list;
991 }
992
993 if (p_verbose == 0)
994 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000995 restore_vimvar(VV_VAL, &save_val);
996
997 return list;
998}
999
1000/*
1001 * "list" is supposed to contain two items: a word and a number. Return the
1002 * word in "pp" and the number as the return value.
1003 * Return -1 if anything isn't right.
1004 * Used to get the good word and score from the eval_spell_expr() result.
1005 */
1006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001007get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001008{
1009 listitem_T *li;
1010
1011 li = list->lv_first;
1012 if (li == NULL)
1013 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001014 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001015
1016 li = li->li_next;
1017 if (li == NULL)
1018 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001019 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001020}
1021#endif
1022
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001023/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001024 * Top level evaluation function.
1025 * Returns an allocated typval_T with the result.
1026 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001027 */
1028 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001029eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001030{
1031 typval_T *tv;
1032
1033 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001034 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001035 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001036
1037 return tv;
1038}
1039
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001042 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001043 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1044 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001045 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048call_vim_function(
1049 char_u *func,
1050 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001051 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001052 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001055 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001057 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001058 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001060 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001061 if (ret == FAIL)
1062 clear_tv(rettv);
1063
1064 return ret;
1065}
1066
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001067/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001068 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001069 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001070 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1071 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001072 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001073 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074call_func_retnr(
1075 char_u *func,
1076 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001077 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001078{
1079 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001080 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001081
Bram Moolenaarded27a12018-08-01 19:06:03 +02001082 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001083 return -1;
1084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001085 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001086 clear_tv(&rettv);
1087 return retval;
1088}
1089
1090#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1091 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1092
Bram Moolenaar4f688582007-07-24 12:34:30 +00001093# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001095 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001096 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001097 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1098 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 */
1100 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001101call_func_retstr(
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;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001107 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001108
Bram Moolenaarded27a12018-08-01 19:06:03 +02001109 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001110 return NULL;
1111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001112 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return retval;
1115}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001116# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001117
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001118/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001119 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001120 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1121 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001122 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001123 */
1124 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001125call_func_retlist(
1126 char_u *func,
1127 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001128 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001129{
1130 typval_T rettv;
1131
Bram Moolenaarded27a12018-08-01 19:06:03 +02001132 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001133 return NULL;
1134
1135 if (rettv.v_type != VAR_LIST)
1136 {
1137 clear_tv(&rettv);
1138 return NULL;
1139 }
1140
1141 return rettv.vval.v_list;
1142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144
Bram Moolenaar05159a02005-02-26 23:04:13 +00001145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#ifdef FEAT_FOLDING
1147/*
1148 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1149 * it in "*cp". Doesn't give error messages.
1150 */
1151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001152eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
Bram Moolenaar33570922005-01-25 22:26:29 +00001154 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001155 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001157 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1158 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001161 if (use_sandbox)
1162 ++sandbox;
1163 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001165 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 retval = 0;
1167 else
1168 {
1169 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 if (tv.v_type == VAR_NUMBER)
1171 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001172 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 retval = 0;
1174 else
1175 {
1176 /* If the result is a string, check if there is a non-digit before
1177 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (!VIM_ISDIGIT(*s) && *s != '-')
1180 *cp = *s++;
1181 retval = atol((char *)s);
1182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001186 if (use_sandbox)
1187 --sandbox;
1188 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
1192#endif
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 * ":let" list all variable values
1196 * ":let var1 var2" list variable values
1197 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001198 * ":let var += expr" assignment command.
1199 * ":let var -= expr" assignment command.
1200 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
1203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001204ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001208 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 int var_count = 0;
1211 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001212 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001213 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001214 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Bram Moolenaardb552d602006-03-23 22:59:57 +00001216 argend = skip_var_list(arg, &var_count, &semicolon);
1217 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001218 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001219 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1220 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001221 expr = skipwhite(argend);
1222 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1223 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001225 /*
1226 * ":let" without "=": list variables
1227 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001228 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001230 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001232 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001235 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001236 list_glob_vars(&first);
1237 list_buf_vars(&first);
1238 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001239 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001240 list_script_vars(&first);
1241 list_func_vars(&first);
1242 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 eap->nextcmd = check_nextcmd(arg);
1245 }
1246 else
1247 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001248 op[0] = '=';
1249 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001250 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001252 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1253 op[0] = *expr; /* +=, -= or .= */
1254 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001255 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001256 else
1257 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (eap->skip)
1260 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001261 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (eap->skip)
1263 {
1264 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 --emsg_skip;
1267 }
1268 else if (i != FAIL)
1269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001270 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 }
1275}
1276
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001277/*
1278 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1279 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 * When "nextchars" is not NULL it points to a string with characters that
1281 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1282 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001283 * Returns OK or FAIL;
1284 */
1285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001286ex_let_vars(
1287 char_u *arg_start,
1288 typval_T *tv,
1289 int copy, /* copy values from "tv", don't move */
1290 int semicolon, /* from skip_var_list() */
1291 int var_count, /* from skip_var_list() */
1292 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001293{
1294 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001296 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 listitem_T *item;
1298 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299
1300 if (*arg != '[')
1301 {
1302 /*
1303 * ":let var = expr" or ":for var in list"
1304 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001305 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001306 return FAIL;
1307 return OK;
1308 }
1309
1310 /*
1311 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1312 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001313 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001315 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001316 return FAIL;
1317 }
1318
1319 i = list_len(l);
1320 if (semicolon == 0 && var_count < i)
1321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001322 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001323 return FAIL;
1324 }
1325 if (var_count - semicolon > i)
1326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001327 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001328 return FAIL;
1329 }
1330
1331 item = l->lv_first;
1332 while (*arg != ']')
1333 {
1334 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336 item = item->li_next;
1337 if (arg == NULL)
1338 return FAIL;
1339
1340 arg = skipwhite(arg);
1341 if (*arg == ';')
1342 {
1343 /* Put the rest of the list (may be empty) in the var after ';'.
1344 * Create a new list for this. */
1345 l = list_alloc();
1346 if (l == NULL)
1347 return FAIL;
1348 while (item != NULL)
1349 {
1350 list_append_tv(l, &item->li_tv);
1351 item = item->li_next;
1352 }
1353
1354 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001355 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356 ltv.vval.v_list = l;
1357 l->lv_refcount = 1;
1358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1360 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001361 clear_tv(&ltv);
1362 if (arg == NULL)
1363 return FAIL;
1364 break;
1365 }
1366 else if (*arg != ',' && *arg != ']')
1367 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001368 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001369 return FAIL;
1370 }
1371 }
1372
1373 return OK;
1374}
1375
1376/*
1377 * Skip over assignable variable "var" or list of variables "[var, var]".
1378 * Used for ":let varvar = expr" and ":for varvar in expr".
1379 * For "[var, var]" increment "*var_count" for each variable.
1380 * for "[var, var; var]" set "semicolon".
1381 * Return NULL for an error.
1382 */
1383 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001384skip_var_list(
1385 char_u *arg,
1386 int *var_count,
1387 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001388{
1389 char_u *p, *s;
1390
1391 if (*arg == '[')
1392 {
1393 /* "[var, var]": find the matching ']'. */
1394 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001395 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001396 {
1397 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1398 s = skip_var_one(p);
1399 if (s == p)
1400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001401 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 return NULL;
1403 }
1404 ++*var_count;
1405
1406 p = skipwhite(s);
1407 if (*p == ']')
1408 break;
1409 else if (*p == ';')
1410 {
1411 if (*semicolon == 1)
1412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001413 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414 return NULL;
1415 }
1416 *semicolon = 1;
1417 }
1418 else if (*p != ',')
1419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001420 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 return NULL;
1422 }
1423 }
1424 return p + 1;
1425 }
1426 else
1427 return skip_var_one(arg);
1428}
1429
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001430/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001431 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001432 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001433 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001434 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001435skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001436{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001437 if (*arg == '@' && arg[1] != NUL)
1438 return arg + 2;
1439 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1440 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001441}
1442
Bram Moolenaara7043832005-01-21 11:56:39 +00001443/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001444 * List variables for hashtab "ht" with prefix "prefix".
1445 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001446 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001448list_hashtable_vars(
1449 hashtab_T *ht,
1450 char_u *prefix,
1451 int empty,
1452 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001453{
Bram Moolenaar33570922005-01-25 22:26:29 +00001454 hashitem_T *hi;
1455 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001456 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001457 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001458
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001459 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001460 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1461 {
1462 if (!HASHITEM_EMPTY(hi))
1463 {
1464 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001465 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001466
1467 // apply :filter /pat/ to variable name
1468 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1469 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1470 if (message_filtered(buf))
1471 continue;
1472
Bram Moolenaar33570922005-01-25 22:26:29 +00001473 if (empty || di->di_tv.v_type != VAR_STRING
1474 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001475 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001476 }
1477 }
1478}
1479
1480/*
1481 * List global variables.
1482 */
1483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001485{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001486 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001487}
1488
1489/*
1490 * List buffer variables.
1491 */
1492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001494{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001495 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001496 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001497}
1498
1499/*
1500 * List window variables.
1501 */
1502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001504{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001505 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001506 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001507}
1508
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001509/*
1510 * List tab page variables.
1511 */
1512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001513list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001514{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001515 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001516 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001517}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001518
Bram Moolenaara7043832005-01-21 11:56:39 +00001519/*
1520 * List Vim variables.
1521 */
1522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001525 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526}
1527
1528/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001529 * List script-local variables, if there is a script.
1530 */
1531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001533{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001534 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1535 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001536 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001537}
1538
1539/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 * List variables in "arg".
1541 */
1542 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544{
1545 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001546 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001548 char_u *name_start;
1549 char_u *arg_subsc;
1550 char_u *tofree;
1551 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001552
1553 while (!ends_excmd(*arg) && !got_int)
1554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001556 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001557 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001558 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 {
1560 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001561 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562 break;
1563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001567 /* get_name_len() takes care of expanding curly braces */
1568 name_start = name = arg;
1569 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1570 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 /* This is mainly to keep test 49 working: when expanding
1573 * curly braces fails overrule the exception error message. */
1574 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001576 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001577 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 break;
1579 }
1580 error = TRUE;
1581 }
1582 else
1583 {
1584 if (tofree != NULL)
1585 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001586 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 else
1589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590 /* handle d.key, l[idx], f(expr) */
1591 arg_subsc = arg;
1592 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001593 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001598 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001599 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001600 case 'g': list_glob_vars(first); break;
1601 case 'b': list_buf_vars(first); break;
1602 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001603 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001604 case 'v': list_vim_vars(first); break;
1605 case 's': list_script_vars(first); break;
1606 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001607 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001608 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001609 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001610 }
1611 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001612 {
1613 char_u numbuf[NUMBUFLEN];
1614 char_u *tf;
1615 int c;
1616 char_u *s;
1617
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001618 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001619 c = *arg;
1620 *arg = NUL;
1621 list_one_var_a((char_u *)"",
1622 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001623 tv.v_type,
1624 s == NULL ? (char_u *)"" : s,
1625 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001626 *arg = c;
1627 vim_free(tf);
1628 }
1629 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 }
1632 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001633
1634 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636
1637 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 }
1639
1640 return arg;
1641}
1642
1643/*
1644 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1645 * Returns a pointer to the char just after the var name.
1646 * Returns NULL if there is an error.
1647 */
1648 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649ex_let_one(
1650 char_u *arg, /* points to variable name */
1651 typval_T *tv, /* value to assign to variable */
1652 int copy, /* copy value from "tv" */
1653 char_u *endchars, /* valid chars after variable name or NULL */
1654 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655{
1656 int c1;
1657 char_u *name;
1658 char_u *p;
1659 char_u *arg_end = NULL;
1660 int len;
1661 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663
1664 /*
1665 * ":let $VAR = expr": Set environment variable.
1666 */
1667 if (*arg == '$')
1668 {
1669 /* Find the end of the name. */
1670 ++arg;
1671 name = arg;
1672 len = get_env_len(&arg);
1673 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001674 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 else
1676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001678 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001681 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001682 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 {
1684 c1 = name[len];
1685 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001686 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001687 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 {
1689 int mustfree = FALSE;
1690 char_u *s = vim_getenv(name, &mustfree);
1691
1692 if (s != NULL)
1693 {
1694 p = tofree = concat_str(s, p);
1695 if (mustfree)
1696 vim_free(s);
1697 }
1698 }
1699 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001702 if (STRICMP(name, "HOME") == 0)
1703 init_homedir();
1704 else if (didset_vim && STRICMP(name, "VIM") == 0)
1705 didset_vim = FALSE;
1706 else if (didset_vimruntime
1707 && STRICMP(name, "VIMRUNTIME") == 0)
1708 didset_vimruntime = FALSE;
1709 arg_end = arg;
1710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 }
1714 }
1715 }
1716
1717 /*
1718 * ":let &option = expr": Set option value.
1719 * ":let &l:option = expr": Set local option value.
1720 * ":let &g:option = expr": Set global option value.
1721 */
1722 else if (*arg == '&')
1723 {
1724 /* Find the end of the name. */
1725 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 if (p == NULL || (endchars != NULL
1727 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001728 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 else
1730 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 long n;
1732 int opt_type;
1733 long numval;
1734 char_u *stringval = NULL;
1735 char_u *s;
1736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001737 c1 = *p;
1738 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001740 n = (long)tv_get_number(tv);
1741 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001742 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 {
1744 opt_type = get_option_value(arg, &numval,
1745 &stringval, opt_flags);
1746 if ((opt_type == 1 && *op == '.')
1747 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001749 semsg(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001750 s = NULL; /* don't set the value */
1751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 else
1753 {
1754 if (opt_type == 1) /* number */
1755 {
1756 if (*op == '+')
1757 n = numval + n;
1758 else
1759 n = numval - n;
1760 }
1761 else if (opt_type == 0 && stringval != NULL) /* string */
1762 {
1763 s = concat_str(stringval, s);
1764 vim_free(stringval);
1765 stringval = s;
1766 }
1767 }
1768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001769 if (s != NULL)
1770 {
1771 set_option_value(arg, n, s, opt_flags);
1772 arg_end = p;
1773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 }
1777 }
1778
1779 /*
1780 * ":let @r = expr": Set register contents.
1781 */
1782 else if (*arg == '@')
1783 {
1784 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001786 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001787 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001789 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 else
1791 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001792 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 char_u *s;
1794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001795 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001796 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001798 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 if (s != NULL)
1800 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001801 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001802 vim_free(s);
1803 }
1804 }
1805 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001806 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001808 arg_end = arg + 1;
1809 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001810 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 }
1812 }
1813
1814 /*
1815 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001816 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001818 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001822 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001823 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001825 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001826 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 else
1828 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001830 arg_end = p;
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 }
1835
1836 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838
1839 return arg_end;
1840}
1841
1842/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 * Get an lval: variable, Dict item or List item that can be assigned a value
1844 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1845 * "name.key", "name.key[expr]" etc.
1846 * Indexing only works if "name" is an existing List or Dictionary.
1847 * "name" points to the start of the name.
1848 * If "rettv" is not NULL it points to the value to be assigned.
1849 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1850 * wrong; must end in space or cmd separator.
1851 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001852 * flags:
1853 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001854 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001855 * GLV_NO_AUTOLOAD: do not use script autoloading
1856 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001857 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001859 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001862get_lval(
1863 char_u *name,
1864 typval_T *rettv,
1865 lval_T *lp,
1866 int unlet,
1867 int skip,
1868 int flags, /* GLV_ values */
1869 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 char_u *expr_start, *expr_end;
1873 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 dictitem_T *v;
1875 typval_T var1;
1876 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001877 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001879 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001880 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001882 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001884 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886
1887 if (skip)
1888 {
1889 /* When skipping just find the end of the name. */
1890 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001891 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001892 }
1893
1894 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001895 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 if (expr_start != NULL)
1897 {
1898 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001899 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 && *p != '[' && *p != '.')
1901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001903 return NULL;
1904 }
1905
1906 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1907 if (lp->ll_exp_name == NULL)
1908 {
1909 /* Report an invalid expression in braces, unless the
1910 * expression evaluation has been cancelled due to an
1911 * aborting error, an interrupt, or an exception. */
1912 if (!aborting() && !quiet)
1913 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001914 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001915 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 return NULL;
1917 }
1918 }
1919 lp->ll_name = lp->ll_exp_name;
1920 }
1921 else
1922 lp->ll_name = name;
1923
1924 /* Without [idx] or .key we are done. */
1925 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1926 return p;
1927
1928 cc = *p;
1929 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001930 /* Only pass &ht when we would write to the variable, it prevents autoload
1931 * as well. */
1932 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1933 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001935 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 if (v == NULL)
1938 return NULL;
1939
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 /*
1941 * Loop until no more [idx] or .key is following.
1942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001943 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001944 var1.v_type = VAR_UNKNOWN;
1945 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1949 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001950 && lp->ll_tv->vval.v_dict != NULL)
1951 && !(lp->ll_tv->v_type == VAR_BLOB
1952 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001955 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001961 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001964
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 len = -1;
1966 if (*p == '.')
1967 {
1968 key = p + 1;
1969 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1970 ;
1971 if (len == 0)
1972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001974 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001976 }
1977 p = key + len;
1978 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001979 else
1980 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001981 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001982 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001983 if (*p == ':')
1984 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001985 else
1986 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001987 empty1 = FALSE;
1988 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001989 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001990 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001991 {
1992 /* not a number or string */
1993 clear_tv(&var1);
1994 return NULL;
1995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 }
1997
1998 /* Optionally get the second index [ :expr]. */
1999 if (*p == ':')
2000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002005 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002006 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002007 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002008 if (rettv != NULL
2009 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002010 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002011 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002012 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002015 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002016 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 }
2019 p = skipwhite(p + 1);
2020 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 else
2023 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2026 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002027 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002030 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002031 {
2032 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002033 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002034 clear_tv(&var2);
2035 return NULL;
2036 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002037 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002042
Bram Moolenaar8c711452005-01-14 21:53:12 +00002043 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002046 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002047 clear_tv(&var1);
2048 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 }
2051
2052 /* Skip to past ']'. */
2053 ++p;
2054 }
2055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002057 {
2058 if (len == -1)
2059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002062 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002063 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 }
2067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 lp->ll_list = NULL;
2069 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002070 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002071
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002072 /* When assigning to a scope dictionary check that a function and
2073 * variable name is valid (only variable name unless it is l: or
2074 * g: dictionary). Disallow overwriting a builtin function. */
2075 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002076 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002077 int prevval;
2078 int wrong;
2079
2080 if (len != -1)
2081 {
2082 prevval = key[len];
2083 key[len] = NUL;
2084 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002085 else
2086 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002087 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2088 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002089 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002090 || !valid_varname(key);
2091 if (len != -1)
2092 key[len] = prevval;
2093 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002094 return NULL;
2095 }
2096
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002098 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 /* Can't add "v:" variable. */
2100 if (lp->ll_dict == &vimvardict)
2101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002102 semsg(_(e_illvar), name);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002103 return NULL;
2104 }
2105
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002106 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002111 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 }
2114 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002118 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 p = NULL;
2121 break;
2122 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002123 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002124 else if ((flags & GLV_READ_ONLY) == 0
2125 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002126 {
2127 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002128 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002129 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002130
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002131 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002134 else if (lp->ll_tv->v_type == VAR_BLOB)
2135 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002136 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2137
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002138 /*
2139 * Get the number and item for the only or first index of the List.
2140 */
2141 if (empty1)
2142 lp->ll_n1 = 0;
2143 else
2144 // is number or string
2145 lp->ll_n1 = (long)tv_get_number(&var1);
2146 clear_tv(&var1);
2147
2148 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002149 || lp->ll_n1 > bloblen
2150 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002151 {
2152 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002153 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002154 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002155 return NULL;
2156 }
2157 if (lp->ll_range && !lp->ll_empty2)
2158 {
2159 lp->ll_n2 = (long)tv_get_number(&var2);
2160 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002161 if (lp->ll_n2 < 0
2162 || lp->ll_n2 >= bloblen
2163 || lp->ll_n2 < lp->ll_n1)
2164 {
2165 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002167 return NULL;
2168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002169 }
2170 lp->ll_blob = lp->ll_tv->vval.v_blob;
2171 lp->ll_tv = NULL;
2172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002173 else
2174 {
2175 /*
2176 * Get the number and item for the only or first index of the List.
2177 */
2178 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002181 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002182 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002183 clear_tv(&var1);
2184
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 lp->ll_list = lp->ll_tv->vval.v_list;
2187 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2188 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002190 if (lp->ll_n1 < 0)
2191 {
2192 lp->ll_n1 = 0;
2193 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2194 }
2195 }
2196 if (lp->ll_li == NULL)
2197 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002198 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002199 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002200 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002202 }
2203
2204 /*
2205 * May need to find the item or absolute index for the second
2206 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 * When no index given: "lp->ll_empty2" is TRUE.
2208 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002209 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002212 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002213 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002219 {
2220 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002221 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002223 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 }
2226
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2228 if (lp->ll_n1 < 0)
2229 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2230 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002231 {
2232 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002233 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002235 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002236 }
2237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 }
2241
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002242 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 return p;
2244}
2245
2246/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002247 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251{
2252 vim_free(lp->ll_exp_name);
2253 vim_free(lp->ll_newkey);
2254}
2255
2256/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002257 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002259 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 */
2261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002262set_var_lval(
2263 lval_T *lp,
2264 char_u *endp,
2265 typval_T *rettv,
2266 int copy,
2267 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268{
2269 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002270 listitem_T *ri;
2271 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272
2273 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002275 cc = *endp;
2276 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002277 if (lp->ll_blob != NULL)
2278 {
2279 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002280
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002281 if (op != NULL && *op != '=')
2282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002284 return;
2285 }
2286
2287 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2288 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002289 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002290
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002291 if (lp->ll_empty2)
2292 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2293
2294 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002296 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002297 return;
2298 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002299 if (lp->ll_empty2)
2300 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002301
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002302 ir = 0;
2303 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2304 blob_set(lp->ll_blob, il,
2305 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002306 }
2307 else
2308 {
2309 val = (int)tv_get_number_chk(rettv, &error);
2310 if (!error)
2311 {
2312 garray_T *gap = &lp->ll_blob->bv_ga;
2313
2314 // Allow for appending a byte. Setting a byte beyond
2315 // the end is an error otherwise.
2316 if (lp->ll_n1 < gap->ga_len
2317 || (lp->ll_n1 == gap->ga_len
2318 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2319 {
2320 blob_set(lp->ll_blob, lp->ll_n1, val);
2321 if (lp->ll_n1 == gap->ga_len)
2322 ++gap->ga_len;
2323 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002324 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002325 }
2326 }
2327 }
2328 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002330 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331
Bram Moolenaar79518e22017-02-17 16:31:35 +01002332 /* handle +=, -= and .= */
2333 di = NULL;
2334 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2335 &tv, &di, TRUE, FALSE) == OK)
2336 {
2337 if ((di == NULL
2338 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2339 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2340 FALSE)))
2341 && tv_op(&tv, rettv, op) == OK)
2342 set_var(lp->ll_name, &tv, FALSE);
2343 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002346 else
2347 set_var(lp->ll_name, rettv, copy);
2348 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002350 else if (tv_check_lock(lp->ll_newkey == NULL
2351 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002352 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002353 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 else if (lp->ll_range)
2355 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002356 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002357 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002358
2359 /*
2360 * Check whether any of the list items is locked
2361 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002362 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002363 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002364 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002365 return;
2366 ri = ri->li_next;
2367 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2368 break;
2369 ll_li = ll_li->li_next;
2370 ++ll_n1;
2371 }
2372
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 /*
2374 * Assign the List values to the list items.
2375 */
2376 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 if (op != NULL && *op != '=')
2379 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2380 else
2381 {
2382 clear_tv(&lp->ll_li->li_tv);
2383 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2384 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 ri = ri->li_next;
2386 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2387 break;
2388 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002391 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 ri = NULL;
2394 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002395 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002396 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 lp->ll_li = lp->ll_li->li_next;
2398 ++lp->ll_n1;
2399 }
2400 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002401 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 else if (lp->ll_empty2
2403 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 }
2407 else
2408 {
2409 /*
2410 * Assign to a List or Dictionary item.
2411 */
2412 if (lp->ll_newkey != NULL)
2413 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 if (op != NULL && *op != '=')
2415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002416 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 return;
2418 }
2419
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002421 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 if (di == NULL)
2423 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002424 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2425 {
2426 vim_free(di);
2427 return;
2428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002430 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 else if (op != NULL && *op != '=')
2432 {
2433 tv_op(lp->ll_tv, rettv, op);
2434 return;
2435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 /*
2440 * Assign the value to the variable or list item.
2441 */
2442 if (copy)
2443 copy_tv(rettv, lp->ll_tv);
2444 else
2445 {
2446 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002447 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451}
2452
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002453/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2455 * Returns OK or FAIL.
2456 */
2457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002458tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002460 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 char_u numbuf[NUMBUFLEN];
2462 char_u *s;
2463
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002464 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2465 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2466 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 {
2468 switch (tv1->v_type)
2469 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002470 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 case VAR_DICT:
2472 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002473 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002474 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002475 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002476 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 break;
2478
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002479 case VAR_BLOB:
2480 if (*op != '+' || tv2->v_type != VAR_BLOB)
2481 break;
2482 // BLOB += BLOB
2483 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2484 {
2485 blob_T *b1 = tv1->vval.v_blob;
2486 blob_T *b2 = tv2->vval.v_blob;
2487 int i, len = blob_len(b2);
2488 for (i = 0; i < len; i++)
2489 ga_append(&b1->bv_ga, blob_get(b2, i));
2490 }
2491 return OK;
2492
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 case VAR_LIST:
2494 if (*op != '+' || tv2->v_type != VAR_LIST)
2495 break;
2496 /* List += List */
2497 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2498 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2499 return OK;
2500
2501 case VAR_NUMBER:
2502 case VAR_STRING:
2503 if (tv2->v_type == VAR_LIST)
2504 break;
2505 if (*op == '+' || *op == '-')
2506 {
2507 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002508 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002509#ifdef FEAT_FLOAT
2510 if (tv2->v_type == VAR_FLOAT)
2511 {
2512 float_T f = n;
2513
2514 if (*op == '+')
2515 f += tv2->vval.v_float;
2516 else
2517 f -= tv2->vval.v_float;
2518 clear_tv(tv1);
2519 tv1->v_type = VAR_FLOAT;
2520 tv1->vval.v_float = f;
2521 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002523#endif
2524 {
2525 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002526 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002527 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002528 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002529 clear_tv(tv1);
2530 tv1->v_type = VAR_NUMBER;
2531 tv1->vval.v_number = n;
2532 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 }
2534 else
2535 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002536 if (tv2->v_type == VAR_FLOAT)
2537 break;
2538
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 s = tv_get_string(tv1);
2541 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 clear_tv(tv1);
2543 tv1->v_type = VAR_STRING;
2544 tv1->vval.v_string = s;
2545 }
2546 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002547
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002548 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002549#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002550 {
2551 float_T f;
2552
2553 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2554 && tv2->v_type != VAR_NUMBER
2555 && tv2->v_type != VAR_STRING))
2556 break;
2557 if (tv2->v_type == VAR_FLOAT)
2558 f = tv2->vval.v_float;
2559 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002560 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002561 if (*op == '+')
2562 tv1->vval.v_float += f;
2563 else
2564 tv1->vval.v_float -= f;
2565 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002566#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002567 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 }
2569 }
2570
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002571 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002572 return FAIL;
2573}
2574
2575/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002576 * Evaluate the expression used in a ":for var in expr" command.
2577 * "arg" points to "var".
2578 * Set "*errp" to TRUE for an error, FALSE otherwise;
2579 * Return a pointer that holds the info. Null when there is an error.
2580 */
2581 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002582eval_for_line(
2583 char_u *arg,
2584 int *errp,
2585 char_u **nextcmdp,
2586 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002587{
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002589 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 typval_T tv;
2591 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002592 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002593
2594 *errp = TRUE; /* default: there is an error */
2595
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002597 if (fi == NULL)
2598 return NULL;
2599
2600 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2601 if (expr == NULL)
2602 return fi;
2603
2604 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002605 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002608 return fi;
2609 }
2610
2611 if (skip)
2612 ++emsg_skip;
2613 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2614 {
2615 *errp = FALSE;
2616 if (!skip)
2617 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002618 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002619 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002620 l = tv.vval.v_list;
2621 if (l == NULL)
2622 {
2623 // a null list is like an empty list: do nothing
2624 clear_tv(&tv);
2625 }
2626 else
2627 {
2628 // No need to increment the refcount, it's already set for
2629 // the list being used in "tv".
2630 fi->fi_list = l;
2631 list_add_watch(l, &fi->fi_lw);
2632 fi->fi_lw.lw_item = l->lv_first;
2633 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002634 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002635 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002636 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002637 b = tv.vval.v_blob;
2638 if (b == NULL)
2639 clear_tv(&tv);
2640 else
2641 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002642 // No need to increment the refcount, it's already set for
2643 // the blob being used in "tv".
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002644 fi->fi_blob = b;
2645 fi->fi_bi = 0;
2646 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002647 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002648 else
2649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002650 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002651 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002652 }
2653 }
2654 }
2655 if (skip)
2656 --emsg_skip;
2657
2658 return fi;
2659}
2660
2661/*
2662 * Use the first item in a ":for" list. Advance to the next.
2663 * Assign the values to the variable (list). "arg" points to the first one.
2664 * Return TRUE when a valid item was found, FALSE when at end of list or
2665 * something wrong.
2666 */
2667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002668next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002670 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002671 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002673
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002674 if (fi->fi_blob != NULL)
2675 {
2676 typval_T tv;
2677
2678 if (fi->fi_bi >= blob_len(fi->fi_blob))
2679 return FALSE;
2680 tv.v_type = VAR_NUMBER;
2681 tv.v_lock = VAR_FIXED;
2682 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2683 ++fi->fi_bi;
2684 return ex_let_vars(arg, &tv, TRUE,
2685 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2686 }
2687
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002688 item = fi->fi_lw.lw_item;
2689 if (item == NULL)
2690 result = FALSE;
2691 else
2692 {
2693 fi->fi_lw.lw_item = item->li_next;
2694 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2695 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2696 }
2697 return result;
2698}
2699
2700/*
2701 * Free the structure used to store info used by ":for".
2702 */
2703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002704free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002705{
Bram Moolenaar33570922005-01-25 22:26:29 +00002706 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002708 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002709 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002711 list_unref(fi->fi_list);
2712 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002713 if (fi != NULL && fi->fi_blob != NULL)
2714 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002715 vim_free(fi);
2716}
2717
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2719
2720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002721set_context_for_expression(
2722 expand_T *xp,
2723 char_u *arg,
2724 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 int got_eq = FALSE;
2727 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002728 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002730 if (cmdidx == CMD_let)
2731 {
2732 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002733 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 {
2735 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002736 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737 {
2738 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002739 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002740 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002741 break;
2742 }
2743 return;
2744 }
2745 }
2746 else
2747 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2748 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 while ((xp->xp_pattern = vim_strpbrk(arg,
2750 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2751 {
2752 c = *xp->xp_pattern;
2753 if (c == '&')
2754 {
2755 c = xp->xp_pattern[1];
2756 if (c == '&')
2757 {
2758 ++xp->xp_pattern;
2759 xp->xp_context = cmdidx != CMD_let || got_eq
2760 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2761 }
2762 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002765 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2766 xp->xp_pattern += 2;
2767
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 else if (c == '$')
2771 {
2772 /* environment variable */
2773 xp->xp_context = EXPAND_ENV_VARS;
2774 }
2775 else if (c == '=')
2776 {
2777 got_eq = TRUE;
2778 xp->xp_context = EXPAND_EXPRESSION;
2779 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002780 else if (c == '#'
2781 && xp->xp_context == EXPAND_EXPRESSION)
2782 {
2783 /* Autoload function/variable contains '#'. */
2784 break;
2785 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002786 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 && xp->xp_context == EXPAND_FUNCTIONS
2788 && vim_strchr(xp->xp_pattern, '(') == NULL)
2789 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002790 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 break;
2792 }
2793 else if (cmdidx != CMD_let || got_eq)
2794 {
2795 if (c == '"') /* string */
2796 {
2797 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2798 if (c == '\\' && xp->xp_pattern[1] != NUL)
2799 ++xp->xp_pattern;
2800 xp->xp_context = EXPAND_NOTHING;
2801 }
2802 else if (c == '\'') /* literal string */
2803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2806 /* skip */ ;
2807 xp->xp_context = EXPAND_NOTHING;
2808 }
2809 else if (c == '|')
2810 {
2811 if (xp->xp_pattern[1] == '|')
2812 {
2813 ++xp->xp_pattern;
2814 xp->xp_context = EXPAND_EXPRESSION;
2815 }
2816 else
2817 xp->xp_context = EXPAND_COMMANDS;
2818 }
2819 else
2820 xp->xp_context = EXPAND_EXPRESSION;
2821 }
2822 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823 /* Doesn't look like something valid, expand as an expression
2824 * anyway. */
2825 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 arg = xp->xp_pattern;
2827 if (*arg != NUL)
2828 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2829 /* skip */ ;
2830 }
2831 xp->xp_pattern = arg;
2832}
2833
2834#endif /* FEAT_CMDL_COMPL */
2835
2836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 * ":unlet[!] var1 ... " command.
2838 */
2839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002840ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002842 ex_unletlock(eap, eap->arg, 0);
2843}
2844
2845/*
2846 * ":lockvar" and ":unlockvar" commands
2847 */
2848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002849ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002850{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002852 int deep = 2;
2853
2854 if (eap->forceit)
2855 deep = -1;
2856 else if (vim_isdigit(*arg))
2857 {
2858 deep = getdigits(&arg);
2859 arg = skipwhite(arg);
2860 }
2861
2862 ex_unletlock(eap, arg, deep);
2863}
2864
2865/*
2866 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2867 */
2868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869ex_unletlock(
2870 exarg_T *eap,
2871 char_u *argstart,
2872 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002873{
2874 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002877 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 do
2880 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002881 if (*arg == '$')
2882 {
2883 char_u *name = ++arg;
2884
2885 if (get_env_len(&arg) == 0)
2886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002887 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002888 return;
2889 }
2890 vim_unsetenv(name);
2891 arg = skipwhite(arg);
2892 continue;
2893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002896 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002897 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 if (lv.ll_name == NULL)
2899 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002900 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (name_end != NULL)
2904 {
2905 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 }
2908 if (!(eap->skip || error))
2909 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 break;
2911 }
2912
2913 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 {
2915 if (eap->cmdidx == CMD_unlet)
2916 {
2917 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2918 error = TRUE;
2919 }
2920 else
2921 {
2922 if (do_lock_var(&lv, name_end, deep,
2923 eap->cmdidx == CMD_lockvar) == FAIL)
2924 error = TRUE;
2925 }
2926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 if (!eap->skip)
2929 clear_lval(&lv);
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 arg = skipwhite(name_end);
2932 } while (!ends_excmd(*arg));
2933
2934 eap->nextcmd = check_nextcmd(arg);
2935}
2936
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938do_unlet_var(
2939 lval_T *lp,
2940 char_u *name_end,
2941 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002942{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 int ret = OK;
2944 int cc;
2945
2946 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 cc = *name_end;
2949 *name_end = NUL;
2950
2951 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002952 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002955 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002956 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002957 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002958 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002959 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 else if (lp->ll_range)
2962 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002963 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002964 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002965 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002966
2967 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2968 {
2969 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002970 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 return FAIL;
2972 ll_li = li;
2973 ++ll_n1;
2974 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975
2976 /* Delete a range of List items. */
2977 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2978 {
2979 li = lp->ll_li->li_next;
2980 listitem_remove(lp->ll_list, lp->ll_li);
2981 lp->ll_li = li;
2982 ++lp->ll_n1;
2983 }
2984 }
2985 else
2986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* unlet a List item. */
2989 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002992 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 }
2994
2995 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996}
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
2999 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003000 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 */
3002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003003do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 hashtab_T *ht;
3006 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003007 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003008 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003009 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 ht = find_var_ht(name, &varname);
3012 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003015 if (d == NULL)
3016 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017 if (ht == &globvarht)
3018 d = &globvardict;
3019 else if (ht == &compat_hashtab)
3020 d = &vimvardict;
3021 else
3022 {
3023 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3024 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3025 }
3026 if (d == NULL)
3027 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003028 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 return FAIL;
3030 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003033 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003034 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003035 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003036 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003037 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003038 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003039 || var_check_ro(di->di_flags, name, FALSE)
3040 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003041 return FAIL;
3042
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003043 delete_var(ht, hi);
3044 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003047 if (forceit)
3048 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003049 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 return FAIL;
3051}
3052
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003053/*
3054 * Lock or unlock variable indicated by "lp".
3055 * "deep" is the levels to go (-1 for unlimited);
3056 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3057 */
3058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059do_lock_var(
3060 lval_T *lp,
3061 char_u *name_end,
3062 int deep,
3063 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003064{
3065 int ret = OK;
3066 int cc;
3067 dictitem_T *di;
3068
3069 if (deep == 0) /* nothing to do */
3070 return OK;
3071
3072 if (lp->ll_tv == NULL)
3073 {
3074 cc = *name_end;
3075 *name_end = NUL;
3076
3077 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003078 di = find_var(lp->ll_name, NULL, TRUE);
3079 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003080 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003081 else if ((di->di_flags & DI_FLAGS_FIX)
3082 && di->di_tv.v_type != VAR_DICT
3083 && di->di_tv.v_type != VAR_LIST)
3084 /* For historic reasons this error is not given for a list or dict.
3085 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003086 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003087 else
3088 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003089 if (lock)
3090 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003091 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003092 di->di_flags &= ~DI_FLAGS_LOCK;
3093 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003094 }
3095 *name_end = cc;
3096 }
3097 else if (lp->ll_range)
3098 {
3099 listitem_T *li = lp->ll_li;
3100
3101 /* (un)lock a range of List items. */
3102 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3103 {
3104 item_lock(&li->li_tv, deep, lock);
3105 li = li->li_next;
3106 ++lp->ll_n1;
3107 }
3108 }
3109 else if (lp->ll_list != NULL)
3110 /* (un)lock a List item. */
3111 item_lock(&lp->ll_li->li_tv, deep, lock);
3112 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003113 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114 item_lock(&lp->ll_di->di_tv, deep, lock);
3115
3116 return ret;
3117}
3118
3119/*
3120 * Lock or unlock an item. "deep" is nr of levels to go.
3121 */
3122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003123item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124{
3125 static int recurse = 0;
3126 list_T *l;
3127 listitem_T *li;
3128 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003129 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003130 hashitem_T *hi;
3131 int todo;
3132
3133 if (recurse >= DICT_MAXNEST)
3134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003135 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003136 return;
3137 }
3138 if (deep == 0)
3139 return;
3140 ++recurse;
3141
3142 /* lock/unlock the item itself */
3143 if (lock)
3144 tv->v_lock |= VAR_LOCKED;
3145 else
3146 tv->v_lock &= ~VAR_LOCKED;
3147
3148 switch (tv->v_type)
3149 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003150 case VAR_UNKNOWN:
3151 case VAR_NUMBER:
3152 case VAR_STRING:
3153 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003154 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003155 case VAR_FLOAT:
3156 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003157 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003158 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003159 break;
3160
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003161 case VAR_BLOB:
3162 if ((b = tv->vval.v_blob) != NULL)
3163 {
3164 if (lock)
3165 b->bv_lock |= VAR_LOCKED;
3166 else
3167 b->bv_lock &= ~VAR_LOCKED;
3168 }
3169 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 case VAR_LIST:
3171 if ((l = tv->vval.v_list) != NULL)
3172 {
3173 if (lock)
3174 l->lv_lock |= VAR_LOCKED;
3175 else
3176 l->lv_lock &= ~VAR_LOCKED;
3177 if (deep < 0 || deep > 1)
3178 /* recursive: lock/unlock the items the List contains */
3179 for (li = l->lv_first; li != NULL; li = li->li_next)
3180 item_lock(&li->li_tv, deep - 1, lock);
3181 }
3182 break;
3183 case VAR_DICT:
3184 if ((d = tv->vval.v_dict) != NULL)
3185 {
3186 if (lock)
3187 d->dv_lock |= VAR_LOCKED;
3188 else
3189 d->dv_lock &= ~VAR_LOCKED;
3190 if (deep < 0 || deep > 1)
3191 {
3192 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003193 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003194 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3195 {
3196 if (!HASHITEM_EMPTY(hi))
3197 {
3198 --todo;
3199 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3200 }
3201 }
3202 }
3203 }
3204 }
3205 --recurse;
3206}
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3209/*
3210 * Delete all "menutrans_" variables.
3211 */
3212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003216 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003219 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003221 {
3222 if (!HASHITEM_EMPTY(hi))
3223 {
3224 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3226 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003227 }
3228 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231#endif
3232
3233#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3234
3235/*
3236 * Local string buffer for the next two functions to store a variable name
3237 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3238 * get_user_var_name().
3239 */
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241static char_u *varnamebuf = NULL;
3242static int varnamebuflen = 0;
3243
3244/*
3245 * Function to concatenate a prefix and a variable name.
3246 */
3247 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 int len;
3251
3252 len = (int)STRLEN(name) + 3;
3253 if (len > varnamebuflen)
3254 {
3255 vim_free(varnamebuf);
3256 len += 10; /* some additional space */
3257 varnamebuf = alloc(len);
3258 if (varnamebuf == NULL)
3259 {
3260 varnamebuflen = 0;
3261 return NULL;
3262 }
3263 varnamebuflen = len;
3264 }
3265 *varnamebuf = prefix;
3266 varnamebuf[1] = ':';
3267 STRCPY(varnamebuf + 2, name);
3268 return varnamebuf;
3269}
3270
3271/*
3272 * Function given to ExpandGeneric() to obtain the list of user defined
3273 * (global/buffer/window/built-in) variable names.
3274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003276get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003278 static long_u gdone;
3279 static long_u bdone;
3280 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003281 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003282 static int vidx;
3283 static hashitem_T *hi;
3284 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
3286 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003287 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003288 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003289 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003291
3292 /* Global variables */
3293 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003295 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 hi = globvarht.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 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3302 return cat_prefix_varname('g', hi->hi_key);
3303 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003305
3306 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003307 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003310 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003311 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003312 else
3313 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003314 while (HASHITEM_EMPTY(hi))
3315 ++hi;
3316 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003318
3319 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003320 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003323 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003325 else
3326 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003327 while (HASHITEM_EMPTY(hi))
3328 ++hi;
3329 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003331
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003332 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003333 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003334 if (tdone < ht->ht_used)
3335 {
3336 if (tdone++ == 0)
3337 hi = ht->ht_array;
3338 else
3339 ++hi;
3340 while (HASHITEM_EMPTY(hi))
3341 ++hi;
3342 return cat_prefix_varname('t', hi->hi_key);
3343 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003344
Bram Moolenaar33570922005-01-25 22:26:29 +00003345 /* v: variables */
3346 if (vidx < VV_LEN)
3347 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaard23a8232018-02-10 18:45:26 +01003349 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 varnamebuflen = 0;
3351 return NULL;
3352}
3353
3354#endif /* FEAT_CMDL_COMPL */
3355
3356/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003357 * Return TRUE if "pat" matches "text".
3358 * Does not use 'cpo' and always uses 'magic'.
3359 */
3360 static int
3361pattern_match(char_u *pat, char_u *text, int ic)
3362{
3363 int matches = FALSE;
3364 char_u *save_cpo;
3365 regmatch_T regmatch;
3366
3367 /* avoid 'l' flag in 'cpoptions' */
3368 save_cpo = p_cpo;
3369 p_cpo = (char_u *)"";
3370 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3371 if (regmatch.regprog != NULL)
3372 {
3373 regmatch.rm_ic = ic;
3374 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3375 vim_regfree(regmatch.regprog);
3376 }
3377 p_cpo = save_cpo;
3378 return matches;
3379}
3380
3381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3385 */
3386
3387/*
3388 * Handle zero level expression.
3389 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003391 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 * Return OK or FAIL.
3393 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003395eval0(
3396 char_u *arg,
3397 typval_T *rettv,
3398 char_u **nextcmd,
3399 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 int ret;
3402 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003403 int did_emsg_before = did_emsg;
3404 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (ret == FAIL || !ends_excmd(*p))
3409 {
3410 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003411 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 /*
3413 * Report the invalid expression unless the expression evaluation has
3414 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003415 * exception, or we already gave a more specific error.
3416 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003418 if (!aborting() && did_emsg == did_emsg_before
3419 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003420 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 ret = FAIL;
3422 }
3423 if (nextcmd != NULL)
3424 *nextcmd = check_nextcmd(p);
3425
3426 return ret;
3427}
3428
3429/*
3430 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003431 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 *
3433 * "arg" must point to the first non-white of the expression.
3434 * "arg" is advanced to the next non-white after the recognized expression.
3435 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003436 * Note: "rettv.v_lock" is not set.
3437 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 * Return OK or FAIL.
3439 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003441eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446 /*
3447 * Get the first variable.
3448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 return FAIL;
3451
3452 if ((*arg)[0] == '?')
3453 {
3454 result = FALSE;
3455 if (evaluate)
3456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003457 int error = FALSE;
3458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003459 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003461 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003462 if (error)
3463 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465
3466 /*
3467 * Get the second variable.
3468 */
3469 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
3472
3473 /*
3474 * Check for the ":".
3475 */
3476 if ((*arg)[0] != ':')
3477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003478 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 return FAIL;
3482 }
3483
3484 /*
3485 * Get the third variable.
3486 */
3487 *arg = skipwhite(*arg + 1);
3488 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3489 {
3490 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 return FAIL;
3493 }
3494 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497
3498 return OK;
3499}
3500
3501/*
3502 * Handle first level expression:
3503 * expr2 || expr2 || expr2 logical OR
3504 *
3505 * "arg" must point to the first non-white of the expression.
3506 * "arg" is advanced to the next non-white after the recognized expression.
3507 *
3508 * Return OK or FAIL.
3509 */
3510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003511eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar33570922005-01-25 22:26:29 +00003513 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 long result;
3515 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003516 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
3518 /*
3519 * Get the first variable.
3520 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003521 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return FAIL;
3523
3524 /*
3525 * Repeat until there is no following "||".
3526 */
3527 first = TRUE;
3528 result = FALSE;
3529 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3530 {
3531 if (evaluate && first)
3532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003533 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003536 if (error)
3537 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 first = FALSE;
3539 }
3540
3541 /*
3542 * Get the second variable.
3543 */
3544 *arg = skipwhite(*arg + 2);
3545 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3546 return FAIL;
3547
3548 /*
3549 * Compute the result.
3550 */
3551 if (evaluate && !result)
3552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003553 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 if (error)
3557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 if (evaluate)
3560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003561 rettv->v_type = VAR_NUMBER;
3562 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 }
3565
3566 return OK;
3567}
3568
3569/*
3570 * Handle second level expression:
3571 * expr3 && expr3 && expr3 logical AND
3572 *
3573 * "arg" must point to the first non-white of the expression.
3574 * "arg" is advanced to the next non-white after the recognized expression.
3575 *
3576 * Return OK or FAIL.
3577 */
3578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
Bram Moolenaar33570922005-01-25 22:26:29 +00003581 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 long result;
3583 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 /*
3587 * Get the first variable.
3588 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 return FAIL;
3591
3592 /*
3593 * Repeat until there is no following "&&".
3594 */
3595 first = TRUE;
3596 result = TRUE;
3597 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3598 {
3599 if (evaluate && first)
3600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003601 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003603 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 if (error)
3605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 first = FALSE;
3607 }
3608
3609 /*
3610 * Get the second variable.
3611 */
3612 *arg = skipwhite(*arg + 2);
3613 if (eval4(arg, &var2, evaluate && result) == FAIL)
3614 return FAIL;
3615
3616 /*
3617 * Compute the result.
3618 */
3619 if (evaluate && result)
3620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003621 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 if (error)
3625 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 if (evaluate)
3628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003629 rettv->v_type = VAR_NUMBER;
3630 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632 }
3633
3634 return OK;
3635}
3636
3637/*
3638 * Handle third level expression:
3639 * var1 == var2
3640 * var1 =~ var2
3641 * var1 != var2
3642 * var1 !~ var2
3643 * var1 > var2
3644 * var1 >= var2
3645 * var1 < var2
3646 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003647 * var1 is var2
3648 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 *
3650 * "arg" must point to the first non-white of the expression.
3651 * "arg" is advanced to the next non-white after the recognized expression.
3652 *
3653 * Return OK or FAIL.
3654 */
3655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 char_u *p;
3660 int i;
3661 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003662 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 /*
3667 * Get the first variable.
3668 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671
3672 p = *arg;
3673 switch (p[0])
3674 {
3675 case '=': if (p[1] == '=')
3676 type = TYPE_EQUAL;
3677 else if (p[1] == '~')
3678 type = TYPE_MATCH;
3679 break;
3680 case '!': if (p[1] == '=')
3681 type = TYPE_NEQUAL;
3682 else if (p[1] == '~')
3683 type = TYPE_NOMATCH;
3684 break;
3685 case '>': if (p[1] != '=')
3686 {
3687 type = TYPE_GREATER;
3688 len = 1;
3689 }
3690 else
3691 type = TYPE_GEQUAL;
3692 break;
3693 case '<': if (p[1] != '=')
3694 {
3695 type = TYPE_SMALLER;
3696 len = 1;
3697 }
3698 else
3699 type = TYPE_SEQUAL;
3700 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003701 case 'i': if (p[1] == 's')
3702 {
3703 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3704 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003705 i = p[len];
3706 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003707 {
3708 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3709 type_is = TRUE;
3710 }
3711 }
3712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714
3715 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003716 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
3718 if (type != TYPE_UNKNOWN)
3719 {
3720 /* extra question mark appended: ignore case */
3721 if (p[len] == '?')
3722 {
3723 ic = TRUE;
3724 ++len;
3725 }
3726 /* extra '#' appended: match case */
3727 else if (p[len] == '#')
3728 {
3729 ic = FALSE;
3730 ++len;
3731 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003732 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 else
3734 ic = p_ic;
3735
3736 /*
3737 * Get the second variable.
3738 */
3739 *arg = skipwhite(p + len);
3740 if (eval5(arg, &var2, evaluate) == FAIL)
3741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 return FAIL;
3744 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003745 if (evaluate)
3746 {
3747 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3748
3749 clear_tv(&var2);
3750 return ret;
3751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753
3754 return OK;
3755}
3756
3757/*
3758 * Handle fourth level expression:
3759 * + number addition
3760 * - number subtraction
3761 * . string concatenation
3762 *
3763 * "arg" must point to the first non-white of the expression.
3764 * "arg" is advanced to the next non-white after the recognized expression.
3765 *
3766 * Return OK or FAIL.
3767 */
3768 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 typval_T var2;
3772 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003774 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775#ifdef FEAT_FLOAT
3776 float_T f1 = 0, f2 = 0;
3777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 char_u *s1, *s2;
3779 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3780 char_u *p;
3781
3782 /*
3783 * Get the first variable.
3784 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003785 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 /*
3789 * Repeat computing, until no '+', '-' or '.' is following.
3790 */
3791 for (;;)
3792 {
3793 op = **arg;
3794 if (op != '+' && op != '-' && op != '.')
3795 break;
3796
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003797 if ((op != '+' || (rettv->v_type != VAR_LIST
3798 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003799#ifdef FEAT_FLOAT
3800 && (op == '.' || rettv->v_type != VAR_FLOAT)
3801#endif
3802 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003803 {
3804 /* For "list + ...", an illegal use of the first operand as
3805 * a number cannot be determined before evaluating the 2nd
3806 * operand: if this is also a list, all is ok.
3807 * For "something . ...", "something - ..." or "non-list + ...",
3808 * we know that the first operand needs to be a string or number
3809 * without evaluating the 2nd operand. So check before to avoid
3810 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003811 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 {
3813 clear_tv(rettv);
3814 return FAIL;
3815 }
3816 }
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 /*
3819 * Get the second variable.
3820 */
3821 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003822 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826 }
3827
3828 if (evaluate)
3829 {
3830 /*
3831 * Compute the result.
3832 */
3833 if (op == '.')
3834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003835 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3836 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 if (s2 == NULL) /* type error ? */
3838 {
3839 clear_tv(rettv);
3840 clear_tv(&var2);
3841 return FAIL;
3842 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003843 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003844 clear_tv(rettv);
3845 rettv->v_type = VAR_STRING;
3846 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003848 else if (op == '+' && rettv->v_type == VAR_BLOB
3849 && var2.v_type == VAR_BLOB)
3850 {
3851 blob_T *b1 = rettv->vval.v_blob;
3852 blob_T *b2 = var2.vval.v_blob;
3853 blob_T *b = blob_alloc();
3854 int i;
3855
3856 if (b != NULL)
3857 {
3858 for (i = 0; i < blob_len(b1); i++)
3859 ga_append(&b->bv_ga, blob_get(b1, i));
3860 for (i = 0; i < blob_len(b2); i++)
3861 ga_append(&b->bv_ga, blob_get(b2, i));
3862
3863 clear_tv(rettv);
3864 rettv_blob_set(rettv, b);
3865 }
3866 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003867 else if (op == '+' && rettv->v_type == VAR_LIST
3868 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003869 {
3870 /* concatenate Lists */
3871 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3872 &var3) == FAIL)
3873 {
3874 clear_tv(rettv);
3875 clear_tv(&var2);
3876 return FAIL;
3877 }
3878 clear_tv(rettv);
3879 *rettv = var3;
3880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 else
3882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 int error = FALSE;
3884
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003885#ifdef FEAT_FLOAT
3886 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888 f1 = rettv->vval.v_float;
3889 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003890 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003891 else
3892#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003893 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003894 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003895 if (error)
3896 {
3897 /* This can only happen for "list + non-list". For
3898 * "non-list + ..." or "something - ...", we returned
3899 * before evaluating the 2nd operand. */
3900 clear_tv(rettv);
3901 return FAIL;
3902 }
3903#ifdef FEAT_FLOAT
3904 if (var2.v_type == VAR_FLOAT)
3905 f1 = n1;
3906#endif
3907 }
3908#ifdef FEAT_FLOAT
3909 if (var2.v_type == VAR_FLOAT)
3910 {
3911 f2 = var2.vval.v_float;
3912 n2 = 0;
3913 }
3914 else
3915#endif
3916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003917 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003918 if (error)
3919 {
3920 clear_tv(rettv);
3921 clear_tv(&var2);
3922 return FAIL;
3923 }
3924#ifdef FEAT_FLOAT
3925 if (rettv->v_type == VAR_FLOAT)
3926 f2 = n2;
3927#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003929 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003930
3931#ifdef FEAT_FLOAT
3932 /* If there is a float on either side the result is a float. */
3933 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3934 {
3935 if (op == '+')
3936 f1 = f1 + f2;
3937 else
3938 f1 = f1 - f2;
3939 rettv->v_type = VAR_FLOAT;
3940 rettv->vval.v_float = f1;
3941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003943#endif
3944 {
3945 if (op == '+')
3946 n1 = n1 + n2;
3947 else
3948 n1 = n1 - n2;
3949 rettv->v_type = VAR_NUMBER;
3950 rettv->vval.v_number = n1;
3951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 }
3956 return OK;
3957}
3958
3959/*
3960 * Handle fifth level expression:
3961 * * number multiplication
3962 * / number division
3963 * % number modulo
3964 *
3965 * "arg" must point to the first non-white of the expression.
3966 * "arg" is advanced to the next non-white after the recognized expression.
3967 *
3968 * Return OK or FAIL.
3969 */
3970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971eval6(
3972 char_u **arg,
3973 typval_T *rettv,
3974 int evaluate,
3975 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003979 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003980#ifdef FEAT_FLOAT
3981 int use_float = FALSE;
3982 float_T f1 = 0, f2;
3983#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 /*
3987 * Get the first variable.
3988 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003989 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 return FAIL;
3991
3992 /*
3993 * Repeat computing, until no '*', '/' or '%' is following.
3994 */
3995 for (;;)
3996 {
3997 op = **arg;
3998 if (op != '*' && op != '/' && op != '%')
3999 break;
4000
4001 if (evaluate)
4002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004003#ifdef FEAT_FLOAT
4004 if (rettv->v_type == VAR_FLOAT)
4005 {
4006 f1 = rettv->vval.v_float;
4007 use_float = TRUE;
4008 n1 = 0;
4009 }
4010 else
4011#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004012 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (error)
4015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 else
4018 n1 = 0;
4019
4020 /*
4021 * Get the second variable.
4022 */
4023 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004024 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 return FAIL;
4026
4027 if (evaluate)
4028 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004029#ifdef FEAT_FLOAT
4030 if (var2.v_type == VAR_FLOAT)
4031 {
4032 if (!use_float)
4033 {
4034 f1 = n1;
4035 use_float = TRUE;
4036 }
4037 f2 = var2.vval.v_float;
4038 n2 = 0;
4039 }
4040 else
4041#endif
4042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004043 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004044 clear_tv(&var2);
4045 if (error)
4046 return FAIL;
4047#ifdef FEAT_FLOAT
4048 if (use_float)
4049 f2 = n2;
4050#endif
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053 /*
4054 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004055 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004057#ifdef FEAT_FLOAT
4058 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 if (op == '*')
4061 f1 = f1 * f2;
4062 else if (op == '/')
4063 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004064# ifdef VMS
4065 /* VMS crashes on divide by zero, work around it */
4066 if (f2 == 0.0)
4067 {
4068 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004069 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004070 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004071 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004072 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004073 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004074 }
4075 else
4076 f1 = f1 / f2;
4077# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004078 /* We rely on the floating point library to handle divide
4079 * by zero to result in "inf" and not a crash. */
4080 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004081# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004085 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004086 return FAIL;
4087 }
4088 rettv->v_type = VAR_FLOAT;
4089 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004094 if (op == '*')
4095 n1 = n1 * n2;
4096 else if (op == '/')
4097 {
4098 if (n2 == 0) /* give an error message? */
4099 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004100 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004101 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004102 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004103 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004104 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004105 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004106 }
4107 else
4108 n1 = n1 / n2;
4109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004111 {
4112 if (n2 == 0) /* give an error message? */
4113 n1 = 0;
4114 else
4115 n1 = n1 % n2;
4116 }
4117 rettv->v_type = VAR_NUMBER;
4118 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121 }
4122
4123 return OK;
4124}
4125
4126/*
4127 * Handle sixth level expression:
4128 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004129 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004130 * "string" string constant
4131 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 * &option-name option value
4133 * @r register contents
4134 * identifier variable value
4135 * function() function call
4136 * $VAR environment variable
4137 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004138 * [expr, expr] List
4139 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 *
4141 * Also handle:
4142 * ! in front logical NOT
4143 * - in front unary minus
4144 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004145 * trailing [] subscript in String or List
4146 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004154eval7(
4155 char_u **arg,
4156 typval_T *rettv,
4157 int evaluate,
4158 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004160 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int len;
4162 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_u *start_leader, *end_leader;
4164 int ret = OK;
4165 char_u *alias;
4166
4167 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004169 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004174 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 */
4176 start_leader = *arg;
4177 while (**arg == '!' || **arg == '-' || **arg == '+')
4178 *arg = skipwhite(*arg + 1);
4179 end_leader = *arg;
4180
4181 switch (**arg)
4182 {
4183 /*
4184 * Number constant.
4185 */
4186 case '0':
4187 case '1':
4188 case '2':
4189 case '3':
4190 case '4':
4191 case '5':
4192 case '6':
4193 case '7':
4194 case '8':
4195 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004196 {
4197#ifdef FEAT_FLOAT
4198 char_u *p = skipdigits(*arg + 1);
4199 int get_float = FALSE;
4200
4201 /* We accept a float when the format matches
4202 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004203 * strict to avoid backwards compatibility problems.
4204 * Don't look for a float after the "." operator, so that
4205 * ":let vers = 1.2.3" doesn't fail. */
4206 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004208 get_float = TRUE;
4209 p = skipdigits(p + 2);
4210 if (*p == 'e' || *p == 'E')
4211 {
4212 ++p;
4213 if (*p == '-' || *p == '+')
4214 ++p;
4215 if (!vim_isdigit(*p))
4216 get_float = FALSE;
4217 else
4218 p = skipdigits(p + 1);
4219 }
4220 if (ASCII_ISALPHA(*p) || *p == '.')
4221 get_float = FALSE;
4222 }
4223 if (get_float)
4224 {
4225 float_T f;
4226
4227 *arg += string2float(*arg, &f);
4228 if (evaluate)
4229 {
4230 rettv->v_type = VAR_FLOAT;
4231 rettv->vval.v_float = f;
4232 }
4233 }
4234 else
4235#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004236 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004237 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004238 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004239 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004240
4241 // Blob constant: 0z0123456789abcdef
4242 if (evaluate)
4243 blob = blob_alloc();
4244 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4245 {
4246 if (!vim_isxdigit(bp[1]))
4247 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004248 if (blob != NULL)
4249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004250 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004251 ga_clear(&blob->bv_ga);
4252 VIM_CLEAR(blob);
4253 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004254 ret = FAIL;
4255 break;
4256 }
4257 if (blob != NULL)
4258 ga_append(&blob->bv_ga,
4259 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4260 }
4261 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004262 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004263 *arg = bp;
4264 }
4265 else
4266 {
4267 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004268 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004269 *arg += len;
4270 if (evaluate)
4271 {
4272 rettv->v_type = VAR_NUMBER;
4273 rettv->vval.v_number = n;
4274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 /*
4280 * String constant: "string".
4281 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 break;
4284
4285 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004286 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004289 break;
4290
4291 /*
4292 * List: [expr, expr]
4293 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 break;
4296
4297 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004298 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004299 * Dictionary: {key: val, key: val}
4300 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004301 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4302 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004303 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004304 break;
4305
4306 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004307 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004309 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 break;
4311
4312 /*
4313 * Environment variable: $VAR.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 break;
4317
4318 /*
4319 * Register contents: @r.
4320 */
4321 case '@': ++*arg;
4322 if (evaluate)
4323 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004325 rettv->vval.v_string = get_reg_contents(**arg,
4326 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 if (**arg != NUL)
4329 ++*arg;
4330 break;
4331
4332 /*
4333 * nested expression: (expression).
4334 */
4335 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (**arg == ')')
4338 ++*arg;
4339 else if (ret == OK)
4340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004341 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 ret = FAIL;
4344 }
4345 break;
4346
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 break;
4349 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350
4351 if (ret == NOTDONE)
4352 {
4353 /*
4354 * Must be a variable or function name.
4355 * Can also be a curly-braces kind of name: {expr}.
4356 */
4357 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004358 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359 if (alias != NULL)
4360 s = alias;
4361
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004362 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004363 ret = FAIL;
4364 else
4365 {
4366 if (**arg == '(') /* recursive! */
4367 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004368 partial_T *partial;
4369
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004370 if (!evaluate)
4371 check_vars(s, len);
4372
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 /* If "s" is the name of a variable of type VAR_FUNC
4374 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004375 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004377 /* Need to make a copy, in case evaluating the arguments makes
4378 * the name invalid. */
4379 s = vim_strsave(s);
4380 if (s == NULL)
4381 ret = FAIL;
4382 else
4383 /* Invoke the function. */
4384 ret = get_func_tv(s, len, rettv, arg,
4385 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4386 &len, evaluate, partial, NULL);
4387 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004388
4389 /* If evaluate is FALSE rettv->v_type was not set in
4390 * get_func_tv, but it's needed in handle_subscript() to parse
4391 * what follows. So set it here. */
4392 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4393 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004394 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004395 rettv->v_type = VAR_FUNC;
4396 }
4397
Bram Moolenaar8c711452005-01-14 21:53:12 +00004398 /* Stop the expression evaluation when immediately
4399 * aborting on error, or when an interrupt occurred or
4400 * an exception was thrown but not caught. */
4401 if (aborting())
4402 {
4403 if (ret == OK)
4404 clear_tv(rettv);
4405 ret = FAIL;
4406 }
4407 }
4408 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004409 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004410 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004411 {
4412 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004413 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004415 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004416 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004417 }
4418
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 *arg = skipwhite(*arg);
4420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004421 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4422 * expr(expr). */
4423 if (ret == OK)
4424 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 /*
4427 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4428 */
4429 if (ret == OK && evaluate && end_leader > start_leader)
4430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004432 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004433#ifdef FEAT_FLOAT
4434 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004436 if (rettv->v_type == VAR_FLOAT)
4437 f = rettv->vval.v_float;
4438 else
4439#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004440 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004443 clear_tv(rettv);
4444 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446 else
4447 {
4448 while (end_leader > start_leader)
4449 {
4450 --end_leader;
4451 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 {
4453#ifdef FEAT_FLOAT
4454 if (rettv->v_type == VAR_FLOAT)
4455 f = !f;
4456 else
4457#endif
4458 val = !val;
4459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004460 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 {
4462#ifdef FEAT_FLOAT
4463 if (rettv->v_type == VAR_FLOAT)
4464 f = -f;
4465 else
4466#endif
4467 val = -val;
4468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470#ifdef FEAT_FLOAT
4471 if (rettv->v_type == VAR_FLOAT)
4472 {
4473 clear_tv(rettv);
4474 rettv->vval.v_float = f;
4475 }
4476 else
4477#endif
4478 {
4479 clear_tv(rettv);
4480 rettv->v_type = VAR_NUMBER;
4481 rettv->vval.v_number = val;
4482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485
4486 return ret;
4487}
4488
4489/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004490 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4491 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004492 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4493 */
4494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004495eval_index(
4496 char_u **arg,
4497 typval_T *rettv,
4498 int evaluate,
4499 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004500{
4501 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004502 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004503 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 long len = -1;
4506 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509
Bram Moolenaara03f2332016-02-06 18:09:59 +01004510 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004512 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004513 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004514 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004516 return FAIL;
4517 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004518#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004519 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004520 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004521 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004522#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004523 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004524 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004525 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004526 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004527 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004528 return FAIL;
4529 case VAR_UNKNOWN:
4530 if (evaluate)
4531 return FAIL;
4532 /* FALLTHROUGH */
4533
4534 case VAR_STRING:
4535 case VAR_NUMBER:
4536 case VAR_LIST:
4537 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004538 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004539 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004542 init_tv(&var1);
4543 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 /*
4547 * dict.name
4548 */
4549 key = *arg + 1;
4550 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4551 ;
4552 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004555 }
4556 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 /*
4559 * something[idx]
4560 *
4561 * Get the (first) variable from inside the [].
4562 */
4563 *arg = skipwhite(*arg + 1);
4564 if (**arg == ':')
4565 empty1 = TRUE;
4566 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4567 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004568 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004569 {
4570 /* not a number or string */
4571 clear_tv(&var1);
4572 return FAIL;
4573 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004574
4575 /*
4576 * Get the second variable from inside the [:].
4577 */
4578 if (**arg == ':')
4579 {
4580 range = TRUE;
4581 *arg = skipwhite(*arg + 1);
4582 if (**arg == ']')
4583 empty2 = TRUE;
4584 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 if (!empty1)
4587 clear_tv(&var1);
4588 return FAIL;
4589 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004590 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004591 {
4592 /* not a number or string */
4593 if (!empty1)
4594 clear_tv(&var1);
4595 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 return FAIL;
4597 }
4598 }
4599
4600 /* Check for the ']'. */
4601 if (**arg != ']')
4602 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004603 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004604 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 clear_tv(&var1);
4606 if (range)
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 }
4612
4613 if (evaluate)
4614 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 n1 = 0;
4616 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004618 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 }
4621 if (range)
4622 {
4623 if (empty2)
4624 n2 = -1;
4625 else
4626 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004627 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 }
4630 }
4631
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004634 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004635 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004636 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004637 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004638 case VAR_SPECIAL:
4639 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004640 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004641 break; /* not evaluating, skipping over subscript */
4642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 case VAR_NUMBER:
4644 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004645 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 len = (long)STRLEN(s);
4647 if (range)
4648 {
4649 /* The resulting variable is a substring. If the indexes
4650 * are out of range the result is empty. */
4651 if (n1 < 0)
4652 {
4653 n1 = len + n1;
4654 if (n1 < 0)
4655 n1 = 0;
4656 }
4657 if (n2 < 0)
4658 n2 = len + n2;
4659 else if (n2 >= len)
4660 n2 = len;
4661 if (n1 >= len || n2 < 0 || n1 > n2)
4662 s = NULL;
4663 else
4664 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4665 }
4666 else
4667 {
4668 /* The resulting variable is a string of a single
4669 * character. If the index is too big or negative the
4670 * result is empty. */
4671 if (n1 >= len || n1 < 0)
4672 s = NULL;
4673 else
4674 s = vim_strnsave(s + n1, 1);
4675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(rettv);
4677 rettv->v_type = VAR_STRING;
4678 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 break;
4680
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004681 case VAR_BLOB:
4682 len = blob_len(rettv->vval.v_blob);
4683 if (range)
4684 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004685 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004686 // are out of range the result is empty.
4687 if (n1 < 0)
4688 {
4689 n1 = len + n1;
4690 if (n1 < 0)
4691 n1 = 0;
4692 }
4693 if (n2 < 0)
4694 n2 = len + n2;
4695 else if (n2 >= len)
4696 n2 = len - 1;
4697 if (n1 >= len || n2 < 0 || n1 > n2)
4698 {
4699 clear_tv(rettv);
4700 rettv->v_type = VAR_BLOB;
4701 rettv->vval.v_blob = NULL;
4702 }
4703 else
4704 {
4705 blob_T *blob = blob_alloc();
4706
4707 if (blob != NULL)
4708 {
4709 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4710 {
4711 blob_free(blob);
4712 return FAIL;
4713 }
4714 blob->bv_ga.ga_len = n2 - n1 + 1;
4715 for (i = n1; i <= n2; i++)
4716 blob_set(blob, i - n1,
4717 blob_get(rettv->vval.v_blob, i));
4718
4719 clear_tv(rettv);
4720 rettv_blob_set(rettv, blob);
4721 }
4722 }
4723 }
4724 else
4725 {
4726 // The resulting variable is a string of a single
4727 // character. If the index is too big or negative the
4728 // result is empty.
4729 if (n1 < len && n1 >= 0)
4730 {
4731 int v = (int)blob_get(rettv->vval.v_blob, n1);
4732
4733 clear_tv(rettv);
4734 rettv->v_type = VAR_NUMBER;
4735 rettv->vval.v_number = v;
4736 }
4737 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004738 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004739 }
4740 break;
4741
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 if (n1 < 0)
4745 n1 = len + n1;
4746 if (!empty1 && (n1 < 0 || n1 >= len))
4747 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004748 /* For a range we allow invalid values and return an empty
4749 * list. A list index out of range is an error. */
4750 if (!range)
4751 {
4752 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004753 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004754 return FAIL;
4755 }
4756 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 }
4758 if (range)
4759 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 list_T *l;
4761 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762
4763 if (n2 < 0)
4764 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004765 else if (n2 >= len)
4766 n2 = len - 1;
4767 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004768 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 l = list_alloc();
4770 if (l == NULL)
4771 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 n1 <= n2; ++n1)
4774 {
4775 if (list_append_tv(l, &item->li_tv) == FAIL)
4776 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004777 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 return FAIL;
4779 }
4780 item = item->li_next;
4781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004782 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004783 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 }
4785 else
4786 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004787 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 clear_tv(rettv);
4789 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 }
4791 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792
4793 case VAR_DICT:
4794 if (range)
4795 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004796 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004797 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 if (len == -1)
4799 clear_tv(&var1);
4800 return FAIL;
4801 }
4802 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804
4805 if (len == -1)
4806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004807 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004808 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 clear_tv(&var1);
4811 return FAIL;
4812 }
4813 }
4814
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004815 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004817 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819 if (len == -1)
4820 clear_tv(&var1);
4821 if (item == NULL)
4822 return FAIL;
4823
4824 copy_tv(&item->di_tv, &var1);
4825 clear_tv(rettv);
4826 *rettv = var1;
4827 }
4828 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 }
4830 }
4831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832 return OK;
4833}
4834
4835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 * Get an option value.
4837 * "arg" points to the '&' or '+' before the option name.
4838 * "arg" is advanced to character after the option name.
4839 * Return OK or FAIL.
4840 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004842get_option_tv(
4843 char_u **arg,
4844 typval_T *rettv, /* when NULL, only check if option exists */
4845 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
4847 char_u *option_end;
4848 long numval;
4849 char_u *stringval;
4850 int opt_type;
4851 int c;
4852 int working = (**arg == '+'); /* has("+option") */
4853 int ret = OK;
4854 int opt_flags;
4855
4856 /*
4857 * Isolate the option name and find its value.
4858 */
4859 option_end = find_option_end(arg, &opt_flags);
4860 if (option_end == NULL)
4861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004863 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 return FAIL;
4865 }
4866
4867 if (!evaluate)
4868 {
4869 *arg = option_end;
4870 return OK;
4871 }
4872
4873 c = *option_end;
4874 *option_end = NUL;
4875 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878 if (opt_type == -3) /* invalid name */
4879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004881 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 ret = FAIL;
4883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
4886 if (opt_type == -2) /* hidden string option */
4887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 rettv->v_type = VAR_STRING;
4889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891 else if (opt_type == -1) /* hidden number option */
4892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 rettv->v_type = VAR_NUMBER;
4894 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896 else if (opt_type == 1) /* number option */
4897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 rettv->v_type = VAR_NUMBER;
4899 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 else /* string option */
4902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 rettv->v_type = VAR_STRING;
4904 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907 else if (working && (opt_type == -2 || opt_type == -1))
4908 ret = FAIL;
4909
4910 *option_end = c; /* put back for error messages */
4911 *arg = option_end;
4912
4913 return ret;
4914}
4915
4916/*
4917 * Allocate a variable for a string constant.
4918 * Return OK or FAIL.
4919 */
4920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004921get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 char_u *p;
4924 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 int extra = 0;
4926
4927 /*
4928 * Find the end of the string, skipping backslashed characters.
4929 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004930 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
4932 if (*p == '\\' && p[1] != NUL)
4933 {
4934 ++p;
4935 /* A "\<x>" form occupies at least 4 characters, and produces up
4936 * to 6 characters: reserve space for 2 extra */
4937 if (*p == '<')
4938 extra += 2;
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941
4942 if (*p != '"')
4943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004944 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 return FAIL;
4946 }
4947
4948 /* If only parsing, set *arg and return here */
4949 if (!evaluate)
4950 {
4951 *arg = p + 1;
4952 return OK;
4953 }
4954
4955 /*
4956 * Copy the string into allocated memory, handling backslashed
4957 * characters.
4958 */
4959 name = alloc((unsigned)(p - *arg + extra));
4960 if (name == NULL)
4961 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 rettv->v_type = VAR_STRING;
4963 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 if (*p == '\\')
4968 {
4969 switch (*++p)
4970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 case 'b': *name++ = BS; ++p; break;
4972 case 'e': *name++ = ESC; ++p; break;
4973 case 'f': *name++ = FF; ++p; break;
4974 case 'n': *name++ = NL; ++p; break;
4975 case 'r': *name++ = CAR; ++p; break;
4976 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 case 'X': /* hex: "\x1", "\x12" */
4979 case 'x':
4980 case 'u': /* Unicode: "\u0023" */
4981 case 'U':
4982 if (vim_isxdigit(p[1]))
4983 {
4984 int n, nr;
4985 int c = toupper(*p);
4986
4987 if (c == 'X')
4988 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004989 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004991 else
4992 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 nr = 0;
4994 while (--n >= 0 && vim_isxdigit(p[1]))
4995 {
4996 ++p;
4997 nr = (nr << 4) + hex2nr(*p);
4998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#ifdef FEAT_MBYTE
5001 /* For "\u" store the number according to
5002 * 'encoding'. */
5003 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 else
5006#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 break;
5010
5011 /* octal: "\1", "\12", "\123" */
5012 case '0':
5013 case '1':
5014 case '2':
5015 case '3':
5016 case '4':
5017 case '5':
5018 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 case '7': *name = *p++ - '0';
5020 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 *name = (*name << 3) + *p++ - '0';
5023 if (*p >= '0' && *p <= '7')
5024 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 break;
5028
5029 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005030 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (extra != 0)
5032 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 break;
5035 }
5036 /* FALLTHROUGH */
5037
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 break;
5040 }
5041 }
5042 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005047 if (*p != NUL) /* just in case */
5048 ++p;
5049 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 return OK;
5052}
5053
5054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 * Return OK or FAIL.
5057 */
5058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005059get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 int reduce = 0;
5064
5065 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005068 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 break;
5074 ++reduce;
5075 ++p;
5076 }
5077 }
5078
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005081 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 return FAIL;
5083 }
5084
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 if (!evaluate)
5087 {
5088 *arg = p + 1;
5089 return OK;
5090 }
5091
5092 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005094 */
5095 str = alloc((unsigned)((p - *arg) - reduce));
5096 if (str == NULL)
5097 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 rettv->v_type = VAR_STRING;
5099 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 break;
5107 ++p;
5108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 *arg = p + 1;
5113
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 return OK;
5115}
5116
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005117/*
5118 * Return the function name of the partial.
5119 */
5120 char_u *
5121partial_name(partial_T *pt)
5122{
5123 if (pt->pt_name != NULL)
5124 return pt->pt_name;
5125 return pt->pt_func->uf_name;
5126}
5127
Bram Moolenaarddecc252016-04-06 22:59:37 +02005128 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005130{
5131 int i;
5132
5133 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005134 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005135 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005137 if (pt->pt_name != NULL)
5138 {
5139 func_unref(pt->pt_name);
5140 vim_free(pt->pt_name);
5141 }
5142 else
5143 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005144 vim_free(pt);
5145}
5146
5147/*
5148 * Unreference a closure: decrement the reference count and free it when it
5149 * becomes zero.
5150 */
5151 void
5152partial_unref(partial_T *pt)
5153{
5154 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005155 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005156}
5157
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005158static int tv_equal_recurse_limit;
5159
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005160 static int
5161func_equal(
5162 typval_T *tv1,
5163 typval_T *tv2,
5164 int ic) /* ignore case */
5165{
5166 char_u *s1, *s2;
5167 dict_T *d1, *d2;
5168 int a1, a2;
5169 int i;
5170
5171 /* empty and NULL function name considered the same */
5172 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005173 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005174 if (s1 != NULL && *s1 == NUL)
5175 s1 = NULL;
5176 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005177 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005178 if (s2 != NULL && *s2 == NUL)
5179 s2 = NULL;
5180 if (s1 == NULL || s2 == NULL)
5181 {
5182 if (s1 != s2)
5183 return FALSE;
5184 }
5185 else if (STRCMP(s1, s2) != 0)
5186 return FALSE;
5187
5188 /* empty dict and NULL dict is different */
5189 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5190 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5191 if (d1 == NULL || d2 == NULL)
5192 {
5193 if (d1 != d2)
5194 return FALSE;
5195 }
5196 else if (!dict_equal(d1, d2, ic, TRUE))
5197 return FALSE;
5198
5199 /* empty list and no list considered the same */
5200 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5201 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5202 if (a1 != a2)
5203 return FALSE;
5204 for (i = 0; i < a1; ++i)
5205 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5206 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5207 return FALSE;
5208
5209 return TRUE;
5210}
5211
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005212/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005213 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005214 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005216 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005217 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005218tv_equal(
5219 typval_T *tv1,
5220 typval_T *tv2,
5221 int ic, /* ignore case */
5222 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005223{
5224 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005226 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005227 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005228
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005229 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005230 * recursiveness to a limit. We guess they are equal then.
5231 * A fixed limit has the problem of still taking an awful long time.
5232 * Reduce the limit every time running into it. That should work fine for
5233 * deeply linked structures that are not recursively linked and catch
5234 * recursiveness quickly. */
5235 if (!recursive)
5236 tv_equal_recurse_limit = 1000;
5237 if (recursive_cnt >= tv_equal_recurse_limit)
5238 {
5239 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005240 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005241 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005242
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005243 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5244 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005245 if ((tv1->v_type == VAR_FUNC
5246 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5247 && (tv2->v_type == VAR_FUNC
5248 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5249 {
5250 ++recursive_cnt;
5251 r = func_equal(tv1, tv2, ic);
5252 --recursive_cnt;
5253 return r;
5254 }
5255
5256 if (tv1->v_type != tv2->v_type)
5257 return FALSE;
5258
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005259 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005260 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005261 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005262 ++recursive_cnt;
5263 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5264 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005265 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005266
5267 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005268 ++recursive_cnt;
5269 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5270 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005271 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005272
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005273 case VAR_BLOB:
5274 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5275
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005276 case VAR_NUMBER:
5277 return tv1->vval.v_number == tv2->vval.v_number;
5278
5279 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005280 s1 = tv_get_string_buf(tv1, buf1);
5281 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005282 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005283
5284 case VAR_SPECIAL:
5285 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005286
5287 case VAR_FLOAT:
5288#ifdef FEAT_FLOAT
5289 return tv1->vval.v_float == tv2->vval.v_float;
5290#endif
5291 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005292#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005293 return tv1->vval.v_job == tv2->vval.v_job;
5294#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005295 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005297 return tv1->vval.v_channel == tv2->vval.v_channel;
5298#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005299 case VAR_FUNC:
5300 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005301 case VAR_UNKNOWN:
5302 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005304
Bram Moolenaara03f2332016-02-06 18:09:59 +01005305 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5306 * does not equal anything, not even itself. */
5307 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005308}
5309
5310/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005311 * Return the next (unique) copy ID.
5312 * Used for serializing nested structures.
5313 */
5314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005315get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005316{
5317 current_copyID += COPYID_INC;
5318 return current_copyID;
5319}
5320
5321/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322 * Garbage collection for lists and dictionaries.
5323 *
5324 * We use reference counts to be able to free most items right away when they
5325 * are no longer used. But for composite items it's possible that it becomes
5326 * unused while the reference count is > 0: When there is a recursive
5327 * reference. Example:
5328 * :let l = [1, 2, 3]
5329 * :let d = {9: l}
5330 * :let l[1] = d
5331 *
5332 * Since this is quite unusual we handle this with garbage collection: every
5333 * once in a while find out which lists and dicts are not referenced from any
5334 * variable.
5335 *
5336 * Here is a good reference text about garbage collection (refers to Python
5337 * but it applies to all reference-counting mechanisms):
5338 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005339 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005340
5341/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005342 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005343 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005344 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005345 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005346 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005347garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005348{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005349 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005350 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005351 buf_T *buf;
5352 win_T *wp;
5353 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005354 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005355 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005356
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005357 if (!testing)
5358 {
5359 /* Only do this once. */
5360 want_garbage_collect = FALSE;
5361 may_garbage_collect = FALSE;
5362 garbage_collect_at_exit = FALSE;
5363 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005364
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005365 /* We advance by two because we add one for items referenced through
5366 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005367 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005368
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005369 /*
5370 * 1. Go through all accessible variables and mark all lists and dicts
5371 * with copyID.
5372 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005373
5374 /* Don't free variables in the previous_funccal list unless they are only
5375 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005376 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005377 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005378
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379 /* script-local variables */
5380 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005381 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005382
5383 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005384 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005385 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5386 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005387
5388 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005389 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005390 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5391 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005392 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005393 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5394 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005395
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005396 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005397 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005398 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5399 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005402
5403 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005406 /* named functions (matters for closures) */
5407 abort = abort || set_ref_in_functions(copyID);
5408
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005409 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005411
Bram Moolenaard812df62008-11-09 12:46:09 +00005412 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005413 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005414
Bram Moolenaar1dced572012-04-05 16:54:08 +02005415#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005417#endif
5418
Bram Moolenaardb913952012-06-29 12:54:53 +02005419#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005420 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005421#endif
5422
5423#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005425#endif
5426
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005427#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005428 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005429 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005430#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005431#ifdef FEAT_NETBEANS_INTG
5432 abort = abort || set_ref_in_nb_channel(copyID);
5433#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005434
Bram Moolenaare3188e22016-05-31 21:13:04 +02005435#ifdef FEAT_TIMERS
5436 abort = abort || set_ref_in_timer(copyID);
5437#endif
5438
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005439#ifdef FEAT_QUICKFIX
5440 abort = abort || set_ref_in_quickfix(copyID);
5441#endif
5442
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005443#ifdef FEAT_TERMINAL
5444 abort = abort || set_ref_in_term(copyID);
5445#endif
5446
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005447 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005448 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005449 /*
5450 * 2. Free lists and dictionaries that are not referenced.
5451 */
5452 did_free = free_unref_items(copyID);
5453
5454 /*
5455 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005457 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005459 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 else if (p_verbose > 0)
5461 {
5462 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5463 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005464
5465 return did_free;
5466}
5467
5468/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005469 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005470 */
5471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005473{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005474 int did_free = FALSE;
5475
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005476 /* Let all "free" functions know that we are here. This means no
5477 * dictionaries, lists, channels or jobs are to be freed, because we will
5478 * do that here. */
5479 in_free_unref_items = TRUE;
5480
5481 /*
5482 * PASS 1: free the contents of the items. We don't free the items
5483 * themselves yet, so that it is possible to decrement refcount counters
5484 */
5485
Bram Moolenaarcd524592016-07-17 14:57:05 +02005486 /* Go through the list of dicts and free items without the copyID. */
5487 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005488
Bram Moolenaarda861d62016-07-17 15:46:27 +02005489 /* Go through the list of lists and free items without the copyID. */
5490 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005491
5492#ifdef FEAT_JOB_CHANNEL
5493 /* Go through the list of jobs and free items without the copyID. This
5494 * must happen before doing channels, because jobs refer to channels, but
5495 * the reference from the channel to the job isn't tracked. */
5496 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5497
5498 /* Go through the list of channels and free items without the copyID. */
5499 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5500#endif
5501
5502 /*
5503 * PASS 2: free the items themselves.
5504 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005505 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005506 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005507
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005508#ifdef FEAT_JOB_CHANNEL
5509 /* Go through the list of jobs and free items without the copyID. This
5510 * must happen before doing channels, because jobs refer to channels, but
5511 * the reference from the channel to the job isn't tracked. */
5512 free_unused_jobs(copyID, COPYID_MASK);
5513
5514 /* Go through the list of channels and free items without the copyID. */
5515 free_unused_channels(copyID, COPYID_MASK);
5516#endif
5517
5518 in_free_unref_items = FALSE;
5519
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005520 return did_free;
5521}
5522
5523/*
5524 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005525 * "list_stack" is used to add lists to be marked. Can be NULL.
5526 *
5527 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005528 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005530set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531{
5532 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005533 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005534 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005535 hashtab_T *cur_ht;
5536 ht_stack_T *ht_stack = NULL;
5537 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005538
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005539 cur_ht = ht;
5540 for (;;)
5541 {
5542 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005543 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005544 /* Mark each item in the hashtab. If the item contains a hashtab
5545 * it is added to ht_stack, if it contains a list it is added to
5546 * list_stack. */
5547 todo = (int)cur_ht->ht_used;
5548 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5549 if (!HASHITEM_EMPTY(hi))
5550 {
5551 --todo;
5552 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5553 &ht_stack, list_stack);
5554 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005555 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005556
5557 if (ht_stack == NULL)
5558 break;
5559
5560 /* take an item from the stack */
5561 cur_ht = ht_stack->ht;
5562 tempitem = ht_stack;
5563 ht_stack = ht_stack->prev;
5564 free(tempitem);
5565 }
5566
5567 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005568}
5569
5570/*
5571 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5573 *
5574 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005575 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005578{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005579 listitem_T *li;
5580 int abort = FALSE;
5581 list_T *cur_l;
5582 list_stack_T *list_stack = NULL;
5583 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005584
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005585 cur_l = l;
5586 for (;;)
5587 {
5588 if (!abort)
5589 /* Mark each item in the list. If the item contains a hashtab
5590 * it is added to ht_stack, if it contains a list it is added to
5591 * list_stack. */
5592 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5593 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5594 ht_stack, &list_stack);
5595 if (list_stack == NULL)
5596 break;
5597
5598 /* take an item from the stack */
5599 cur_l = list_stack->list;
5600 tempitem = list_stack;
5601 list_stack = list_stack->prev;
5602 free(tempitem);
5603 }
5604
5605 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005606}
5607
5608/*
5609 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005610 * "list_stack" is used to add lists to be marked. Can be NULL.
5611 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5612 *
5613 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005614 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005616set_ref_in_item(
5617 typval_T *tv,
5618 int copyID,
5619 ht_stack_T **ht_stack,
5620 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005621{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005622 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005623
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005624 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005625 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005626 dict_T *dd = tv->vval.v_dict;
5627
Bram Moolenaara03f2332016-02-06 18:09:59 +01005628 if (dd != NULL && dd->dv_copyID != copyID)
5629 {
5630 /* Didn't see this dict yet. */
5631 dd->dv_copyID = copyID;
5632 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005633 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005634 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5635 }
5636 else
5637 {
5638 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5639 if (newitem == NULL)
5640 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005641 else
5642 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005643 newitem->ht = &dd->dv_hashtab;
5644 newitem->prev = *ht_stack;
5645 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005646 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005647 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005648 }
5649 }
5650 else if (tv->v_type == VAR_LIST)
5651 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005652 list_T *ll = tv->vval.v_list;
5653
Bram Moolenaara03f2332016-02-06 18:09:59 +01005654 if (ll != NULL && ll->lv_copyID != copyID)
5655 {
5656 /* Didn't see this list yet. */
5657 ll->lv_copyID = copyID;
5658 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005659 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005660 abort = set_ref_in_list(ll, copyID, ht_stack);
5661 }
5662 else
5663 {
5664 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005665 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005666 if (newitem == NULL)
5667 abort = TRUE;
5668 else
5669 {
5670 newitem->list = ll;
5671 newitem->prev = *list_stack;
5672 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005673 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005674 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005675 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005676 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005677 else if (tv->v_type == VAR_FUNC)
5678 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005679 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005680 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005681 else if (tv->v_type == VAR_PARTIAL)
5682 {
5683 partial_T *pt = tv->vval.v_partial;
5684 int i;
5685
5686 /* A partial does not have a copyID, because it cannot contain itself.
5687 */
5688 if (pt != NULL)
5689 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005690 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005691
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005692 if (pt->pt_dict != NULL)
5693 {
5694 typval_T dtv;
5695
5696 dtv.v_type = VAR_DICT;
5697 dtv.vval.v_dict = pt->pt_dict;
5698 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5699 }
5700
5701 for (i = 0; i < pt->pt_argc; ++i)
5702 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5703 ht_stack, list_stack);
5704 }
5705 }
5706#ifdef FEAT_JOB_CHANNEL
5707 else if (tv->v_type == VAR_JOB)
5708 {
5709 job_T *job = tv->vval.v_job;
5710 typval_T dtv;
5711
5712 if (job != NULL && job->jv_copyID != copyID)
5713 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005714 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005715 if (job->jv_channel != NULL)
5716 {
5717 dtv.v_type = VAR_CHANNEL;
5718 dtv.vval.v_channel = job->jv_channel;
5719 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5720 }
5721 if (job->jv_exit_partial != NULL)
5722 {
5723 dtv.v_type = VAR_PARTIAL;
5724 dtv.vval.v_partial = job->jv_exit_partial;
5725 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5726 }
5727 }
5728 }
5729 else if (tv->v_type == VAR_CHANNEL)
5730 {
5731 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005732 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005733 typval_T dtv;
5734 jsonq_T *jq;
5735 cbq_T *cq;
5736
5737 if (ch != NULL && ch->ch_copyID != copyID)
5738 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005739 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005740 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005741 {
5742 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5743 jq = jq->jq_next)
5744 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5745 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5746 cq = cq->cq_next)
5747 if (cq->cq_partial != NULL)
5748 {
5749 dtv.v_type = VAR_PARTIAL;
5750 dtv.vval.v_partial = cq->cq_partial;
5751 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5752 }
5753 if (ch->ch_part[part].ch_partial != NULL)
5754 {
5755 dtv.v_type = VAR_PARTIAL;
5756 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5757 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5758 }
5759 }
5760 if (ch->ch_partial != NULL)
5761 {
5762 dtv.v_type = VAR_PARTIAL;
5763 dtv.vval.v_partial = ch->ch_partial;
5764 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5765 }
5766 if (ch->ch_close_partial != NULL)
5767 {
5768 dtv.v_type = VAR_PARTIAL;
5769 dtv.vval.v_partial = ch->ch_close_partial;
5770 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5771 }
5772 }
5773 }
5774#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005775 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005776}
5777
Bram Moolenaar17a13432016-01-24 14:22:10 +01005778 static char *
5779get_var_special_name(int nr)
5780{
5781 switch (nr)
5782 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005783 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005784 case VVAL_TRUE: return "v:true";
5785 case VVAL_NONE: return "v:none";
5786 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005787 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005788 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005789 return "42";
5790}
5791
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 * Return a string with the string representation of a variable.
5794 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005796 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005797 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5798 * stings as "string()", otherwise does not put quotes around strings, as
5799 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005800 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5801 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005802 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005804 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005805echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005806 typval_T *tv,
5807 char_u **tofree,
5808 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005809 int copyID,
5810 int echo_style,
5811 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005812 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005814 static int recurse = 0;
5815 char_u *r = NULL;
5816
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005818 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005819 if (!did_echo_string_emsg)
5820 {
5821 /* Only give this message once for a recursive call to avoid
5822 * flooding the user with errors. And stop iterating over lists
5823 * and dicts. */
5824 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005825 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005827 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005828 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005829 }
5830 ++recurse;
5831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 switch (tv->v_type)
5833 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005834 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005835 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005836 {
5837 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005838 r = tv->vval.v_string;
5839 if (r == NULL)
5840 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005841 }
5842 else
5843 {
5844 *tofree = string_quote(tv->vval.v_string, FALSE);
5845 r = *tofree;
5846 }
5847 break;
5848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005850 if (echo_style)
5851 {
5852 *tofree = NULL;
5853 r = tv->vval.v_string;
5854 }
5855 else
5856 {
5857 *tofree = string_quote(tv->vval.v_string, TRUE);
5858 r = *tofree;
5859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005860 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005861
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005862 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005863 {
5864 partial_T *pt = tv->vval.v_partial;
5865 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005866 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005867 garray_T ga;
5868 int i;
5869 char_u *tf;
5870
5871 ga_init2(&ga, 1, 100);
5872 ga_concat(&ga, (char_u *)"function(");
5873 if (fname != NULL)
5874 {
5875 ga_concat(&ga, fname);
5876 vim_free(fname);
5877 }
5878 if (pt != NULL && pt->pt_argc > 0)
5879 {
5880 ga_concat(&ga, (char_u *)", [");
5881 for (i = 0; i < pt->pt_argc; ++i)
5882 {
5883 if (i > 0)
5884 ga_concat(&ga, (char_u *)", ");
5885 ga_concat(&ga,
5886 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5887 vim_free(tf);
5888 }
5889 ga_concat(&ga, (char_u *)"]");
5890 }
5891 if (pt != NULL && pt->pt_dict != NULL)
5892 {
5893 typval_T dtv;
5894
5895 ga_concat(&ga, (char_u *)", ");
5896 dtv.v_type = VAR_DICT;
5897 dtv.vval.v_dict = pt->pt_dict;
5898 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5899 vim_free(tf);
5900 }
5901 ga_concat(&ga, (char_u *)")");
5902
5903 *tofree = ga.ga_data;
5904 r = *tofree;
5905 break;
5906 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005907
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005908 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005909 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005910 break;
5911
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005913 if (tv->vval.v_list == NULL)
5914 {
5915 *tofree = NULL;
5916 r = NULL;
5917 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005918 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5919 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005920 {
5921 *tofree = NULL;
5922 r = (char_u *)"[...]";
5923 }
5924 else
5925 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005926 int old_copyID = tv->vval.v_list->lv_copyID;
5927
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005928 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005929 *tofree = list2string(tv, copyID, restore_copyID);
5930 if (restore_copyID)
5931 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005932 r = *tofree;
5933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005934 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005937 if (tv->vval.v_dict == NULL)
5938 {
5939 *tofree = NULL;
5940 r = NULL;
5941 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005942 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5943 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005944 {
5945 *tofree = NULL;
5946 r = (char_u *)"{...}";
5947 }
5948 else
5949 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005950 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005952 *tofree = dict2string(tv, copyID, restore_copyID);
5953 if (restore_copyID)
5954 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005955 r = *tofree;
5956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005957 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005959 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005960 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005961 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005962 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005963 break;
5964
Bram Moolenaar835dc632016-02-07 14:27:38 +01005965 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005966 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005967 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005968 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005969 if (composite_val)
5970 {
5971 *tofree = string_quote(r, FALSE);
5972 r = *tofree;
5973 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005975
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005977#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005978 *tofree = NULL;
5979 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5980 r = numbuf;
5981 break;
5982#endif
5983
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005984 case VAR_SPECIAL:
5985 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005986 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005987 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005988 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989
Bram Moolenaar8502c702014-06-17 12:51:16 +02005990 if (--recurse == 0)
5991 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005992 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993}
5994
5995/*
5996 * Return a string with the string representation of a variable.
5997 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5998 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005999 * Does not put quotes around strings, as ":echo" displays values.
6000 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6001 * May return NULL.
6002 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006004echo_string(
6005 typval_T *tv,
6006 char_u **tofree,
6007 char_u *numbuf,
6008 int copyID)
6009{
6010 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6011}
6012
6013/*
6014 * Return a string with the string representation of a variable.
6015 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6016 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006018 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006019 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006020 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006021tv2string(
6022 typval_T *tv,
6023 char_u **tofree,
6024 char_u *numbuf,
6025 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006026{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006027 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028}
6029
6030/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 * Return string "str" in ' quotes, doubling ' characters.
6032 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006033 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006034 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006035 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039 char_u *p, *r, *s;
6040
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 len = (function ? 13 : 3);
6042 if (str != NULL)
6043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006044 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006045 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 if (*p == '\'')
6047 ++len;
6048 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006049 s = r = alloc(len);
6050 if (r != NULL)
6051 {
6052 if (function)
6053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 r += 10;
6056 }
6057 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006058 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 if (str != NULL)
6060 for (p = str; *p != NUL; )
6061 {
6062 if (*p == '\'')
6063 *r++ = '\'';
6064 MB_COPY_CHAR(p, r);
6065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067 if (function)
6068 *r++ = ')';
6069 *r++ = NUL;
6070 }
6071 return s;
6072}
6073
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006074#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006075/*
6076 * Convert the string "text" to a floating point number.
6077 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6078 * this always uses a decimal point.
6079 * Returns the length of the text that was consumed.
6080 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006082string2float(
6083 char_u *text,
6084 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006085{
6086 char *s = (char *)text;
6087 float_T f;
6088
Bram Moolenaar62473612017-01-08 19:25:40 +01006089 /* MS-Windows does not deal with "inf" and "nan" properly. */
6090 if (STRNICMP(text, "inf", 3) == 0)
6091 {
6092 *value = INFINITY;
6093 return 3;
6094 }
6095 if (STRNICMP(text, "-inf", 3) == 0)
6096 {
6097 *value = -INFINITY;
6098 return 4;
6099 }
6100 if (STRNICMP(text, "nan", 3) == 0)
6101 {
6102 *value = NAN;
6103 return 3;
6104 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006105 f = strtod(s, &s);
6106 *value = f;
6107 return (int)((char_u *)s - text);
6108}
6109#endif
6110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 * Get the value of an environment variable.
6113 * "arg" is pointing to the '$'. It is advanced to after the name.
6114 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006115 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 */
6117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 char_u *string = NULL;
6121 int len;
6122 int cc;
6123 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006124 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126 ++*arg;
6127 name = *arg;
6128 len = get_env_len(arg);
6129 if (evaluate)
6130 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006131 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006132 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006133
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006134 cc = name[len];
6135 name[len] = NUL;
6136 /* first try vim_getenv(), fast for normal environment vars */
6137 string = vim_getenv(name, &mustfree);
6138 if (string != NULL && *string != NUL)
6139 {
6140 if (!mustfree)
6141 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006143 else
6144 {
6145 if (mustfree)
6146 vim_free(string);
6147
6148 /* next try expanding things like $VIM and ${HOME} */
6149 string = expand_env_save(name - 1);
6150 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006151 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006152 }
6153 name[len] = cc;
6154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 rettv->v_type = VAR_STRING;
6156 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 }
6158
6159 return OK;
6160}
6161
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006162
6163
6164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006166 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006168 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169var2fpos(
6170 typval_T *varp,
6171 int dollar_lnum, /* TRUE when $ is last line */
6172 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006174 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006176 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
Bram Moolenaara5525202006-03-02 22:52:09 +00006178 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006179 if (varp->v_type == VAR_LIST)
6180 {
6181 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006182 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006183 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006184 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006185
6186 l = varp->vval.v_list;
6187 if (l == NULL)
6188 return NULL;
6189
6190 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006191 pos.lnum = list_find_nr(l, 0L, &error);
6192 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006193 return NULL; /* invalid line number */
6194
6195 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006196 pos.col = list_find_nr(l, 1L, &error);
6197 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006198 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006199 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006200
6201 /* We accept "$" for the column number: last column. */
6202 li = list_find(l, 1L);
6203 if (li != NULL && li->li_tv.v_type == VAR_STRING
6204 && li->li_tv.vval.v_string != NULL
6205 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6206 pos.col = len + 1;
6207
Bram Moolenaara5525202006-03-02 22:52:09 +00006208 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006209 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006210 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006211 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006212
Bram Moolenaara5525202006-03-02 22:52:09 +00006213#ifdef FEAT_VIRTUALEDIT
6214 /* Get the virtual offset. Defaults to zero. */
6215 pos.coladd = list_find_nr(l, 2L, &error);
6216 if (error)
6217 pos.coladd = 0;
6218#endif
6219
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006220 return &pos;
6221 }
6222
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006223 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006224 if (name == NULL)
6225 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006226 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006228 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6229 {
6230 if (VIsual_active)
6231 return &VIsual;
6232 return &curwin->w_cursor;
6233 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006234 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006236 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6238 return NULL;
6239 return pp;
6240 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006241
6242#ifdef FEAT_VIRTUALEDIT
6243 pos.coladd = 0;
6244#endif
6245
Bram Moolenaar477933c2007-07-17 14:32:23 +00006246 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006247 {
6248 pos.col = 0;
6249 if (name[1] == '0') /* "w0": first visible line */
6250 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006251 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006252 /* In silent Ex mode topline is zero, but that's not a valid line
6253 * number; use one instead. */
6254 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006255 return &pos;
6256 }
6257 else if (name[1] == '$') /* "w$": last visible line */
6258 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006259 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006260 /* In silent Ex mode botline is zero, return zero then. */
6261 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006262 return &pos;
6263 }
6264 }
6265 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006267 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
6269 pos.lnum = curbuf->b_ml.ml_line_count;
6270 pos.col = 0;
6271 }
6272 else
6273 {
6274 pos.lnum = curwin->w_cursor.lnum;
6275 pos.col = (colnr_T)STRLEN(ml_get_curline());
6276 }
6277 return &pos;
6278 }
6279 return NULL;
6280}
6281
6282/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006283 * Convert list in "arg" into a position and optional file number.
6284 * When "fnump" is NULL there is no file number, only 3 items.
6285 * Note that the column is passed on as-is, the caller may want to decrement
6286 * it to use 1 for the first column.
6287 * Return FAIL when conversion is not possible, doesn't check the position for
6288 * validity.
6289 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006291list2fpos(
6292 typval_T *arg,
6293 pos_T *posp,
6294 int *fnump,
6295 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006296{
6297 list_T *l = arg->vval.v_list;
6298 long i = 0;
6299 long n;
6300
Bram Moolenaar493c1782014-05-28 14:34:46 +02006301 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6302 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006303 if (arg->v_type != VAR_LIST
6304 || l == NULL
6305 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006306 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006307 return FAIL;
6308
6309 if (fnump != NULL)
6310 {
6311 n = list_find_nr(l, i++, NULL); /* fnum */
6312 if (n < 0)
6313 return FAIL;
6314 if (n == 0)
6315 n = curbuf->b_fnum; /* current buffer */
6316 *fnump = n;
6317 }
6318
6319 n = list_find_nr(l, i++, NULL); /* lnum */
6320 if (n < 0)
6321 return FAIL;
6322 posp->lnum = n;
6323
6324 n = list_find_nr(l, i++, NULL); /* col */
6325 if (n < 0)
6326 return FAIL;
6327 posp->col = n;
6328
6329#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006330 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006331 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006332 posp->coladd = 0;
6333 else
6334 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006335#endif
6336
Bram Moolenaar493c1782014-05-28 14:34:46 +02006337 if (curswantp != NULL)
6338 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6339
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006340 return OK;
6341}
6342
6343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 * Get the length of an environment variable name.
6345 * Advance "arg" to the first character after the name.
6346 * Return 0 for error.
6347 */
6348 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 char_u *p;
6352 int len;
6353
6354 for (p = *arg; vim_isIDc(*p); ++p)
6355 ;
6356 if (p == *arg) /* no name found */
6357 return 0;
6358
6359 len = (int)(p - *arg);
6360 *arg = p;
6361 return len;
6362}
6363
6364/*
6365 * Get the length of the name of a function or internal variable.
6366 * "arg" is advanced to the first non-white character after the name.
6367 * Return 0 if something is wrong.
6368 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 char_u *p;
6373 int len;
6374
6375 /* Find the end of the name. */
6376 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006377 {
6378 if (*p == ':')
6379 {
6380 /* "s:" is start of "s:var", but "n:" is not and can be used in
6381 * slice "[n:]". Also "xx:" is not a namespace. */
6382 len = (int)(p - *arg);
6383 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6384 || len > 1)
6385 break;
6386 }
6387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (p == *arg) /* no name found */
6389 return 0;
6390
6391 len = (int)(p - *arg);
6392 *arg = skipwhite(p);
6393
6394 return len;
6395}
6396
6397/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006398 * Get the length of the name of a variable or function.
6399 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006401 * Return -1 if curly braces expansion failed.
6402 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 * If the name contains 'magic' {}'s, expand them and return the
6404 * expanded name in an allocated string via 'alias' - caller must free.
6405 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006407get_name_len(
6408 char_u **arg,
6409 char_u **alias,
6410 int evaluate,
6411 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 char_u *p;
6415 char_u *expr_start;
6416 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418 *alias = NULL; /* default to no alias */
6419
6420 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6421 && (*arg)[2] == (int)KE_SNR)
6422 {
6423 /* hard coded <SNR>, already translated */
6424 *arg += 3;
6425 return get_id_len(arg) + 3;
6426 }
6427 len = eval_fname_script(*arg);
6428 if (len > 0)
6429 {
6430 /* literal "<SID>", "s:" or "<SNR>" */
6431 *arg += len;
6432 }
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006437 p = find_name_end(*arg, &expr_start, &expr_end,
6438 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (expr_start != NULL)
6440 {
6441 char_u *temp_string;
6442
6443 if (!evaluate)
6444 {
6445 len += (int)(p - *arg);
6446 *arg = skipwhite(p);
6447 return len;
6448 }
6449
6450 /*
6451 * Include any <SID> etc in the expanded string:
6452 * Thus the -len here.
6453 */
6454 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6455 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006456 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 *alias = temp_string;
6458 *arg = skipwhite(p);
6459 return (int)STRLEN(temp_string);
6460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006463 // Only give an error when there is something, otherwise it will be
6464 // reported at a higher level.
6465 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006466 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468 return len;
6469}
6470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006471/*
6472 * Find the end of a variable or function name, taking care of magic braces.
6473 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6474 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006475 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 * Return a pointer to just after the name. Equal to "arg" if there is no
6477 * valid name.
6478 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006479 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006480find_name_end(
6481 char_u *arg,
6482 char_u **expr_start,
6483 char_u **expr_end,
6484 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486 int mb_nest = 0;
6487 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006489 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006491 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 *expr_start = NULL;
6494 *expr_end = NULL;
6495 }
6496
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006497 /* Quick check for valid starting character. */
6498 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6499 return arg;
6500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006501 for (p = arg; *p != NUL
6502 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006503 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006504 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006505 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006506 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006508 if (*p == '\'')
6509 {
6510 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006511 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006512 ;
6513 if (*p == NUL)
6514 break;
6515 }
6516 else if (*p == '"')
6517 {
6518 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006519 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006520 if (*p == '\\' && p[1] != NUL)
6521 ++p;
6522 if (*p == NUL)
6523 break;
6524 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006525 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6526 {
6527 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006528 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006529 len = (int)(p - arg);
6530 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006531 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006532 break;
6533 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006537 if (*p == '[')
6538 ++br_nest;
6539 else if (*p == ']')
6540 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006543 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006545 if (*p == '{')
6546 {
6547 mb_nest++;
6548 if (expr_start != NULL && *expr_start == NULL)
6549 *expr_start = p;
6550 }
6551 else if (*p == '}')
6552 {
6553 mb_nest--;
6554 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6555 *expr_end = p;
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559
6560 return p;
6561}
6562
6563/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006564 * Expands out the 'magic' {}'s in a variable/function name.
6565 * Note that this can call itself recursively, to deal with
6566 * constructs like foo{bar}{baz}{bam}
6567 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6568 * "in_start" ^
6569 * "expr_start" ^
6570 * "expr_end" ^
6571 * "in_end" ^
6572 *
6573 * Returns a new allocated string, which the caller must free.
6574 * Returns NULL for failure.
6575 */
6576 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577make_expanded_name(
6578 char_u *in_start,
6579 char_u *expr_start,
6580 char_u *expr_end,
6581 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006582{
6583 char_u c1;
6584 char_u *retval = NULL;
6585 char_u *temp_result;
6586 char_u *nextcmd = NULL;
6587
6588 if (expr_end == NULL || in_end == NULL)
6589 return NULL;
6590 *expr_start = NUL;
6591 *expr_end = NUL;
6592 c1 = *in_end;
6593 *in_end = NUL;
6594
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006595 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006596 if (temp_result != NULL && nextcmd == NULL)
6597 {
6598 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6599 + (in_end - expr_end) + 1));
6600 if (retval != NULL)
6601 {
6602 STRCPY(retval, in_start);
6603 STRCAT(retval, temp_result);
6604 STRCAT(retval, expr_end + 1);
6605 }
6606 }
6607 vim_free(temp_result);
6608
6609 *in_end = c1; /* put char back for error messages */
6610 *expr_start = '{';
6611 *expr_end = '}';
6612
6613 if (retval != NULL)
6614 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006615 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006616 if (expr_start != NULL)
6617 {
6618 /* Further expansion! */
6619 temp_result = make_expanded_name(retval, expr_start,
6620 expr_end, temp_result);
6621 vim_free(retval);
6622 retval = temp_result;
6623 }
6624 }
6625
6626 return retval;
6627}
6628
6629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006636 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6637}
6638
6639/*
6640 * Return TRUE if character "c" can be used as the first character in a
6641 * variable or function name (excluding '{' and '}').
6642 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006645{
6646 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647}
6648
6649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 * Set number v: variable to "val".
6651 */
6652 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006653set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
6657
6658/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006659 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006661 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006662get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665}
6666
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006667/*
6668 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006669 * If the String variable has never been set, return an empty string.
6670 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006671 */
6672 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006673get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006674{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006675 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006676}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006679 * Get List v: variable value. Caller must take care of reference count when
6680 * needed.
6681 */
6682 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006684{
6685 return vimvars[idx].vv_list;
6686}
6687
6688/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006689 * Get Dict v: variable value. Caller must take care of reference count when
6690 * needed.
6691 */
6692 dict_T *
6693get_vim_var_dict(int idx)
6694{
6695 return vimvars[idx].vv_dict;
6696}
6697
6698/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006699 * Set v:char to character "c".
6700 */
6701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006703{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006704 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006705
6706#ifdef FEAT_MBYTE
6707 if (has_mbyte)
6708 buf[(*mb_char2bytes)(c, buf)] = NUL;
6709 else
6710#endif
6711 {
6712 buf[0] = c;
6713 buf[1] = NUL;
6714 }
6715 set_vim_var_string(VV_CHAR, buf, -1);
6716}
6717
6718/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006719 * Set v:count to "count" and v:count1 to "count1".
6720 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 */
6722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006723set_vcount(
6724 long count,
6725 long count1,
6726 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006728 if (set_prevcount)
6729 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006730 vimvars[VV_COUNT].vv_nr = count;
6731 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732}
6733
6734/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006735 * Save variables that might be changed as a side effect. Used when executing
6736 * a timer callback.
6737 */
6738 void
6739save_vimvars(vimvars_save_T *vvsave)
6740{
6741 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6742 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6743 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6744}
6745
6746/*
6747 * Restore variables saved by save_vimvars().
6748 */
6749 void
6750restore_vimvars(vimvars_save_T *vvsave)
6751{
6752 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6753 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6754 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6755}
6756
6757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 * Set string v: variable to a copy of "val".
6759 */
6760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006761set_vim_var_string(
6762 int idx,
6763 char_u *val,
6764 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
Bram Moolenaara542c682016-01-31 16:28:04 +01006766 clear_tv(&vimvars[idx].vv_di.di_tv);
6767 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006773 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774}
6775
6776/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006777 * Set List v: variable to "val".
6778 */
6779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006781{
Bram Moolenaara542c682016-01-31 16:28:04 +01006782 clear_tv(&vimvars[idx].vv_di.di_tv);
6783 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006784 vimvars[idx].vv_list = val;
6785 if (val != NULL)
6786 ++val->lv_refcount;
6787}
6788
6789/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006790 * Set Dictionary v: variable to "val".
6791 */
6792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006793set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006794{
Bram Moolenaara542c682016-01-31 16:28:04 +01006795 clear_tv(&vimvars[idx].vv_di.di_tv);
6796 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006797 vimvars[idx].vv_dict = val;
6798 if (val != NULL)
6799 {
6800 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006801 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006802 }
6803}
6804
6805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * Set v:register if needed.
6807 */
6808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006809set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
6811 char_u regname;
6812
6813 if (c == 0 || c == ' ')
6814 regname = '"';
6815 else
6816 regname = c;
6817 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 set_vim_var_string(VV_REG, &regname, 1);
6820}
6821
6822/*
6823 * Get or set v:exception. If "oldval" == NULL, return the current value.
6824 * Otherwise, restore the value to "oldval" and return NULL.
6825 * Must always be called in pairs to save and restore v:exception! Does not
6826 * take care of memory allocations.
6827 */
6828 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006829v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 return NULL;
6836}
6837
6838/*
6839 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6840 * Otherwise, restore the value to "oldval" and return NULL.
6841 * Must always be called in pairs to save and restore v:throwpoint! Does not
6842 * take care of memory allocations.
6843 */
6844 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006845v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006848 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 return NULL;
6852}
6853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854/*
6855 * Set v:cmdarg.
6856 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6857 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6858 * Must always be called in pairs!
6859 */
6860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006861set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 char_u *oldval;
6864 char_u *newval;
6865 unsigned len;
6866
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006868 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006870 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006872 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 }
6874
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006875 if (eap->force_bin == FORCE_BIN)
6876 len = 6;
6877 else if (eap->force_bin == FORCE_NOBIN)
6878 len = 8;
6879 else
6880 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006881
6882 if (eap->read_edit)
6883 len += 7;
6884
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006885 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006886 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006887# ifdef FEAT_MBYTE
6888 if (eap->force_enc != 0)
6889 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006890 if (eap->bad_char != 0)
6891 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006892# endif
6893
6894 newval = alloc(len + 1);
6895 if (newval == NULL)
6896 return NULL;
6897
6898 if (eap->force_bin == FORCE_BIN)
6899 sprintf((char *)newval, " ++bin");
6900 else if (eap->force_bin == FORCE_NOBIN)
6901 sprintf((char *)newval, " ++nobin");
6902 else
6903 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006904
6905 if (eap->read_edit)
6906 STRCAT(newval, " ++edit");
6907
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006908 if (eap->force_ff != 0)
6909 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006910 eap->force_ff == 'u' ? "unix"
6911 : eap->force_ff == 'd' ? "dos"
6912 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006913#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006914 if (eap->force_enc != 0)
6915 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6916 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006917 if (eap->bad_char == BAD_KEEP)
6918 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6919 else if (eap->bad_char == BAD_DROP)
6920 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6921 else if (eap->bad_char != 0)
6922 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006923#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006925 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
6928/*
6929 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006930 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933get_var_tv(
6934 char_u *name,
6935 int len, /* length of "name" */
6936 typval_T *rettv, /* NULL when only checking existence */
6937 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6938 int verbose, /* may give error message */
6939 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 /* truncate the name, so that we can use strcmp() */
6947 cc = name[len];
6948 name[len] = NUL;
6949
6950 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 * Check for user-defined variables.
6952 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006953 v = find_var(name, NULL, no_autoload);
6954 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006956 tv = &v->di_tv;
6957 if (dip != NULL)
6958 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
Bram Moolenaare9a41262005-01-15 22:18:47 +00006961 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006963 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006964 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 ret = FAIL;
6966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006967 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
6970 name[len] = cc;
6971
6972 return ret;
6973}
6974
6975/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006976 * Check if variable "name[len]" is a local variable or an argument.
6977 * If so, "*eval_lavars_used" is set to TRUE.
6978 */
6979 static void
6980check_vars(char_u *name, int len)
6981{
6982 int cc;
6983 char_u *varname;
6984 hashtab_T *ht;
6985
6986 if (eval_lavars_used == NULL)
6987 return;
6988
6989 /* truncate the name, so that we can use strcmp() */
6990 cc = name[len];
6991 name[len] = NUL;
6992
6993 ht = find_var_ht(name, &varname);
6994 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6995 {
6996 if (find_var(name, NULL, TRUE) != NULL)
6997 *eval_lavars_used = TRUE;
6998 }
6999
7000 name[len] = cc;
7001}
7002
7003/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007004 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7005 * Also handle function call with Funcref variable: func(expr)
7006 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7007 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009handle_subscript(
7010 char_u **arg,
7011 typval_T *rettv,
7012 int evaluate, /* do more than finding the end */
7013 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007014{
7015 int ret = OK;
7016 dict_T *selfdict = NULL;
7017 char_u *s;
7018 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007019 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020
7021 while (ret == OK
7022 && (**arg == '['
7023 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007024 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7025 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007026 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007027 {
7028 if (**arg == '(')
7029 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007030 partial_T *pt = NULL;
7031
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007033 if (evaluate)
7034 {
7035 functv = *rettv;
7036 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007037
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007038 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007039 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007040 {
7041 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007042 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007043 }
7044 else
7045 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007046 }
7047 else
7048 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007049 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007051 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052
7053 /* Clear the funcref afterwards, so that deleting it while
7054 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007055 if (evaluate)
7056 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007057
7058 /* Stop the expression evaluation when immediately aborting on
7059 * error, or when an interrupt occurred or an exception was thrown
7060 * but not caught. */
7061 if (aborting())
7062 {
7063 if (ret == OK)
7064 clear_tv(rettv);
7065 ret = FAIL;
7066 }
7067 dict_unref(selfdict);
7068 selfdict = NULL;
7069 }
7070 else /* **arg == '[' || **arg == '.' */
7071 {
7072 dict_unref(selfdict);
7073 if (rettv->v_type == VAR_DICT)
7074 {
7075 selfdict = rettv->vval.v_dict;
7076 if (selfdict != NULL)
7077 ++selfdict->dv_refcount;
7078 }
7079 else
7080 selfdict = NULL;
7081 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7082 {
7083 clear_tv(rettv);
7084 ret = FAIL;
7085 }
7086 }
7087 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007088
Bram Moolenaar1d429612016-05-24 15:44:17 +02007089 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7090 * Don't do this when "Func" is already a partial that was bound
7091 * explicitly (pt_auto is FALSE). */
7092 if (selfdict != NULL
7093 && (rettv->v_type == VAR_FUNC
7094 || (rettv->v_type == VAR_PARTIAL
7095 && (rettv->vval.v_partial->pt_auto
7096 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007097 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007098
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007099 dict_unref(selfdict);
7100 return ret;
7101}
7102
7103/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007104 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 * value).
7106 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007107 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109{
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007111}
7112
7113/*
7114 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 * The string "s" must have been allocated, it is consumed.
7116 * Return NULL for out of memory, the variable otherwise.
7117 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007119alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007123 rettv = alloc_tv();
7124 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007126 rettv->v_type = VAR_STRING;
7127 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 }
7129 else
7130 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007131 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132}
7133
7134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007135 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007138free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 if (varp != NULL)
7141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007142 switch (varp->v_type)
7143 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007144 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007145 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007146 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007147 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007148 vim_free(varp->vval.v_string);
7149 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007150 case VAR_PARTIAL:
7151 partial_unref(varp->vval.v_partial);
7152 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007153 case VAR_BLOB:
7154 blob_unref(varp->vval.v_blob);
7155 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 case VAR_LIST:
7157 list_unref(varp->vval.v_list);
7158 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007159 case VAR_DICT:
7160 dict_unref(varp->vval.v_dict);
7161 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007162 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007163#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007164 job_unref(varp->vval.v_job);
7165 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007166#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007167 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007169 channel_unref(varp->vval.v_channel);
7170 break;
7171#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007172 case VAR_NUMBER:
7173 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007174 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007175 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007176 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 vim_free(varp);
7179 }
7180}
7181
7182/*
7183 * Free the memory for a variable value and set the value to NULL or 0.
7184 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007186clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (varp != NULL)
7189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007190 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007193 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007194 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007195 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007196 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007197 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007198 case VAR_PARTIAL:
7199 partial_unref(varp->vval.v_partial);
7200 varp->vval.v_partial = NULL;
7201 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007202 case VAR_BLOB:
7203 blob_unref(varp->vval.v_blob);
7204 varp->vval.v_blob = NULL;
7205 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 case VAR_LIST:
7207 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007209 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 case VAR_DICT:
7211 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007214 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007215 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007216 varp->vval.v_number = 0;
7217 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007218 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007219#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007220 varp->vval.v_float = 0.0;
7221 break;
7222#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007223 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007224#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007225 job_unref(varp->vval.v_job);
7226 varp->vval.v_job = NULL;
7227#endif
7228 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007229 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007230#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007231 channel_unref(varp->vval.v_channel);
7232 varp->vval.v_channel = NULL;
7233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007234 case VAR_UNKNOWN:
7235 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007237 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239}
7240
7241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242 * Set the value of a variable to NULL without freeing items.
7243 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007245init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246{
7247 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249}
7250
7251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 * Get the number value of a variable.
7253 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007254 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007255 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256 * caller of incompatible types: it sets *denote to TRUE if "denote"
7257 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007259 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007260tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 int error = FALSE;
7263
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007264 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265}
7266
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007267 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007268tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007269{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007270 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007275 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007276 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007277#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007278 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007279 break;
7280#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007281 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007282 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007283 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 break;
7285 case VAR_STRING:
7286 if (varp->vval.v_string != NULL)
7287 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007288 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007289 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007290 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007291 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007292 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007293 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007294 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007295 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007296 case VAR_SPECIAL:
7297 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7298 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007299 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007300#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007302 break;
7303#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007304 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007305#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007306 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007307 break;
7308#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007309 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007310 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007311 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007312 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007313 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007316 if (denote == NULL) /* useful for values that must be unsigned */
7317 n = -1;
7318 else
7319 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007320 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007323#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007325tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007326{
7327 switch (varp->v_type)
7328 {
7329 case VAR_NUMBER:
7330 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007331 case VAR_FLOAT:
7332 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007333 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007334 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007336 break;
7337 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007338 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007339 break;
7340 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007341 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007342 break;
7343 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007344 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007345 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007346 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007347 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007348 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007349 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007350# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007351 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007352 break;
7353# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007354 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007355# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007356 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007357 break;
7358# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007359 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007360 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007361 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007362 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007363 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007364 break;
7365 }
7366 return 0;
7367}
7368#endif
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 * Get the string value of a variable.
7372 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007373 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7374 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 * If the String variable has never been set, return an empty string.
7376 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007377 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007380 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007381tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
7383 static char_u mybuf[NUMBUFLEN];
7384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386}
7387
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007388 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007391 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392
7393 return res != NULL ? res : (char_u *)"";
7394}
7395
Bram Moolenaar7d647822014-04-05 21:28:56 +02007396/*
7397 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7398 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007399 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007400tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007401{
7402 static char_u mybuf[NUMBUFLEN];
7403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007404 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405}
7406
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007407 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007410 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007412 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007413 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007414 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 return buf;
7416 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007417 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007418 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007419 break;
7420 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007421 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 break;
7423 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007424 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007425 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007426 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007427#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007428 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007429 break;
7430#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007431 case VAR_STRING:
7432 if (varp->vval.v_string != NULL)
7433 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007434 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007435 case VAR_SPECIAL:
7436 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7437 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007438 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007439 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007440 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007441 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007442#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007443 {
7444 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007445 char *status;
7446
7447 if (job == NULL)
7448 return (char_u *)"no process";
7449 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007450 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007451 : "run";
7452# ifdef UNIX
7453 vim_snprintf((char *)buf, NUMBUFLEN,
7454 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007455# elif defined(WIN32)
7456 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007457 "process %ld %s",
7458 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007459 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007460# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007461 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007462 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7463# endif
7464 return buf;
7465 }
7466#endif
7467 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007468 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007469#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007470 {
7471 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007472 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007473
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007474 if (channel == NULL)
7475 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7476 else
7477 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007478 "channel %d %s", channel->ch_id, status);
7479 return buf;
7480 }
7481#endif
7482 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007483 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007484 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007487 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007491 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7492 * string() on Dict, List, etc.
7493 */
7494 char_u *
7495tv_stringify(typval_T *varp, char_u *buf)
7496{
7497 if (varp->v_type == VAR_LIST
7498 || varp->v_type == VAR_DICT
7499 || varp->v_type == VAR_FUNC
7500 || varp->v_type == VAR_PARTIAL
7501 || varp->v_type == VAR_FLOAT)
7502 {
7503 typval_T tmp;
7504
7505 f_string(varp, &tmp);
7506 tv_get_string_buf(&tmp, buf);
7507 clear_tv(varp);
7508 *varp = tmp;
7509 return tmp.vval.v_string;
7510 }
7511 return tv_get_string_buf(varp, buf);
7512}
7513
7514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 * Find variable "name" in the list of variables.
7516 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007517 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007518 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007521 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007526 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
Bram Moolenaara7043832005-01-21 11:56:39 +00007528 ht = find_var_ht(name, &varname);
7529 if (htp != NULL)
7530 *htp = ht;
7531 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007533 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7534 if (ret != NULL)
7535 return ret;
7536
7537 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007538 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540
7541/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007542 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007543 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546find_var_in_ht(
7547 hashtab_T *ht,
7548 int htname,
7549 char_u *varname,
7550 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007551{
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 hashitem_T *hi;
7553
7554 if (*varname == NUL)
7555 {
7556 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007557 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007559 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 case 'g': return &globvars_var;
7561 case 'v': return &vimvars_var;
7562 case 'b': return &curbuf->b_bufvar;
7563 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007564 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007565 case 'l': return get_funccal_local_var();
7566 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 }
7568 return NULL;
7569 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007570
7571 hi = hash_find(ht, varname);
7572 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007573 {
7574 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007575 * worked find the variable again. Don't auto-load a script if it was
7576 * loaded already, otherwise it would be loaded every time when
7577 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007578 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007579 {
7580 /* Note: script_autoload() may make "hi" invalid. It must either
7581 * be obtained again or not used. */
7582 if (!script_autoload(varname, FALSE) || aborting())
7583 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007585 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 if (HASHITEM_EMPTY(hi))
7587 return NULL;
7588 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007590}
7591
7592/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007594 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007595 * Set "varname" to the start of name without ':'.
7596 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007597 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007598find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007600 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007601 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007602
Bram Moolenaar73627d02015-08-11 15:46:09 +02007603 if (name[0] == NUL)
7604 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (name[1] != ':')
7606 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007607 /* The name must not start with a colon or #. */
7608 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 return NULL;
7610 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007611
7612 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007613 hi = hash_find(&compat_hashtab, name);
7614 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007615 return &compat_hashtab;
7616
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007617 ht = get_funccal_local_ht();
7618 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007620 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007623 if (*name == 'g') /* global variable */
7624 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007625 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7626 */
7627 if (vim_strchr(name + 2, ':') != NULL
7628 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007629 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007631 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007633 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007634 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007635 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 if (*name == 'v') /* v: variable */
7637 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007638 if (*name == 'a') /* a: function argument */
7639 return get_funccal_args_ht();
7640 if (*name == 'l') /* l: local function variable */
7641 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007643 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7644 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 return NULL;
7646}
7647
7648/*
7649 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007650 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 * Returns NULL when it doesn't exist.
7652 */
7653 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007658 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 if (v == NULL)
7660 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007661 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662}
7663
7664/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 * sourcing this script and when executing functions defined in the script.
7667 */
7668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007669new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaara7043832005-01-21 11:56:39 +00007671 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 hashtab_T *ht;
7673 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007674
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7676 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007677 /* Re-allocating ga_data means that an ht_array pointing to
7678 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007680 for (i = 1; i <= ga_scripts.ga_len; ++i)
7681 {
7682 ht = &SCRIPT_VARS(i);
7683 if (ht->ht_mask == HT_INIT_SIZE - 1)
7684 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007685 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007687 }
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 while (ga_scripts.ga_len < id)
7690 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007691 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007692 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007693 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696 }
7697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7701 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 */
7703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007704init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007707 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007708 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007709 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007710 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 dict_var->di_tv.vval.v_dict = dict;
7712 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7715 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
7718/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007719 * Unreference a dictionary initialized by init_var_dict().
7720 */
7721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007723{
7724 /* Now the dict needs to be freed if no one else is using it, go back to
7725 * normal reference counting. */
7726 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7727 dict_unref(dict);
7728}
7729
7730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 * Frees all allocated variables and the value they contain.
7733 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 */
7735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737{
7738 vars_clear_ext(ht, TRUE);
7739}
7740
7741/*
7742 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7743 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007745vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
Bram Moolenaara7043832005-01-21 11:56:39 +00007747 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 hashitem_T *hi;
7749 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007752 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007753 for (hi = ht->ht_array; todo > 0; ++hi)
7754 {
7755 if (!HASHITEM_EMPTY(hi))
7756 {
7757 --todo;
7758
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007760 * ht_array might change then. hash_clear() takes care of it
7761 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 v = HI2DI(hi);
7763 if (free_val)
7764 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007765 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007767 }
7768 }
7769 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007770 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
Bram Moolenaara7043832005-01-21 11:56:39 +00007773/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 * Delete a variable from hashtab "ht" at item "hi".
7775 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007781
7782 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 clear_tv(&di->di_tv);
7784 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
7787/*
7788 * List the value of one internal variable.
7789 */
7790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007791list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 char_u *tofree;
7794 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007797 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007799 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801}
7802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007804list_one_var_a(
7805 char_u *prefix,
7806 char_u *name,
7807 int type,
7808 char_u *string,
7809 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
Bram Moolenaar31859182007-08-14 20:41:13 +00007811 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7812 msg_start();
7813 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (name != NULL) /* "a:" vars don't have a name stored */
7815 msg_puts(name);
7816 msg_putchar(' ');
7817 msg_advance(22);
7818 if (type == VAR_NUMBER)
7819 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007820 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821 msg_putchar('*');
7822 else if (type == VAR_LIST)
7823 {
7824 msg_putchar('[');
7825 if (*string == '[')
7826 ++string;
7827 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 else if (type == VAR_DICT)
7829 {
7830 msg_putchar('{');
7831 if (*string == '{')
7832 ++string;
7833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 else
7835 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007839 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007841 if (*first)
7842 {
7843 msg_clr_eos();
7844 *first = FALSE;
7845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
7848/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 * If the variable already exists, the value is updated.
7851 * Otherwise the variable is created.
7852 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007854set_var(
7855 char_u *name,
7856 typval_T *tv,
7857 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007863 ht = find_var_ht(name, &varname);
7864 if (ht == NULL || *varname == NUL)
7865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007866 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007867 return;
7868 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007869 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007870
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007871 /* Search in parent scope which is possible to reference from lambda */
7872 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007873 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007874
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007875 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7876 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007877 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007882 if (var_check_ro(v->di_flags, name, FALSE)
7883 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007885
7886 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007887 * Handle setting internal v: variables separately where needed to
7888 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 */
7890 if (ht == &vimvarht)
7891 {
7892 if (v->di_tv.v_type == VAR_STRING)
7893 {
7894 vim_free(v->di_tv.vval.v_string);
7895 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007896 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 else
7898 {
7899 /* Take over the string to avoid an extra alloc/free. */
7900 v->di_tv.vval.v_string = tv->vval.v_string;
7901 tv->vval.v_string = NULL;
7902 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007903 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007905 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007907 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908 if (STRCMP(varname, "searchforward") == 0)
7909 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007910#ifdef FEAT_SEARCH_EXTRA
7911 else if (STRCMP(varname, "hlsearch") == 0)
7912 {
7913 no_hlsearch = !v->di_tv.vval.v_number;
7914 redraw_all_later(SOME_VALID);
7915 }
7916#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007917 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007918 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007919 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007921 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007922 return;
7923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 }
7925
7926 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928 else /* add a new variable */
7929 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007930 /* Can't add "v:" variable. */
7931 if (ht == &vimvarht)
7932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007933 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007934 return;
7935 }
7936
Bram Moolenaar92124a32005-06-17 22:03:40 +00007937 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007938 if (!valid_varname(varname))
7939 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007941 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7942 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007943 if (v == NULL)
7944 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007948 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 return;
7950 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007951 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007953
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007954 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007956 else
7957 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007959 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962}
7963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007965 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 * Also give an error message.
7967 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007969var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007970{
7971 if (flags & DI_FLAGS_RO)
7972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007973 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 return TRUE;
7975 }
7976 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007978 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 return TRUE;
7980 }
7981 return FALSE;
7982}
7983
7984/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007985 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7986 * Also give an error message.
7987 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007989var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007990{
7991 if (flags & DI_FLAGS_FIX)
7992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007993 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007994 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007995 return TRUE;
7996 }
7997 return FALSE;
7998}
7999
8000/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008001 * Check if a funcref is assigned to a valid variable name.
8002 * Return TRUE and give an error if not.
8003 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008005var_check_func_name(
8006 char_u *name, /* points to start of variable name */
8007 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008008{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008009 /* Allow for w: b: s: and t:. */
8010 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008011 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8012 ? name[2] : name[0]))
8013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008014 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008015 name);
8016 return TRUE;
8017 }
8018 /* Don't allow hiding a function. When "v" is not NULL we might be
8019 * assigning another function to the same var, the type is checked
8020 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008021 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008023 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008024 name);
8025 return TRUE;
8026 }
8027 return FALSE;
8028}
8029
8030/*
8031 * Check if a variable name is valid.
8032 * Return FALSE and give an error if not.
8033 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008034 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008035valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008036{
8037 char_u *p;
8038
8039 for (p = varname; *p != NUL; ++p)
8040 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8041 && *p != AUTOLOAD_CHAR)
8042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008043 semsg(_(e_illvar), varname);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008044 return FALSE;
8045 }
8046 return TRUE;
8047}
8048
8049/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008050 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008051 * Also give an error message, using "name" or _("name") when use_gettext is
8052 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008053 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008055tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008056{
8057 if (lock & VAR_LOCKED)
8058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008059 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008060 name == NULL ? (char_u *)_("Unknown")
8061 : use_gettext ? (char_u *)_(name)
8062 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008063 return TRUE;
8064 }
8065 if (lock & VAR_FIXED)
8066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008067 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008068 name == NULL ? (char_u *)_("Unknown")
8069 : use_gettext ? (char_u *)_(name)
8070 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008071 return TRUE;
8072 }
8073 return FALSE;
8074}
8075
8076/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008078 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008080 * It is OK for "from" and "to" to point to the same item. This is used to
8081 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008082 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008084copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008087 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 switch (from->v_type)
8089 {
8090 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008091 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 to->vval.v_number = from->vval.v_number;
8093 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008094 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008095#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096 to->vval.v_float = from->vval.v_float;
8097 break;
8098#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008099 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008100#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008101 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008102 if (to->vval.v_job != NULL)
8103 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008104 break;
8105#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008106 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008107#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008108 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008109 if (to->vval.v_channel != NULL)
8110 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008111 break;
8112#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008113 case VAR_STRING:
8114 case VAR_FUNC:
8115 if (from->vval.v_string == NULL)
8116 to->vval.v_string = NULL;
8117 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008120 if (from->v_type == VAR_FUNC)
8121 func_ref(to->vval.v_string);
8122 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008123 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008124 case VAR_PARTIAL:
8125 if (from->vval.v_partial == NULL)
8126 to->vval.v_partial = NULL;
8127 else
8128 {
8129 to->vval.v_partial = from->vval.v_partial;
8130 ++to->vval.v_partial->pt_refcount;
8131 }
8132 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008133 case VAR_BLOB:
8134 if (from->vval.v_blob == NULL)
8135 to->vval.v_blob = NULL;
8136 else
8137 {
8138 to->vval.v_blob = from->vval.v_blob;
8139 ++to->vval.v_blob->bv_refcount;
8140 }
8141 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 case VAR_LIST:
8143 if (from->vval.v_list == NULL)
8144 to->vval.v_list = NULL;
8145 else
8146 {
8147 to->vval.v_list = from->vval.v_list;
8148 ++to->vval.v_list->lv_refcount;
8149 }
8150 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008151 case VAR_DICT:
8152 if (from->vval.v_dict == NULL)
8153 to->vval.v_dict = NULL;
8154 else
8155 {
8156 to->vval.v_dict = from->vval.v_dict;
8157 ++to->vval.v_dict->dv_refcount;
8158 }
8159 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008160 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008161 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008162 break;
8163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164}
8165
8166/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167 * Make a copy of an item.
8168 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008169 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8170 * reference to an already copied list/dict can be used.
8171 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008172 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008174item_copy(
8175 typval_T *from,
8176 typval_T *to,
8177 int deep,
8178 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179{
8180 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008181 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008185 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008186 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 }
8188 ++recurse;
8189
8190 switch (from->v_type)
8191 {
8192 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 case VAR_STRING:
8195 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008196 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008197 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008198 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008199 case VAR_CHANNEL:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008200 case VAR_BLOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 copy_tv(from, to);
8202 break;
8203 case VAR_LIST:
8204 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008205 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 if (from->vval.v_list == NULL)
8207 to->vval.v_list = NULL;
8208 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8209 {
8210 /* use the copy made earlier */
8211 to->vval.v_list = from->vval.v_list->lv_copylist;
8212 ++to->vval.v_list->lv_refcount;
8213 }
8214 else
8215 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8216 if (to->vval.v_list == NULL)
8217 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 break;
8219 case VAR_DICT:
8220 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008221 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008222 if (from->vval.v_dict == NULL)
8223 to->vval.v_dict = NULL;
8224 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8225 {
8226 /* use the copy made earlier */
8227 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8228 ++to->vval.v_dict->dv_refcount;
8229 }
8230 else
8231 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8232 if (to->vval.v_dict == NULL)
8233 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008234 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008235 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008236 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008237 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008238 }
8239 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008240 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241}
8242
8243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 * This function is used by f_input() and f_inputdialog() functions. The third
8245 * argument to f_input() specifies the type of completion to use at the
8246 * prompt. The third argument to f_inputdialog() specifies the value to return
8247 * when the user cancels the prompt.
8248 */
8249 void
8250get_user_input(
8251 typval_T *argvars,
8252 typval_T *rettv,
8253 int inputdialog,
8254 int secret)
8255{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 char_u *p = NULL;
8258 int c;
8259 char_u buf[NUMBUFLEN];
8260 int cmd_silent_save = cmd_silent;
8261 char_u *defstr = (char_u *)"";
8262 int xp_type = EXPAND_NOTHING;
8263 char_u *xp_arg = NULL;
8264
8265 rettv->v_type = VAR_STRING;
8266 rettv->vval.v_string = NULL;
8267
8268#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008269 /* While starting up, there is no place to enter text. When running tests
8270 * with --not-a-term we assume feedkeys() will be used. */
8271 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 return;
8273#endif
8274
8275 cmd_silent = FALSE; /* Want to see the prompt. */
8276 if (prompt != NULL)
8277 {
8278 /* Only the part of the message after the last NL is considered as
8279 * prompt for the command line */
8280 p = vim_strrchr(prompt, '\n');
8281 if (p == NULL)
8282 p = prompt;
8283 else
8284 {
8285 ++p;
8286 c = *p;
8287 *p = NUL;
8288 msg_start();
8289 msg_clr_eos();
8290 msg_puts_attr(prompt, echo_attr);
8291 msg_didout = FALSE;
8292 msg_starthere();
8293 *p = c;
8294 }
8295 cmdline_row = msg_row;
8296
8297 if (argvars[1].v_type != VAR_UNKNOWN)
8298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008299 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 if (defstr != NULL)
8301 stuffReadbuffSpec(defstr);
8302
8303 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8304 {
8305 char_u *xp_name;
8306 int xp_namelen;
8307 long argt;
8308
8309 /* input() with a third argument: completion */
8310 rettv->vval.v_string = NULL;
8311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008312 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 if (xp_name == NULL)
8314 return;
8315
8316 xp_namelen = (int)STRLEN(xp_name);
8317
8318 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8319 &xp_arg) == FAIL)
8320 return;
8321 }
8322 }
8323
8324 if (defstr != NULL)
8325 {
8326 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 ex_normal_busy = 0;
8329 rettv->vval.v_string =
8330 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8331 xp_type, xp_arg);
8332 ex_normal_busy = save_ex_normal_busy;
8333 }
8334 if (inputdialog && rettv->vval.v_string == NULL
8335 && argvars[1].v_type != VAR_UNKNOWN
8336 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 &argvars[2], buf));
8339
8340 vim_free(xp_arg);
8341
8342 /* since the user typed this, no need to wait for return */
8343 need_wait_return = FALSE;
8344 msg_didout = FALSE;
8345 }
8346 cmd_silent = cmd_silent_save;
8347}
8348
8349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 * ":echo expr1 ..." print each argument separated with a space, add a
8351 * newline at the end.
8352 * ":echon expr1 ..." print each argument plain.
8353 */
8354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008355ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008359 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 char_u *p;
8361 int needclr = TRUE;
8362 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008364 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008365 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
8367 if (eap->skip)
8368 ++emsg_skip;
8369 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8370 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371 /* If eval1() causes an error message the text from the command may
8372 * still need to be cleared. E.g., "echo 22,44". */
8373 need_clr_eos = needclr;
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {
8378 /*
8379 * Report the invalid expression unless the expression evaluation
8380 * has been cancelled due to an aborting error, an interrupt, or an
8381 * exception.
8382 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008383 if (!aborting() && did_emsg == did_emsg_before
8384 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 break;
8388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008389 need_clr_eos = FALSE;
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 if (!eap->skip)
8392 {
8393 if (atstart)
8394 {
8395 atstart = FALSE;
8396 /* Call msg_start() after eval1(), evaluating the expression
8397 * may cause a message to appear. */
8398 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008399 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008400 /* Mark the saved text as finishing the line, so that what
8401 * follows is displayed on a new line when scrolling back
8402 * at the more prompt. */
8403 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 else if (eap->cmdidx == CMD_echo)
8408 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008409 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008410 if (p != NULL)
8411 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008413 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 if (*p != TAB && needclr)
8416 {
8417 /* remove any text still there from the command */
8418 msg_clr_eos();
8419 needclr = FALSE;
8420 }
8421 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
8423 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008424 {
8425#ifdef FEAT_MBYTE
8426 if (has_mbyte)
8427 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008428 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008429
8430 (void)msg_outtrans_len_attr(p, i, echo_attr);
8431 p += i - 1;
8432 }
8433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008435 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 arg = skipwhite(arg);
8442 }
8443 eap->nextcmd = check_nextcmd(arg);
8444
8445 if (eap->skip)
8446 --emsg_skip;
8447 else
8448 {
8449 /* remove text that may still be there from the command */
8450 if (needclr)
8451 msg_clr_eos();
8452 if (eap->cmdidx == CMD_echo)
8453 msg_end();
8454 }
8455}
8456
8457/*
8458 * ":echohl {name}".
8459 */
8460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008461ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008463 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464}
8465
8466/*
8467 * ":execute expr1 ..." execute the result of an expression.
8468 * ":echomsg expr1 ..." Print a message
8469 * ":echoerr expr1 ..." Print an error
8470 * Each gets spaces around each argument and a newline at the end for
8471 * echo commands
8472 */
8473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008474ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 int ret = OK;
8479 char_u *p;
8480 garray_T ga;
8481 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008482 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484 ga_init2(&ga, 1, 80);
8485
8486 if (eap->skip)
8487 ++emsg_skip;
8488 while (*arg != NUL && *arg != '|' && *arg != '\n')
8489 {
8490 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008491 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8492 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494
8495 if (!eap->skip)
8496 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008497 char_u buf[NUMBUFLEN];
8498
8499 if (eap->cmdidx == CMD_execute)
8500 p = tv_get_string_buf(&rettv, buf);
8501 else
8502 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 len = (int)STRLEN(p);
8504 if (ga_grow(&ga, len + 2) == FAIL)
8505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 ret = FAIL;
8508 break;
8509 }
8510 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 ga.ga_len += len;
8514 }
8515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 arg = skipwhite(arg);
8518 }
8519
8520 if (ret != FAIL && ga.ga_data != NULL)
8521 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008522 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8523 {
8524 /* Mark the already saved text as finishing the line, so that what
8525 * follows is displayed on a new line when scrolling back at the
8526 * more prompt. */
8527 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008528 }
8529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008533 out_flush();
8534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 else if (eap->cmdidx == CMD_echoerr)
8536 {
8537 /* We don't want to abort following commands, restore did_emsg. */
8538 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (!force_abort)
8541 did_emsg = save_did_emsg;
8542 }
8543 else if (eap->cmdidx == CMD_execute)
8544 do_cmdline((char_u *)ga.ga_data,
8545 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8546 }
8547
8548 ga_clear(&ga);
8549
8550 if (eap->skip)
8551 --emsg_skip;
8552
8553 eap->nextcmd = check_nextcmd(arg);
8554}
8555
8556/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 * Find window specified by "vp" in tabpage "tp".
8558 */
8559 win_T *
8560find_win_by_nr(
8561 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008562 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008565 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 if (nr < 0)
8568 return NULL;
8569 if (nr == 0)
8570 return curwin;
8571
Bram Moolenaar29323592016-07-24 22:04:11 +02008572 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8573 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 if (nr >= LOWEST_WIN_ID)
8575 {
8576 if (wp->w_id == nr)
8577 return wp;
8578 }
8579 else if (--nr <= 0)
8580 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008581 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 if (nr >= LOWEST_WIN_ID)
8583 return NULL;
8584 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585}
8586
8587/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008588 * Find a window: When using a Window ID in any tab page, when using a number
8589 * in the current tab page.
8590 */
8591 win_T *
8592find_win_by_nr_or_id(typval_T *vp)
8593{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008594 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008595
8596 if (nr >= LOWEST_WIN_ID)
8597 return win_id2wp(vp);
8598 return find_win_by_nr(vp, NULL);
8599}
8600
8601/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 * Find window specified by "wvp" in tabpage "tvp".
8603 */
8604 win_T *
8605find_tabwin(
8606 typval_T *wvp, /* VAR_UNKNOWN for current window */
8607 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8608{
8609 win_T *wp = NULL;
8610 tabpage_T *tp = NULL;
8611 long n;
8612
8613 if (wvp->v_type != VAR_UNKNOWN)
8614 {
8615 if (tvp->v_type != VAR_UNKNOWN)
8616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008617 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 if (n >= 0)
8619 tp = find_tabpage(n);
8620 }
8621 else
8622 tp = curtab;
8623
8624 if (tp != NULL)
8625 wp = find_win_by_nr(wvp, tp);
8626 }
8627 else
8628 wp = curwin;
8629
8630 return wp;
8631}
8632
8633/*
8634 * getwinvar() and gettabwinvar()
8635 */
8636 void
8637getwinvar(
8638 typval_T *argvars,
8639 typval_T *rettv,
8640 int off) /* 1 for gettabwinvar() */
8641{
8642 win_T *win;
8643 char_u *varname;
8644 dictitem_T *v;
8645 tabpage_T *tp = NULL;
8646 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008647 win_T *oldcurwin;
8648 tabpage_T *oldtabpage;
8649 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008652 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 else
8654 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008656 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 ++emsg_off;
8658
8659 rettv->v_type = VAR_STRING;
8660 rettv->vval.v_string = NULL;
8661
8662 if (win != NULL && varname != NULL)
8663 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 /* Set curwin to be our win, temporarily. Also set the tabpage,
8665 * otherwise the window is not valid. Only do this when needed,
8666 * autocommands get blocked. */
8667 need_switch_win = !(tp == curtab && win == curwin);
8668 if (!need_switch_win
8669 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008671 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008673 if (varname[1] == NUL)
8674 {
8675 /* get all window-local options in a dict */
8676 dict_T *opts = get_winbuf_options(FALSE);
8677
8678 if (opts != NULL)
8679 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008680 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008681 done = TRUE;
8682 }
8683 }
8684 else if (get_option_tv(&varname, rettv, 1) == OK)
8685 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 done = TRUE;
8687 }
8688 else
8689 {
8690 /* Look up the variable. */
8691 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8692 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8693 varname, FALSE);
8694 if (v != NULL)
8695 {
8696 copy_tv(&v->di_tv, rettv);
8697 done = TRUE;
8698 }
8699 }
8700 }
8701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 if (need_switch_win)
8703 /* restore previous notion of curwin */
8704 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 }
8706
8707 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8708 /* use the default return value */
8709 copy_tv(&argvars[off + 2], rettv);
8710
8711 --emsg_off;
8712}
8713
8714/*
8715 * "setwinvar()" and "settabwinvar()" functions
8716 */
8717 void
8718setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8719{
8720 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 win_T *save_curwin;
8722 tabpage_T *save_curtab;
8723 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 char_u *varname, *winvarname;
8725 typval_T *varp;
8726 char_u nbuf[NUMBUFLEN];
8727 tabpage_T *tp = NULL;
8728
8729 if (check_restricted() || check_secure())
8730 return;
8731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008733 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 else
8735 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008737 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738 varp = &argvars[off + 2];
8739
8740 if (win != NULL && varname != NULL && varp != NULL)
8741 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 need_switch_win = !(tp == curtab && win == curwin);
8743 if (!need_switch_win
8744 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 {
8746 if (*varname == '&')
8747 {
8748 long numval;
8749 char_u *strval;
8750 int error = FALSE;
8751
8752 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 numval = (long)tv_get_number_chk(varp, &error);
8754 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 if (!error && strval != NULL)
8756 set_option_value(varname, numval, strval, OPT_LOCAL);
8757 }
8758 else
8759 {
8760 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8761 if (winvarname != NULL)
8762 {
8763 STRCPY(winvarname, "w:");
8764 STRCPY(winvarname + 2, varname);
8765 set_var(winvarname, varp, TRUE);
8766 vim_free(winvarname);
8767 }
8768 }
8769 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 if (need_switch_win)
8771 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 }
8773}
8774
8775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8777 * "arg" points to the "&" or '+' when called, to "option" when returning.
8778 * Returns NULL when no option name found. Otherwise pointer to the char
8779 * after the option name.
8780 */
8781 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008782find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
8784 char_u *p = *arg;
8785
8786 ++p;
8787 if (*p == 'g' && p[1] == ':')
8788 {
8789 *opt_flags = OPT_GLOBAL;
8790 p += 2;
8791 }
8792 else if (*p == 'l' && p[1] == ':')
8793 {
8794 *opt_flags = OPT_LOCAL;
8795 p += 2;
8796 }
8797 else
8798 *opt_flags = 0;
8799
8800 if (!ASCII_ISALPHA(*p))
8801 return NULL;
8802 *arg = p;
8803
8804 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8805 p += 4; /* termcap option */
8806 else
8807 while (ASCII_ISALPHA(*p))
8808 ++p;
8809 return p;
8810}
8811
8812/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008813 * Return the autoload script name for a function or variable name.
8814 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008816 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008817autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008818{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008819 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008820 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008821
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008822 /* Get the script file name: replace '#' with '/', append ".vim". */
8823 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8824 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008825 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008826 STRCPY(scriptname, "autoload/");
8827 STRCAT(scriptname, name);
8828 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8829 STRCAT(scriptname, ".vim");
8830 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8831 *p = '/';
8832 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833}
8834
8835/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008836 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008837 * Return TRUE if a package was loaded.
8838 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008840script_autoload(
8841 char_u *name,
8842 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008843{
8844 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008845 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008846 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008847 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008849 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008850 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008851 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 return FALSE;
8853
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008854 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008855
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008856 /* Find the name in the list of previously loaded package names. Skip
8857 * "autoload/", it's always the same. */
8858 for (i = 0; i < ga_loaded.ga_len; ++i)
8859 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8860 break;
8861 if (!reload && i < ga_loaded.ga_len)
8862 ret = FALSE; /* was loaded already */
8863 else
8864 {
8865 /* Remember the name if it wasn't loaded already. */
8866 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8867 {
8868 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8869 tofree = NULL;
8870 }
8871
8872 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008873 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008874 ret = TRUE;
8875 }
8876
8877 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008878 return ret;
8879}
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8882typedef enum
8883{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008884 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8885 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8886 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887} var_flavour_T;
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008890var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891{
8892 char_u *p = varname;
8893
8894 if (ASCII_ISUPPER(*p))
8895 {
8896 while (*(++p))
8897 if (ASCII_ISLOWER(*p))
8898 return VAR_FLAVOUR_SESSION;
8899 return VAR_FLAVOUR_VIMINFO;
8900 }
8901 else
8902 return VAR_FLAVOUR_DEFAULT;
8903}
8904#endif
8905
8906#if defined(FEAT_VIMINFO) || defined(PROTO)
8907/*
8908 * Restore global vars that start with a capital from the viminfo file
8909 */
8910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008911read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912{
8913 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008914 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008916 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
8918 if (!writing && (find_viminfo_parameter('!') != NULL))
8919 {
8920 tab = vim_strchr(virp->vir_line + 1, '\t');
8921 if (tab != NULL)
8922 {
8923 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008924 switch (*tab)
8925 {
8926 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008927#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008928 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008930 case 'D': type = VAR_DICT; break;
8931 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008932 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008933 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
8936 tab = vim_strchr(tab, '\t');
8937 if (tab != NULL)
8938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008939 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008940 if (type == VAR_STRING || type == VAR_DICT
8941 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008942 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#ifdef FEAT_FLOAT
8945 else if (type == VAR_FLOAT)
8946 (void)string2float(tab + 1, &tv.vval.v_float);
8947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008949 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008950 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008951 {
8952 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8953
8954 if (etv == NULL)
8955 /* Failed to parse back the dict or list, use it as a
8956 * string. */
8957 tv.v_type = VAR_STRING;
8958 else
8959 {
8960 vim_free(tv.vval.v_string);
8961 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008962 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008963 }
8964 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008965 else if (type == VAR_BLOB)
8966 {
8967 blob_T *blob = string2blob(tv.vval.v_string);
8968
8969 if (blob == NULL)
8970 // Failed to parse back the blob, use it as a string.
8971 tv.v_type = VAR_STRING;
8972 else
8973 {
8974 vim_free(tv.vval.v_string);
8975 tv.v_type = VAR_BLOB;
8976 tv.vval.v_blob = blob;
8977 }
8978 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008979
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008980 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008981 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008982 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008983 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008984
8985 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008986 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008987 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8988 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008989 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 }
8991 }
8992 }
8993
8994 return viminfo_readline(virp);
8995}
8996
8997/*
8998 * Write global vars that start with a capital to the viminfo file
8999 */
9000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009001write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002{
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 hashitem_T *hi;
9004 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009005 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009006 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009007 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
9011 if (find_viminfo_parameter('!') == NULL)
9012 return;
9013
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009014 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009015
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009016 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009019 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009021 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009022 this_var = HI2DI(hi);
9023 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009025 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009026 {
9027 case VAR_STRING: s = "STR"; break;
9028 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009029 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009030 case VAR_DICT: s = "DIC"; break;
9031 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009032 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009033 case VAR_SPECIAL: s = "XPL"; break;
9034
9035 case VAR_UNKNOWN:
9036 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009037 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009038 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009039 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009040 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009041 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009043 if (this_var->di_tv.v_type == VAR_SPECIAL)
9044 {
9045 sprintf((char *)numbuf, "%ld",
9046 (long)this_var->di_tv.vval.v_number);
9047 p = numbuf;
9048 tofree = NULL;
9049 }
9050 else
9051 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009052 if (p != NULL)
9053 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009054 vim_free(tofree);
9055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 }
9057 }
9058}
9059#endif
9060
9061#if defined(FEAT_SESSION) || defined(PROTO)
9062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 hashitem_T *hi;
9066 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009067 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 char_u *p, *t;
9069
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009070 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009071 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009073 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009075 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 this_var = HI2DI(hi);
9077 if ((this_var->di_tv.v_type == VAR_NUMBER
9078 || this_var->di_tv.v_type == VAR_STRING)
9079 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009080 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009081 /* Escape special characters with a backslash. Turn a LF and
9082 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009083 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009084 (char_u *)"\\\"\n\r");
9085 if (p == NULL) /* out of memory */
9086 break;
9087 for (t = p; *t != NUL; ++t)
9088 if (*t == '\n')
9089 *t = 'n';
9090 else if (*t == '\r')
9091 *t = 'r';
9092 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009093 this_var->di_key,
9094 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9095 : ' ',
9096 p,
9097 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9098 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009099 || put_eol(fd) == FAIL)
9100 {
9101 vim_free(p);
9102 return FAIL;
9103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 vim_free(p);
9105 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009106#ifdef FEAT_FLOAT
9107 else if (this_var->di_tv.v_type == VAR_FLOAT
9108 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9109 {
9110 float_T f = this_var->di_tv.vval.v_float;
9111 int sign = ' ';
9112
9113 if (f < 0)
9114 {
9115 f = -f;
9116 sign = '-';
9117 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009118 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009119 this_var->di_key, sign, f) < 0)
9120 || put_eol(fd) == FAIL)
9121 return FAIL;
9122 }
9123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 }
9125 }
9126 return OK;
9127}
9128#endif
9129
Bram Moolenaar661b1822005-07-28 22:36:45 +00009130/*
9131 * Display script name where an item was last set.
9132 * Should only be invoked when 'verbose' is non-zero.
9133 */
9134 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009135last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009136{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009137 char_u *p;
9138
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009139 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009140 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009141 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009142 if (p != NULL)
9143 {
9144 verbose_enter();
9145 MSG_PUTS(_("\n\tLast set from "));
9146 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009147 if (script_ctx.sc_lnum > 0)
9148 {
9149 MSG_PUTS(_(" line "));
9150 msg_outnum((long)script_ctx.sc_lnum);
9151 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009152 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009153 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009154 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009155 }
9156}
9157
Bram Moolenaar53744302015-07-17 17:38:22 +02009158/* reset v:option_new, v:option_old and v:option_type */
9159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009160reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009161{
9162 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9163 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9164 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9165}
9166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167/*
9168 * Prepare "gap" for an assert error and add the sourcing position.
9169 */
9170 void
9171prepare_assert_error(garray_T *gap)
9172{
9173 char buf[NUMBUFLEN];
9174
9175 ga_init2(gap, 1, 100);
9176 if (sourcing_name != NULL)
9177 {
9178 ga_concat(gap, sourcing_name);
9179 if (sourcing_lnum > 0)
9180 ga_concat(gap, (char_u *)" ");
9181 }
9182 if (sourcing_lnum > 0)
9183 {
9184 sprintf(buf, "line %ld", (long)sourcing_lnum);
9185 ga_concat(gap, (char_u *)buf);
9186 }
9187 if (sourcing_name != NULL || sourcing_lnum > 0)
9188 ga_concat(gap, (char_u *)": ");
9189}
9190
9191/*
9192 * Add an assert error to v:errors.
9193 */
9194 void
9195assert_error(garray_T *gap)
9196{
9197 struct vimvar *vp = &vimvars[VV_ERRORS];
9198
9199 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9200 /* Make sure v:errors is a list. */
9201 set_vim_var_list(VV_ERRORS, list_alloc());
9202 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9203}
9204
Bram Moolenaar65a54642018-04-28 16:56:53 +02009205 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206assert_equal_common(typval_T *argvars, assert_type_T atype)
9207{
9208 garray_T ga;
9209
9210 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9211 != (atype == ASSERT_EQUAL))
9212 {
9213 prepare_assert_error(&ga);
9214 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9215 atype);
9216 assert_error(&ga);
9217 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009218 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009220 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221}
9222
Bram Moolenaar65a54642018-04-28 16:56:53 +02009223 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009224assert_equalfile(typval_T *argvars)
9225{
9226 char_u buf1[NUMBUFLEN];
9227 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009228 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9229 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009230 garray_T ga;
9231 FILE *fd1;
9232 FILE *fd2;
9233
9234 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009235 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009236
9237 IObuff[0] = NUL;
9238 fd1 = mch_fopen((char *)fname1, READBIN);
9239 if (fd1 == NULL)
9240 {
9241 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9242 }
9243 else
9244 {
9245 fd2 = mch_fopen((char *)fname2, READBIN);
9246 if (fd2 == NULL)
9247 {
9248 fclose(fd1);
9249 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9250 }
9251 else
9252 {
9253 int c1, c2;
9254 long count = 0;
9255
9256 for (;;)
9257 {
9258 c1 = fgetc(fd1);
9259 c2 = fgetc(fd2);
9260 if (c1 == EOF)
9261 {
9262 if (c2 != EOF)
9263 STRCPY(IObuff, "first file is shorter");
9264 break;
9265 }
9266 else if (c2 == EOF)
9267 {
9268 STRCPY(IObuff, "second file is shorter");
9269 break;
9270 }
9271 else if (c1 != c2)
9272 {
9273 vim_snprintf((char *)IObuff, IOSIZE,
9274 "difference at byte %ld", count);
9275 break;
9276 }
9277 ++count;
9278 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009279 fclose(fd1);
9280 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009281 }
9282 }
9283 if (IObuff[0] != NUL)
9284 {
9285 prepare_assert_error(&ga);
9286 ga_concat(&ga, IObuff);
9287 assert_error(&ga);
9288 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009289 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009290 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009291 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009292}
9293
Bram Moolenaar65a54642018-04-28 16:56:53 +02009294 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295assert_match_common(typval_T *argvars, assert_type_T atype)
9296{
9297 garray_T ga;
9298 char_u buf1[NUMBUFLEN];
9299 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009300 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9301 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302
9303 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009304 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009305 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9306 {
9307 prepare_assert_error(&ga);
9308 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9309 atype);
9310 assert_error(&ga);
9311 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009312 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009314 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009315}
9316
Bram Moolenaar65a54642018-04-28 16:56:53 +02009317 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009318assert_inrange(typval_T *argvars)
9319{
9320 garray_T ga;
9321 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009322 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9323 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9324 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009325 char_u *tofree;
9326 char msg[200];
9327 char_u numbuf[NUMBUFLEN];
9328
9329 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009330 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009331 if (actual < lower || actual > upper)
9332 {
9333 prepare_assert_error(&ga);
9334 if (argvars[3].v_type != VAR_UNKNOWN)
9335 {
9336 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9337 vim_free(tofree);
9338 }
9339 else
9340 {
9341 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9342 (long)lower, (long)upper, (long)actual);
9343 ga_concat(&ga, (char_u *)msg);
9344 }
9345 assert_error(&ga);
9346 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009347 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009348 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009349 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009350}
9351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352/*
9353 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009354 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009356 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357assert_bool(typval_T *argvars, int isTrue)
9358{
9359 int error = FALSE;
9360 garray_T ga;
9361
9362 if (argvars[0].v_type == VAR_SPECIAL
9363 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009364 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009366 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367 || error)
9368 {
9369 prepare_assert_error(&ga);
9370 fill_assert_error(&ga, &argvars[1],
9371 (char_u *)(isTrue ? "True" : "False"),
9372 NULL, &argvars[0], ASSERT_OTHER);
9373 assert_error(&ga);
9374 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009375 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009377 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378}
9379
Bram Moolenaar65a54642018-04-28 16:56:53 +02009380 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009381assert_report(typval_T *argvars)
9382{
9383 garray_T ga;
9384
9385 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009386 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009387 assert_error(&ga);
9388 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009389 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009390}
9391
Bram Moolenaar65a54642018-04-28 16:56:53 +02009392 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393assert_exception(typval_T *argvars)
9394{
9395 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009396 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397
9398 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9399 {
9400 prepare_assert_error(&ga);
9401 ga_concat(&ga, (char_u *)"v:exception is not set");
9402 assert_error(&ga);
9403 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009404 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009405 }
9406 else if (error != NULL
9407 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9408 {
9409 prepare_assert_error(&ga);
9410 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9411 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9412 assert_error(&ga);
9413 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009414 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009416 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417}
9418
Bram Moolenaar65a54642018-04-28 16:56:53 +02009419 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009420assert_beeps(typval_T *argvars)
9421{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009422 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009423 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009424 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009425
9426 called_vim_beep = FALSE;
9427 suppress_errthrow = TRUE;
9428 emsg_silent = FALSE;
9429 do_cmdline_cmd(cmd);
9430 if (!called_vim_beep)
9431 {
9432 prepare_assert_error(&ga);
9433 ga_concat(&ga, (char_u *)"command did not beep: ");
9434 ga_concat(&ga, cmd);
9435 assert_error(&ga);
9436 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009437 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009438 }
9439
9440 suppress_errthrow = FALSE;
9441 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009442 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009443}
9444
Bram Moolenaar65a54642018-04-28 16:56:53 +02009445 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446assert_fails(typval_T *argvars)
9447{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009448 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009450 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009451 char_u numbuf[NUMBUFLEN];
9452 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453
9454 called_emsg = FALSE;
9455 suppress_errthrow = TRUE;
9456 emsg_silent = TRUE;
9457 do_cmdline_cmd(cmd);
9458 if (!called_emsg)
9459 {
9460 prepare_assert_error(&ga);
9461 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009462 if (argvars[1].v_type != VAR_UNKNOWN
9463 && argvars[2].v_type != VAR_UNKNOWN)
9464 {
9465 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9466 vim_free(tofree);
9467 }
9468 else
9469 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 assert_error(&ga);
9471 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009472 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 }
9474 else if (argvars[1].v_type != VAR_UNKNOWN)
9475 {
9476 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009477 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478
9479 if (error == NULL
9480 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9481 {
9482 prepare_assert_error(&ga);
9483 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9484 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9485 assert_error(&ga);
9486 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009487 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488 }
9489 }
9490
9491 called_emsg = FALSE;
9492 suppress_errthrow = FALSE;
9493 emsg_silent = FALSE;
9494 emsg_on_display = FALSE;
9495 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009496 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009497}
9498
9499/*
9500 * Append "str" to "gap", escaping unprintable characters.
9501 * Changes NL to \n, CR to \r, etc.
9502 */
9503 static void
9504ga_concat_esc(garray_T *gap, char_u *str)
9505{
9506 char_u *p;
9507 char_u buf[NUMBUFLEN];
9508
9509 if (str == NULL)
9510 {
9511 ga_concat(gap, (char_u *)"NULL");
9512 return;
9513 }
9514
9515 for (p = str; *p != NUL; ++p)
9516 switch (*p)
9517 {
9518 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9519 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9520 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9521 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9522 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9523 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9524 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9525 default:
9526 if (*p < ' ')
9527 {
9528 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9529 ga_concat(gap, buf);
9530 }
9531 else
9532 ga_append(gap, *p);
9533 break;
9534 }
9535}
9536
9537/*
9538 * Fill "gap" with information about an assert error.
9539 */
9540 void
9541fill_assert_error(
9542 garray_T *gap,
9543 typval_T *opt_msg_tv,
9544 char_u *exp_str,
9545 typval_T *exp_tv,
9546 typval_T *got_tv,
9547 assert_type_T atype)
9548{
9549 char_u numbuf[NUMBUFLEN];
9550 char_u *tofree;
9551
9552 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9553 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009554 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9555 vim_free(tofree);
9556 ga_concat(gap, (char_u *)": ");
9557 }
9558
9559 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9560 ga_concat(gap, (char_u *)"Pattern ");
9561 else if (atype == ASSERT_NOTEQUAL)
9562 ga_concat(gap, (char_u *)"Expected not equal to ");
9563 else
9564 ga_concat(gap, (char_u *)"Expected ");
9565 if (exp_str == NULL)
9566 {
9567 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 vim_free(tofree);
9569 }
9570 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009571 ga_concat_esc(gap, exp_str);
9572 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009574 if (atype == ASSERT_MATCH)
9575 ga_concat(gap, (char_u *)" does not match ");
9576 else if (atype == ASSERT_NOTMATCH)
9577 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009579 ga_concat(gap, (char_u *)" but got ");
9580 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9581 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 }
9583}
9584
Bram Moolenaar31988702018-02-13 12:57:42 +01009585/*
9586 * Compare "typ1" and "typ2". Put the result in "typ1".
9587 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009588 int
9589typval_compare(
9590 typval_T *typ1, /* first operand */
9591 typval_T *typ2, /* second operand */
9592 exptype_T type, /* operator */
9593 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009594 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009595{
9596 int i;
9597 varnumber_T n1, n2;
9598 char_u *s1, *s2;
9599 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9600
Bram Moolenaar31988702018-02-13 12:57:42 +01009601 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009602 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009603 /* For "is" a different type always means FALSE, for "notis"
9604 * it means TRUE. */
9605 n1 = (type == TYPE_NEQUAL);
9606 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009607 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9608 {
9609 if (type_is)
9610 {
9611 n1 = (typ1->v_type == typ2->v_type
9612 && typ1->vval.v_blob == typ2->vval.v_blob);
9613 if (type == TYPE_NEQUAL)
9614 n1 = !n1;
9615 }
9616 else if (typ1->v_type != typ2->v_type
9617 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9618 {
9619 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009620 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009621 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009622 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009623 clear_tv(typ1);
9624 return FAIL;
9625 }
9626 else
9627 {
9628 // Compare two Blobs for being equal or unequal.
9629 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9630 if (type == TYPE_NEQUAL)
9631 n1 = !n1;
9632 }
9633 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009634 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9635 {
9636 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009637 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009638 n1 = (typ1->v_type == typ2->v_type
9639 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009640 if (type == TYPE_NEQUAL)
9641 n1 = !n1;
9642 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009643 else if (typ1->v_type != typ2->v_type
9644 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009645 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009646 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009647 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009648 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009649 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009650 clear_tv(typ1);
9651 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009652 }
9653 else
9654 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009655 /* Compare two Lists for being equal or unequal. */
9656 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9657 ic, FALSE);
9658 if (type == TYPE_NEQUAL)
9659 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009660 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009661 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009662
9663 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9664 {
9665 if (type_is)
9666 {
9667 n1 = (typ1->v_type == typ2->v_type
9668 && typ1->vval.v_dict == typ2->vval.v_dict);
9669 if (type == TYPE_NEQUAL)
9670 n1 = !n1;
9671 }
9672 else if (typ1->v_type != typ2->v_type
9673 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9674 {
9675 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009676 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009677 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009678 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009679 clear_tv(typ1);
9680 return FAIL;
9681 }
9682 else
9683 {
9684 /* Compare two Dictionaries for being equal or unequal. */
9685 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9686 ic, FALSE);
9687 if (type == TYPE_NEQUAL)
9688 n1 = !n1;
9689 }
9690 }
9691
9692 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9693 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9694 {
9695 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009697 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009698 clear_tv(typ1);
9699 return FAIL;
9700 }
9701 if ((typ1->v_type == VAR_PARTIAL
9702 && typ1->vval.v_partial == NULL)
9703 || (typ2->v_type == VAR_PARTIAL
9704 && typ2->vval.v_partial == NULL))
9705 /* when a partial is NULL assume not equal */
9706 n1 = FALSE;
9707 else if (type_is)
9708 {
9709 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9710 /* strings are considered the same if their value is
9711 * the same */
9712 n1 = tv_equal(typ1, typ2, ic, FALSE);
9713 else if (typ1->v_type == VAR_PARTIAL
9714 && typ2->v_type == VAR_PARTIAL)
9715 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9716 else
9717 n1 = FALSE;
9718 }
9719 else
9720 n1 = tv_equal(typ1, typ2, ic, FALSE);
9721 if (type == TYPE_NEQUAL)
9722 n1 = !n1;
9723 }
9724
9725#ifdef FEAT_FLOAT
9726 /*
9727 * If one of the two variables is a float, compare as a float.
9728 * When using "=~" or "!~", always compare as string.
9729 */
9730 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9731 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9732 {
9733 float_T f1, f2;
9734
9735 if (typ1->v_type == VAR_FLOAT)
9736 f1 = typ1->vval.v_float;
9737 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009738 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009739 if (typ2->v_type == VAR_FLOAT)
9740 f2 = typ2->vval.v_float;
9741 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009742 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009743 n1 = FALSE;
9744 switch (type)
9745 {
9746 case TYPE_EQUAL: n1 = (f1 == f2); break;
9747 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9748 case TYPE_GREATER: n1 = (f1 > f2); break;
9749 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9750 case TYPE_SMALLER: n1 = (f1 < f2); break;
9751 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9752 case TYPE_UNKNOWN:
9753 case TYPE_MATCH:
9754 case TYPE_NOMATCH: break; /* avoid gcc warning */
9755 }
9756 }
9757#endif
9758
9759 /*
9760 * If one of the two variables is a number, compare as a number.
9761 * When using "=~" or "!~", always compare as string.
9762 */
9763 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9764 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009766 n1 = tv_get_number(typ1);
9767 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009768 switch (type)
9769 {
9770 case TYPE_EQUAL: n1 = (n1 == n2); break;
9771 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9772 case TYPE_GREATER: n1 = (n1 > n2); break;
9773 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9774 case TYPE_SMALLER: n1 = (n1 < n2); break;
9775 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9776 case TYPE_UNKNOWN:
9777 case TYPE_MATCH:
9778 case TYPE_NOMATCH: break; /* avoid gcc warning */
9779 }
9780 }
9781 else
9782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009783 s1 = tv_get_string_buf(typ1, buf1);
9784 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009785 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9786 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9787 else
9788 i = 0;
9789 n1 = FALSE;
9790 switch (type)
9791 {
9792 case TYPE_EQUAL: n1 = (i == 0); break;
9793 case TYPE_NEQUAL: n1 = (i != 0); break;
9794 case TYPE_GREATER: n1 = (i > 0); break;
9795 case TYPE_GEQUAL: n1 = (i >= 0); break;
9796 case TYPE_SMALLER: n1 = (i < 0); break;
9797 case TYPE_SEQUAL: n1 = (i <= 0); break;
9798
9799 case TYPE_MATCH:
9800 case TYPE_NOMATCH:
9801 n1 = pattern_match(s2, s1, ic);
9802 if (type == TYPE_NOMATCH)
9803 n1 = !n1;
9804 break;
9805
9806 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9807 }
9808 }
9809 clear_tv(typ1);
9810 typ1->v_type = VAR_NUMBER;
9811 typ1->vval.v_number = n1;
9812
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009813 return OK;
9814}
9815
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009816 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009817typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009818{
9819 char_u *tofree;
9820 char_u numbuf[NUMBUFLEN];
9821 char_u *ret = NULL;
9822
9823 if (arg == NULL)
9824 return vim_strsave((char_u *)"(does not exist)");
9825 ret = tv2string(arg, &tofree, numbuf, 0);
9826 /* Make a copy if we have a value but it's not in allocated memory. */
9827 if (ret != NULL && tofree == NULL)
9828 ret = vim_strsave(ret);
9829 return ret;
9830}
9831
9832 int
9833var_exists(char_u *var)
9834{
9835 char_u *name;
9836 char_u *tofree;
9837 typval_T tv;
9838 int len = 0;
9839 int n = FALSE;
9840
9841 /* get_name_len() takes care of expanding curly braces */
9842 name = var;
9843 len = get_name_len(&var, &tofree, TRUE, FALSE);
9844 if (len > 0)
9845 {
9846 if (tofree != NULL)
9847 name = tofree;
9848 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9849 if (n)
9850 {
9851 /* handle d.key, l[idx], f(expr) */
9852 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9853 if (n)
9854 clear_tv(&tv);
9855 }
9856 }
9857 if (*var != NUL)
9858 n = FALSE;
9859
9860 vim_free(tofree);
9861 return n;
9862}
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864#endif /* FEAT_EVAL */
9865
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009867#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
9869#ifdef WIN3264
9870/*
9871 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
9874/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009875 * Get the short path (8.3) for the filename in "fnamep".
9876 * Only works for a valid file name.
9877 * When the path gets longer "fnamep" is changed and the allocated buffer
9878 * is put in "bufp".
9879 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9880 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 */
9882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009883get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009885 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 char_u *newbuf;
9887
9888 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009889 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 if (l > len - 1)
9891 {
9892 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009893 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 newbuf = vim_strnsave(*fnamep, l);
9895 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898 vim_free(*bufp);
9899 *fnamep = *bufp = newbuf;
9900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009901 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009902 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 }
9904
9905 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009906 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907}
9908
9909/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009910 * Get the short path (8.3) for the filename in "fname". The converted
9911 * path is returned in "bufp".
9912 *
9913 * Some of the directories specified in "fname" may not exist. This function
9914 * will shorten the existing directories at the beginning of the path and then
9915 * append the remaining non-existing path.
9916 *
9917 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009918 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009919 * bufp - Pointer to an allocated buffer for the filename.
9920 * fnamelen - Length of the filename pointed to by fname
9921 *
9922 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 */
9924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009925shortpath_for_invalid_fname(
9926 char_u **fname,
9927 char_u **bufp,
9928 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009930 char_u *short_fname, *save_fname, *pbuf_unused;
9931 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009933 int old_len, len;
9934 int new_len, sfx_len;
9935 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
9937 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009938 old_len = *fnamelen;
9939 save_fname = vim_strnsave(*fname, old_len);
9940 pbuf_unused = NULL;
9941 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009943 endp = save_fname + old_len - 1; /* Find the end of the copy */
9944 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009946 /*
9947 * Try shortening the supplied path till it succeeds by removing one
9948 * directory at a time from the tail of the path.
9949 */
9950 len = 0;
9951 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009953 /* go back one path-separator */
9954 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9955 --endp;
9956 if (endp <= save_fname)
9957 break; /* processed the complete path */
9958
9959 /*
9960 * Replace the path separator with a NUL and try to shorten the
9961 * resulting path.
9962 */
9963 ch = *endp;
9964 *endp = 0;
9965 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009966 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009967 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9968 {
9969 retval = FAIL;
9970 goto theend;
9971 }
9972 *endp = ch; /* preserve the string */
9973
9974 if (len > 0)
9975 break; /* successfully shortened the path */
9976
9977 /* failed to shorten the path. Skip the path separator */
9978 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 }
9980
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009981 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009983 /*
9984 * Succeeded in shortening the path. Now concatenate the shortened
9985 * path with the remaining path at the tail.
9986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009988 /* Compute the length of the new path. */
9989 sfx_len = (int)(save_endp - endp) + 1;
9990 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009992 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009994 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009996 /* There is not enough space in the currently allocated string,
9997 * copy it to a buffer big enough. */
9998 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010000 {
10001 retval = FAIL;
10002 goto theend;
10003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 }
10005 else
10006 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010007 /* Transfer short_fname to the main buffer (it's big enough),
10008 * unless get_short_pathname() did its work in-place. */
10009 *fname = *bufp = save_fname;
10010 if (short_fname != save_fname)
10011 vim_strncpy(save_fname, short_fname, len);
10012 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010014
10015 /* concat the not-shortened part of the path */
10016 vim_strncpy(*fname + len, endp, sfx_len);
10017 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010019
10020theend:
10021 vim_free(pbuf_unused);
10022 vim_free(save_fname);
10023
10024 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025}
10026
10027/*
10028 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010029 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 */
10031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010032shortpath_for_partial(
10033 char_u **fnamep,
10034 char_u **bufp,
10035 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 int sepcount, len, tflen;
10038 char_u *p;
10039 char_u *pbuf, *tfname;
10040 int hasTilde;
10041
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010042 /* Count up the path separators from the RHS.. so we know which part
10043 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010045 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (vim_ispathsep(*p))
10047 ++sepcount;
10048
10049 /* Need full path first (use expand_env() to remove a "~/") */
10050 hasTilde = (**fnamep == '~');
10051 if (hasTilde)
10052 pbuf = tfname = expand_env_save(*fnamep);
10053 else
10054 pbuf = tfname = FullName_save(*fnamep, FALSE);
10055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010056 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010058 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10059 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
10061 if (len == 0)
10062 {
10063 /* Don't have a valid filename, so shorten the rest of the
10064 * path if we can. This CAN give us invalid 8.3 filenames, but
10065 * there's not a lot of point in guessing what it might be.
10066 */
10067 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010068 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10069 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 }
10071
10072 /* Count the paths backward to find the beginning of the desired string. */
10073 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010074 {
10075#ifdef FEAT_MBYTE
10076 if (has_mbyte)
10077 p -= mb_head_off(tfname, p);
10078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (vim_ispathsep(*p))
10080 {
10081 if (sepcount == 0 || (hasTilde && sepcount == 1))
10082 break;
10083 else
10084 sepcount --;
10085 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 if (hasTilde)
10088 {
10089 --p;
10090 if (p >= tfname)
10091 *p = '~';
10092 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010093 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 }
10095 else
10096 ++p;
10097
10098 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10099 vim_free(*bufp);
10100 *fnamelen = (int)STRLEN(p);
10101 *bufp = pbuf;
10102 *fnamep = p;
10103
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010104 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105}
10106#endif /* WIN3264 */
10107
10108/*
10109 * Adjust a filename, according to a string of modifiers.
10110 * *fnamep must be NUL terminated when called. When returning, the length is
10111 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010112 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 * When there is an error, *fnamep is set to NULL.
10114 */
10115 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010116modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010117 char_u *src, // string with modifiers
10118 int tilde_file, // "~" is a file name, not $HOME
10119 int *usedlen, // characters after src that are used
10120 char_u **fnamep, // file name so far
10121 char_u **bufp, // buffer for allocated file name or NULL
10122 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123{
10124 int valid = 0;
10125 char_u *tail;
10126 char_u *s, *p, *pbuf;
10127 char_u dirname[MAXPATHL];
10128 int c;
10129 int has_fullname = 0;
10130#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010131 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 int has_shortname = 0;
10133#endif
10134
10135repeat:
10136 /* ":p" - full path/file_name */
10137 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10138 {
10139 has_fullname = 1;
10140
10141 valid |= VALID_PATH;
10142 *usedlen += 2;
10143
10144 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10145 if ((*fnamep)[0] == '~'
10146#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10147 && ((*fnamep)[1] == '/'
10148# ifdef BACKSLASH_IN_FILENAME
10149 || (*fnamep)[1] == '\\'
10150# endif
10151 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010153 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 )
10155 {
10156 *fnamep = expand_env_save(*fnamep);
10157 vim_free(*bufp); /* free any allocated file name */
10158 *bufp = *fnamep;
10159 if (*fnamep == NULL)
10160 return -1;
10161 }
10162
10163 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010164 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 {
10166 if (vim_ispathsep(*p)
10167 && p[1] == '.'
10168 && (p[2] == NUL
10169 || vim_ispathsep(p[2])
10170 || (p[2] == '.'
10171 && (p[3] == NUL || vim_ispathsep(p[3])))))
10172 break;
10173 }
10174
10175 /* FullName_save() is slow, don't use it when not needed. */
10176 if (*p != NUL || !vim_isAbsName(*fnamep))
10177 {
10178 *fnamep = FullName_save(*fnamep, *p != NUL);
10179 vim_free(*bufp); /* free any allocated file name */
10180 *bufp = *fnamep;
10181 if (*fnamep == NULL)
10182 return -1;
10183 }
10184
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010185#ifdef WIN3264
10186# if _WIN32_WINNT >= 0x0500
10187 if (vim_strchr(*fnamep, '~') != NULL)
10188 {
10189 /* Expand 8.3 filename to full path. Needed to make sure the same
10190 * file does not have two different names.
10191 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10192 p = alloc(_MAX_PATH + 1);
10193 if (p != NULL)
10194 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010195 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010196 {
10197 vim_free(*bufp);
10198 *bufp = *fnamep = p;
10199 }
10200 else
10201 vim_free(p);
10202 }
10203 }
10204# endif
10205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 /* Append a path separator to a directory. */
10207 if (mch_isdir(*fnamep))
10208 {
10209 /* Make room for one or two extra characters. */
10210 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10211 vim_free(*bufp); /* free any allocated file name */
10212 *bufp = *fnamep;
10213 if (*fnamep == NULL)
10214 return -1;
10215 add_pathsep(*fnamep);
10216 }
10217 }
10218
10219 /* ":." - path relative to the current directory */
10220 /* ":~" - path relative to the home directory */
10221 /* ":8" - shortname path - postponed till after */
10222 while (src[*usedlen] == ':'
10223 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10224 {
10225 *usedlen += 2;
10226 if (c == '8')
10227 {
10228#ifdef WIN3264
10229 has_shortname = 1; /* Postpone this. */
10230#endif
10231 continue;
10232 }
10233 pbuf = NULL;
10234 /* Need full path first (use expand_env() to remove a "~/") */
10235 if (!has_fullname)
10236 {
10237 if (c == '.' && **fnamep == '~')
10238 p = pbuf = expand_env_save(*fnamep);
10239 else
10240 p = pbuf = FullName_save(*fnamep, FALSE);
10241 }
10242 else
10243 p = *fnamep;
10244
10245 has_fullname = 0;
10246
10247 if (p != NULL)
10248 {
10249 if (c == '.')
10250 {
10251 mch_dirname(dirname, MAXPATHL);
10252 s = shorten_fname(p, dirname);
10253 if (s != NULL)
10254 {
10255 *fnamep = s;
10256 if (pbuf != NULL)
10257 {
10258 vim_free(*bufp); /* free any allocated file name */
10259 *bufp = pbuf;
10260 pbuf = NULL;
10261 }
10262 }
10263 }
10264 else
10265 {
10266 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10267 /* Only replace it when it starts with '~' */
10268 if (*dirname == '~')
10269 {
10270 s = vim_strsave(dirname);
10271 if (s != NULL)
10272 {
10273 *fnamep = s;
10274 vim_free(*bufp);
10275 *bufp = s;
10276 }
10277 }
10278 }
10279 vim_free(pbuf);
10280 }
10281 }
10282
10283 tail = gettail(*fnamep);
10284 *fnamelen = (int)STRLEN(*fnamep);
10285
10286 /* ":h" - head, remove "/file_name", can be repeated */
10287 /* Don't remove the first "/" or "c:\" */
10288 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10289 {
10290 valid |= VALID_HEAD;
10291 *usedlen += 2;
10292 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010293 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010294 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 *fnamelen = (int)(tail - *fnamep);
10296#ifdef VMS
10297 if (*fnamelen > 0)
10298 *fnamelen += 1; /* the path separator is part of the path */
10299#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010300 if (*fnamelen == 0)
10301 {
10302 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10303 p = vim_strsave((char_u *)".");
10304 if (p == NULL)
10305 return -1;
10306 vim_free(*bufp);
10307 *bufp = *fnamep = tail = p;
10308 *fnamelen = 1;
10309 }
10310 else
10311 {
10312 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010313 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 }
10316
10317 /* ":8" - shortname */
10318 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10319 {
10320 *usedlen += 2;
10321#ifdef WIN3264
10322 has_shortname = 1;
10323#endif
10324 }
10325
10326#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010327 /*
10328 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 */
10330 if (has_shortname)
10331 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010332 /* Copy the string if it is shortened by :h and when it wasn't copied
10333 * yet, because we are going to change it in place. Avoids changing
10334 * the buffer name for "%:8". */
10335 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 {
10337 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010338 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 return -1;
10340 vim_free(*bufp);
10341 *bufp = *fnamep = p;
10342 }
10343
10344 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010345 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 if (!has_fullname && !vim_isAbsName(*fnamep))
10347 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010348 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 return -1;
10350 }
10351 else
10352 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010353 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354
Bram Moolenaardc935552011-08-17 15:23:23 +020010355 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010357 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 return -1;
10359
10360 if (l == 0)
10361 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010362 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010364 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 return -1;
10366 }
10367 *fnamelen = l;
10368 }
10369 }
10370#endif /* WIN3264 */
10371
10372 /* ":t" - tail, just the basename */
10373 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10374 {
10375 *usedlen += 2;
10376 *fnamelen -= (int)(tail - *fnamep);
10377 *fnamep = tail;
10378 }
10379
10380 /* ":e" - extension, can be repeated */
10381 /* ":r" - root, without extension, can be repeated */
10382 while (src[*usedlen] == ':'
10383 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10384 {
10385 /* find a '.' in the tail:
10386 * - for second :e: before the current fname
10387 * - otherwise: The last '.'
10388 */
10389 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10390 s = *fnamep - 2;
10391 else
10392 s = *fnamep + *fnamelen - 1;
10393 for ( ; s > tail; --s)
10394 if (s[0] == '.')
10395 break;
10396 if (src[*usedlen + 1] == 'e') /* :e */
10397 {
10398 if (s > tail)
10399 {
10400 *fnamelen += (int)(*fnamep - (s + 1));
10401 *fnamep = s + 1;
10402#ifdef VMS
10403 /* cut version from the extension */
10404 s = *fnamep + *fnamelen - 1;
10405 for ( ; s > *fnamep; --s)
10406 if (s[0] == ';')
10407 break;
10408 if (s > *fnamep)
10409 *fnamelen = s - *fnamep;
10410#endif
10411 }
10412 else if (*fnamep <= tail)
10413 *fnamelen = 0;
10414 }
10415 else /* :r */
10416 {
10417 if (s > tail) /* remove one extension */
10418 *fnamelen = (int)(s - *fnamep);
10419 }
10420 *usedlen += 2;
10421 }
10422
10423 /* ":s?pat?foo?" - substitute */
10424 /* ":gs?pat?foo?" - global substitute */
10425 if (src[*usedlen] == ':'
10426 && (src[*usedlen + 1] == 's'
10427 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10428 {
10429 char_u *str;
10430 char_u *pat;
10431 char_u *sub;
10432 int sep;
10433 char_u *flags;
10434 int didit = FALSE;
10435
10436 flags = (char_u *)"";
10437 s = src + *usedlen + 2;
10438 if (src[*usedlen + 1] == 'g')
10439 {
10440 flags = (char_u *)"g";
10441 ++s;
10442 }
10443
10444 sep = *s++;
10445 if (sep)
10446 {
10447 /* find end of pattern */
10448 p = vim_strchr(s, sep);
10449 if (p != NULL)
10450 {
10451 pat = vim_strnsave(s, (int)(p - s));
10452 if (pat != NULL)
10453 {
10454 s = p + 1;
10455 /* find end of substitution */
10456 p = vim_strchr(s, sep);
10457 if (p != NULL)
10458 {
10459 sub = vim_strnsave(s, (int)(p - s));
10460 str = vim_strnsave(*fnamep, *fnamelen);
10461 if (sub != NULL && str != NULL)
10462 {
10463 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010464 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (s != NULL)
10466 {
10467 *fnamep = s;
10468 *fnamelen = (int)STRLEN(s);
10469 vim_free(*bufp);
10470 *bufp = s;
10471 didit = TRUE;
10472 }
10473 }
10474 vim_free(sub);
10475 vim_free(str);
10476 }
10477 vim_free(pat);
10478 }
10479 }
10480 /* after using ":s", repeat all the modifiers */
10481 if (didit)
10482 goto repeat;
10483 }
10484 }
10485
Bram Moolenaar26df0922014-02-23 23:39:13 +010010486 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10487 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010488 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010489 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010490 if (c != NUL)
10491 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010492 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010493 if (c != NUL)
10494 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010495 if (p == NULL)
10496 return -1;
10497 vim_free(*bufp);
10498 *bufp = *fnamep = p;
10499 *fnamelen = (int)STRLEN(p);
10500 *usedlen += 2;
10501 }
10502
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 return valid;
10504}
10505
10506/*
10507 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010508 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 * "flags" can be "g" to do a global substitute.
10510 * Returns an allocated string, NULL for error.
10511 */
10512 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010513do_string_sub(
10514 char_u *str,
10515 char_u *pat,
10516 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010517 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010518 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520 int sublen;
10521 regmatch_T regmatch;
10522 int i;
10523 int do_all;
10524 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010525 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 garray_T ga;
10527 char_u *ret;
10528 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010529 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
10531 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10532 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010533 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534
10535 ga_init2(&ga, 1, 200);
10536
10537 do_all = (flags[0] == 'g');
10538
10539 regmatch.rm_ic = p_ic;
10540 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10541 if (regmatch.regprog != NULL)
10542 {
10543 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010544 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10546 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010547 /* Skip empty match except for first match. */
10548 if (regmatch.startp[0] == regmatch.endp[0])
10549 {
10550 if (zero_width == regmatch.startp[0])
10551 {
10552 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010553 i = MB_PTR2LEN(tail);
10554 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10555 (size_t)i);
10556 ga.ga_len += i;
10557 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010558 continue;
10559 }
10560 zero_width = regmatch.startp[0];
10561 }
10562
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 /*
10564 * Get some space for a temporary buffer to do the substitution
10565 * into. It will contain:
10566 * - The text up to where the match is.
10567 * - The substituted text.
10568 * - The text after the match.
10569 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010570 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010571 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10573 {
10574 ga_clear(&ga);
10575 break;
10576 }
10577
10578 /* copy the text up to where the match is */
10579 i = (int)(regmatch.startp[0] - tail);
10580 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10581 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010582 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 + ga.ga_len + i, TRUE, TRUE, FALSE);
10584 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010585 tail = regmatch.endp[0];
10586 if (*tail == NUL)
10587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 if (!do_all)
10589 break;
10590 }
10591
10592 if (ga.ga_data != NULL)
10593 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10594
Bram Moolenaar473de612013-06-08 18:19:48 +020010595 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 }
10597
10598 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10599 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010600 if (p_cpo == empty_option)
10601 p_cpo = save_cpo;
10602 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010603 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010604 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
10606 return ret;
10607}
10608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 static int
10610filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10611{
10612 typval_T rettv;
10613 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615
10616 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10617 argv[0] = vimvars[VV_KEY].vv_tv;
10618 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010619 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10620 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 if (map)
10622 {
10623 /* map(): replace the list item value */
10624 clear_tv(tv);
10625 rettv.v_lock = 0;
10626 *tv = rettv;
10627 }
10628 else
10629 {
10630 int error = FALSE;
10631
10632 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010633 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634 clear_tv(&rettv);
10635 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010636 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 if (error)
10638 goto theend;
10639 }
10640 retval = OK;
10641theend:
10642 clear_tv(&vimvars[VV_VAL].vv_tv);
10643 return retval;
10644}
10645
10646
10647/*
10648 * Implementation of map() and filter().
10649 */
10650 void
10651filter_map(typval_T *argvars, typval_T *rettv, int map)
10652{
10653 typval_T *expr;
10654 listitem_T *li, *nli;
10655 list_T *l = NULL;
10656 dictitem_T *di;
10657 hashtab_T *ht;
10658 hashitem_T *hi;
10659 dict_T *d = NULL;
10660 typval_T save_val;
10661 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010662 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 int rem;
10664 int todo;
10665 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10666 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10667 : N_("filter() argument"));
10668 int save_did_emsg;
10669 int idx = 0;
10670
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010671 if (argvars[0].v_type == VAR_BLOB)
10672 {
10673 if ((b = argvars[0].vval.v_blob) == NULL)
10674 return;
10675 }
10676 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 {
10678 if ((l = argvars[0].vval.v_list) == NULL
10679 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10680 return;
10681 }
10682 else if (argvars[0].v_type == VAR_DICT)
10683 {
10684 if ((d = argvars[0].vval.v_dict) == NULL
10685 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10686 return;
10687 }
10688 else
10689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010690 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 return;
10692 }
10693
10694 expr = &argvars[1];
10695 /* On type errors, the preceding call has already displayed an error
10696 * message. Avoid a misleading error message for an empty string that
10697 * was not passed as argument. */
10698 if (expr->v_type != VAR_UNKNOWN)
10699 {
10700 prepare_vimvar(VV_VAL, &save_val);
10701
10702 /* We reset "did_emsg" to be able to detect whether an error
10703 * occurred during evaluation of the expression. */
10704 save_did_emsg = did_emsg;
10705 did_emsg = FALSE;
10706
10707 prepare_vimvar(VV_KEY, &save_key);
10708 if (argvars[0].v_type == VAR_DICT)
10709 {
10710 vimvars[VV_KEY].vv_type = VAR_STRING;
10711
10712 ht = &d->dv_hashtab;
10713 hash_lock(ht);
10714 todo = (int)ht->ht_used;
10715 for (hi = ht->ht_array; todo > 0; ++hi)
10716 {
10717 if (!HASHITEM_EMPTY(hi))
10718 {
10719 int r;
10720
10721 --todo;
10722 di = HI2DI(hi);
10723 if (map &&
10724 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10725 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10726 break;
10727 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10728 r = filter_map_one(&di->di_tv, expr, map, &rem);
10729 clear_tv(&vimvars[VV_KEY].vv_tv);
10730 if (r == FAIL || did_emsg)
10731 break;
10732 if (!map && rem)
10733 {
10734 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10735 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10736 break;
10737 dictitem_remove(d, di);
10738 }
10739 }
10740 }
10741 hash_unlock(ht);
10742 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010743 else if (argvars[0].v_type == VAR_BLOB)
10744 {
10745 int i;
10746 typval_T tv;
10747
10748 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10749 for (i = 0; i < b->bv_ga.ga_len; i++)
10750 {
10751 tv.v_type = VAR_NUMBER;
10752 tv.vval.v_number = blob_get(b, i);
10753 vimvars[VV_KEY].vv_nr = idx;
10754 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10755 break;
10756 if (tv.v_type != VAR_NUMBER)
10757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010758 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010759 return;
10760 }
10761 tv.v_type = VAR_NUMBER;
10762 blob_set(b, i, tv.vval.v_number);
10763 if (!map && rem)
10764 {
10765 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10766
10767 mch_memmove(p + idx, p + i + 1,
10768 (size_t)b->bv_ga.ga_len - i - 1);
10769 --b->bv_ga.ga_len;
10770 --i;
10771 }
10772 }
10773 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 else
10775 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010776 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10778
10779 for (li = l->lv_first; li != NULL; li = nli)
10780 {
10781 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10782 break;
10783 nli = li->li_next;
10784 vimvars[VV_KEY].vv_nr = idx;
10785 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10786 || did_emsg)
10787 break;
10788 if (!map && rem)
10789 listitem_remove(l, li);
10790 ++idx;
10791 }
10792 }
10793
10794 restore_vimvar(VV_KEY, &save_key);
10795 restore_vimvar(VV_VAL, &save_val);
10796
10797 did_emsg |= save_did_emsg;
10798 }
10799
10800 copy_tv(&argvars[0], rettv);
10801}
10802
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */