blob: b31ed351da6488187119341607685c8b14e1a7e6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
248static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
249static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100255
256#if defined(EBCDIC) || defined(PROTO)
257/*
258 * Compare struct fst by function name.
259 */
260 static int
261compare_func_name(const void *s1, const void *s2)
262{
263 struct fst *p1 = (struct fst *)s1;
264 struct fst *p2 = (struct fst *)s2;
265
266 return STRCMP(p1->f_name, p2->f_name);
267}
268
269/*
270 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100271 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100272 * On machines using EBCDIC we have to sort it.
273 */
274 static void
275sortFunctions(void)
276{
277 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
278
279 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
280}
281#endif
282
283
Bram Moolenaar33570922005-01-25 22:26:29 +0000284/*
285 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000286 */
287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100288eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000289{
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 int i;
291 struct vimvar *p;
292
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200293 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
294 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200295 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000296 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299 for (i = 0; i < VV_LEN; ++i)
300 {
301 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 if (STRLEN(p->vv_name) > 16)
303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100304 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200305 getout(1);
306 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 STRCPY(p->vv_di.di_key, p->vv_name);
308 if (p->vv_flags & VV_RO)
309 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
310 else if (p->vv_flags & VV_RO_SBX)
311 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
312 else
313 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000314
315 /* add to v: scope dict, unless the value is not always available */
316 if (p->vv_type != VAR_UNKNOWN)
317 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000319 /* add to compat scope dict */
320 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000321 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100322 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
323
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000324 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100325 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100326 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100327 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100328 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329
330 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
331 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
332 set_vim_var_nr(VV_NONE, VVAL_NONE);
333 set_vim_var_nr(VV_NULL, VVAL_NULL);
334
Bram Moolenaarf562e722016-07-19 17:25:25 +0200335 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
336 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
337 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
338 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
339 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
340 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
341 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
342 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
343 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
344 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100345 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200346
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200347 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348
349#ifdef EBCDIC
350 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100351 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200352 */
353 sortFunctions();
354#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000355}
356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000357#if defined(EXITFREE) || defined(PROTO)
358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100359eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000360{
361 int i;
362 struct vimvar *p;
363
364 for (i = 0; i < VV_LEN; ++i)
365 {
366 p = &vimvars[i];
367 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100368 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000369 else if (p->vv_di.di_tv.v_type == VAR_LIST)
370 {
371 list_unref(p->vv_list);
372 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 }
375 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000376 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377 hash_clear(&compat_hashtab);
378
Bram Moolenaard9fba312005-06-26 22:34:35 +0000379 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100380# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200381 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100382# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000383
384 /* global variables */
385 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000386
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000387 /* autoloaded script names */
388 ga_clear_strings(&ga_loaded);
389
Bram Moolenaarcca74132013-09-25 21:00:28 +0200390 /* Script-local variables. First clear all the variables and in a second
391 * loop free the scriptvar_T, because a variable in one script might hold
392 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200395 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200396 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 ga_clear(&ga_scripts);
398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000399 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200400 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000401
402 /* functions */
403 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000404}
405#endif
406
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * Set an internal variable to a string value. Creates the variable if it does
410 * not already exist.
411 */
412 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100413set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000415 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000416 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418 val = vim_strsave(value);
419 if (val != NULL)
420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000424 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427 }
428}
429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200431#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000432static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433static char_u *redir_endp = NULL;
434static char_u *redir_varname = NULL;
435
436/*
437 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439 * Returns OK if successfully completed the setup. FAIL otherwise.
440 */
441 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100442var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000443{
444 int save_emsg;
445 int err;
446 typval_T tv;
447
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000448 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000449 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100451 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 return FAIL;
453 }
454
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000455 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000456 redir_varname = vim_strsave(name);
457 if (redir_varname == NULL)
458 return FAIL;
459
460 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
461 if (redir_lval == NULL)
462 {
463 var_redir_stop();
464 return FAIL;
465 }
466
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000467 /* The output is stored in growarray "redir_ga" until redirection ends. */
468 ga_init2(&redir_ga, (int)sizeof(char), 500);
469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100471 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000472 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
474 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200475 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 if (redir_endp != NULL && *redir_endp != NUL)
477 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100478 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100480 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000481 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482 var_redir_stop();
483 return FAIL;
484 }
485
486 /* check if we can write to the variable: set it to or append an empty
487 * string */
488 save_emsg = did_emsg;
489 did_emsg = FALSE;
490 tv.v_type = VAR_STRING;
491 tv.vval.v_string = (char_u *)"";
492 if (append)
493 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
494 else
495 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200496 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000498 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 if (err)
500 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000501 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 var_redir_stop();
503 return FAIL;
504 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000505
506 return OK;
507}
508
509/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000510 * Append "value[value_len]" to the variable set by var_redir_start().
511 * The actual appending is postponed until redirection ends, because the value
512 * appended may in fact be the string we write to, changing it may cause freed
513 * memory to be used:
514 * :redir => foo
515 * :let foo
516 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 */
518 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100519var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522
523 if (redir_lval == NULL)
524 return;
525
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000530
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 {
533 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000534 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000535 }
536 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538}
539
540/*
541 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547 typval_T tv;
548
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200549 if (EVALCMD_BUSY)
550 {
551 redir_lval = NULL;
552 return;
553 }
554
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000555 if (redir_lval != NULL)
556 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000557 /* If there was no error: assign the text to the variable. */
558 if (redir_endp != NULL)
559 {
560 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
561 tv.v_type = VAR_STRING;
562 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 /* Call get_lval() again, if it's inside a Dict or List it may
564 * have changed. */
565 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100566 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200567 if (redir_endp != NULL && redir_lval->ll_name != NULL)
568 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
569 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000570 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000572 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574
Bram Moolenaard23a8232018-02-10 18:45:26 +0100575 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100577 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100689 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100699/*
700 * Call eval1() and give an error message if not done at a lower level.
701 */
702 static int
703eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
704{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100705 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100706 int ret;
707 int did_emsg_before = did_emsg;
708 int called_emsg_before = called_emsg;
709
710 ret = eval1(arg, rettv, evaluate);
711 if (ret == FAIL)
712 {
713 // Report the invalid expression unless the expression evaluation has
714 // been cancelled due to an aborting error, an interrupt, or an
715 // exception, or we already gave a more specific error.
716 // Also check called_emsg for when using assert_fails().
717 if (!aborting() && did_emsg == did_emsg_before
718 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100719 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100720 }
721 return ret;
722}
723
Bram Moolenaar48570482017-10-30 21:48:41 +0100724 static int
725eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
726{
727 char_u *s;
728 int dummy;
729 char_u buf[NUMBUFLEN];
730
731 if (expr->v_type == VAR_FUNC)
732 {
733 s = expr->vval.v_string;
734 if (s == NULL || *s == NUL)
735 return FAIL;
736 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
737 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
738 return FAIL;
739 }
740 else if (expr->v_type == VAR_PARTIAL)
741 {
742 partial_T *partial = expr->vval.v_partial;
743
744 s = partial_name(partial);
745 if (s == NULL || *s == NUL)
746 return FAIL;
747 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
748 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
749 return FAIL;
750 }
751 else
752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100753 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100754 if (s == NULL)
755 return FAIL;
756 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100757 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100758 return FAIL;
759 if (*s != NUL) /* check for trailing chars after expr */
760 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200761 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100762 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100763 return FAIL;
764 }
765 }
766 return OK;
767}
768
769/*
770 * Like eval_to_bool() but using a typval_T instead of a string.
771 * Works for string, funcref and partial.
772 */
773 int
774eval_expr_to_bool(typval_T *expr, int *error)
775{
776 typval_T rettv;
777 int res;
778
779 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
780 {
781 *error = TRUE;
782 return FALSE;
783 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100784 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100785 clear_tv(&rettv);
786 return res;
787}
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789/*
790 * Top level evaluation function, returning a string. If "skip" is TRUE,
791 * only parsing to "nextcmd" is done, without reporting errors. Return
792 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
793 */
794 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100795eval_to_string_skip(
796 char_u *arg,
797 char_u **nextcmd,
798 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 char_u *retval;
802
803 if (skip)
804 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000805 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 retval = NULL;
807 else
808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100809 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000810 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 if (skip)
813 --emsg_skip;
814
815 return retval;
816}
817
818/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000819 * Skip over an expression at "*pp".
820 * Return FAIL for an error, OK otherwise.
821 */
822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000824{
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000826
827 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000828 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000829}
830
831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000833 * When "convert" is TRUE convert a List into a sequence of lines and convert
834 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * Return pointer to allocated memory, or NULL for failure.
836 */
837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838eval_to_string(
839 char_u *arg,
840 char_u **nextcmd,
841 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000846#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 retval = NULL;
852 else
853 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000854 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000855 {
856 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000857 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200858 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200859 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200860 if (tv.vval.v_list->lv_len > 0)
861 ga_append(&ga, NL);
862 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000863 ga_append(&ga, NUL);
864 retval = (char_u *)ga.ga_data;
865 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000866#ifdef FEAT_FLOAT
867 else if (convert && tv.v_type == VAR_FLOAT)
868 {
869 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
870 retval = vim_strsave(numbuf);
871 }
872#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000873 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100874 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877
878 return retval;
879}
880
881/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000882 * Call eval_to_string() without using current local variables and using
883 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 */
885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886eval_to_string_safe(
887 char_u *arg,
888 char_u **nextcmd,
889 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200892 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200894 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000895 if (use_sandbox)
896 ++sandbox;
897 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000898 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000899 if (use_sandbox)
900 --sandbox;
901 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200902 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return retval;
904}
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906/*
907 * Top level evaluation function, returning a number.
908 * Evaluates "expr" silently.
909 * Returns -1 for an error.
910 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200911 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913{
Bram Moolenaar33570922005-01-25 22:26:29 +0000914 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200915 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000916 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 ++emsg_off;
919
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000920 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = -1;
922 else
923 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100924 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927 --emsg_off;
928
929 return retval;
930}
931
Bram Moolenaara40058a2005-07-11 22:42:07 +0000932/*
933 * Prepare v: variable "idx" to be used.
934 * Save the current typeval in "save_tv".
935 * When not used yet add the variable to the v: hashtable.
936 */
937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100938prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000939{
940 *save_tv = vimvars[idx].vv_tv;
941 if (vimvars[idx].vv_type == VAR_UNKNOWN)
942 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
943}
944
945/*
946 * Restore v: variable "idx" to typeval "save_tv".
947 * When no longer defined, remove the variable from the v: hashtable.
948 */
949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100950restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000951{
952 hashitem_T *hi;
953
Bram Moolenaara40058a2005-07-11 22:42:07 +0000954 vimvars[idx].vv_tv = *save_tv;
955 if (vimvars[idx].vv_type == VAR_UNKNOWN)
956 {
957 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
958 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100959 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000960 else
961 hash_remove(&vimvarht, hi);
962 }
963}
964
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000965#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000966/*
967 * Evaluate an expression to a list with suggestions.
968 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000969 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000970 */
971 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000973{
974 typval_T save_val;
975 typval_T rettv;
976 list_T *list = NULL;
977 char_u *p = skipwhite(expr);
978
979 /* Set "v:val" to the bad word. */
980 prepare_vimvar(VV_VAL, &save_val);
981 vimvars[VV_VAL].vv_type = VAR_STRING;
982 vimvars[VV_VAL].vv_str = badword;
983 if (p_verbose == 0)
984 ++emsg_off;
985
986 if (eval1(&p, &rettv, TRUE) == OK)
987 {
988 if (rettv.v_type != VAR_LIST)
989 clear_tv(&rettv);
990 else
991 list = rettv.vval.v_list;
992 }
993
994 if (p_verbose == 0)
995 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000996 restore_vimvar(VV_VAL, &save_val);
997
998 return list;
999}
1000
1001/*
1002 * "list" is supposed to contain two items: a word and a number. Return the
1003 * word in "pp" and the number as the return value.
1004 * Return -1 if anything isn't right.
1005 * Used to get the good word and score from the eval_spell_expr() result.
1006 */
1007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001009{
1010 listitem_T *li;
1011
1012 li = list->lv_first;
1013 if (li == NULL)
1014 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001015 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001016
1017 li = li->li_next;
1018 if (li == NULL)
1019 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001020 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001021}
1022#endif
1023
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001024/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001025 * Top level evaluation function.
1026 * Returns an allocated typval_T with the result.
1027 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001028 */
1029 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001031{
1032 typval_T *tv;
1033
1034 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001035 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001036 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001037
1038 return tv;
1039}
1040
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001043 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001044 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1045 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001046 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049call_vim_function(
1050 char_u *func,
1051 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001052 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001053 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001056 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001058 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001059 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001061 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 if (ret == FAIL)
1063 clear_tv(rettv);
1064
1065 return ret;
1066}
1067
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001068/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001069 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001070 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001071 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1072 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001073 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001074 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001075call_func_retnr(
1076 char_u *func,
1077 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001078 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001079{
1080 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001081 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001082
Bram Moolenaarded27a12018-08-01 19:06:03 +02001083 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001084 return -1;
1085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001086 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001087 clear_tv(&rettv);
1088 return retval;
1089}
1090
1091#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1092 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1093
Bram Moolenaar4f688582007-07-24 12:34:30 +00001094# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001096 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001097 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001098 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1099 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 */
1101 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102call_func_retstr(
1103 char_u *func,
1104 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001105 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001106{
1107 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001108 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109
Bram Moolenaarded27a12018-08-01 19:06:03 +02001110 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001111 return NULL;
1112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001113 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 return retval;
1116}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001117# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001118
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001119/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001120 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001121 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1122 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001123 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001124 */
1125 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001126call_func_retlist(
1127 char_u *func,
1128 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001129 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001130{
1131 typval_T rettv;
1132
Bram Moolenaarded27a12018-08-01 19:06:03 +02001133 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001134 return NULL;
1135
1136 if (rettv.v_type != VAR_LIST)
1137 {
1138 clear_tv(&rettv);
1139 return NULL;
1140 }
1141
1142 return rettv.vval.v_list;
1143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145
Bram Moolenaar05159a02005-02-26 23:04:13 +00001146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#ifdef FEAT_FOLDING
1148/*
1149 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1150 * it in "*cp". Doesn't give error messages.
1151 */
1152 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001153eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
Bram Moolenaar33570922005-01-25 22:26:29 +00001155 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001156 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001158 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1159 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001162 if (use_sandbox)
1163 ++sandbox;
1164 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = 0;
1168 else
1169 {
1170 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 if (tv.v_type == VAR_NUMBER)
1172 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001173 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 retval = 0;
1175 else
1176 {
1177 /* If the result is a string, check if there is a non-digit before
1178 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (!VIM_ISDIGIT(*s) && *s != '-')
1181 *cp = *s++;
1182 retval = atol((char *)s);
1183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001187 if (use_sandbox)
1188 --sandbox;
1189 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001191 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192}
1193#endif
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 * ":let" list all variable values
1197 * ":let var1 var2" list variable values
1198 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001199 * ":let var += expr" assignment command.
1200 * ":let var -= expr" assignment command.
1201 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
1204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001205ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001209 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 int var_count = 0;
1212 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001213 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001214 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001215 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaardb552d602006-03-23 22:59:57 +00001217 argend = skip_var_list(arg, &var_count, &semicolon);
1218 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001219 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001220 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1221 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001222 expr = skipwhite(argend);
1223 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1224 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001226 /*
1227 * ":let" without "=": list variables
1228 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001229 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001230 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001231 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001233 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001236 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001237 list_glob_vars(&first);
1238 list_buf_vars(&first);
1239 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001240 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001241 list_script_vars(&first);
1242 list_func_vars(&first);
1243 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 eap->nextcmd = check_nextcmd(arg);
1246 }
1247 else
1248 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 op[0] = '=';
1250 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001251 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001252 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001253 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1254 op[0] = *expr; /* +=, -= or .= */
1255 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001256 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001257 else
1258 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (eap->skip)
1261 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001262 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (eap->skip)
1264 {
1265 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 --emsg_skip;
1268 }
1269 else if (i != FAIL)
1270 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 }
1276}
1277
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001278/*
1279 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1280 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 * When "nextchars" is not NULL it points to a string with characters that
1282 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1283 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001284 * Returns OK or FAIL;
1285 */
1286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287ex_let_vars(
1288 char_u *arg_start,
1289 typval_T *tv,
1290 int copy, /* copy values from "tv", don't move */
1291 int semicolon, /* from skip_var_list() */
1292 int var_count, /* from skip_var_list() */
1293 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001294{
1295 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001297 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 listitem_T *item;
1299 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001300
1301 if (*arg != '[')
1302 {
1303 /*
1304 * ":let var = expr" or ":for var in list"
1305 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001306 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307 return FAIL;
1308 return OK;
1309 }
1310
1311 /*
1312 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1313 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001314 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001316 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001317 return FAIL;
1318 }
1319
1320 i = list_len(l);
1321 if (semicolon == 0 && var_count < i)
1322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001323 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 return FAIL;
1325 }
1326 if (var_count - semicolon > i)
1327 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001328 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001329 return FAIL;
1330 }
1331
1332 item = l->lv_first;
1333 while (*arg != ']')
1334 {
1335 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 item = item->li_next;
1338 if (arg == NULL)
1339 return FAIL;
1340
1341 arg = skipwhite(arg);
1342 if (*arg == ';')
1343 {
1344 /* Put the rest of the list (may be empty) in the var after ';'.
1345 * Create a new list for this. */
1346 l = list_alloc();
1347 if (l == NULL)
1348 return FAIL;
1349 while (item != NULL)
1350 {
1351 list_append_tv(l, &item->li_tv);
1352 item = item->li_next;
1353 }
1354
1355 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001356 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001357 ltv.vval.v_list = l;
1358 l->lv_refcount = 1;
1359
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1361 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001362 clear_tv(&ltv);
1363 if (arg == NULL)
1364 return FAIL;
1365 break;
1366 }
1367 else if (*arg != ',' && *arg != ']')
1368 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001369 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001370 return FAIL;
1371 }
1372 }
1373
1374 return OK;
1375}
1376
1377/*
1378 * Skip over assignable variable "var" or list of variables "[var, var]".
1379 * Used for ":let varvar = expr" and ":for varvar in expr".
1380 * For "[var, var]" increment "*var_count" for each variable.
1381 * for "[var, var; var]" set "semicolon".
1382 * Return NULL for an error.
1383 */
1384 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001385skip_var_list(
1386 char_u *arg,
1387 int *var_count,
1388 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001389{
1390 char_u *p, *s;
1391
1392 if (*arg == '[')
1393 {
1394 /* "[var, var]": find the matching ']'. */
1395 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001396 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001397 {
1398 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1399 s = skip_var_one(p);
1400 if (s == p)
1401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 return NULL;
1404 }
1405 ++*var_count;
1406
1407 p = skipwhite(s);
1408 if (*p == ']')
1409 break;
1410 else if (*p == ';')
1411 {
1412 if (*semicolon == 1)
1413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 return NULL;
1416 }
1417 *semicolon = 1;
1418 }
1419 else if (*p != ',')
1420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001421 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 return NULL;
1423 }
1424 }
1425 return p + 1;
1426 }
1427 else
1428 return skip_var_one(arg);
1429}
1430
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001431/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001432 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001433 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001434 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001436skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001438 if (*arg == '@' && arg[1] != NUL)
1439 return arg + 2;
1440 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1441 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442}
1443
Bram Moolenaara7043832005-01-21 11:56:39 +00001444/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001445 * List variables for hashtab "ht" with prefix "prefix".
1446 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001447 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001449list_hashtable_vars(
1450 hashtab_T *ht,
1451 char_u *prefix,
1452 int empty,
1453 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001454{
Bram Moolenaar33570922005-01-25 22:26:29 +00001455 hashitem_T *hi;
1456 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001457 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001458 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001459
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001460 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001461 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1462 {
1463 if (!HASHITEM_EMPTY(hi))
1464 {
1465 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001467
1468 // apply :filter /pat/ to variable name
1469 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1470 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1471 if (message_filtered(buf))
1472 continue;
1473
Bram Moolenaar33570922005-01-25 22:26:29 +00001474 if (empty || di->di_tv.v_type != VAR_STRING
1475 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001477 }
1478 }
1479}
1480
1481/*
1482 * List global variables.
1483 */
1484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001486{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001487 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001488}
1489
1490/*
1491 * List buffer variables.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001495{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001496 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001497 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001498}
1499
1500/*
1501 * List window variables.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001505{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001506 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001507 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001508}
1509
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001510/*
1511 * List tab page variables.
1512 */
1513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001515{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001516 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001517 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001518}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001519
Bram Moolenaara7043832005-01-21 11:56:39 +00001520/*
1521 * List Vim variables.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001526 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527}
1528
1529/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001530 * List script-local variables, if there is a script.
1531 */
1532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001534{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001535 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1536 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001537 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001538}
1539
1540/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001541 * List variables in "arg".
1542 */
1543 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001545{
1546 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001547 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001549 char_u *name_start;
1550 char_u *arg_subsc;
1551 char_u *tofree;
1552 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553
1554 while (!ends_excmd(*arg) && !got_int)
1555 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001556 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001558 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001559 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 {
1561 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001562 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 break;
1564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001568 /* get_name_len() takes care of expanding curly braces */
1569 name_start = name = arg;
1570 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1571 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001573 /* This is mainly to keep test 49 working: when expanding
1574 * curly braces fails overrule the exception error message. */
1575 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001578 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 break;
1580 }
1581 error = TRUE;
1582 }
1583 else
1584 {
1585 if (tofree != NULL)
1586 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001587 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589 else
1590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001591 /* handle d.key, l[idx], f(expr) */
1592 arg_subsc = arg;
1593 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001594 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001596 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001597 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001600 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001601 case 'g': list_glob_vars(first); break;
1602 case 'b': list_buf_vars(first); break;
1603 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001604 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001605 case 'v': list_vim_vars(first); break;
1606 case 's': list_script_vars(first); break;
1607 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001608 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001609 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001610 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001611 }
1612 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001613 {
1614 char_u numbuf[NUMBUFLEN];
1615 char_u *tf;
1616 int c;
1617 char_u *s;
1618
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001619 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001620 c = *arg;
1621 *arg = NUL;
1622 list_one_var_a((char_u *)"",
1623 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001624 tv.v_type,
1625 s == NULL ? (char_u *)"" : s,
1626 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 *arg = c;
1628 vim_free(tf);
1629 }
1630 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 }
1633 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634
1635 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001637
1638 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 }
1640
1641 return arg;
1642}
1643
1644/*
1645 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1646 * Returns a pointer to the char just after the var name.
1647 * Returns NULL if there is an error.
1648 */
1649 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001650ex_let_one(
1651 char_u *arg, /* points to variable name */
1652 typval_T *tv, /* value to assign to variable */
1653 int copy, /* copy value from "tv" */
1654 char_u *endchars, /* valid chars after variable name or NULL */
1655 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656{
1657 int c1;
1658 char_u *name;
1659 char_u *p;
1660 char_u *arg_end = NULL;
1661 int len;
1662 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664
1665 /*
1666 * ":let $VAR = expr": Set environment variable.
1667 */
1668 if (*arg == '$')
1669 {
1670 /* Find the end of the name. */
1671 ++arg;
1672 name = arg;
1673 len = get_env_len(&arg);
1674 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001675 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 else
1677 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001679 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001682 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001683 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 {
1685 c1 = name[len];
1686 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001687 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001688 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001689 {
1690 int mustfree = FALSE;
1691 char_u *s = vim_getenv(name, &mustfree);
1692
1693 if (s != NULL)
1694 {
1695 p = tofree = concat_str(s, p);
1696 if (mustfree)
1697 vim_free(s);
1698 }
1699 }
1700 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001703 if (STRICMP(name, "HOME") == 0)
1704 init_homedir();
1705 else if (didset_vim && STRICMP(name, "VIM") == 0)
1706 didset_vim = FALSE;
1707 else if (didset_vimruntime
1708 && STRICMP(name, "VIMRUNTIME") == 0)
1709 didset_vimruntime = FALSE;
1710 arg_end = arg;
1711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 }
1715 }
1716 }
1717
1718 /*
1719 * ":let &option = expr": Set option value.
1720 * ":let &l:option = expr": Set local option value.
1721 * ":let &g:option = expr": Set global option value.
1722 */
1723 else if (*arg == '&')
1724 {
1725 /* Find the end of the name. */
1726 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 if (p == NULL || (endchars != NULL
1728 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001729 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 else
1731 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 long n;
1733 int opt_type;
1734 long numval;
1735 char_u *stringval = NULL;
1736 char_u *s;
1737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 c1 = *p;
1739 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001741 n = (long)tv_get_number(tv);
1742 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001743 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001744 {
1745 opt_type = get_option_value(arg, &numval,
1746 &stringval, opt_flags);
1747 if ((opt_type == 1 && *op == '.')
1748 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001750 semsg(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001751 s = NULL; /* don't set the value */
1752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 else
1754 {
1755 if (opt_type == 1) /* number */
1756 {
1757 if (*op == '+')
1758 n = numval + n;
1759 else
1760 n = numval - n;
1761 }
1762 else if (opt_type == 0 && stringval != NULL) /* string */
1763 {
1764 s = concat_str(stringval, s);
1765 vim_free(stringval);
1766 stringval = s;
1767 }
1768 }
1769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 if (s != NULL)
1771 {
1772 set_option_value(arg, n, s, opt_flags);
1773 arg_end = p;
1774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 }
1778 }
1779
1780 /*
1781 * ":let @r = expr": Set register contents.
1782 */
1783 else if (*arg == '@')
1784 {
1785 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001786 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001788 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001790 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 else
1792 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001793 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 char_u *s;
1795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001796 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001798 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001799 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 if (s != NULL)
1801 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001802 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 vim_free(s);
1804 }
1805 }
1806 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001807 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001809 arg_end = arg + 1;
1810 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001811 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 }
1813 }
1814
1815 /*
1816 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001819 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001821 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001823 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001826 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001827 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001828 else
1829 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 arg_end = p;
1832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001834 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 }
1836
1837 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839
1840 return arg_end;
1841}
1842
1843/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001844 * Get an lval: variable, Dict item or List item that can be assigned a value
1845 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1846 * "name.key", "name.key[expr]" etc.
1847 * Indexing only works if "name" is an existing List or Dictionary.
1848 * "name" points to the start of the name.
1849 * If "rettv" is not NULL it points to the value to be assigned.
1850 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1851 * wrong; must end in space or cmd separator.
1852 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001853 * flags:
1854 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001855 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001856 * GLV_NO_AUTOLOAD: do not use script autoloading
1857 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001858 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001859 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001863get_lval(
1864 char_u *name,
1865 typval_T *rettv,
1866 lval_T *lp,
1867 int unlet,
1868 int skip,
1869 int flags, /* GLV_ values */
1870 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001873 char_u *expr_start, *expr_end;
1874 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001875 dictitem_T *v;
1876 typval_T var1;
1877 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001880 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001881 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001883 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001885 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001887
1888 if (skip)
1889 {
1890 /* When skipping just find the end of the name. */
1891 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001892 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001893 }
1894
1895 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001896 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 if (expr_start != NULL)
1898 {
1899 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001900 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 && *p != '[' && *p != '.')
1902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 return NULL;
1905 }
1906
1907 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1908 if (lp->ll_exp_name == NULL)
1909 {
1910 /* Report an invalid expression in braces, unless the
1911 * expression evaluation has been cancelled due to an
1912 * aborting error, an interrupt, or an exception. */
1913 if (!aborting() && !quiet)
1914 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001915 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001916 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 return NULL;
1918 }
1919 }
1920 lp->ll_name = lp->ll_exp_name;
1921 }
1922 else
1923 lp->ll_name = name;
1924
1925 /* Without [idx] or .key we are done. */
1926 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1927 return p;
1928
1929 cc = *p;
1930 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001931 /* Only pass &ht when we would write to the variable, it prevents autoload
1932 * as well. */
1933 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1934 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001936 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 if (v == NULL)
1939 return NULL;
1940
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 /*
1942 * Loop until no more [idx] or .key is following.
1943 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001945 var1.v_type = VAR_UNKNOWN;
1946 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001947 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1950 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001951 && lp->ll_tv->vval.v_dict != NULL)
1952 && !(lp->ll_tv->v_type == VAR_BLOB
1953 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001956 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001962 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001965
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 len = -1;
1967 if (*p == '.')
1968 {
1969 key = p + 1;
1970 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1971 ;
1972 if (len == 0)
1973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 }
1978 p = key + len;
1979 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001980 else
1981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001982 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 if (*p == ':')
1985 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001986 else
1987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 empty1 = FALSE;
1989 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001991 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 {
1993 /* not a number or string */
1994 clear_tv(&var1);
1995 return NULL;
1996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001997 }
1998
1999 /* Optionally get the second index [ :expr]. */
2000 if (*p == ':')
2001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002005 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002006 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002008 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002009 if (rettv != NULL
2010 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002011 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002012 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002013 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002017 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 }
2020 p = skipwhite(p + 1);
2021 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 else
2024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2027 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002028 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002031 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 {
2033 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002034 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002035 clear_tv(&var2);
2036 return NULL;
2037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002039 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002043
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002047 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002048 clear_tv(&var1);
2049 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 }
2052
2053 /* Skip to past ']'. */
2054 ++p;
2055 }
2056
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002058 {
2059 if (len == -1)
2060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002062 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002063 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002065 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 }
2068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 lp->ll_list = NULL;
2070 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002071 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002072
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002073 /* When assigning to a scope dictionary check that a function and
2074 * variable name is valid (only variable name unless it is l: or
2075 * g: dictionary). Disallow overwriting a builtin function. */
2076 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002077 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002078 int prevval;
2079 int wrong;
2080
2081 if (len != -1)
2082 {
2083 prevval = key[len];
2084 key[len] = NUL;
2085 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002086 else
2087 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002088 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2089 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002090 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002091 || !valid_varname(key);
2092 if (len != -1)
2093 key[len] = prevval;
2094 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002095 return NULL;
2096 }
2097
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002100 /* Can't add "v:" variable. */
2101 if (lp->ll_dict == &vimvardict)
2102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 semsg(_(e_illvar), name);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002104 return NULL;
2105 }
2106
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002107 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002111 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002112 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 }
2115 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002119 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002121 p = NULL;
2122 break;
2123 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002124 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002125 else if ((flags & GLV_READ_ONLY) == 0
2126 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002127 {
2128 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002129 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002130 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002131
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002132 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002135 else if (lp->ll_tv->v_type == VAR_BLOB)
2136 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002137 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2138
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002139 /*
2140 * Get the number and item for the only or first index of the List.
2141 */
2142 if (empty1)
2143 lp->ll_n1 = 0;
2144 else
2145 // is number or string
2146 lp->ll_n1 = (long)tv_get_number(&var1);
2147 clear_tv(&var1);
2148
2149 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002150 || lp->ll_n1 > bloblen
2151 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002152 {
2153 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002154 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002155 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002156 return NULL;
2157 }
2158 if (lp->ll_range && !lp->ll_empty2)
2159 {
2160 lp->ll_n2 = (long)tv_get_number(&var2);
2161 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002162 if (lp->ll_n2 < 0
2163 || lp->ll_n2 >= bloblen
2164 || lp->ll_n2 < lp->ll_n1)
2165 {
2166 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002167 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002168 return NULL;
2169 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002170 }
2171 lp->ll_blob = lp->ll_tv->vval.v_blob;
2172 lp->ll_tv = NULL;
2173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002174 else
2175 {
2176 /*
2177 * Get the number and item for the only or first index of the List.
2178 */
2179 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002181 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002182 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002183 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002184 clear_tv(&var1);
2185
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 lp->ll_list = lp->ll_tv->vval.v_list;
2188 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2189 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002191 if (lp->ll_n1 < 0)
2192 {
2193 lp->ll_n1 = 0;
2194 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2195 }
2196 }
2197 if (lp->ll_li == NULL)
2198 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002199 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002200 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 }
2204
2205 /*
2206 * May need to find the item or absolute index for the second
2207 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 * When no index given: "lp->ll_empty2" is TRUE.
2209 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002213 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002214 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002215 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002220 {
2221 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002222 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002224 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002226 }
2227
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2229 if (lp->ll_n1 < 0)
2230 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2231 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002232 {
2233 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002234 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002236 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002237 }
2238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 }
2242
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002243 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 return p;
2245}
2246
2247/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002251clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252{
2253 vim_free(lp->ll_exp_name);
2254 vim_free(lp->ll_newkey);
2255}
2256
2257/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002258 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002260 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 */
2262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002263set_var_lval(
2264 lval_T *lp,
2265 char_u *endp,
2266 typval_T *rettv,
2267 int copy,
2268 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269{
2270 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002271 listitem_T *ri;
2272 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273
2274 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002276 cc = *endp;
2277 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002278 if (lp->ll_blob != NULL)
2279 {
2280 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002281
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 if (op != NULL && *op != '=')
2283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002285 return;
2286 }
2287
2288 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2289 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002290 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002291
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002292 if (lp->ll_empty2)
2293 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2294
2295 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002297 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002298 return;
2299 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 if (lp->ll_empty2)
2301 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002302
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002303 ir = 0;
2304 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2305 blob_set(lp->ll_blob, il,
2306 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002307 }
2308 else
2309 {
2310 val = (int)tv_get_number_chk(rettv, &error);
2311 if (!error)
2312 {
2313 garray_T *gap = &lp->ll_blob->bv_ga;
2314
2315 // Allow for appending a byte. Setting a byte beyond
2316 // the end is an error otherwise.
2317 if (lp->ll_n1 < gap->ga_len
2318 || (lp->ll_n1 == gap->ga_len
2319 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2320 {
2321 blob_set(lp->ll_blob, lp->ll_n1, val);
2322 if (lp->ll_n1 == gap->ga_len)
2323 ++gap->ga_len;
2324 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002325 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002326 }
2327 }
2328 }
2329 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002331 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332
Bram Moolenaar79518e22017-02-17 16:31:35 +01002333 /* handle +=, -= and .= */
2334 di = NULL;
2335 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2336 &tv, &di, TRUE, FALSE) == OK)
2337 {
2338 if ((di == NULL
2339 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2340 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2341 FALSE)))
2342 && tv_op(&tv, rettv, op) == OK)
2343 set_var(lp->ll_name, &tv, FALSE);
2344 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002347 else
2348 set_var(lp->ll_name, rettv, copy);
2349 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002351 else if (tv_check_lock(lp->ll_newkey == NULL
2352 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002353 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002354 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 else if (lp->ll_range)
2356 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002357 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002358 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002359
2360 /*
2361 * Check whether any of the list items is locked
2362 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002363 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002364 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002365 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002366 return;
2367 ri = ri->li_next;
2368 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2369 break;
2370 ll_li = ll_li->li_next;
2371 ++ll_n1;
2372 }
2373
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 /*
2375 * Assign the List values to the list items.
2376 */
2377 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 if (op != NULL && *op != '=')
2380 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2381 else
2382 {
2383 clear_tv(&lp->ll_li->li_tv);
2384 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2385 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 ri = ri->li_next;
2387 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2388 break;
2389 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002392 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 ri = NULL;
2395 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002396 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002397 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 lp->ll_li = lp->ll_li->li_next;
2399 ++lp->ll_n1;
2400 }
2401 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002402 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 else if (lp->ll_empty2
2404 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002406 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 }
2408 else
2409 {
2410 /*
2411 * Assign to a List or Dictionary item.
2412 */
2413 if (lp->ll_newkey != NULL)
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && *op != '=')
2416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002417 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 return;
2419 }
2420
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002422 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (di == NULL)
2424 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002425 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2426 {
2427 vim_free(di);
2428 return;
2429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 else if (op != NULL && *op != '=')
2433 {
2434 tv_op(lp->ll_tv, rettv, op);
2435 return;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 /*
2441 * Assign the value to the variable or list item.
2442 */
2443 if (copy)
2444 copy_tv(rettv, lp->ll_tv);
2445 else
2446 {
2447 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002448 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 }
2451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452}
2453
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2456 * Returns OK or FAIL.
2457 */
2458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002459tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002461 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 char_u numbuf[NUMBUFLEN];
2463 char_u *s;
2464
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002465 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2466 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2467 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 {
2469 switch (tv1->v_type)
2470 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002471 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 case VAR_DICT:
2473 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002474 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002475 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002476 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002477 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 break;
2479
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002480 case VAR_BLOB:
2481 if (*op != '+' || tv2->v_type != VAR_BLOB)
2482 break;
2483 // BLOB += BLOB
2484 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2485 {
2486 blob_T *b1 = tv1->vval.v_blob;
2487 blob_T *b2 = tv2->vval.v_blob;
2488 int i, len = blob_len(b2);
2489 for (i = 0; i < len; i++)
2490 ga_append(&b1->bv_ga, blob_get(b2, i));
2491 }
2492 return OK;
2493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 case VAR_LIST:
2495 if (*op != '+' || tv2->v_type != VAR_LIST)
2496 break;
2497 /* List += List */
2498 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2499 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2500 return OK;
2501
2502 case VAR_NUMBER:
2503 case VAR_STRING:
2504 if (tv2->v_type == VAR_LIST)
2505 break;
2506 if (*op == '+' || *op == '-')
2507 {
2508 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002509 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002510#ifdef FEAT_FLOAT
2511 if (tv2->v_type == VAR_FLOAT)
2512 {
2513 float_T f = n;
2514
2515 if (*op == '+')
2516 f += tv2->vval.v_float;
2517 else
2518 f -= tv2->vval.v_float;
2519 clear_tv(tv1);
2520 tv1->v_type = VAR_FLOAT;
2521 tv1->vval.v_float = f;
2522 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524#endif
2525 {
2526 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002527 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002528 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002529 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002530 clear_tv(tv1);
2531 tv1->v_type = VAR_NUMBER;
2532 tv1->vval.v_number = n;
2533 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 }
2535 else
2536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 if (tv2->v_type == VAR_FLOAT)
2538 break;
2539
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002540 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 s = tv_get_string(tv1);
2542 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 clear_tv(tv1);
2544 tv1->v_type = VAR_STRING;
2545 tv1->vval.v_string = s;
2546 }
2547 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002548
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002549 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002550#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002551 {
2552 float_T f;
2553
2554 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2555 && tv2->v_type != VAR_NUMBER
2556 && tv2->v_type != VAR_STRING))
2557 break;
2558 if (tv2->v_type == VAR_FLOAT)
2559 f = tv2->vval.v_float;
2560 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002561 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562 if (*op == '+')
2563 tv1->vval.v_float += f;
2564 else
2565 tv1->vval.v_float -= f;
2566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002567#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002568 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 }
2570 }
2571
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002572 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002573 return FAIL;
2574}
2575
2576/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002577 * Evaluate the expression used in a ":for var in expr" command.
2578 * "arg" points to "var".
2579 * Set "*errp" to TRUE for an error, FALSE otherwise;
2580 * Return a pointer that holds the info. Null when there is an error.
2581 */
2582 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583eval_for_line(
2584 char_u *arg,
2585 int *errp,
2586 char_u **nextcmdp,
2587 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002588{
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002590 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 typval_T tv;
2592 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002593 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594
2595 *errp = TRUE; /* default: there is an error */
2596
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002598 if (fi == NULL)
2599 return NULL;
2600
2601 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2602 if (expr == NULL)
2603 return fi;
2604
2605 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002606 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002609 return fi;
2610 }
2611
2612 if (skip)
2613 ++emsg_skip;
2614 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2615 {
2616 *errp = FALSE;
2617 if (!skip)
2618 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002619 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002620 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002621 l = tv.vval.v_list;
2622 if (l == NULL)
2623 {
2624 // a null list is like an empty list: do nothing
2625 clear_tv(&tv);
2626 }
2627 else
2628 {
2629 // No need to increment the refcount, it's already set for
2630 // the list being used in "tv".
2631 fi->fi_list = l;
2632 list_add_watch(l, &fi->fi_lw);
2633 fi->fi_lw.lw_item = l->lv_first;
2634 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002635 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002636 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002637 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002638 b = tv.vval.v_blob;
2639 if (b == NULL)
2640 clear_tv(&tv);
2641 else
2642 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002643 // No need to increment the refcount, it's already set for
2644 // the blob being used in "tv".
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002645 fi->fi_blob = b;
2646 fi->fi_bi = 0;
2647 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002648 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649 else
2650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002652 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002653 }
2654 }
2655 }
2656 if (skip)
2657 --emsg_skip;
2658
2659 return fi;
2660}
2661
2662/*
2663 * Use the first item in a ":for" list. Advance to the next.
2664 * Assign the values to the variable (list). "arg" points to the first one.
2665 * Return TRUE when a valid item was found, FALSE when at end of list or
2666 * something wrong.
2667 */
2668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002669next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002670{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002671 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002674
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002675 if (fi->fi_blob != NULL)
2676 {
2677 typval_T tv;
2678
2679 if (fi->fi_bi >= blob_len(fi->fi_blob))
2680 return FALSE;
2681 tv.v_type = VAR_NUMBER;
2682 tv.v_lock = VAR_FIXED;
2683 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2684 ++fi->fi_bi;
2685 return ex_let_vars(arg, &tv, TRUE,
2686 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2687 }
2688
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002689 item = fi->fi_lw.lw_item;
2690 if (item == NULL)
2691 result = FALSE;
2692 else
2693 {
2694 fi->fi_lw.lw_item = item->li_next;
2695 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2696 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2697 }
2698 return result;
2699}
2700
2701/*
2702 * Free the structure used to store info used by ":for".
2703 */
2704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002705free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706{
Bram Moolenaar33570922005-01-25 22:26:29 +00002707 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002708
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002709 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002710 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002712 list_unref(fi->fi_list);
2713 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002714 if (fi != NULL && fi->fi_blob != NULL)
2715 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002716 vim_free(fi);
2717}
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2720
2721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002722set_context_for_expression(
2723 expand_T *xp,
2724 char_u *arg,
2725 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 int got_eq = FALSE;
2728 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002729 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731 if (cmdidx == CMD_let)
2732 {
2733 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002734 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002735 {
2736 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002737 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738 {
2739 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002740 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002741 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742 break;
2743 }
2744 return;
2745 }
2746 }
2747 else
2748 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2749 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 while ((xp->xp_pattern = vim_strpbrk(arg,
2751 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2752 {
2753 c = *xp->xp_pattern;
2754 if (c == '&')
2755 {
2756 c = xp->xp_pattern[1];
2757 if (c == '&')
2758 {
2759 ++xp->xp_pattern;
2760 xp->xp_context = cmdidx != CMD_let || got_eq
2761 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2762 }
2763 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002766 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2767 xp->xp_pattern += 2;
2768
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else if (c == '$')
2772 {
2773 /* environment variable */
2774 xp->xp_context = EXPAND_ENV_VARS;
2775 }
2776 else if (c == '=')
2777 {
2778 got_eq = TRUE;
2779 xp->xp_context = EXPAND_EXPRESSION;
2780 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002781 else if (c == '#'
2782 && xp->xp_context == EXPAND_EXPRESSION)
2783 {
2784 /* Autoload function/variable contains '#'. */
2785 break;
2786 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002787 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 && xp->xp_context == EXPAND_FUNCTIONS
2789 && vim_strchr(xp->xp_pattern, '(') == NULL)
2790 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002791 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 break;
2793 }
2794 else if (cmdidx != CMD_let || got_eq)
2795 {
2796 if (c == '"') /* string */
2797 {
2798 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2799 if (c == '\\' && xp->xp_pattern[1] != NUL)
2800 ++xp->xp_pattern;
2801 xp->xp_context = EXPAND_NOTHING;
2802 }
2803 else if (c == '\'') /* literal string */
2804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2807 /* skip */ ;
2808 xp->xp_context = EXPAND_NOTHING;
2809 }
2810 else if (c == '|')
2811 {
2812 if (xp->xp_pattern[1] == '|')
2813 {
2814 ++xp->xp_pattern;
2815 xp->xp_context = EXPAND_EXPRESSION;
2816 }
2817 else
2818 xp->xp_context = EXPAND_COMMANDS;
2819 }
2820 else
2821 xp->xp_context = EXPAND_EXPRESSION;
2822 }
2823 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002824 /* Doesn't look like something valid, expand as an expression
2825 * anyway. */
2826 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 arg = xp->xp_pattern;
2828 if (*arg != NUL)
2829 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2830 /* skip */ ;
2831 }
2832 xp->xp_pattern = arg;
2833}
2834
2835#endif /* FEAT_CMDL_COMPL */
2836
2837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 * ":unlet[!] var1 ... " command.
2839 */
2840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002843 ex_unletlock(eap, eap->arg, 0);
2844}
2845
2846/*
2847 * ":lockvar" and ":unlockvar" commands
2848 */
2849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002850ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002853 int deep = 2;
2854
2855 if (eap->forceit)
2856 deep = -1;
2857 else if (vim_isdigit(*arg))
2858 {
2859 deep = getdigits(&arg);
2860 arg = skipwhite(arg);
2861 }
2862
2863 ex_unletlock(eap, arg, deep);
2864}
2865
2866/*
2867 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2868 */
2869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002870ex_unletlock(
2871 exarg_T *eap,
2872 char_u *argstart,
2873 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874{
2875 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002878 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 do
2881 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002882 if (*arg == '$')
2883 {
2884 char_u *name = ++arg;
2885
2886 if (get_env_len(&arg) == 0)
2887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002888 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002889 return;
2890 }
2891 vim_unsetenv(name);
2892 arg = skipwhite(arg);
2893 continue;
2894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002897 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002898 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (lv.ll_name == NULL)
2900 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002901 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (name_end != NULL)
2905 {
2906 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002907 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 }
2909 if (!(eap->skip || error))
2910 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 break;
2912 }
2913
2914 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 {
2916 if (eap->cmdidx == CMD_unlet)
2917 {
2918 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2919 error = TRUE;
2920 }
2921 else
2922 {
2923 if (do_lock_var(&lv, name_end, deep,
2924 eap->cmdidx == CMD_lockvar) == FAIL)
2925 error = TRUE;
2926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 if (!eap->skip)
2930 clear_lval(&lv);
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 arg = skipwhite(name_end);
2933 } while (!ends_excmd(*arg));
2934
2935 eap->nextcmd = check_nextcmd(arg);
2936}
2937
Bram Moolenaar8c711452005-01-14 21:53:12 +00002938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939do_unlet_var(
2940 lval_T *lp,
2941 char_u *name_end,
2942 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002943{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 int ret = OK;
2945 int cc;
2946
2947 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 cc = *name_end;
2950 *name_end = NUL;
2951
2952 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002953 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002956 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002957 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002958 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002959 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002960 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 else if (lp->ll_range)
2963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002966 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002967
2968 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2969 {
2970 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002971 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 return FAIL;
2973 ll_li = li;
2974 ++ll_n1;
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976
2977 /* Delete a range of List items. */
2978 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2979 {
2980 li = lp->ll_li->li_next;
2981 listitem_remove(lp->ll_list, lp->ll_li);
2982 lp->ll_li = li;
2983 ++lp->ll_n1;
2984 }
2985 }
2986 else
2987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /* unlet a List item. */
2990 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002993 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 }
2995
2996 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999/*
3000 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003001 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
3003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003004do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 hashtab_T *ht;
3007 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003008 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003009 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003010 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 ht = find_var_ht(name, &varname);
3013 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003016 if (d == NULL)
3017 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018 if (ht == &globvarht)
3019 d = &globvardict;
3020 else if (ht == &compat_hashtab)
3021 d = &vimvardict;
3022 else
3023 {
3024 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3025 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3026 }
3027 if (d == NULL)
3028 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003029 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 return FAIL;
3031 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003033 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003034 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003035 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003036 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003037 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003038 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003039 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003040 || var_check_ro(di->di_flags, name, FALSE)
3041 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003042 return FAIL;
3043
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003044 delete_var(ht, hi);
3045 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003048 if (forceit)
3049 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 return FAIL;
3052}
3053
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003054/*
3055 * Lock or unlock variable indicated by "lp".
3056 * "deep" is the levels to go (-1 for unlimited);
3057 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3058 */
3059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060do_lock_var(
3061 lval_T *lp,
3062 char_u *name_end,
3063 int deep,
3064 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003065{
3066 int ret = OK;
3067 int cc;
3068 dictitem_T *di;
3069
3070 if (deep == 0) /* nothing to do */
3071 return OK;
3072
3073 if (lp->ll_tv == NULL)
3074 {
3075 cc = *name_end;
3076 *name_end = NUL;
3077
3078 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003079 di = find_var(lp->ll_name, NULL, TRUE);
3080 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003081 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003082 else if ((di->di_flags & DI_FLAGS_FIX)
3083 && di->di_tv.v_type != VAR_DICT
3084 && di->di_tv.v_type != VAR_LIST)
3085 /* For historic reasons this error is not given for a list or dict.
3086 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003087 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003088 else
3089 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003090 if (lock)
3091 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003092 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003093 di->di_flags &= ~DI_FLAGS_LOCK;
3094 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003095 }
3096 *name_end = cc;
3097 }
3098 else if (lp->ll_range)
3099 {
3100 listitem_T *li = lp->ll_li;
3101
3102 /* (un)lock a range of List items. */
3103 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3104 {
3105 item_lock(&li->li_tv, deep, lock);
3106 li = li->li_next;
3107 ++lp->ll_n1;
3108 }
3109 }
3110 else if (lp->ll_list != NULL)
3111 /* (un)lock a List item. */
3112 item_lock(&lp->ll_li->li_tv, deep, lock);
3113 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003114 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 item_lock(&lp->ll_di->di_tv, deep, lock);
3116
3117 return ret;
3118}
3119
3120/*
3121 * Lock or unlock an item. "deep" is nr of levels to go.
3122 */
3123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003124item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003125{
3126 static int recurse = 0;
3127 list_T *l;
3128 listitem_T *li;
3129 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003130 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003131 hashitem_T *hi;
3132 int todo;
3133
3134 if (recurse >= DICT_MAXNEST)
3135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003136 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003137 return;
3138 }
3139 if (deep == 0)
3140 return;
3141 ++recurse;
3142
3143 /* lock/unlock the item itself */
3144 if (lock)
3145 tv->v_lock |= VAR_LOCKED;
3146 else
3147 tv->v_lock &= ~VAR_LOCKED;
3148
3149 switch (tv->v_type)
3150 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003151 case VAR_UNKNOWN:
3152 case VAR_NUMBER:
3153 case VAR_STRING:
3154 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003155 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003156 case VAR_FLOAT:
3157 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003158 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003159 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003160 break;
3161
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003162 case VAR_BLOB:
3163 if ((b = tv->vval.v_blob) != NULL)
3164 {
3165 if (lock)
3166 b->bv_lock |= VAR_LOCKED;
3167 else
3168 b->bv_lock &= ~VAR_LOCKED;
3169 }
3170 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003171 case VAR_LIST:
3172 if ((l = tv->vval.v_list) != NULL)
3173 {
3174 if (lock)
3175 l->lv_lock |= VAR_LOCKED;
3176 else
3177 l->lv_lock &= ~VAR_LOCKED;
3178 if (deep < 0 || deep > 1)
3179 /* recursive: lock/unlock the items the List contains */
3180 for (li = l->lv_first; li != NULL; li = li->li_next)
3181 item_lock(&li->li_tv, deep - 1, lock);
3182 }
3183 break;
3184 case VAR_DICT:
3185 if ((d = tv->vval.v_dict) != NULL)
3186 {
3187 if (lock)
3188 d->dv_lock |= VAR_LOCKED;
3189 else
3190 d->dv_lock &= ~VAR_LOCKED;
3191 if (deep < 0 || deep > 1)
3192 {
3193 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003194 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003195 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3196 {
3197 if (!HASHITEM_EMPTY(hi))
3198 {
3199 --todo;
3200 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3201 }
3202 }
3203 }
3204 }
3205 }
3206 --recurse;
3207}
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3210/*
3211 * Delete all "menutrans_" variables.
3212 */
3213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003217 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003220 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003222 {
3223 if (!HASHITEM_EMPTY(hi))
3224 {
3225 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3227 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003228 }
3229 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231}
3232#endif
3233
3234#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3235
3236/*
3237 * Local string buffer for the next two functions to store a variable name
3238 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3239 * get_user_var_name().
3240 */
3241
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242static char_u *varnamebuf = NULL;
3243static int varnamebuflen = 0;
3244
3245/*
3246 * Function to concatenate a prefix and a variable name.
3247 */
3248 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003249cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 int len;
3252
3253 len = (int)STRLEN(name) + 3;
3254 if (len > varnamebuflen)
3255 {
3256 vim_free(varnamebuf);
3257 len += 10; /* some additional space */
3258 varnamebuf = alloc(len);
3259 if (varnamebuf == NULL)
3260 {
3261 varnamebuflen = 0;
3262 return NULL;
3263 }
3264 varnamebuflen = len;
3265 }
3266 *varnamebuf = prefix;
3267 varnamebuf[1] = ':';
3268 STRCPY(varnamebuf + 2, name);
3269 return varnamebuf;
3270}
3271
3272/*
3273 * Function given to ExpandGeneric() to obtain the list of user defined
3274 * (global/buffer/window/built-in) variable names.
3275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003277get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003279 static long_u gdone;
3280 static long_u bdone;
3281 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003282 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003283 static int vidx;
3284 static hashitem_T *hi;
3285 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003288 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003289 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003291 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003292
3293 /* Global variables */
3294 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003296 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003298 else
3299 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003300 while (HASHITEM_EMPTY(hi))
3301 ++hi;
3302 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3303 return cat_prefix_varname('g', hi->hi_key);
3304 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003306
3307 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003308 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003311 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003313 else
3314 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003315 while (HASHITEM_EMPTY(hi))
3316 ++hi;
3317 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003319
3320 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003321 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003324 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003326 else
3327 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003328 while (HASHITEM_EMPTY(hi))
3329 ++hi;
3330 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003332
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003333 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003334 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003335 if (tdone < ht->ht_used)
3336 {
3337 if (tdone++ == 0)
3338 hi = ht->ht_array;
3339 else
3340 ++hi;
3341 while (HASHITEM_EMPTY(hi))
3342 ++hi;
3343 return cat_prefix_varname('t', hi->hi_key);
3344 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003345
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 /* v: variables */
3347 if (vidx < VV_LEN)
3348 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaard23a8232018-02-10 18:45:26 +01003350 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 varnamebuflen = 0;
3352 return NULL;
3353}
3354
3355#endif /* FEAT_CMDL_COMPL */
3356
3357/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003358 * Return TRUE if "pat" matches "text".
3359 * Does not use 'cpo' and always uses 'magic'.
3360 */
3361 static int
3362pattern_match(char_u *pat, char_u *text, int ic)
3363{
3364 int matches = FALSE;
3365 char_u *save_cpo;
3366 regmatch_T regmatch;
3367
3368 /* avoid 'l' flag in 'cpoptions' */
3369 save_cpo = p_cpo;
3370 p_cpo = (char_u *)"";
3371 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3372 if (regmatch.regprog != NULL)
3373 {
3374 regmatch.rm_ic = ic;
3375 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3376 vim_regfree(regmatch.regprog);
3377 }
3378 p_cpo = save_cpo;
3379 return matches;
3380}
3381
3382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3386 */
3387
3388/*
3389 * Handle zero level expression.
3390 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003392 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Return OK or FAIL.
3394 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003396eval0(
3397 char_u *arg,
3398 typval_T *rettv,
3399 char_u **nextcmd,
3400 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 int ret;
3403 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003404 int did_emsg_before = did_emsg;
3405 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (ret == FAIL || !ends_excmd(*p))
3410 {
3411 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 /*
3414 * Report the invalid expression unless the expression evaluation has
3415 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003416 * exception, or we already gave a more specific error.
3417 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003419 if (!aborting() && did_emsg == did_emsg_before
3420 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003421 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ret = FAIL;
3423 }
3424 if (nextcmd != NULL)
3425 *nextcmd = check_nextcmd(p);
3426
3427 return ret;
3428}
3429
3430/*
3431 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003432 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 *
3434 * "arg" must point to the first non-white of the expression.
3435 * "arg" is advanced to the next non-white after the recognized expression.
3436 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003437 * Note: "rettv.v_lock" is not set.
3438 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Return OK or FAIL.
3440 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003442eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /*
3448 * Get the first variable.
3449 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return FAIL;
3452
3453 if ((*arg)[0] == '?')
3454 {
3455 result = FALSE;
3456 if (evaluate)
3457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003458 int error = FALSE;
3459
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003460 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003463 if (error)
3464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466
3467 /*
3468 * Get the second variable.
3469 */
3470 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473
3474 /*
3475 * Check for the ":".
3476 */
3477 if ((*arg)[0] != ':')
3478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003479 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return FAIL;
3483 }
3484
3485 /*
3486 * Get the third variable.
3487 */
3488 *arg = skipwhite(*arg + 1);
3489 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3490 {
3491 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return FAIL;
3494 }
3495 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
3498
3499 return OK;
3500}
3501
3502/*
3503 * Handle first level expression:
3504 * expr2 || expr2 || expr2 logical OR
3505 *
3506 * "arg" must point to the first non-white of the expression.
3507 * "arg" is advanced to the next non-white after the recognized expression.
3508 *
3509 * Return OK or FAIL.
3510 */
3511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
Bram Moolenaar33570922005-01-25 22:26:29 +00003514 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 long result;
3516 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 /*
3520 * Get the first variable.
3521 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 return FAIL;
3524
3525 /*
3526 * Repeat until there is no following "||".
3527 */
3528 first = TRUE;
3529 result = FALSE;
3530 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3531 {
3532 if (evaluate && first)
3533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003534 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003536 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003537 if (error)
3538 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 first = FALSE;
3540 }
3541
3542 /*
3543 * Get the second variable.
3544 */
3545 *arg = skipwhite(*arg + 2);
3546 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3547 return FAIL;
3548
3549 /*
3550 * Compute the result.
3551 */
3552 if (evaluate && !result)
3553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003554 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003557 if (error)
3558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 if (evaluate)
3561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 rettv->v_type = VAR_NUMBER;
3563 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
3566
3567 return OK;
3568}
3569
3570/*
3571 * Handle second level expression:
3572 * expr3 && expr3 && expr3 logical AND
3573 *
3574 * "arg" must point to the first non-white of the expression.
3575 * "arg" is advanced to the next non-white after the recognized expression.
3576 *
3577 * Return OK or FAIL.
3578 */
3579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
Bram Moolenaar33570922005-01-25 22:26:29 +00003582 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 long result;
3584 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003585 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 /*
3588 * Get the first variable.
3589 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003590 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return FAIL;
3592
3593 /*
3594 * Repeat until there is no following "&&".
3595 */
3596 first = TRUE;
3597 result = TRUE;
3598 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3599 {
3600 if (evaluate && first)
3601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003602 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003605 if (error)
3606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 first = FALSE;
3608 }
3609
3610 /*
3611 * Get the second variable.
3612 */
3613 *arg = skipwhite(*arg + 2);
3614 if (eval4(arg, &var2, evaluate && result) == FAIL)
3615 return FAIL;
3616
3617 /*
3618 * Compute the result.
3619 */
3620 if (evaluate && result)
3621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003622 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003624 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003625 if (error)
3626 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628 if (evaluate)
3629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003630 rettv->v_type = VAR_NUMBER;
3631 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 }
3634
3635 return OK;
3636}
3637
3638/*
3639 * Handle third level expression:
3640 * var1 == var2
3641 * var1 =~ var2
3642 * var1 != var2
3643 * var1 !~ var2
3644 * var1 > var2
3645 * var1 >= var2
3646 * var1 < var2
3647 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003648 * var1 is var2
3649 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 *
3651 * "arg" must point to the first non-white of the expression.
3652 * "arg" is advanced to the next non-white after the recognized expression.
3653 *
3654 * Return OK or FAIL.
3655 */
3656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 char_u *p;
3661 int i;
3662 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003663 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 /*
3668 * Get the first variable.
3669 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672
3673 p = *arg;
3674 switch (p[0])
3675 {
3676 case '=': if (p[1] == '=')
3677 type = TYPE_EQUAL;
3678 else if (p[1] == '~')
3679 type = TYPE_MATCH;
3680 break;
3681 case '!': if (p[1] == '=')
3682 type = TYPE_NEQUAL;
3683 else if (p[1] == '~')
3684 type = TYPE_NOMATCH;
3685 break;
3686 case '>': if (p[1] != '=')
3687 {
3688 type = TYPE_GREATER;
3689 len = 1;
3690 }
3691 else
3692 type = TYPE_GEQUAL;
3693 break;
3694 case '<': if (p[1] != '=')
3695 {
3696 type = TYPE_SMALLER;
3697 len = 1;
3698 }
3699 else
3700 type = TYPE_SEQUAL;
3701 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003702 case 'i': if (p[1] == 's')
3703 {
3704 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3705 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003706 i = p[len];
3707 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003708 {
3709 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3710 type_is = TRUE;
3711 }
3712 }
3713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715
3716 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
3719 if (type != TYPE_UNKNOWN)
3720 {
3721 /* extra question mark appended: ignore case */
3722 if (p[len] == '?')
3723 {
3724 ic = TRUE;
3725 ++len;
3726 }
3727 /* extra '#' appended: match case */
3728 else if (p[len] == '#')
3729 {
3730 ic = FALSE;
3731 ++len;
3732 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 else
3735 ic = p_ic;
3736
3737 /*
3738 * Get the second variable.
3739 */
3740 *arg = skipwhite(p + len);
3741 if (eval5(arg, &var2, evaluate) == FAIL)
3742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 return FAIL;
3745 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003746 if (evaluate)
3747 {
3748 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3749
3750 clear_tv(&var2);
3751 return ret;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754
3755 return OK;
3756}
3757
3758/*
3759 * Handle fourth level expression:
3760 * + number addition
3761 * - number subtraction
3762 * . string concatenation
3763 *
3764 * "arg" must point to the first non-white of the expression.
3765 * "arg" is advanced to the next non-white after the recognized expression.
3766 *
3767 * Return OK or FAIL.
3768 */
3769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003770eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar33570922005-01-25 22:26:29 +00003772 typval_T var2;
3773 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003775 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003776#ifdef FEAT_FLOAT
3777 float_T f1 = 0, f2 = 0;
3778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 char_u *s1, *s2;
3780 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3781 char_u *p;
3782
3783 /*
3784 * Get the first variable.
3785 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003786 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return FAIL;
3788
3789 /*
3790 * Repeat computing, until no '+', '-' or '.' is following.
3791 */
3792 for (;;)
3793 {
3794 op = **arg;
3795 if (op != '+' && op != '-' && op != '.')
3796 break;
3797
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003798 if ((op != '+' || (rettv->v_type != VAR_LIST
3799 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800#ifdef FEAT_FLOAT
3801 && (op == '.' || rettv->v_type != VAR_FLOAT)
3802#endif
3803 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003804 {
3805 /* For "list + ...", an illegal use of the first operand as
3806 * a number cannot be determined before evaluating the 2nd
3807 * operand: if this is also a list, all is ok.
3808 * For "something . ...", "something - ..." or "non-list + ...",
3809 * we know that the first operand needs to be a string or number
3810 * without evaluating the 2nd operand. So check before to avoid
3811 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003812 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 {
3814 clear_tv(rettv);
3815 return FAIL;
3816 }
3817 }
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /*
3820 * Get the second variable.
3821 */
3822 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003823 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return FAIL;
3827 }
3828
3829 if (evaluate)
3830 {
3831 /*
3832 * Compute the result.
3833 */
3834 if (op == '.')
3835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003836 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3837 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003838 if (s2 == NULL) /* type error ? */
3839 {
3840 clear_tv(rettv);
3841 clear_tv(&var2);
3842 return FAIL;
3843 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003844 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 clear_tv(rettv);
3846 rettv->v_type = VAR_STRING;
3847 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003849 else if (op == '+' && rettv->v_type == VAR_BLOB
3850 && var2.v_type == VAR_BLOB)
3851 {
3852 blob_T *b1 = rettv->vval.v_blob;
3853 blob_T *b2 = var2.vval.v_blob;
3854 blob_T *b = blob_alloc();
3855 int i;
3856
3857 if (b != NULL)
3858 {
3859 for (i = 0; i < blob_len(b1); i++)
3860 ga_append(&b->bv_ga, blob_get(b1, i));
3861 for (i = 0; i < blob_len(b2); i++)
3862 ga_append(&b->bv_ga, blob_get(b2, i));
3863
3864 clear_tv(rettv);
3865 rettv_blob_set(rettv, b);
3866 }
3867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003868 else if (op == '+' && rettv->v_type == VAR_LIST
3869 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003870 {
3871 /* concatenate Lists */
3872 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3873 &var3) == FAIL)
3874 {
3875 clear_tv(rettv);
3876 clear_tv(&var2);
3877 return FAIL;
3878 }
3879 clear_tv(rettv);
3880 *rettv = var3;
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else
3883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003884 int error = FALSE;
3885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003886#ifdef FEAT_FLOAT
3887 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889 f1 = rettv->vval.v_float;
3890 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892 else
3893#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003896 if (error)
3897 {
3898 /* This can only happen for "list + non-list". For
3899 * "non-list + ..." or "something - ...", we returned
3900 * before evaluating the 2nd operand. */
3901 clear_tv(rettv);
3902 return FAIL;
3903 }
3904#ifdef FEAT_FLOAT
3905 if (var2.v_type == VAR_FLOAT)
3906 f1 = n1;
3907#endif
3908 }
3909#ifdef FEAT_FLOAT
3910 if (var2.v_type == VAR_FLOAT)
3911 {
3912 f2 = var2.vval.v_float;
3913 n2 = 0;
3914 }
3915 else
3916#endif
3917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003918 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003919 if (error)
3920 {
3921 clear_tv(rettv);
3922 clear_tv(&var2);
3923 return FAIL;
3924 }
3925#ifdef FEAT_FLOAT
3926 if (rettv->v_type == VAR_FLOAT)
3927 f2 = n2;
3928#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931
3932#ifdef FEAT_FLOAT
3933 /* If there is a float on either side the result is a float. */
3934 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3935 {
3936 if (op == '+')
3937 f1 = f1 + f2;
3938 else
3939 f1 = f1 - f2;
3940 rettv->v_type = VAR_FLOAT;
3941 rettv->vval.v_float = f1;
3942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003944#endif
3945 {
3946 if (op == '+')
3947 n1 = n1 + n2;
3948 else
3949 n1 = n1 - n2;
3950 rettv->v_type = VAR_NUMBER;
3951 rettv->vval.v_number = n1;
3952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 }
3957 return OK;
3958}
3959
3960/*
3961 * Handle fifth level expression:
3962 * * number multiplication
3963 * / number division
3964 * % number modulo
3965 *
3966 * "arg" must point to the first non-white of the expression.
3967 * "arg" is advanced to the next non-white after the recognized expression.
3968 *
3969 * Return OK or FAIL.
3970 */
3971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003972eval6(
3973 char_u **arg,
3974 typval_T *rettv,
3975 int evaluate,
3976 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003980 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003981#ifdef FEAT_FLOAT
3982 int use_float = FALSE;
3983 float_T f1 = 0, f2;
3984#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003985 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 /*
3988 * Get the first variable.
3989 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003990 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 return FAIL;
3992
3993 /*
3994 * Repeat computing, until no '*', '/' or '%' is following.
3995 */
3996 for (;;)
3997 {
3998 op = **arg;
3999 if (op != '*' && op != '/' && op != '%')
4000 break;
4001
4002 if (evaluate)
4003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004004#ifdef FEAT_FLOAT
4005 if (rettv->v_type == VAR_FLOAT)
4006 {
4007 f1 = rettv->vval.v_float;
4008 use_float = TRUE;
4009 n1 = 0;
4010 }
4011 else
4012#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004013 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (error)
4016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 else
4019 n1 = 0;
4020
4021 /*
4022 * Get the second variable.
4023 */
4024 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004025 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return FAIL;
4027
4028 if (evaluate)
4029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004030#ifdef FEAT_FLOAT
4031 if (var2.v_type == VAR_FLOAT)
4032 {
4033 if (!use_float)
4034 {
4035 f1 = n1;
4036 use_float = TRUE;
4037 }
4038 f2 = var2.vval.v_float;
4039 n2 = 0;
4040 }
4041 else
4042#endif
4043 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004044 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045 clear_tv(&var2);
4046 if (error)
4047 return FAIL;
4048#ifdef FEAT_FLOAT
4049 if (use_float)
4050 f2 = n2;
4051#endif
4052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 /*
4055 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058#ifdef FEAT_FLOAT
4059 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 if (op == '*')
4062 f1 = f1 * f2;
4063 else if (op == '/')
4064 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004065# ifdef VMS
4066 /* VMS crashes on divide by zero, work around it */
4067 if (f2 == 0.0)
4068 {
4069 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004070 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004071 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004072 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004073 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004074 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004075 }
4076 else
4077 f1 = f1 / f2;
4078# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004079 /* We rely on the floating point library to handle divide
4080 * by zero to result in "inf" and not a crash. */
4081 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004082# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004086 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 return FAIL;
4088 }
4089 rettv->v_type = VAR_FLOAT;
4090 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004095 if (op == '*')
4096 n1 = n1 * n2;
4097 else if (op == '/')
4098 {
4099 if (n2 == 0) /* give an error message? */
4100 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004101 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004102 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004103 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004104 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004105 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004106 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004107 }
4108 else
4109 n1 = n1 / n2;
4110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004112 {
4113 if (n2 == 0) /* give an error message? */
4114 n1 = 0;
4115 else
4116 n1 = n1 % n2;
4117 }
4118 rettv->v_type = VAR_NUMBER;
4119 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * Handle sixth level expression:
4129 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004130 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004131 * "string" string constant
4132 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 * &option-name option value
4134 * @r register contents
4135 * identifier variable value
4136 * function() function call
4137 * $VAR environment variable
4138 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004139 * [expr, expr] List
4140 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 *
4142 * Also handle:
4143 * ! in front logical NOT
4144 * - in front unary minus
4145 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 * trailing [] subscript in String or List
4147 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 *
4149 * "arg" must point to the first non-white of the expression.
4150 * "arg" is advanced to the next non-white after the recognized expression.
4151 *
4152 * Return OK or FAIL.
4153 */
4154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155eval7(
4156 char_u **arg,
4157 typval_T *rettv,
4158 int evaluate,
4159 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004161 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int len;
4163 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u *start_leader, *end_leader;
4165 int ret = OK;
4166 char_u *alias;
4167
4168 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004170 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004175 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 */
4177 start_leader = *arg;
4178 while (**arg == '!' || **arg == '-' || **arg == '+')
4179 *arg = skipwhite(*arg + 1);
4180 end_leader = *arg;
4181
4182 switch (**arg)
4183 {
4184 /*
4185 * Number constant.
4186 */
4187 case '0':
4188 case '1':
4189 case '2':
4190 case '3':
4191 case '4':
4192 case '5':
4193 case '6':
4194 case '7':
4195 case '8':
4196 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004197 {
4198#ifdef FEAT_FLOAT
4199 char_u *p = skipdigits(*arg + 1);
4200 int get_float = FALSE;
4201
4202 /* We accept a float when the format matches
4203 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004204 * strict to avoid backwards compatibility problems.
4205 * Don't look for a float after the "." operator, so that
4206 * ":let vers = 1.2.3" doesn't fail. */
4207 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004209 get_float = TRUE;
4210 p = skipdigits(p + 2);
4211 if (*p == 'e' || *p == 'E')
4212 {
4213 ++p;
4214 if (*p == '-' || *p == '+')
4215 ++p;
4216 if (!vim_isdigit(*p))
4217 get_float = FALSE;
4218 else
4219 p = skipdigits(p + 1);
4220 }
4221 if (ASCII_ISALPHA(*p) || *p == '.')
4222 get_float = FALSE;
4223 }
4224 if (get_float)
4225 {
4226 float_T f;
4227
4228 *arg += string2float(*arg, &f);
4229 if (evaluate)
4230 {
4231 rettv->v_type = VAR_FLOAT;
4232 rettv->vval.v_float = f;
4233 }
4234 }
4235 else
4236#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004237 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004238 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004239 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004240 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004241
4242 // Blob constant: 0z0123456789abcdef
4243 if (evaluate)
4244 blob = blob_alloc();
4245 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4246 {
4247 if (!vim_isxdigit(bp[1]))
4248 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004249 if (blob != NULL)
4250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004252 ga_clear(&blob->bv_ga);
4253 VIM_CLEAR(blob);
4254 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004255 ret = FAIL;
4256 break;
4257 }
4258 if (blob != NULL)
4259 ga_append(&blob->bv_ga,
4260 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004261 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4262 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004263 }
4264 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004265 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004266 *arg = bp;
4267 }
4268 else
4269 {
4270 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004271 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004272 *arg += len;
4273 if (evaluate)
4274 {
4275 rettv->v_type = VAR_NUMBER;
4276 rettv->vval.v_number = n;
4277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 /*
4283 * String constant: "string".
4284 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 break;
4287
4288 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004289 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 break;
4293
4294 /*
4295 * List: [expr, expr]
4296 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 break;
4299
4300 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004301 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302 * Dictionary: {key: val, key: val}
4303 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004304 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4305 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004306 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 break;
4308
4309 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004310 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004312 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 break;
4314
4315 /*
4316 * Environment variable: $VAR.
4317 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 break;
4320
4321 /*
4322 * Register contents: @r.
4323 */
4324 case '@': ++*arg;
4325 if (evaluate)
4326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004328 rettv->vval.v_string = get_reg_contents(**arg,
4329 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 if (**arg != NUL)
4332 ++*arg;
4333 break;
4334
4335 /*
4336 * nested expression: (expression).
4337 */
4338 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 if (**arg == ')')
4341 ++*arg;
4342 else if (ret == OK)
4343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004344 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 ret = FAIL;
4347 }
4348 break;
4349
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 break;
4352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353
4354 if (ret == NOTDONE)
4355 {
4356 /*
4357 * Must be a variable or function name.
4358 * Can also be a curly-braces kind of name: {expr}.
4359 */
4360 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004361 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 if (alias != NULL)
4363 s = alias;
4364
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004365 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004366 ret = FAIL;
4367 else
4368 {
4369 if (**arg == '(') /* recursive! */
4370 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004371 partial_T *partial;
4372
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004373 if (!evaluate)
4374 check_vars(s, len);
4375
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376 /* If "s" is the name of a variable of type VAR_FUNC
4377 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004378 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004379
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004380 /* Need to make a copy, in case evaluating the arguments makes
4381 * the name invalid. */
4382 s = vim_strsave(s);
4383 if (s == NULL)
4384 ret = FAIL;
4385 else
4386 /* Invoke the function. */
4387 ret = get_func_tv(s, len, rettv, arg,
4388 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4389 &len, evaluate, partial, NULL);
4390 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004391
4392 /* If evaluate is FALSE rettv->v_type was not set in
4393 * get_func_tv, but it's needed in handle_subscript() to parse
4394 * what follows. So set it here. */
4395 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4396 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004397 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004398 rettv->v_type = VAR_FUNC;
4399 }
4400
Bram Moolenaar8c711452005-01-14 21:53:12 +00004401 /* Stop the expression evaluation when immediately
4402 * aborting on error, or when an interrupt occurred or
4403 * an exception was thrown but not caught. */
4404 if (aborting())
4405 {
4406 if (ret == OK)
4407 clear_tv(rettv);
4408 ret = FAIL;
4409 }
4410 }
4411 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004412 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004413 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004414 {
4415 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004416 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004417 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004419 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004420 }
4421
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 *arg = skipwhite(*arg);
4423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004424 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4425 * expr(expr). */
4426 if (ret == OK)
4427 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 /*
4430 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4431 */
4432 if (ret == OK && evaluate && end_leader > start_leader)
4433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004434 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004435 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004436#ifdef FEAT_FLOAT
4437 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004439 if (rettv->v_type == VAR_FLOAT)
4440 f = rettv->vval.v_float;
4441 else
4442#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004443 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004444 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446 clear_tv(rettv);
4447 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 else
4450 {
4451 while (end_leader > start_leader)
4452 {
4453 --end_leader;
4454 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 {
4456#ifdef FEAT_FLOAT
4457 if (rettv->v_type == VAR_FLOAT)
4458 f = !f;
4459 else
4460#endif
4461 val = !val;
4462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 {
4465#ifdef FEAT_FLOAT
4466 if (rettv->v_type == VAR_FLOAT)
4467 f = -f;
4468 else
4469#endif
4470 val = -val;
4471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004472 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473#ifdef FEAT_FLOAT
4474 if (rettv->v_type == VAR_FLOAT)
4475 {
4476 clear_tv(rettv);
4477 rettv->vval.v_float = f;
4478 }
4479 else
4480#endif
4481 {
4482 clear_tv(rettv);
4483 rettv->v_type = VAR_NUMBER;
4484 rettv->vval.v_number = val;
4485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
4489 return ret;
4490}
4491
4492/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004493 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4494 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4496 */
4497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004498eval_index(
4499 char_u **arg,
4500 typval_T *rettv,
4501 int evaluate,
4502 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503{
4504 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004505 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004506 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 long len = -1;
4509 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004510 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004511 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004512
Bram Moolenaara03f2332016-02-06 18:09:59 +01004513 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004516 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004517 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004518 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004519 return FAIL;
4520 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004521#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004522 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004523 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004524 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004525#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004526 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004527 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004528 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004529 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004530 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004531 return FAIL;
4532 case VAR_UNKNOWN:
4533 if (evaluate)
4534 return FAIL;
4535 /* FALLTHROUGH */
4536
4537 case VAR_STRING:
4538 case VAR_NUMBER:
4539 case VAR_LIST:
4540 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004541 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004542 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004543 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004545 init_tv(&var1);
4546 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 /*
4550 * dict.name
4551 */
4552 key = *arg + 1;
4553 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4554 ;
4555 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558 }
4559 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 /*
4562 * something[idx]
4563 *
4564 * Get the (first) variable from inside the [].
4565 */
4566 *arg = skipwhite(*arg + 1);
4567 if (**arg == ':')
4568 empty1 = TRUE;
4569 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4570 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004571 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004572 {
4573 /* not a number or string */
4574 clear_tv(&var1);
4575 return FAIL;
4576 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577
4578 /*
4579 * Get the second variable from inside the [:].
4580 */
4581 if (**arg == ':')
4582 {
4583 range = TRUE;
4584 *arg = skipwhite(*arg + 1);
4585 if (**arg == ']')
4586 empty2 = TRUE;
4587 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004589 if (!empty1)
4590 clear_tv(&var1);
4591 return FAIL;
4592 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004593 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 {
4595 /* not a number or string */
4596 if (!empty1)
4597 clear_tv(&var1);
4598 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 return FAIL;
4600 }
4601 }
4602
4603 /* Check for the ']'. */
4604 if (**arg != ']')
4605 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004606 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004607 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 clear_tv(&var1);
4609 if (range)
4610 clear_tv(&var2);
4611 return FAIL;
4612 }
4613 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 }
4615
4616 if (evaluate)
4617 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 n1 = 0;
4619 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 }
4624 if (range)
4625 {
4626 if (empty2)
4627 n2 = -1;
4628 else
4629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004630 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 }
4633 }
4634
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004635 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004637 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004638 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004639 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004640 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004641 case VAR_SPECIAL:
4642 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004643 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004644 break; /* not evaluating, skipping over subscript */
4645
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 case VAR_NUMBER:
4647 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004648 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 len = (long)STRLEN(s);
4650 if (range)
4651 {
4652 /* The resulting variable is a substring. If the indexes
4653 * are out of range the result is empty. */
4654 if (n1 < 0)
4655 {
4656 n1 = len + n1;
4657 if (n1 < 0)
4658 n1 = 0;
4659 }
4660 if (n2 < 0)
4661 n2 = len + n2;
4662 else if (n2 >= len)
4663 n2 = len;
4664 if (n1 >= len || n2 < 0 || n1 > n2)
4665 s = NULL;
4666 else
4667 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4668 }
4669 else
4670 {
4671 /* The resulting variable is a string of a single
4672 * character. If the index is too big or negative the
4673 * result is empty. */
4674 if (n1 >= len || n1 < 0)
4675 s = NULL;
4676 else
4677 s = vim_strnsave(s + n1, 1);
4678 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 break;
4683
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004684 case VAR_BLOB:
4685 len = blob_len(rettv->vval.v_blob);
4686 if (range)
4687 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004688 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004689 // are out of range the result is empty.
4690 if (n1 < 0)
4691 {
4692 n1 = len + n1;
4693 if (n1 < 0)
4694 n1 = 0;
4695 }
4696 if (n2 < 0)
4697 n2 = len + n2;
4698 else if (n2 >= len)
4699 n2 = len - 1;
4700 if (n1 >= len || n2 < 0 || n1 > n2)
4701 {
4702 clear_tv(rettv);
4703 rettv->v_type = VAR_BLOB;
4704 rettv->vval.v_blob = NULL;
4705 }
4706 else
4707 {
4708 blob_T *blob = blob_alloc();
4709
4710 if (blob != NULL)
4711 {
4712 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4713 {
4714 blob_free(blob);
4715 return FAIL;
4716 }
4717 blob->bv_ga.ga_len = n2 - n1 + 1;
4718 for (i = n1; i <= n2; i++)
4719 blob_set(blob, i - n1,
4720 blob_get(rettv->vval.v_blob, i));
4721
4722 clear_tv(rettv);
4723 rettv_blob_set(rettv, blob);
4724 }
4725 }
4726 }
4727 else
4728 {
4729 // The resulting variable is a string of a single
4730 // character. If the index is too big or negative the
4731 // result is empty.
4732 if (n1 < len && n1 >= 0)
4733 {
4734 int v = (int)blob_get(rettv->vval.v_blob, n1);
4735
4736 clear_tv(rettv);
4737 rettv->v_type = VAR_NUMBER;
4738 rettv->vval.v_number = v;
4739 }
4740 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004741 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004742 }
4743 break;
4744
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 if (n1 < 0)
4748 n1 = len + n1;
4749 if (!empty1 && (n1 < 0 || n1 >= len))
4750 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004751 /* For a range we allow invalid values and return an empty
4752 * list. A list index out of range is an error. */
4753 if (!range)
4754 {
4755 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004756 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004757 return FAIL;
4758 }
4759 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761 if (range)
4762 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004763 list_T *l;
4764 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765
4766 if (n2 < 0)
4767 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004768 else if (n2 >= len)
4769 n2 = len - 1;
4770 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004771 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 l = list_alloc();
4773 if (l == NULL)
4774 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004775 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 n1 <= n2; ++n1)
4777 {
4778 if (list_append_tv(l, &item->li_tv) == FAIL)
4779 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004780 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 return FAIL;
4782 }
4783 item = item->li_next;
4784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004786 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 }
4788 else
4789 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004790 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004791 clear_tv(rettv);
4792 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 }
4794 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795
4796 case VAR_DICT:
4797 if (range)
4798 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004799 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004800 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 if (len == -1)
4802 clear_tv(&var1);
4803 return FAIL;
4804 }
4805 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004806 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807
4808 if (len == -1)
4809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004810 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004811 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 clear_tv(&var1);
4814 return FAIL;
4815 }
4816 }
4817
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004818 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004820 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004821 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822 if (len == -1)
4823 clear_tv(&var1);
4824 if (item == NULL)
4825 return FAIL;
4826
4827 copy_tv(&item->di_tv, &var1);
4828 clear_tv(rettv);
4829 *rettv = var1;
4830 }
4831 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832 }
4833 }
4834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 return OK;
4836}
4837
4838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 * Get an option value.
4840 * "arg" points to the '&' or '+' before the option name.
4841 * "arg" is advanced to character after the option name.
4842 * Return OK or FAIL.
4843 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004845get_option_tv(
4846 char_u **arg,
4847 typval_T *rettv, /* when NULL, only check if option exists */
4848 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 char_u *option_end;
4851 long numval;
4852 char_u *stringval;
4853 int opt_type;
4854 int c;
4855 int working = (**arg == '+'); /* has("+option") */
4856 int ret = OK;
4857 int opt_flags;
4858
4859 /*
4860 * Isolate the option name and find its value.
4861 */
4862 option_end = find_option_end(arg, &opt_flags);
4863 if (option_end == NULL)
4864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return FAIL;
4868 }
4869
4870 if (!evaluate)
4871 {
4872 *arg = option_end;
4873 return OK;
4874 }
4875
4876 c = *option_end;
4877 *option_end = NUL;
4878 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 if (opt_type == -3) /* invalid name */
4882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004884 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 ret = FAIL;
4886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
4889 if (opt_type == -2) /* hidden string option */
4890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->v_type = VAR_STRING;
4892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 else if (opt_type == -1) /* hidden number option */
4895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 rettv->v_type = VAR_NUMBER;
4897 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 else if (opt_type == 1) /* number option */
4900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 rettv->v_type = VAR_NUMBER;
4902 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else /* string option */
4905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 rettv->v_type = VAR_STRING;
4907 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 }
4910 else if (working && (opt_type == -2 || opt_type == -1))
4911 ret = FAIL;
4912
4913 *option_end = c; /* put back for error messages */
4914 *arg = option_end;
4915
4916 return ret;
4917}
4918
4919/*
4920 * Allocate a variable for a string constant.
4921 * Return OK or FAIL.
4922 */
4923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004924get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 char_u *p;
4927 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 int extra = 0;
4929
4930 /*
4931 * Find the end of the string, skipping backslashed characters.
4932 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004933 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 if (*p == '\\' && p[1] != NUL)
4936 {
4937 ++p;
4938 /* A "\<x>" form occupies at least 4 characters, and produces up
4939 * to 6 characters: reserve space for 2 extra */
4940 if (*p == '<')
4941 extra += 2;
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944
4945 if (*p != '"')
4946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004947 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return FAIL;
4949 }
4950
4951 /* If only parsing, set *arg and return here */
4952 if (!evaluate)
4953 {
4954 *arg = p + 1;
4955 return OK;
4956 }
4957
4958 /*
4959 * Copy the string into allocated memory, handling backslashed
4960 * characters.
4961 */
4962 name = alloc((unsigned)(p - *arg + extra));
4963 if (name == NULL)
4964 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 if (*p == '\\')
4971 {
4972 switch (*++p)
4973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 case 'b': *name++ = BS; ++p; break;
4975 case 'e': *name++ = ESC; ++p; break;
4976 case 'f': *name++ = FF; ++p; break;
4977 case 'n': *name++ = NL; ++p; break;
4978 case 'r': *name++ = CAR; ++p; break;
4979 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981 case 'X': /* hex: "\x1", "\x12" */
4982 case 'x':
4983 case 'u': /* Unicode: "\u0023" */
4984 case 'U':
4985 if (vim_isxdigit(p[1]))
4986 {
4987 int n, nr;
4988 int c = toupper(*p);
4989
4990 if (c == 'X')
4991 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004992 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004994 else
4995 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 nr = 0;
4997 while (--n >= 0 && vim_isxdigit(p[1]))
4998 {
4999 ++p;
5000 nr = (nr << 4) + hex2nr(*p);
5001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#ifdef FEAT_MBYTE
5004 /* For "\u" store the number according to
5005 * 'encoding'. */
5006 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
5009#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
5013
5014 /* octal: "\1", "\12", "\123" */
5015 case '0':
5016 case '1':
5017 case '2':
5018 case '3':
5019 case '4':
5020 case '5':
5021 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 case '7': *name = *p++ - '0';
5023 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 *name = (*name << 3) + *p++ - '0';
5026 if (*p >= '0' && *p <= '7')
5027 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031
5032 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005033 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (extra != 0)
5035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
5039 /* FALLTHROUGH */
5040
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043 }
5044 }
5045 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005050 if (*p != NUL) /* just in case */
5051 ++p;
5052 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 return OK;
5055}
5056
5057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 * Return OK or FAIL.
5060 */
5061 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005062get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
5064 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 int reduce = 0;
5067
5068 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005071 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 break;
5077 ++reduce;
5078 ++p;
5079 }
5080 }
5081
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005084 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 return FAIL;
5086 }
5087
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 if (!evaluate)
5090 {
5091 *arg = p + 1;
5092 return OK;
5093 }
5094
5095 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 */
5098 str = alloc((unsigned)((p - *arg) - reduce));
5099 if (str == NULL)
5100 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 rettv->v_type = VAR_STRING;
5102 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 break;
5110 ++p;
5111 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 *arg = p + 1;
5116
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005117 return OK;
5118}
5119
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005120/*
5121 * Return the function name of the partial.
5122 */
5123 char_u *
5124partial_name(partial_T *pt)
5125{
5126 if (pt->pt_name != NULL)
5127 return pt->pt_name;
5128 return pt->pt_func->uf_name;
5129}
5130
Bram Moolenaarddecc252016-04-06 22:59:37 +02005131 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005132partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005133{
5134 int i;
5135
5136 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005137 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005138 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005139 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005140 if (pt->pt_name != NULL)
5141 {
5142 func_unref(pt->pt_name);
5143 vim_free(pt->pt_name);
5144 }
5145 else
5146 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005147 vim_free(pt);
5148}
5149
5150/*
5151 * Unreference a closure: decrement the reference count and free it when it
5152 * becomes zero.
5153 */
5154 void
5155partial_unref(partial_T *pt)
5156{
5157 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005158 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005159}
5160
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005161static int tv_equal_recurse_limit;
5162
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005163 static int
5164func_equal(
5165 typval_T *tv1,
5166 typval_T *tv2,
5167 int ic) /* ignore case */
5168{
5169 char_u *s1, *s2;
5170 dict_T *d1, *d2;
5171 int a1, a2;
5172 int i;
5173
5174 /* empty and NULL function name considered the same */
5175 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005176 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005177 if (s1 != NULL && *s1 == NUL)
5178 s1 = NULL;
5179 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005180 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005181 if (s2 != NULL && *s2 == NUL)
5182 s2 = NULL;
5183 if (s1 == NULL || s2 == NULL)
5184 {
5185 if (s1 != s2)
5186 return FALSE;
5187 }
5188 else if (STRCMP(s1, s2) != 0)
5189 return FALSE;
5190
5191 /* empty dict and NULL dict is different */
5192 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5193 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5194 if (d1 == NULL || d2 == NULL)
5195 {
5196 if (d1 != d2)
5197 return FALSE;
5198 }
5199 else if (!dict_equal(d1, d2, ic, TRUE))
5200 return FALSE;
5201
5202 /* empty list and no list considered the same */
5203 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5204 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5205 if (a1 != a2)
5206 return FALSE;
5207 for (i = 0; i < a1; ++i)
5208 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5209 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5210 return FALSE;
5211
5212 return TRUE;
5213}
5214
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005215/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005216 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005217 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005219 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005220 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005221tv_equal(
5222 typval_T *tv1,
5223 typval_T *tv2,
5224 int ic, /* ignore case */
5225 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005226{
5227 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005229 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005230 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005231
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005232 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005233 * recursiveness to a limit. We guess they are equal then.
5234 * A fixed limit has the problem of still taking an awful long time.
5235 * Reduce the limit every time running into it. That should work fine for
5236 * deeply linked structures that are not recursively linked and catch
5237 * recursiveness quickly. */
5238 if (!recursive)
5239 tv_equal_recurse_limit = 1000;
5240 if (recursive_cnt >= tv_equal_recurse_limit)
5241 {
5242 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005243 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005244 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005245
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005246 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5247 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005248 if ((tv1->v_type == VAR_FUNC
5249 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5250 && (tv2->v_type == VAR_FUNC
5251 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5252 {
5253 ++recursive_cnt;
5254 r = func_equal(tv1, tv2, ic);
5255 --recursive_cnt;
5256 return r;
5257 }
5258
5259 if (tv1->v_type != tv2->v_type)
5260 return FALSE;
5261
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005262 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005263 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005264 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005265 ++recursive_cnt;
5266 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5267 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005268 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005269
5270 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005271 ++recursive_cnt;
5272 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5273 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005274 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005275
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005276 case VAR_BLOB:
5277 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5278
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005279 case VAR_NUMBER:
5280 return tv1->vval.v_number == tv2->vval.v_number;
5281
5282 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005283 s1 = tv_get_string_buf(tv1, buf1);
5284 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005285 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005286
5287 case VAR_SPECIAL:
5288 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005289
5290 case VAR_FLOAT:
5291#ifdef FEAT_FLOAT
5292 return tv1->vval.v_float == tv2->vval.v_float;
5293#endif
5294 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005295#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005296 return tv1->vval.v_job == tv2->vval.v_job;
5297#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005298 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005299#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005300 return tv1->vval.v_channel == tv2->vval.v_channel;
5301#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005302 case VAR_FUNC:
5303 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005304 case VAR_UNKNOWN:
5305 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005306 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005307
Bram Moolenaara03f2332016-02-06 18:09:59 +01005308 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5309 * does not equal anything, not even itself. */
5310 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005311}
5312
5313/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005314 * Return the next (unique) copy ID.
5315 * Used for serializing nested structures.
5316 */
5317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005318get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005319{
5320 current_copyID += COPYID_INC;
5321 return current_copyID;
5322}
5323
5324/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005325 * Garbage collection for lists and dictionaries.
5326 *
5327 * We use reference counts to be able to free most items right away when they
5328 * are no longer used. But for composite items it's possible that it becomes
5329 * unused while the reference count is > 0: When there is a recursive
5330 * reference. Example:
5331 * :let l = [1, 2, 3]
5332 * :let d = {9: l}
5333 * :let l[1] = d
5334 *
5335 * Since this is quite unusual we handle this with garbage collection: every
5336 * once in a while find out which lists and dicts are not referenced from any
5337 * variable.
5338 *
5339 * Here is a good reference text about garbage collection (refers to Python
5340 * but it applies to all reference-counting mechanisms):
5341 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005342 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005343
5344/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005346 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005348 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005349 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005350garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005351{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005352 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005354 buf_T *buf;
5355 win_T *wp;
5356 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005357 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005358 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005359
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005360 if (!testing)
5361 {
5362 /* Only do this once. */
5363 want_garbage_collect = FALSE;
5364 may_garbage_collect = FALSE;
5365 garbage_collect_at_exit = FALSE;
5366 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005367
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005368 /* We advance by two because we add one for items referenced through
5369 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005370 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005371
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372 /*
5373 * 1. Go through all accessible variables and mark all lists and dicts
5374 * with copyID.
5375 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005376
5377 /* Don't free variables in the previous_funccal list unless they are only
5378 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005379 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005381
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005382 /* script-local variables */
5383 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005384 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005385
5386 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005387 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005388 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5389 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005390
5391 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005392 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005393 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5394 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005395 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5397 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005398
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005399 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005400 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5402 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005404 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405
5406 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005409 /* named functions (matters for closures) */
5410 abort = abort || set_ref_in_functions(copyID);
5411
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005412 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005414
Bram Moolenaard812df62008-11-09 12:46:09 +00005415 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005417
Bram Moolenaar1dced572012-04-05 16:54:08 +02005418#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005420#endif
5421
Bram Moolenaardb913952012-06-29 12:54:53 +02005422#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005424#endif
5425
5426#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005427 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005428#endif
5429
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005430#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005431 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005432 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005433#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005434#ifdef FEAT_NETBEANS_INTG
5435 abort = abort || set_ref_in_nb_channel(copyID);
5436#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005437
Bram Moolenaare3188e22016-05-31 21:13:04 +02005438#ifdef FEAT_TIMERS
5439 abort = abort || set_ref_in_timer(copyID);
5440#endif
5441
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005442#ifdef FEAT_QUICKFIX
5443 abort = abort || set_ref_in_quickfix(copyID);
5444#endif
5445
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005446#ifdef FEAT_TERMINAL
5447 abort = abort || set_ref_in_term(copyID);
5448#endif
5449
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005451 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005452 /*
5453 * 2. Free lists and dictionaries that are not referenced.
5454 */
5455 did_free = free_unref_items(copyID);
5456
5457 /*
5458 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005459 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005462 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005463 else if (p_verbose > 0)
5464 {
5465 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5466 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005467
5468 return did_free;
5469}
5470
5471/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005472 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005473 */
5474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005475free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005476{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005477 int did_free = FALSE;
5478
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005479 /* Let all "free" functions know that we are here. This means no
5480 * dictionaries, lists, channels or jobs are to be freed, because we will
5481 * do that here. */
5482 in_free_unref_items = TRUE;
5483
5484 /*
5485 * PASS 1: free the contents of the items. We don't free the items
5486 * themselves yet, so that it is possible to decrement refcount counters
5487 */
5488
Bram Moolenaarcd524592016-07-17 14:57:05 +02005489 /* Go through the list of dicts and free items without the copyID. */
5490 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005491
Bram Moolenaarda861d62016-07-17 15:46:27 +02005492 /* Go through the list of lists and free items without the copyID. */
5493 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005494
5495#ifdef FEAT_JOB_CHANNEL
5496 /* Go through the list of jobs and free items without the copyID. This
5497 * must happen before doing channels, because jobs refer to channels, but
5498 * the reference from the channel to the job isn't tracked. */
5499 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5500
5501 /* Go through the list of channels and free items without the copyID. */
5502 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5503#endif
5504
5505 /*
5506 * PASS 2: free the items themselves.
5507 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005508 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005509 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005511#ifdef FEAT_JOB_CHANNEL
5512 /* Go through the list of jobs and free items without the copyID. This
5513 * must happen before doing channels, because jobs refer to channels, but
5514 * the reference from the channel to the job isn't tracked. */
5515 free_unused_jobs(copyID, COPYID_MASK);
5516
5517 /* Go through the list of channels and free items without the copyID. */
5518 free_unused_channels(copyID, COPYID_MASK);
5519#endif
5520
5521 in_free_unref_items = FALSE;
5522
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005523 return did_free;
5524}
5525
5526/*
5527 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005528 * "list_stack" is used to add lists to be marked. Can be NULL.
5529 *
5530 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005533set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005534{
5535 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005536 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005537 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005538 hashtab_T *cur_ht;
5539 ht_stack_T *ht_stack = NULL;
5540 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005541
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005542 cur_ht = ht;
5543 for (;;)
5544 {
5545 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005546 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005547 /* Mark each item in the hashtab. If the item contains a hashtab
5548 * it is added to ht_stack, if it contains a list it is added to
5549 * list_stack. */
5550 todo = (int)cur_ht->ht_used;
5551 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5552 if (!HASHITEM_EMPTY(hi))
5553 {
5554 --todo;
5555 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5556 &ht_stack, list_stack);
5557 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005558 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005559
5560 if (ht_stack == NULL)
5561 break;
5562
5563 /* take an item from the stack */
5564 cur_ht = ht_stack->ht;
5565 tempitem = ht_stack;
5566 ht_stack = ht_stack->prev;
5567 free(tempitem);
5568 }
5569
5570 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005571}
5572
5573/*
5574 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005575 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5576 *
5577 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005578 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005580set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005581{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 listitem_T *li;
5583 int abort = FALSE;
5584 list_T *cur_l;
5585 list_stack_T *list_stack = NULL;
5586 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005587
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005588 cur_l = l;
5589 for (;;)
5590 {
5591 if (!abort)
5592 /* Mark each item in the list. If the item contains a hashtab
5593 * it is added to ht_stack, if it contains a list it is added to
5594 * list_stack. */
5595 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5596 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5597 ht_stack, &list_stack);
5598 if (list_stack == NULL)
5599 break;
5600
5601 /* take an item from the stack */
5602 cur_l = list_stack->list;
5603 tempitem = list_stack;
5604 list_stack = list_stack->prev;
5605 free(tempitem);
5606 }
5607
5608 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005609}
5610
5611/*
5612 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005613 * "list_stack" is used to add lists to be marked. Can be NULL.
5614 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5615 *
5616 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005617 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005619set_ref_in_item(
5620 typval_T *tv,
5621 int copyID,
5622 ht_stack_T **ht_stack,
5623 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005624{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005625 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005626
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005627 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005628 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005629 dict_T *dd = tv->vval.v_dict;
5630
Bram Moolenaara03f2332016-02-06 18:09:59 +01005631 if (dd != NULL && dd->dv_copyID != copyID)
5632 {
5633 /* Didn't see this dict yet. */
5634 dd->dv_copyID = copyID;
5635 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005636 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005637 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5638 }
5639 else
5640 {
5641 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5642 if (newitem == NULL)
5643 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005644 else
5645 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005646 newitem->ht = &dd->dv_hashtab;
5647 newitem->prev = *ht_stack;
5648 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005650 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005651 }
5652 }
5653 else if (tv->v_type == VAR_LIST)
5654 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005655 list_T *ll = tv->vval.v_list;
5656
Bram Moolenaara03f2332016-02-06 18:09:59 +01005657 if (ll != NULL && ll->lv_copyID != copyID)
5658 {
5659 /* Didn't see this list yet. */
5660 ll->lv_copyID = copyID;
5661 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005662 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005663 abort = set_ref_in_list(ll, copyID, ht_stack);
5664 }
5665 else
5666 {
5667 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005668 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005669 if (newitem == NULL)
5670 abort = TRUE;
5671 else
5672 {
5673 newitem->list = ll;
5674 newitem->prev = *list_stack;
5675 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005676 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005677 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005678 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005679 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005680 else if (tv->v_type == VAR_FUNC)
5681 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005682 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005683 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005684 else if (tv->v_type == VAR_PARTIAL)
5685 {
5686 partial_T *pt = tv->vval.v_partial;
5687 int i;
5688
5689 /* A partial does not have a copyID, because it cannot contain itself.
5690 */
5691 if (pt != NULL)
5692 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005693 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005694
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005695 if (pt->pt_dict != NULL)
5696 {
5697 typval_T dtv;
5698
5699 dtv.v_type = VAR_DICT;
5700 dtv.vval.v_dict = pt->pt_dict;
5701 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5702 }
5703
5704 for (i = 0; i < pt->pt_argc; ++i)
5705 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5706 ht_stack, list_stack);
5707 }
5708 }
5709#ifdef FEAT_JOB_CHANNEL
5710 else if (tv->v_type == VAR_JOB)
5711 {
5712 job_T *job = tv->vval.v_job;
5713 typval_T dtv;
5714
5715 if (job != NULL && job->jv_copyID != copyID)
5716 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005717 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005718 if (job->jv_channel != NULL)
5719 {
5720 dtv.v_type = VAR_CHANNEL;
5721 dtv.vval.v_channel = job->jv_channel;
5722 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5723 }
5724 if (job->jv_exit_partial != NULL)
5725 {
5726 dtv.v_type = VAR_PARTIAL;
5727 dtv.vval.v_partial = job->jv_exit_partial;
5728 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5729 }
5730 }
5731 }
5732 else if (tv->v_type == VAR_CHANNEL)
5733 {
5734 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005735 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005736 typval_T dtv;
5737 jsonq_T *jq;
5738 cbq_T *cq;
5739
5740 if (ch != NULL && ch->ch_copyID != copyID)
5741 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005742 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005743 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005744 {
5745 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5746 jq = jq->jq_next)
5747 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5748 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5749 cq = cq->cq_next)
5750 if (cq->cq_partial != NULL)
5751 {
5752 dtv.v_type = VAR_PARTIAL;
5753 dtv.vval.v_partial = cq->cq_partial;
5754 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5755 }
5756 if (ch->ch_part[part].ch_partial != NULL)
5757 {
5758 dtv.v_type = VAR_PARTIAL;
5759 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5760 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5761 }
5762 }
5763 if (ch->ch_partial != NULL)
5764 {
5765 dtv.v_type = VAR_PARTIAL;
5766 dtv.vval.v_partial = ch->ch_partial;
5767 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5768 }
5769 if (ch->ch_close_partial != NULL)
5770 {
5771 dtv.v_type = VAR_PARTIAL;
5772 dtv.vval.v_partial = ch->ch_close_partial;
5773 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5774 }
5775 }
5776 }
5777#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005778 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005779}
5780
Bram Moolenaar17a13432016-01-24 14:22:10 +01005781 static char *
5782get_var_special_name(int nr)
5783{
5784 switch (nr)
5785 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005786 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005787 case VVAL_TRUE: return "v:true";
5788 case VVAL_NONE: return "v:none";
5789 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005790 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005791 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005792 return "42";
5793}
5794
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 * Return a string with the string representation of a variable.
5797 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005798 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005799 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005800 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5801 * stings as "string()", otherwise does not put quotes around strings, as
5802 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005803 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5804 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005805 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005807 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005808echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809 typval_T *tv,
5810 char_u **tofree,
5811 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005812 int copyID,
5813 int echo_style,
5814 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005815 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005817 static int recurse = 0;
5818 char_u *r = NULL;
5819
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005821 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005822 if (!did_echo_string_emsg)
5823 {
5824 /* Only give this message once for a recursive call to avoid
5825 * flooding the user with errors. And stop iterating over lists
5826 * and dicts. */
5827 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005828 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005830 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005831 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005832 }
5833 ++recurse;
5834
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 switch (tv->v_type)
5836 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005837 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005838 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005839 {
5840 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005841 r = tv->vval.v_string;
5842 if (r == NULL)
5843 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005844 }
5845 else
5846 {
5847 *tofree = string_quote(tv->vval.v_string, FALSE);
5848 r = *tofree;
5849 }
5850 break;
5851
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005853 if (echo_style)
5854 {
5855 *tofree = NULL;
5856 r = tv->vval.v_string;
5857 }
5858 else
5859 {
5860 *tofree = string_quote(tv->vval.v_string, TRUE);
5861 r = *tofree;
5862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005864
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005865 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005866 {
5867 partial_T *pt = tv->vval.v_partial;
5868 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005869 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005870 garray_T ga;
5871 int i;
5872 char_u *tf;
5873
5874 ga_init2(&ga, 1, 100);
5875 ga_concat(&ga, (char_u *)"function(");
5876 if (fname != NULL)
5877 {
5878 ga_concat(&ga, fname);
5879 vim_free(fname);
5880 }
5881 if (pt != NULL && pt->pt_argc > 0)
5882 {
5883 ga_concat(&ga, (char_u *)", [");
5884 for (i = 0; i < pt->pt_argc; ++i)
5885 {
5886 if (i > 0)
5887 ga_concat(&ga, (char_u *)", ");
5888 ga_concat(&ga,
5889 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5890 vim_free(tf);
5891 }
5892 ga_concat(&ga, (char_u *)"]");
5893 }
5894 if (pt != NULL && pt->pt_dict != NULL)
5895 {
5896 typval_T dtv;
5897
5898 ga_concat(&ga, (char_u *)", ");
5899 dtv.v_type = VAR_DICT;
5900 dtv.vval.v_dict = pt->pt_dict;
5901 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5902 vim_free(tf);
5903 }
5904 ga_concat(&ga, (char_u *)")");
5905
5906 *tofree = ga.ga_data;
5907 r = *tofree;
5908 break;
5909 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005910
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005911 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005912 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005913 break;
5914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005916 if (tv->vval.v_list == NULL)
5917 {
5918 *tofree = NULL;
5919 r = NULL;
5920 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005921 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5922 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005923 {
5924 *tofree = NULL;
5925 r = (char_u *)"[...]";
5926 }
5927 else
5928 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005929 int old_copyID = tv->vval.v_list->lv_copyID;
5930
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005931 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005932 *tofree = list2string(tv, copyID, restore_copyID);
5933 if (restore_copyID)
5934 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935 r = *tofree;
5936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005937 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005938
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005940 if (tv->vval.v_dict == NULL)
5941 {
5942 *tofree = NULL;
5943 r = NULL;
5944 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005945 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5946 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005947 {
5948 *tofree = NULL;
5949 r = (char_u *)"{...}";
5950 }
5951 else
5952 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005953 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005954 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005955 *tofree = dict2string(tv, copyID, restore_copyID);
5956 if (restore_copyID)
5957 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005958 r = *tofree;
5959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005960 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005963 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005964 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005965 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005966 break;
5967
Bram Moolenaar835dc632016-02-07 14:27:38 +01005968 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005969 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005971 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005972 if (composite_val)
5973 {
5974 *tofree = string_quote(r, FALSE);
5975 r = *tofree;
5976 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005978
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005979 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005980#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005981 *tofree = NULL;
5982 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5983 r = numbuf;
5984 break;
5985#endif
5986
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005987 case VAR_SPECIAL:
5988 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005989 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005990 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005992
Bram Moolenaar8502c702014-06-17 12:51:16 +02005993 if (--recurse == 0)
5994 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996}
5997
5998/*
5999 * Return a string with the string representation of a variable.
6000 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6001 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002 * Does not put quotes around strings, as ":echo" displays values.
6003 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6004 * May return NULL.
6005 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006006 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006007echo_string(
6008 typval_T *tv,
6009 char_u **tofree,
6010 char_u *numbuf,
6011 int copyID)
6012{
6013 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6014}
6015
6016/*
6017 * Return a string with the string representation of a variable.
6018 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6019 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006020 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006021 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006022 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024tv2string(
6025 typval_T *tv,
6026 char_u **tofree,
6027 char_u *numbuf,
6028 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006030 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031}
6032
6033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 * Return string "str" in ' quotes, doubling ' characters.
6035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006038 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006040{
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006042 char_u *p, *r, *s;
6043
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 len = (function ? 13 : 3);
6045 if (str != NULL)
6046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006047 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006048 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 if (*p == '\'')
6050 ++len;
6051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006052 s = r = alloc(len);
6053 if (r != NULL)
6054 {
6055 if (function)
6056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006058 r += 10;
6059 }
6060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 if (str != NULL)
6063 for (p = str; *p != NUL; )
6064 {
6065 if (*p == '\'')
6066 *r++ = '\'';
6067 MB_COPY_CHAR(p, r);
6068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006070 if (function)
6071 *r++ = ')';
6072 *r++ = NUL;
6073 }
6074 return s;
6075}
6076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006078/*
6079 * Convert the string "text" to a floating point number.
6080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6081 * this always uses a decimal point.
6082 * Returns the length of the text that was consumed.
6083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085string2float(
6086 char_u *text,
6087 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006088{
6089 char *s = (char *)text;
6090 float_T f;
6091
Bram Moolenaar62473612017-01-08 19:25:40 +01006092 /* MS-Windows does not deal with "inf" and "nan" properly. */
6093 if (STRNICMP(text, "inf", 3) == 0)
6094 {
6095 *value = INFINITY;
6096 return 3;
6097 }
6098 if (STRNICMP(text, "-inf", 3) == 0)
6099 {
6100 *value = -INFINITY;
6101 return 4;
6102 }
6103 if (STRNICMP(text, "nan", 3) == 0)
6104 {
6105 *value = NAN;
6106 return 3;
6107 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006108 f = strtod(s, &s);
6109 *value = f;
6110 return (int)((char_u *)s - text);
6111}
6112#endif
6113
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * Get the value of an environment variable.
6116 * "arg" is pointing to the '$'. It is advanced to after the name.
6117 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006118 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 char_u *string = NULL;
6124 int len;
6125 int cc;
6126 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006127 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129 ++*arg;
6130 name = *arg;
6131 len = get_env_len(arg);
6132 if (evaluate)
6133 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006134 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006135 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006136
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006137 cc = name[len];
6138 name[len] = NUL;
6139 /* first try vim_getenv(), fast for normal environment vars */
6140 string = vim_getenv(name, &mustfree);
6141 if (string != NULL && *string != NUL)
6142 {
6143 if (!mustfree)
6144 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006146 else
6147 {
6148 if (mustfree)
6149 vim_free(string);
6150
6151 /* next try expanding things like $VIM and ${HOME} */
6152 string = expand_env_save(name - 1);
6153 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006154 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006155 }
6156 name[len] = cc;
6157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 rettv->v_type = VAR_STRING;
6159 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161
6162 return OK;
6163}
6164
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006165
6166
6167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006169 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006172var2fpos(
6173 typval_T *varp,
6174 int dollar_lnum, /* TRUE when $ is last line */
6175 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006177 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006179 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
Bram Moolenaara5525202006-03-02 22:52:09 +00006181 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006182 if (varp->v_type == VAR_LIST)
6183 {
6184 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006185 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006186 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006187 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006188
6189 l = varp->vval.v_list;
6190 if (l == NULL)
6191 return NULL;
6192
6193 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006194 pos.lnum = list_find_nr(l, 0L, &error);
6195 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 return NULL; /* invalid line number */
6197
6198 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006199 pos.col = list_find_nr(l, 1L, &error);
6200 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006201 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006202 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006203
6204 /* We accept "$" for the column number: last column. */
6205 li = list_find(l, 1L);
6206 if (li != NULL && li->li_tv.v_type == VAR_STRING
6207 && li->li_tv.vval.v_string != NULL
6208 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6209 pos.col = len + 1;
6210
Bram Moolenaara5525202006-03-02 22:52:09 +00006211 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006212 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006214 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006215
Bram Moolenaara5525202006-03-02 22:52:09 +00006216#ifdef FEAT_VIRTUALEDIT
6217 /* Get the virtual offset. Defaults to zero. */
6218 pos.coladd = list_find_nr(l, 2L, &error);
6219 if (error)
6220 pos.coladd = 0;
6221#endif
6222
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006223 return &pos;
6224 }
6225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006226 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006227 if (name == NULL)
6228 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006229 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006231 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6232 {
6233 if (VIsual_active)
6234 return &VIsual;
6235 return &curwin->w_cursor;
6236 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006237 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006239 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6241 return NULL;
6242 return pp;
6243 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006244
6245#ifdef FEAT_VIRTUALEDIT
6246 pos.coladd = 0;
6247#endif
6248
Bram Moolenaar477933c2007-07-17 14:32:23 +00006249 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006250 {
6251 pos.col = 0;
6252 if (name[1] == '0') /* "w0": first visible line */
6253 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006254 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006255 /* In silent Ex mode topline is zero, but that's not a valid line
6256 * number; use one instead. */
6257 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006258 return &pos;
6259 }
6260 else if (name[1] == '$') /* "w$": last visible line */
6261 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006262 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006263 /* In silent Ex mode botline is zero, return zero then. */
6264 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006265 return &pos;
6266 }
6267 }
6268 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006270 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272 pos.lnum = curbuf->b_ml.ml_line_count;
6273 pos.col = 0;
6274 }
6275 else
6276 {
6277 pos.lnum = curwin->w_cursor.lnum;
6278 pos.col = (colnr_T)STRLEN(ml_get_curline());
6279 }
6280 return &pos;
6281 }
6282 return NULL;
6283}
6284
6285/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006286 * Convert list in "arg" into a position and optional file number.
6287 * When "fnump" is NULL there is no file number, only 3 items.
6288 * Note that the column is passed on as-is, the caller may want to decrement
6289 * it to use 1 for the first column.
6290 * Return FAIL when conversion is not possible, doesn't check the position for
6291 * validity.
6292 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006294list2fpos(
6295 typval_T *arg,
6296 pos_T *posp,
6297 int *fnump,
6298 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006299{
6300 list_T *l = arg->vval.v_list;
6301 long i = 0;
6302 long n;
6303
Bram Moolenaar493c1782014-05-28 14:34:46 +02006304 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6305 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006306 if (arg->v_type != VAR_LIST
6307 || l == NULL
6308 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006309 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006310 return FAIL;
6311
6312 if (fnump != NULL)
6313 {
6314 n = list_find_nr(l, i++, NULL); /* fnum */
6315 if (n < 0)
6316 return FAIL;
6317 if (n == 0)
6318 n = curbuf->b_fnum; /* current buffer */
6319 *fnump = n;
6320 }
6321
6322 n = list_find_nr(l, i++, NULL); /* lnum */
6323 if (n < 0)
6324 return FAIL;
6325 posp->lnum = n;
6326
6327 n = list_find_nr(l, i++, NULL); /* col */
6328 if (n < 0)
6329 return FAIL;
6330 posp->col = n;
6331
6332#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006333 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006334 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006335 posp->coladd = 0;
6336 else
6337 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006338#endif
6339
Bram Moolenaar493c1782014-05-28 14:34:46 +02006340 if (curswantp != NULL)
6341 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6342
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006343 return OK;
6344}
6345
6346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 * Get the length of an environment variable name.
6348 * Advance "arg" to the first character after the name.
6349 * Return 0 for error.
6350 */
6351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 char_u *p;
6355 int len;
6356
6357 for (p = *arg; vim_isIDc(*p); ++p)
6358 ;
6359 if (p == *arg) /* no name found */
6360 return 0;
6361
6362 len = (int)(p - *arg);
6363 *arg = p;
6364 return len;
6365}
6366
6367/*
6368 * Get the length of the name of a function or internal variable.
6369 * "arg" is advanced to the first non-white character after the name.
6370 * Return 0 if something is wrong.
6371 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006372 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006373get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 char_u *p;
6376 int len;
6377
6378 /* Find the end of the name. */
6379 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006380 {
6381 if (*p == ':')
6382 {
6383 /* "s:" is start of "s:var", but "n:" is not and can be used in
6384 * slice "[n:]". Also "xx:" is not a namespace. */
6385 len = (int)(p - *arg);
6386 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6387 || len > 1)
6388 break;
6389 }
6390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 if (p == *arg) /* no name found */
6392 return 0;
6393
6394 len = (int)(p - *arg);
6395 *arg = skipwhite(p);
6396
6397 return len;
6398}
6399
6400/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006401 * Get the length of the name of a variable or function.
6402 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006404 * Return -1 if curly braces expansion failed.
6405 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 * If the name contains 'magic' {}'s, expand them and return the
6407 * expanded name in an allocated string via 'alias' - caller must free.
6408 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410get_name_len(
6411 char_u **arg,
6412 char_u **alias,
6413 int evaluate,
6414 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 char_u *p;
6418 char_u *expr_start;
6419 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 *alias = NULL; /* default to no alias */
6422
6423 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6424 && (*arg)[2] == (int)KE_SNR)
6425 {
6426 /* hard coded <SNR>, already translated */
6427 *arg += 3;
6428 return get_id_len(arg) + 3;
6429 }
6430 len = eval_fname_script(*arg);
6431 if (len > 0)
6432 {
6433 /* literal "<SID>", "s:" or "<SNR>" */
6434 *arg += len;
6435 }
6436
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006438 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006440 p = find_name_end(*arg, &expr_start, &expr_end,
6441 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (expr_start != NULL)
6443 {
6444 char_u *temp_string;
6445
6446 if (!evaluate)
6447 {
6448 len += (int)(p - *arg);
6449 *arg = skipwhite(p);
6450 return len;
6451 }
6452
6453 /*
6454 * Include any <SID> etc in the expanded string:
6455 * Thus the -len here.
6456 */
6457 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6458 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006459 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 *alias = temp_string;
6461 *arg = skipwhite(p);
6462 return (int)STRLEN(temp_string);
6463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006466 // Only give an error when there is something, otherwise it will be
6467 // reported at a higher level.
6468 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006469 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471 return len;
6472}
6473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474/*
6475 * Find the end of a variable or function name, taking care of magic braces.
6476 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6477 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006478 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006479 * Return a pointer to just after the name. Equal to "arg" if there is no
6480 * valid name.
6481 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483find_name_end(
6484 char_u *arg,
6485 char_u **expr_start,
6486 char_u **expr_end,
6487 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006489 int mb_nest = 0;
6490 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006492 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006494 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006496 *expr_start = NULL;
6497 *expr_end = NULL;
6498 }
6499
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006500 /* Quick check for valid starting character. */
6501 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6502 return arg;
6503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 for (p = arg; *p != NUL
6505 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006507 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006509 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006510 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006511 if (*p == '\'')
6512 {
6513 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006514 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006515 ;
6516 if (*p == NUL)
6517 break;
6518 }
6519 else if (*p == '"')
6520 {
6521 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006522 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006523 if (*p == '\\' && p[1] != NUL)
6524 ++p;
6525 if (*p == NUL)
6526 break;
6527 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006528 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6529 {
6530 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006531 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006532 len = (int)(p - arg);
6533 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006534 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006535 break;
6536 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006540 if (*p == '[')
6541 ++br_nest;
6542 else if (*p == ']')
6543 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 if (*p == '{')
6549 {
6550 mb_nest++;
6551 if (expr_start != NULL && *expr_start == NULL)
6552 *expr_start = p;
6553 }
6554 else if (*p == '}')
6555 {
6556 mb_nest--;
6557 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6558 *expr_end = p;
6559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
6562
6563 return p;
6564}
6565
6566/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006567 * Expands out the 'magic' {}'s in a variable/function name.
6568 * Note that this can call itself recursively, to deal with
6569 * constructs like foo{bar}{baz}{bam}
6570 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6571 * "in_start" ^
6572 * "expr_start" ^
6573 * "expr_end" ^
6574 * "in_end" ^
6575 *
6576 * Returns a new allocated string, which the caller must free.
6577 * Returns NULL for failure.
6578 */
6579 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580make_expanded_name(
6581 char_u *in_start,
6582 char_u *expr_start,
6583 char_u *expr_end,
6584 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006585{
6586 char_u c1;
6587 char_u *retval = NULL;
6588 char_u *temp_result;
6589 char_u *nextcmd = NULL;
6590
6591 if (expr_end == NULL || in_end == NULL)
6592 return NULL;
6593 *expr_start = NUL;
6594 *expr_end = NUL;
6595 c1 = *in_end;
6596 *in_end = NUL;
6597
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006598 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006599 if (temp_result != NULL && nextcmd == NULL)
6600 {
6601 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6602 + (in_end - expr_end) + 1));
6603 if (retval != NULL)
6604 {
6605 STRCPY(retval, in_start);
6606 STRCAT(retval, temp_result);
6607 STRCAT(retval, expr_end + 1);
6608 }
6609 }
6610 vim_free(temp_result);
6611
6612 *in_end = c1; /* put char back for error messages */
6613 *expr_start = '{';
6614 *expr_end = '}';
6615
6616 if (retval != NULL)
6617 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006618 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006619 if (expr_start != NULL)
6620 {
6621 /* Further expansion! */
6622 temp_result = make_expanded_name(retval, expr_start,
6623 expr_end, temp_result);
6624 vim_free(retval);
6625 retval = temp_result;
6626 }
6627 }
6628
6629 return retval;
6630}
6631
6632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006634 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006637eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006639 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6640}
6641
6642/*
6643 * Return TRUE if character "c" can be used as the first character in a
6644 * variable or function name (excluding '{' and '}').
6645 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006648{
6649 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650}
6651
6652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 * Set number v: variable to "val".
6654 */
6655 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006656set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006658 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659}
6660
6661/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006662 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006664 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668}
6669
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006670/*
6671 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006672 * If the String variable has never been set, return an empty string.
6673 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006674 */
6675 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006677{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006678 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006679}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006680
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006682 * Get List v: variable value. Caller must take care of reference count when
6683 * needed.
6684 */
6685 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006687{
6688 return vimvars[idx].vv_list;
6689}
6690
6691/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006692 * Get Dict v: variable value. Caller must take care of reference count when
6693 * needed.
6694 */
6695 dict_T *
6696get_vim_var_dict(int idx)
6697{
6698 return vimvars[idx].vv_dict;
6699}
6700
6701/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006702 * Set v:char to character "c".
6703 */
6704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006706{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006707 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006708
6709#ifdef FEAT_MBYTE
6710 if (has_mbyte)
6711 buf[(*mb_char2bytes)(c, buf)] = NUL;
6712 else
6713#endif
6714 {
6715 buf[0] = c;
6716 buf[1] = NUL;
6717 }
6718 set_vim_var_string(VV_CHAR, buf, -1);
6719}
6720
6721/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006722 * Set v:count to "count" and v:count1 to "count1".
6723 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 */
6725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006726set_vcount(
6727 long count,
6728 long count1,
6729 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006731 if (set_prevcount)
6732 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006733 vimvars[VV_COUNT].vv_nr = count;
6734 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735}
6736
6737/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006738 * Save variables that might be changed as a side effect. Used when executing
6739 * a timer callback.
6740 */
6741 void
6742save_vimvars(vimvars_save_T *vvsave)
6743{
6744 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6745 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6746 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6747}
6748
6749/*
6750 * Restore variables saved by save_vimvars().
6751 */
6752 void
6753restore_vimvars(vimvars_save_T *vvsave)
6754{
6755 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6756 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6757 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6758}
6759
6760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 * Set string v: variable to a copy of "val".
6762 */
6763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006764set_vim_var_string(
6765 int idx,
6766 char_u *val,
6767 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
Bram Moolenaara542c682016-01-31 16:28:04 +01006769 clear_tv(&vimvars[idx].vv_di.di_tv);
6770 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006772 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777}
6778
6779/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006780 * Set List v: variable to "val".
6781 */
6782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006784{
Bram Moolenaara542c682016-01-31 16:28:04 +01006785 clear_tv(&vimvars[idx].vv_di.di_tv);
6786 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006787 vimvars[idx].vv_list = val;
6788 if (val != NULL)
6789 ++val->lv_refcount;
6790}
6791
6792/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006793 * Set Dictionary v: variable to "val".
6794 */
6795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006796set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006797{
Bram Moolenaara542c682016-01-31 16:28:04 +01006798 clear_tv(&vimvars[idx].vv_di.di_tv);
6799 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006800 vimvars[idx].vv_dict = val;
6801 if (val != NULL)
6802 {
6803 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006804 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006805 }
6806}
6807
6808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 * Set v:register if needed.
6810 */
6811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006812set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814 char_u regname;
6815
6816 if (c == 0 || c == ' ')
6817 regname = '"';
6818 else
6819 regname = c;
6820 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006821 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 set_vim_var_string(VV_REG, &regname, 1);
6823}
6824
6825/*
6826 * Get or set v:exception. If "oldval" == NULL, return the current value.
6827 * Otherwise, restore the value to "oldval" and return NULL.
6828 * Must always be called in pairs to save and restore v:exception! Does not
6829 * take care of memory allocations.
6830 */
6831 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006832v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 return NULL;
6839}
6840
6841/*
6842 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6843 * Otherwise, restore the value to "oldval" and return NULL.
6844 * Must always be called in pairs to save and restore v:throwpoint! Does not
6845 * take care of memory allocations.
6846 */
6847 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006848v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 return NULL;
6855}
6856
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857/*
6858 * Set v:cmdarg.
6859 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6860 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6861 * Must always be called in pairs!
6862 */
6863 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006864set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 char_u *oldval;
6867 char_u *newval;
6868 unsigned len;
6869
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006871 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006873 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006874 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006875 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006878 if (eap->force_bin == FORCE_BIN)
6879 len = 6;
6880 else if (eap->force_bin == FORCE_NOBIN)
6881 len = 8;
6882 else
6883 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006884
6885 if (eap->read_edit)
6886 len += 7;
6887
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006888 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006889 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006890# ifdef FEAT_MBYTE
6891 if (eap->force_enc != 0)
6892 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006893 if (eap->bad_char != 0)
6894 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006895# endif
6896
6897 newval = alloc(len + 1);
6898 if (newval == NULL)
6899 return NULL;
6900
6901 if (eap->force_bin == FORCE_BIN)
6902 sprintf((char *)newval, " ++bin");
6903 else if (eap->force_bin == FORCE_NOBIN)
6904 sprintf((char *)newval, " ++nobin");
6905 else
6906 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907
6908 if (eap->read_edit)
6909 STRCAT(newval, " ++edit");
6910
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006911 if (eap->force_ff != 0)
6912 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006913 eap->force_ff == 'u' ? "unix"
6914 : eap->force_ff == 'd' ? "dos"
6915 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006916#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006917 if (eap->force_enc != 0)
6918 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6919 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006920 if (eap->bad_char == BAD_KEEP)
6921 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6922 else if (eap->bad_char == BAD_DROP)
6923 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6924 else if (eap->bad_char != 0)
6925 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006926#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006928 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
6931/*
6932 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006933 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006936get_var_tv(
6937 char_u *name,
6938 int len, /* length of "name" */
6939 typval_T *rettv, /* NULL when only checking existence */
6940 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6941 int verbose, /* may give error message */
6942 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949 /* truncate the name, so that we can use strcmp() */
6950 cc = name[len];
6951 name[len] = NUL;
6952
6953 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 * Check for user-defined variables.
6955 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006956 v = find_var(name, NULL, no_autoload);
6957 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006959 tv = &v->di_tv;
6960 if (dip != NULL)
6961 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 }
6963
Bram Moolenaare9a41262005-01-15 22:18:47 +00006964 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006966 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006967 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 ret = FAIL;
6969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006970 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 name[len] = cc;
6974
6975 return ret;
6976}
6977
6978/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006979 * Check if variable "name[len]" is a local variable or an argument.
6980 * If so, "*eval_lavars_used" is set to TRUE.
6981 */
6982 static void
6983check_vars(char_u *name, int len)
6984{
6985 int cc;
6986 char_u *varname;
6987 hashtab_T *ht;
6988
6989 if (eval_lavars_used == NULL)
6990 return;
6991
6992 /* truncate the name, so that we can use strcmp() */
6993 cc = name[len];
6994 name[len] = NUL;
6995
6996 ht = find_var_ht(name, &varname);
6997 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6998 {
6999 if (find_var(name, NULL, TRUE) != NULL)
7000 *eval_lavars_used = TRUE;
7001 }
7002
7003 name[len] = cc;
7004}
7005
7006/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007007 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7008 * Also handle function call with Funcref variable: func(expr)
7009 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7010 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007012handle_subscript(
7013 char_u **arg,
7014 typval_T *rettv,
7015 int evaluate, /* do more than finding the end */
7016 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007017{
7018 int ret = OK;
7019 dict_T *selfdict = NULL;
7020 char_u *s;
7021 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007022 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007023
7024 while (ret == OK
7025 && (**arg == '['
7026 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007027 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7028 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007029 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007030 {
7031 if (**arg == '(')
7032 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007033 partial_T *pt = NULL;
7034
Bram Moolenaard9fba312005-06-26 22:34:35 +00007035 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007036 if (evaluate)
7037 {
7038 functv = *rettv;
7039 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007040
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007041 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007042 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007043 {
7044 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007045 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007046 }
7047 else
7048 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007049 }
7050 else
7051 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007052 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007053 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007054 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055
7056 /* Clear the funcref afterwards, so that deleting it while
7057 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007058 if (evaluate)
7059 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007060
7061 /* Stop the expression evaluation when immediately aborting on
7062 * error, or when an interrupt occurred or an exception was thrown
7063 * but not caught. */
7064 if (aborting())
7065 {
7066 if (ret == OK)
7067 clear_tv(rettv);
7068 ret = FAIL;
7069 }
7070 dict_unref(selfdict);
7071 selfdict = NULL;
7072 }
7073 else /* **arg == '[' || **arg == '.' */
7074 {
7075 dict_unref(selfdict);
7076 if (rettv->v_type == VAR_DICT)
7077 {
7078 selfdict = rettv->vval.v_dict;
7079 if (selfdict != NULL)
7080 ++selfdict->dv_refcount;
7081 }
7082 else
7083 selfdict = NULL;
7084 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7085 {
7086 clear_tv(rettv);
7087 ret = FAIL;
7088 }
7089 }
7090 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007091
Bram Moolenaar1d429612016-05-24 15:44:17 +02007092 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7093 * Don't do this when "Func" is already a partial that was bound
7094 * explicitly (pt_auto is FALSE). */
7095 if (selfdict != NULL
7096 && (rettv->v_type == VAR_FUNC
7097 || (rettv->v_type == VAR_PARTIAL
7098 && (rettv->vval.v_partial->pt_auto
7099 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007100 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007101
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007102 dict_unref(selfdict);
7103 return ret;
7104}
7105
7106/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007107 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 * value).
7109 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007110 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114}
7115
7116/*
7117 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 * The string "s" must have been allocated, it is consumed.
7119 * Return NULL for out of memory, the variable otherwise.
7120 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007122alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007126 rettv = alloc_tv();
7127 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007129 rettv->v_type = VAR_STRING;
7130 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 }
7132 else
7133 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007134 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135}
7136
7137/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007138 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 if (varp != NULL)
7144 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007145 switch (varp->v_type)
7146 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007147 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007148 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007149 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007150 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007151 vim_free(varp->vval.v_string);
7152 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007153 case VAR_PARTIAL:
7154 partial_unref(varp->vval.v_partial);
7155 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007156 case VAR_BLOB:
7157 blob_unref(varp->vval.v_blob);
7158 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007159 case VAR_LIST:
7160 list_unref(varp->vval.v_list);
7161 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007162 case VAR_DICT:
7163 dict_unref(varp->vval.v_dict);
7164 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007165 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007166#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007167 job_unref(varp->vval.v_job);
7168 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007169#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007170 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007172 channel_unref(varp->vval.v_channel);
7173 break;
7174#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007175 case VAR_NUMBER:
7176 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007177 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007178 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007179 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 vim_free(varp);
7182 }
7183}
7184
7185/*
7186 * Free the memory for a variable value and set the value to NULL or 0.
7187 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 if (varp != NULL)
7192 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007193 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007195 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007196 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007197 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007198 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007199 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007200 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007201 case VAR_PARTIAL:
7202 partial_unref(varp->vval.v_partial);
7203 varp->vval.v_partial = NULL;
7204 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007205 case VAR_BLOB:
7206 blob_unref(varp->vval.v_blob);
7207 varp->vval.v_blob = NULL;
7208 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007209 case VAR_LIST:
7210 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007212 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 case VAR_DICT:
7214 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007218 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007219 varp->vval.v_number = 0;
7220 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007221 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007222#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007223 varp->vval.v_float = 0.0;
7224 break;
7225#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007226 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007227#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007228 job_unref(varp->vval.v_job);
7229 varp->vval.v_job = NULL;
7230#endif
7231 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007232 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007233#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007234 channel_unref(varp->vval.v_channel);
7235 varp->vval.v_channel = NULL;
7236#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007237 case VAR_UNKNOWN:
7238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007240 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 }
7242}
7243
7244/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007245 * Set the value of a variable to NULL without freeing items.
7246 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249{
7250 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007252}
7253
7254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 * Get the number value of a variable.
7256 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007257 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007258 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007259 * caller of incompatible types: it sets *denote to TRUE if "denote"
7260 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007262 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007263tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265 int error = FALSE;
7266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007267 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007268}
7269
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007270 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007271tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007272{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007273 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007277 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007278 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007279 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007280#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007281 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007282 break;
7283#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007285 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007286 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007287 break;
7288 case VAR_STRING:
7289 if (varp->vval.v_string != NULL)
7290 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007291 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007292 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007293 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007294 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007295 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007296 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007297 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007298 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007299 case VAR_SPECIAL:
7300 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7301 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007302 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007303#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007304 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007305 break;
7306#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007307 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007308#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007309 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007310 break;
7311#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007312 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007313 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007314 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007315 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007316 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 if (denote == NULL) /* useful for values that must be unsigned */
7320 n = -1;
7321 else
7322 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324}
7325
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007326#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007328tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007329{
7330 switch (varp->v_type)
7331 {
7332 case VAR_NUMBER:
7333 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007334 case VAR_FLOAT:
7335 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007336 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007337 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007338 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007339 break;
7340 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007341 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007342 break;
7343 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007344 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007345 break;
7346 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007347 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007348 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007349 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007350 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007351 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007352 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007353# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007354 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007355 break;
7356# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007357 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007358# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007359 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007360 break;
7361# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007362 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007363 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007364 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007365 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007366 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007367 break;
7368 }
7369 return 0;
7370}
7371#endif
7372
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 * Get the string value of a variable.
7375 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007376 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7377 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 * If the String variable has never been set, return an empty string.
7379 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007380 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007381 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007383 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007384tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 static char_u mybuf[NUMBUFLEN];
7387
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007388 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389}
7390
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007391 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007392tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007394 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007395
7396 return res != NULL ? res : (char_u *)"";
7397}
7398
Bram Moolenaar7d647822014-04-05 21:28:56 +02007399/*
7400 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7401 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007402 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007403tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404{
7405 static char_u mybuf[NUMBUFLEN];
7406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007407 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007408}
7409
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007410 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007411tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007413 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007415 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007416 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007417 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418 return buf;
7419 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007420 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007421 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007422 break;
7423 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007424 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 break;
7426 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007427 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007428 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007429 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007430#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007431 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007432 break;
7433#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007434 case VAR_STRING:
7435 if (varp->vval.v_string != NULL)
7436 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007437 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007438 case VAR_SPECIAL:
7439 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7440 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007441 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007442 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007443 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007444 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007445#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007446 {
7447 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007448 char *status;
7449
7450 if (job == NULL)
7451 return (char_u *)"no process";
7452 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007453 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007454 : "run";
7455# ifdef UNIX
7456 vim_snprintf((char *)buf, NUMBUFLEN,
7457 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007458# elif defined(WIN32)
7459 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007460 "process %ld %s",
7461 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007462 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007463# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007464 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007465 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7466# endif
7467 return buf;
7468 }
7469#endif
7470 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007471 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007472#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007473 {
7474 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007475 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007476
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007477 if (channel == NULL)
7478 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7479 else
7480 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007481 "channel %d %s", channel->ch_id, status);
7482 return buf;
7483 }
7484#endif
7485 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007486 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007487 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007490 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491}
7492
7493/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007494 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7495 * string() on Dict, List, etc.
7496 */
7497 char_u *
7498tv_stringify(typval_T *varp, char_u *buf)
7499{
7500 if (varp->v_type == VAR_LIST
7501 || varp->v_type == VAR_DICT
7502 || varp->v_type == VAR_FUNC
7503 || varp->v_type == VAR_PARTIAL
7504 || varp->v_type == VAR_FLOAT)
7505 {
7506 typval_T tmp;
7507
7508 f_string(varp, &tmp);
7509 tv_get_string_buf(&tmp, buf);
7510 clear_tv(varp);
7511 *varp = tmp;
7512 return tmp.vval.v_string;
7513 }
7514 return tv_get_string_buf(varp, buf);
7515}
7516
7517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 * Find variable "name" in the list of variables.
7519 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007520 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007521 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007524 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007525find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007529 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
Bram Moolenaara7043832005-01-21 11:56:39 +00007531 ht = find_var_ht(name, &varname);
7532 if (htp != NULL)
7533 *htp = ht;
7534 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007536 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7537 if (ret != NULL)
7538 return ret;
7539
7540 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007541 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542}
7543
7544/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007545 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007546 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549find_var_in_ht(
7550 hashtab_T *ht,
7551 int htname,
7552 char_u *varname,
7553 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007554{
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 hashitem_T *hi;
7556
7557 if (*varname == NUL)
7558 {
7559 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007560 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007562 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 case 'g': return &globvars_var;
7564 case 'v': return &vimvars_var;
7565 case 'b': return &curbuf->b_bufvar;
7566 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007567 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007568 case 'l': return get_funccal_local_var();
7569 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 }
7571 return NULL;
7572 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007573
7574 hi = hash_find(ht, varname);
7575 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007576 {
7577 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007578 * worked find the variable again. Don't auto-load a script if it was
7579 * loaded already, otherwise it would be loaded every time when
7580 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007581 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007582 {
7583 /* Note: script_autoload() may make "hi" invalid. It must either
7584 * be obtained again or not used. */
7585 if (!script_autoload(varname, FALSE) || aborting())
7586 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007588 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 if (HASHITEM_EMPTY(hi))
7590 return NULL;
7591 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007593}
7594
7595/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007597 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007598 * Set "varname" to the start of name without ':'.
7599 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007600 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007601find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007603 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007604 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007605
Bram Moolenaar73627d02015-08-11 15:46:09 +02007606 if (name[0] == NUL)
7607 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (name[1] != ':')
7609 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007610 /* The name must not start with a colon or #. */
7611 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 return NULL;
7613 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007614
7615 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007616 hi = hash_find(&compat_hashtab, name);
7617 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007618 return &compat_hashtab;
7619
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007620 ht = get_funccal_local_ht();
7621 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007623 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007626 if (*name == 'g') /* global variable */
7627 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007628 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7629 */
7630 if (vim_strchr(name + 2, ':') != NULL
7631 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007632 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007634 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007636 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007637 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007638 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 if (*name == 'v') /* v: variable */
7640 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007641 if (*name == 'a') /* a: function argument */
7642 return get_funccal_args_ht();
7643 if (*name == 'l') /* l: local function variable */
7644 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007646 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7647 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 return NULL;
7649}
7650
7651/*
7652 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007653 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 * Returns NULL when it doesn't exist.
7655 */
7656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007657get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007661 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (v == NULL)
7663 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007664 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665}
7666
7667/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 * sourcing this script and when executing functions defined in the script.
7670 */
7671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007672new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
Bram Moolenaara7043832005-01-21 11:56:39 +00007674 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 hashtab_T *ht;
7676 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007677
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7679 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007680 /* Re-allocating ga_data means that an ht_array pointing to
7681 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007683 for (i = 1; i <= ga_scripts.ga_len; ++i)
7684 {
7685 ht = &SCRIPT_VARS(i);
7686 if (ht->ht_mask == HT_INIT_SIZE - 1)
7687 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007688 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007690 }
7691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 while (ga_scripts.ga_len < id)
7693 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007694 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007695 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007696 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699 }
7700}
7701
7702/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7704 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 */
7706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007707init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007710 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007711 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007712 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007713 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 dict_var->di_tv.vval.v_dict = dict;
7715 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007716 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7718 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719}
7720
7721/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007722 * Unreference a dictionary initialized by init_var_dict().
7723 */
7724 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007725unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007726{
7727 /* Now the dict needs to be freed if no one else is using it, go back to
7728 * normal reference counting. */
7729 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7730 dict_unref(dict);
7731}
7732
7733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 * Frees all allocated variables and the value they contain.
7736 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007739vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007740{
7741 vars_clear_ext(ht, TRUE);
7742}
7743
7744/*
7745 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7746 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007748vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
Bram Moolenaara7043832005-01-21 11:56:39 +00007750 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 hashitem_T *hi;
7752 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007755 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007756 for (hi = ht->ht_array; todo > 0; ++hi)
7757 {
7758 if (!HASHITEM_EMPTY(hi))
7759 {
7760 --todo;
7761
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007763 * ht_array might change then. hash_clear() takes care of it
7764 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007765 v = HI2DI(hi);
7766 if (free_val)
7767 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007768 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007769 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007770 }
7771 }
7772 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007773 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774}
7775
Bram Moolenaara7043832005-01-21 11:56:39 +00007776/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 * Delete a variable from hashtab "ht" at item "hi".
7778 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007781delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007784
7785 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 clear_tv(&di->di_tv);
7787 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790/*
7791 * List the value of one internal variable.
7792 */
7793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 char_u *tofree;
7797 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007800 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007802 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804}
7805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007807list_one_var_a(
7808 char_u *prefix,
7809 char_u *name,
7810 int type,
7811 char_u *string,
7812 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
Bram Moolenaar31859182007-08-14 20:41:13 +00007814 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7815 msg_start();
7816 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 if (name != NULL) /* "a:" vars don't have a name stored */
7818 msg_puts(name);
7819 msg_putchar(' ');
7820 msg_advance(22);
7821 if (type == VAR_NUMBER)
7822 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007823 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007824 msg_putchar('*');
7825 else if (type == VAR_LIST)
7826 {
7827 msg_putchar('[');
7828 if (*string == '[')
7829 ++string;
7830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 else if (type == VAR_DICT)
7832 {
7833 msg_putchar('{');
7834 if (*string == '{')
7835 ++string;
7836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 else
7838 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007842 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007844 if (*first)
7845 {
7846 msg_clr_eos();
7847 *first = FALSE;
7848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
7851/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 * If the variable already exists, the value is updated.
7854 * Otherwise the variable is created.
7855 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007857set_var(
7858 char_u *name,
7859 typval_T *tv,
7860 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007866 ht = find_var_ht(name, &varname);
7867 if (ht == NULL || *varname == NUL)
7868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007869 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007870 return;
7871 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007872 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007873
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007874 /* Search in parent scope which is possible to reference from lambda */
7875 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007876 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007877
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007878 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7879 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007880 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007885 if (var_check_ro(v->di_flags, name, FALSE)
7886 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007888
7889 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007890 * Handle setting internal v: variables separately where needed to
7891 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 */
7893 if (ht == &vimvarht)
7894 {
7895 if (v->di_tv.v_type == VAR_STRING)
7896 {
7897 vim_free(v->di_tv.vval.v_string);
7898 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007899 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 else
7901 {
7902 /* Take over the string to avoid an extra alloc/free. */
7903 v->di_tv.vval.v_string = tv->vval.v_string;
7904 tv->vval.v_string = NULL;
7905 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007906 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007908 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007910 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911 if (STRCMP(varname, "searchforward") == 0)
7912 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007913#ifdef FEAT_SEARCH_EXTRA
7914 else if (STRCMP(varname, "hlsearch") == 0)
7915 {
7916 no_hlsearch = !v->di_tv.vval.v_number;
7917 redraw_all_later(SOME_VALID);
7918 }
7919#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007920 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007921 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007922 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007924 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007925 return;
7926 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007927 }
7928
7929 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 else /* add a new variable */
7932 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007933 /* Can't add "v:" variable. */
7934 if (ht == &vimvarht)
7935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007936 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007937 return;
7938 }
7939
Bram Moolenaar92124a32005-06-17 22:03:40 +00007940 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007941 if (!valid_varname(varname))
7942 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007944 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7945 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007946 if (v == NULL)
7947 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007951 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 return;
7953 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007954 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007956
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007957 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007959 else
7960 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007962 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965}
7966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007968 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007969 * Also give an error message.
7970 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007972var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007973{
7974 if (flags & DI_FLAGS_RO)
7975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007976 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 return TRUE;
7978 }
7979 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007981 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 return TRUE;
7983 }
7984 return FALSE;
7985}
7986
7987/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007988 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7989 * Also give an error message.
7990 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007992var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007993{
7994 if (flags & DI_FLAGS_FIX)
7995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007996 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007997 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007998 return TRUE;
7999 }
8000 return FALSE;
8001}
8002
8003/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008004 * Check if a funcref is assigned to a valid variable name.
8005 * Return TRUE and give an error if not.
8006 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008008var_check_func_name(
8009 char_u *name, /* points to start of variable name */
8010 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008011{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008012 /* Allow for w: b: s: and t:. */
8013 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008014 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8015 ? name[2] : name[0]))
8016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008017 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008018 name);
8019 return TRUE;
8020 }
8021 /* Don't allow hiding a function. When "v" is not NULL we might be
8022 * assigning another function to the same var, the type is checked
8023 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008024 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008026 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008027 name);
8028 return TRUE;
8029 }
8030 return FALSE;
8031}
8032
8033/*
8034 * Check if a variable name is valid.
8035 * Return FALSE and give an error if not.
8036 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008038valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008039{
8040 char_u *p;
8041
8042 for (p = varname; *p != NUL; ++p)
8043 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8044 && *p != AUTOLOAD_CHAR)
8045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008046 semsg(_(e_illvar), varname);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008047 return FALSE;
8048 }
8049 return TRUE;
8050}
8051
8052/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008053 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008054 * Also give an error message, using "name" or _("name") when use_gettext is
8055 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008056 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008058tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008059{
8060 if (lock & VAR_LOCKED)
8061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008062 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008063 name == NULL ? (char_u *)_("Unknown")
8064 : use_gettext ? (char_u *)_(name)
8065 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008066 return TRUE;
8067 }
8068 if (lock & VAR_FIXED)
8069 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008070 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008071 name == NULL ? (char_u *)_("Unknown")
8072 : use_gettext ? (char_u *)_(name)
8073 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008074 return TRUE;
8075 }
8076 return FALSE;
8077}
8078
8079/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008081 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008082 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008083 * It is OK for "from" and "to" to point to the same item. This is used to
8084 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008085 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008087copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008090 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008091 switch (from->v_type)
8092 {
8093 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008094 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 to->vval.v_number = from->vval.v_number;
8096 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008098#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099 to->vval.v_float = from->vval.v_float;
8100 break;
8101#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008102 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008103#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008104 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008105 if (to->vval.v_job != NULL)
8106 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008107 break;
8108#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008109 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008110#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008111 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008112 if (to->vval.v_channel != NULL)
8113 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008114 break;
8115#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 case VAR_STRING:
8117 case VAR_FUNC:
8118 if (from->vval.v_string == NULL)
8119 to->vval.v_string = NULL;
8120 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008121 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008123 if (from->v_type == VAR_FUNC)
8124 func_ref(to->vval.v_string);
8125 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008127 case VAR_PARTIAL:
8128 if (from->vval.v_partial == NULL)
8129 to->vval.v_partial = NULL;
8130 else
8131 {
8132 to->vval.v_partial = from->vval.v_partial;
8133 ++to->vval.v_partial->pt_refcount;
8134 }
8135 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008136 case VAR_BLOB:
8137 if (from->vval.v_blob == NULL)
8138 to->vval.v_blob = NULL;
8139 else
8140 {
8141 to->vval.v_blob = from->vval.v_blob;
8142 ++to->vval.v_blob->bv_refcount;
8143 }
8144 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 case VAR_LIST:
8146 if (from->vval.v_list == NULL)
8147 to->vval.v_list = NULL;
8148 else
8149 {
8150 to->vval.v_list = from->vval.v_list;
8151 ++to->vval.v_list->lv_refcount;
8152 }
8153 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008154 case VAR_DICT:
8155 if (from->vval.v_dict == NULL)
8156 to->vval.v_dict = NULL;
8157 else
8158 {
8159 to->vval.v_dict = from->vval.v_dict;
8160 ++to->vval.v_dict->dv_refcount;
8161 }
8162 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008163 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008164 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165 break;
8166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167}
8168
8169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 * Make a copy of an item.
8171 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008172 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8173 * reference to an already copied list/dict can be used.
8174 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008175 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008177item_copy(
8178 typval_T *from,
8179 typval_T *to,
8180 int deep,
8181 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182{
8183 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008184 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185
Bram Moolenaar33570922005-01-25 22:26:29 +00008186 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008188 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008189 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190 }
8191 ++recurse;
8192
8193 switch (from->v_type)
8194 {
8195 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008196 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 case VAR_STRING:
8198 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008199 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008200 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008201 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008202 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 copy_tv(from, to);
8204 break;
8205 case VAR_LIST:
8206 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008207 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 if (from->vval.v_list == NULL)
8209 to->vval.v_list = NULL;
8210 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8211 {
8212 /* use the copy made earlier */
8213 to->vval.v_list = from->vval.v_list->lv_copylist;
8214 ++to->vval.v_list->lv_refcount;
8215 }
8216 else
8217 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8218 if (to->vval.v_list == NULL)
8219 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008221 case VAR_BLOB:
8222 to->v_type = VAR_BLOB;
8223 if (from->vval.v_blob == NULL)
8224 to->vval.v_blob = NULL;
8225 else if (rettv_blob_alloc(to) == FAIL)
8226 ret = FAIL;
8227 else
8228 {
8229 int len = from->vval.v_blob->bv_ga.ga_len;
8230
8231 to->vval.v_blob->bv_ga.ga_data =
8232 vim_memsave(from->vval.v_blob->bv_ga.ga_data, len);
8233 to->vval.v_blob->bv_ga.ga_len = len;
8234 }
8235 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008236 case VAR_DICT:
8237 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008238 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008239 if (from->vval.v_dict == NULL)
8240 to->vval.v_dict = NULL;
8241 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8242 {
8243 /* use the copy made earlier */
8244 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8245 ++to->vval.v_dict->dv_refcount;
8246 }
8247 else
8248 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8249 if (to->vval.v_dict == NULL)
8250 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008251 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008252 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008253 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008254 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008255 }
8256 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008257 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008258}
8259
8260/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008261 * This function is used by f_input() and f_inputdialog() functions. The third
8262 * argument to f_input() specifies the type of completion to use at the
8263 * prompt. The third argument to f_inputdialog() specifies the value to return
8264 * when the user cancels the prompt.
8265 */
8266 void
8267get_user_input(
8268 typval_T *argvars,
8269 typval_T *rettv,
8270 int inputdialog,
8271 int secret)
8272{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008273 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 char_u *p = NULL;
8275 int c;
8276 char_u buf[NUMBUFLEN];
8277 int cmd_silent_save = cmd_silent;
8278 char_u *defstr = (char_u *)"";
8279 int xp_type = EXPAND_NOTHING;
8280 char_u *xp_arg = NULL;
8281
8282 rettv->v_type = VAR_STRING;
8283 rettv->vval.v_string = NULL;
8284
8285#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008286 /* While starting up, there is no place to enter text. When running tests
8287 * with --not-a-term we assume feedkeys() will be used. */
8288 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 return;
8290#endif
8291
8292 cmd_silent = FALSE; /* Want to see the prompt. */
8293 if (prompt != NULL)
8294 {
8295 /* Only the part of the message after the last NL is considered as
8296 * prompt for the command line */
8297 p = vim_strrchr(prompt, '\n');
8298 if (p == NULL)
8299 p = prompt;
8300 else
8301 {
8302 ++p;
8303 c = *p;
8304 *p = NUL;
8305 msg_start();
8306 msg_clr_eos();
8307 msg_puts_attr(prompt, echo_attr);
8308 msg_didout = FALSE;
8309 msg_starthere();
8310 *p = c;
8311 }
8312 cmdline_row = msg_row;
8313
8314 if (argvars[1].v_type != VAR_UNKNOWN)
8315 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008316 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 if (defstr != NULL)
8318 stuffReadbuffSpec(defstr);
8319
8320 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8321 {
8322 char_u *xp_name;
8323 int xp_namelen;
8324 long argt;
8325
8326 /* input() with a third argument: completion */
8327 rettv->vval.v_string = NULL;
8328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008329 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330 if (xp_name == NULL)
8331 return;
8332
8333 xp_namelen = (int)STRLEN(xp_name);
8334
8335 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8336 &xp_arg) == FAIL)
8337 return;
8338 }
8339 }
8340
8341 if (defstr != NULL)
8342 {
8343 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345 ex_normal_busy = 0;
8346 rettv->vval.v_string =
8347 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8348 xp_type, xp_arg);
8349 ex_normal_busy = save_ex_normal_busy;
8350 }
8351 if (inputdialog && rettv->vval.v_string == NULL
8352 && argvars[1].v_type != VAR_UNKNOWN
8353 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008354 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 &argvars[2], buf));
8356
8357 vim_free(xp_arg);
8358
8359 /* since the user typed this, no need to wait for return */
8360 need_wait_return = FALSE;
8361 msg_didout = FALSE;
8362 }
8363 cmd_silent = cmd_silent_save;
8364}
8365
8366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 * ":echo expr1 ..." print each argument separated with a space, add a
8368 * newline at the end.
8369 * ":echon expr1 ..." print each argument plain.
8370 */
8371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008372ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008376 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 char_u *p;
8378 int needclr = TRUE;
8379 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008380 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008381 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008382 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 if (eap->skip)
8385 ++emsg_skip;
8386 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8387 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008388 /* If eval1() causes an error message the text from the command may
8389 * still need to be cleared. E.g., "echo 22,44". */
8390 need_clr_eos = needclr;
8391
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {
8395 /*
8396 * Report the invalid expression unless the expression evaluation
8397 * has been cancelled due to an aborting error, an interrupt, or an
8398 * exception.
8399 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008400 if (!aborting() && did_emsg == did_emsg_before
8401 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008402 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008403 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 break;
8405 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008406 need_clr_eos = FALSE;
8407
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 if (!eap->skip)
8409 {
8410 if (atstart)
8411 {
8412 atstart = FALSE;
8413 /* Call msg_start() after eval1(), evaluating the expression
8414 * may cause a message to appear. */
8415 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008416 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008417 /* Mark the saved text as finishing the line, so that what
8418 * follows is displayed on a new line when scrolling back
8419 * at the more prompt. */
8420 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 }
8424 else if (eap->cmdidx == CMD_echo)
8425 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008426 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008427 if (p != NULL)
8428 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008430 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008432 if (*p != TAB && needclr)
8433 {
8434 /* remove any text still there from the command */
8435 msg_clr_eos();
8436 needclr = FALSE;
8437 }
8438 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
8440 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008441 {
8442#ifdef FEAT_MBYTE
8443 if (has_mbyte)
8444 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008445 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008446
8447 (void)msg_outtrans_len_attr(p, i, echo_attr);
8448 p += i - 1;
8449 }
8450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008452 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008455 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 arg = skipwhite(arg);
8459 }
8460 eap->nextcmd = check_nextcmd(arg);
8461
8462 if (eap->skip)
8463 --emsg_skip;
8464 else
8465 {
8466 /* remove text that may still be there from the command */
8467 if (needclr)
8468 msg_clr_eos();
8469 if (eap->cmdidx == CMD_echo)
8470 msg_end();
8471 }
8472}
8473
8474/*
8475 * ":echohl {name}".
8476 */
8477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008478ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008480 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
8483/*
8484 * ":execute expr1 ..." execute the result of an expression.
8485 * ":echomsg expr1 ..." Print a message
8486 * ":echoerr expr1 ..." Print an error
8487 * Each gets spaces around each argument and a newline at the end for
8488 * echo commands
8489 */
8490 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008491ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 int ret = OK;
8496 char_u *p;
8497 garray_T ga;
8498 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008499 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500
8501 ga_init2(&ga, 1, 80);
8502
8503 if (eap->skip)
8504 ++emsg_skip;
8505 while (*arg != NUL && *arg != '|' && *arg != '\n')
8506 {
8507 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008508 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8509 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
8512 if (!eap->skip)
8513 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008514 char_u buf[NUMBUFLEN];
8515
8516 if (eap->cmdidx == CMD_execute)
8517 p = tv_get_string_buf(&rettv, buf);
8518 else
8519 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 len = (int)STRLEN(p);
8521 if (ga_grow(&ga, len + 2) == FAIL)
8522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008523 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 ret = FAIL;
8525 break;
8526 }
8527 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 ga.ga_len += len;
8531 }
8532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 arg = skipwhite(arg);
8535 }
8536
8537 if (ret != FAIL && ga.ga_data != NULL)
8538 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008539 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8540 {
8541 /* Mark the already saved text as finishing the line, so that what
8542 * follows is displayed on a new line when scrolling back at the
8543 * more prompt. */
8544 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008545 }
8546
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008550 out_flush();
8551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 else if (eap->cmdidx == CMD_echoerr)
8553 {
8554 /* We don't want to abort following commands, restore did_emsg. */
8555 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008556 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 if (!force_abort)
8558 did_emsg = save_did_emsg;
8559 }
8560 else if (eap->cmdidx == CMD_execute)
8561 do_cmdline((char_u *)ga.ga_data,
8562 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8563 }
8564
8565 ga_clear(&ga);
8566
8567 if (eap->skip)
8568 --emsg_skip;
8569
8570 eap->nextcmd = check_nextcmd(arg);
8571}
8572
8573/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 * Find window specified by "vp" in tabpage "tp".
8575 */
8576 win_T *
8577find_win_by_nr(
8578 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008579 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008582 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 if (nr < 0)
8585 return NULL;
8586 if (nr == 0)
8587 return curwin;
8588
Bram Moolenaar29323592016-07-24 22:04:11 +02008589 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8590 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 if (nr >= LOWEST_WIN_ID)
8592 {
8593 if (wp->w_id == nr)
8594 return wp;
8595 }
8596 else if (--nr <= 0)
8597 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008598 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 if (nr >= LOWEST_WIN_ID)
8600 return NULL;
8601 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602}
8603
8604/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008605 * Find a window: When using a Window ID in any tab page, when using a number
8606 * in the current tab page.
8607 */
8608 win_T *
8609find_win_by_nr_or_id(typval_T *vp)
8610{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008611 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008612
8613 if (nr >= LOWEST_WIN_ID)
8614 return win_id2wp(vp);
8615 return find_win_by_nr(vp, NULL);
8616}
8617
8618/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 * Find window specified by "wvp" in tabpage "tvp".
8620 */
8621 win_T *
8622find_tabwin(
8623 typval_T *wvp, /* VAR_UNKNOWN for current window */
8624 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8625{
8626 win_T *wp = NULL;
8627 tabpage_T *tp = NULL;
8628 long n;
8629
8630 if (wvp->v_type != VAR_UNKNOWN)
8631 {
8632 if (tvp->v_type != VAR_UNKNOWN)
8633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008634 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 if (n >= 0)
8636 tp = find_tabpage(n);
8637 }
8638 else
8639 tp = curtab;
8640
8641 if (tp != NULL)
8642 wp = find_win_by_nr(wvp, tp);
8643 }
8644 else
8645 wp = curwin;
8646
8647 return wp;
8648}
8649
8650/*
8651 * getwinvar() and gettabwinvar()
8652 */
8653 void
8654getwinvar(
8655 typval_T *argvars,
8656 typval_T *rettv,
8657 int off) /* 1 for gettabwinvar() */
8658{
8659 win_T *win;
8660 char_u *varname;
8661 dictitem_T *v;
8662 tabpage_T *tp = NULL;
8663 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 win_T *oldcurwin;
8665 tabpage_T *oldtabpage;
8666 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008669 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 else
8671 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008673 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 ++emsg_off;
8675
8676 rettv->v_type = VAR_STRING;
8677 rettv->vval.v_string = NULL;
8678
8679 if (win != NULL && varname != NULL)
8680 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 /* Set curwin to be our win, temporarily. Also set the tabpage,
8682 * otherwise the window is not valid. Only do this when needed,
8683 * autocommands get blocked. */
8684 need_switch_win = !(tp == curtab && win == curwin);
8685 if (!need_switch_win
8686 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008688 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008690 if (varname[1] == NUL)
8691 {
8692 /* get all window-local options in a dict */
8693 dict_T *opts = get_winbuf_options(FALSE);
8694
8695 if (opts != NULL)
8696 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008697 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008698 done = TRUE;
8699 }
8700 }
8701 else if (get_option_tv(&varname, rettv, 1) == OK)
8702 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 done = TRUE;
8704 }
8705 else
8706 {
8707 /* Look up the variable. */
8708 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8709 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8710 varname, FALSE);
8711 if (v != NULL)
8712 {
8713 copy_tv(&v->di_tv, rettv);
8714 done = TRUE;
8715 }
8716 }
8717 }
8718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 if (need_switch_win)
8720 /* restore previous notion of curwin */
8721 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 }
8723
8724 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8725 /* use the default return value */
8726 copy_tv(&argvars[off + 2], rettv);
8727
8728 --emsg_off;
8729}
8730
8731/*
8732 * "setwinvar()" and "settabwinvar()" functions
8733 */
8734 void
8735setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8736{
8737 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738 win_T *save_curwin;
8739 tabpage_T *save_curtab;
8740 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 char_u *varname, *winvarname;
8742 typval_T *varp;
8743 char_u nbuf[NUMBUFLEN];
8744 tabpage_T *tp = NULL;
8745
8746 if (check_restricted() || check_secure())
8747 return;
8748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008750 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 else
8752 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 varp = &argvars[off + 2];
8756
8757 if (win != NULL && varname != NULL && varp != NULL)
8758 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 need_switch_win = !(tp == curtab && win == curwin);
8760 if (!need_switch_win
8761 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 {
8763 if (*varname == '&')
8764 {
8765 long numval;
8766 char_u *strval;
8767 int error = FALSE;
8768
8769 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008770 numval = (long)tv_get_number_chk(varp, &error);
8771 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 if (!error && strval != NULL)
8773 set_option_value(varname, numval, strval, OPT_LOCAL);
8774 }
8775 else
8776 {
8777 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8778 if (winvarname != NULL)
8779 {
8780 STRCPY(winvarname, "w:");
8781 STRCPY(winvarname + 2, varname);
8782 set_var(winvarname, varp, TRUE);
8783 vim_free(winvarname);
8784 }
8785 }
8786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 if (need_switch_win)
8788 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 }
8790}
8791
8792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8794 * "arg" points to the "&" or '+' when called, to "option" when returning.
8795 * Returns NULL when no option name found. Otherwise pointer to the char
8796 * after the option name.
8797 */
8798 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008799find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 char_u *p = *arg;
8802
8803 ++p;
8804 if (*p == 'g' && p[1] == ':')
8805 {
8806 *opt_flags = OPT_GLOBAL;
8807 p += 2;
8808 }
8809 else if (*p == 'l' && p[1] == ':')
8810 {
8811 *opt_flags = OPT_LOCAL;
8812 p += 2;
8813 }
8814 else
8815 *opt_flags = 0;
8816
8817 if (!ASCII_ISALPHA(*p))
8818 return NULL;
8819 *arg = p;
8820
8821 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8822 p += 4; /* termcap option */
8823 else
8824 while (ASCII_ISALPHA(*p))
8825 ++p;
8826 return p;
8827}
8828
8829/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008830 * Return the autoload script name for a function or variable name.
8831 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008833 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008834autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008835{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008836 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008837 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008838
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008839 /* Get the script file name: replace '#' with '/', append ".vim". */
8840 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8841 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008842 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008843 STRCPY(scriptname, "autoload/");
8844 STRCAT(scriptname, name);
8845 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8846 STRCAT(scriptname, ".vim");
8847 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8848 *p = '/';
8849 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850}
8851
8852/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008853 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008854 * Return TRUE if a package was loaded.
8855 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008857script_autoload(
8858 char_u *name,
8859 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008860{
8861 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008862 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008863 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008864 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008865
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008866 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008867 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008868 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008869 return FALSE;
8870
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008871 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008872
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008873 /* Find the name in the list of previously loaded package names. Skip
8874 * "autoload/", it's always the same. */
8875 for (i = 0; i < ga_loaded.ga_len; ++i)
8876 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8877 break;
8878 if (!reload && i < ga_loaded.ga_len)
8879 ret = FALSE; /* was loaded already */
8880 else
8881 {
8882 /* Remember the name if it wasn't loaded already. */
8883 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8884 {
8885 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8886 tofree = NULL;
8887 }
8888
8889 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008890 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008891 ret = TRUE;
8892 }
8893
8894 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008895 return ret;
8896}
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8899typedef enum
8900{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008901 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8902 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8903 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904} var_flavour_T;
8905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008907var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 char_u *p = varname;
8910
8911 if (ASCII_ISUPPER(*p))
8912 {
8913 while (*(++p))
8914 if (ASCII_ISLOWER(*p))
8915 return VAR_FLAVOUR_SESSION;
8916 return VAR_FLAVOUR_VIMINFO;
8917 }
8918 else
8919 return VAR_FLAVOUR_DEFAULT;
8920}
8921#endif
8922
8923#if defined(FEAT_VIMINFO) || defined(PROTO)
8924/*
8925 * Restore global vars that start with a capital from the viminfo file
8926 */
8927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008928read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008931 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008933 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
8935 if (!writing && (find_viminfo_parameter('!') != NULL))
8936 {
8937 tab = vim_strchr(virp->vir_line + 1, '\t');
8938 if (tab != NULL)
8939 {
8940 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008941 switch (*tab)
8942 {
8943 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008945 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008946#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008947 case 'D': type = VAR_DICT; break;
8948 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008949 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008950 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
8953 tab = vim_strchr(tab, '\t');
8954 if (tab != NULL)
8955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008956 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008957 if (type == VAR_STRING || type == VAR_DICT
8958 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008959 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008961#ifdef FEAT_FLOAT
8962 else if (type == VAR_FLOAT)
8963 (void)string2float(tab + 1, &tv.vval.v_float);
8964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008966 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008967 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008968 {
8969 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8970
8971 if (etv == NULL)
8972 /* Failed to parse back the dict or list, use it as a
8973 * string. */
8974 tv.v_type = VAR_STRING;
8975 else
8976 {
8977 vim_free(tv.vval.v_string);
8978 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008979 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008980 }
8981 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008982 else if (type == VAR_BLOB)
8983 {
8984 blob_T *blob = string2blob(tv.vval.v_string);
8985
8986 if (blob == NULL)
8987 // Failed to parse back the blob, use it as a string.
8988 tv.v_type = VAR_STRING;
8989 else
8990 {
8991 vim_free(tv.vval.v_string);
8992 tv.v_type = VAR_BLOB;
8993 tv.vval.v_blob = blob;
8994 }
8995 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008996
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008997 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008998 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008999 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009000 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009001
9002 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009003 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009004 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9005 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009006 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 }
9008 }
9009 }
9010
9011 return viminfo_readline(virp);
9012}
9013
9014/*
9015 * Write global vars that start with a capital to the viminfo file
9016 */
9017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009018write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 hashitem_T *hi;
9021 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009022 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009023 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009024 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009026 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027
9028 if (find_viminfo_parameter('!') == NULL)
9029 return;
9030
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009031 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009032
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009033 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009036 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009038 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 this_var = HI2DI(hi);
9040 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009043 {
9044 case VAR_STRING: s = "STR"; break;
9045 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009046 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009047 case VAR_DICT: s = "DIC"; break;
9048 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009049 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009050 case VAR_SPECIAL: s = "XPL"; break;
9051
9052 case VAR_UNKNOWN:
9053 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009055 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009056 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009057 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009060 if (this_var->di_tv.v_type == VAR_SPECIAL)
9061 {
9062 sprintf((char *)numbuf, "%ld",
9063 (long)this_var->di_tv.vval.v_number);
9064 p = numbuf;
9065 tofree = NULL;
9066 }
9067 else
9068 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009069 if (p != NULL)
9070 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009071 vim_free(tofree);
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 }
9074 }
9075}
9076#endif
9077
9078#if defined(FEAT_SESSION) || defined(PROTO)
9079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009080store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 hashitem_T *hi;
9083 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009084 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 char_u *p, *t;
9086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009087 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009090 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009093 this_var = HI2DI(hi);
9094 if ((this_var->di_tv.v_type == VAR_NUMBER
9095 || this_var->di_tv.v_type == VAR_STRING)
9096 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009097 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009098 /* Escape special characters with a backslash. Turn a LF and
9099 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009100 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009101 (char_u *)"\\\"\n\r");
9102 if (p == NULL) /* out of memory */
9103 break;
9104 for (t = p; *t != NUL; ++t)
9105 if (*t == '\n')
9106 *t = 'n';
9107 else if (*t == '\r')
9108 *t = 'r';
9109 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 this_var->di_key,
9111 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9112 : ' ',
9113 p,
9114 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9115 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009116 || put_eol(fd) == FAIL)
9117 {
9118 vim_free(p);
9119 return FAIL;
9120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 vim_free(p);
9122 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009123#ifdef FEAT_FLOAT
9124 else if (this_var->di_tv.v_type == VAR_FLOAT
9125 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9126 {
9127 float_T f = this_var->di_tv.vval.v_float;
9128 int sign = ' ';
9129
9130 if (f < 0)
9131 {
9132 f = -f;
9133 sign = '-';
9134 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009135 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009136 this_var->di_key, sign, f) < 0)
9137 || put_eol(fd) == FAIL)
9138 return FAIL;
9139 }
9140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 }
9142 }
9143 return OK;
9144}
9145#endif
9146
Bram Moolenaar661b1822005-07-28 22:36:45 +00009147/*
9148 * Display script name where an item was last set.
9149 * Should only be invoked when 'verbose' is non-zero.
9150 */
9151 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009152last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009153{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009154 char_u *p;
9155
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009156 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009157 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009158 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009159 if (p != NULL)
9160 {
9161 verbose_enter();
9162 MSG_PUTS(_("\n\tLast set from "));
9163 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009164 if (script_ctx.sc_lnum > 0)
9165 {
9166 MSG_PUTS(_(" line "));
9167 msg_outnum((long)script_ctx.sc_lnum);
9168 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009169 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009170 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009171 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009172 }
9173}
9174
Bram Moolenaar53744302015-07-17 17:38:22 +02009175/* reset v:option_new, v:option_old and v:option_type */
9176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009177reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009178{
9179 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9180 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9181 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9182}
9183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184/*
9185 * Prepare "gap" for an assert error and add the sourcing position.
9186 */
9187 void
9188prepare_assert_error(garray_T *gap)
9189{
9190 char buf[NUMBUFLEN];
9191
9192 ga_init2(gap, 1, 100);
9193 if (sourcing_name != NULL)
9194 {
9195 ga_concat(gap, sourcing_name);
9196 if (sourcing_lnum > 0)
9197 ga_concat(gap, (char_u *)" ");
9198 }
9199 if (sourcing_lnum > 0)
9200 {
9201 sprintf(buf, "line %ld", (long)sourcing_lnum);
9202 ga_concat(gap, (char_u *)buf);
9203 }
9204 if (sourcing_name != NULL || sourcing_lnum > 0)
9205 ga_concat(gap, (char_u *)": ");
9206}
9207
9208/*
9209 * Add an assert error to v:errors.
9210 */
9211 void
9212assert_error(garray_T *gap)
9213{
9214 struct vimvar *vp = &vimvars[VV_ERRORS];
9215
9216 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9217 /* Make sure v:errors is a list. */
9218 set_vim_var_list(VV_ERRORS, list_alloc());
9219 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9220}
9221
Bram Moolenaar65a54642018-04-28 16:56:53 +02009222 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009223assert_equal_common(typval_T *argvars, assert_type_T atype)
9224{
9225 garray_T ga;
9226
9227 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9228 != (atype == ASSERT_EQUAL))
9229 {
9230 prepare_assert_error(&ga);
9231 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9232 atype);
9233 assert_error(&ga);
9234 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009235 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009237 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238}
9239
Bram Moolenaar65a54642018-04-28 16:56:53 +02009240 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009241assert_equalfile(typval_T *argvars)
9242{
9243 char_u buf1[NUMBUFLEN];
9244 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009245 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9246 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009247 garray_T ga;
9248 FILE *fd1;
9249 FILE *fd2;
9250
9251 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009252 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009253
9254 IObuff[0] = NUL;
9255 fd1 = mch_fopen((char *)fname1, READBIN);
9256 if (fd1 == NULL)
9257 {
9258 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9259 }
9260 else
9261 {
9262 fd2 = mch_fopen((char *)fname2, READBIN);
9263 if (fd2 == NULL)
9264 {
9265 fclose(fd1);
9266 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9267 }
9268 else
9269 {
9270 int c1, c2;
9271 long count = 0;
9272
9273 for (;;)
9274 {
9275 c1 = fgetc(fd1);
9276 c2 = fgetc(fd2);
9277 if (c1 == EOF)
9278 {
9279 if (c2 != EOF)
9280 STRCPY(IObuff, "first file is shorter");
9281 break;
9282 }
9283 else if (c2 == EOF)
9284 {
9285 STRCPY(IObuff, "second file is shorter");
9286 break;
9287 }
9288 else if (c1 != c2)
9289 {
9290 vim_snprintf((char *)IObuff, IOSIZE,
9291 "difference at byte %ld", count);
9292 break;
9293 }
9294 ++count;
9295 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009296 fclose(fd1);
9297 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009298 }
9299 }
9300 if (IObuff[0] != NUL)
9301 {
9302 prepare_assert_error(&ga);
9303 ga_concat(&ga, IObuff);
9304 assert_error(&ga);
9305 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009306 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009307 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009308 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009309}
9310
Bram Moolenaar65a54642018-04-28 16:56:53 +02009311 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312assert_match_common(typval_T *argvars, assert_type_T atype)
9313{
9314 garray_T ga;
9315 char_u buf1[NUMBUFLEN];
9316 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009317 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9318 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319
9320 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009321 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009322 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9323 {
9324 prepare_assert_error(&ga);
9325 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9326 atype);
9327 assert_error(&ga);
9328 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009329 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009331 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332}
9333
Bram Moolenaar65a54642018-04-28 16:56:53 +02009334 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009335assert_inrange(typval_T *argvars)
9336{
9337 garray_T ga;
9338 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009339 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9340 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9341 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009342 char_u *tofree;
9343 char msg[200];
9344 char_u numbuf[NUMBUFLEN];
9345
9346 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009347 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009348 if (actual < lower || actual > upper)
9349 {
9350 prepare_assert_error(&ga);
9351 if (argvars[3].v_type != VAR_UNKNOWN)
9352 {
9353 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9354 vim_free(tofree);
9355 }
9356 else
9357 {
9358 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9359 (long)lower, (long)upper, (long)actual);
9360 ga_concat(&ga, (char_u *)msg);
9361 }
9362 assert_error(&ga);
9363 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009364 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009365 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009366 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009367}
9368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369/*
9370 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009371 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009372 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009373 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374assert_bool(typval_T *argvars, int isTrue)
9375{
9376 int error = FALSE;
9377 garray_T ga;
9378
9379 if (argvars[0].v_type == VAR_SPECIAL
9380 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009381 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009383 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 || error)
9385 {
9386 prepare_assert_error(&ga);
9387 fill_assert_error(&ga, &argvars[1],
9388 (char_u *)(isTrue ? "True" : "False"),
9389 NULL, &argvars[0], ASSERT_OTHER);
9390 assert_error(&ga);
9391 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009392 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009394 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395}
9396
Bram Moolenaar65a54642018-04-28 16:56:53 +02009397 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009398assert_report(typval_T *argvars)
9399{
9400 garray_T ga;
9401
9402 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009403 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009404 assert_error(&ga);
9405 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009406 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009407}
9408
Bram Moolenaar65a54642018-04-28 16:56:53 +02009409 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410assert_exception(typval_T *argvars)
9411{
9412 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009413 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414
9415 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9416 {
9417 prepare_assert_error(&ga);
9418 ga_concat(&ga, (char_u *)"v:exception is not set");
9419 assert_error(&ga);
9420 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009421 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 }
9423 else if (error != NULL
9424 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9425 {
9426 prepare_assert_error(&ga);
9427 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9428 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9429 assert_error(&ga);
9430 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009431 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009433 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434}
9435
Bram Moolenaar65a54642018-04-28 16:56:53 +02009436 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009437assert_beeps(typval_T *argvars)
9438{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009439 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009440 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009441 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009442
9443 called_vim_beep = FALSE;
9444 suppress_errthrow = TRUE;
9445 emsg_silent = FALSE;
9446 do_cmdline_cmd(cmd);
9447 if (!called_vim_beep)
9448 {
9449 prepare_assert_error(&ga);
9450 ga_concat(&ga, (char_u *)"command did not beep: ");
9451 ga_concat(&ga, cmd);
9452 assert_error(&ga);
9453 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009454 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009455 }
9456
9457 suppress_errthrow = FALSE;
9458 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009459 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009460}
9461
Bram Moolenaar65a54642018-04-28 16:56:53 +02009462 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463assert_fails(typval_T *argvars)
9464{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009465 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009467 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009468 char_u numbuf[NUMBUFLEN];
9469 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470
9471 called_emsg = FALSE;
9472 suppress_errthrow = TRUE;
9473 emsg_silent = TRUE;
9474 do_cmdline_cmd(cmd);
9475 if (!called_emsg)
9476 {
9477 prepare_assert_error(&ga);
9478 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009479 if (argvars[1].v_type != VAR_UNKNOWN
9480 && argvars[2].v_type != VAR_UNKNOWN)
9481 {
9482 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9483 vim_free(tofree);
9484 }
9485 else
9486 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 assert_error(&ga);
9488 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009489 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 }
9491 else if (argvars[1].v_type != VAR_UNKNOWN)
9492 {
9493 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009494 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495
9496 if (error == NULL
9497 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9498 {
9499 prepare_assert_error(&ga);
9500 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9501 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9502 assert_error(&ga);
9503 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009504 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 }
9506 }
9507
9508 called_emsg = FALSE;
9509 suppress_errthrow = FALSE;
9510 emsg_silent = FALSE;
9511 emsg_on_display = FALSE;
9512 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009513 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514}
9515
9516/*
9517 * Append "str" to "gap", escaping unprintable characters.
9518 * Changes NL to \n, CR to \r, etc.
9519 */
9520 static void
9521ga_concat_esc(garray_T *gap, char_u *str)
9522{
9523 char_u *p;
9524 char_u buf[NUMBUFLEN];
9525
9526 if (str == NULL)
9527 {
9528 ga_concat(gap, (char_u *)"NULL");
9529 return;
9530 }
9531
9532 for (p = str; *p != NUL; ++p)
9533 switch (*p)
9534 {
9535 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9536 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9537 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9538 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9539 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9540 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9541 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9542 default:
9543 if (*p < ' ')
9544 {
9545 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9546 ga_concat(gap, buf);
9547 }
9548 else
9549 ga_append(gap, *p);
9550 break;
9551 }
9552}
9553
9554/*
9555 * Fill "gap" with information about an assert error.
9556 */
9557 void
9558fill_assert_error(
9559 garray_T *gap,
9560 typval_T *opt_msg_tv,
9561 char_u *exp_str,
9562 typval_T *exp_tv,
9563 typval_T *got_tv,
9564 assert_type_T atype)
9565{
9566 char_u numbuf[NUMBUFLEN];
9567 char_u *tofree;
9568
9569 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9570 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009571 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9572 vim_free(tofree);
9573 ga_concat(gap, (char_u *)": ");
9574 }
9575
9576 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9577 ga_concat(gap, (char_u *)"Pattern ");
9578 else if (atype == ASSERT_NOTEQUAL)
9579 ga_concat(gap, (char_u *)"Expected not equal to ");
9580 else
9581 ga_concat(gap, (char_u *)"Expected ");
9582 if (exp_str == NULL)
9583 {
9584 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585 vim_free(tofree);
9586 }
9587 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009588 ga_concat_esc(gap, exp_str);
9589 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009591 if (atype == ASSERT_MATCH)
9592 ga_concat(gap, (char_u *)" does not match ");
9593 else if (atype == ASSERT_NOTMATCH)
9594 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009596 ga_concat(gap, (char_u *)" but got ");
9597 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9598 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599 }
9600}
9601
Bram Moolenaar31988702018-02-13 12:57:42 +01009602/*
9603 * Compare "typ1" and "typ2". Put the result in "typ1".
9604 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009605 int
9606typval_compare(
9607 typval_T *typ1, /* first operand */
9608 typval_T *typ2, /* second operand */
9609 exptype_T type, /* operator */
9610 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009611 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009612{
9613 int i;
9614 varnumber_T n1, n2;
9615 char_u *s1, *s2;
9616 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9617
Bram Moolenaar31988702018-02-13 12:57:42 +01009618 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009619 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009620 /* For "is" a different type always means FALSE, for "notis"
9621 * it means TRUE. */
9622 n1 = (type == TYPE_NEQUAL);
9623 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009624 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9625 {
9626 if (type_is)
9627 {
9628 n1 = (typ1->v_type == typ2->v_type
9629 && typ1->vval.v_blob == typ2->vval.v_blob);
9630 if (type == TYPE_NEQUAL)
9631 n1 = !n1;
9632 }
9633 else if (typ1->v_type != typ2->v_type
9634 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9635 {
9636 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009637 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009638 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009639 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009640 clear_tv(typ1);
9641 return FAIL;
9642 }
9643 else
9644 {
9645 // Compare two Blobs for being equal or unequal.
9646 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9647 if (type == TYPE_NEQUAL)
9648 n1 = !n1;
9649 }
9650 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009651 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9652 {
9653 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009654 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009655 n1 = (typ1->v_type == typ2->v_type
9656 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009657 if (type == TYPE_NEQUAL)
9658 n1 = !n1;
9659 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009660 else if (typ1->v_type != typ2->v_type
9661 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009662 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009663 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009664 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009665 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009666 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009667 clear_tv(typ1);
9668 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009669 }
9670 else
9671 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009672 /* Compare two Lists for being equal or unequal. */
9673 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9674 ic, FALSE);
9675 if (type == TYPE_NEQUAL)
9676 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009677 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009678 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009679
9680 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9681 {
9682 if (type_is)
9683 {
9684 n1 = (typ1->v_type == typ2->v_type
9685 && typ1->vval.v_dict == typ2->vval.v_dict);
9686 if (type == TYPE_NEQUAL)
9687 n1 = !n1;
9688 }
9689 else if (typ1->v_type != typ2->v_type
9690 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9691 {
9692 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009693 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009694 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009695 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009696 clear_tv(typ1);
9697 return FAIL;
9698 }
9699 else
9700 {
9701 /* Compare two Dictionaries for being equal or unequal. */
9702 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9703 ic, FALSE);
9704 if (type == TYPE_NEQUAL)
9705 n1 = !n1;
9706 }
9707 }
9708
9709 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9710 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9711 {
9712 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009714 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009715 clear_tv(typ1);
9716 return FAIL;
9717 }
9718 if ((typ1->v_type == VAR_PARTIAL
9719 && typ1->vval.v_partial == NULL)
9720 || (typ2->v_type == VAR_PARTIAL
9721 && typ2->vval.v_partial == NULL))
9722 /* when a partial is NULL assume not equal */
9723 n1 = FALSE;
9724 else if (type_is)
9725 {
9726 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9727 /* strings are considered the same if their value is
9728 * the same */
9729 n1 = tv_equal(typ1, typ2, ic, FALSE);
9730 else if (typ1->v_type == VAR_PARTIAL
9731 && typ2->v_type == VAR_PARTIAL)
9732 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9733 else
9734 n1 = FALSE;
9735 }
9736 else
9737 n1 = tv_equal(typ1, typ2, ic, FALSE);
9738 if (type == TYPE_NEQUAL)
9739 n1 = !n1;
9740 }
9741
9742#ifdef FEAT_FLOAT
9743 /*
9744 * If one of the two variables is a float, compare as a float.
9745 * When using "=~" or "!~", always compare as string.
9746 */
9747 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9748 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9749 {
9750 float_T f1, f2;
9751
9752 if (typ1->v_type == VAR_FLOAT)
9753 f1 = typ1->vval.v_float;
9754 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009755 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009756 if (typ2->v_type == VAR_FLOAT)
9757 f2 = typ2->vval.v_float;
9758 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009759 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009760 n1 = FALSE;
9761 switch (type)
9762 {
9763 case TYPE_EQUAL: n1 = (f1 == f2); break;
9764 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9765 case TYPE_GREATER: n1 = (f1 > f2); break;
9766 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9767 case TYPE_SMALLER: n1 = (f1 < f2); break;
9768 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9769 case TYPE_UNKNOWN:
9770 case TYPE_MATCH:
9771 case TYPE_NOMATCH: break; /* avoid gcc warning */
9772 }
9773 }
9774#endif
9775
9776 /*
9777 * If one of the two variables is a number, compare as a number.
9778 * When using "=~" or "!~", always compare as string.
9779 */
9780 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9781 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009783 n1 = tv_get_number(typ1);
9784 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009785 switch (type)
9786 {
9787 case TYPE_EQUAL: n1 = (n1 == n2); break;
9788 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9789 case TYPE_GREATER: n1 = (n1 > n2); break;
9790 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9791 case TYPE_SMALLER: n1 = (n1 < n2); break;
9792 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9793 case TYPE_UNKNOWN:
9794 case TYPE_MATCH:
9795 case TYPE_NOMATCH: break; /* avoid gcc warning */
9796 }
9797 }
9798 else
9799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009800 s1 = tv_get_string_buf(typ1, buf1);
9801 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009802 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9803 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9804 else
9805 i = 0;
9806 n1 = FALSE;
9807 switch (type)
9808 {
9809 case TYPE_EQUAL: n1 = (i == 0); break;
9810 case TYPE_NEQUAL: n1 = (i != 0); break;
9811 case TYPE_GREATER: n1 = (i > 0); break;
9812 case TYPE_GEQUAL: n1 = (i >= 0); break;
9813 case TYPE_SMALLER: n1 = (i < 0); break;
9814 case TYPE_SEQUAL: n1 = (i <= 0); break;
9815
9816 case TYPE_MATCH:
9817 case TYPE_NOMATCH:
9818 n1 = pattern_match(s2, s1, ic);
9819 if (type == TYPE_NOMATCH)
9820 n1 = !n1;
9821 break;
9822
9823 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9824 }
9825 }
9826 clear_tv(typ1);
9827 typ1->v_type = VAR_NUMBER;
9828 typ1->vval.v_number = n1;
9829
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009830 return OK;
9831}
9832
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009833 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009834typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009835{
9836 char_u *tofree;
9837 char_u numbuf[NUMBUFLEN];
9838 char_u *ret = NULL;
9839
9840 if (arg == NULL)
9841 return vim_strsave((char_u *)"(does not exist)");
9842 ret = tv2string(arg, &tofree, numbuf, 0);
9843 /* Make a copy if we have a value but it's not in allocated memory. */
9844 if (ret != NULL && tofree == NULL)
9845 ret = vim_strsave(ret);
9846 return ret;
9847}
9848
9849 int
9850var_exists(char_u *var)
9851{
9852 char_u *name;
9853 char_u *tofree;
9854 typval_T tv;
9855 int len = 0;
9856 int n = FALSE;
9857
9858 /* get_name_len() takes care of expanding curly braces */
9859 name = var;
9860 len = get_name_len(&var, &tofree, TRUE, FALSE);
9861 if (len > 0)
9862 {
9863 if (tofree != NULL)
9864 name = tofree;
9865 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9866 if (n)
9867 {
9868 /* handle d.key, l[idx], f(expr) */
9869 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9870 if (n)
9871 clear_tv(&tv);
9872 }
9873 }
9874 if (*var != NUL)
9875 n = FALSE;
9876
9877 vim_free(tofree);
9878 return n;
9879}
9880
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881#endif /* FEAT_EVAL */
9882
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009884#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885
9886#ifdef WIN3264
9887/*
9888 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009892 * Get the short path (8.3) for the filename in "fnamep".
9893 * Only works for a valid file name.
9894 * When the path gets longer "fnamep" is changed and the allocated buffer
9895 * is put in "bufp".
9896 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9897 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 */
9899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009900get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009902 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 char_u *newbuf;
9904
9905 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009906 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 if (l > len - 1)
9908 {
9909 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009910 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 newbuf = vim_strnsave(*fnamep, l);
9912 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914
9915 vim_free(*bufp);
9916 *fnamep = *bufp = newbuf;
9917
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009918 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009919 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 }
9921
9922 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009923 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924}
9925
9926/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009927 * Get the short path (8.3) for the filename in "fname". The converted
9928 * path is returned in "bufp".
9929 *
9930 * Some of the directories specified in "fname" may not exist. This function
9931 * will shorten the existing directories at the beginning of the path and then
9932 * append the remaining non-existing path.
9933 *
9934 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009935 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009936 * bufp - Pointer to an allocated buffer for the filename.
9937 * fnamelen - Length of the filename pointed to by fname
9938 *
9939 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 */
9941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009942shortpath_for_invalid_fname(
9943 char_u **fname,
9944 char_u **bufp,
9945 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009947 char_u *short_fname, *save_fname, *pbuf_unused;
9948 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009950 int old_len, len;
9951 int new_len, sfx_len;
9952 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953
9954 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009955 old_len = *fnamelen;
9956 save_fname = vim_strnsave(*fname, old_len);
9957 pbuf_unused = NULL;
9958 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009960 endp = save_fname + old_len - 1; /* Find the end of the copy */
9961 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009963 /*
9964 * Try shortening the supplied path till it succeeds by removing one
9965 * directory at a time from the tail of the path.
9966 */
9967 len = 0;
9968 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009970 /* go back one path-separator */
9971 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9972 --endp;
9973 if (endp <= save_fname)
9974 break; /* processed the complete path */
9975
9976 /*
9977 * Replace the path separator with a NUL and try to shorten the
9978 * resulting path.
9979 */
9980 ch = *endp;
9981 *endp = 0;
9982 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009983 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009984 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9985 {
9986 retval = FAIL;
9987 goto theend;
9988 }
9989 *endp = ch; /* preserve the string */
9990
9991 if (len > 0)
9992 break; /* successfully shortened the path */
9993
9994 /* failed to shorten the path. Skip the path separator */
9995 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 }
9997
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009998 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010000 /*
10001 * Succeeded in shortening the path. Now concatenate the shortened
10002 * path with the remaining path at the tail.
10003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010005 /* Compute the length of the new path. */
10006 sfx_len = (int)(save_endp - endp) + 1;
10007 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010009 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010011 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010013 /* There is not enough space in the currently allocated string,
10014 * copy it to a buffer big enough. */
10015 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010017 {
10018 retval = FAIL;
10019 goto theend;
10020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022 else
10023 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010024 /* Transfer short_fname to the main buffer (it's big enough),
10025 * unless get_short_pathname() did its work in-place. */
10026 *fname = *bufp = save_fname;
10027 if (short_fname != save_fname)
10028 vim_strncpy(save_fname, short_fname, len);
10029 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010031
10032 /* concat the not-shortened part of the path */
10033 vim_strncpy(*fname + len, endp, sfx_len);
10034 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010036
10037theend:
10038 vim_free(pbuf_unused);
10039 vim_free(save_fname);
10040
10041 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042}
10043
10044/*
10045 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010046 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 */
10048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010049shortpath_for_partial(
10050 char_u **fnamep,
10051 char_u **bufp,
10052 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 int sepcount, len, tflen;
10055 char_u *p;
10056 char_u *pbuf, *tfname;
10057 int hasTilde;
10058
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010059 /* Count up the path separators from the RHS.. so we know which part
10060 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010062 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (vim_ispathsep(*p))
10064 ++sepcount;
10065
10066 /* Need full path first (use expand_env() to remove a "~/") */
10067 hasTilde = (**fnamep == '~');
10068 if (hasTilde)
10069 pbuf = tfname = expand_env_save(*fnamep);
10070 else
10071 pbuf = tfname = FullName_save(*fnamep, FALSE);
10072
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010073 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010075 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10076 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
10078 if (len == 0)
10079 {
10080 /* Don't have a valid filename, so shorten the rest of the
10081 * path if we can. This CAN give us invalid 8.3 filenames, but
10082 * there's not a lot of point in guessing what it might be.
10083 */
10084 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010085 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 }
10088
10089 /* Count the paths backward to find the beginning of the desired string. */
10090 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010091 {
10092#ifdef FEAT_MBYTE
10093 if (has_mbyte)
10094 p -= mb_head_off(tfname, p);
10095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 if (vim_ispathsep(*p))
10097 {
10098 if (sepcount == 0 || (hasTilde && sepcount == 1))
10099 break;
10100 else
10101 sepcount --;
10102 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 if (hasTilde)
10105 {
10106 --p;
10107 if (p >= tfname)
10108 *p = '~';
10109 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010110 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112 else
10113 ++p;
10114
10115 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10116 vim_free(*bufp);
10117 *fnamelen = (int)STRLEN(p);
10118 *bufp = pbuf;
10119 *fnamep = p;
10120
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010121 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122}
10123#endif /* WIN3264 */
10124
10125/*
10126 * Adjust a filename, according to a string of modifiers.
10127 * *fnamep must be NUL terminated when called. When returning, the length is
10128 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010129 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 * When there is an error, *fnamep is set to NULL.
10131 */
10132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010133modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010134 char_u *src, // string with modifiers
10135 int tilde_file, // "~" is a file name, not $HOME
10136 int *usedlen, // characters after src that are used
10137 char_u **fnamep, // file name so far
10138 char_u **bufp, // buffer for allocated file name or NULL
10139 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140{
10141 int valid = 0;
10142 char_u *tail;
10143 char_u *s, *p, *pbuf;
10144 char_u dirname[MAXPATHL];
10145 int c;
10146 int has_fullname = 0;
10147#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010148 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 int has_shortname = 0;
10150#endif
10151
10152repeat:
10153 /* ":p" - full path/file_name */
10154 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10155 {
10156 has_fullname = 1;
10157
10158 valid |= VALID_PATH;
10159 *usedlen += 2;
10160
10161 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10162 if ((*fnamep)[0] == '~'
10163#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10164 && ((*fnamep)[1] == '/'
10165# ifdef BACKSLASH_IN_FILENAME
10166 || (*fnamep)[1] == '\\'
10167# endif
10168 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010170 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 )
10172 {
10173 *fnamep = expand_env_save(*fnamep);
10174 vim_free(*bufp); /* free any allocated file name */
10175 *bufp = *fnamep;
10176 if (*fnamep == NULL)
10177 return -1;
10178 }
10179
10180 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010181 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 {
10183 if (vim_ispathsep(*p)
10184 && p[1] == '.'
10185 && (p[2] == NUL
10186 || vim_ispathsep(p[2])
10187 || (p[2] == '.'
10188 && (p[3] == NUL || vim_ispathsep(p[3])))))
10189 break;
10190 }
10191
10192 /* FullName_save() is slow, don't use it when not needed. */
10193 if (*p != NUL || !vim_isAbsName(*fnamep))
10194 {
10195 *fnamep = FullName_save(*fnamep, *p != NUL);
10196 vim_free(*bufp); /* free any allocated file name */
10197 *bufp = *fnamep;
10198 if (*fnamep == NULL)
10199 return -1;
10200 }
10201
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010202#ifdef WIN3264
10203# if _WIN32_WINNT >= 0x0500
10204 if (vim_strchr(*fnamep, '~') != NULL)
10205 {
10206 /* Expand 8.3 filename to full path. Needed to make sure the same
10207 * file does not have two different names.
10208 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10209 p = alloc(_MAX_PATH + 1);
10210 if (p != NULL)
10211 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010212 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010213 {
10214 vim_free(*bufp);
10215 *bufp = *fnamep = p;
10216 }
10217 else
10218 vim_free(p);
10219 }
10220 }
10221# endif
10222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 /* Append a path separator to a directory. */
10224 if (mch_isdir(*fnamep))
10225 {
10226 /* Make room for one or two extra characters. */
10227 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10228 vim_free(*bufp); /* free any allocated file name */
10229 *bufp = *fnamep;
10230 if (*fnamep == NULL)
10231 return -1;
10232 add_pathsep(*fnamep);
10233 }
10234 }
10235
10236 /* ":." - path relative to the current directory */
10237 /* ":~" - path relative to the home directory */
10238 /* ":8" - shortname path - postponed till after */
10239 while (src[*usedlen] == ':'
10240 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10241 {
10242 *usedlen += 2;
10243 if (c == '8')
10244 {
10245#ifdef WIN3264
10246 has_shortname = 1; /* Postpone this. */
10247#endif
10248 continue;
10249 }
10250 pbuf = NULL;
10251 /* Need full path first (use expand_env() to remove a "~/") */
10252 if (!has_fullname)
10253 {
10254 if (c == '.' && **fnamep == '~')
10255 p = pbuf = expand_env_save(*fnamep);
10256 else
10257 p = pbuf = FullName_save(*fnamep, FALSE);
10258 }
10259 else
10260 p = *fnamep;
10261
10262 has_fullname = 0;
10263
10264 if (p != NULL)
10265 {
10266 if (c == '.')
10267 {
10268 mch_dirname(dirname, MAXPATHL);
10269 s = shorten_fname(p, dirname);
10270 if (s != NULL)
10271 {
10272 *fnamep = s;
10273 if (pbuf != NULL)
10274 {
10275 vim_free(*bufp); /* free any allocated file name */
10276 *bufp = pbuf;
10277 pbuf = NULL;
10278 }
10279 }
10280 }
10281 else
10282 {
10283 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10284 /* Only replace it when it starts with '~' */
10285 if (*dirname == '~')
10286 {
10287 s = vim_strsave(dirname);
10288 if (s != NULL)
10289 {
10290 *fnamep = s;
10291 vim_free(*bufp);
10292 *bufp = s;
10293 }
10294 }
10295 }
10296 vim_free(pbuf);
10297 }
10298 }
10299
10300 tail = gettail(*fnamep);
10301 *fnamelen = (int)STRLEN(*fnamep);
10302
10303 /* ":h" - head, remove "/file_name", can be repeated */
10304 /* Don't remove the first "/" or "c:\" */
10305 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10306 {
10307 valid |= VALID_HEAD;
10308 *usedlen += 2;
10309 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010310 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010311 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 *fnamelen = (int)(tail - *fnamep);
10313#ifdef VMS
10314 if (*fnamelen > 0)
10315 *fnamelen += 1; /* the path separator is part of the path */
10316#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010317 if (*fnamelen == 0)
10318 {
10319 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10320 p = vim_strsave((char_u *)".");
10321 if (p == NULL)
10322 return -1;
10323 vim_free(*bufp);
10324 *bufp = *fnamep = tail = p;
10325 *fnamelen = 1;
10326 }
10327 else
10328 {
10329 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010330 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 }
10333
10334 /* ":8" - shortname */
10335 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10336 {
10337 *usedlen += 2;
10338#ifdef WIN3264
10339 has_shortname = 1;
10340#endif
10341 }
10342
10343#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010344 /*
10345 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 */
10347 if (has_shortname)
10348 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010349 /* Copy the string if it is shortened by :h and when it wasn't copied
10350 * yet, because we are going to change it in place. Avoids changing
10351 * the buffer name for "%:8". */
10352 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 {
10354 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010355 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 return -1;
10357 vim_free(*bufp);
10358 *bufp = *fnamep = p;
10359 }
10360
10361 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010362 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 if (!has_fullname && !vim_isAbsName(*fnamep))
10364 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010365 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 return -1;
10367 }
10368 else
10369 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010370 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
Bram Moolenaardc935552011-08-17 15:23:23 +020010372 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010374 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 return -1;
10376
10377 if (l == 0)
10378 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010379 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010381 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 return -1;
10383 }
10384 *fnamelen = l;
10385 }
10386 }
10387#endif /* WIN3264 */
10388
10389 /* ":t" - tail, just the basename */
10390 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10391 {
10392 *usedlen += 2;
10393 *fnamelen -= (int)(tail - *fnamep);
10394 *fnamep = tail;
10395 }
10396
10397 /* ":e" - extension, can be repeated */
10398 /* ":r" - root, without extension, can be repeated */
10399 while (src[*usedlen] == ':'
10400 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10401 {
10402 /* find a '.' in the tail:
10403 * - for second :e: before the current fname
10404 * - otherwise: The last '.'
10405 */
10406 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10407 s = *fnamep - 2;
10408 else
10409 s = *fnamep + *fnamelen - 1;
10410 for ( ; s > tail; --s)
10411 if (s[0] == '.')
10412 break;
10413 if (src[*usedlen + 1] == 'e') /* :e */
10414 {
10415 if (s > tail)
10416 {
10417 *fnamelen += (int)(*fnamep - (s + 1));
10418 *fnamep = s + 1;
10419#ifdef VMS
10420 /* cut version from the extension */
10421 s = *fnamep + *fnamelen - 1;
10422 for ( ; s > *fnamep; --s)
10423 if (s[0] == ';')
10424 break;
10425 if (s > *fnamep)
10426 *fnamelen = s - *fnamep;
10427#endif
10428 }
10429 else if (*fnamep <= tail)
10430 *fnamelen = 0;
10431 }
10432 else /* :r */
10433 {
10434 if (s > tail) /* remove one extension */
10435 *fnamelen = (int)(s - *fnamep);
10436 }
10437 *usedlen += 2;
10438 }
10439
10440 /* ":s?pat?foo?" - substitute */
10441 /* ":gs?pat?foo?" - global substitute */
10442 if (src[*usedlen] == ':'
10443 && (src[*usedlen + 1] == 's'
10444 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10445 {
10446 char_u *str;
10447 char_u *pat;
10448 char_u *sub;
10449 int sep;
10450 char_u *flags;
10451 int didit = FALSE;
10452
10453 flags = (char_u *)"";
10454 s = src + *usedlen + 2;
10455 if (src[*usedlen + 1] == 'g')
10456 {
10457 flags = (char_u *)"g";
10458 ++s;
10459 }
10460
10461 sep = *s++;
10462 if (sep)
10463 {
10464 /* find end of pattern */
10465 p = vim_strchr(s, sep);
10466 if (p != NULL)
10467 {
10468 pat = vim_strnsave(s, (int)(p - s));
10469 if (pat != NULL)
10470 {
10471 s = p + 1;
10472 /* find end of substitution */
10473 p = vim_strchr(s, sep);
10474 if (p != NULL)
10475 {
10476 sub = vim_strnsave(s, (int)(p - s));
10477 str = vim_strnsave(*fnamep, *fnamelen);
10478 if (sub != NULL && str != NULL)
10479 {
10480 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010481 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 if (s != NULL)
10483 {
10484 *fnamep = s;
10485 *fnamelen = (int)STRLEN(s);
10486 vim_free(*bufp);
10487 *bufp = s;
10488 didit = TRUE;
10489 }
10490 }
10491 vim_free(sub);
10492 vim_free(str);
10493 }
10494 vim_free(pat);
10495 }
10496 }
10497 /* after using ":s", repeat all the modifiers */
10498 if (didit)
10499 goto repeat;
10500 }
10501 }
10502
Bram Moolenaar26df0922014-02-23 23:39:13 +010010503 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10504 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010505 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010506 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010507 if (c != NUL)
10508 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010509 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010510 if (c != NUL)
10511 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010512 if (p == NULL)
10513 return -1;
10514 vim_free(*bufp);
10515 *bufp = *fnamep = p;
10516 *fnamelen = (int)STRLEN(p);
10517 *usedlen += 2;
10518 }
10519
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 return valid;
10521}
10522
10523/*
10524 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010525 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 * "flags" can be "g" to do a global substitute.
10527 * Returns an allocated string, NULL for error.
10528 */
10529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010530do_string_sub(
10531 char_u *str,
10532 char_u *pat,
10533 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010534 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010535 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536{
10537 int sublen;
10538 regmatch_T regmatch;
10539 int i;
10540 int do_all;
10541 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010542 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 garray_T ga;
10544 char_u *ret;
10545 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010546 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547
10548 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10549 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010550 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551
10552 ga_init2(&ga, 1, 200);
10553
10554 do_all = (flags[0] == 'g');
10555
10556 regmatch.rm_ic = p_ic;
10557 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10558 if (regmatch.regprog != NULL)
10559 {
10560 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010561 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10563 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010564 /* Skip empty match except for first match. */
10565 if (regmatch.startp[0] == regmatch.endp[0])
10566 {
10567 if (zero_width == regmatch.startp[0])
10568 {
10569 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010570 i = MB_PTR2LEN(tail);
10571 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10572 (size_t)i);
10573 ga.ga_len += i;
10574 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010575 continue;
10576 }
10577 zero_width = regmatch.startp[0];
10578 }
10579
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 /*
10581 * Get some space for a temporary buffer to do the substitution
10582 * into. It will contain:
10583 * - The text up to where the match is.
10584 * - The substituted text.
10585 * - The text after the match.
10586 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010587 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010588 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10590 {
10591 ga_clear(&ga);
10592 break;
10593 }
10594
10595 /* copy the text up to where the match is */
10596 i = (int)(regmatch.startp[0] - tail);
10597 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10598 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010599 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 + ga.ga_len + i, TRUE, TRUE, FALSE);
10601 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010602 tail = regmatch.endp[0];
10603 if (*tail == NUL)
10604 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 if (!do_all)
10606 break;
10607 }
10608
10609 if (ga.ga_data != NULL)
10610 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10611
Bram Moolenaar473de612013-06-08 18:19:48 +020010612 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 }
10614
10615 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10616 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010617 if (p_cpo == empty_option)
10618 p_cpo = save_cpo;
10619 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010620 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010621 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622
10623 return ret;
10624}
10625
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 static int
10627filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10628{
10629 typval_T rettv;
10630 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010631 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632
10633 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10634 argv[0] = vimvars[VV_KEY].vv_tv;
10635 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010636 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10637 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638 if (map)
10639 {
10640 /* map(): replace the list item value */
10641 clear_tv(tv);
10642 rettv.v_lock = 0;
10643 *tv = rettv;
10644 }
10645 else
10646 {
10647 int error = FALSE;
10648
10649 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010650 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010651 clear_tv(&rettv);
10652 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010653 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 if (error)
10655 goto theend;
10656 }
10657 retval = OK;
10658theend:
10659 clear_tv(&vimvars[VV_VAL].vv_tv);
10660 return retval;
10661}
10662
10663
10664/*
10665 * Implementation of map() and filter().
10666 */
10667 void
10668filter_map(typval_T *argvars, typval_T *rettv, int map)
10669{
10670 typval_T *expr;
10671 listitem_T *li, *nli;
10672 list_T *l = NULL;
10673 dictitem_T *di;
10674 hashtab_T *ht;
10675 hashitem_T *hi;
10676 dict_T *d = NULL;
10677 typval_T save_val;
10678 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010679 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 int rem;
10681 int todo;
10682 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10683 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10684 : N_("filter() argument"));
10685 int save_did_emsg;
10686 int idx = 0;
10687
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010688 if (argvars[0].v_type == VAR_BLOB)
10689 {
10690 if ((b = argvars[0].vval.v_blob) == NULL)
10691 return;
10692 }
10693 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010694 {
10695 if ((l = argvars[0].vval.v_list) == NULL
10696 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10697 return;
10698 }
10699 else if (argvars[0].v_type == VAR_DICT)
10700 {
10701 if ((d = argvars[0].vval.v_dict) == NULL
10702 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10703 return;
10704 }
10705 else
10706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010707 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 return;
10709 }
10710
10711 expr = &argvars[1];
10712 /* On type errors, the preceding call has already displayed an error
10713 * message. Avoid a misleading error message for an empty string that
10714 * was not passed as argument. */
10715 if (expr->v_type != VAR_UNKNOWN)
10716 {
10717 prepare_vimvar(VV_VAL, &save_val);
10718
10719 /* We reset "did_emsg" to be able to detect whether an error
10720 * occurred during evaluation of the expression. */
10721 save_did_emsg = did_emsg;
10722 did_emsg = FALSE;
10723
10724 prepare_vimvar(VV_KEY, &save_key);
10725 if (argvars[0].v_type == VAR_DICT)
10726 {
10727 vimvars[VV_KEY].vv_type = VAR_STRING;
10728
10729 ht = &d->dv_hashtab;
10730 hash_lock(ht);
10731 todo = (int)ht->ht_used;
10732 for (hi = ht->ht_array; todo > 0; ++hi)
10733 {
10734 if (!HASHITEM_EMPTY(hi))
10735 {
10736 int r;
10737
10738 --todo;
10739 di = HI2DI(hi);
10740 if (map &&
10741 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10742 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10743 break;
10744 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10745 r = filter_map_one(&di->di_tv, expr, map, &rem);
10746 clear_tv(&vimvars[VV_KEY].vv_tv);
10747 if (r == FAIL || did_emsg)
10748 break;
10749 if (!map && rem)
10750 {
10751 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10752 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10753 break;
10754 dictitem_remove(d, di);
10755 }
10756 }
10757 }
10758 hash_unlock(ht);
10759 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010760 else if (argvars[0].v_type == VAR_BLOB)
10761 {
10762 int i;
10763 typval_T tv;
10764
10765 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10766 for (i = 0; i < b->bv_ga.ga_len; i++)
10767 {
10768 tv.v_type = VAR_NUMBER;
10769 tv.vval.v_number = blob_get(b, i);
10770 vimvars[VV_KEY].vv_nr = idx;
10771 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10772 break;
10773 if (tv.v_type != VAR_NUMBER)
10774 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010775 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010776 return;
10777 }
10778 tv.v_type = VAR_NUMBER;
10779 blob_set(b, i, tv.vval.v_number);
10780 if (!map && rem)
10781 {
10782 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10783
10784 mch_memmove(p + idx, p + i + 1,
10785 (size_t)b->bv_ga.ga_len - i - 1);
10786 --b->bv_ga.ga_len;
10787 --i;
10788 }
10789 }
10790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010791 else
10792 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010793 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10795
10796 for (li = l->lv_first; li != NULL; li = nli)
10797 {
10798 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10799 break;
10800 nli = li->li_next;
10801 vimvars[VV_KEY].vv_nr = idx;
10802 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10803 || did_emsg)
10804 break;
10805 if (!map && rem)
10806 listitem_remove(l, li);
10807 ++idx;
10808 }
10809 }
10810
10811 restore_vimvar(VV_KEY, &save_key);
10812 restore_vimvar(VV_VAL, &save_val);
10813
10814 did_emsg |= save_did_emsg;
10815 }
10816
10817 copy_tv(&argvars[0], rettv);
10818}
10819
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */