blob: 46d24ef78814c8294bad1e880dbba266356ffef0 [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);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100248static void list_one_var(dictitem_T *v, char *prefix, int *first);
249static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100250static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253/* for VIM_VERSION_ defines */
254#include "version.h"
255
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100256
257#if defined(EBCDIC) || defined(PROTO)
258/*
259 * Compare struct fst by function name.
260 */
261 static int
262compare_func_name(const void *s1, const void *s2)
263{
264 struct fst *p1 = (struct fst *)s1;
265 struct fst *p2 = (struct fst *)s2;
266
267 return STRCMP(p1->f_name, p2->f_name);
268}
269
270/*
271 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100272 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100273 * On machines using EBCDIC we have to sort it.
274 */
275 static void
276sortFunctions(void)
277{
278 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
279
280 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
281}
282#endif
283
284
Bram Moolenaar33570922005-01-25 22:26:29 +0000285/*
286 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000287 */
288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100289eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000290{
Bram Moolenaar33570922005-01-25 22:26:29 +0000291 int i;
292 struct vimvar *p;
293
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200294 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
295 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200296 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000297 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300 for (i = 0; i < VV_LEN; ++i)
301 {
302 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200303 if (STRLEN(p->vv_name) > 16)
304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100305 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200306 getout(1);
307 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000308 STRCPY(p->vv_di.di_key, p->vv_name);
309 if (p->vv_flags & VV_RO)
310 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
311 else if (p->vv_flags & VV_RO_SBX)
312 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
313 else
314 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000315
316 /* add to v: scope dict, unless the value is not always available */
317 if (p->vv_type != VAR_UNKNOWN)
318 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000319 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000320 /* add to compat scope dict */
321 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000322 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100323 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
324
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000325 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100326 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100327 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100328 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100329 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100330
331 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
332 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
333 set_vim_var_nr(VV_NONE, VVAL_NONE);
334 set_vim_var_nr(VV_NULL, VVAL_NULL);
335
Bram Moolenaarf562e722016-07-19 17:25:25 +0200336 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
337 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
338 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
339 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
340 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
341 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
342 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
343 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
344 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
345 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100346 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200347
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200348 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200349
350#ifdef EBCDIC
351 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100352 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200353 */
354 sortFunctions();
355#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000356}
357
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000358#if defined(EXITFREE) || defined(PROTO)
359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100360eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000361{
362 int i;
363 struct vimvar *p;
364
365 for (i = 0; i < VV_LEN; ++i)
366 {
367 p = &vimvars[i];
368 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100369 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000370 else if (p->vv_di.di_tv.v_type == VAR_LIST)
371 {
372 list_unref(p->vv_list);
373 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000374 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000375 }
376 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000377 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000378 hash_clear(&compat_hashtab);
379
Bram Moolenaard9fba312005-06-26 22:34:35 +0000380 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100381# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200382 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100383# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000384
385 /* global variables */
386 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000387
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000388 /* autoloaded script names */
389 ga_clear_strings(&ga_loaded);
390
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 /* Script-local variables. First clear all the variables and in a second
392 * loop free the scriptvar_T, because a variable in one script might hold
393 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200395 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200396 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200398 ga_clear(&ga_scripts);
399
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000400 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200401 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000402
403 /* functions */
404 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000405}
406#endif
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 * Set an internal variable to a string value. Creates the variable if it does
411 * not already exist.
412 */
413 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100414set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000416 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000417 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419 val = vim_strsave(value);
420 if (val != NULL)
421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000422 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000423 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000425 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000426 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 }
428 }
429}
430
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000431static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200432#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000433static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000434static char_u *redir_endp = NULL;
435static char_u *redir_varname = NULL;
436
437/*
438 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100439 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000440 * Returns OK if successfully completed the setup. FAIL otherwise.
441 */
442 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100443var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000444{
445 int save_emsg;
446 int err;
447 typval_T tv;
448
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000449 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000450 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100452 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000453 return FAIL;
454 }
455
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000456 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000457 redir_varname = vim_strsave(name);
458 if (redir_varname == NULL)
459 return FAIL;
460
461 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
462 if (redir_lval == NULL)
463 {
464 var_redir_stop();
465 return FAIL;
466 }
467
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000468 /* The output is stored in growarray "redir_ga" until redirection ends. */
469 ga_init2(&redir_ga, (int)sizeof(char), 500);
470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100472 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000473 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
475 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200476 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477 if (redir_endp != NULL && *redir_endp != NUL)
478 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100479 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000480 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100481 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000482 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000483 var_redir_stop();
484 return FAIL;
485 }
486
487 /* check if we can write to the variable: set it to or append an empty
488 * string */
489 save_emsg = did_emsg;
490 did_emsg = FALSE;
491 tv.v_type = VAR_STRING;
492 tv.vval.v_string = (char_u *)"";
493 if (append)
494 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
495 else
496 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200497 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000499 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500 if (err)
501 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000502 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000503 var_redir_stop();
504 return FAIL;
505 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000506
507 return OK;
508}
509
510/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000511 * Append "value[value_len]" to the variable set by var_redir_start().
512 * The actual appending is postponed until redirection ends, because the value
513 * appended may in fact be the string we write to, changing it may cause freed
514 * memory to be used:
515 * :redir => foo
516 * :let foo
517 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 */
519 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100520var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000522 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000523
524 if (redir_lval == NULL)
525 return;
526
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000527 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000529 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000532 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000533 {
534 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000535 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000536 }
537 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539}
540
541/*
542 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000543 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544 */
545 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100546var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000547{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000548 typval_T tv;
549
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200550 if (EVALCMD_BUSY)
551 {
552 redir_lval = NULL;
553 return;
554 }
555
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000556 if (redir_lval != NULL)
557 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000558 /* If there was no error: assign the text to the variable. */
559 if (redir_endp != NULL)
560 {
561 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
562 tv.v_type = VAR_STRING;
563 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200564 /* Call get_lval() again, if it's inside a Dict or List it may
565 * have changed. */
566 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100567 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200568 if (redir_endp != NULL && redir_lval->ll_name != NULL)
569 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
570 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000571 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000572
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000573 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100574 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000575
Bram Moolenaard23a8232018-02-10 18:45:26 +0100576 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000577 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100578 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 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}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 int err = FALSE;
611
612 set_vim_var_string(VV_FNAME_IN, fname, -1);
613 set_vim_var_string(VV_CMDARG, args, -1);
614 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
615 err = TRUE;
616 set_vim_var_string(VV_FNAME_IN, NULL, -1);
617 set_vim_var_string(VV_CMDARG, NULL, -1);
618
619 if (err)
620 {
621 mch_remove(fname);
622 return FAIL;
623 }
624 return OK;
625}
626# endif
627
628# if defined(FEAT_DIFF) || defined(PROTO)
629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630eval_diff(
631 char_u *origfile,
632 char_u *newfile,
633 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int err = FALSE;
636
637 set_vim_var_string(VV_FNAME_IN, origfile, -1);
638 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
639 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
640 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
641 set_vim_var_string(VV_FNAME_IN, NULL, -1);
642 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
643 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
644}
645
646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100647eval_patch(
648 char_u *origfile,
649 char_u *difffile,
650 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err;
653
654 set_vim_var_string(VV_FNAME_IN, origfile, -1);
655 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
656 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
657 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
660 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
661}
662# endif
663
664/*
665 * Top level evaluation function, returning a boolean.
666 * Sets "error" to TRUE if there was an error.
667 * Return TRUE or FALSE.
668 */
669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100670eval_to_bool(
671 char_u *arg,
672 int *error,
673 char_u **nextcmd,
674 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
Bram Moolenaar33570922005-01-25 22:26:29 +0000676 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 if (skip)
680 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000681 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 else
684 {
685 *error = FALSE;
686 if (!skip)
687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100688 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691 }
692 if (skip)
693 --emsg_skip;
694
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200695 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100698/*
699 * Call eval1() and give an error message if not done at a lower level.
700 */
701 static int
702eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
703{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100704 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100705 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)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100718 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100719 }
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,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001450 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001451 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
Bram Moolenaar32526b32019-01-19 17:43:09 +01001468 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1469 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001470 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 Moolenaar32526b32019-01-19 17:43:09 +01001486 list_hashtable_vars(&globvarht, "", 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 Moolenaar32526b32019-01-19 17:43:09 +01001495 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001496}
1497
1498/*
1499 * List window variables.
1500 */
1501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001503{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001504 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001505}
1506
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001507/*
1508 * List tab page variables.
1509 */
1510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001511list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001512{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001513 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001514}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001515
Bram Moolenaara7043832005-01-21 11:56:39 +00001516/*
1517 * List Vim variables.
1518 */
1519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001522 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523}
1524
1525/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001526 * List script-local variables, if there is a script.
1527 */
1528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001530{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001531 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1532 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001533 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001534}
1535
1536/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 * List variables in "arg".
1538 */
1539 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001540list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001541{
1542 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001545 char_u *name_start;
1546 char_u *arg_subsc;
1547 char_u *tofree;
1548 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549
1550 while (!ends_excmd(*arg) && !got_int)
1551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001554 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001555 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001556 {
1557 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001558 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 break;
1560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001561 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001563 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001564 /* get_name_len() takes care of expanding curly braces */
1565 name_start = name = arg;
1566 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1567 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001568 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001569 /* This is mainly to keep test 49 working: when expanding
1570 * curly braces fails overrule the exception error message. */
1571 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001573 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001574 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 break;
1576 }
1577 error = TRUE;
1578 }
1579 else
1580 {
1581 if (tofree != NULL)
1582 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001583 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 else
1586 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001587 /* handle d.key, l[idx], f(expr) */
1588 arg_subsc = arg;
1589 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001590 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001591 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001593 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001596 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001597 case 'g': list_glob_vars(first); break;
1598 case 'b': list_buf_vars(first); break;
1599 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001600 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001601 case 'v': list_vim_vars(first); break;
1602 case 's': list_script_vars(first); break;
1603 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001604 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001605 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001606 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001607 }
1608 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001609 {
1610 char_u numbuf[NUMBUFLEN];
1611 char_u *tf;
1612 int c;
1613 char_u *s;
1614
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001615 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001616 c = *arg;
1617 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001618 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001619 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001620 tv.v_type,
1621 s == NULL ? (char_u *)"" : s,
1622 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001623 *arg = c;
1624 vim_free(tf);
1625 }
1626 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 }
1629 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001630
1631 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001633
1634 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 }
1636
1637 return arg;
1638}
1639
1640/*
1641 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1642 * Returns a pointer to the char just after the var name.
1643 * Returns NULL if there is an error.
1644 */
1645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646ex_let_one(
1647 char_u *arg, /* points to variable name */
1648 typval_T *tv, /* value to assign to variable */
1649 int copy, /* copy value from "tv" */
1650 char_u *endchars, /* valid chars after variable name or NULL */
1651 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652{
1653 int c1;
1654 char_u *name;
1655 char_u *p;
1656 char_u *arg_end = NULL;
1657 int len;
1658 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660
1661 /*
1662 * ":let $VAR = expr": Set environment variable.
1663 */
1664 if (*arg == '$')
1665 {
1666 /* Find the end of the name. */
1667 ++arg;
1668 name = arg;
1669 len = get_env_len(&arg);
1670 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001671 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 else
1673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001675 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001678 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001679 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 {
1681 c1 = name[len];
1682 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001683 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001684 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 {
1686 int mustfree = FALSE;
1687 char_u *s = vim_getenv(name, &mustfree);
1688
1689 if (s != NULL)
1690 {
1691 p = tofree = concat_str(s, p);
1692 if (mustfree)
1693 vim_free(s);
1694 }
1695 }
1696 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001697 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001699 if (STRICMP(name, "HOME") == 0)
1700 init_homedir();
1701 else if (didset_vim && STRICMP(name, "VIM") == 0)
1702 didset_vim = FALSE;
1703 else if (didset_vimruntime
1704 && STRICMP(name, "VIMRUNTIME") == 0)
1705 didset_vimruntime = FALSE;
1706 arg_end = arg;
1707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001709 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001710 }
1711 }
1712 }
1713
1714 /*
1715 * ":let &option = expr": Set option value.
1716 * ":let &l:option = expr": Set local option value.
1717 * ":let &g:option = expr": Set global option value.
1718 */
1719 else if (*arg == '&')
1720 {
1721 /* Find the end of the name. */
1722 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 if (p == NULL || (endchars != NULL
1724 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001725 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726 else
1727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 long n;
1729 int opt_type;
1730 long numval;
1731 char_u *stringval = NULL;
1732 char_u *s;
1733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 c1 = *p;
1735 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001737 n = (long)tv_get_number(tv);
1738 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001739 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 {
1741 opt_type = get_option_value(arg, &numval,
1742 &stringval, opt_flags);
1743 if ((opt_type == 1 && *op == '.')
1744 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001746 semsg(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001747 s = NULL; /* don't set the value */
1748 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 else
1750 {
1751 if (opt_type == 1) /* number */
1752 {
1753 if (*op == '+')
1754 n = numval + n;
1755 else
1756 n = numval - n;
1757 }
1758 else if (opt_type == 0 && stringval != NULL) /* string */
1759 {
1760 s = concat_str(stringval, s);
1761 vim_free(stringval);
1762 stringval = s;
1763 }
1764 }
1765 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001766 if (s != NULL)
1767 {
1768 set_option_value(arg, n, s, opt_flags);
1769 arg_end = p;
1770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 }
1774 }
1775
1776 /*
1777 * ":let @r = expr": Set register contents.
1778 */
1779 else if (*arg == '@')
1780 {
1781 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001783 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001784 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001786 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 else
1788 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001789 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001790 char_u *s;
1791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001792 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001793 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001795 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001796 if (s != NULL)
1797 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001798 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 vim_free(s);
1800 }
1801 }
1802 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001805 arg_end = arg + 1;
1806 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001807 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 }
1809 }
1810
1811 /*
1812 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001813 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001815 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001819 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001820 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001822 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001823 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 else
1825 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 arg_end = p;
1828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001830 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 }
1832
1833 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001834 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835
1836 return arg_end;
1837}
1838
1839/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001840 * Get an lval: variable, Dict item or List item that can be assigned a value
1841 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1842 * "name.key", "name.key[expr]" etc.
1843 * Indexing only works if "name" is an existing List or Dictionary.
1844 * "name" points to the start of the name.
1845 * If "rettv" is not NULL it points to the value to be assigned.
1846 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1847 * wrong; must end in space or cmd separator.
1848 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001849 * flags:
1850 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001851 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001852 * GLV_NO_AUTOLOAD: do not use script autoloading
1853 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001855 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001858 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001859get_lval(
1860 char_u *name,
1861 typval_T *rettv,
1862 lval_T *lp,
1863 int unlet,
1864 int skip,
1865 int flags, /* GLV_ values */
1866 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001869 char_u *expr_start, *expr_end;
1870 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 dictitem_T *v;
1872 typval_T var1;
1873 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001875 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001876 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001877 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001879 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001883
1884 if (skip)
1885 {
1886 /* When skipping just find the end of the name. */
1887 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001888 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001889 }
1890
1891 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001892 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001893 if (expr_start != NULL)
1894 {
1895 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001896 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 && *p != '[' && *p != '.')
1898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 return NULL;
1901 }
1902
1903 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1904 if (lp->ll_exp_name == NULL)
1905 {
1906 /* Report an invalid expression in braces, unless the
1907 * expression evaluation has been cancelled due to an
1908 * aborting error, an interrupt, or an exception. */
1909 if (!aborting() && !quiet)
1910 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001911 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001912 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 return NULL;
1914 }
1915 }
1916 lp->ll_name = lp->ll_exp_name;
1917 }
1918 else
1919 lp->ll_name = name;
1920
1921 /* Without [idx] or .key we are done. */
1922 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1923 return p;
1924
1925 cc = *p;
1926 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001927 /* Only pass &ht when we would write to the variable, it prevents autoload
1928 * as well. */
1929 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1930 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001931 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001932 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 if (v == NULL)
1935 return NULL;
1936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 /*
1938 * Loop until no more [idx] or .key is following.
1939 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001941 var1.v_type = VAR_UNKNOWN;
1942 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001943 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1946 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001947 && lp->ll_tv->vval.v_dict != NULL)
1948 && !(lp->ll_tv->v_type == VAR_BLOB
1949 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001952 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001961
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 len = -1;
1963 if (*p == '.')
1964 {
1965 key = p + 1;
1966 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1967 ;
1968 if (len == 0)
1969 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001971 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001973 }
1974 p = key + len;
1975 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001976 else
1977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001979 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 if (*p == ':')
1981 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001982 else
1983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 empty1 = FALSE;
1985 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001987 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001988 {
1989 /* not a number or string */
1990 clear_tv(&var1);
1991 return NULL;
1992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001993 }
1994
1995 /* Optionally get the second index [ :expr]. */
1996 if (*p == ':')
1997 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002001 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002002 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002004 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002005 if (rettv != NULL
2006 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002007 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002008 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002009 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002013 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 }
2016 p = skipwhite(p + 1);
2017 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 else
2020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2023 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002024 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002027 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002028 {
2029 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002030 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002031 clear_tv(&var2);
2032 return NULL;
2033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002036 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002037 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002039
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002043 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002044 clear_tv(&var1);
2045 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002047 }
2048
2049 /* Skip to past ']'. */
2050 ++p;
2051 }
2052
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 {
2055 if (len == -1)
2056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002058 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002059 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002063 }
2064 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 lp->ll_list = NULL;
2066 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002067 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002068
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002069 /* When assigning to a scope dictionary check that a function and
2070 * variable name is valid (only variable name unless it is l: or
2071 * g: dictionary). Disallow overwriting a builtin function. */
2072 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002073 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002074 int prevval;
2075 int wrong;
2076
2077 if (len != -1)
2078 {
2079 prevval = key[len];
2080 key[len] = NUL;
2081 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002082 else
2083 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002084 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2085 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002086 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002087 || !valid_varname(key);
2088 if (len != -1)
2089 key[len] = prevval;
2090 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002091 return NULL;
2092 }
2093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002096 // Can't add "v:" or "a:" variable.
2097 if (lp->ll_dict == &vimvardict
2098 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 semsg(_(e_illvar), name);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002101 return NULL;
2102 }
2103
Bram Moolenaar31b81602019-02-10 22:14:27 +01002104 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002109 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002111 }
2112 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002116 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002118 p = NULL;
2119 break;
2120 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002121 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002122 else if ((flags & GLV_READ_ONLY) == 0
2123 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002124 {
2125 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002126 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002127 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002128
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002129 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002132 else if (lp->ll_tv->v_type == VAR_BLOB)
2133 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002134 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2135
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002136 /*
2137 * Get the number and item for the only or first index of the List.
2138 */
2139 if (empty1)
2140 lp->ll_n1 = 0;
2141 else
2142 // is number or string
2143 lp->ll_n1 = (long)tv_get_number(&var1);
2144 clear_tv(&var1);
2145
2146 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002147 || lp->ll_n1 > bloblen
2148 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002149 {
2150 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002151 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002152 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002153 return NULL;
2154 }
2155 if (lp->ll_range && !lp->ll_empty2)
2156 {
2157 lp->ll_n2 = (long)tv_get_number(&var2);
2158 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002159 if (lp->ll_n2 < 0
2160 || lp->ll_n2 >= bloblen
2161 || lp->ll_n2 < lp->ll_n1)
2162 {
2163 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002165 return NULL;
2166 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002167 }
2168 lp->ll_blob = lp->ll_tv->vval.v_blob;
2169 lp->ll_tv = NULL;
2170 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 else
2172 {
2173 /*
2174 * Get the number and item for the only or first index of the List.
2175 */
2176 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002179 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002180 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002181 clear_tv(&var1);
2182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 lp->ll_list = lp->ll_tv->vval.v_list;
2185 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2186 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002188 if (lp->ll_n1 < 0)
2189 {
2190 lp->ll_n1 = 0;
2191 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2192 }
2193 }
2194 if (lp->ll_li == NULL)
2195 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002196 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002197 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002198 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 }
2201
2202 /*
2203 * May need to find the item or absolute index for the second
2204 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 * When no index given: "lp->ll_empty2" is TRUE.
2206 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002209 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002210 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002211 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002217 {
2218 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002219 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002221 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 }
2224
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2226 if (lp->ll_n1 < 0)
2227 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2228 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002229 {
2230 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002231 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002233 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002234 }
2235
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
2239
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002240 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 return p;
2242}
2243
2244/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249{
2250 vim_free(lp->ll_exp_name);
2251 vim_free(lp->ll_newkey);
2252}
2253
2254/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002255 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 */
2259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260set_var_lval(
2261 lval_T *lp,
2262 char_u *endp,
2263 typval_T *rettv,
2264 int copy,
2265 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266{
2267 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002268 listitem_T *ri;
2269 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270
2271 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002273 cc = *endp;
2274 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002275 if (lp->ll_blob != NULL)
2276 {
2277 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002278
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002279 if (op != NULL && *op != '=')
2280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 return;
2283 }
2284
2285 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2286 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002287 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002288
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002289 if (lp->ll_empty2)
2290 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2291
2292 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002294 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002295 return;
2296 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002297 if (lp->ll_empty2)
2298 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002299
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 ir = 0;
2301 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2302 blob_set(lp->ll_blob, il,
2303 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002304 }
2305 else
2306 {
2307 val = (int)tv_get_number_chk(rettv, &error);
2308 if (!error)
2309 {
2310 garray_T *gap = &lp->ll_blob->bv_ga;
2311
2312 // Allow for appending a byte. Setting a byte beyond
2313 // the end is an error otherwise.
2314 if (lp->ll_n1 < gap->ga_len
2315 || (lp->ll_n1 == gap->ga_len
2316 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2317 {
2318 blob_set(lp->ll_blob, lp->ll_n1, val);
2319 if (lp->ll_n1 == gap->ga_len)
2320 ++gap->ga_len;
2321 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002322 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002323 }
2324 }
2325 }
2326 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002328 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329
Bram Moolenaar79518e22017-02-17 16:31:35 +01002330 /* handle +=, -= and .= */
2331 di = NULL;
2332 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2333 &tv, &di, TRUE, FALSE) == OK)
2334 {
2335 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002336 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2337 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002338 && tv_op(&tv, rettv, op) == OK)
2339 set_var(lp->ll_name, &tv, FALSE);
2340 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002343 else
2344 set_var(lp->ll_name, rettv, copy);
2345 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002347 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002348 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002349 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002350 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 else if (lp->ll_range)
2352 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002353 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002354 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002355
2356 /*
2357 * Check whether any of the list items is locked
2358 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002359 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002360 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002361 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002362 return;
2363 ri = ri->li_next;
2364 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2365 break;
2366 ll_li = ll_li->li_next;
2367 ++ll_n1;
2368 }
2369
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 /*
2371 * Assign the List values to the list items.
2372 */
2373 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 if (op != NULL && *op != '=')
2376 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2377 else
2378 {
2379 clear_tv(&lp->ll_li->li_tv);
2380 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2381 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 ri = ri->li_next;
2383 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2384 break;
2385 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002388 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002389 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 ri = NULL;
2391 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002392 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002393 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 lp->ll_li = lp->ll_li->li_next;
2395 ++lp->ll_n1;
2396 }
2397 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002398 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 else if (lp->ll_empty2
2400 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002402 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 }
2404 else
2405 {
2406 /*
2407 * Assign to a List or Dictionary item.
2408 */
2409 if (lp->ll_newkey != NULL)
2410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 if (op != NULL && *op != '=')
2412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002413 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 return;
2415 }
2416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002418 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 if (di == NULL)
2420 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002421 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2422 {
2423 vim_free(di);
2424 return;
2425 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002427 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 else if (op != NULL && *op != '=')
2429 {
2430 tv_op(lp->ll_tv, rettv, op);
2431 return;
2432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 /*
2437 * Assign the value to the variable or list item.
2438 */
2439 if (copy)
2440 copy_tv(rettv, lp->ll_tv);
2441 else
2442 {
2443 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002444 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 }
2447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448}
2449
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2452 * Returns OK or FAIL.
2453 */
2454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002455tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002457 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 char_u numbuf[NUMBUFLEN];
2459 char_u *s;
2460
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002461 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2462 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2463 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 {
2465 switch (tv1->v_type)
2466 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002467 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 case VAR_DICT:
2469 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002470 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002471 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002472 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002473 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 break;
2475
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002476 case VAR_BLOB:
2477 if (*op != '+' || tv2->v_type != VAR_BLOB)
2478 break;
2479 // BLOB += BLOB
2480 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2481 {
2482 blob_T *b1 = tv1->vval.v_blob;
2483 blob_T *b2 = tv2->vval.v_blob;
2484 int i, len = blob_len(b2);
2485 for (i = 0; i < len; i++)
2486 ga_append(&b1->bv_ga, blob_get(b2, i));
2487 }
2488 return OK;
2489
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 case VAR_LIST:
2491 if (*op != '+' || tv2->v_type != VAR_LIST)
2492 break;
2493 /* List += List */
2494 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2495 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2496 return OK;
2497
2498 case VAR_NUMBER:
2499 case VAR_STRING:
2500 if (tv2->v_type == VAR_LIST)
2501 break;
2502 if (*op == '+' || *op == '-')
2503 {
2504 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002505 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002506#ifdef FEAT_FLOAT
2507 if (tv2->v_type == VAR_FLOAT)
2508 {
2509 float_T f = n;
2510
2511 if (*op == '+')
2512 f += tv2->vval.v_float;
2513 else
2514 f -= tv2->vval.v_float;
2515 clear_tv(tv1);
2516 tv1->v_type = VAR_FLOAT;
2517 tv1->vval.v_float = f;
2518 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002520#endif
2521 {
2522 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002523 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002525 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002526 clear_tv(tv1);
2527 tv1->v_type = VAR_NUMBER;
2528 tv1->vval.v_number = n;
2529 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 }
2531 else
2532 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002533 if (tv2->v_type == VAR_FLOAT)
2534 break;
2535
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002537 s = tv_get_string(tv1);
2538 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 clear_tv(tv1);
2540 tv1->v_type = VAR_STRING;
2541 tv1->vval.v_string = s;
2542 }
2543 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002544
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002545 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002546#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002547 {
2548 float_T f;
2549
2550 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2551 && tv2->v_type != VAR_NUMBER
2552 && tv2->v_type != VAR_STRING))
2553 break;
2554 if (tv2->v_type == VAR_FLOAT)
2555 f = tv2->vval.v_float;
2556 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002557 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002558 if (*op == '+')
2559 tv1->vval.v_float += f;
2560 else
2561 tv1->vval.v_float -= f;
2562 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002563#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002564 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 }
2566 }
2567
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002568 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 return FAIL;
2570}
2571
2572/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573 * Evaluate the expression used in a ":for var in expr" command.
2574 * "arg" points to "var".
2575 * Set "*errp" to TRUE for an error, FALSE otherwise;
2576 * Return a pointer that holds the info. Null when there is an error.
2577 */
2578 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002579eval_for_line(
2580 char_u *arg,
2581 int *errp,
2582 char_u **nextcmdp,
2583 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002584{
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002586 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 typval_T tv;
2588 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002589
2590 *errp = TRUE; /* default: there is an error */
2591
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002593 if (fi == NULL)
2594 return NULL;
2595
2596 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2597 if (expr == NULL)
2598 return fi;
2599
2600 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002601 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002603 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002604 return fi;
2605 }
2606
2607 if (skip)
2608 ++emsg_skip;
2609 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2610 {
2611 *errp = FALSE;
2612 if (!skip)
2613 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002614 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002615 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002616 l = tv.vval.v_list;
2617 if (l == NULL)
2618 {
2619 // a null list is like an empty list: do nothing
2620 clear_tv(&tv);
2621 }
2622 else
2623 {
2624 // No need to increment the refcount, it's already set for
2625 // the list being used in "tv".
2626 fi->fi_list = l;
2627 list_add_watch(l, &fi->fi_lw);
2628 fi->fi_lw.lw_item = l->lv_first;
2629 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002630 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002631 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002632 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002633 fi->fi_bi = 0;
2634 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002635 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002636 typval_T btv;
2637
2638 // Make a copy, so that the iteration still works when the
2639 // blob is changed.
2640 blob_copy(&tv, &btv);
2641 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002642 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002643 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002644 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645 else
2646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002647 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002648 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649 }
2650 }
2651 }
2652 if (skip)
2653 --emsg_skip;
2654
2655 return fi;
2656}
2657
2658/*
2659 * Use the first item in a ":for" list. Advance to the next.
2660 * Assign the values to the variable (list). "arg" points to the first one.
2661 * Return TRUE when a valid item was found, FALSE when at end of list or
2662 * something wrong.
2663 */
2664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002665next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002666{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002667 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002668 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002669 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002670
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002671 if (fi->fi_blob != NULL)
2672 {
2673 typval_T tv;
2674
2675 if (fi->fi_bi >= blob_len(fi->fi_blob))
2676 return FALSE;
2677 tv.v_type = VAR_NUMBER;
2678 tv.v_lock = VAR_FIXED;
2679 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2680 ++fi->fi_bi;
2681 return ex_let_vars(arg, &tv, TRUE,
2682 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2683 }
2684
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002685 item = fi->fi_lw.lw_item;
2686 if (item == NULL)
2687 result = FALSE;
2688 else
2689 {
2690 fi->fi_lw.lw_item = item->li_next;
2691 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2692 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2693 }
2694 return result;
2695}
2696
2697/*
2698 * Free the structure used to store info used by ":for".
2699 */
2700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002701free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002702{
Bram Moolenaar33570922005-01-25 22:26:29 +00002703 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002705 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002706 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002708 list_unref(fi->fi_list);
2709 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002710 if (fi != NULL && fi->fi_blob != NULL)
2711 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002712 vim_free(fi);
2713}
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2716
2717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002718set_context_for_expression(
2719 expand_T *xp,
2720 char_u *arg,
2721 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 int got_eq = FALSE;
2724 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002727 if (cmdidx == CMD_let)
2728 {
2729 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002730 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731 {
2732 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002733 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 {
2735 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002736 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002737 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738 break;
2739 }
2740 return;
2741 }
2742 }
2743 else
2744 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2745 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 while ((xp->xp_pattern = vim_strpbrk(arg,
2747 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2748 {
2749 c = *xp->xp_pattern;
2750 if (c == '&')
2751 {
2752 c = xp->xp_pattern[1];
2753 if (c == '&')
2754 {
2755 ++xp->xp_pattern;
2756 xp->xp_context = cmdidx != CMD_let || got_eq
2757 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2758 }
2759 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002762 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2763 xp->xp_pattern += 2;
2764
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 else if (c == '$')
2768 {
2769 /* environment variable */
2770 xp->xp_context = EXPAND_ENV_VARS;
2771 }
2772 else if (c == '=')
2773 {
2774 got_eq = TRUE;
2775 xp->xp_context = EXPAND_EXPRESSION;
2776 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002777 else if (c == '#'
2778 && xp->xp_context == EXPAND_EXPRESSION)
2779 {
2780 /* Autoload function/variable contains '#'. */
2781 break;
2782 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002783 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 && xp->xp_context == EXPAND_FUNCTIONS
2785 && vim_strchr(xp->xp_pattern, '(') == NULL)
2786 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002787 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 break;
2789 }
2790 else if (cmdidx != CMD_let || got_eq)
2791 {
2792 if (c == '"') /* string */
2793 {
2794 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2795 if (c == '\\' && xp->xp_pattern[1] != NUL)
2796 ++xp->xp_pattern;
2797 xp->xp_context = EXPAND_NOTHING;
2798 }
2799 else if (c == '\'') /* literal string */
2800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2803 /* skip */ ;
2804 xp->xp_context = EXPAND_NOTHING;
2805 }
2806 else if (c == '|')
2807 {
2808 if (xp->xp_pattern[1] == '|')
2809 {
2810 ++xp->xp_pattern;
2811 xp->xp_context = EXPAND_EXPRESSION;
2812 }
2813 else
2814 xp->xp_context = EXPAND_COMMANDS;
2815 }
2816 else
2817 xp->xp_context = EXPAND_EXPRESSION;
2818 }
2819 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 /* Doesn't look like something valid, expand as an expression
2821 * anyway. */
2822 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 arg = xp->xp_pattern;
2824 if (*arg != NUL)
2825 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2826 /* skip */ ;
2827 }
2828 xp->xp_pattern = arg;
2829}
2830
2831#endif /* FEAT_CMDL_COMPL */
2832
2833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 * ":unlet[!] var1 ... " command.
2835 */
2836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002837ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002839 ex_unletlock(eap, eap->arg, 0);
2840}
2841
2842/*
2843 * ":lockvar" and ":unlockvar" commands
2844 */
2845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002846ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849 int deep = 2;
2850
2851 if (eap->forceit)
2852 deep = -1;
2853 else if (vim_isdigit(*arg))
2854 {
2855 deep = getdigits(&arg);
2856 arg = skipwhite(arg);
2857 }
2858
2859 ex_unletlock(eap, arg, deep);
2860}
2861
2862/*
2863 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2864 */
2865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002866ex_unletlock(
2867 exarg_T *eap,
2868 char_u *argstart,
2869 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002870{
2871 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 do
2877 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002878 if (*arg == '$')
2879 {
2880 char_u *name = ++arg;
2881
2882 if (get_env_len(&arg) == 0)
2883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002884 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002885 return;
2886 }
2887 vim_unsetenv(name);
2888 arg = skipwhite(arg);
2889 continue;
2890 }
2891
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002893 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002894 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 if (lv.ll_name == NULL)
2896 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002897 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 if (name_end != NULL)
2901 {
2902 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002903 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 }
2905 if (!(eap->skip || error))
2906 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 break;
2908 }
2909
2910 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 {
2912 if (eap->cmdidx == CMD_unlet)
2913 {
2914 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2915 error = TRUE;
2916 }
2917 else
2918 {
2919 if (do_lock_var(&lv, name_end, deep,
2920 eap->cmdidx == CMD_lockvar) == FAIL)
2921 error = TRUE;
2922 }
2923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 if (!eap->skip)
2926 clear_lval(&lv);
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 arg = skipwhite(name_end);
2929 } while (!ends_excmd(*arg));
2930
2931 eap->nextcmd = check_nextcmd(arg);
2932}
2933
Bram Moolenaar8c711452005-01-14 21:53:12 +00002934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935do_unlet_var(
2936 lval_T *lp,
2937 char_u *name_end,
2938 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002939{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 int ret = OK;
2941 int cc;
2942
2943 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 cc = *name_end;
2946 *name_end = NUL;
2947
2948 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002949 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002952 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002953 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002954 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002955 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002956 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002957 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 else if (lp->ll_range)
2959 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002961 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002962 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002963
2964 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2965 {
2966 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002967 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 return FAIL;
2969 ll_li = li;
2970 ++ll_n1;
2971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972
2973 /* Delete a range of List items. */
2974 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2975 {
2976 li = lp->ll_li->li_next;
2977 listitem_remove(lp->ll_list, lp->ll_li);
2978 lp->ll_li = li;
2979 ++lp->ll_n1;
2980 }
2981 }
2982 else
2983 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* unlet a List item. */
2986 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 }
2991
2992 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993}
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
2996 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002997 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 */
2999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 hashtab_T *ht;
3003 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003004 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003005 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003006 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 ht = find_var_ht(name, &varname);
3009 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003012 if (d == NULL)
3013 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 if (ht == &globvarht)
3015 d = &globvardict;
3016 else if (ht == &compat_hashtab)
3017 d = &vimvardict;
3018 else
3019 {
3020 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3021 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3022 }
3023 if (d == NULL)
3024 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003025 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 return FAIL;
3027 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003029 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003030 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003031 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003032 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003033 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003034 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003035 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003036 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003037 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003038 return FAIL;
3039
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003040 delete_var(ht, hi);
3041 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003044 if (forceit)
3045 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003046 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 return FAIL;
3048}
3049
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003050/*
3051 * Lock or unlock variable indicated by "lp".
3052 * "deep" is the levels to go (-1 for unlimited);
3053 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3054 */
3055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003056do_lock_var(
3057 lval_T *lp,
3058 char_u *name_end,
3059 int deep,
3060 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003061{
3062 int ret = OK;
3063 int cc;
3064 dictitem_T *di;
3065
3066 if (deep == 0) /* nothing to do */
3067 return OK;
3068
3069 if (lp->ll_tv == NULL)
3070 {
3071 cc = *name_end;
3072 *name_end = NUL;
3073
3074 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003075 di = find_var(lp->ll_name, NULL, TRUE);
3076 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003077 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003078 else if ((di->di_flags & DI_FLAGS_FIX)
3079 && di->di_tv.v_type != VAR_DICT
3080 && di->di_tv.v_type != VAR_LIST)
3081 /* For historic reasons this error is not given for a list or dict.
3082 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003084 else
3085 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003086 if (lock)
3087 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003088 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003089 di->di_flags &= ~DI_FLAGS_LOCK;
3090 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003091 }
3092 *name_end = cc;
3093 }
3094 else if (lp->ll_range)
3095 {
3096 listitem_T *li = lp->ll_li;
3097
3098 /* (un)lock a range of List items. */
3099 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3100 {
3101 item_lock(&li->li_tv, deep, lock);
3102 li = li->li_next;
3103 ++lp->ll_n1;
3104 }
3105 }
3106 else if (lp->ll_list != NULL)
3107 /* (un)lock a List item. */
3108 item_lock(&lp->ll_li->li_tv, deep, lock);
3109 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003110 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003111 item_lock(&lp->ll_di->di_tv, deep, lock);
3112
3113 return ret;
3114}
3115
3116/*
3117 * Lock or unlock an item. "deep" is nr of levels to go.
3118 */
3119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003121{
3122 static int recurse = 0;
3123 list_T *l;
3124 listitem_T *li;
3125 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003126 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003127 hashitem_T *hi;
3128 int todo;
3129
3130 if (recurse >= DICT_MAXNEST)
3131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003132 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003133 return;
3134 }
3135 if (deep == 0)
3136 return;
3137 ++recurse;
3138
3139 /* lock/unlock the item itself */
3140 if (lock)
3141 tv->v_lock |= VAR_LOCKED;
3142 else
3143 tv->v_lock &= ~VAR_LOCKED;
3144
3145 switch (tv->v_type)
3146 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003147 case VAR_UNKNOWN:
3148 case VAR_NUMBER:
3149 case VAR_STRING:
3150 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003151 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003152 case VAR_FLOAT:
3153 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003154 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003155 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003156 break;
3157
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003158 case VAR_BLOB:
3159 if ((b = tv->vval.v_blob) != NULL)
3160 {
3161 if (lock)
3162 b->bv_lock |= VAR_LOCKED;
3163 else
3164 b->bv_lock &= ~VAR_LOCKED;
3165 }
3166 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003167 case VAR_LIST:
3168 if ((l = tv->vval.v_list) != NULL)
3169 {
3170 if (lock)
3171 l->lv_lock |= VAR_LOCKED;
3172 else
3173 l->lv_lock &= ~VAR_LOCKED;
3174 if (deep < 0 || deep > 1)
3175 /* recursive: lock/unlock the items the List contains */
3176 for (li = l->lv_first; li != NULL; li = li->li_next)
3177 item_lock(&li->li_tv, deep - 1, lock);
3178 }
3179 break;
3180 case VAR_DICT:
3181 if ((d = tv->vval.v_dict) != NULL)
3182 {
3183 if (lock)
3184 d->dv_lock |= VAR_LOCKED;
3185 else
3186 d->dv_lock &= ~VAR_LOCKED;
3187 if (deep < 0 || deep > 1)
3188 {
3189 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003190 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003191 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3192 {
3193 if (!HASHITEM_EMPTY(hi))
3194 {
3195 --todo;
3196 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3197 }
3198 }
3199 }
3200 }
3201 }
3202 --recurse;
3203}
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3206/*
3207 * Delete all "menutrans_" variables.
3208 */
3209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003213 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003216 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003218 {
3219 if (!HASHITEM_EMPTY(hi))
3220 {
3221 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3223 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003224 }
3225 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227}
3228#endif
3229
3230#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3231
3232/*
3233 * Local string buffer for the next two functions to store a variable name
3234 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3235 * get_user_var_name().
3236 */
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238static char_u *varnamebuf = NULL;
3239static int varnamebuflen = 0;
3240
3241/*
3242 * Function to concatenate a prefix and a variable name.
3243 */
3244 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003245cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 int len;
3248
3249 len = (int)STRLEN(name) + 3;
3250 if (len > varnamebuflen)
3251 {
3252 vim_free(varnamebuf);
3253 len += 10; /* some additional space */
3254 varnamebuf = alloc(len);
3255 if (varnamebuf == NULL)
3256 {
3257 varnamebuflen = 0;
3258 return NULL;
3259 }
3260 varnamebuflen = len;
3261 }
3262 *varnamebuf = prefix;
3263 varnamebuf[1] = ':';
3264 STRCPY(varnamebuf + 2, name);
3265 return varnamebuf;
3266}
3267
3268/*
3269 * Function given to ExpandGeneric() to obtain the list of user defined
3270 * (global/buffer/window/built-in) variable names.
3271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003273get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003275 static long_u gdone;
3276 static long_u bdone;
3277 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003278 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003279 static int vidx;
3280 static hashitem_T *hi;
3281 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003284 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003285 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003286 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003287 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003288
3289 /* Global variables */
3290 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003292 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003294 else
3295 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003296 while (HASHITEM_EMPTY(hi))
3297 ++hi;
3298 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3299 return cat_prefix_varname('g', hi->hi_key);
3300 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003302
3303 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003304 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003305 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003307 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003309 else
3310 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003311 while (HASHITEM_EMPTY(hi))
3312 ++hi;
3313 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003315
3316 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003317 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003320 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003322 else
3323 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003324 while (HASHITEM_EMPTY(hi))
3325 ++hi;
3326 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003328
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003329 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003330 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003331 if (tdone < ht->ht_used)
3332 {
3333 if (tdone++ == 0)
3334 hi = ht->ht_array;
3335 else
3336 ++hi;
3337 while (HASHITEM_EMPTY(hi))
3338 ++hi;
3339 return cat_prefix_varname('t', hi->hi_key);
3340 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003341
Bram Moolenaar33570922005-01-25 22:26:29 +00003342 /* v: variables */
3343 if (vidx < VV_LEN)
3344 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaard23a8232018-02-10 18:45:26 +01003346 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 varnamebuflen = 0;
3348 return NULL;
3349}
3350
3351#endif /* FEAT_CMDL_COMPL */
3352
3353/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003354 * Return TRUE if "pat" matches "text".
3355 * Does not use 'cpo' and always uses 'magic'.
3356 */
3357 static int
3358pattern_match(char_u *pat, char_u *text, int ic)
3359{
3360 int matches = FALSE;
3361 char_u *save_cpo;
3362 regmatch_T regmatch;
3363
3364 /* avoid 'l' flag in 'cpoptions' */
3365 save_cpo = p_cpo;
3366 p_cpo = (char_u *)"";
3367 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3368 if (regmatch.regprog != NULL)
3369 {
3370 regmatch.rm_ic = ic;
3371 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3372 vim_regfree(regmatch.regprog);
3373 }
3374 p_cpo = save_cpo;
3375 return matches;
3376}
3377
3378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3382 */
3383
3384/*
3385 * Handle zero level expression.
3386 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003388 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * Return OK or FAIL.
3390 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003392eval0(
3393 char_u *arg,
3394 typval_T *rettv,
3395 char_u **nextcmd,
3396 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 int ret;
3399 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003400 int did_emsg_before = did_emsg;
3401 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (ret == FAIL || !ends_excmd(*p))
3406 {
3407 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 /*
3410 * Report the invalid expression unless the expression evaluation has
3411 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003412 * exception, or we already gave a more specific error.
3413 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003415 if (!aborting() && did_emsg == did_emsg_before
3416 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003417 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 ret = FAIL;
3419 }
3420 if (nextcmd != NULL)
3421 *nextcmd = check_nextcmd(p);
3422
3423 return ret;
3424}
3425
3426/*
3427 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003428 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 *
3430 * "arg" must point to the first non-white of the expression.
3431 * "arg" is advanced to the next non-white after the recognized expression.
3432 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003433 * Note: "rettv.v_lock" is not set.
3434 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * Return OK or FAIL.
3436 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003438eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 /*
3444 * Get the first variable.
3445 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return FAIL;
3448
3449 if ((*arg)[0] == '?')
3450 {
3451 result = FALSE;
3452 if (evaluate)
3453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003454 int error = FALSE;
3455
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003456 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003458 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003459 if (error)
3460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462
3463 /*
3464 * Get the second variable.
3465 */
3466 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return FAIL;
3469
3470 /*
3471 * Check for the ":".
3472 */
3473 if ((*arg)[0] != ':')
3474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003475 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003477 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return FAIL;
3479 }
3480
3481 /*
3482 * Get the third variable.
3483 */
3484 *arg = skipwhite(*arg + 1);
3485 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3486 {
3487 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return FAIL;
3490 }
3491 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494
3495 return OK;
3496}
3497
3498/*
3499 * Handle first level expression:
3500 * expr2 || expr2 || expr2 logical OR
3501 *
3502 * "arg" must point to the first non-white of the expression.
3503 * "arg" is advanced to the next non-white after the recognized expression.
3504 *
3505 * Return OK or FAIL.
3506 */
3507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
Bram Moolenaar33570922005-01-25 22:26:29 +00003510 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 long result;
3512 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003513 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /*
3516 * Get the first variable.
3517 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 return FAIL;
3520
3521 /*
3522 * Repeat until there is no following "||".
3523 */
3524 first = TRUE;
3525 result = FALSE;
3526 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3527 {
3528 if (evaluate && first)
3529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003530 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003532 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003533 if (error)
3534 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 first = FALSE;
3536 }
3537
3538 /*
3539 * Get the second variable.
3540 */
3541 *arg = skipwhite(*arg + 2);
3542 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3543 return FAIL;
3544
3545 /*
3546 * Compute the result.
3547 */
3548 if (evaluate && !result)
3549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003550 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003552 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003553 if (error)
3554 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556 if (evaluate)
3557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 rettv->v_type = VAR_NUMBER;
3559 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561 }
3562
3563 return OK;
3564}
3565
3566/*
3567 * Handle second level expression:
3568 * expr3 && expr3 && expr3 logical AND
3569 *
3570 * "arg" must point to the first non-white of the expression.
3571 * "arg" is advanced to the next non-white after the recognized expression.
3572 *
3573 * Return OK or FAIL.
3574 */
3575 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 long result;
3580 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /*
3584 * Get the first variable.
3585 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003586 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return FAIL;
3588
3589 /*
3590 * Repeat until there is no following "&&".
3591 */
3592 first = TRUE;
3593 result = TRUE;
3594 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3595 {
3596 if (evaluate && first)
3597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003598 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 if (error)
3602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 first = FALSE;
3604 }
3605
3606 /*
3607 * Get the second variable.
3608 */
3609 *arg = skipwhite(*arg + 2);
3610 if (eval4(arg, &var2, evaluate && result) == FAIL)
3611 return FAIL;
3612
3613 /*
3614 * Compute the result.
3615 */
3616 if (evaluate && result)
3617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003618 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003621 if (error)
3622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 if (evaluate)
3625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 rettv->v_type = VAR_NUMBER;
3627 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 }
3630
3631 return OK;
3632}
3633
3634/*
3635 * Handle third level expression:
3636 * var1 == var2
3637 * var1 =~ var2
3638 * var1 != var2
3639 * var1 !~ var2
3640 * var1 > var2
3641 * var1 >= var2
3642 * var1 < var2
3643 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003644 * var1 is var2
3645 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 *
3647 * "arg" must point to the first non-white of the expression.
3648 * "arg" is advanced to the next non-white after the recognized expression.
3649 *
3650 * Return OK or FAIL.
3651 */
3652 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003653eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u *p;
3657 int i;
3658 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003659 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /*
3664 * Get the first variable.
3665 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return FAIL;
3668
3669 p = *arg;
3670 switch (p[0])
3671 {
3672 case '=': if (p[1] == '=')
3673 type = TYPE_EQUAL;
3674 else if (p[1] == '~')
3675 type = TYPE_MATCH;
3676 break;
3677 case '!': if (p[1] == '=')
3678 type = TYPE_NEQUAL;
3679 else if (p[1] == '~')
3680 type = TYPE_NOMATCH;
3681 break;
3682 case '>': if (p[1] != '=')
3683 {
3684 type = TYPE_GREATER;
3685 len = 1;
3686 }
3687 else
3688 type = TYPE_GEQUAL;
3689 break;
3690 case '<': if (p[1] != '=')
3691 {
3692 type = TYPE_SMALLER;
3693 len = 1;
3694 }
3695 else
3696 type = TYPE_SEQUAL;
3697 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003698 case 'i': if (p[1] == 's')
3699 {
3700 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3701 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003702 i = p[len];
3703 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003704 {
3705 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3706 type_is = TRUE;
3707 }
3708 }
3709 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711
3712 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003713 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 */
3715 if (type != TYPE_UNKNOWN)
3716 {
3717 /* extra question mark appended: ignore case */
3718 if (p[len] == '?')
3719 {
3720 ic = TRUE;
3721 ++len;
3722 }
3723 /* extra '#' appended: match case */
3724 else if (p[len] == '#')
3725 {
3726 ic = FALSE;
3727 ++len;
3728 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003729 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 else
3731 ic = p_ic;
3732
3733 /*
3734 * Get the second variable.
3735 */
3736 *arg = skipwhite(p + len);
3737 if (eval5(arg, &var2, evaluate) == FAIL)
3738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 return FAIL;
3741 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003742 if (evaluate)
3743 {
3744 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3745
3746 clear_tv(&var2);
3747 return ret;
3748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750
3751 return OK;
3752}
3753
3754/*
3755 * Handle fourth level expression:
3756 * + number addition
3757 * - number subtraction
3758 * . string concatenation
3759 *
3760 * "arg" must point to the first non-white of the expression.
3761 * "arg" is advanced to the next non-white after the recognized expression.
3762 *
3763 * Return OK or FAIL.
3764 */
3765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaar33570922005-01-25 22:26:29 +00003768 typval_T var2;
3769 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003771 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003772#ifdef FEAT_FLOAT
3773 float_T f1 = 0, f2 = 0;
3774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 char_u *s1, *s2;
3776 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3777 char_u *p;
3778
3779 /*
3780 * Get the first variable.
3781 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003782 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return FAIL;
3784
3785 /*
3786 * Repeat computing, until no '+', '-' or '.' is following.
3787 */
3788 for (;;)
3789 {
3790 op = **arg;
3791 if (op != '+' && op != '-' && op != '.')
3792 break;
3793
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003794 if ((op != '+' || (rettv->v_type != VAR_LIST
3795 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003796#ifdef FEAT_FLOAT
3797 && (op == '.' || rettv->v_type != VAR_FLOAT)
3798#endif
3799 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 {
3801 /* For "list + ...", an illegal use of the first operand as
3802 * a number cannot be determined before evaluating the 2nd
3803 * operand: if this is also a list, all is ok.
3804 * For "something . ...", "something - ..." or "non-list + ...",
3805 * we know that the first operand needs to be a string or number
3806 * without evaluating the 2nd operand. So check before to avoid
3807 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003808 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 {
3810 clear_tv(rettv);
3811 return FAIL;
3812 }
3813 }
3814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 /*
3816 * Get the second variable.
3817 */
3818 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003819 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823 }
3824
3825 if (evaluate)
3826 {
3827 /*
3828 * Compute the result.
3829 */
3830 if (op == '.')
3831 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003832 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3833 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (s2 == NULL) /* type error ? */
3835 {
3836 clear_tv(rettv);
3837 clear_tv(&var2);
3838 return FAIL;
3839 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003840 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
3842 rettv->v_type = VAR_STRING;
3843 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003845 else if (op == '+' && rettv->v_type == VAR_BLOB
3846 && var2.v_type == VAR_BLOB)
3847 {
3848 blob_T *b1 = rettv->vval.v_blob;
3849 blob_T *b2 = var2.vval.v_blob;
3850 blob_T *b = blob_alloc();
3851 int i;
3852
3853 if (b != NULL)
3854 {
3855 for (i = 0; i < blob_len(b1); i++)
3856 ga_append(&b->bv_ga, blob_get(b1, i));
3857 for (i = 0; i < blob_len(b2); i++)
3858 ga_append(&b->bv_ga, blob_get(b2, i));
3859
3860 clear_tv(rettv);
3861 rettv_blob_set(rettv, b);
3862 }
3863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003864 else if (op == '+' && rettv->v_type == VAR_LIST
3865 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003866 {
3867 /* concatenate Lists */
3868 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3869 &var3) == FAIL)
3870 {
3871 clear_tv(rettv);
3872 clear_tv(&var2);
3873 return FAIL;
3874 }
3875 clear_tv(rettv);
3876 *rettv = var3;
3877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
3879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 int error = FALSE;
3881
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003882#ifdef FEAT_FLOAT
3883 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003884 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003885 f1 = rettv->vval.v_float;
3886 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003887 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888 else
3889#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003890 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003891 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892 if (error)
3893 {
3894 /* This can only happen for "list + non-list". For
3895 * "non-list + ..." or "something - ...", we returned
3896 * before evaluating the 2nd operand. */
3897 clear_tv(rettv);
3898 return FAIL;
3899 }
3900#ifdef FEAT_FLOAT
3901 if (var2.v_type == VAR_FLOAT)
3902 f1 = n1;
3903#endif
3904 }
3905#ifdef FEAT_FLOAT
3906 if (var2.v_type == VAR_FLOAT)
3907 {
3908 f2 = var2.vval.v_float;
3909 n2 = 0;
3910 }
3911 else
3912#endif
3913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003914 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003915 if (error)
3916 {
3917 clear_tv(rettv);
3918 clear_tv(&var2);
3919 return FAIL;
3920 }
3921#ifdef FEAT_FLOAT
3922 if (rettv->v_type == VAR_FLOAT)
3923 f2 = n2;
3924#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003927
3928#ifdef FEAT_FLOAT
3929 /* If there is a float on either side the result is a float. */
3930 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3931 {
3932 if (op == '+')
3933 f1 = f1 + f2;
3934 else
3935 f1 = f1 - f2;
3936 rettv->v_type = VAR_FLOAT;
3937 rettv->vval.v_float = f1;
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003940#endif
3941 {
3942 if (op == '+')
3943 n1 = n1 + n2;
3944 else
3945 n1 = n1 - n2;
3946 rettv->v_type = VAR_NUMBER;
3947 rettv->vval.v_number = n1;
3948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952 }
3953 return OK;
3954}
3955
3956/*
3957 * Handle fifth level expression:
3958 * * number multiplication
3959 * / number division
3960 * % number modulo
3961 *
3962 * "arg" must point to the first non-white of the expression.
3963 * "arg" is advanced to the next non-white after the recognized expression.
3964 *
3965 * Return OK or FAIL.
3966 */
3967 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003968eval6(
3969 char_u **arg,
3970 typval_T *rettv,
3971 int evaluate,
3972 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003976 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003977#ifdef FEAT_FLOAT
3978 int use_float = FALSE;
3979 float_T f1 = 0, f2;
3980#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003981 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
3983 /*
3984 * Get the first variable.
3985 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003986 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return FAIL;
3988
3989 /*
3990 * Repeat computing, until no '*', '/' or '%' is following.
3991 */
3992 for (;;)
3993 {
3994 op = **arg;
3995 if (op != '*' && op != '/' && op != '%')
3996 break;
3997
3998 if (evaluate)
3999 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004000#ifdef FEAT_FLOAT
4001 if (rettv->v_type == VAR_FLOAT)
4002 {
4003 f1 = rettv->vval.v_float;
4004 use_float = TRUE;
4005 n1 = 0;
4006 }
4007 else
4008#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004009 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 if (error)
4012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014 else
4015 n1 = 0;
4016
4017 /*
4018 * Get the second variable.
4019 */
4020 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004021 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return FAIL;
4023
4024 if (evaluate)
4025 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004026#ifdef FEAT_FLOAT
4027 if (var2.v_type == VAR_FLOAT)
4028 {
4029 if (!use_float)
4030 {
4031 f1 = n1;
4032 use_float = TRUE;
4033 }
4034 f2 = var2.vval.v_float;
4035 n2 = 0;
4036 }
4037 else
4038#endif
4039 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004040 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004041 clear_tv(&var2);
4042 if (error)
4043 return FAIL;
4044#ifdef FEAT_FLOAT
4045 if (use_float)
4046 f2 = n2;
4047#endif
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 /*
4051 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004052 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004054#ifdef FEAT_FLOAT
4055 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004057 if (op == '*')
4058 f1 = f1 * f2;
4059 else if (op == '/')
4060 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004061# ifdef VMS
4062 /* VMS crashes on divide by zero, work around it */
4063 if (f2 == 0.0)
4064 {
4065 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004066 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004067 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004068 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004069 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004070 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004071 }
4072 else
4073 f1 = f1 / f2;
4074# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075 /* We rely on the floating point library to handle divide
4076 * by zero to result in "inf" and not a crash. */
4077 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004078# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004082 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004083 return FAIL;
4084 }
4085 rettv->v_type = VAR_FLOAT;
4086 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004091 if (op == '*')
4092 n1 = n1 * n2;
4093 else if (op == '/')
4094 {
4095 if (n2 == 0) /* give an error message? */
4096 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004097 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004098 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004099 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004100 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004101 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004102 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 }
4104 else
4105 n1 = n1 / n2;
4106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004108 {
4109 if (n2 == 0) /* give an error message? */
4110 n1 = 0;
4111 else
4112 n1 = n1 % n2;
4113 }
4114 rettv->v_type = VAR_NUMBER;
4115 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 }
4119
4120 return OK;
4121}
4122
4123/*
4124 * Handle sixth level expression:
4125 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004126 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004127 * "string" string constant
4128 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * &option-name option value
4130 * @r register contents
4131 * identifier variable value
4132 * function() function call
4133 * $VAR environment variable
4134 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004135 * [expr, expr] List
4136 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 *
4138 * Also handle:
4139 * ! in front logical NOT
4140 * - in front unary minus
4141 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004142 * trailing [] subscript in String or List
4143 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 *
4145 * "arg" must point to the first non-white of the expression.
4146 * "arg" is advanced to the next non-white after the recognized expression.
4147 *
4148 * Return OK or FAIL.
4149 */
4150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004151eval7(
4152 char_u **arg,
4153 typval_T *rettv,
4154 int evaluate,
4155 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004157 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int len;
4159 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 char_u *start_leader, *end_leader;
4161 int ret = OK;
4162 char_u *alias;
4163
4164 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004166 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004171 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 */
4173 start_leader = *arg;
4174 while (**arg == '!' || **arg == '-' || **arg == '+')
4175 *arg = skipwhite(*arg + 1);
4176 end_leader = *arg;
4177
4178 switch (**arg)
4179 {
4180 /*
4181 * Number constant.
4182 */
4183 case '0':
4184 case '1':
4185 case '2':
4186 case '3':
4187 case '4':
4188 case '5':
4189 case '6':
4190 case '7':
4191 case '8':
4192 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004193 {
4194#ifdef FEAT_FLOAT
4195 char_u *p = skipdigits(*arg + 1);
4196 int get_float = FALSE;
4197
4198 /* We accept a float when the format matches
4199 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004200 * strict to avoid backwards compatibility problems.
4201 * Don't look for a float after the "." operator, so that
4202 * ":let vers = 1.2.3" doesn't fail. */
4203 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004205 get_float = TRUE;
4206 p = skipdigits(p + 2);
4207 if (*p == 'e' || *p == 'E')
4208 {
4209 ++p;
4210 if (*p == '-' || *p == '+')
4211 ++p;
4212 if (!vim_isdigit(*p))
4213 get_float = FALSE;
4214 else
4215 p = skipdigits(p + 1);
4216 }
4217 if (ASCII_ISALPHA(*p) || *p == '.')
4218 get_float = FALSE;
4219 }
4220 if (get_float)
4221 {
4222 float_T f;
4223
4224 *arg += string2float(*arg, &f);
4225 if (evaluate)
4226 {
4227 rettv->v_type = VAR_FLOAT;
4228 rettv->vval.v_float = f;
4229 }
4230 }
4231 else
4232#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004233 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004234 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004235 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004236 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004237
4238 // Blob constant: 0z0123456789abcdef
4239 if (evaluate)
4240 blob = blob_alloc();
4241 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4242 {
4243 if (!vim_isxdigit(bp[1]))
4244 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004245 if (blob != NULL)
4246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004248 ga_clear(&blob->bv_ga);
4249 VIM_CLEAR(blob);
4250 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004251 ret = FAIL;
4252 break;
4253 }
4254 if (blob != NULL)
4255 ga_append(&blob->bv_ga,
4256 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004257 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4258 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004259 }
4260 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004261 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004262 *arg = bp;
4263 }
4264 else
4265 {
4266 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004267 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004268 *arg += len;
4269 if (evaluate)
4270 {
4271 rettv->v_type = VAR_NUMBER;
4272 rettv->vval.v_number = n;
4273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 /*
4279 * String constant: "string".
4280 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 break;
4283
4284 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004285 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004288 break;
4289
4290 /*
4291 * List: [expr, expr]
4292 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 break;
4295
4296 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004297 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004298 * Dictionary: {key: val, key: val}
4299 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004300 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4301 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004302 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004303 break;
4304
4305 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004306 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004308 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 break;
4310
4311 /*
4312 * Environment variable: $VAR.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 break;
4316
4317 /*
4318 * Register contents: @r.
4319 */
4320 case '@': ++*arg;
4321 if (evaluate)
4322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004324 rettv->vval.v_string = get_reg_contents(**arg,
4325 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 if (**arg != NUL)
4328 ++*arg;
4329 break;
4330
4331 /*
4332 * nested expression: (expression).
4333 */
4334 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (**arg == ')')
4337 ++*arg;
4338 else if (ret == OK)
4339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004340 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 ret = FAIL;
4343 }
4344 break;
4345
Bram Moolenaar8c711452005-01-14 21:53:12 +00004346 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 break;
4348 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349
4350 if (ret == NOTDONE)
4351 {
4352 /*
4353 * Must be a variable or function name.
4354 * Can also be a curly-braces kind of name: {expr}.
4355 */
4356 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004357 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 if (alias != NULL)
4359 s = alias;
4360
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004361 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 ret = FAIL;
4363 else
4364 {
4365 if (**arg == '(') /* recursive! */
4366 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004367 partial_T *partial;
4368
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004369 if (!evaluate)
4370 check_vars(s, len);
4371
Bram Moolenaar8c711452005-01-14 21:53:12 +00004372 /* If "s" is the name of a variable of type VAR_FUNC
4373 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004374 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004375
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004376 /* Need to make a copy, in case evaluating the arguments makes
4377 * the name invalid. */
4378 s = vim_strsave(s);
4379 if (s == NULL)
4380 ret = FAIL;
4381 else
4382 /* Invoke the function. */
4383 ret = get_func_tv(s, len, rettv, arg,
4384 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4385 &len, evaluate, partial, NULL);
4386 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004387
4388 /* If evaluate is FALSE rettv->v_type was not set in
4389 * get_func_tv, but it's needed in handle_subscript() to parse
4390 * what follows. So set it here. */
4391 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4392 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004393 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004394 rettv->v_type = VAR_FUNC;
4395 }
4396
Bram Moolenaar8c711452005-01-14 21:53:12 +00004397 /* Stop the expression evaluation when immediately
4398 * aborting on error, or when an interrupt occurred or
4399 * an exception was thrown but not caught. */
4400 if (aborting())
4401 {
4402 if (ret == OK)
4403 clear_tv(rettv);
4404 ret = FAIL;
4405 }
4406 }
4407 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004408 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004409 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004410 {
4411 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004412 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004413 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004415 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004416 }
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 *arg = skipwhite(*arg);
4419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004420 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4421 * expr(expr). */
4422 if (ret == OK)
4423 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 /*
4426 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4427 */
4428 if (ret == OK && evaluate && end_leader > start_leader)
4429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004430 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004431 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004432#ifdef FEAT_FLOAT
4433 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004434
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004435 if (rettv->v_type == VAR_FLOAT)
4436 f = rettv->vval.v_float;
4437 else
4438#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004439 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004440 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 clear_tv(rettv);
4443 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 else
4446 {
4447 while (end_leader > start_leader)
4448 {
4449 --end_leader;
4450 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004451 {
4452#ifdef FEAT_FLOAT
4453 if (rettv->v_type == VAR_FLOAT)
4454 f = !f;
4455 else
4456#endif
4457 val = !val;
4458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460 {
4461#ifdef FEAT_FLOAT
4462 if (rettv->v_type == VAR_FLOAT)
4463 f = -f;
4464 else
4465#endif
4466 val = -val;
4467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004468 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004469#ifdef FEAT_FLOAT
4470 if (rettv->v_type == VAR_FLOAT)
4471 {
4472 clear_tv(rettv);
4473 rettv->vval.v_float = f;
4474 }
4475 else
4476#endif
4477 {
4478 clear_tv(rettv);
4479 rettv->v_type = VAR_NUMBER;
4480 rettv->vval.v_number = val;
4481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484
4485 return ret;
4486}
4487
4488/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004489 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4490 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4492 */
4493 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004494eval_index(
4495 char_u **arg,
4496 typval_T *rettv,
4497 int evaluate,
4498 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499{
4500 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004501 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004502 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 long len = -1;
4505 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508
Bram Moolenaara03f2332016-02-06 18:09:59 +01004509 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004510 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004512 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004513 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004514 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 return FAIL;
4516 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004517#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004518 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004519 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004520 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004521#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004522 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004523 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004524 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004525 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004526 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004527 return FAIL;
4528 case VAR_UNKNOWN:
4529 if (evaluate)
4530 return FAIL;
4531 /* FALLTHROUGH */
4532
4533 case VAR_STRING:
4534 case VAR_NUMBER:
4535 case VAR_LIST:
4536 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004537 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004538 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004539 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004541 init_tv(&var1);
4542 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 /*
4546 * dict.name
4547 */
4548 key = *arg + 1;
4549 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4550 ;
4551 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 }
4555 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 /*
4558 * something[idx]
4559 *
4560 * Get the (first) variable from inside the [].
4561 */
4562 *arg = skipwhite(*arg + 1);
4563 if (**arg == ':')
4564 empty1 = TRUE;
4565 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4566 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004567 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004568 {
4569 /* not a number or string */
4570 clear_tv(&var1);
4571 return FAIL;
4572 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573
4574 /*
4575 * Get the second variable from inside the [:].
4576 */
4577 if (**arg == ':')
4578 {
4579 range = TRUE;
4580 *arg = skipwhite(*arg + 1);
4581 if (**arg == ']')
4582 empty2 = TRUE;
4583 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004585 if (!empty1)
4586 clear_tv(&var1);
4587 return FAIL;
4588 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004589 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 {
4591 /* not a number or string */
4592 if (!empty1)
4593 clear_tv(&var1);
4594 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 return FAIL;
4596 }
4597 }
4598
4599 /* Check for the ']'. */
4600 if (**arg != ']')
4601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004603 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004604 clear_tv(&var1);
4605 if (range)
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004610 }
4611
4612 if (evaluate)
4613 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 n1 = 0;
4615 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004617 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 }
4620 if (range)
4621 {
4622 if (empty2)
4623 n2 = -1;
4624 else
4625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004626 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 }
4629 }
4630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004633 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004634 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004635 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004636 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004637 case VAR_SPECIAL:
4638 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004639 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004640 break; /* not evaluating, skipping over subscript */
4641
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 case VAR_NUMBER:
4643 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004644 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 len = (long)STRLEN(s);
4646 if (range)
4647 {
4648 /* The resulting variable is a substring. If the indexes
4649 * are out of range the result is empty. */
4650 if (n1 < 0)
4651 {
4652 n1 = len + n1;
4653 if (n1 < 0)
4654 n1 = 0;
4655 }
4656 if (n2 < 0)
4657 n2 = len + n2;
4658 else if (n2 >= len)
4659 n2 = len;
4660 if (n1 >= len || n2 < 0 || n1 > n2)
4661 s = NULL;
4662 else
4663 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4664 }
4665 else
4666 {
4667 /* The resulting variable is a string of a single
4668 * character. If the index is too big or negative the
4669 * result is empty. */
4670 if (n1 >= len || n1 < 0)
4671 s = NULL;
4672 else
4673 s = vim_strnsave(s + n1, 1);
4674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 clear_tv(rettv);
4676 rettv->v_type = VAR_STRING;
4677 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004678 break;
4679
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004680 case VAR_BLOB:
4681 len = blob_len(rettv->vval.v_blob);
4682 if (range)
4683 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004684 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004685 // are out of range the result is empty.
4686 if (n1 < 0)
4687 {
4688 n1 = len + n1;
4689 if (n1 < 0)
4690 n1 = 0;
4691 }
4692 if (n2 < 0)
4693 n2 = len + n2;
4694 else if (n2 >= len)
4695 n2 = len - 1;
4696 if (n1 >= len || n2 < 0 || n1 > n2)
4697 {
4698 clear_tv(rettv);
4699 rettv->v_type = VAR_BLOB;
4700 rettv->vval.v_blob = NULL;
4701 }
4702 else
4703 {
4704 blob_T *blob = blob_alloc();
4705
4706 if (blob != NULL)
4707 {
4708 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4709 {
4710 blob_free(blob);
4711 return FAIL;
4712 }
4713 blob->bv_ga.ga_len = n2 - n1 + 1;
4714 for (i = n1; i <= n2; i++)
4715 blob_set(blob, i - n1,
4716 blob_get(rettv->vval.v_blob, i));
4717
4718 clear_tv(rettv);
4719 rettv_blob_set(rettv, blob);
4720 }
4721 }
4722 }
4723 else
4724 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004725 // The resulting variable is a byte value.
4726 // If the index is too big or negative that is an error.
4727 if (n1 < 0)
4728 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004729 if (n1 < len && n1 >= 0)
4730 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004731 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004732
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 /* For "\u" store the number according to
5001 * 'encoding'. */
5002 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008
5009 /* octal: "\1", "\12", "\123" */
5010 case '0':
5011 case '1':
5012 case '2':
5013 case '3':
5014 case '4':
5015 case '5':
5016 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 case '7': *name = *p++ - '0';
5018 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 *name = (*name << 3) + *p++ - '0';
5021 if (*p >= '0' && *p <= '7')
5022 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026
5027 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005028 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (extra != 0)
5030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 break;
5033 }
5034 /* FALLTHROUGH */
5035
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
5039 }
5040 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005045 if (*p != NUL) /* just in case */
5046 ++p;
5047 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 return OK;
5050}
5051
5052/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 * Return OK or FAIL.
5055 */
5056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005057get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
5059 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 int reduce = 0;
5062
5063 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005066 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 break;
5072 ++reduce;
5073 ++p;
5074 }
5075 }
5076
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005079 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 return FAIL;
5081 }
5082
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 if (!evaluate)
5085 {
5086 *arg = p + 1;
5087 return OK;
5088 }
5089
5090 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005092 */
5093 str = alloc((unsigned)((p - *arg) - reduce));
5094 if (str == NULL)
5095 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 rettv->v_type = VAR_STRING;
5097 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 break;
5105 ++p;
5106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 *arg = p + 1;
5111
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 return OK;
5113}
5114
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005115/*
5116 * Return the function name of the partial.
5117 */
5118 char_u *
5119partial_name(partial_T *pt)
5120{
5121 if (pt->pt_name != NULL)
5122 return pt->pt_name;
5123 return pt->pt_func->uf_name;
5124}
5125
Bram Moolenaarddecc252016-04-06 22:59:37 +02005126 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005127partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005128{
5129 int i;
5130
5131 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005132 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005133 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005134 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005135 if (pt->pt_name != NULL)
5136 {
5137 func_unref(pt->pt_name);
5138 vim_free(pt->pt_name);
5139 }
5140 else
5141 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005142 vim_free(pt);
5143}
5144
5145/*
5146 * Unreference a closure: decrement the reference count and free it when it
5147 * becomes zero.
5148 */
5149 void
5150partial_unref(partial_T *pt)
5151{
5152 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005153 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005154}
5155
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005156static int tv_equal_recurse_limit;
5157
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005158 static int
5159func_equal(
5160 typval_T *tv1,
5161 typval_T *tv2,
5162 int ic) /* ignore case */
5163{
5164 char_u *s1, *s2;
5165 dict_T *d1, *d2;
5166 int a1, a2;
5167 int i;
5168
5169 /* empty and NULL function name considered the same */
5170 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005171 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005172 if (s1 != NULL && *s1 == NUL)
5173 s1 = NULL;
5174 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005175 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005176 if (s2 != NULL && *s2 == NUL)
5177 s2 = NULL;
5178 if (s1 == NULL || s2 == NULL)
5179 {
5180 if (s1 != s2)
5181 return FALSE;
5182 }
5183 else if (STRCMP(s1, s2) != 0)
5184 return FALSE;
5185
5186 /* empty dict and NULL dict is different */
5187 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5188 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5189 if (d1 == NULL || d2 == NULL)
5190 {
5191 if (d1 != d2)
5192 return FALSE;
5193 }
5194 else if (!dict_equal(d1, d2, ic, TRUE))
5195 return FALSE;
5196
5197 /* empty list and no list considered the same */
5198 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5199 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5200 if (a1 != a2)
5201 return FALSE;
5202 for (i = 0; i < a1; ++i)
5203 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5204 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5205 return FALSE;
5206
5207 return TRUE;
5208}
5209
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005210/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005211 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005212 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005214 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005216tv_equal(
5217 typval_T *tv1,
5218 typval_T *tv2,
5219 int ic, /* ignore case */
5220 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005221{
5222 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005224 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005225 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005226
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005227 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005228 * recursiveness to a limit. We guess they are equal then.
5229 * A fixed limit has the problem of still taking an awful long time.
5230 * Reduce the limit every time running into it. That should work fine for
5231 * deeply linked structures that are not recursively linked and catch
5232 * recursiveness quickly. */
5233 if (!recursive)
5234 tv_equal_recurse_limit = 1000;
5235 if (recursive_cnt >= tv_equal_recurse_limit)
5236 {
5237 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005238 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005239 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005240
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005241 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5242 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005243 if ((tv1->v_type == VAR_FUNC
5244 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5245 && (tv2->v_type == VAR_FUNC
5246 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5247 {
5248 ++recursive_cnt;
5249 r = func_equal(tv1, tv2, ic);
5250 --recursive_cnt;
5251 return r;
5252 }
5253
5254 if (tv1->v_type != tv2->v_type)
5255 return FALSE;
5256
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005257 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005258 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005259 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005260 ++recursive_cnt;
5261 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5262 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005263 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005264
5265 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005266 ++recursive_cnt;
5267 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5268 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005269 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005270
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005271 case VAR_BLOB:
5272 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5273
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005274 case VAR_NUMBER:
5275 return tv1->vval.v_number == tv2->vval.v_number;
5276
5277 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005278 s1 = tv_get_string_buf(tv1, buf1);
5279 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005280 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005281
5282 case VAR_SPECIAL:
5283 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005284
5285 case VAR_FLOAT:
5286#ifdef FEAT_FLOAT
5287 return tv1->vval.v_float == tv2->vval.v_float;
5288#endif
5289 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005290#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005291 return tv1->vval.v_job == tv2->vval.v_job;
5292#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005293 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005294#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005295 return tv1->vval.v_channel == tv2->vval.v_channel;
5296#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005297 case VAR_FUNC:
5298 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005299 case VAR_UNKNOWN:
5300 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005301 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005302
Bram Moolenaara03f2332016-02-06 18:09:59 +01005303 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5304 * does not equal anything, not even itself. */
5305 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005306}
5307
5308/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005309 * Return the next (unique) copy ID.
5310 * Used for serializing nested structures.
5311 */
5312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005314{
5315 current_copyID += COPYID_INC;
5316 return current_copyID;
5317}
5318
5319/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320 * Garbage collection for lists and dictionaries.
5321 *
5322 * We use reference counts to be able to free most items right away when they
5323 * are no longer used. But for composite items it's possible that it becomes
5324 * unused while the reference count is > 0: When there is a recursive
5325 * reference. Example:
5326 * :let l = [1, 2, 3]
5327 * :let d = {9: l}
5328 * :let l[1] = d
5329 *
5330 * Since this is quite unusual we handle this with garbage collection: every
5331 * once in a while find out which lists and dicts are not referenced from any
5332 * variable.
5333 *
5334 * Here is a good reference text about garbage collection (refers to Python
5335 * but it applies to all reference-counting mechanisms):
5336 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005337 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005338
5339/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005340 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005341 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005342 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005343 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005344 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005345garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005346{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005347 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005349 buf_T *buf;
5350 win_T *wp;
5351 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005352 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005353 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005355 if (!testing)
5356 {
5357 /* Only do this once. */
5358 want_garbage_collect = FALSE;
5359 may_garbage_collect = FALSE;
5360 garbage_collect_at_exit = FALSE;
5361 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005362
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005363 /* We advance by two because we add one for items referenced through
5364 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005365 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005366
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005367 /*
5368 * 1. Go through all accessible variables and mark all lists and dicts
5369 * with copyID.
5370 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005371
5372 /* Don't free variables in the previous_funccal list unless they are only
5373 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005374 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005375 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005376
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005377 /* script-local variables */
5378 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005379 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005380
5381 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005382 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005383 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5384 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005385
5386 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005387 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005388 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5389 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005390 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005391 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5392 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005393
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005394 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005395 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5397 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400
5401 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005404 /* named functions (matters for closures) */
5405 abort = abort || set_ref_in_functions(copyID);
5406
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005407 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005409
Bram Moolenaard812df62008-11-09 12:46:09 +00005410 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005411 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005412
Bram Moolenaar1dced572012-04-05 16:54:08 +02005413#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005414 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005415#endif
5416
Bram Moolenaardb913952012-06-29 12:54:53 +02005417#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005419#endif
5420
5421#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005423#endif
5424
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005425#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005426 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005427 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005428#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005429#ifdef FEAT_NETBEANS_INTG
5430 abort = abort || set_ref_in_nb_channel(copyID);
5431#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005432
Bram Moolenaare3188e22016-05-31 21:13:04 +02005433#ifdef FEAT_TIMERS
5434 abort = abort || set_ref_in_timer(copyID);
5435#endif
5436
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005437#ifdef FEAT_QUICKFIX
5438 abort = abort || set_ref_in_quickfix(copyID);
5439#endif
5440
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005441#ifdef FEAT_TERMINAL
5442 abort = abort || set_ref_in_term(copyID);
5443#endif
5444
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005445 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005446 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005447 /*
5448 * 2. Free lists and dictionaries that are not referenced.
5449 */
5450 did_free = free_unref_items(copyID);
5451
5452 /*
5453 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005454 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005455 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005457 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 else if (p_verbose > 0)
5459 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005460 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005461 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005462
5463 return did_free;
5464}
5465
5466/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005467 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005468 */
5469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005470free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005471{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005472 int did_free = FALSE;
5473
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005474 /* Let all "free" functions know that we are here. This means no
5475 * dictionaries, lists, channels or jobs are to be freed, because we will
5476 * do that here. */
5477 in_free_unref_items = TRUE;
5478
5479 /*
5480 * PASS 1: free the contents of the items. We don't free the items
5481 * themselves yet, so that it is possible to decrement refcount counters
5482 */
5483
Bram Moolenaarcd524592016-07-17 14:57:05 +02005484 /* Go through the list of dicts and free items without the copyID. */
5485 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005486
Bram Moolenaarda861d62016-07-17 15:46:27 +02005487 /* Go through the list of lists and free items without the copyID. */
5488 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005489
5490#ifdef FEAT_JOB_CHANNEL
5491 /* Go through the list of jobs and free items without the copyID. This
5492 * must happen before doing channels, because jobs refer to channels, but
5493 * the reference from the channel to the job isn't tracked. */
5494 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5495
5496 /* Go through the list of channels and free items without the copyID. */
5497 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5498#endif
5499
5500 /*
5501 * PASS 2: free the items themselves.
5502 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005503 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005504 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005505
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005506#ifdef FEAT_JOB_CHANNEL
5507 /* Go through the list of jobs and free items without the copyID. This
5508 * must happen before doing channels, because jobs refer to channels, but
5509 * the reference from the channel to the job isn't tracked. */
5510 free_unused_jobs(copyID, COPYID_MASK);
5511
5512 /* Go through the list of channels and free items without the copyID. */
5513 free_unused_channels(copyID, COPYID_MASK);
5514#endif
5515
5516 in_free_unref_items = FALSE;
5517
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005518 return did_free;
5519}
5520
5521/*
5522 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005523 * "list_stack" is used to add lists to be marked. Can be NULL.
5524 *
5525 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005526 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005528set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005529{
5530 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005532 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005533 hashtab_T *cur_ht;
5534 ht_stack_T *ht_stack = NULL;
5535 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005536
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 cur_ht = ht;
5538 for (;;)
5539 {
5540 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005541 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005542 /* Mark each item in the hashtab. If the item contains a hashtab
5543 * it is added to ht_stack, if it contains a list it is added to
5544 * list_stack. */
5545 todo = (int)cur_ht->ht_used;
5546 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5547 if (!HASHITEM_EMPTY(hi))
5548 {
5549 --todo;
5550 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5551 &ht_stack, list_stack);
5552 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005553 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005554
5555 if (ht_stack == NULL)
5556 break;
5557
5558 /* take an item from the stack */
5559 cur_ht = ht_stack->ht;
5560 tempitem = ht_stack;
5561 ht_stack = ht_stack->prev;
5562 free(tempitem);
5563 }
5564
5565 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005566}
5567
5568/*
5569 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005570 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5571 *
5572 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005573 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005575set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005576{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005577 listitem_T *li;
5578 int abort = FALSE;
5579 list_T *cur_l;
5580 list_stack_T *list_stack = NULL;
5581 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005583 cur_l = l;
5584 for (;;)
5585 {
5586 if (!abort)
5587 /* Mark each item in the list. If the item contains a hashtab
5588 * it is added to ht_stack, if it contains a list it is added to
5589 * list_stack. */
5590 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5591 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5592 ht_stack, &list_stack);
5593 if (list_stack == NULL)
5594 break;
5595
5596 /* take an item from the stack */
5597 cur_l = list_stack->list;
5598 tempitem = list_stack;
5599 list_stack = list_stack->prev;
5600 free(tempitem);
5601 }
5602
5603 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005604}
5605
5606/*
5607 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005608 * "list_stack" is used to add lists to be marked. Can be NULL.
5609 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5610 *
5611 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005612 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005614set_ref_in_item(
5615 typval_T *tv,
5616 int copyID,
5617 ht_stack_T **ht_stack,
5618 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005619{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005620 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005621
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005622 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005623 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005624 dict_T *dd = tv->vval.v_dict;
5625
Bram Moolenaara03f2332016-02-06 18:09:59 +01005626 if (dd != NULL && dd->dv_copyID != copyID)
5627 {
5628 /* Didn't see this dict yet. */
5629 dd->dv_copyID = copyID;
5630 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005631 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005632 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5633 }
5634 else
5635 {
5636 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5637 if (newitem == NULL)
5638 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005639 else
5640 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005641 newitem->ht = &dd->dv_hashtab;
5642 newitem->prev = *ht_stack;
5643 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005644 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005645 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005646 }
5647 }
5648 else if (tv->v_type == VAR_LIST)
5649 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005650 list_T *ll = tv->vval.v_list;
5651
Bram Moolenaara03f2332016-02-06 18:09:59 +01005652 if (ll != NULL && ll->lv_copyID != copyID)
5653 {
5654 /* Didn't see this list yet. */
5655 ll->lv_copyID = copyID;
5656 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005657 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005658 abort = set_ref_in_list(ll, copyID, ht_stack);
5659 }
5660 else
5661 {
5662 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005663 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005664 if (newitem == NULL)
5665 abort = TRUE;
5666 else
5667 {
5668 newitem->list = ll;
5669 newitem->prev = *list_stack;
5670 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005671 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005672 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005673 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005674 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005675 else if (tv->v_type == VAR_FUNC)
5676 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005677 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005678 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005679 else if (tv->v_type == VAR_PARTIAL)
5680 {
5681 partial_T *pt = tv->vval.v_partial;
5682 int i;
5683
5684 /* A partial does not have a copyID, because it cannot contain itself.
5685 */
5686 if (pt != NULL)
5687 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005688 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005689
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005690 if (pt->pt_dict != NULL)
5691 {
5692 typval_T dtv;
5693
5694 dtv.v_type = VAR_DICT;
5695 dtv.vval.v_dict = pt->pt_dict;
5696 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5697 }
5698
5699 for (i = 0; i < pt->pt_argc; ++i)
5700 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5701 ht_stack, list_stack);
5702 }
5703 }
5704#ifdef FEAT_JOB_CHANNEL
5705 else if (tv->v_type == VAR_JOB)
5706 {
5707 job_T *job = tv->vval.v_job;
5708 typval_T dtv;
5709
5710 if (job != NULL && job->jv_copyID != copyID)
5711 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005712 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005713 if (job->jv_channel != NULL)
5714 {
5715 dtv.v_type = VAR_CHANNEL;
5716 dtv.vval.v_channel = job->jv_channel;
5717 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5718 }
5719 if (job->jv_exit_partial != NULL)
5720 {
5721 dtv.v_type = VAR_PARTIAL;
5722 dtv.vval.v_partial = job->jv_exit_partial;
5723 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5724 }
5725 }
5726 }
5727 else if (tv->v_type == VAR_CHANNEL)
5728 {
5729 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005730 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005731 typval_T dtv;
5732 jsonq_T *jq;
5733 cbq_T *cq;
5734
5735 if (ch != NULL && ch->ch_copyID != copyID)
5736 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005737 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005738 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005739 {
5740 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5741 jq = jq->jq_next)
5742 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5743 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5744 cq = cq->cq_next)
5745 if (cq->cq_partial != NULL)
5746 {
5747 dtv.v_type = VAR_PARTIAL;
5748 dtv.vval.v_partial = cq->cq_partial;
5749 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5750 }
5751 if (ch->ch_part[part].ch_partial != NULL)
5752 {
5753 dtv.v_type = VAR_PARTIAL;
5754 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5755 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5756 }
5757 }
5758 if (ch->ch_partial != NULL)
5759 {
5760 dtv.v_type = VAR_PARTIAL;
5761 dtv.vval.v_partial = ch->ch_partial;
5762 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5763 }
5764 if (ch->ch_close_partial != NULL)
5765 {
5766 dtv.v_type = VAR_PARTIAL;
5767 dtv.vval.v_partial = ch->ch_close_partial;
5768 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5769 }
5770 }
5771 }
5772#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005773 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005774}
5775
Bram Moolenaar17a13432016-01-24 14:22:10 +01005776 static char *
5777get_var_special_name(int nr)
5778{
5779 switch (nr)
5780 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005781 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005782 case VVAL_TRUE: return "v:true";
5783 case VVAL_NONE: return "v:none";
5784 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005785 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005786 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005787 return "42";
5788}
5789
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 * Return a string with the string representation of a variable.
5792 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005793 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005794 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005795 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5796 * stings as "string()", otherwise does not put quotes around strings, as
5797 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005798 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5799 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005800 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005802 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005803echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005804 typval_T *tv,
5805 char_u **tofree,
5806 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005807 int copyID,
5808 int echo_style,
5809 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005810 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005812 static int recurse = 0;
5813 char_u *r = NULL;
5814
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005816 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005817 if (!did_echo_string_emsg)
5818 {
5819 /* Only give this message once for a recursive call to avoid
5820 * flooding the user with errors. And stop iterating over lists
5821 * and dicts. */
5822 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005823 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005825 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005826 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005827 }
5828 ++recurse;
5829
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 switch (tv->v_type)
5831 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005832 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005833 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005834 {
5835 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005836 r = tv->vval.v_string;
5837 if (r == NULL)
5838 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005839 }
5840 else
5841 {
5842 *tofree = string_quote(tv->vval.v_string, FALSE);
5843 r = *tofree;
5844 }
5845 break;
5846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005848 if (echo_style)
5849 {
5850 *tofree = NULL;
5851 r = tv->vval.v_string;
5852 }
5853 else
5854 {
5855 *tofree = string_quote(tv->vval.v_string, TRUE);
5856 r = *tofree;
5857 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005858 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005859
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005860 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005861 {
5862 partial_T *pt = tv->vval.v_partial;
5863 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005864 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005865 garray_T ga;
5866 int i;
5867 char_u *tf;
5868
5869 ga_init2(&ga, 1, 100);
5870 ga_concat(&ga, (char_u *)"function(");
5871 if (fname != NULL)
5872 {
5873 ga_concat(&ga, fname);
5874 vim_free(fname);
5875 }
5876 if (pt != NULL && pt->pt_argc > 0)
5877 {
5878 ga_concat(&ga, (char_u *)", [");
5879 for (i = 0; i < pt->pt_argc; ++i)
5880 {
5881 if (i > 0)
5882 ga_concat(&ga, (char_u *)", ");
5883 ga_concat(&ga,
5884 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5885 vim_free(tf);
5886 }
5887 ga_concat(&ga, (char_u *)"]");
5888 }
5889 if (pt != NULL && pt->pt_dict != NULL)
5890 {
5891 typval_T dtv;
5892
5893 ga_concat(&ga, (char_u *)", ");
5894 dtv.v_type = VAR_DICT;
5895 dtv.vval.v_dict = pt->pt_dict;
5896 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5897 vim_free(tf);
5898 }
5899 ga_concat(&ga, (char_u *)")");
5900
5901 *tofree = ga.ga_data;
5902 r = *tofree;
5903 break;
5904 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005905
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005906 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005907 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005908 break;
5909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005911 if (tv->vval.v_list == NULL)
5912 {
5913 *tofree = NULL;
5914 r = NULL;
5915 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005916 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5917 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005918 {
5919 *tofree = NULL;
5920 r = (char_u *)"[...]";
5921 }
5922 else
5923 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005924 int old_copyID = tv->vval.v_list->lv_copyID;
5925
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005926 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005927 *tofree = list2string(tv, copyID, restore_copyID);
5928 if (restore_copyID)
5929 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 r = *tofree;
5931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005933
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935 if (tv->vval.v_dict == NULL)
5936 {
5937 *tofree = NULL;
5938 r = NULL;
5939 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005940 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5941 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005942 {
5943 *tofree = NULL;
5944 r = (char_u *)"{...}";
5945 }
5946 else
5947 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005948 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005949 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005950 *tofree = dict2string(tv, copyID, restore_copyID);
5951 if (restore_copyID)
5952 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953 r = *tofree;
5954 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005957 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005958 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005959 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005960 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005961 break;
5962
Bram Moolenaar835dc632016-02-07 14:27:38 +01005963 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005964 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005966 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005967 if (composite_val)
5968 {
5969 *tofree = string_quote(r, FALSE);
5970 r = *tofree;
5971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 *tofree = NULL;
5977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5978 r = numbuf;
5979 break;
5980#endif
5981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005982 case VAR_SPECIAL:
5983 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005984 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987
Bram Moolenaar8502c702014-06-17 12:51:16 +02005988 if (--recurse == 0)
5989 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991}
5992
5993/*
5994 * Return a string with the string representation of a variable.
5995 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5996 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005997 * Does not put quotes around strings, as ":echo" displays values.
5998 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5999 * May return NULL.
6000 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006001 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002echo_string(
6003 typval_T *tv,
6004 char_u **tofree,
6005 char_u *numbuf,
6006 int copyID)
6007{
6008 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6009}
6010
6011/*
6012 * Return a string with the string representation of a variable.
6013 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6014 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006016 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006018 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006019tv2string(
6020 typval_T *tv,
6021 char_u **tofree,
6022 char_u *numbuf,
6023 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006024{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 * Return string "str" in ' quotes, doubling ' characters.
6030 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006031 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006032 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037 char_u *p, *r, *s;
6038
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 len = (function ? 13 : 3);
6040 if (str != NULL)
6041 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006042 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006043 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 if (*p == '\'')
6045 ++len;
6046 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047 s = r = alloc(len);
6048 if (r != NULL)
6049 {
6050 if (function)
6051 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006053 r += 10;
6054 }
6055 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006056 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 if (str != NULL)
6058 for (p = str; *p != NUL; )
6059 {
6060 if (*p == '\'')
6061 *r++ = '\'';
6062 MB_COPY_CHAR(p, r);
6063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006065 if (function)
6066 *r++ = ')';
6067 *r++ = NUL;
6068 }
6069 return s;
6070}
6071
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006072#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006073/*
6074 * Convert the string "text" to a floating point number.
6075 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6076 * this always uses a decimal point.
6077 * Returns the length of the text that was consumed.
6078 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080string2float(
6081 char_u *text,
6082 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006083{
6084 char *s = (char *)text;
6085 float_T f;
6086
Bram Moolenaar62473612017-01-08 19:25:40 +01006087 /* MS-Windows does not deal with "inf" and "nan" properly. */
6088 if (STRNICMP(text, "inf", 3) == 0)
6089 {
6090 *value = INFINITY;
6091 return 3;
6092 }
6093 if (STRNICMP(text, "-inf", 3) == 0)
6094 {
6095 *value = -INFINITY;
6096 return 4;
6097 }
6098 if (STRNICMP(text, "nan", 3) == 0)
6099 {
6100 *value = NAN;
6101 return 3;
6102 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006103 f = strtod(s, &s);
6104 *value = f;
6105 return (int)((char_u *)s - text);
6106}
6107#endif
6108
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 * Get the value of an environment variable.
6111 * "arg" is pointing to the '$'. It is advanced to after the name.
6112 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006113 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 char_u *string = NULL;
6119 int len;
6120 int cc;
6121 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006122 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
6124 ++*arg;
6125 name = *arg;
6126 len = get_env_len(arg);
6127 if (evaluate)
6128 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006129 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006130 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006131
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006132 cc = name[len];
6133 name[len] = NUL;
6134 /* first try vim_getenv(), fast for normal environment vars */
6135 string = vim_getenv(name, &mustfree);
6136 if (string != NULL && *string != NUL)
6137 {
6138 if (!mustfree)
6139 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006141 else
6142 {
6143 if (mustfree)
6144 vim_free(string);
6145
6146 /* next try expanding things like $VIM and ${HOME} */
6147 string = expand_env_save(name - 1);
6148 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006149 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006150 }
6151 name[len] = cc;
6152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153 rettv->v_type = VAR_STRING;
6154 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 }
6156
6157 return OK;
6158}
6159
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006160
6161
6162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006164 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006166 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167var2fpos(
6168 typval_T *varp,
6169 int dollar_lnum, /* TRUE when $ is last line */
6170 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006172 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006174 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaara5525202006-03-02 22:52:09 +00006176 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006177 if (varp->v_type == VAR_LIST)
6178 {
6179 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006180 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006181 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006182 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006183
6184 l = varp->vval.v_list;
6185 if (l == NULL)
6186 return NULL;
6187
6188 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006189 pos.lnum = list_find_nr(l, 0L, &error);
6190 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006191 return NULL; /* invalid line number */
6192
6193 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006194 pos.col = list_find_nr(l, 1L, &error);
6195 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006197 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006198
6199 /* We accept "$" for the column number: last column. */
6200 li = list_find(l, 1L);
6201 if (li != NULL && li->li_tv.v_type == VAR_STRING
6202 && li->li_tv.vval.v_string != NULL
6203 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6204 pos.col = len + 1;
6205
Bram Moolenaara5525202006-03-02 22:52:09 +00006206 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006207 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006208 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006209 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006210
Bram Moolenaara5525202006-03-02 22:52:09 +00006211 /* Get the virtual offset. Defaults to zero. */
6212 pos.coladd = list_find_nr(l, 2L, &error);
6213 if (error)
6214 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006215
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006216 return &pos;
6217 }
6218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006219 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006220 if (name == NULL)
6221 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006222 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006224 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6225 {
6226 if (VIsual_active)
6227 return &VIsual;
6228 return &curwin->w_cursor;
6229 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006230 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006232 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6234 return NULL;
6235 return pp;
6236 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006237
Bram Moolenaara5525202006-03-02 22:52:09 +00006238 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006239
Bram Moolenaar477933c2007-07-17 14:32:23 +00006240 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006241 {
6242 pos.col = 0;
6243 if (name[1] == '0') /* "w0": first visible line */
6244 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006245 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006246 /* In silent Ex mode topline is zero, but that's not a valid line
6247 * number; use one instead. */
6248 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006249 return &pos;
6250 }
6251 else if (name[1] == '$') /* "w$": last visible line */
6252 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006253 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006254 /* In silent Ex mode botline is zero, return zero then. */
6255 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006256 return &pos;
6257 }
6258 }
6259 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006261 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 pos.lnum = curbuf->b_ml.ml_line_count;
6264 pos.col = 0;
6265 }
6266 else
6267 {
6268 pos.lnum = curwin->w_cursor.lnum;
6269 pos.col = (colnr_T)STRLEN(ml_get_curline());
6270 }
6271 return &pos;
6272 }
6273 return NULL;
6274}
6275
6276/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006277 * Convert list in "arg" into a position and optional file number.
6278 * When "fnump" is NULL there is no file number, only 3 items.
6279 * Note that the column is passed on as-is, the caller may want to decrement
6280 * it to use 1 for the first column.
6281 * Return FAIL when conversion is not possible, doesn't check the position for
6282 * validity.
6283 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006285list2fpos(
6286 typval_T *arg,
6287 pos_T *posp,
6288 int *fnump,
6289 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006290{
6291 list_T *l = arg->vval.v_list;
6292 long i = 0;
6293 long n;
6294
Bram Moolenaar493c1782014-05-28 14:34:46 +02006295 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6296 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006297 if (arg->v_type != VAR_LIST
6298 || l == NULL
6299 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006300 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006301 return FAIL;
6302
6303 if (fnump != NULL)
6304 {
6305 n = list_find_nr(l, i++, NULL); /* fnum */
6306 if (n < 0)
6307 return FAIL;
6308 if (n == 0)
6309 n = curbuf->b_fnum; /* current buffer */
6310 *fnump = n;
6311 }
6312
6313 n = list_find_nr(l, i++, NULL); /* lnum */
6314 if (n < 0)
6315 return FAIL;
6316 posp->lnum = n;
6317
6318 n = list_find_nr(l, i++, NULL); /* col */
6319 if (n < 0)
6320 return FAIL;
6321 posp->col = n;
6322
Bram Moolenaar493c1782014-05-28 14:34:46 +02006323 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006324 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006325 posp->coladd = 0;
6326 else
6327 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006328
Bram Moolenaar493c1782014-05-28 14:34:46 +02006329 if (curswantp != NULL)
6330 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6331
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006332 return OK;
6333}
6334
6335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 * Get the length of an environment variable name.
6337 * Advance "arg" to the first character after the name.
6338 * Return 0 for error.
6339 */
6340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006341get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 char_u *p;
6344 int len;
6345
6346 for (p = *arg; vim_isIDc(*p); ++p)
6347 ;
6348 if (p == *arg) /* no name found */
6349 return 0;
6350
6351 len = (int)(p - *arg);
6352 *arg = p;
6353 return len;
6354}
6355
6356/*
6357 * Get the length of the name of a function or internal variable.
6358 * "arg" is advanced to the first non-white character after the name.
6359 * Return 0 if something is wrong.
6360 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006361 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006362get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 char_u *p;
6365 int len;
6366
6367 /* Find the end of the name. */
6368 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006369 {
6370 if (*p == ':')
6371 {
6372 /* "s:" is start of "s:var", but "n:" is not and can be used in
6373 * slice "[n:]". Also "xx:" is not a namespace. */
6374 len = (int)(p - *arg);
6375 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6376 || len > 1)
6377 break;
6378 }
6379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (p == *arg) /* no name found */
6381 return 0;
6382
6383 len = (int)(p - *arg);
6384 *arg = skipwhite(p);
6385
6386 return len;
6387}
6388
6389/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006390 * Get the length of the name of a variable or function.
6391 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006393 * Return -1 if curly braces expansion failed.
6394 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 * If the name contains 'magic' {}'s, expand them and return the
6396 * expanded name in an allocated string via 'alias' - caller must free.
6397 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006399get_name_len(
6400 char_u **arg,
6401 char_u **alias,
6402 int evaluate,
6403 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 char_u *p;
6407 char_u *expr_start;
6408 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410 *alias = NULL; /* default to no alias */
6411
6412 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6413 && (*arg)[2] == (int)KE_SNR)
6414 {
6415 /* hard coded <SNR>, already translated */
6416 *arg += 3;
6417 return get_id_len(arg) + 3;
6418 }
6419 len = eval_fname_script(*arg);
6420 if (len > 0)
6421 {
6422 /* literal "<SID>", "s:" or "<SNR>" */
6423 *arg += len;
6424 }
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006427 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006429 p = find_name_end(*arg, &expr_start, &expr_end,
6430 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (expr_start != NULL)
6432 {
6433 char_u *temp_string;
6434
6435 if (!evaluate)
6436 {
6437 len += (int)(p - *arg);
6438 *arg = skipwhite(p);
6439 return len;
6440 }
6441
6442 /*
6443 * Include any <SID> etc in the expanded string:
6444 * Thus the -len here.
6445 */
6446 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6447 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006448 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 *alias = temp_string;
6450 *arg = skipwhite(p);
6451 return (int)STRLEN(temp_string);
6452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006455 // Only give an error when there is something, otherwise it will be
6456 // reported at a higher level.
6457 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006458 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
6460 return len;
6461}
6462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006463/*
6464 * Find the end of a variable or function name, taking care of magic braces.
6465 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6466 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006467 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006468 * Return a pointer to just after the name. Equal to "arg" if there is no
6469 * valid name.
6470 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006472find_name_end(
6473 char_u *arg,
6474 char_u **expr_start,
6475 char_u **expr_end,
6476 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006478 int mb_nest = 0;
6479 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006481 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006483 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006485 *expr_start = NULL;
6486 *expr_end = NULL;
6487 }
6488
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006489 /* Quick check for valid starting character. */
6490 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6491 return arg;
6492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 for (p = arg; *p != NUL
6494 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006496 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006497 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006498 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006499 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006500 if (*p == '\'')
6501 {
6502 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006503 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006504 ;
6505 if (*p == NUL)
6506 break;
6507 }
6508 else if (*p == '"')
6509 {
6510 /* skip over "str\"ing" 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 if (*p == '\\' && p[1] != NUL)
6513 ++p;
6514 if (*p == NUL)
6515 break;
6516 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006517 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6518 {
6519 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006520 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006521 len = (int)(p - arg);
6522 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006523 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006524 break;
6525 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006527 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006529 if (*p == '[')
6530 ++br_nest;
6531 else if (*p == ']')
6532 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006537 if (*p == '{')
6538 {
6539 mb_nest++;
6540 if (expr_start != NULL && *expr_start == NULL)
6541 *expr_start = p;
6542 }
6543 else if (*p == '}')
6544 {
6545 mb_nest--;
6546 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6547 *expr_end = p;
6548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551
6552 return p;
6553}
6554
6555/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006556 * Expands out the 'magic' {}'s in a variable/function name.
6557 * Note that this can call itself recursively, to deal with
6558 * constructs like foo{bar}{baz}{bam}
6559 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6560 * "in_start" ^
6561 * "expr_start" ^
6562 * "expr_end" ^
6563 * "in_end" ^
6564 *
6565 * Returns a new allocated string, which the caller must free.
6566 * Returns NULL for failure.
6567 */
6568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569make_expanded_name(
6570 char_u *in_start,
6571 char_u *expr_start,
6572 char_u *expr_end,
6573 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006574{
6575 char_u c1;
6576 char_u *retval = NULL;
6577 char_u *temp_result;
6578 char_u *nextcmd = NULL;
6579
6580 if (expr_end == NULL || in_end == NULL)
6581 return NULL;
6582 *expr_start = NUL;
6583 *expr_end = NUL;
6584 c1 = *in_end;
6585 *in_end = NUL;
6586
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006587 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006588 if (temp_result != NULL && nextcmd == NULL)
6589 {
6590 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6591 + (in_end - expr_end) + 1));
6592 if (retval != NULL)
6593 {
6594 STRCPY(retval, in_start);
6595 STRCAT(retval, temp_result);
6596 STRCAT(retval, expr_end + 1);
6597 }
6598 }
6599 vim_free(temp_result);
6600
6601 *in_end = c1; /* put char back for error messages */
6602 *expr_start = '{';
6603 *expr_end = '}';
6604
6605 if (retval != NULL)
6606 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006607 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006608 if (expr_start != NULL)
6609 {
6610 /* Further expansion! */
6611 temp_result = make_expanded_name(retval, expr_start,
6612 expr_end, temp_result);
6613 vim_free(retval);
6614 retval = temp_result;
6615 }
6616 }
6617
6618 return retval;
6619}
6620
6621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006628 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6629}
6630
6631/*
6632 * Return TRUE if character "c" can be used as the first character in a
6633 * variable or function name (excluding '{' and '}').
6634 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006637{
6638 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
6640
6641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 * Set number v: variable to "val".
6643 */
6644 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006645set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649
6650/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006651 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006653 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006654get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006656 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006659/*
6660 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006661 * If the String variable has never been set, return an empty string.
6662 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006663 */
6664 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006666{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006667 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006668}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006671 * Get List v: variable value. Caller must take care of reference count when
6672 * needed.
6673 */
6674 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006676{
6677 return vimvars[idx].vv_list;
6678}
6679
6680/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006681 * Get Dict v: variable value. Caller must take care of reference count when
6682 * needed.
6683 */
6684 dict_T *
6685get_vim_var_dict(int idx)
6686{
6687 return vimvars[idx].vv_dict;
6688}
6689
6690/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006691 * Set v:char to character "c".
6692 */
6693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006695{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006696 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006697
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006698 if (has_mbyte)
6699 buf[(*mb_char2bytes)(c, buf)] = NUL;
6700 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006701 {
6702 buf[0] = c;
6703 buf[1] = NUL;
6704 }
6705 set_vim_var_string(VV_CHAR, buf, -1);
6706}
6707
6708/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006709 * Set v:count to "count" and v:count1 to "count1".
6710 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 */
6712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006713set_vcount(
6714 long count,
6715 long count1,
6716 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006718 if (set_prevcount)
6719 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006720 vimvars[VV_COUNT].vv_nr = count;
6721 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
6724/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006725 * Save variables that might be changed as a side effect. Used when executing
6726 * a timer callback.
6727 */
6728 void
6729save_vimvars(vimvars_save_T *vvsave)
6730{
6731 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6732 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6733 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6734}
6735
6736/*
6737 * Restore variables saved by save_vimvars().
6738 */
6739 void
6740restore_vimvars(vimvars_save_T *vvsave)
6741{
6742 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6743 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6744 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6745}
6746
6747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 * Set string v: variable to a copy of "val".
6749 */
6750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006751set_vim_var_string(
6752 int idx,
6753 char_u *val,
6754 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
Bram Moolenaara542c682016-01-31 16:28:04 +01006756 clear_tv(&vimvars[idx].vv_di.di_tv);
6757 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006761 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006763 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764}
6765
6766/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006767 * Set List v: variable to "val".
6768 */
6769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006770set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006771{
Bram Moolenaara542c682016-01-31 16:28:04 +01006772 clear_tv(&vimvars[idx].vv_di.di_tv);
6773 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006774 vimvars[idx].vv_list = val;
6775 if (val != NULL)
6776 ++val->lv_refcount;
6777}
6778
6779/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006780 * Set Dictionary v: variable to "val".
6781 */
6782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006784{
Bram Moolenaara542c682016-01-31 16:28:04 +01006785 clear_tv(&vimvars[idx].vv_di.di_tv);
6786 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006787 vimvars[idx].vv_dict = val;
6788 if (val != NULL)
6789 {
6790 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006791 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006792 }
6793}
6794
6795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 * Set v:register if needed.
6797 */
6798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006799set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 char_u regname;
6802
6803 if (c == 0 || c == ' ')
6804 regname = '"';
6805 else
6806 regname = c;
6807 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 set_vim_var_string(VV_REG, &regname, 1);
6810}
6811
6812/*
6813 * Get or set v:exception. If "oldval" == NULL, return the current value.
6814 * Otherwise, restore the value to "oldval" and return NULL.
6815 * Must always be called in pairs to save and restore v:exception! Does not
6816 * take care of memory allocations.
6817 */
6818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 return NULL;
6826}
6827
6828/*
6829 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6830 * Otherwise, restore the value to "oldval" and return NULL.
6831 * Must always be called in pairs to save and restore v:throwpoint! Does not
6832 * take care of memory allocations.
6833 */
6834 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 return NULL;
6842}
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844/*
6845 * Set v:cmdarg.
6846 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6847 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6848 * Must always be called in pairs!
6849 */
6850 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 char_u *oldval;
6854 char_u *newval;
6855 unsigned len;
6856
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006858 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006860 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006861 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006862 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006865 if (eap->force_bin == FORCE_BIN)
6866 len = 6;
6867 else if (eap->force_bin == FORCE_NOBIN)
6868 len = 8;
6869 else
6870 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006871
6872 if (eap->read_edit)
6873 len += 7;
6874
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006875 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006876 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006877 if (eap->force_enc != 0)
6878 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006879 if (eap->bad_char != 0)
6880 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006881
6882 newval = alloc(len + 1);
6883 if (newval == NULL)
6884 return NULL;
6885
6886 if (eap->force_bin == FORCE_BIN)
6887 sprintf((char *)newval, " ++bin");
6888 else if (eap->force_bin == FORCE_NOBIN)
6889 sprintf((char *)newval, " ++nobin");
6890 else
6891 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006892
6893 if (eap->read_edit)
6894 STRCAT(newval, " ++edit");
6895
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006896 if (eap->force_ff != 0)
6897 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006898 eap->force_ff == 'u' ? "unix"
6899 : eap->force_ff == 'd' ? "dos"
6900 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006901 if (eap->force_enc != 0)
6902 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6903 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006904 if (eap->bad_char == BAD_KEEP)
6905 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6906 else if (eap->bad_char == BAD_DROP)
6907 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6908 else if (eap->bad_char != 0)
6909 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006910 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006911 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
6914/*
6915 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006916 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006919get_var_tv(
6920 char_u *name,
6921 int len, /* length of "name" */
6922 typval_T *rettv, /* NULL when only checking existence */
6923 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6924 int verbose, /* may give error message */
6925 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
6927 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006928 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006929 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
6932 /* truncate the name, so that we can use strcmp() */
6933 cc = name[len];
6934 name[len] = NUL;
6935
6936 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 * Check for user-defined variables.
6938 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006939 v = find_var(name, NULL, no_autoload);
6940 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006942 tv = &v->di_tv;
6943 if (dip != NULL)
6944 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006949 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006950 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 ret = FAIL;
6952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006953 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006954 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 name[len] = cc;
6957
6958 return ret;
6959}
6960
6961/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006962 * Check if variable "name[len]" is a local variable or an argument.
6963 * If so, "*eval_lavars_used" is set to TRUE.
6964 */
6965 static void
6966check_vars(char_u *name, int len)
6967{
6968 int cc;
6969 char_u *varname;
6970 hashtab_T *ht;
6971
6972 if (eval_lavars_used == NULL)
6973 return;
6974
6975 /* truncate the name, so that we can use strcmp() */
6976 cc = name[len];
6977 name[len] = NUL;
6978
6979 ht = find_var_ht(name, &varname);
6980 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6981 {
6982 if (find_var(name, NULL, TRUE) != NULL)
6983 *eval_lavars_used = TRUE;
6984 }
6985
6986 name[len] = cc;
6987}
6988
6989/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006990 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6991 * Also handle function call with Funcref variable: func(expr)
6992 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6993 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006995handle_subscript(
6996 char_u **arg,
6997 typval_T *rettv,
6998 int evaluate, /* do more than finding the end */
6999 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007000{
7001 int ret = OK;
7002 dict_T *selfdict = NULL;
7003 char_u *s;
7004 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007006
7007 while (ret == OK
7008 && (**arg == '['
7009 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007010 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7011 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007012 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007013 {
7014 if (**arg == '(')
7015 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007016 partial_T *pt = NULL;
7017
Bram Moolenaard9fba312005-06-26 22:34:35 +00007018 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007019 if (evaluate)
7020 {
7021 functv = *rettv;
7022 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007023
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007024 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007025 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007026 {
7027 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007028 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007029 }
7030 else
7031 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007032 }
7033 else
7034 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007035 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007037 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007038
7039 /* Clear the funcref afterwards, so that deleting it while
7040 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007041 if (evaluate)
7042 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007043
7044 /* Stop the expression evaluation when immediately aborting on
7045 * error, or when an interrupt occurred or an exception was thrown
7046 * but not caught. */
7047 if (aborting())
7048 {
7049 if (ret == OK)
7050 clear_tv(rettv);
7051 ret = FAIL;
7052 }
7053 dict_unref(selfdict);
7054 selfdict = NULL;
7055 }
7056 else /* **arg == '[' || **arg == '.' */
7057 {
7058 dict_unref(selfdict);
7059 if (rettv->v_type == VAR_DICT)
7060 {
7061 selfdict = rettv->vval.v_dict;
7062 if (selfdict != NULL)
7063 ++selfdict->dv_refcount;
7064 }
7065 else
7066 selfdict = NULL;
7067 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7068 {
7069 clear_tv(rettv);
7070 ret = FAIL;
7071 }
7072 }
7073 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007074
Bram Moolenaar1d429612016-05-24 15:44:17 +02007075 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7076 * Don't do this when "Func" is already a partial that was bound
7077 * explicitly (pt_auto is FALSE). */
7078 if (selfdict != NULL
7079 && (rettv->v_type == VAR_FUNC
7080 || (rettv->v_type == VAR_PARTIAL
7081 && (rettv->vval.v_partial->pt_auto
7082 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007083 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007084
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007085 dict_unref(selfdict);
7086 return ret;
7087}
7088
7089/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007090 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007091 * value).
7092 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007093 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007094alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007095{
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007097}
7098
7099/*
7100 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 * The string "s" must have been allocated, it is consumed.
7102 * Return NULL for out of memory, the variable otherwise.
7103 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007109 rettv = alloc_tv();
7110 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007112 rettv->v_type = VAR_STRING;
7113 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
7115 else
7116 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007117 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119
7120/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007124free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 if (varp != NULL)
7127 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007128 switch (varp->v_type)
7129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007130 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007131 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007132 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007133 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007134 vim_free(varp->vval.v_string);
7135 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007136 case VAR_PARTIAL:
7137 partial_unref(varp->vval.v_partial);
7138 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007139 case VAR_BLOB:
7140 blob_unref(varp->vval.v_blob);
7141 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007142 case VAR_LIST:
7143 list_unref(varp->vval.v_list);
7144 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007145 case VAR_DICT:
7146 dict_unref(varp->vval.v_dict);
7147 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007148 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007149#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007150 job_unref(varp->vval.v_job);
7151 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007152#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007153 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007154#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007155 channel_unref(varp->vval.v_channel);
7156 break;
7157#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007158 case VAR_NUMBER:
7159 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007160 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007161 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007162 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 vim_free(varp);
7165 }
7166}
7167
7168/*
7169 * Free the memory for a variable value and set the value to NULL or 0.
7170 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 if (varp != NULL)
7175 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007176 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007178 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007179 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007180 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007181 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007182 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007183 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007184 case VAR_PARTIAL:
7185 partial_unref(varp->vval.v_partial);
7186 varp->vval.v_partial = NULL;
7187 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007188 case VAR_BLOB:
7189 blob_unref(varp->vval.v_blob);
7190 varp->vval.v_blob = NULL;
7191 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 case VAR_LIST:
7193 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007194 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007195 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196 case VAR_DICT:
7197 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007200 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007201 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007202 varp->vval.v_number = 0;
7203 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007204 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007205#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007206 varp->vval.v_float = 0.0;
7207 break;
7208#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007209 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007210#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007211 job_unref(varp->vval.v_job);
7212 varp->vval.v_job = NULL;
7213#endif
7214 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007215 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007216#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007217 channel_unref(varp->vval.v_channel);
7218 varp->vval.v_channel = NULL;
7219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007220 case VAR_UNKNOWN:
7221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007223 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
7225}
7226
7227/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007228 * Set the value of a variable to NULL without freeing items.
7229 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007232{
7233 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007235}
7236
7237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 * Get the number value of a variable.
7239 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007240 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007241 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007242 * caller of incompatible types: it sets *denote to TRUE if "denote"
7243 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007245 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007246tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007248 int error = FALSE;
7249
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007250 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007251}
7252
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007253 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007254tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007255{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007256 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007258 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007260 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007261 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007262 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007263#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007264 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007265 break;
7266#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007267 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007268 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007269 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007270 break;
7271 case VAR_STRING:
7272 if (varp->vval.v_string != NULL)
7273 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007274 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007275 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007276 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007277 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007278 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007279 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007280 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007281 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007282 case VAR_SPECIAL:
7283 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7284 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007285 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007286#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007287 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007288 break;
7289#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007290 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007291#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007292 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007293 break;
7294#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007295 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007296 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007297 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007298 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007302 if (denote == NULL) /* useful for values that must be unsigned */
7303 n = -1;
7304 else
7305 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
7308
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007309#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007311tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007312{
7313 switch (varp->v_type)
7314 {
7315 case VAR_NUMBER:
7316 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007317 case VAR_FLOAT:
7318 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007319 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007320 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007321 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007322 break;
7323 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007324 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007325 break;
7326 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007327 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007328 break;
7329 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007330 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007331 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007332 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007333 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007334 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007335 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007336# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007337 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007338 break;
7339# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007340 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007341# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007342 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007343 break;
7344# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007345 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007347 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007348 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007349 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007350 break;
7351 }
7352 return 0;
7353}
7354#endif
7355
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * Get the string value of a variable.
7358 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7360 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 * If the String variable has never been set, return an empty string.
7362 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007363 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007364 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007366 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007367tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369 static char_u mybuf[NUMBUFLEN];
7370
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007371 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372}
7373
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007374 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007375tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007377 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378
7379 return res != NULL ? res : (char_u *)"";
7380}
7381
Bram Moolenaar7d647822014-04-05 21:28:56 +02007382/*
7383 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7384 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007385 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007386tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007387{
7388 static char_u mybuf[NUMBUFLEN];
7389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007390 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007391}
7392
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007393 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007394tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007395{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007398 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007399 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007400 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 return buf;
7402 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007403 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007404 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 break;
7406 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007407 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 break;
7409 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007410 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007412 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007413#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007414 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007415 break;
7416#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007417 case VAR_STRING:
7418 if (varp->vval.v_string != NULL)
7419 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007420 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007421 case VAR_SPECIAL:
7422 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7423 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007424 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007425 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007426 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007427 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007429 {
7430 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007431 char *status;
7432
7433 if (job == NULL)
7434 return (char_u *)"no process";
7435 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007436 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007437 : "run";
7438# ifdef UNIX
7439 vim_snprintf((char *)buf, NUMBUFLEN,
7440 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007441# elif defined(WIN32)
7442 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007443 "process %ld %s",
7444 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007445 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007446# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007447 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007448 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7449# endif
7450 return buf;
7451 }
7452#endif
7453 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007454 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007455#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007456 {
7457 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007458 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007459
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007460 if (channel == NULL)
7461 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7462 else
7463 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007464 "channel %d %s", channel->ch_id, status);
7465 return buf;
7466 }
7467#endif
7468 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007469 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007470 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007473 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474}
7475
7476/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007477 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7478 * string() on Dict, List, etc.
7479 */
7480 char_u *
7481tv_stringify(typval_T *varp, char_u *buf)
7482{
7483 if (varp->v_type == VAR_LIST
7484 || varp->v_type == VAR_DICT
7485 || varp->v_type == VAR_FUNC
7486 || varp->v_type == VAR_PARTIAL
7487 || varp->v_type == VAR_FLOAT)
7488 {
7489 typval_T tmp;
7490
7491 f_string(varp, &tmp);
7492 tv_get_string_buf(&tmp, buf);
7493 clear_tv(varp);
7494 *varp = tmp;
7495 return tmp.vval.v_string;
7496 }
7497 return tv_get_string_buf(varp, buf);
7498}
7499
7500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 * Find variable "name" in the list of variables.
7502 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007504 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007507 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007508find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007512 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaara7043832005-01-21 11:56:39 +00007514 ht = find_var_ht(name, &varname);
7515 if (htp != NULL)
7516 *htp = ht;
7517 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007519 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7520 if (ret != NULL)
7521 return ret;
7522
7523 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007524 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525}
7526
7527/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007528 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007529 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532find_var_in_ht(
7533 hashtab_T *ht,
7534 int htname,
7535 char_u *varname,
7536 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007537{
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 hashitem_T *hi;
7539
7540 if (*varname == NUL)
7541 {
7542 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007543 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007545 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 case 'g': return &globvars_var;
7547 case 'v': return &vimvars_var;
7548 case 'b': return &curbuf->b_bufvar;
7549 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007550 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007551 case 'l': return get_funccal_local_var();
7552 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 }
7554 return NULL;
7555 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007556
7557 hi = hash_find(ht, varname);
7558 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007559 {
7560 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007561 * worked find the variable again. Don't auto-load a script if it was
7562 * loaded already, otherwise it would be loaded every time when
7563 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007564 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007565 {
7566 /* Note: script_autoload() may make "hi" invalid. It must either
7567 * be obtained again or not used. */
7568 if (!script_autoload(varname, FALSE) || aborting())
7569 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007570 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007571 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007572 if (HASHITEM_EMPTY(hi))
7573 return NULL;
7574 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007576}
7577
7578/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007580 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007581 * Set "varname" to the start of name without ':'.
7582 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007583 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007586 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007587 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007588
Bram Moolenaar73627d02015-08-11 15:46:09 +02007589 if (name[0] == NUL)
7590 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (name[1] != ':')
7592 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007593 /* The name must not start with a colon or #. */
7594 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 return NULL;
7596 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007597
7598 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007599 hi = hash_find(&compat_hashtab, name);
7600 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007601 return &compat_hashtab;
7602
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007603 ht = get_funccal_local_ht();
7604 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007606 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
7608 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007609 if (*name == 'g') /* global variable */
7610 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007611 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7612 */
7613 if (vim_strchr(name + 2, ':') != NULL
7614 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007615 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007617 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007619 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007620 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007621 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 if (*name == 'v') /* v: variable */
7623 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007624 if (*name == 'a') /* a: function argument */
7625 return get_funccal_args_ht();
7626 if (*name == 'l') /* l: local function variable */
7627 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007629 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7630 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 return NULL;
7632}
7633
7634/*
7635 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007636 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 * Returns NULL when it doesn't exist.
7638 */
7639 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007640get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007644 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (v == NULL)
7646 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007647 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 * sourcing this script and when executing functions defined in the script.
7653 */
7654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
Bram Moolenaara7043832005-01-21 11:56:39 +00007657 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 hashtab_T *ht;
7659 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7662 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007663 /* Re-allocating ga_data means that an ht_array pointing to
7664 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007666 for (i = 1; i <= ga_scripts.ga_len; ++i)
7667 {
7668 ht = &SCRIPT_VARS(i);
7669 if (ht->ht_mask == HT_INIT_SIZE - 1)
7670 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007671 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007673 }
7674
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 while (ga_scripts.ga_len < id)
7676 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007677 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007678 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007679 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 }
7683}
7684
7685/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7687 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 */
7689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007690init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007693 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007694 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007695 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007696 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 dict_var->di_tv.vval.v_dict = dict;
7698 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7701 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702}
7703
7704/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007705 * Unreference a dictionary initialized by init_var_dict().
7706 */
7707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007708unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007709{
7710 /* Now the dict needs to be freed if no one else is using it, go back to
7711 * normal reference counting. */
7712 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7713 dict_unref(dict);
7714}
7715
7716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 * Frees all allocated variables and the value they contain.
7719 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 */
7721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007723{
7724 vars_clear_ext(ht, TRUE);
7725}
7726
7727/*
7728 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7729 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
Bram Moolenaara7043832005-01-21 11:56:39 +00007733 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 hashitem_T *hi;
7735 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007738 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007739 for (hi = ht->ht_array; todo > 0; ++hi)
7740 {
7741 if (!HASHITEM_EMPTY(hi))
7742 {
7743 --todo;
7744
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007746 * ht_array might change then. hash_clear() takes care of it
7747 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007748 v = HI2DI(hi);
7749 if (free_val)
7750 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007751 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007753 }
7754 }
7755 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007756 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757}
7758
Bram Moolenaara7043832005-01-21 11:56:39 +00007759/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 * Delete a variable from hashtab "ht" at item "hi".
7761 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007767
7768 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007769 clear_tv(&di->di_tv);
7770 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773/*
7774 * List the value of one internal variable.
7775 */
7776 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007777list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 char_u *tofree;
7780 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007781 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007783 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007785 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007786 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007790list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007791 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007792 char_u *name,
7793 int type,
7794 char_u *string,
7795 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
Bram Moolenaar31859182007-08-14 20:41:13 +00007797 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7798 msg_start();
7799 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007801 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 msg_putchar(' ');
7803 msg_advance(22);
7804 if (type == VAR_NUMBER)
7805 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007806 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 msg_putchar('*');
7808 else if (type == VAR_LIST)
7809 {
7810 msg_putchar('[');
7811 if (*string == '[')
7812 ++string;
7813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814 else if (type == VAR_DICT)
7815 {
7816 msg_putchar('{');
7817 if (*string == '{')
7818 ++string;
7819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 else
7821 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007824
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007825 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007826 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007827 if (*first)
7828 {
7829 msg_clr_eos();
7830 *first = FALSE;
7831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832}
7833
7834/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 * If the variable already exists, the value is updated.
7837 * Otherwise the variable is created.
7838 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007840set_var(
7841 char_u *name,
7842 typval_T *tv,
7843 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007849 ht = find_var_ht(name, &varname);
7850 if (ht == NULL || *varname == NUL)
7851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007852 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007853 return;
7854 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007855 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007856
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007857 /* Search in parent scope which is possible to reference from lambda */
7858 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007859 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007860
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007861 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7862 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007863 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007864
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007868 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007869 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007871
7872 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007873 * Handle setting internal v: variables separately where needed to
7874 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 */
7876 if (ht == &vimvarht)
7877 {
7878 if (v->di_tv.v_type == VAR_STRING)
7879 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007880 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007882 {
7883 char_u *val = tv_get_string(tv);
7884
7885 // Careful: when assigning to v:errmsg and tv_get_string()
7886 // causes an error message the variable will alrady be set.
7887 if (v->di_tv.vval.v_string == NULL)
7888 v->di_tv.vval.v_string = vim_strsave(val);
7889 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 else
7891 {
7892 /* Take over the string to avoid an extra alloc/free. */
7893 v->di_tv.vval.v_string = tv->vval.v_string;
7894 tv->vval.v_string = NULL;
7895 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007896 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007898 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007900 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901 if (STRCMP(varname, "searchforward") == 0)
7902 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007903#ifdef FEAT_SEARCH_EXTRA
7904 else if (STRCMP(varname, "hlsearch") == 0)
7905 {
7906 no_hlsearch = !v->di_tv.vval.v_number;
7907 redraw_all_later(SOME_VALID);
7908 }
7909#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007910 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007912 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007914 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007915 return;
7916 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 }
7918
7919 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921 else /* add a new variable */
7922 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01007923 // Can't add "v:" or "a:" variable.
7924 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007926 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007927 return;
7928 }
7929
Bram Moolenaar31b81602019-02-10 22:14:27 +01007930 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007931 if (!valid_varname(varname))
7932 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007933
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007934 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7935 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007936 if (v == NULL)
7937 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007941 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 return;
7943 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007944 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007946
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007949 else
7950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007952 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955}
7956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007957/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007958 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 * Also give an error message.
7960 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007962var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007963{
7964 if (flags & DI_FLAGS_RO)
7965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007966 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 return TRUE;
7968 }
7969 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007971 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 return TRUE;
7973 }
7974 return FALSE;
7975}
7976
7977/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007978 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7979 * Also give an error message.
7980 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007982var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007983{
7984 if (flags & DI_FLAGS_FIX)
7985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007986 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007987 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007988 return TRUE;
7989 }
7990 return FALSE;
7991}
7992
7993/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007994 * Check if a funcref is assigned to a valid variable name.
7995 * Return TRUE and give an error if not.
7996 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007998var_check_func_name(
7999 char_u *name, /* points to start of variable name */
8000 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008001{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008002 /* Allow for w: b: s: and t:. */
8003 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008004 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8005 ? name[2] : name[0]))
8006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008008 name);
8009 return TRUE;
8010 }
8011 /* Don't allow hiding a function. When "v" is not NULL we might be
8012 * assigning another function to the same var, the type is checked
8013 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008014 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008016 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008017 name);
8018 return TRUE;
8019 }
8020 return FALSE;
8021}
8022
8023/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008024 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008025 * Also give an error message, using "name" or _("name") when use_gettext is
8026 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008027 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008028 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008029var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008030{
8031 if (lock & VAR_LOCKED)
8032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008033 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008034 name == NULL ? (char_u *)_("Unknown")
8035 : use_gettext ? (char_u *)_(name)
8036 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008037 return TRUE;
8038 }
8039 if (lock & VAR_FIXED)
8040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008041 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008042 name == NULL ? (char_u *)_("Unknown")
8043 : use_gettext ? (char_u *)_(name)
8044 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008045 return TRUE;
8046 }
8047 return FALSE;
8048}
8049
8050/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008051 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8052 * Also give an error message, using "name" or _("name") when use_gettext is
8053 * TRUE.
8054 */
8055 static int
8056tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8057{
8058 int lock = 0;
8059
8060 switch (tv->v_type)
8061 {
8062 case VAR_BLOB:
8063 if (tv->vval.v_blob != NULL)
8064 lock = tv->vval.v_blob->bv_lock;
8065 break;
8066 case VAR_LIST:
8067 if (tv->vval.v_list != NULL)
8068 lock = tv->vval.v_list->lv_lock;
8069 break;
8070 case VAR_DICT:
8071 if (tv->vval.v_dict != NULL)
8072 lock = tv->vval.v_dict->dv_lock;
8073 break;
8074 default:
8075 break;
8076 }
8077 return var_check_lock(tv->v_lock, name, use_gettext)
8078 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8079}
8080
8081/*
8082 * Check if a variable name is valid.
8083 * Return FALSE and give an error if not.
8084 */
8085 int
8086valid_varname(char_u *varname)
8087{
8088 char_u *p;
8089
8090 for (p = varname; *p != NUL; ++p)
8091 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8092 && *p != AUTOLOAD_CHAR)
8093 {
8094 semsg(_(e_illvar), varname);
8095 return FALSE;
8096 }
8097 return TRUE;
8098}
8099
8100/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008103 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008104 * It is OK for "from" and "to" to point to the same item. This is used to
8105 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008106 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008108copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008110 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008111 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112 switch (from->v_type)
8113 {
8114 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008115 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 to->vval.v_number = from->vval.v_number;
8117 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008118 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008119#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120 to->vval.v_float = from->vval.v_float;
8121 break;
8122#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008123 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008124#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008125 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008126 if (to->vval.v_job != NULL)
8127 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008128 break;
8129#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008130 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008132 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008133 if (to->vval.v_channel != NULL)
8134 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008135 break;
8136#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137 case VAR_STRING:
8138 case VAR_FUNC:
8139 if (from->vval.v_string == NULL)
8140 to->vval.v_string = NULL;
8141 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008144 if (from->v_type == VAR_FUNC)
8145 func_ref(to->vval.v_string);
8146 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008148 case VAR_PARTIAL:
8149 if (from->vval.v_partial == NULL)
8150 to->vval.v_partial = NULL;
8151 else
8152 {
8153 to->vval.v_partial = from->vval.v_partial;
8154 ++to->vval.v_partial->pt_refcount;
8155 }
8156 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008157 case VAR_BLOB:
8158 if (from->vval.v_blob == NULL)
8159 to->vval.v_blob = NULL;
8160 else
8161 {
8162 to->vval.v_blob = from->vval.v_blob;
8163 ++to->vval.v_blob->bv_refcount;
8164 }
8165 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008166 case VAR_LIST:
8167 if (from->vval.v_list == NULL)
8168 to->vval.v_list = NULL;
8169 else
8170 {
8171 to->vval.v_list = from->vval.v_list;
8172 ++to->vval.v_list->lv_refcount;
8173 }
8174 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008175 case VAR_DICT:
8176 if (from->vval.v_dict == NULL)
8177 to->vval.v_dict = NULL;
8178 else
8179 {
8180 to->vval.v_dict = from->vval.v_dict;
8181 ++to->vval.v_dict->dv_refcount;
8182 }
8183 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008184 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008185 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186 break;
8187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188}
8189
8190/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008191 * Make a copy of an item.
8192 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008193 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8194 * reference to an already copied list/dict can be used.
8195 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008198item_copy(
8199 typval_T *from,
8200 typval_T *to,
8201 int deep,
8202 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203{
8204 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008205 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008206
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008209 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008210 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008211 }
8212 ++recurse;
8213
8214 switch (from->v_type)
8215 {
8216 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008217 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 case VAR_STRING:
8219 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008220 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008221 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008222 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008223 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008224 copy_tv(from, to);
8225 break;
8226 case VAR_LIST:
8227 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008228 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008229 if (from->vval.v_list == NULL)
8230 to->vval.v_list = NULL;
8231 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8232 {
8233 /* use the copy made earlier */
8234 to->vval.v_list = from->vval.v_list->lv_copylist;
8235 ++to->vval.v_list->lv_refcount;
8236 }
8237 else
8238 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8239 if (to->vval.v_list == NULL)
8240 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008242 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008243 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008244 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008245 case VAR_DICT:
8246 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008247 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008248 if (from->vval.v_dict == NULL)
8249 to->vval.v_dict = NULL;
8250 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8251 {
8252 /* use the copy made earlier */
8253 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8254 ++to->vval.v_dict->dv_refcount;
8255 }
8256 else
8257 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8258 if (to->vval.v_dict == NULL)
8259 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008261 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008262 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008263 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 }
8265 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008266 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008267}
8268
8269/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 * This function is used by f_input() and f_inputdialog() functions. The third
8271 * argument to f_input() specifies the type of completion to use at the
8272 * prompt. The third argument to f_inputdialog() specifies the value to return
8273 * when the user cancels the prompt.
8274 */
8275 void
8276get_user_input(
8277 typval_T *argvars,
8278 typval_T *rettv,
8279 int inputdialog,
8280 int secret)
8281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008282 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 char_u *p = NULL;
8284 int c;
8285 char_u buf[NUMBUFLEN];
8286 int cmd_silent_save = cmd_silent;
8287 char_u *defstr = (char_u *)"";
8288 int xp_type = EXPAND_NOTHING;
8289 char_u *xp_arg = NULL;
8290
8291 rettv->v_type = VAR_STRING;
8292 rettv->vval.v_string = NULL;
8293
8294#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008295 /* While starting up, there is no place to enter text. When running tests
8296 * with --not-a-term we assume feedkeys() will be used. */
8297 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 return;
8299#endif
8300
8301 cmd_silent = FALSE; /* Want to see the prompt. */
8302 if (prompt != NULL)
8303 {
8304 /* Only the part of the message after the last NL is considered as
8305 * prompt for the command line */
8306 p = vim_strrchr(prompt, '\n');
8307 if (p == NULL)
8308 p = prompt;
8309 else
8310 {
8311 ++p;
8312 c = *p;
8313 *p = NUL;
8314 msg_start();
8315 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008316 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 msg_didout = FALSE;
8318 msg_starthere();
8319 *p = c;
8320 }
8321 cmdline_row = msg_row;
8322
8323 if (argvars[1].v_type != VAR_UNKNOWN)
8324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008325 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (defstr != NULL)
8327 stuffReadbuffSpec(defstr);
8328
8329 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8330 {
8331 char_u *xp_name;
8332 int xp_namelen;
8333 long argt;
8334
8335 /* input() with a third argument: completion */
8336 rettv->vval.v_string = NULL;
8337
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008338 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 if (xp_name == NULL)
8340 return;
8341
8342 xp_namelen = (int)STRLEN(xp_name);
8343
8344 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8345 &xp_arg) == FAIL)
8346 return;
8347 }
8348 }
8349
8350 if (defstr != NULL)
8351 {
8352 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 ex_normal_busy = 0;
8355 rettv->vval.v_string =
8356 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8357 xp_type, xp_arg);
8358 ex_normal_busy = save_ex_normal_busy;
8359 }
8360 if (inputdialog && rettv->vval.v_string == NULL
8361 && argvars[1].v_type != VAR_UNKNOWN
8362 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008363 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 &argvars[2], buf));
8365
8366 vim_free(xp_arg);
8367
8368 /* since the user typed this, no need to wait for return */
8369 need_wait_return = FALSE;
8370 msg_didout = FALSE;
8371 }
8372 cmd_silent = cmd_silent_save;
8373}
8374
8375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * ":echo expr1 ..." print each argument separated with a space, add a
8377 * newline at the end.
8378 * ":echon expr1 ..." print each argument plain.
8379 */
8380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008381ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382{
8383 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008385 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 char_u *p;
8387 int needclr = TRUE;
8388 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008389 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008390 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008391 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
8393 if (eap->skip)
8394 ++emsg_skip;
8395 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8396 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008397 /* If eval1() causes an error message the text from the command may
8398 * still need to be cleared. E.g., "echo 22,44". */
8399 need_clr_eos = needclr;
8400
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008402 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {
8404 /*
8405 * Report the invalid expression unless the expression evaluation
8406 * has been cancelled due to an aborting error, an interrupt, or an
8407 * exception.
8408 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008409 if (!aborting() && did_emsg == did_emsg_before
8410 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008411 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008412 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 break;
8414 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008415 need_clr_eos = FALSE;
8416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (!eap->skip)
8418 {
8419 if (atstart)
8420 {
8421 atstart = FALSE;
8422 /* Call msg_start() after eval1(), evaluating the expression
8423 * may cause a message to appear. */
8424 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008425 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008426 /* Mark the saved text as finishing the line, so that what
8427 * follows is displayed on a new line when scrolling back
8428 * at the more prompt. */
8429 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 }
8433 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008434 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008435 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008436 if (p != NULL)
8437 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008441 if (*p != TAB && needclr)
8442 {
8443 /* remove any text still there from the command */
8444 msg_clr_eos();
8445 needclr = FALSE;
8446 }
8447 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008450 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008451 if (has_mbyte)
8452 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008453 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008454
8455 (void)msg_outtrans_len_attr(p, i, echo_attr);
8456 p += i - 1;
8457 }
8458 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008459 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008462 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 arg = skipwhite(arg);
8466 }
8467 eap->nextcmd = check_nextcmd(arg);
8468
8469 if (eap->skip)
8470 --emsg_skip;
8471 else
8472 {
8473 /* remove text that may still be there from the command */
8474 if (needclr)
8475 msg_clr_eos();
8476 if (eap->cmdidx == CMD_echo)
8477 msg_end();
8478 }
8479}
8480
8481/*
8482 * ":echohl {name}".
8483 */
8484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008485ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008487 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488}
8489
8490/*
8491 * ":execute expr1 ..." execute the result of an expression.
8492 * ":echomsg expr1 ..." Print a message
8493 * ":echoerr expr1 ..." Print an error
8494 * Each gets spaces around each argument and a newline at the end for
8495 * echo commands
8496 */
8497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008498ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 int ret = OK;
8503 char_u *p;
8504 garray_T ga;
8505 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008506 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507
8508 ga_init2(&ga, 1, 80);
8509
8510 if (eap->skip)
8511 ++emsg_skip;
8512 while (*arg != NUL && *arg != '|' && *arg != '\n')
8513 {
8514 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008515 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8516 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518
8519 if (!eap->skip)
8520 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008521 char_u buf[NUMBUFLEN];
8522
8523 if (eap->cmdidx == CMD_execute)
8524 p = tv_get_string_buf(&rettv, buf);
8525 else
8526 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 len = (int)STRLEN(p);
8528 if (ga_grow(&ga, len + 2) == FAIL)
8529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 ret = FAIL;
8532 break;
8533 }
8534 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 ga.ga_len += len;
8538 }
8539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 arg = skipwhite(arg);
8542 }
8543
8544 if (ret != FAIL && ga.ga_data != NULL)
8545 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008546 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8547 {
8548 /* Mark the already saved text as finishing the line, so that what
8549 * follows is displayed on a new line when scrolling back at the
8550 * more prompt. */
8551 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008552 }
8553
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008555 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008556 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008557 out_flush();
8558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 else if (eap->cmdidx == CMD_echoerr)
8560 {
8561 /* We don't want to abort following commands, restore did_emsg. */
8562 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008563 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (!force_abort)
8565 did_emsg = save_did_emsg;
8566 }
8567 else if (eap->cmdidx == CMD_execute)
8568 do_cmdline((char_u *)ga.ga_data,
8569 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8570 }
8571
8572 ga_clear(&ga);
8573
8574 if (eap->skip)
8575 --emsg_skip;
8576
8577 eap->nextcmd = check_nextcmd(arg);
8578}
8579
8580/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 * Find window specified by "vp" in tabpage "tp".
8582 */
8583 win_T *
8584find_win_by_nr(
8585 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008586 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008589 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 if (nr < 0)
8592 return NULL;
8593 if (nr == 0)
8594 return curwin;
8595
Bram Moolenaar29323592016-07-24 22:04:11 +02008596 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8597 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 if (nr >= LOWEST_WIN_ID)
8599 {
8600 if (wp->w_id == nr)
8601 return wp;
8602 }
8603 else if (--nr <= 0)
8604 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008605 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 if (nr >= LOWEST_WIN_ID)
8607 return NULL;
8608 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609}
8610
8611/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008612 * Find a window: When using a Window ID in any tab page, when using a number
8613 * in the current tab page.
8614 */
8615 win_T *
8616find_win_by_nr_or_id(typval_T *vp)
8617{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008618 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008619
8620 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008621 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008622 return find_win_by_nr(vp, NULL);
8623}
8624
8625/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008626 * Find window specified by "wvp" in tabpage "tvp".
8627 */
8628 win_T *
8629find_tabwin(
8630 typval_T *wvp, /* VAR_UNKNOWN for current window */
8631 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8632{
8633 win_T *wp = NULL;
8634 tabpage_T *tp = NULL;
8635 long n;
8636
8637 if (wvp->v_type != VAR_UNKNOWN)
8638 {
8639 if (tvp->v_type != VAR_UNKNOWN)
8640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008641 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 if (n >= 0)
8643 tp = find_tabpage(n);
8644 }
8645 else
8646 tp = curtab;
8647
8648 if (tp != NULL)
8649 wp = find_win_by_nr(wvp, tp);
8650 }
8651 else
8652 wp = curwin;
8653
8654 return wp;
8655}
8656
8657/*
8658 * getwinvar() and gettabwinvar()
8659 */
8660 void
8661getwinvar(
8662 typval_T *argvars,
8663 typval_T *rettv,
8664 int off) /* 1 for gettabwinvar() */
8665{
8666 win_T *win;
8667 char_u *varname;
8668 dictitem_T *v;
8669 tabpage_T *tp = NULL;
8670 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 win_T *oldcurwin;
8672 tabpage_T *oldtabpage;
8673 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008676 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 else
8678 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008680 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 ++emsg_off;
8682
8683 rettv->v_type = VAR_STRING;
8684 rettv->vval.v_string = NULL;
8685
8686 if (win != NULL && varname != NULL)
8687 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 /* Set curwin to be our win, temporarily. Also set the tabpage,
8689 * otherwise the window is not valid. Only do this when needed,
8690 * autocommands get blocked. */
8691 need_switch_win = !(tp == curtab && win == curwin);
8692 if (!need_switch_win
8693 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008695 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008697 if (varname[1] == NUL)
8698 {
8699 /* get all window-local options in a dict */
8700 dict_T *opts = get_winbuf_options(FALSE);
8701
8702 if (opts != NULL)
8703 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008704 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008705 done = TRUE;
8706 }
8707 }
8708 else if (get_option_tv(&varname, rettv, 1) == OK)
8709 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 done = TRUE;
8711 }
8712 else
8713 {
8714 /* Look up the variable. */
8715 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8716 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8717 varname, FALSE);
8718 if (v != NULL)
8719 {
8720 copy_tv(&v->di_tv, rettv);
8721 done = TRUE;
8722 }
8723 }
8724 }
8725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 if (need_switch_win)
8727 /* restore previous notion of curwin */
8728 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 }
8730
8731 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8732 /* use the default return value */
8733 copy_tv(&argvars[off + 2], rettv);
8734
8735 --emsg_off;
8736}
8737
8738/*
8739 * "setwinvar()" and "settabwinvar()" functions
8740 */
8741 void
8742setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8743{
8744 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 win_T *save_curwin;
8746 tabpage_T *save_curtab;
8747 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748 char_u *varname, *winvarname;
8749 typval_T *varp;
8750 char_u nbuf[NUMBUFLEN];
8751 tabpage_T *tp = NULL;
8752
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008753 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 return;
8755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 else
8759 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008761 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 varp = &argvars[off + 2];
8763
8764 if (win != NULL && varname != NULL && varp != NULL)
8765 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 need_switch_win = !(tp == curtab && win == curwin);
8767 if (!need_switch_win
8768 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769 {
8770 if (*varname == '&')
8771 {
8772 long numval;
8773 char_u *strval;
8774 int error = FALSE;
8775
8776 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008777 numval = (long)tv_get_number_chk(varp, &error);
8778 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 if (!error && strval != NULL)
8780 set_option_value(varname, numval, strval, OPT_LOCAL);
8781 }
8782 else
8783 {
8784 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8785 if (winvarname != NULL)
8786 {
8787 STRCPY(winvarname, "w:");
8788 STRCPY(winvarname + 2, varname);
8789 set_var(winvarname, varp, TRUE);
8790 vim_free(winvarname);
8791 }
8792 }
8793 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 if (need_switch_win)
8795 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 }
8797}
8798
8799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8801 * "arg" points to the "&" or '+' when called, to "option" when returning.
8802 * Returns NULL when no option name found. Otherwise pointer to the char
8803 * after the option name.
8804 */
8805 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008806find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
8808 char_u *p = *arg;
8809
8810 ++p;
8811 if (*p == 'g' && p[1] == ':')
8812 {
8813 *opt_flags = OPT_GLOBAL;
8814 p += 2;
8815 }
8816 else if (*p == 'l' && p[1] == ':')
8817 {
8818 *opt_flags = OPT_LOCAL;
8819 p += 2;
8820 }
8821 else
8822 *opt_flags = 0;
8823
8824 if (!ASCII_ISALPHA(*p))
8825 return NULL;
8826 *arg = p;
8827
8828 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8829 p += 4; /* termcap option */
8830 else
8831 while (ASCII_ISALPHA(*p))
8832 ++p;
8833 return p;
8834}
8835
8836/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008837 * Return the autoload script name for a function or variable name.
8838 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008840 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008841autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008842{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008843 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008844 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008845
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008846 /* Get the script file name: replace '#' with '/', append ".vim". */
8847 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8848 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008849 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008850 STRCPY(scriptname, "autoload/");
8851 STRCAT(scriptname, name);
8852 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8853 STRCAT(scriptname, ".vim");
8854 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8855 *p = '/';
8856 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008857}
8858
8859/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008860 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008861 * Return TRUE if a package was loaded.
8862 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008864script_autoload(
8865 char_u *name,
8866 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008867{
8868 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008869 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008870 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008871 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008872
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008873 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008874 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008875 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008876 return FALSE;
8877
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008878 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008879
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008880 /* Find the name in the list of previously loaded package names. Skip
8881 * "autoload/", it's always the same. */
8882 for (i = 0; i < ga_loaded.ga_len; ++i)
8883 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8884 break;
8885 if (!reload && i < ga_loaded.ga_len)
8886 ret = FALSE; /* was loaded already */
8887 else
8888 {
8889 /* Remember the name if it wasn't loaded already. */
8890 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8891 {
8892 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8893 tofree = NULL;
8894 }
8895
8896 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008897 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008898 ret = TRUE;
8899 }
8900
8901 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008902 return ret;
8903}
8904
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8906typedef enum
8907{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008908 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8909 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8910 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911} var_flavour_T;
8912
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008914var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 char_u *p = varname;
8917
8918 if (ASCII_ISUPPER(*p))
8919 {
8920 while (*(++p))
8921 if (ASCII_ISLOWER(*p))
8922 return VAR_FLAVOUR_SESSION;
8923 return VAR_FLAVOUR_VIMINFO;
8924 }
8925 else
8926 return VAR_FLAVOUR_DEFAULT;
8927}
8928#endif
8929
8930#if defined(FEAT_VIMINFO) || defined(PROTO)
8931/*
8932 * Restore global vars that start with a capital from the viminfo file
8933 */
8934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008935read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008940 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
8942 if (!writing && (find_viminfo_parameter('!') != NULL))
8943 {
8944 tab = vim_strchr(virp->vir_line + 1, '\t');
8945 if (tab != NULL)
8946 {
8947 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008948 switch (*tab)
8949 {
8950 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008951#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008952 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008953#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008954 case 'D': type = VAR_DICT; break;
8955 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008956 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008957 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960 tab = vim_strchr(tab, '\t');
8961 if (tab != NULL)
8962 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008963 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008964 if (type == VAR_STRING || type == VAR_DICT
8965 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008966 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008968#ifdef FEAT_FLOAT
8969 else if (type == VAR_FLOAT)
8970 (void)string2float(tab + 1, &tv.vval.v_float);
8971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008973 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008974 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008975 {
8976 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8977
8978 if (etv == NULL)
8979 /* Failed to parse back the dict or list, use it as a
8980 * string. */
8981 tv.v_type = VAR_STRING;
8982 else
8983 {
8984 vim_free(tv.vval.v_string);
8985 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008986 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008987 }
8988 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008989 else if (type == VAR_BLOB)
8990 {
8991 blob_T *blob = string2blob(tv.vval.v_string);
8992
8993 if (blob == NULL)
8994 // Failed to parse back the blob, use it as a string.
8995 tv.v_type = VAR_STRING;
8996 else
8997 {
8998 vim_free(tv.vval.v_string);
8999 tv.v_type = VAR_BLOB;
9000 tv.vval.v_blob = blob;
9001 }
9002 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009003
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009004 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009005 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009006 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009007 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009008
9009 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009010 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009011 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9012 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009013 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 }
9015 }
9016 }
9017
9018 return viminfo_readline(virp);
9019}
9020
9021/*
9022 * Write global vars that start with a capital to the viminfo file
9023 */
9024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009025write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 hashitem_T *hi;
9028 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009029 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009030 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009031 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009033 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034
9035 if (find_viminfo_parameter('!') == NULL)
9036 return;
9037
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009038 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009039
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009040 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009041 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009043 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009045 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 this_var = HI2DI(hi);
9047 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009050 {
9051 case VAR_STRING: s = "STR"; break;
9052 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009053 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009054 case VAR_DICT: s = "DIC"; break;
9055 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009056 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009057 case VAR_SPECIAL: s = "XPL"; break;
9058
9059 case VAR_UNKNOWN:
9060 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009061 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009062 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009063 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009064 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009067 if (this_var->di_tv.v_type == VAR_SPECIAL)
9068 {
9069 sprintf((char *)numbuf, "%ld",
9070 (long)this_var->di_tv.vval.v_number);
9071 p = numbuf;
9072 tofree = NULL;
9073 }
9074 else
9075 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009076 if (p != NULL)
9077 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009078 vim_free(tofree);
9079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 }
9081 }
9082}
9083#endif
9084
9085#if defined(FEAT_SESSION) || defined(PROTO)
9086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 hashitem_T *hi;
9090 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009091 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 char_u *p, *t;
9093
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009094 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009097 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009099 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 this_var = HI2DI(hi);
9101 if ((this_var->di_tv.v_type == VAR_NUMBER
9102 || this_var->di_tv.v_type == VAR_STRING)
9103 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009104 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009105 /* Escape special characters with a backslash. Turn a LF and
9106 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009107 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009108 (char_u *)"\\\"\n\r");
9109 if (p == NULL) /* out of memory */
9110 break;
9111 for (t = p; *t != NUL; ++t)
9112 if (*t == '\n')
9113 *t = 'n';
9114 else if (*t == '\r')
9115 *t = 'r';
9116 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 this_var->di_key,
9118 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9119 : ' ',
9120 p,
9121 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9122 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009123 || put_eol(fd) == FAIL)
9124 {
9125 vim_free(p);
9126 return FAIL;
9127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 vim_free(p);
9129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009130#ifdef FEAT_FLOAT
9131 else if (this_var->di_tv.v_type == VAR_FLOAT
9132 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9133 {
9134 float_T f = this_var->di_tv.vval.v_float;
9135 int sign = ' ';
9136
9137 if (f < 0)
9138 {
9139 f = -f;
9140 sign = '-';
9141 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009142 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009143 this_var->di_key, sign, f) < 0)
9144 || put_eol(fd) == FAIL)
9145 return FAIL;
9146 }
9147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 }
9149 }
9150 return OK;
9151}
9152#endif
9153
Bram Moolenaar661b1822005-07-28 22:36:45 +00009154/*
9155 * Display script name where an item was last set.
9156 * Should only be invoked when 'verbose' is non-zero.
9157 */
9158 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009159last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009160{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009161 char_u *p;
9162
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009163 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009164 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009165 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009166 if (p != NULL)
9167 {
9168 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009169 msg_puts(_("\n\tLast set from "));
9170 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009171 if (script_ctx.sc_lnum > 0)
9172 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009173 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009174 msg_outnum((long)script_ctx.sc_lnum);
9175 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009176 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009177 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009178 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009179 }
9180}
9181
Bram Moolenaar53744302015-07-17 17:38:22 +02009182/* reset v:option_new, v:option_old and v:option_type */
9183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009184reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009185{
9186 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9187 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9188 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9189}
9190
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191/*
9192 * Prepare "gap" for an assert error and add the sourcing position.
9193 */
9194 void
9195prepare_assert_error(garray_T *gap)
9196{
9197 char buf[NUMBUFLEN];
9198
9199 ga_init2(gap, 1, 100);
9200 if (sourcing_name != NULL)
9201 {
9202 ga_concat(gap, sourcing_name);
9203 if (sourcing_lnum > 0)
9204 ga_concat(gap, (char_u *)" ");
9205 }
9206 if (sourcing_lnum > 0)
9207 {
9208 sprintf(buf, "line %ld", (long)sourcing_lnum);
9209 ga_concat(gap, (char_u *)buf);
9210 }
9211 if (sourcing_name != NULL || sourcing_lnum > 0)
9212 ga_concat(gap, (char_u *)": ");
9213}
9214
9215/*
9216 * Add an assert error to v:errors.
9217 */
9218 void
9219assert_error(garray_T *gap)
9220{
9221 struct vimvar *vp = &vimvars[VV_ERRORS];
9222
9223 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9224 /* Make sure v:errors is a list. */
9225 set_vim_var_list(VV_ERRORS, list_alloc());
9226 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9227}
9228
Bram Moolenaar65a54642018-04-28 16:56:53 +02009229 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230assert_equal_common(typval_T *argvars, assert_type_T atype)
9231{
9232 garray_T ga;
9233
9234 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9235 != (atype == ASSERT_EQUAL))
9236 {
9237 prepare_assert_error(&ga);
9238 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9239 atype);
9240 assert_error(&ga);
9241 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009242 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009244 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009245}
9246
Bram Moolenaar65a54642018-04-28 16:56:53 +02009247 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009248assert_equalfile(typval_T *argvars)
9249{
9250 char_u buf1[NUMBUFLEN];
9251 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009252 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9253 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009254 garray_T ga;
9255 FILE *fd1;
9256 FILE *fd2;
9257
9258 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009259 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009260
9261 IObuff[0] = NUL;
9262 fd1 = mch_fopen((char *)fname1, READBIN);
9263 if (fd1 == NULL)
9264 {
9265 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9266 }
9267 else
9268 {
9269 fd2 = mch_fopen((char *)fname2, READBIN);
9270 if (fd2 == NULL)
9271 {
9272 fclose(fd1);
9273 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9274 }
9275 else
9276 {
9277 int c1, c2;
9278 long count = 0;
9279
9280 for (;;)
9281 {
9282 c1 = fgetc(fd1);
9283 c2 = fgetc(fd2);
9284 if (c1 == EOF)
9285 {
9286 if (c2 != EOF)
9287 STRCPY(IObuff, "first file is shorter");
9288 break;
9289 }
9290 else if (c2 == EOF)
9291 {
9292 STRCPY(IObuff, "second file is shorter");
9293 break;
9294 }
9295 else if (c1 != c2)
9296 {
9297 vim_snprintf((char *)IObuff, IOSIZE,
9298 "difference at byte %ld", count);
9299 break;
9300 }
9301 ++count;
9302 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009303 fclose(fd1);
9304 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009305 }
9306 }
9307 if (IObuff[0] != NUL)
9308 {
9309 prepare_assert_error(&ga);
9310 ga_concat(&ga, IObuff);
9311 assert_error(&ga);
9312 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009313 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009314 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009315 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009316}
9317
Bram Moolenaar65a54642018-04-28 16:56:53 +02009318 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319assert_match_common(typval_T *argvars, assert_type_T atype)
9320{
9321 garray_T ga;
9322 char_u buf1[NUMBUFLEN];
9323 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9325 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326
9327 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009328 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9330 {
9331 prepare_assert_error(&ga);
9332 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9333 atype);
9334 assert_error(&ga);
9335 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009336 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009338 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339}
9340
Bram Moolenaar65a54642018-04-28 16:56:53 +02009341 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009342assert_inrange(typval_T *argvars)
9343{
9344 garray_T ga;
9345 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009346 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9347 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9348 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009349 char_u *tofree;
9350 char msg[200];
9351 char_u numbuf[NUMBUFLEN];
9352
9353 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009354 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009355 if (actual < lower || actual > upper)
9356 {
9357 prepare_assert_error(&ga);
9358 if (argvars[3].v_type != VAR_UNKNOWN)
9359 {
9360 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9361 vim_free(tofree);
9362 }
9363 else
9364 {
9365 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9366 (long)lower, (long)upper, (long)actual);
9367 ga_concat(&ga, (char_u *)msg);
9368 }
9369 assert_error(&ga);
9370 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009371 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009372 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009373 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009374}
9375
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376/*
9377 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009378 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009380 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381assert_bool(typval_T *argvars, int isTrue)
9382{
9383 int error = FALSE;
9384 garray_T ga;
9385
9386 if (argvars[0].v_type == VAR_SPECIAL
9387 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009388 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009390 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391 || error)
9392 {
9393 prepare_assert_error(&ga);
9394 fill_assert_error(&ga, &argvars[1],
9395 (char_u *)(isTrue ? "True" : "False"),
9396 NULL, &argvars[0], ASSERT_OTHER);
9397 assert_error(&ga);
9398 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009399 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009401 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402}
9403
Bram Moolenaar65a54642018-04-28 16:56:53 +02009404 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009405assert_report(typval_T *argvars)
9406{
9407 garray_T ga;
9408
9409 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009411 assert_error(&ga);
9412 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009413 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009414}
9415
Bram Moolenaar65a54642018-04-28 16:56:53 +02009416 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417assert_exception(typval_T *argvars)
9418{
9419 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009420 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421
9422 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9423 {
9424 prepare_assert_error(&ga);
9425 ga_concat(&ga, (char_u *)"v:exception is not set");
9426 assert_error(&ga);
9427 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009428 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009429 }
9430 else if (error != NULL
9431 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9432 {
9433 prepare_assert_error(&ga);
9434 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9435 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9436 assert_error(&ga);
9437 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009438 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009440 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441}
9442
Bram Moolenaar65a54642018-04-28 16:56:53 +02009443 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009444assert_beeps(typval_T *argvars)
9445{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009446 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009447 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009448 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009449
9450 called_vim_beep = FALSE;
9451 suppress_errthrow = TRUE;
9452 emsg_silent = FALSE;
9453 do_cmdline_cmd(cmd);
9454 if (!called_vim_beep)
9455 {
9456 prepare_assert_error(&ga);
9457 ga_concat(&ga, (char_u *)"command did not beep: ");
9458 ga_concat(&ga, cmd);
9459 assert_error(&ga);
9460 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009461 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009462 }
9463
9464 suppress_errthrow = FALSE;
9465 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009466 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009467}
9468
Bram Moolenaar65a54642018-04-28 16:56:53 +02009469 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470assert_fails(typval_T *argvars)
9471{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009472 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009474 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009475 char_u numbuf[NUMBUFLEN];
9476 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477
9478 called_emsg = FALSE;
9479 suppress_errthrow = TRUE;
9480 emsg_silent = TRUE;
9481 do_cmdline_cmd(cmd);
9482 if (!called_emsg)
9483 {
9484 prepare_assert_error(&ga);
9485 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009486 if (argvars[1].v_type != VAR_UNKNOWN
9487 && argvars[2].v_type != VAR_UNKNOWN)
9488 {
9489 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9490 vim_free(tofree);
9491 }
9492 else
9493 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 assert_error(&ga);
9495 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009496 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009497 }
9498 else if (argvars[1].v_type != VAR_UNKNOWN)
9499 {
9500 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009501 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502
9503 if (error == NULL
9504 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9505 {
9506 prepare_assert_error(&ga);
9507 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9508 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9509 assert_error(&ga);
9510 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009511 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009512 }
9513 }
9514
9515 called_emsg = FALSE;
9516 suppress_errthrow = FALSE;
9517 emsg_silent = FALSE;
9518 emsg_on_display = FALSE;
9519 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009520 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521}
9522
9523/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009524 * Append "p[clen]" to "gap", escaping unprintable characters.
9525 * Changes NL to \n, CR to \r, etc.
9526 */
9527 static void
9528ga_concat_esc(garray_T *gap, char_u *p, int clen)
9529{
9530 char_u buf[NUMBUFLEN];
9531
9532 if (clen > 1)
9533 {
9534 mch_memmove(buf, p, clen);
9535 buf[clen] = NUL;
9536 ga_concat(gap, buf);
9537 }
9538 else switch (*p)
9539 {
9540 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9541 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9542 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9543 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9544 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9545 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9546 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9547 default:
9548 if (*p < ' ')
9549 {
9550 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9551 ga_concat(gap, buf);
9552 }
9553 else
9554 ga_append(gap, *p);
9555 break;
9556 }
9557}
9558
9559/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560 * Append "str" to "gap", escaping unprintable characters.
9561 * Changes NL to \n, CR to \r, etc.
9562 */
9563 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009564ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565{
9566 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009567 char_u *s;
9568 int c;
9569 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009570 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009571 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572
9573 if (str == NULL)
9574 {
9575 ga_concat(gap, (char_u *)"NULL");
9576 return;
9577 }
9578
9579 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009580 {
9581 same_len = 1;
9582 s = p;
9583 c = mb_ptr2char_adv(&s);
9584 clen = s - p;
9585 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009587 ++same_len;
9588 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009590 if (same_len > 20)
9591 {
9592 ga_concat(gap, (char_u *)"\\[");
9593 ga_concat_esc(gap, p, clen);
9594 ga_concat(gap, (char_u *)" occurs ");
9595 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9596 ga_concat(gap, buf);
9597 ga_concat(gap, (char_u *)" times]");
9598 p = s - 1;
9599 }
9600 else
9601 ga_concat_esc(gap, p, clen);
9602 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603}
9604
9605/*
9606 * Fill "gap" with information about an assert error.
9607 */
9608 void
9609fill_assert_error(
9610 garray_T *gap,
9611 typval_T *opt_msg_tv,
9612 char_u *exp_str,
9613 typval_T *exp_tv,
9614 typval_T *got_tv,
9615 assert_type_T atype)
9616{
9617 char_u numbuf[NUMBUFLEN];
9618 char_u *tofree;
9619
9620 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9621 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009622 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9623 vim_free(tofree);
9624 ga_concat(gap, (char_u *)": ");
9625 }
9626
9627 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9628 ga_concat(gap, (char_u *)"Pattern ");
9629 else if (atype == ASSERT_NOTEQUAL)
9630 ga_concat(gap, (char_u *)"Expected not equal to ");
9631 else
9632 ga_concat(gap, (char_u *)"Expected ");
9633 if (exp_str == NULL)
9634 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009635 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 vim_free(tofree);
9637 }
9638 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009639 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009640 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009642 if (atype == ASSERT_MATCH)
9643 ga_concat(gap, (char_u *)" does not match ");
9644 else if (atype == ASSERT_NOTMATCH)
9645 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009647 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009648 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009649 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650 }
9651}
9652
Bram Moolenaar31988702018-02-13 12:57:42 +01009653/*
9654 * Compare "typ1" and "typ2". Put the result in "typ1".
9655 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009656 int
9657typval_compare(
9658 typval_T *typ1, /* first operand */
9659 typval_T *typ2, /* second operand */
9660 exptype_T type, /* operator */
9661 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009662 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009663{
9664 int i;
9665 varnumber_T n1, n2;
9666 char_u *s1, *s2;
9667 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9668
Bram Moolenaar31988702018-02-13 12:57:42 +01009669 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009670 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009671 /* For "is" a different type always means FALSE, for "notis"
9672 * it means TRUE. */
9673 n1 = (type == TYPE_NEQUAL);
9674 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009675 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9676 {
9677 if (type_is)
9678 {
9679 n1 = (typ1->v_type == typ2->v_type
9680 && typ1->vval.v_blob == typ2->vval.v_blob);
9681 if (type == TYPE_NEQUAL)
9682 n1 = !n1;
9683 }
9684 else if (typ1->v_type != typ2->v_type
9685 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9686 {
9687 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009688 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009689 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009690 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009691 clear_tv(typ1);
9692 return FAIL;
9693 }
9694 else
9695 {
9696 // Compare two Blobs for being equal or unequal.
9697 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9698 if (type == TYPE_NEQUAL)
9699 n1 = !n1;
9700 }
9701 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009702 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9703 {
9704 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009705 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009706 n1 = (typ1->v_type == typ2->v_type
9707 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009708 if (type == TYPE_NEQUAL)
9709 n1 = !n1;
9710 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009711 else if (typ1->v_type != typ2->v_type
9712 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009713 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009714 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009715 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009716 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009717 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009718 clear_tv(typ1);
9719 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009720 }
9721 else
9722 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009723 /* Compare two Lists for being equal or unequal. */
9724 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9725 ic, FALSE);
9726 if (type == TYPE_NEQUAL)
9727 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009728 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009729 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009730
9731 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9732 {
9733 if (type_is)
9734 {
9735 n1 = (typ1->v_type == typ2->v_type
9736 && typ1->vval.v_dict == typ2->vval.v_dict);
9737 if (type == TYPE_NEQUAL)
9738 n1 = !n1;
9739 }
9740 else if (typ1->v_type != typ2->v_type
9741 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9742 {
9743 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009744 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009745 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009746 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009747 clear_tv(typ1);
9748 return FAIL;
9749 }
9750 else
9751 {
9752 /* Compare two Dictionaries for being equal or unequal. */
9753 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9754 ic, FALSE);
9755 if (type == TYPE_NEQUAL)
9756 n1 = !n1;
9757 }
9758 }
9759
9760 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9761 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9762 {
9763 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009765 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009766 clear_tv(typ1);
9767 return FAIL;
9768 }
9769 if ((typ1->v_type == VAR_PARTIAL
9770 && typ1->vval.v_partial == NULL)
9771 || (typ2->v_type == VAR_PARTIAL
9772 && typ2->vval.v_partial == NULL))
9773 /* when a partial is NULL assume not equal */
9774 n1 = FALSE;
9775 else if (type_is)
9776 {
9777 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9778 /* strings are considered the same if their value is
9779 * the same */
9780 n1 = tv_equal(typ1, typ2, ic, FALSE);
9781 else if (typ1->v_type == VAR_PARTIAL
9782 && typ2->v_type == VAR_PARTIAL)
9783 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9784 else
9785 n1 = FALSE;
9786 }
9787 else
9788 n1 = tv_equal(typ1, typ2, ic, FALSE);
9789 if (type == TYPE_NEQUAL)
9790 n1 = !n1;
9791 }
9792
9793#ifdef FEAT_FLOAT
9794 /*
9795 * If one of the two variables is a float, compare as a float.
9796 * When using "=~" or "!~", always compare as string.
9797 */
9798 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9799 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9800 {
9801 float_T f1, f2;
9802
9803 if (typ1->v_type == VAR_FLOAT)
9804 f1 = typ1->vval.v_float;
9805 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009806 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009807 if (typ2->v_type == VAR_FLOAT)
9808 f2 = typ2->vval.v_float;
9809 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009810 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009811 n1 = FALSE;
9812 switch (type)
9813 {
9814 case TYPE_EQUAL: n1 = (f1 == f2); break;
9815 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9816 case TYPE_GREATER: n1 = (f1 > f2); break;
9817 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9818 case TYPE_SMALLER: n1 = (f1 < f2); break;
9819 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9820 case TYPE_UNKNOWN:
9821 case TYPE_MATCH:
9822 case TYPE_NOMATCH: break; /* avoid gcc warning */
9823 }
9824 }
9825#endif
9826
9827 /*
9828 * If one of the two variables is a number, compare as a number.
9829 * When using "=~" or "!~", always compare as string.
9830 */
9831 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9832 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9833 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009834 n1 = tv_get_number(typ1);
9835 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009836 switch (type)
9837 {
9838 case TYPE_EQUAL: n1 = (n1 == n2); break;
9839 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9840 case TYPE_GREATER: n1 = (n1 > n2); break;
9841 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9842 case TYPE_SMALLER: n1 = (n1 < n2); break;
9843 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9844 case TYPE_UNKNOWN:
9845 case TYPE_MATCH:
9846 case TYPE_NOMATCH: break; /* avoid gcc warning */
9847 }
9848 }
9849 else
9850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009851 s1 = tv_get_string_buf(typ1, buf1);
9852 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009853 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9854 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9855 else
9856 i = 0;
9857 n1 = FALSE;
9858 switch (type)
9859 {
9860 case TYPE_EQUAL: n1 = (i == 0); break;
9861 case TYPE_NEQUAL: n1 = (i != 0); break;
9862 case TYPE_GREATER: n1 = (i > 0); break;
9863 case TYPE_GEQUAL: n1 = (i >= 0); break;
9864 case TYPE_SMALLER: n1 = (i < 0); break;
9865 case TYPE_SEQUAL: n1 = (i <= 0); break;
9866
9867 case TYPE_MATCH:
9868 case TYPE_NOMATCH:
9869 n1 = pattern_match(s2, s1, ic);
9870 if (type == TYPE_NOMATCH)
9871 n1 = !n1;
9872 break;
9873
9874 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9875 }
9876 }
9877 clear_tv(typ1);
9878 typ1->v_type = VAR_NUMBER;
9879 typ1->vval.v_number = n1;
9880
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009881 return OK;
9882}
9883
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009884 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009885typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009886{
9887 char_u *tofree;
9888 char_u numbuf[NUMBUFLEN];
9889 char_u *ret = NULL;
9890
9891 if (arg == NULL)
9892 return vim_strsave((char_u *)"(does not exist)");
9893 ret = tv2string(arg, &tofree, numbuf, 0);
9894 /* Make a copy if we have a value but it's not in allocated memory. */
9895 if (ret != NULL && tofree == NULL)
9896 ret = vim_strsave(ret);
9897 return ret;
9898}
9899
9900 int
9901var_exists(char_u *var)
9902{
9903 char_u *name;
9904 char_u *tofree;
9905 typval_T tv;
9906 int len = 0;
9907 int n = FALSE;
9908
9909 /* get_name_len() takes care of expanding curly braces */
9910 name = var;
9911 len = get_name_len(&var, &tofree, TRUE, FALSE);
9912 if (len > 0)
9913 {
9914 if (tofree != NULL)
9915 name = tofree;
9916 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9917 if (n)
9918 {
9919 /* handle d.key, l[idx], f(expr) */
9920 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9921 if (n)
9922 clear_tv(&tv);
9923 }
9924 }
9925 if (*var != NUL)
9926 n = FALSE;
9927
9928 vim_free(tofree);
9929 return n;
9930}
9931
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932#endif /* FEAT_EVAL */
9933
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
9937#ifdef WIN3264
9938/*
9939 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
9942/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009943 * Get the short path (8.3) for the filename in "fnamep".
9944 * Only works for a valid file name.
9945 * When the path gets longer "fnamep" is changed and the allocated buffer
9946 * is put in "bufp".
9947 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9948 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 */
9950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009951get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009953 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 char_u *newbuf;
9955
9956 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009957 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (l > len - 1)
9959 {
9960 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009961 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 newbuf = vim_strnsave(*fnamep, l);
9963 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 vim_free(*bufp);
9967 *fnamep = *bufp = newbuf;
9968
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009969 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009970 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 }
9972
9973 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009974 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975}
9976
9977/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009978 * Get the short path (8.3) for the filename in "fname". The converted
9979 * path is returned in "bufp".
9980 *
9981 * Some of the directories specified in "fname" may not exist. This function
9982 * will shorten the existing directories at the beginning of the path and then
9983 * append the remaining non-existing path.
9984 *
9985 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009986 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009987 * bufp - Pointer to an allocated buffer for the filename.
9988 * fnamelen - Length of the filename pointed to by fname
9989 *
9990 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 */
9992 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009993shortpath_for_invalid_fname(
9994 char_u **fname,
9995 char_u **bufp,
9996 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009998 char_u *short_fname, *save_fname, *pbuf_unused;
9999 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010001 int old_len, len;
10002 int new_len, sfx_len;
10003 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
10005 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010006 old_len = *fnamelen;
10007 save_fname = vim_strnsave(*fname, old_len);
10008 pbuf_unused = NULL;
10009 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010011 endp = save_fname + old_len - 1; /* Find the end of the copy */
10012 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010014 /*
10015 * Try shortening the supplied path till it succeeds by removing one
10016 * directory at a time from the tail of the path.
10017 */
10018 len = 0;
10019 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010021 /* go back one path-separator */
10022 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10023 --endp;
10024 if (endp <= save_fname)
10025 break; /* processed the complete path */
10026
10027 /*
10028 * Replace the path separator with a NUL and try to shorten the
10029 * resulting path.
10030 */
10031 ch = *endp;
10032 *endp = 0;
10033 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010034 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010035 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10036 {
10037 retval = FAIL;
10038 goto theend;
10039 }
10040 *endp = ch; /* preserve the string */
10041
10042 if (len > 0)
10043 break; /* successfully shortened the path */
10044
10045 /* failed to shorten the path. Skip the path separator */
10046 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
10048
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010049 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010051 /*
10052 * Succeeded in shortening the path. Now concatenate the shortened
10053 * path with the remaining path at the tail.
10054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010056 /* Compute the length of the new path. */
10057 sfx_len = (int)(save_endp - endp) + 1;
10058 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010060 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010062 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010064 /* There is not enough space in the currently allocated string,
10065 * copy it to a buffer big enough. */
10066 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010068 {
10069 retval = FAIL;
10070 goto theend;
10071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 }
10073 else
10074 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010075 /* Transfer short_fname to the main buffer (it's big enough),
10076 * unless get_short_pathname() did its work in-place. */
10077 *fname = *bufp = save_fname;
10078 if (short_fname != save_fname)
10079 vim_strncpy(save_fname, short_fname, len);
10080 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010082
10083 /* concat the not-shortened part of the path */
10084 vim_strncpy(*fname + len, endp, sfx_len);
10085 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010087
10088theend:
10089 vim_free(pbuf_unused);
10090 vim_free(save_fname);
10091
10092 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093}
10094
10095/*
10096 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010097 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 */
10099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010100shortpath_for_partial(
10101 char_u **fnamep,
10102 char_u **bufp,
10103 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105 int sepcount, len, tflen;
10106 char_u *p;
10107 char_u *pbuf, *tfname;
10108 int hasTilde;
10109
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010110 /* Count up the path separators from the RHS.. so we know which part
10111 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010113 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (vim_ispathsep(*p))
10115 ++sepcount;
10116
10117 /* Need full path first (use expand_env() to remove a "~/") */
10118 hasTilde = (**fnamep == '~');
10119 if (hasTilde)
10120 pbuf = tfname = expand_env_save(*fnamep);
10121 else
10122 pbuf = tfname = FullName_save(*fnamep, FALSE);
10123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010124 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010126 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128
10129 if (len == 0)
10130 {
10131 /* Don't have a valid filename, so shorten the rest of the
10132 * path if we can. This CAN give us invalid 8.3 filenames, but
10133 * there's not a lot of point in guessing what it might be.
10134 */
10135 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010136 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139
10140 /* Count the paths backward to find the beginning of the desired string. */
10141 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010142 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010143 if (has_mbyte)
10144 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 if (vim_ispathsep(*p))
10146 {
10147 if (sepcount == 0 || (hasTilde && sepcount == 1))
10148 break;
10149 else
10150 sepcount --;
10151 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 if (hasTilde)
10154 {
10155 --p;
10156 if (p >= tfname)
10157 *p = '~';
10158 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 }
10161 else
10162 ++p;
10163
10164 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10165 vim_free(*bufp);
10166 *fnamelen = (int)STRLEN(p);
10167 *bufp = pbuf;
10168 *fnamep = p;
10169
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010170 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171}
10172#endif /* WIN3264 */
10173
10174/*
10175 * Adjust a filename, according to a string of modifiers.
10176 * *fnamep must be NUL terminated when called. When returning, the length is
10177 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010178 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 * When there is an error, *fnamep is set to NULL.
10180 */
10181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010182modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010183 char_u *src, // string with modifiers
10184 int tilde_file, // "~" is a file name, not $HOME
10185 int *usedlen, // characters after src that are used
10186 char_u **fnamep, // file name so far
10187 char_u **bufp, // buffer for allocated file name or NULL
10188 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 int valid = 0;
10191 char_u *tail;
10192 char_u *s, *p, *pbuf;
10193 char_u dirname[MAXPATHL];
10194 int c;
10195 int has_fullname = 0;
10196#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010197 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 int has_shortname = 0;
10199#endif
10200
10201repeat:
10202 /* ":p" - full path/file_name */
10203 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10204 {
10205 has_fullname = 1;
10206
10207 valid |= VALID_PATH;
10208 *usedlen += 2;
10209
10210 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10211 if ((*fnamep)[0] == '~'
10212#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10213 && ((*fnamep)[1] == '/'
10214# ifdef BACKSLASH_IN_FILENAME
10215 || (*fnamep)[1] == '\\'
10216# endif
10217 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010219 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 )
10221 {
10222 *fnamep = expand_env_save(*fnamep);
10223 vim_free(*bufp); /* free any allocated file name */
10224 *bufp = *fnamep;
10225 if (*fnamep == NULL)
10226 return -1;
10227 }
10228
10229 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010230 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 {
10232 if (vim_ispathsep(*p)
10233 && p[1] == '.'
10234 && (p[2] == NUL
10235 || vim_ispathsep(p[2])
10236 || (p[2] == '.'
10237 && (p[3] == NUL || vim_ispathsep(p[3])))))
10238 break;
10239 }
10240
10241 /* FullName_save() is slow, don't use it when not needed. */
10242 if (*p != NUL || !vim_isAbsName(*fnamep))
10243 {
10244 *fnamep = FullName_save(*fnamep, *p != NUL);
10245 vim_free(*bufp); /* free any allocated file name */
10246 *bufp = *fnamep;
10247 if (*fnamep == NULL)
10248 return -1;
10249 }
10250
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010251#ifdef WIN3264
10252# if _WIN32_WINNT >= 0x0500
10253 if (vim_strchr(*fnamep, '~') != NULL)
10254 {
10255 /* Expand 8.3 filename to full path. Needed to make sure the same
10256 * file does not have two different names.
10257 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10258 p = alloc(_MAX_PATH + 1);
10259 if (p != NULL)
10260 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010261 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010262 {
10263 vim_free(*bufp);
10264 *bufp = *fnamep = p;
10265 }
10266 else
10267 vim_free(p);
10268 }
10269 }
10270# endif
10271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 /* Append a path separator to a directory. */
10273 if (mch_isdir(*fnamep))
10274 {
10275 /* Make room for one or two extra characters. */
10276 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10277 vim_free(*bufp); /* free any allocated file name */
10278 *bufp = *fnamep;
10279 if (*fnamep == NULL)
10280 return -1;
10281 add_pathsep(*fnamep);
10282 }
10283 }
10284
10285 /* ":." - path relative to the current directory */
10286 /* ":~" - path relative to the home directory */
10287 /* ":8" - shortname path - postponed till after */
10288 while (src[*usedlen] == ':'
10289 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10290 {
10291 *usedlen += 2;
10292 if (c == '8')
10293 {
10294#ifdef WIN3264
10295 has_shortname = 1; /* Postpone this. */
10296#endif
10297 continue;
10298 }
10299 pbuf = NULL;
10300 /* Need full path first (use expand_env() to remove a "~/") */
10301 if (!has_fullname)
10302 {
10303 if (c == '.' && **fnamep == '~')
10304 p = pbuf = expand_env_save(*fnamep);
10305 else
10306 p = pbuf = FullName_save(*fnamep, FALSE);
10307 }
10308 else
10309 p = *fnamep;
10310
10311 has_fullname = 0;
10312
10313 if (p != NULL)
10314 {
10315 if (c == '.')
10316 {
10317 mch_dirname(dirname, MAXPATHL);
10318 s = shorten_fname(p, dirname);
10319 if (s != NULL)
10320 {
10321 *fnamep = s;
10322 if (pbuf != NULL)
10323 {
10324 vim_free(*bufp); /* free any allocated file name */
10325 *bufp = pbuf;
10326 pbuf = NULL;
10327 }
10328 }
10329 }
10330 else
10331 {
10332 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10333 /* Only replace it when it starts with '~' */
10334 if (*dirname == '~')
10335 {
10336 s = vim_strsave(dirname);
10337 if (s != NULL)
10338 {
10339 *fnamep = s;
10340 vim_free(*bufp);
10341 *bufp = s;
10342 }
10343 }
10344 }
10345 vim_free(pbuf);
10346 }
10347 }
10348
10349 tail = gettail(*fnamep);
10350 *fnamelen = (int)STRLEN(*fnamep);
10351
10352 /* ":h" - head, remove "/file_name", can be repeated */
10353 /* Don't remove the first "/" or "c:\" */
10354 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10355 {
10356 valid |= VALID_HEAD;
10357 *usedlen += 2;
10358 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010359 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010360 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 *fnamelen = (int)(tail - *fnamep);
10362#ifdef VMS
10363 if (*fnamelen > 0)
10364 *fnamelen += 1; /* the path separator is part of the path */
10365#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010366 if (*fnamelen == 0)
10367 {
10368 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10369 p = vim_strsave((char_u *)".");
10370 if (p == NULL)
10371 return -1;
10372 vim_free(*bufp);
10373 *bufp = *fnamep = tail = p;
10374 *fnamelen = 1;
10375 }
10376 else
10377 {
10378 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010379 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 }
10382
10383 /* ":8" - shortname */
10384 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10385 {
10386 *usedlen += 2;
10387#ifdef WIN3264
10388 has_shortname = 1;
10389#endif
10390 }
10391
10392#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010393 /*
10394 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 */
10396 if (has_shortname)
10397 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010398 /* Copy the string if it is shortened by :h and when it wasn't copied
10399 * yet, because we are going to change it in place. Avoids changing
10400 * the buffer name for "%:8". */
10401 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 {
10403 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010404 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 return -1;
10406 vim_free(*bufp);
10407 *bufp = *fnamep = p;
10408 }
10409
10410 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010411 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 if (!has_fullname && !vim_isAbsName(*fnamep))
10413 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010414 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 return -1;
10416 }
10417 else
10418 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010419 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420
Bram Moolenaardc935552011-08-17 15:23:23 +020010421 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010423 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 return -1;
10425
10426 if (l == 0)
10427 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010428 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010430 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 return -1;
10432 }
10433 *fnamelen = l;
10434 }
10435 }
10436#endif /* WIN3264 */
10437
10438 /* ":t" - tail, just the basename */
10439 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10440 {
10441 *usedlen += 2;
10442 *fnamelen -= (int)(tail - *fnamep);
10443 *fnamep = tail;
10444 }
10445
10446 /* ":e" - extension, can be repeated */
10447 /* ":r" - root, without extension, can be repeated */
10448 while (src[*usedlen] == ':'
10449 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10450 {
10451 /* find a '.' in the tail:
10452 * - for second :e: before the current fname
10453 * - otherwise: The last '.'
10454 */
10455 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10456 s = *fnamep - 2;
10457 else
10458 s = *fnamep + *fnamelen - 1;
10459 for ( ; s > tail; --s)
10460 if (s[0] == '.')
10461 break;
10462 if (src[*usedlen + 1] == 'e') /* :e */
10463 {
10464 if (s > tail)
10465 {
10466 *fnamelen += (int)(*fnamep - (s + 1));
10467 *fnamep = s + 1;
10468#ifdef VMS
10469 /* cut version from the extension */
10470 s = *fnamep + *fnamelen - 1;
10471 for ( ; s > *fnamep; --s)
10472 if (s[0] == ';')
10473 break;
10474 if (s > *fnamep)
10475 *fnamelen = s - *fnamep;
10476#endif
10477 }
10478 else if (*fnamep <= tail)
10479 *fnamelen = 0;
10480 }
10481 else /* :r */
10482 {
10483 if (s > tail) /* remove one extension */
10484 *fnamelen = (int)(s - *fnamep);
10485 }
10486 *usedlen += 2;
10487 }
10488
10489 /* ":s?pat?foo?" - substitute */
10490 /* ":gs?pat?foo?" - global substitute */
10491 if (src[*usedlen] == ':'
10492 && (src[*usedlen + 1] == 's'
10493 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10494 {
10495 char_u *str;
10496 char_u *pat;
10497 char_u *sub;
10498 int sep;
10499 char_u *flags;
10500 int didit = FALSE;
10501
10502 flags = (char_u *)"";
10503 s = src + *usedlen + 2;
10504 if (src[*usedlen + 1] == 'g')
10505 {
10506 flags = (char_u *)"g";
10507 ++s;
10508 }
10509
10510 sep = *s++;
10511 if (sep)
10512 {
10513 /* find end of pattern */
10514 p = vim_strchr(s, sep);
10515 if (p != NULL)
10516 {
10517 pat = vim_strnsave(s, (int)(p - s));
10518 if (pat != NULL)
10519 {
10520 s = p + 1;
10521 /* find end of substitution */
10522 p = vim_strchr(s, sep);
10523 if (p != NULL)
10524 {
10525 sub = vim_strnsave(s, (int)(p - s));
10526 str = vim_strnsave(*fnamep, *fnamelen);
10527 if (sub != NULL && str != NULL)
10528 {
10529 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010530 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 if (s != NULL)
10532 {
10533 *fnamep = s;
10534 *fnamelen = (int)STRLEN(s);
10535 vim_free(*bufp);
10536 *bufp = s;
10537 didit = TRUE;
10538 }
10539 }
10540 vim_free(sub);
10541 vim_free(str);
10542 }
10543 vim_free(pat);
10544 }
10545 }
10546 /* after using ":s", repeat all the modifiers */
10547 if (didit)
10548 goto repeat;
10549 }
10550 }
10551
Bram Moolenaar26df0922014-02-23 23:39:13 +010010552 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10553 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010554 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010555 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010556 if (c != NUL)
10557 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010558 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010559 if (c != NUL)
10560 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010561 if (p == NULL)
10562 return -1;
10563 vim_free(*bufp);
10564 *bufp = *fnamep = p;
10565 *fnamelen = (int)STRLEN(p);
10566 *usedlen += 2;
10567 }
10568
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 return valid;
10570}
10571
10572/*
10573 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010574 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 * "flags" can be "g" to do a global substitute.
10576 * Returns an allocated string, NULL for error.
10577 */
10578 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010579do_string_sub(
10580 char_u *str,
10581 char_u *pat,
10582 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010583 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010584 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 int sublen;
10587 regmatch_T regmatch;
10588 int i;
10589 int do_all;
10590 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010591 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 garray_T ga;
10593 char_u *ret;
10594 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010595 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596
10597 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10598 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010599 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600
10601 ga_init2(&ga, 1, 200);
10602
10603 do_all = (flags[0] == 'g');
10604
10605 regmatch.rm_ic = p_ic;
10606 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10607 if (regmatch.regprog != NULL)
10608 {
10609 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010610 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10612 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010613 /* Skip empty match except for first match. */
10614 if (regmatch.startp[0] == regmatch.endp[0])
10615 {
10616 if (zero_width == regmatch.startp[0])
10617 {
10618 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010619 i = MB_PTR2LEN(tail);
10620 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10621 (size_t)i);
10622 ga.ga_len += i;
10623 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010624 continue;
10625 }
10626 zero_width = regmatch.startp[0];
10627 }
10628
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 /*
10630 * Get some space for a temporary buffer to do the substitution
10631 * into. It will contain:
10632 * - The text up to where the match is.
10633 * - The substituted text.
10634 * - The text after the match.
10635 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010636 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010637 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10639 {
10640 ga_clear(&ga);
10641 break;
10642 }
10643
10644 /* copy the text up to where the match is */
10645 i = (int)(regmatch.startp[0] - tail);
10646 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10647 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010648 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 + ga.ga_len + i, TRUE, TRUE, FALSE);
10650 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010651 tail = regmatch.endp[0];
10652 if (*tail == NUL)
10653 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 if (!do_all)
10655 break;
10656 }
10657
10658 if (ga.ga_data != NULL)
10659 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10660
Bram Moolenaar473de612013-06-08 18:19:48 +020010661 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663
10664 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10665 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010666 if (p_cpo == empty_option)
10667 p_cpo = save_cpo;
10668 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010669 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010670 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671
10672 return ret;
10673}
10674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 static int
10676filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10677{
10678 typval_T rettv;
10679 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010681
10682 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10683 argv[0] = vimvars[VV_KEY].vv_tv;
10684 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010685 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10686 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 if (map)
10688 {
10689 /* map(): replace the list item value */
10690 clear_tv(tv);
10691 rettv.v_lock = 0;
10692 *tv = rettv;
10693 }
10694 else
10695 {
10696 int error = FALSE;
10697
10698 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010699 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010700 clear_tv(&rettv);
10701 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010702 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 if (error)
10704 goto theend;
10705 }
10706 retval = OK;
10707theend:
10708 clear_tv(&vimvars[VV_VAL].vv_tv);
10709 return retval;
10710}
10711
10712
10713/*
10714 * Implementation of map() and filter().
10715 */
10716 void
10717filter_map(typval_T *argvars, typval_T *rettv, int map)
10718{
10719 typval_T *expr;
10720 listitem_T *li, *nli;
10721 list_T *l = NULL;
10722 dictitem_T *di;
10723 hashtab_T *ht;
10724 hashitem_T *hi;
10725 dict_T *d = NULL;
10726 typval_T save_val;
10727 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010728 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010729 int rem;
10730 int todo;
10731 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10732 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10733 : N_("filter() argument"));
10734 int save_did_emsg;
10735 int idx = 0;
10736
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010737 if (argvars[0].v_type == VAR_BLOB)
10738 {
10739 if ((b = argvars[0].vval.v_blob) == NULL)
10740 return;
10741 }
10742 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 {
10744 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010745 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746 return;
10747 }
10748 else if (argvars[0].v_type == VAR_DICT)
10749 {
10750 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010751 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 return;
10753 }
10754 else
10755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010756 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 return;
10758 }
10759
10760 expr = &argvars[1];
10761 /* On type errors, the preceding call has already displayed an error
10762 * message. Avoid a misleading error message for an empty string that
10763 * was not passed as argument. */
10764 if (expr->v_type != VAR_UNKNOWN)
10765 {
10766 prepare_vimvar(VV_VAL, &save_val);
10767
10768 /* We reset "did_emsg" to be able to detect whether an error
10769 * occurred during evaluation of the expression. */
10770 save_did_emsg = did_emsg;
10771 did_emsg = FALSE;
10772
10773 prepare_vimvar(VV_KEY, &save_key);
10774 if (argvars[0].v_type == VAR_DICT)
10775 {
10776 vimvars[VV_KEY].vv_type = VAR_STRING;
10777
10778 ht = &d->dv_hashtab;
10779 hash_lock(ht);
10780 todo = (int)ht->ht_used;
10781 for (hi = ht->ht_array; todo > 0; ++hi)
10782 {
10783 if (!HASHITEM_EMPTY(hi))
10784 {
10785 int r;
10786
10787 --todo;
10788 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010789 if (map && (var_check_lock(di->di_tv.v_lock,
10790 arg_errmsg, TRUE)
10791 || var_check_ro(di->di_flags,
10792 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010793 break;
10794 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10795 r = filter_map_one(&di->di_tv, expr, map, &rem);
10796 clear_tv(&vimvars[VV_KEY].vv_tv);
10797 if (r == FAIL || did_emsg)
10798 break;
10799 if (!map && rem)
10800 {
10801 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10802 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10803 break;
10804 dictitem_remove(d, di);
10805 }
10806 }
10807 }
10808 hash_unlock(ht);
10809 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010810 else if (argvars[0].v_type == VAR_BLOB)
10811 {
10812 int i;
10813 typval_T tv;
10814
10815 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10816 for (i = 0; i < b->bv_ga.ga_len; i++)
10817 {
10818 tv.v_type = VAR_NUMBER;
10819 tv.vval.v_number = blob_get(b, i);
10820 vimvars[VV_KEY].vv_nr = idx;
10821 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10822 break;
10823 if (tv.v_type != VAR_NUMBER)
10824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010825 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010826 return;
10827 }
10828 tv.v_type = VAR_NUMBER;
10829 blob_set(b, i, tv.vval.v_number);
10830 if (!map && rem)
10831 {
10832 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10833
10834 mch_memmove(p + idx, p + i + 1,
10835 (size_t)b->bv_ga.ga_len - i - 1);
10836 --b->bv_ga.ga_len;
10837 --i;
10838 }
10839 }
10840 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010841 else
10842 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010843 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10845
10846 for (li = l->lv_first; li != NULL; li = nli)
10847 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010848 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010849 break;
10850 nli = li->li_next;
10851 vimvars[VV_KEY].vv_nr = idx;
10852 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10853 || did_emsg)
10854 break;
10855 if (!map && rem)
10856 listitem_remove(l, li);
10857 ++idx;
10858 }
10859 }
10860
10861 restore_vimvar(VV_KEY, &save_key);
10862 restore_vimvar(VV_VAL, &save_val);
10863
10864 did_emsg |= save_did_emsg;
10865 }
10866
10867 copy_tv(&argvars[0], rettv);
10868}
10869
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */