blob: 67a006ec9525a0f6af0c9cf430e91b2854b9096e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100248static void list_one_var(dictitem_T *v, char *prefix, int *first);
249static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram 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 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100581eval_charconvert(
582 char_u *enc_from,
583 char_u *enc_to,
584 char_u *fname_from,
585 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 int err = FALSE;
588
589 set_vim_var_string(VV_CC_FROM, enc_from, -1);
590 set_vim_var_string(VV_CC_TO, enc_to, -1);
591 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
592 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
593 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
594 err = TRUE;
595 set_vim_var_string(VV_CC_FROM, NULL, -1);
596 set_vim_var_string(VV_CC_TO, NULL, -1);
597 set_vim_var_string(VV_FNAME_IN, NULL, -1);
598 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
599
600 if (err)
601 return FAIL;
602 return OK;
603}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
605# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
606 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 int err = FALSE;
610
611 set_vim_var_string(VV_FNAME_IN, fname, -1);
612 set_vim_var_string(VV_CMDARG, args, -1);
613 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
614 err = TRUE;
615 set_vim_var_string(VV_FNAME_IN, NULL, -1);
616 set_vim_var_string(VV_CMDARG, NULL, -1);
617
618 if (err)
619 {
620 mch_remove(fname);
621 return FAIL;
622 }
623 return OK;
624}
625# endif
626
627# if defined(FEAT_DIFF) || defined(PROTO)
628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100629eval_diff(
630 char_u *origfile,
631 char_u *newfile,
632 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int err = FALSE;
635
636 set_vim_var_string(VV_FNAME_IN, origfile, -1);
637 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
638 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
639 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
640 set_vim_var_string(VV_FNAME_IN, NULL, -1);
641 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
642 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
643}
644
645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100646eval_patch(
647 char_u *origfile,
648 char_u *difffile,
649 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 int err;
652
653 set_vim_var_string(VV_FNAME_IN, origfile, -1);
654 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
655 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
656 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
657 set_vim_var_string(VV_FNAME_IN, NULL, -1);
658 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
659 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
660}
661# endif
662
663/*
664 * Top level evaluation function, returning a boolean.
665 * Sets "error" to TRUE if there was an error.
666 * Return TRUE or FALSE.
667 */
668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669eval_to_bool(
670 char_u *arg,
671 int *error,
672 char_u **nextcmd,
673 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaar33570922005-01-25 22:26:29 +0000675 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200676 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 if (skip)
679 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000680 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 else
683 {
684 *error = FALSE;
685 if (!skip)
686 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100687 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000688 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690 }
691 if (skip)
692 --emsg_skip;
693
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200694 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695}
696
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100697/*
698 * Call eval1() and give an error message if not done at a lower level.
699 */
700 static int
701eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
702{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100703 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100704 int ret;
705 int did_emsg_before = did_emsg;
706 int called_emsg_before = called_emsg;
707
708 ret = eval1(arg, rettv, evaluate);
709 if (ret == FAIL)
710 {
711 // Report the invalid expression unless the expression evaluation has
712 // been cancelled due to an aborting error, an interrupt, or an
713 // exception, or we already gave a more specific error.
714 // Also check called_emsg for when using assert_fails().
715 if (!aborting() && did_emsg == did_emsg_before
716 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100717 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100718 }
719 return ret;
720}
721
Bram Moolenaar48570482017-10-30 21:48:41 +0100722 static int
723eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
724{
725 char_u *s;
726 int dummy;
727 char_u buf[NUMBUFLEN];
728
729 if (expr->v_type == VAR_FUNC)
730 {
731 s = expr->vval.v_string;
732 if (s == NULL || *s == NUL)
733 return FAIL;
734 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
735 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
736 return FAIL;
737 }
738 else if (expr->v_type == VAR_PARTIAL)
739 {
740 partial_T *partial = expr->vval.v_partial;
741
742 s = partial_name(partial);
743 if (s == NULL || *s == NUL)
744 return FAIL;
745 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
746 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
747 return FAIL;
748 }
749 else
750 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100751 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100752 if (s == NULL)
753 return FAIL;
754 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100755 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100756 return FAIL;
757 if (*s != NUL) /* check for trailing chars after expr */
758 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200759 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100760 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100761 return FAIL;
762 }
763 }
764 return OK;
765}
766
767/*
768 * Like eval_to_bool() but using a typval_T instead of a string.
769 * Works for string, funcref and partial.
770 */
771 int
772eval_expr_to_bool(typval_T *expr, int *error)
773{
774 typval_T rettv;
775 int res;
776
777 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
778 {
779 *error = TRUE;
780 return FALSE;
781 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100782 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100783 clear_tv(&rettv);
784 return res;
785}
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787/*
788 * Top level evaluation function, returning a string. If "skip" is TRUE,
789 * only parsing to "nextcmd" is done, without reporting errors. Return
790 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
791 */
792 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793eval_to_string_skip(
794 char_u *arg,
795 char_u **nextcmd,
796 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
Bram Moolenaar33570922005-01-25 22:26:29 +0000798 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 char_u *retval;
800
801 if (skip)
802 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = NULL;
805 else
806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100807 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810 if (skip)
811 --emsg_skip;
812
813 return retval;
814}
815
816/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000817 * Skip over an expression at "*pp".
818 * Return FAIL for an error, OK otherwise.
819 */
820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000822{
Bram Moolenaar33570922005-01-25 22:26:29 +0000823 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000824
825 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000826 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000827}
828
829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000831 * When "convert" is TRUE convert a List into a sequence of lines and convert
832 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Return pointer to allocated memory, or NULL for failure.
834 */
835 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836eval_to_string(
837 char_u *arg,
838 char_u **nextcmd,
839 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000843 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000844#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000845 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 retval = NULL;
850 else
851 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000852 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000853 {
854 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000855 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200856 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200857 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200858 if (tv.vval.v_list->lv_len > 0)
859 ga_append(&ga, NL);
860 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000861 ga_append(&ga, NUL);
862 retval = (char_u *)ga.ga_data;
863 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000864#ifdef FEAT_FLOAT
865 else if (convert && tv.v_type == VAR_FLOAT)
866 {
867 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
868 retval = vim_strsave(numbuf);
869 }
870#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000871 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100872 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875
876 return retval;
877}
878
879/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000880 * Call eval_to_string() without using current local variables and using
881 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 */
883 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884eval_to_string_safe(
885 char_u *arg,
886 char_u **nextcmd,
887 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200890 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200892 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000893 if (use_sandbox)
894 ++sandbox;
895 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000896 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000897 if (use_sandbox)
898 --sandbox;
899 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200900 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 return retval;
902}
903
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904/*
905 * Top level evaluation function, returning a number.
906 * Evaluates "expr" silently.
907 * Returns -1 for an error.
908 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200909 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200913 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000914 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 ++emsg_off;
917
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 retval = -1;
920 else
921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100922 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925 --emsg_off;
926
927 return retval;
928}
929
Bram Moolenaara40058a2005-07-11 22:42:07 +0000930/*
931 * Prepare v: variable "idx" to be used.
932 * Save the current typeval in "save_tv".
933 * When not used yet add the variable to the v: hashtable.
934 */
935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000937{
938 *save_tv = vimvars[idx].vv_tv;
939 if (vimvars[idx].vv_type == VAR_UNKNOWN)
940 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
941}
942
943/*
944 * Restore v: variable "idx" to typeval "save_tv".
945 * When no longer defined, remove the variable from the v: hashtable.
946 */
947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000949{
950 hashitem_T *hi;
951
Bram Moolenaara40058a2005-07-11 22:42:07 +0000952 vimvars[idx].vv_tv = *save_tv;
953 if (vimvars[idx].vv_type == VAR_UNKNOWN)
954 {
955 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
956 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100957 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000958 else
959 hash_remove(&vimvarht, hi);
960 }
961}
962
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000963#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000964/*
965 * Evaluate an expression to a list with suggestions.
966 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000967 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000968 */
969 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100970eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000971{
972 typval_T save_val;
973 typval_T rettv;
974 list_T *list = NULL;
975 char_u *p = skipwhite(expr);
976
977 /* Set "v:val" to the bad word. */
978 prepare_vimvar(VV_VAL, &save_val);
979 vimvars[VV_VAL].vv_type = VAR_STRING;
980 vimvars[VV_VAL].vv_str = badword;
981 if (p_verbose == 0)
982 ++emsg_off;
983
984 if (eval1(&p, &rettv, TRUE) == OK)
985 {
986 if (rettv.v_type != VAR_LIST)
987 clear_tv(&rettv);
988 else
989 list = rettv.vval.v_list;
990 }
991
992 if (p_verbose == 0)
993 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000994 restore_vimvar(VV_VAL, &save_val);
995
996 return list;
997}
998
999/*
1000 * "list" is supposed to contain two items: a word and a number. Return the
1001 * word in "pp" and the number as the return value.
1002 * Return -1 if anything isn't right.
1003 * Used to get the good word and score from the eval_spell_expr() result.
1004 */
1005 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001007{
1008 listitem_T *li;
1009
1010 li = list->lv_first;
1011 if (li == NULL)
1012 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001013 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001014
1015 li = li->li_next;
1016 if (li == NULL)
1017 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001018 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001019}
1020#endif
1021
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001022/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001023 * Top level evaluation function.
1024 * Returns an allocated typval_T with the result.
1025 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001026 */
1027 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001028eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001029{
1030 typval_T *tv;
1031
1032 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001033 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001034 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001035
1036 return tv;
1037}
1038
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001041 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001042 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1043 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001044 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001047call_vim_function(
1048 char_u *func,
1049 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001050 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001051 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001054 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001056 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001057 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001059 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001060 if (ret == FAIL)
1061 clear_tv(rettv);
1062
1063 return ret;
1064}
1065
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001066/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001067 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001068 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001069 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1070 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001071 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001072 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073call_func_retnr(
1074 char_u *func,
1075 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001076 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001077{
1078 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001079 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001080
Bram Moolenaarded27a12018-08-01 19:06:03 +02001081 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001082 return -1;
1083
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001084 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001085 clear_tv(&rettv);
1086 return retval;
1087}
1088
1089#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1090 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1091
Bram Moolenaar4f688582007-07-24 12:34:30 +00001092# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001094 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001095 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001096 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1097 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 */
1099 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001100call_func_retstr(
1101 char_u *func,
1102 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001103 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001104{
1105 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001106 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001107
Bram Moolenaarded27a12018-08-01 19:06:03 +02001108 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109 return NULL;
1110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001111 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001112 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 return retval;
1114}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001115# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001116
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001117/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001118 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001119 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1120 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001121 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001122 */
1123 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001124call_func_retlist(
1125 char_u *func,
1126 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001127 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001128{
1129 typval_T rettv;
1130
Bram Moolenaarded27a12018-08-01 19:06:03 +02001131 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001132 return NULL;
1133
1134 if (rettv.v_type != VAR_LIST)
1135 {
1136 clear_tv(&rettv);
1137 return NULL;
1138 }
1139
1140 return rettv.vval.v_list;
1141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
Bram Moolenaar05159a02005-02-26 23:04:13 +00001144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef FEAT_FOLDING
1146/*
1147 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1148 * it in "*cp". Doesn't give error messages.
1149 */
1150 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001151eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
Bram Moolenaar33570922005-01-25 22:26:29 +00001153 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001154 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001156 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1157 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
1159 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001160 if (use_sandbox)
1161 ++sandbox;
1162 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 retval = 0;
1166 else
1167 {
1168 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001169 if (tv.v_type == VAR_NUMBER)
1170 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001171 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 retval = 0;
1173 else
1174 {
1175 /* If the result is a string, check if there is a non-digit before
1176 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (!VIM_ISDIGIT(*s) && *s != '-')
1179 *cp = *s++;
1180 retval = atol((char *)s);
1181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001185 if (use_sandbox)
1186 --sandbox;
1187 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001189 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190}
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001194 * ":let" list all variable values
1195 * ":let var1 var2" list variable values
1196 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001197 * ":let var += expr" assignment command.
1198 * ":let var -= expr" assignment command.
1199 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 */
1202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001203ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001207 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 int var_count = 0;
1210 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001211 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001212 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001213 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaardb552d602006-03-23 22:59:57 +00001215 argend = skip_var_list(arg, &var_count, &semicolon);
1216 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001217 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001218 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1219 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001220 expr = skipwhite(argend);
1221 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1222 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001224 /*
1225 * ":let" without "=": list variables
1226 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001227 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001229 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001231 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001235 list_glob_vars(&first);
1236 list_buf_vars(&first);
1237 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001238 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001239 list_script_vars(&first);
1240 list_func_vars(&first);
1241 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 eap->nextcmd = check_nextcmd(arg);
1244 }
1245 else
1246 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 op[0] = '=';
1248 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001249 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001250 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001251 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1252 op[0] = *expr; /* +=, -= or .= */
1253 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001254 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001255 else
1256 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (eap->skip)
1259 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (eap->skip)
1262 {
1263 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001264 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 --emsg_skip;
1266 }
1267 else if (i != FAIL)
1268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001269 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 }
1274}
1275
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001276/*
1277 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1278 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001279 * When "nextchars" is not NULL it points to a string with characters that
1280 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1281 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001282 * Returns OK or FAIL;
1283 */
1284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285ex_let_vars(
1286 char_u *arg_start,
1287 typval_T *tv,
1288 int copy, /* copy values from "tv", don't move */
1289 int semicolon, /* from skip_var_list() */
1290 int var_count, /* from skip_var_list() */
1291 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001292{
1293 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001294 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001295 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 listitem_T *item;
1297 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001298
1299 if (*arg != '[')
1300 {
1301 /*
1302 * ":let var = expr" or ":for var in list"
1303 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001305 return FAIL;
1306 return OK;
1307 }
1308
1309 /*
1310 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1311 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001312 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001314 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001315 return FAIL;
1316 }
1317
1318 i = list_len(l);
1319 if (semicolon == 0 && var_count < i)
1320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001322 return FAIL;
1323 }
1324 if (var_count - semicolon > i)
1325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001326 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001327 return FAIL;
1328 }
1329
1330 item = l->lv_first;
1331 while (*arg != ']')
1332 {
1333 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001335 item = item->li_next;
1336 if (arg == NULL)
1337 return FAIL;
1338
1339 arg = skipwhite(arg);
1340 if (*arg == ';')
1341 {
1342 /* Put the rest of the list (may be empty) in the var after ';'.
1343 * Create a new list for this. */
1344 l = list_alloc();
1345 if (l == NULL)
1346 return FAIL;
1347 while (item != NULL)
1348 {
1349 list_append_tv(l, &item->li_tv);
1350 item = item->li_next;
1351 }
1352
1353 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001354 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355 ltv.vval.v_list = l;
1356 l->lv_refcount = 1;
1357
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001358 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1359 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001360 clear_tv(&ltv);
1361 if (arg == NULL)
1362 return FAIL;
1363 break;
1364 }
1365 else if (*arg != ',' && *arg != ']')
1366 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001367 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001368 return FAIL;
1369 }
1370 }
1371
1372 return OK;
1373}
1374
1375/*
1376 * Skip over assignable variable "var" or list of variables "[var, var]".
1377 * Used for ":let varvar = expr" and ":for varvar in expr".
1378 * For "[var, var]" increment "*var_count" for each variable.
1379 * for "[var, var; var]" set "semicolon".
1380 * Return NULL for an error.
1381 */
1382 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001383skip_var_list(
1384 char_u *arg,
1385 int *var_count,
1386 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001387{
1388 char_u *p, *s;
1389
1390 if (*arg == '[')
1391 {
1392 /* "[var, var]": find the matching ']'. */
1393 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001394 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001395 {
1396 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1397 s = skip_var_one(p);
1398 if (s == p)
1399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001400 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 return NULL;
1402 }
1403 ++*var_count;
1404
1405 p = skipwhite(s);
1406 if (*p == ']')
1407 break;
1408 else if (*p == ';')
1409 {
1410 if (*semicolon == 1)
1411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001412 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413 return NULL;
1414 }
1415 *semicolon = 1;
1416 }
1417 else if (*p != ',')
1418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001419 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420 return NULL;
1421 }
1422 }
1423 return p + 1;
1424 }
1425 else
1426 return skip_var_one(arg);
1427}
1428
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001429/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001430 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001431 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001432 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001434skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001436 if (*arg == '@' && arg[1] != NUL)
1437 return arg + 2;
1438 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1439 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001440}
1441
Bram Moolenaara7043832005-01-21 11:56:39 +00001442/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001443 * List variables for hashtab "ht" with prefix "prefix".
1444 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001445 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447list_hashtable_vars(
1448 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001449 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450 int empty,
1451 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001452{
Bram Moolenaar33570922005-01-25 22:26:29 +00001453 hashitem_T *hi;
1454 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001455 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001456 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001458 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001459 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1460 {
1461 if (!HASHITEM_EMPTY(hi))
1462 {
1463 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001464 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001465
1466 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001467 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1468 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001469 if (message_filtered(buf))
1470 continue;
1471
Bram Moolenaar33570922005-01-25 22:26:29 +00001472 if (empty || di->di_tv.v_type != VAR_STRING
1473 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001474 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001475 }
1476 }
1477}
1478
1479/*
1480 * List global variables.
1481 */
1482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001484{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001485 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001486}
1487
1488/*
1489 * List buffer variables.
1490 */
1491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001493{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001494 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001495}
1496
1497/*
1498 * List window variables.
1499 */
1500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001501list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001502{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001503 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001504}
1505
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001506/*
1507 * List tab page variables.
1508 */
1509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001510list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001511{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001512 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001513}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001514
Bram Moolenaara7043832005-01-21 11:56:39 +00001515/*
1516 * List Vim variables.
1517 */
1518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001521 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522}
1523
1524/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001525 * List script-local variables, if there is a script.
1526 */
1527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001528list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001529{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001530 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1531 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001532 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001533}
1534
1535/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 * List variables in "arg".
1537 */
1538 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001539list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540{
1541 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001542 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001543 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001544 char_u *name_start;
1545 char_u *arg_subsc;
1546 char_u *tofree;
1547 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548
1549 while (!ends_excmd(*arg) && !got_int)
1550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001551 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001552 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001553 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001554 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 {
1556 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001557 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001558 break;
1559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001560 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 /* get_name_len() takes care of expanding curly braces */
1564 name_start = name = arg;
1565 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1566 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001568 /* This is mainly to keep test 49 working: when expanding
1569 * curly braces fails overrule the exception error message. */
1570 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001573 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001574 break;
1575 }
1576 error = TRUE;
1577 }
1578 else
1579 {
1580 if (tofree != NULL)
1581 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001582 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 else
1585 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001586 /* handle d.key, l[idx], f(expr) */
1587 arg_subsc = arg;
1588 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001589 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001591 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001593 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001594 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001595 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001596 case 'g': list_glob_vars(first); break;
1597 case 'b': list_buf_vars(first); break;
1598 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001599 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001600 case 'v': list_vim_vars(first); break;
1601 case 's': list_script_vars(first); break;
1602 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001603 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001604 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001605 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001606 }
1607 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001608 {
1609 char_u numbuf[NUMBUFLEN];
1610 char_u *tf;
1611 int c;
1612 char_u *s;
1613
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001614 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001615 c = *arg;
1616 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001617 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001618 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001619 tv.v_type,
1620 s == NULL ? (char_u *)"" : s,
1621 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001622 *arg = c;
1623 vim_free(tf);
1624 }
1625 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001627 }
1628 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001629
1630 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001632
1633 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 }
1635
1636 return arg;
1637}
1638
1639/*
1640 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1641 * Returns a pointer to the char just after the var name.
1642 * Returns NULL if there is an error.
1643 */
1644 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001645ex_let_one(
1646 char_u *arg, /* points to variable name */
1647 typval_T *tv, /* value to assign to variable */
1648 int copy, /* copy value from "tv" */
1649 char_u *endchars, /* valid chars after variable name or NULL */
1650 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651{
1652 int c1;
1653 char_u *name;
1654 char_u *p;
1655 char_u *arg_end = NULL;
1656 int len;
1657 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659
1660 /*
1661 * ":let $VAR = expr": Set environment variable.
1662 */
1663 if (*arg == '$')
1664 {
1665 /* Find the end of the name. */
1666 ++arg;
1667 name = arg;
1668 len = get_env_len(&arg);
1669 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001670 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 else
1672 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001674 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001677 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001678 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 {
1680 c1 = name[len];
1681 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001682 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 {
1685 int mustfree = FALSE;
1686 char_u *s = vim_getenv(name, &mustfree);
1687
1688 if (s != NULL)
1689 {
1690 p = tofree = concat_str(s, p);
1691 if (mustfree)
1692 vim_free(s);
1693 }
1694 }
1695 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001697 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001698 if (STRICMP(name, "HOME") == 0)
1699 init_homedir();
1700 else if (didset_vim && STRICMP(name, "VIM") == 0)
1701 didset_vim = FALSE;
1702 else if (didset_vimruntime
1703 && STRICMP(name, "VIMRUNTIME") == 0)
1704 didset_vimruntime = FALSE;
1705 arg_end = arg;
1706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001707 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 }
1710 }
1711 }
1712
1713 /*
1714 * ":let &option = expr": Set option value.
1715 * ":let &l:option = expr": Set local option value.
1716 * ":let &g:option = expr": Set global option value.
1717 */
1718 else if (*arg == '&')
1719 {
1720 /* Find the end of the name. */
1721 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 if (p == NULL || (endchars != NULL
1723 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001724 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725 else
1726 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 long n;
1728 int opt_type;
1729 long numval;
1730 char_u *stringval = NULL;
1731 char_u *s;
1732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 c1 = *p;
1734 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001736 n = (long)tv_get_number(tv);
1737 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001738 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 {
1740 opt_type = get_option_value(arg, &numval,
1741 &stringval, opt_flags);
1742 if ((opt_type == 1 && *op == '.')
1743 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001745 semsg(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001746 s = NULL; /* don't set the value */
1747 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 else
1749 {
1750 if (opt_type == 1) /* number */
1751 {
1752 if (*op == '+')
1753 n = numval + n;
1754 else
1755 n = numval - n;
1756 }
1757 else if (opt_type == 0 && stringval != NULL) /* string */
1758 {
1759 s = concat_str(stringval, s);
1760 vim_free(stringval);
1761 stringval = s;
1762 }
1763 }
1764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001765 if (s != NULL)
1766 {
1767 set_option_value(arg, n, s, opt_flags);
1768 arg_end = p;
1769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 }
1773 }
1774
1775 /*
1776 * ":let @r = expr": Set register contents.
1777 */
1778 else if (*arg == '@')
1779 {
1780 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001782 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001785 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786 else
1787 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001788 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 char_u *s;
1790
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001791 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001792 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001794 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001795 if (s != NULL)
1796 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001797 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001798 vim_free(s);
1799 }
1800 }
1801 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001802 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001804 arg_end = arg + 1;
1805 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001806 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 }
1808 }
1809
1810 /*
1811 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001812 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001814 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001818 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001821 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001822 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001823 else
1824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001826 arg_end = p;
1827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001829 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 }
1831
1832 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001833 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834
1835 return arg_end;
1836}
1837
1838/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001839 * Get an lval: variable, Dict item or List item that can be assigned a value
1840 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1841 * "name.key", "name.key[expr]" etc.
1842 * Indexing only works if "name" is an existing List or Dictionary.
1843 * "name" points to the start of the name.
1844 * If "rettv" is not NULL it points to the value to be assigned.
1845 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1846 * wrong; must end in space or cmd separator.
1847 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001848 * flags:
1849 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001850 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001851 * GLV_NO_AUTOLOAD: do not use script autoloading
1852 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001854 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001858get_lval(
1859 char_u *name,
1860 typval_T *rettv,
1861 lval_T *lp,
1862 int unlet,
1863 int skip,
1864 int flags, /* GLV_ values */
1865 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 char_u *expr_start, *expr_end;
1869 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 dictitem_T *v;
1871 typval_T var1;
1872 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001873 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001875 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001876 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001878 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001880 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001882
1883 if (skip)
1884 {
1885 /* When skipping just find the end of the name. */
1886 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001887 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 }
1889
1890 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001891 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001892 if (expr_start != NULL)
1893 {
1894 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001895 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 && *p != '[' && *p != '.')
1897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001898 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 return NULL;
1900 }
1901
1902 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1903 if (lp->ll_exp_name == NULL)
1904 {
1905 /* Report an invalid expression in braces, unless the
1906 * expression evaluation has been cancelled due to an
1907 * aborting error, an interrupt, or an exception. */
1908 if (!aborting() && !quiet)
1909 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001910 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001911 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001912 return NULL;
1913 }
1914 }
1915 lp->ll_name = lp->ll_exp_name;
1916 }
1917 else
1918 lp->ll_name = name;
1919
1920 /* Without [idx] or .key we are done. */
1921 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1922 return p;
1923
1924 cc = *p;
1925 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001926 /* Only pass &ht when we would write to the variable, it prevents autoload
1927 * as well. */
1928 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1929 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001931 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 if (v == NULL)
1934 return NULL;
1935
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 /*
1937 * Loop until no more [idx] or .key is following.
1938 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001940 var1.v_type = VAR_UNKNOWN;
1941 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1945 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001946 && lp->ll_tv->vval.v_dict != NULL)
1947 && !(lp->ll_tv->v_type == VAR_BLOB
1948 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001951 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001960
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 len = -1;
1962 if (*p == '.')
1963 {
1964 key = p + 1;
1965 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1966 ;
1967 if (len == 0)
1968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 }
1973 p = key + len;
1974 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001975 else
1976 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001978 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001979 if (*p == ':')
1980 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001981 else
1982 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001983 empty1 = FALSE;
1984 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001986 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001987 {
1988 /* not a number or string */
1989 clear_tv(&var1);
1990 return NULL;
1991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 }
1993
1994 /* Optionally get the second index [ :expr]. */
1995 if (*p == ':')
1996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002000 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002001 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002003 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002004 if (rettv != NULL
2005 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002006 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002007 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002008 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002012 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 }
2015 p = skipwhite(p + 1);
2016 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 else
2019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002021 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2022 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002023 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002025 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002026 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002027 {
2028 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002029 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002030 clear_tv(&var2);
2031 return NULL;
2032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002036 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002038
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002040 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002043 clear_tv(&var1);
2044 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 }
2047
2048 /* Skip to past ']'. */
2049 ++p;
2050 }
2051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002052 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002053 {
2054 if (len == -1)
2055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002057 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002058 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002062 }
2063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 lp->ll_list = NULL;
2065 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002066 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002067
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002068 /* When assigning to a scope dictionary check that a function and
2069 * variable name is valid (only variable name unless it is l: or
2070 * g: dictionary). Disallow overwriting a builtin function. */
2071 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002072 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002073 int prevval;
2074 int wrong;
2075
2076 if (len != -1)
2077 {
2078 prevval = key[len];
2079 key[len] = NUL;
2080 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002081 else
2082 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002083 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2084 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002085 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002086 || !valid_varname(key);
2087 if (len != -1)
2088 key[len] = prevval;
2089 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002090 return NULL;
2091 }
2092
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002094 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002095 /* Can't add "v:" variable. */
2096 if (lp->ll_dict == &vimvardict)
2097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002098 semsg(_(e_illvar), name);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 return NULL;
2100 }
2101
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002102 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002103 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002106 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 }
2110 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002112 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002114 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 p = NULL;
2117 break;
2118 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002119 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002120 else if ((flags & GLV_READ_ONLY) == 0
2121 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002122 {
2123 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002124 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002125 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002126
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002127 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002130 else if (lp->ll_tv->v_type == VAR_BLOB)
2131 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002132 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2133
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002134 /*
2135 * Get the number and item for the only or first index of the List.
2136 */
2137 if (empty1)
2138 lp->ll_n1 = 0;
2139 else
2140 // is number or string
2141 lp->ll_n1 = (long)tv_get_number(&var1);
2142 clear_tv(&var1);
2143
2144 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002145 || lp->ll_n1 > bloblen
2146 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002147 {
2148 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002150 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002151 return NULL;
2152 }
2153 if (lp->ll_range && !lp->ll_empty2)
2154 {
2155 lp->ll_n2 = (long)tv_get_number(&var2);
2156 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002157 if (lp->ll_n2 < 0
2158 || lp->ll_n2 >= bloblen
2159 || lp->ll_n2 < lp->ll_n1)
2160 {
2161 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002162 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002163 return NULL;
2164 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002165 }
2166 lp->ll_blob = lp->ll_tv->vval.v_blob;
2167 lp->ll_tv = NULL;
2168 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002169 else
2170 {
2171 /*
2172 * Get the number and item for the only or first index of the List.
2173 */
2174 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002176 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002177 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002178 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002179 clear_tv(&var1);
2180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002181 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 lp->ll_list = lp->ll_tv->vval.v_list;
2183 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2184 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002186 if (lp->ll_n1 < 0)
2187 {
2188 lp->ll_n1 = 0;
2189 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2190 }
2191 }
2192 if (lp->ll_li == NULL)
2193 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002194 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002198 }
2199
2200 /*
2201 * May need to find the item or absolute index for the second
2202 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 * When no index given: "lp->ll_empty2" is TRUE.
2204 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002208 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002209 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002215 {
2216 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002217 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002219 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002221 }
2222
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2224 if (lp->ll_n1 < 0)
2225 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2226 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002227 {
2228 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002229 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002231 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002232 }
2233
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 }
2237
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002238 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 return p;
2240}
2241
2242/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002243 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002246clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247{
2248 vim_free(lp->ll_exp_name);
2249 vim_free(lp->ll_newkey);
2250}
2251
2252/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002253 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002255 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 */
2257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002258set_var_lval(
2259 lval_T *lp,
2260 char_u *endp,
2261 typval_T *rettv,
2262 int copy,
2263 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264{
2265 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002266 listitem_T *ri;
2267 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268
2269 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002271 cc = *endp;
2272 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002273 if (lp->ll_blob != NULL)
2274 {
2275 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002276
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002277 if (op != NULL && *op != '=')
2278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002279 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002280 return;
2281 }
2282
2283 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2284 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002285 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002286
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002287 if (lp->ll_empty2)
2288 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2289
2290 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002292 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002293 return;
2294 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002295 if (lp->ll_empty2)
2296 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002297
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002298 ir = 0;
2299 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2300 blob_set(lp->ll_blob, il,
2301 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002302 }
2303 else
2304 {
2305 val = (int)tv_get_number_chk(rettv, &error);
2306 if (!error)
2307 {
2308 garray_T *gap = &lp->ll_blob->bv_ga;
2309
2310 // Allow for appending a byte. Setting a byte beyond
2311 // the end is an error otherwise.
2312 if (lp->ll_n1 < gap->ga_len
2313 || (lp->ll_n1 == gap->ga_len
2314 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2315 {
2316 blob_set(lp->ll_blob, lp->ll_n1, val);
2317 if (lp->ll_n1 == gap->ga_len)
2318 ++gap->ga_len;
2319 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002320 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002321 }
2322 }
2323 }
2324 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002326 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327
Bram Moolenaar79518e22017-02-17 16:31:35 +01002328 /* handle +=, -= and .= */
2329 di = NULL;
2330 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2331 &tv, &di, TRUE, FALSE) == OK)
2332 {
2333 if ((di == NULL
2334 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2335 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2336 FALSE)))
2337 && tv_op(&tv, rettv, op) == OK)
2338 set_var(lp->ll_name, &tv, FALSE);
2339 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002342 else
2343 set_var(lp->ll_name, rettv, copy);
2344 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002346 else if (tv_check_lock(lp->ll_newkey == NULL
2347 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002348 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002349 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 else if (lp->ll_range)
2351 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002352 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002353 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002354
2355 /*
2356 * Check whether any of the list items is locked
2357 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002358 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002359 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002360 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002361 return;
2362 ri = ri->li_next;
2363 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2364 break;
2365 ll_li = ll_li->li_next;
2366 ++ll_n1;
2367 }
2368
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 /*
2370 * Assign the List values to the list items.
2371 */
2372 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002374 if (op != NULL && *op != '=')
2375 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2376 else
2377 {
2378 clear_tv(&lp->ll_li->li_tv);
2379 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2380 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 ri = ri->li_next;
2382 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2383 break;
2384 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002387 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 ri = NULL;
2390 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002391 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002392 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 lp->ll_li = lp->ll_li->li_next;
2394 ++lp->ll_n1;
2395 }
2396 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002397 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 else if (lp->ll_empty2
2399 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002401 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 }
2403 else
2404 {
2405 /*
2406 * Assign to a List or Dictionary item.
2407 */
2408 if (lp->ll_newkey != NULL)
2409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 if (op != NULL && *op != '=')
2411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002412 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 return;
2414 }
2415
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002417 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 if (di == NULL)
2419 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002420 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2421 {
2422 vim_free(di);
2423 return;
2424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 else if (op != NULL && *op != '=')
2428 {
2429 tv_op(lp->ll_tv, rettv, op);
2430 return;
2431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 /*
2436 * Assign the value to the variable or list item.
2437 */
2438 if (copy)
2439 copy_tv(rettv, lp->ll_tv);
2440 else
2441 {
2442 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002443 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 }
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447}
2448
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2451 * Returns OK or FAIL.
2452 */
2453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002454tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002456 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 char_u numbuf[NUMBUFLEN];
2458 char_u *s;
2459
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002460 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2461 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2462 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 {
2464 switch (tv1->v_type)
2465 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002466 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 case VAR_DICT:
2468 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002469 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002470 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002471 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002472 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 break;
2474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002475 case VAR_BLOB:
2476 if (*op != '+' || tv2->v_type != VAR_BLOB)
2477 break;
2478 // BLOB += BLOB
2479 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2480 {
2481 blob_T *b1 = tv1->vval.v_blob;
2482 blob_T *b2 = tv2->vval.v_blob;
2483 int i, len = blob_len(b2);
2484 for (i = 0; i < len; i++)
2485 ga_append(&b1->bv_ga, blob_get(b2, i));
2486 }
2487 return OK;
2488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 case VAR_LIST:
2490 if (*op != '+' || tv2->v_type != VAR_LIST)
2491 break;
2492 /* List += List */
2493 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2494 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2495 return OK;
2496
2497 case VAR_NUMBER:
2498 case VAR_STRING:
2499 if (tv2->v_type == VAR_LIST)
2500 break;
2501 if (*op == '+' || *op == '-')
2502 {
2503 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505#ifdef FEAT_FLOAT
2506 if (tv2->v_type == VAR_FLOAT)
2507 {
2508 float_T f = n;
2509
2510 if (*op == '+')
2511 f += tv2->vval.v_float;
2512 else
2513 f -= tv2->vval.v_float;
2514 clear_tv(tv1);
2515 tv1->v_type = VAR_FLOAT;
2516 tv1->vval.v_float = f;
2517 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002519#endif
2520 {
2521 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002522 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002523 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002524 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002525 clear_tv(tv1);
2526 tv1->v_type = VAR_NUMBER;
2527 tv1->vval.v_number = n;
2528 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 }
2530 else
2531 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002532 if (tv2->v_type == VAR_FLOAT)
2533 break;
2534
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 s = tv_get_string(tv1);
2537 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 clear_tv(tv1);
2539 tv1->v_type = VAR_STRING;
2540 tv1->vval.v_string = s;
2541 }
2542 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002543
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002544 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002545#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002546 {
2547 float_T f;
2548
2549 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2550 && tv2->v_type != VAR_NUMBER
2551 && tv2->v_type != VAR_STRING))
2552 break;
2553 if (tv2->v_type == VAR_FLOAT)
2554 f = tv2->vval.v_float;
2555 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002556 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002557 if (*op == '+')
2558 tv1->vval.v_float += f;
2559 else
2560 tv1->vval.v_float -= f;
2561 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002563 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 }
2565 }
2566
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002567 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 return FAIL;
2569}
2570
2571/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002572 * Evaluate the expression used in a ":for var in expr" command.
2573 * "arg" points to "var".
2574 * Set "*errp" to TRUE for an error, FALSE otherwise;
2575 * Return a pointer that holds the info. Null when there is an error.
2576 */
2577 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002578eval_for_line(
2579 char_u *arg,
2580 int *errp,
2581 char_u **nextcmdp,
2582 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002583{
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002585 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 typval_T tv;
2587 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002588
2589 *errp = TRUE; /* default: there is an error */
2590
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002592 if (fi == NULL)
2593 return NULL;
2594
2595 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2596 if (expr == NULL)
2597 return fi;
2598
2599 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002600 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002603 return fi;
2604 }
2605
2606 if (skip)
2607 ++emsg_skip;
2608 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2609 {
2610 *errp = FALSE;
2611 if (!skip)
2612 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002613 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002614 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002615 l = tv.vval.v_list;
2616 if (l == NULL)
2617 {
2618 // a null list is like an empty list: do nothing
2619 clear_tv(&tv);
2620 }
2621 else
2622 {
2623 // No need to increment the refcount, it's already set for
2624 // the list being used in "tv".
2625 fi->fi_list = l;
2626 list_add_watch(l, &fi->fi_lw);
2627 fi->fi_lw.lw_item = l->lv_first;
2628 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002629 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002630 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002631 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002632 fi->fi_bi = 0;
2633 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002634 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002635 typval_T btv;
2636
2637 // Make a copy, so that the iteration still works when the
2638 // blob is changed.
2639 blob_copy(&tv, &btv);
2640 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002641 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002642 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002643 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002644 else
2645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002647 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002648 }
2649 }
2650 }
2651 if (skip)
2652 --emsg_skip;
2653
2654 return fi;
2655}
2656
2657/*
2658 * Use the first item in a ":for" list. Advance to the next.
2659 * Assign the values to the variable (list). "arg" points to the first one.
2660 * Return TRUE when a valid item was found, FALSE when at end of list or
2661 * something wrong.
2662 */
2663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002665{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002666 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002667 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002668 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002670 if (fi->fi_blob != NULL)
2671 {
2672 typval_T tv;
2673
2674 if (fi->fi_bi >= blob_len(fi->fi_blob))
2675 return FALSE;
2676 tv.v_type = VAR_NUMBER;
2677 tv.v_lock = VAR_FIXED;
2678 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2679 ++fi->fi_bi;
2680 return ex_let_vars(arg, &tv, TRUE,
2681 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2682 }
2683
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002684 item = fi->fi_lw.lw_item;
2685 if (item == NULL)
2686 result = FALSE;
2687 else
2688 {
2689 fi->fi_lw.lw_item = item->li_next;
2690 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2691 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2692 }
2693 return result;
2694}
2695
2696/*
2697 * Free the structure used to store info used by ":for".
2698 */
2699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002700free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002701{
Bram Moolenaar33570922005-01-25 22:26:29 +00002702 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002703
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002704 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002705 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002707 list_unref(fi->fi_list);
2708 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002709 if (fi != NULL && fi->fi_blob != NULL)
2710 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711 vim_free(fi);
2712}
2713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2715
2716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717set_context_for_expression(
2718 expand_T *xp,
2719 char_u *arg,
2720 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 int got_eq = FALSE;
2723 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002726 if (cmdidx == CMD_let)
2727 {
2728 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002729 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002730 {
2731 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002732 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002733 {
2734 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002735 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002736 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737 break;
2738 }
2739 return;
2740 }
2741 }
2742 else
2743 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2744 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 while ((xp->xp_pattern = vim_strpbrk(arg,
2746 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2747 {
2748 c = *xp->xp_pattern;
2749 if (c == '&')
2750 {
2751 c = xp->xp_pattern[1];
2752 if (c == '&')
2753 {
2754 ++xp->xp_pattern;
2755 xp->xp_context = cmdidx != CMD_let || got_eq
2756 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2757 }
2758 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002761 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2762 xp->xp_pattern += 2;
2763
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766 else if (c == '$')
2767 {
2768 /* environment variable */
2769 xp->xp_context = EXPAND_ENV_VARS;
2770 }
2771 else if (c == '=')
2772 {
2773 got_eq = TRUE;
2774 xp->xp_context = EXPAND_EXPRESSION;
2775 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002776 else if (c == '#'
2777 && xp->xp_context == EXPAND_EXPRESSION)
2778 {
2779 /* Autoload function/variable contains '#'. */
2780 break;
2781 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002782 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 && xp->xp_context == EXPAND_FUNCTIONS
2784 && vim_strchr(xp->xp_pattern, '(') == NULL)
2785 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002786 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 break;
2788 }
2789 else if (cmdidx != CMD_let || got_eq)
2790 {
2791 if (c == '"') /* string */
2792 {
2793 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2794 if (c == '\\' && xp->xp_pattern[1] != NUL)
2795 ++xp->xp_pattern;
2796 xp->xp_context = EXPAND_NOTHING;
2797 }
2798 else if (c == '\'') /* literal string */
2799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2802 /* skip */ ;
2803 xp->xp_context = EXPAND_NOTHING;
2804 }
2805 else if (c == '|')
2806 {
2807 if (xp->xp_pattern[1] == '|')
2808 {
2809 ++xp->xp_pattern;
2810 xp->xp_context = EXPAND_EXPRESSION;
2811 }
2812 else
2813 xp->xp_context = EXPAND_COMMANDS;
2814 }
2815 else
2816 xp->xp_context = EXPAND_EXPRESSION;
2817 }
2818 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819 /* Doesn't look like something valid, expand as an expression
2820 * anyway. */
2821 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 arg = xp->xp_pattern;
2823 if (*arg != NUL)
2824 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2825 /* skip */ ;
2826 }
2827 xp->xp_pattern = arg;
2828}
2829
2830#endif /* FEAT_CMDL_COMPL */
2831
2832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 * ":unlet[!] var1 ... " command.
2834 */
2835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002836ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002838 ex_unletlock(eap, eap->arg, 0);
2839}
2840
2841/*
2842 * ":lockvar" and ":unlockvar" commands
2843 */
2844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002846{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 int deep = 2;
2849
2850 if (eap->forceit)
2851 deep = -1;
2852 else if (vim_isdigit(*arg))
2853 {
2854 deep = getdigits(&arg);
2855 arg = skipwhite(arg);
2856 }
2857
2858 ex_unletlock(eap, arg, deep);
2859}
2860
2861/*
2862 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2863 */
2864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002865ex_unletlock(
2866 exarg_T *eap,
2867 char_u *argstart,
2868 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869{
2870 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002873 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875 do
2876 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002877 if (*arg == '$')
2878 {
2879 char_u *name = ++arg;
2880
2881 if (get_env_len(&arg) == 0)
2882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002883 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002884 return;
2885 }
2886 vim_unsetenv(name);
2887 arg = skipwhite(arg);
2888 continue;
2889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002892 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002893 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 if (lv.ll_name == NULL)
2895 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002896 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (name_end != NULL)
2900 {
2901 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002902 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 }
2904 if (!(eap->skip || error))
2905 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 break;
2907 }
2908
2909 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 {
2911 if (eap->cmdidx == CMD_unlet)
2912 {
2913 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2914 error = TRUE;
2915 }
2916 else
2917 {
2918 if (do_lock_var(&lv, name_end, deep,
2919 eap->cmdidx == CMD_lockvar) == FAIL)
2920 error = TRUE;
2921 }
2922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 if (!eap->skip)
2925 clear_lval(&lv);
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 arg = skipwhite(name_end);
2928 } while (!ends_excmd(*arg));
2929
2930 eap->nextcmd = check_nextcmd(arg);
2931}
2932
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002934do_unlet_var(
2935 lval_T *lp,
2936 char_u *name_end,
2937 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002938{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 int ret = OK;
2940 int cc;
2941
2942 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 cc = *name_end;
2945 *name_end = NUL;
2946
2947 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002948 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002951 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002952 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002953 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002954 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002955 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 else if (lp->ll_range)
2958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002959 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002961 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002962
2963 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2964 {
2965 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002966 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002967 return FAIL;
2968 ll_li = li;
2969 ++ll_n1;
2970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971
2972 /* Delete a range of List items. */
2973 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2974 {
2975 li = lp->ll_li->li_next;
2976 listitem_remove(lp->ll_list, lp->ll_li);
2977 lp->ll_li = li;
2978 ++lp->ll_n1;
2979 }
2980 }
2981 else
2982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 /* unlet a List item. */
2985 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002988 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 }
2990
2991 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002992}
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
2995 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002996 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 */
2998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002999do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 hashtab_T *ht;
3002 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003003 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003004 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003005 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
Bram Moolenaar33570922005-01-25 22:26:29 +00003007 ht = find_var_ht(name, &varname);
3008 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003011 if (d == NULL)
3012 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 if (ht == &globvarht)
3014 d = &globvardict;
3015 else if (ht == &compat_hashtab)
3016 d = &vimvardict;
3017 else
3018 {
3019 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3020 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3021 }
3022 if (d == NULL)
3023 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003024 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 return FAIL;
3026 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003029 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003030 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003031 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003032 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003033 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003034 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003035 || var_check_ro(di->di_flags, name, FALSE)
3036 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003037 return FAIL;
3038
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003039 delete_var(ht, hi);
3040 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003043 if (forceit)
3044 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003045 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 return FAIL;
3047}
3048
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003049/*
3050 * Lock or unlock variable indicated by "lp".
3051 * "deep" is the levels to go (-1 for unlimited);
3052 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3053 */
3054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055do_lock_var(
3056 lval_T *lp,
3057 char_u *name_end,
3058 int deep,
3059 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003060{
3061 int ret = OK;
3062 int cc;
3063 dictitem_T *di;
3064
3065 if (deep == 0) /* nothing to do */
3066 return OK;
3067
3068 if (lp->ll_tv == NULL)
3069 {
3070 cc = *name_end;
3071 *name_end = NUL;
3072
3073 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003074 di = find_var(lp->ll_name, NULL, TRUE);
3075 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003076 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003077 else if ((di->di_flags & DI_FLAGS_FIX)
3078 && di->di_tv.v_type != VAR_DICT
3079 && di->di_tv.v_type != VAR_LIST)
3080 /* For historic reasons this error is not given for a list or dict.
3081 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003082 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003083 else
3084 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003085 if (lock)
3086 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003087 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003088 di->di_flags &= ~DI_FLAGS_LOCK;
3089 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003090 }
3091 *name_end = cc;
3092 }
3093 else if (lp->ll_range)
3094 {
3095 listitem_T *li = lp->ll_li;
3096
3097 /* (un)lock a range of List items. */
3098 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3099 {
3100 item_lock(&li->li_tv, deep, lock);
3101 li = li->li_next;
3102 ++lp->ll_n1;
3103 }
3104 }
3105 else if (lp->ll_list != NULL)
3106 /* (un)lock a List item. */
3107 item_lock(&lp->ll_li->li_tv, deep, lock);
3108 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003109 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003110 item_lock(&lp->ll_di->di_tv, deep, lock);
3111
3112 return ret;
3113}
3114
3115/*
3116 * Lock or unlock an item. "deep" is nr of levels to go.
3117 */
3118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003119item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003120{
3121 static int recurse = 0;
3122 list_T *l;
3123 listitem_T *li;
3124 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003125 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003126 hashitem_T *hi;
3127 int todo;
3128
3129 if (recurse >= DICT_MAXNEST)
3130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003131 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003132 return;
3133 }
3134 if (deep == 0)
3135 return;
3136 ++recurse;
3137
3138 /* lock/unlock the item itself */
3139 if (lock)
3140 tv->v_lock |= VAR_LOCKED;
3141 else
3142 tv->v_lock &= ~VAR_LOCKED;
3143
3144 switch (tv->v_type)
3145 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003146 case VAR_UNKNOWN:
3147 case VAR_NUMBER:
3148 case VAR_STRING:
3149 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003150 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003151 case VAR_FLOAT:
3152 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003153 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003154 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003155 break;
3156
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003157 case VAR_BLOB:
3158 if ((b = tv->vval.v_blob) != NULL)
3159 {
3160 if (lock)
3161 b->bv_lock |= VAR_LOCKED;
3162 else
3163 b->bv_lock &= ~VAR_LOCKED;
3164 }
3165 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003166 case VAR_LIST:
3167 if ((l = tv->vval.v_list) != NULL)
3168 {
3169 if (lock)
3170 l->lv_lock |= VAR_LOCKED;
3171 else
3172 l->lv_lock &= ~VAR_LOCKED;
3173 if (deep < 0 || deep > 1)
3174 /* recursive: lock/unlock the items the List contains */
3175 for (li = l->lv_first; li != NULL; li = li->li_next)
3176 item_lock(&li->li_tv, deep - 1, lock);
3177 }
3178 break;
3179 case VAR_DICT:
3180 if ((d = tv->vval.v_dict) != NULL)
3181 {
3182 if (lock)
3183 d->dv_lock |= VAR_LOCKED;
3184 else
3185 d->dv_lock &= ~VAR_LOCKED;
3186 if (deep < 0 || deep > 1)
3187 {
3188 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003189 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003190 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3191 {
3192 if (!HASHITEM_EMPTY(hi))
3193 {
3194 --todo;
3195 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3196 }
3197 }
3198 }
3199 }
3200 }
3201 --recurse;
3202}
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3205/*
3206 * Delete all "menutrans_" variables.
3207 */
3208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003212 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003215 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003217 {
3218 if (!HASHITEM_EMPTY(hi))
3219 {
3220 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3222 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003223 }
3224 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226}
3227#endif
3228
3229#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3230
3231/*
3232 * Local string buffer for the next two functions to store a variable name
3233 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3234 * get_user_var_name().
3235 */
3236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237static char_u *varnamebuf = NULL;
3238static int varnamebuflen = 0;
3239
3240/*
3241 * Function to concatenate a prefix and a variable name.
3242 */
3243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 int len;
3247
3248 len = (int)STRLEN(name) + 3;
3249 if (len > varnamebuflen)
3250 {
3251 vim_free(varnamebuf);
3252 len += 10; /* some additional space */
3253 varnamebuf = alloc(len);
3254 if (varnamebuf == NULL)
3255 {
3256 varnamebuflen = 0;
3257 return NULL;
3258 }
3259 varnamebuflen = len;
3260 }
3261 *varnamebuf = prefix;
3262 varnamebuf[1] = ':';
3263 STRCPY(varnamebuf + 2, name);
3264 return varnamebuf;
3265}
3266
3267/*
3268 * Function given to ExpandGeneric() to obtain the list of user defined
3269 * (global/buffer/window/built-in) variable names.
3270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003274 static long_u gdone;
3275 static long_u bdone;
3276 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003277 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003278 static int vidx;
3279 static hashitem_T *hi;
3280 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003283 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003284 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003285 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003286 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003287
3288 /* Global variables */
3289 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003291 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003293 else
3294 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003295 while (HASHITEM_EMPTY(hi))
3296 ++hi;
3297 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3298 return cat_prefix_varname('g', hi->hi_key);
3299 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003301
3302 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003303 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003306 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003308 else
3309 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003310 while (HASHITEM_EMPTY(hi))
3311 ++hi;
3312 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003314
3315 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003316 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003319 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003321 else
3322 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003323 while (HASHITEM_EMPTY(hi))
3324 ++hi;
3325 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003327
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003328 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003329 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003330 if (tdone < ht->ht_used)
3331 {
3332 if (tdone++ == 0)
3333 hi = ht->ht_array;
3334 else
3335 ++hi;
3336 while (HASHITEM_EMPTY(hi))
3337 ++hi;
3338 return cat_prefix_varname('t', hi->hi_key);
3339 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003340
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 /* v: variables */
3342 if (vidx < VV_LEN)
3343 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaard23a8232018-02-10 18:45:26 +01003345 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 varnamebuflen = 0;
3347 return NULL;
3348}
3349
3350#endif /* FEAT_CMDL_COMPL */
3351
3352/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003353 * Return TRUE if "pat" matches "text".
3354 * Does not use 'cpo' and always uses 'magic'.
3355 */
3356 static int
3357pattern_match(char_u *pat, char_u *text, int ic)
3358{
3359 int matches = FALSE;
3360 char_u *save_cpo;
3361 regmatch_T regmatch;
3362
3363 /* avoid 'l' flag in 'cpoptions' */
3364 save_cpo = p_cpo;
3365 p_cpo = (char_u *)"";
3366 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3367 if (regmatch.regprog != NULL)
3368 {
3369 regmatch.rm_ic = ic;
3370 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3371 vim_regfree(regmatch.regprog);
3372 }
3373 p_cpo = save_cpo;
3374 return matches;
3375}
3376
3377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003379 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3381 */
3382
3383/*
3384 * Handle zero level expression.
3385 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003386 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003387 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * Return OK or FAIL.
3389 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391eval0(
3392 char_u *arg,
3393 typval_T *rettv,
3394 char_u **nextcmd,
3395 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 int ret;
3398 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003399 int did_emsg_before = did_emsg;
3400 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
3402 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (ret == FAIL || !ends_excmd(*p))
3405 {
3406 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 /*
3409 * Report the invalid expression unless the expression evaluation has
3410 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003411 * exception, or we already gave a more specific error.
3412 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003414 if (!aborting() && did_emsg == did_emsg_before
3415 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003416 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 ret = FAIL;
3418 }
3419 if (nextcmd != NULL)
3420 *nextcmd = check_nextcmd(p);
3421
3422 return ret;
3423}
3424
3425/*
3426 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003427 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 *
3429 * "arg" must point to the first non-white of the expression.
3430 * "arg" is advanced to the next non-white after the recognized expression.
3431 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003432 * Note: "rettv.v_lock" is not set.
3433 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * Return OK or FAIL.
3435 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003437eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 /*
3443 * Get the first variable.
3444 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 return FAIL;
3447
3448 if ((*arg)[0] == '?')
3449 {
3450 result = FALSE;
3451 if (evaluate)
3452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003453 int error = FALSE;
3454
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003455 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003457 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003458 if (error)
3459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461
3462 /*
3463 * Get the second variable.
3464 */
3465 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 return FAIL;
3468
3469 /*
3470 * Check for the ":".
3471 */
3472 if ((*arg)[0] != ':')
3473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003474 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return FAIL;
3478 }
3479
3480 /*
3481 * Get the third variable.
3482 */
3483 *arg = skipwhite(*arg + 1);
3484 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3485 {
3486 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return FAIL;
3489 }
3490 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 return OK;
3495}
3496
3497/*
3498 * Handle first level expression:
3499 * expr2 || expr2 || expr2 logical OR
3500 *
3501 * "arg" must point to the first non-white of the expression.
3502 * "arg" is advanced to the next non-white after the recognized expression.
3503 *
3504 * Return OK or FAIL.
3505 */
3506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
Bram Moolenaar33570922005-01-25 22:26:29 +00003509 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 long result;
3511 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003512 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 /*
3515 * Get the first variable.
3516 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003517 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 return FAIL;
3519
3520 /*
3521 * Repeat until there is no following "||".
3522 */
3523 first = TRUE;
3524 result = FALSE;
3525 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3526 {
3527 if (evaluate && first)
3528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 if (error)
3533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 first = FALSE;
3535 }
3536
3537 /*
3538 * Get the second variable.
3539 */
3540 *arg = skipwhite(*arg + 2);
3541 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3542 return FAIL;
3543
3544 /*
3545 * Compute the result.
3546 */
3547 if (evaluate && !result)
3548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003549 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003552 if (error)
3553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 if (evaluate)
3556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 rettv->v_type = VAR_NUMBER;
3558 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 }
3561
3562 return OK;
3563}
3564
3565/*
3566 * Handle second level expression:
3567 * expr3 && expr3 && expr3 logical AND
3568 *
3569 * "arg" must point to the first non-white of the expression.
3570 * "arg" is advanced to the next non-white after the recognized expression.
3571 *
3572 * Return OK or FAIL.
3573 */
3574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Bram Moolenaar33570922005-01-25 22:26:29 +00003577 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 long result;
3579 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 /*
3583 * Get the first variable.
3584 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003585 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 return FAIL;
3587
3588 /*
3589 * Repeat until there is no following "&&".
3590 */
3591 first = TRUE;
3592 result = TRUE;
3593 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3594 {
3595 if (evaluate && first)
3596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 if (error)
3601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 first = FALSE;
3603 }
3604
3605 /*
3606 * Get the second variable.
3607 */
3608 *arg = skipwhite(*arg + 2);
3609 if (eval4(arg, &var2, evaluate && result) == FAIL)
3610 return FAIL;
3611
3612 /*
3613 * Compute the result.
3614 */
3615 if (evaluate && result)
3616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003617 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003619 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003620 if (error)
3621 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 if (evaluate)
3624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003625 rettv->v_type = VAR_NUMBER;
3626 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628 }
3629
3630 return OK;
3631}
3632
3633/*
3634 * Handle third level expression:
3635 * var1 == var2
3636 * var1 =~ var2
3637 * var1 != var2
3638 * var1 !~ var2
3639 * var1 > var2
3640 * var1 >= var2
3641 * var1 < var2
3642 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003643 * var1 is var2
3644 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 *
3646 * "arg" must point to the first non-white of the expression.
3647 * "arg" is advanced to the next non-white after the recognized expression.
3648 *
3649 * Return OK or FAIL.
3650 */
3651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003652eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 char_u *p;
3656 int i;
3657 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003658 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 /*
3663 * Get the first variable.
3664 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 return FAIL;
3667
3668 p = *arg;
3669 switch (p[0])
3670 {
3671 case '=': if (p[1] == '=')
3672 type = TYPE_EQUAL;
3673 else if (p[1] == '~')
3674 type = TYPE_MATCH;
3675 break;
3676 case '!': if (p[1] == '=')
3677 type = TYPE_NEQUAL;
3678 else if (p[1] == '~')
3679 type = TYPE_NOMATCH;
3680 break;
3681 case '>': if (p[1] != '=')
3682 {
3683 type = TYPE_GREATER;
3684 len = 1;
3685 }
3686 else
3687 type = TYPE_GEQUAL;
3688 break;
3689 case '<': if (p[1] != '=')
3690 {
3691 type = TYPE_SMALLER;
3692 len = 1;
3693 }
3694 else
3695 type = TYPE_SEQUAL;
3696 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003697 case 'i': if (p[1] == 's')
3698 {
3699 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3700 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003701 i = p[len];
3702 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003703 {
3704 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3705 type_is = TRUE;
3706 }
3707 }
3708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710
3711 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003712 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 */
3714 if (type != TYPE_UNKNOWN)
3715 {
3716 /* extra question mark appended: ignore case */
3717 if (p[len] == '?')
3718 {
3719 ic = TRUE;
3720 ++len;
3721 }
3722 /* extra '#' appended: match case */
3723 else if (p[len] == '#')
3724 {
3725 ic = FALSE;
3726 ++len;
3727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 else
3730 ic = p_ic;
3731
3732 /*
3733 * Get the second variable.
3734 */
3735 *arg = skipwhite(p + len);
3736 if (eval5(arg, &var2, evaluate) == FAIL)
3737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 return FAIL;
3740 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003741 if (evaluate)
3742 {
3743 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3744
3745 clear_tv(&var2);
3746 return ret;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
3750 return OK;
3751}
3752
3753/*
3754 * Handle fourth level expression:
3755 * + number addition
3756 * - number subtraction
3757 * . string concatenation
3758 *
3759 * "arg" must point to the first non-white of the expression.
3760 * "arg" is advanced to the next non-white after the recognized expression.
3761 *
3762 * Return OK or FAIL.
3763 */
3764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003765eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 typval_T var2;
3768 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003770 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003771#ifdef FEAT_FLOAT
3772 float_T f1 = 0, f2 = 0;
3773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 char_u *s1, *s2;
3775 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3776 char_u *p;
3777
3778 /*
3779 * Get the first variable.
3780 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003781 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return FAIL;
3783
3784 /*
3785 * Repeat computing, until no '+', '-' or '.' is following.
3786 */
3787 for (;;)
3788 {
3789 op = **arg;
3790 if (op != '+' && op != '-' && op != '.')
3791 break;
3792
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003793 if ((op != '+' || (rettv->v_type != VAR_LIST
3794 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003795#ifdef FEAT_FLOAT
3796 && (op == '.' || rettv->v_type != VAR_FLOAT)
3797#endif
3798 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003799 {
3800 /* For "list + ...", an illegal use of the first operand as
3801 * a number cannot be determined before evaluating the 2nd
3802 * operand: if this is also a list, all is ok.
3803 * For "something . ...", "something - ..." or "non-list + ...",
3804 * we know that the first operand needs to be a string or number
3805 * without evaluating the 2nd operand. So check before to avoid
3806 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003807 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003808 {
3809 clear_tv(rettv);
3810 return FAIL;
3811 }
3812 }
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 /*
3815 * Get the second variable.
3816 */
3817 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003818 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822 }
3823
3824 if (evaluate)
3825 {
3826 /*
3827 * Compute the result.
3828 */
3829 if (op == '.')
3830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003831 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3832 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 if (s2 == NULL) /* type error ? */
3834 {
3835 clear_tv(rettv);
3836 clear_tv(&var2);
3837 return FAIL;
3838 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003839 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 clear_tv(rettv);
3841 rettv->v_type = VAR_STRING;
3842 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003844 else if (op == '+' && rettv->v_type == VAR_BLOB
3845 && var2.v_type == VAR_BLOB)
3846 {
3847 blob_T *b1 = rettv->vval.v_blob;
3848 blob_T *b2 = var2.vval.v_blob;
3849 blob_T *b = blob_alloc();
3850 int i;
3851
3852 if (b != NULL)
3853 {
3854 for (i = 0; i < blob_len(b1); i++)
3855 ga_append(&b->bv_ga, blob_get(b1, i));
3856 for (i = 0; i < blob_len(b2); i++)
3857 ga_append(&b->bv_ga, blob_get(b2, i));
3858
3859 clear_tv(rettv);
3860 rettv_blob_set(rettv, b);
3861 }
3862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003863 else if (op == '+' && rettv->v_type == VAR_LIST
3864 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003865 {
3866 /* concatenate Lists */
3867 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3868 &var3) == FAIL)
3869 {
3870 clear_tv(rettv);
3871 clear_tv(&var2);
3872 return FAIL;
3873 }
3874 clear_tv(rettv);
3875 *rettv = var3;
3876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 else
3878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003879 int error = FALSE;
3880
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003881#ifdef FEAT_FLOAT
3882 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003884 f1 = rettv->vval.v_float;
3885 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003887 else
3888#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003890 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003891 if (error)
3892 {
3893 /* This can only happen for "list + non-list". For
3894 * "non-list + ..." or "something - ...", we returned
3895 * before evaluating the 2nd operand. */
3896 clear_tv(rettv);
3897 return FAIL;
3898 }
3899#ifdef FEAT_FLOAT
3900 if (var2.v_type == VAR_FLOAT)
3901 f1 = n1;
3902#endif
3903 }
3904#ifdef FEAT_FLOAT
3905 if (var2.v_type == VAR_FLOAT)
3906 {
3907 f2 = var2.vval.v_float;
3908 n2 = 0;
3909 }
3910 else
3911#endif
3912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003913 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003914 if (error)
3915 {
3916 clear_tv(rettv);
3917 clear_tv(&var2);
3918 return FAIL;
3919 }
3920#ifdef FEAT_FLOAT
3921 if (rettv->v_type == VAR_FLOAT)
3922 f2 = n2;
3923#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003926
3927#ifdef FEAT_FLOAT
3928 /* If there is a float on either side the result is a float. */
3929 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3930 {
3931 if (op == '+')
3932 f1 = f1 + f2;
3933 else
3934 f1 = f1 - f2;
3935 rettv->v_type = VAR_FLOAT;
3936 rettv->vval.v_float = f1;
3937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003939#endif
3940 {
3941 if (op == '+')
3942 n1 = n1 + n2;
3943 else
3944 n1 = n1 - n2;
3945 rettv->v_type = VAR_NUMBER;
3946 rettv->vval.v_number = n1;
3947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 }
3952 return OK;
3953}
3954
3955/*
3956 * Handle fifth level expression:
3957 * * number multiplication
3958 * / number division
3959 * % number modulo
3960 *
3961 * "arg" must point to the first non-white of the expression.
3962 * "arg" is advanced to the next non-white after the recognized expression.
3963 *
3964 * Return OK or FAIL.
3965 */
3966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003967eval6(
3968 char_u **arg,
3969 typval_T *rettv,
3970 int evaluate,
3971 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003975 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003976#ifdef FEAT_FLOAT
3977 int use_float = FALSE;
3978 float_T f1 = 0, f2;
3979#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003980 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982 /*
3983 * Get the first variable.
3984 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003985 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 return FAIL;
3987
3988 /*
3989 * Repeat computing, until no '*', '/' or '%' is following.
3990 */
3991 for (;;)
3992 {
3993 op = **arg;
3994 if (op != '*' && op != '/' && op != '%')
3995 break;
3996
3997 if (evaluate)
3998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003999#ifdef FEAT_FLOAT
4000 if (rettv->v_type == VAR_FLOAT)
4001 {
4002 f1 = rettv->vval.v_float;
4003 use_float = TRUE;
4004 n1 = 0;
4005 }
4006 else
4007#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004008 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004009 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 if (error)
4011 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 else
4014 n1 = 0;
4015
4016 /*
4017 * Get the second variable.
4018 */
4019 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004020 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return FAIL;
4022
4023 if (evaluate)
4024 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004025#ifdef FEAT_FLOAT
4026 if (var2.v_type == VAR_FLOAT)
4027 {
4028 if (!use_float)
4029 {
4030 f1 = n1;
4031 use_float = TRUE;
4032 }
4033 f2 = var2.vval.v_float;
4034 n2 = 0;
4035 }
4036 else
4037#endif
4038 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004039 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040 clear_tv(&var2);
4041 if (error)
4042 return FAIL;
4043#ifdef FEAT_FLOAT
4044 if (use_float)
4045 f2 = n2;
4046#endif
4047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 /*
4050 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004051 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004053#ifdef FEAT_FLOAT
4054 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 if (op == '*')
4057 f1 = f1 * f2;
4058 else if (op == '/')
4059 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004060# ifdef VMS
4061 /* VMS crashes on divide by zero, work around it */
4062 if (f2 == 0.0)
4063 {
4064 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004065 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004066 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004067 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004068 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004069 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004070 }
4071 else
4072 f1 = f1 / f2;
4073# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 /* We rely on the floating point library to handle divide
4075 * by zero to result in "inf" and not a crash. */
4076 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004077# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004081 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 return FAIL;
4083 }
4084 rettv->v_type = VAR_FLOAT;
4085 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004090 if (op == '*')
4091 n1 = n1 * n2;
4092 else if (op == '/')
4093 {
4094 if (n2 == 0) /* give an error message? */
4095 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004096 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004097 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004098 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004099 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004100 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004101 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004102 }
4103 else
4104 n1 = n1 / n2;
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004107 {
4108 if (n2 == 0) /* give an error message? */
4109 n1 = 0;
4110 else
4111 n1 = n1 % n2;
4112 }
4113 rettv->v_type = VAR_NUMBER;
4114 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
4118
4119 return OK;
4120}
4121
4122/*
4123 * Handle sixth level expression:
4124 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004125 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004126 * "string" string constant
4127 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * &option-name option value
4129 * @r register contents
4130 * identifier variable value
4131 * function() function call
4132 * $VAR environment variable
4133 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004134 * [expr, expr] List
4135 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 *
4137 * Also handle:
4138 * ! in front logical NOT
4139 * - in front unary minus
4140 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004141 * trailing [] subscript in String or List
4142 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 *
4144 * "arg" must point to the first non-white of the expression.
4145 * "arg" is advanced to the next non-white after the recognized expression.
4146 *
4147 * Return OK or FAIL.
4148 */
4149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004150eval7(
4151 char_u **arg,
4152 typval_T *rettv,
4153 int evaluate,
4154 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004156 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int len;
4158 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u *start_leader, *end_leader;
4160 int ret = OK;
4161 char_u *alias;
4162
4163 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004170 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
4172 start_leader = *arg;
4173 while (**arg == '!' || **arg == '-' || **arg == '+')
4174 *arg = skipwhite(*arg + 1);
4175 end_leader = *arg;
4176
4177 switch (**arg)
4178 {
4179 /*
4180 * Number constant.
4181 */
4182 case '0':
4183 case '1':
4184 case '2':
4185 case '3':
4186 case '4':
4187 case '5':
4188 case '6':
4189 case '7':
4190 case '8':
4191 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004192 {
4193#ifdef FEAT_FLOAT
4194 char_u *p = skipdigits(*arg + 1);
4195 int get_float = FALSE;
4196
4197 /* We accept a float when the format matches
4198 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004199 * strict to avoid backwards compatibility problems.
4200 * Don't look for a float after the "." operator, so that
4201 * ":let vers = 1.2.3" doesn't fail. */
4202 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004204 get_float = TRUE;
4205 p = skipdigits(p + 2);
4206 if (*p == 'e' || *p == 'E')
4207 {
4208 ++p;
4209 if (*p == '-' || *p == '+')
4210 ++p;
4211 if (!vim_isdigit(*p))
4212 get_float = FALSE;
4213 else
4214 p = skipdigits(p + 1);
4215 }
4216 if (ASCII_ISALPHA(*p) || *p == '.')
4217 get_float = FALSE;
4218 }
4219 if (get_float)
4220 {
4221 float_T f;
4222
4223 *arg += string2float(*arg, &f);
4224 if (evaluate)
4225 {
4226 rettv->v_type = VAR_FLOAT;
4227 rettv->vval.v_float = f;
4228 }
4229 }
4230 else
4231#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004232 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004233 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004234 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004235 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004236
4237 // Blob constant: 0z0123456789abcdef
4238 if (evaluate)
4239 blob = blob_alloc();
4240 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4241 {
4242 if (!vim_isxdigit(bp[1]))
4243 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004244 if (blob != NULL)
4245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004247 ga_clear(&blob->bv_ga);
4248 VIM_CLEAR(blob);
4249 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004250 ret = FAIL;
4251 break;
4252 }
4253 if (blob != NULL)
4254 ga_append(&blob->bv_ga,
4255 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004256 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4257 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004258 }
4259 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004260 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004261 *arg = bp;
4262 }
4263 else
4264 {
4265 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004266 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004267 *arg += len;
4268 if (evaluate)
4269 {
4270 rettv->v_type = VAR_NUMBER;
4271 rettv->vval.v_number = n;
4272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /*
4278 * String constant: "string".
4279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 break;
4282
4283 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004284 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 break;
4288
4289 /*
4290 * List: [expr, expr]
4291 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 break;
4294
4295 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004296 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 * Dictionary: {key: val, key: val}
4298 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004299 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4300 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004301 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004302 break;
4303
4304 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004305 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004307 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 break;
4309
4310 /*
4311 * Environment variable: $VAR.
4312 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 break;
4315
4316 /*
4317 * Register contents: @r.
4318 */
4319 case '@': ++*arg;
4320 if (evaluate)
4321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004323 rettv->vval.v_string = get_reg_contents(**arg,
4324 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 if (**arg != NUL)
4327 ++*arg;
4328 break;
4329
4330 /*
4331 * nested expression: (expression).
4332 */
4333 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (**arg == ')')
4336 ++*arg;
4337 else if (ret == OK)
4338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004339 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 ret = FAIL;
4342 }
4343 break;
4344
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 break;
4347 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348
4349 if (ret == NOTDONE)
4350 {
4351 /*
4352 * Must be a variable or function name.
4353 * Can also be a curly-braces kind of name: {expr}.
4354 */
4355 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004356 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004357 if (alias != NULL)
4358 s = alias;
4359
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004360 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004361 ret = FAIL;
4362 else
4363 {
4364 if (**arg == '(') /* recursive! */
4365 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004366 partial_T *partial;
4367
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004368 if (!evaluate)
4369 check_vars(s, len);
4370
Bram Moolenaar8c711452005-01-14 21:53:12 +00004371 /* If "s" is the name of a variable of type VAR_FUNC
4372 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004373 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004375 /* Need to make a copy, in case evaluating the arguments makes
4376 * the name invalid. */
4377 s = vim_strsave(s);
4378 if (s == NULL)
4379 ret = FAIL;
4380 else
4381 /* Invoke the function. */
4382 ret = get_func_tv(s, len, rettv, arg,
4383 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4384 &len, evaluate, partial, NULL);
4385 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004386
4387 /* If evaluate is FALSE rettv->v_type was not set in
4388 * get_func_tv, but it's needed in handle_subscript() to parse
4389 * what follows. So set it here. */
4390 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4391 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004392 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004393 rettv->v_type = VAR_FUNC;
4394 }
4395
Bram Moolenaar8c711452005-01-14 21:53:12 +00004396 /* Stop the expression evaluation when immediately
4397 * aborting on error, or when an interrupt occurred or
4398 * an exception was thrown but not caught. */
4399 if (aborting())
4400 {
4401 if (ret == OK)
4402 clear_tv(rettv);
4403 ret = FAIL;
4404 }
4405 }
4406 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004407 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004408 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004409 {
4410 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004411 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004412 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004413 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004414 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004415 }
4416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 *arg = skipwhite(*arg);
4418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004419 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4420 * expr(expr). */
4421 if (ret == OK)
4422 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 /*
4425 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4426 */
4427 if (ret == OK && evaluate && end_leader > start_leader)
4428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004430 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431#ifdef FEAT_FLOAT
4432 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004434 if (rettv->v_type == VAR_FLOAT)
4435 f = rettv->vval.v_float;
4436 else
4437#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004438 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004439 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 clear_tv(rettv);
4442 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004444 else
4445 {
4446 while (end_leader > start_leader)
4447 {
4448 --end_leader;
4449 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004450 {
4451#ifdef FEAT_FLOAT
4452 if (rettv->v_type == VAR_FLOAT)
4453 f = !f;
4454 else
4455#endif
4456 val = !val;
4457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004458 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459 {
4460#ifdef FEAT_FLOAT
4461 if (rettv->v_type == VAR_FLOAT)
4462 f = -f;
4463 else
4464#endif
4465 val = -val;
4466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004467 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004468#ifdef FEAT_FLOAT
4469 if (rettv->v_type == VAR_FLOAT)
4470 {
4471 clear_tv(rettv);
4472 rettv->vval.v_float = f;
4473 }
4474 else
4475#endif
4476 {
4477 clear_tv(rettv);
4478 rettv->v_type = VAR_NUMBER;
4479 rettv->vval.v_number = val;
4480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483
4484 return ret;
4485}
4486
4487/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004488 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4489 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4491 */
4492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004493eval_index(
4494 char_u **arg,
4495 typval_T *rettv,
4496 int evaluate,
4497 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004498{
4499 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004500 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004501 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 long len = -1;
4504 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507
Bram Moolenaara03f2332016-02-06 18:09:59 +01004508 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004510 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004511 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004512 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004513 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004514 return FAIL;
4515 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004516#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004517 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004518 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004519 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004520#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004521 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004522 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004523 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004524 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004525 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004526 return FAIL;
4527 case VAR_UNKNOWN:
4528 if (evaluate)
4529 return FAIL;
4530 /* FALLTHROUGH */
4531
4532 case VAR_STRING:
4533 case VAR_NUMBER:
4534 case VAR_LIST:
4535 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004536 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004537 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004538 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004540 init_tv(&var1);
4541 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 /*
4545 * dict.name
4546 */
4547 key = *arg + 1;
4548 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4549 ;
4550 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004551 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004553 }
4554 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004555 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 /*
4557 * something[idx]
4558 *
4559 * Get the (first) variable from inside the [].
4560 */
4561 *arg = skipwhite(*arg + 1);
4562 if (**arg == ':')
4563 empty1 = TRUE;
4564 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4565 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004566 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 {
4568 /* not a number or string */
4569 clear_tv(&var1);
4570 return FAIL;
4571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572
4573 /*
4574 * Get the second variable from inside the [:].
4575 */
4576 if (**arg == ':')
4577 {
4578 range = TRUE;
4579 *arg = skipwhite(*arg + 1);
4580 if (**arg == ']')
4581 empty2 = TRUE;
4582 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 if (!empty1)
4585 clear_tv(&var1);
4586 return FAIL;
4587 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004588 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004589 {
4590 /* not a number or string */
4591 if (!empty1)
4592 clear_tv(&var1);
4593 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 return FAIL;
4595 }
4596 }
4597
4598 /* Check for the ']'. */
4599 if (**arg != ']')
4600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004601 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004602 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 clear_tv(&var1);
4604 if (range)
4605 clear_tv(&var2);
4606 return FAIL;
4607 }
4608 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 }
4610
4611 if (evaluate)
4612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 n1 = 0;
4614 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004616 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 }
4619 if (range)
4620 {
4621 if (empty2)
4622 n2 = -1;
4623 else
4624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004625 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004627 }
4628 }
4629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004632 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004633 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004634 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004635 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004636 case VAR_SPECIAL:
4637 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004638 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004639 break; /* not evaluating, skipping over subscript */
4640
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 case VAR_NUMBER:
4642 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 len = (long)STRLEN(s);
4645 if (range)
4646 {
4647 /* The resulting variable is a substring. If the indexes
4648 * are out of range the result is empty. */
4649 if (n1 < 0)
4650 {
4651 n1 = len + n1;
4652 if (n1 < 0)
4653 n1 = 0;
4654 }
4655 if (n2 < 0)
4656 n2 = len + n2;
4657 else if (n2 >= len)
4658 n2 = len;
4659 if (n1 >= len || n2 < 0 || n1 > n2)
4660 s = NULL;
4661 else
4662 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4663 }
4664 else
4665 {
4666 /* The resulting variable is a string of a single
4667 * character. If the index is too big or negative the
4668 * result is empty. */
4669 if (n1 >= len || n1 < 0)
4670 s = NULL;
4671 else
4672 s = vim_strnsave(s + n1, 1);
4673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 rettv->v_type = VAR_STRING;
4676 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 break;
4678
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004679 case VAR_BLOB:
4680 len = blob_len(rettv->vval.v_blob);
4681 if (range)
4682 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004683 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004684 // are out of range the result is empty.
4685 if (n1 < 0)
4686 {
4687 n1 = len + n1;
4688 if (n1 < 0)
4689 n1 = 0;
4690 }
4691 if (n2 < 0)
4692 n2 = len + n2;
4693 else if (n2 >= len)
4694 n2 = len - 1;
4695 if (n1 >= len || n2 < 0 || n1 > n2)
4696 {
4697 clear_tv(rettv);
4698 rettv->v_type = VAR_BLOB;
4699 rettv->vval.v_blob = NULL;
4700 }
4701 else
4702 {
4703 blob_T *blob = blob_alloc();
4704
4705 if (blob != NULL)
4706 {
4707 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4708 {
4709 blob_free(blob);
4710 return FAIL;
4711 }
4712 blob->bv_ga.ga_len = n2 - n1 + 1;
4713 for (i = n1; i <= n2; i++)
4714 blob_set(blob, i - n1,
4715 blob_get(rettv->vval.v_blob, i));
4716
4717 clear_tv(rettv);
4718 rettv_blob_set(rettv, blob);
4719 }
4720 }
4721 }
4722 else
4723 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004724 // The resulting variable is a byte value.
4725 // If the index is too big or negative that is an error.
4726 if (n1 < 0)
4727 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004728 if (n1 < len && n1 >= 0)
4729 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004730 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004731
4732 clear_tv(rettv);
4733 rettv->v_type = VAR_NUMBER;
4734 rettv->vval.v_number = v;
4735 }
4736 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004737 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004738 }
4739 break;
4740
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 if (n1 < 0)
4744 n1 = len + n1;
4745 if (!empty1 && (n1 < 0 || n1 >= len))
4746 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004747 /* For a range we allow invalid values and return an empty
4748 * list. A list index out of range is an error. */
4749 if (!range)
4750 {
4751 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004752 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004753 return FAIL;
4754 }
4755 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 }
4757 if (range)
4758 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004759 list_T *l;
4760 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761
4762 if (n2 < 0)
4763 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004764 else if (n2 >= len)
4765 n2 = len - 1;
4766 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004767 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 l = list_alloc();
4769 if (l == NULL)
4770 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 n1 <= n2; ++n1)
4773 {
4774 if (list_append_tv(l, &item->li_tv) == FAIL)
4775 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004776 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 return FAIL;
4778 }
4779 item = item->li_next;
4780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004782 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 }
4784 else
4785 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004786 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 clear_tv(rettv);
4788 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 }
4790 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791
4792 case VAR_DICT:
4793 if (range)
4794 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004795 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004796 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 if (len == -1)
4798 clear_tv(&var1);
4799 return FAIL;
4800 }
4801 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004802 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803
4804 if (len == -1)
4805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004806 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004807 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 clear_tv(&var1);
4810 return FAIL;
4811 }
4812 }
4813
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004814 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004815
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004816 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004817 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818 if (len == -1)
4819 clear_tv(&var1);
4820 if (item == NULL)
4821 return FAIL;
4822
4823 copy_tv(&item->di_tv, &var1);
4824 clear_tv(rettv);
4825 *rettv = var1;
4826 }
4827 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 }
4829 }
4830
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 return OK;
4832}
4833
4834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 * Get an option value.
4836 * "arg" points to the '&' or '+' before the option name.
4837 * "arg" is advanced to character after the option name.
4838 * Return OK or FAIL.
4839 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004841get_option_tv(
4842 char_u **arg,
4843 typval_T *rettv, /* when NULL, only check if option exists */
4844 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
4846 char_u *option_end;
4847 long numval;
4848 char_u *stringval;
4849 int opt_type;
4850 int c;
4851 int working = (**arg == '+'); /* has("+option") */
4852 int ret = OK;
4853 int opt_flags;
4854
4855 /*
4856 * Isolate the option name and find its value.
4857 */
4858 option_end = find_option_end(arg, &opt_flags);
4859 if (option_end == NULL)
4860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004861 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004862 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 return FAIL;
4864 }
4865
4866 if (!evaluate)
4867 {
4868 *arg = option_end;
4869 return OK;
4870 }
4871
4872 c = *option_end;
4873 *option_end = NUL;
4874 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 if (opt_type == -3) /* invalid name */
4878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 ret = FAIL;
4882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
4885 if (opt_type == -2) /* hidden string option */
4886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv->v_type = VAR_STRING;
4888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 else if (opt_type == -1) /* hidden number option */
4891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 rettv->v_type = VAR_NUMBER;
4893 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895 else if (opt_type == 1) /* number option */
4896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 rettv->v_type = VAR_NUMBER;
4898 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 else /* string option */
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 rettv->v_type = VAR_STRING;
4903 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 }
4906 else if (working && (opt_type == -2 || opt_type == -1))
4907 ret = FAIL;
4908
4909 *option_end = c; /* put back for error messages */
4910 *arg = option_end;
4911
4912 return ret;
4913}
4914
4915/*
4916 * Allocate a variable for a string constant.
4917 * Return OK or FAIL.
4918 */
4919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004920get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 char_u *p;
4923 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 int extra = 0;
4925
4926 /*
4927 * Find the end of the string, skipping backslashed characters.
4928 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004929 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
4931 if (*p == '\\' && p[1] != NUL)
4932 {
4933 ++p;
4934 /* A "\<x>" form occupies at least 4 characters, and produces up
4935 * to 6 characters: reserve space for 2 extra */
4936 if (*p == '<')
4937 extra += 2;
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940
4941 if (*p != '"')
4942 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004943 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 return FAIL;
4945 }
4946
4947 /* If only parsing, set *arg and return here */
4948 if (!evaluate)
4949 {
4950 *arg = p + 1;
4951 return OK;
4952 }
4953
4954 /*
4955 * Copy the string into allocated memory, handling backslashed
4956 * characters.
4957 */
4958 name = alloc((unsigned)(p - *arg + extra));
4959 if (name == NULL)
4960 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 rettv->v_type = VAR_STRING;
4962 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 if (*p == '\\')
4967 {
4968 switch (*++p)
4969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 case 'b': *name++ = BS; ++p; break;
4971 case 'e': *name++ = ESC; ++p; break;
4972 case 'f': *name++ = FF; ++p; break;
4973 case 'n': *name++ = NL; ++p; break;
4974 case 'r': *name++ = CAR; ++p; break;
4975 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 case 'X': /* hex: "\x1", "\x12" */
4978 case 'x':
4979 case 'u': /* Unicode: "\u0023" */
4980 case 'U':
4981 if (vim_isxdigit(p[1]))
4982 {
4983 int n, nr;
4984 int c = toupper(*p);
4985
4986 if (c == 'X')
4987 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004988 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004990 else
4991 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 nr = 0;
4993 while (--n >= 0 && vim_isxdigit(p[1]))
4994 {
4995 ++p;
4996 nr = (nr << 4) + hex2nr(*p);
4997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 /* For "\u" store the number according to
5000 * 'encoding'. */
5001 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007
5008 /* octal: "\1", "\12", "\123" */
5009 case '0':
5010 case '1':
5011 case '2':
5012 case '3':
5013 case '4':
5014 case '5':
5015 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 case '7': *name = *p++ - '0';
5017 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 *name = (*name << 3) + *p++ - '0';
5020 if (*p >= '0' && *p <= '7')
5021 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 break;
5025
5026 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005027 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (extra != 0)
5029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032 }
5033 /* FALLTHROUGH */
5034
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 break;
5037 }
5038 }
5039 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005044 if (*p != NUL) /* just in case */
5045 ++p;
5046 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 return OK;
5049}
5050
5051/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 * Return OK or FAIL.
5054 */
5055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005056get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 int reduce = 0;
5061
5062 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005065 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 break;
5071 ++reduce;
5072 ++p;
5073 }
5074 }
5075
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005078 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 return FAIL;
5080 }
5081
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 if (!evaluate)
5084 {
5085 *arg = p + 1;
5086 return OK;
5087 }
5088
5089 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 */
5092 str = alloc((unsigned)((p - *arg) - reduce));
5093 if (str == NULL)
5094 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 rettv->v_type = VAR_STRING;
5096 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 break;
5104 ++p;
5105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 *arg = p + 1;
5110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 return OK;
5112}
5113
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005114/*
5115 * Return the function name of the partial.
5116 */
5117 char_u *
5118partial_name(partial_T *pt)
5119{
5120 if (pt->pt_name != NULL)
5121 return pt->pt_name;
5122 return pt->pt_func->uf_name;
5123}
5124
Bram Moolenaarddecc252016-04-06 22:59:37 +02005125 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005126partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005127{
5128 int i;
5129
5130 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005131 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005132 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005133 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005134 if (pt->pt_name != NULL)
5135 {
5136 func_unref(pt->pt_name);
5137 vim_free(pt->pt_name);
5138 }
5139 else
5140 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005141 vim_free(pt);
5142}
5143
5144/*
5145 * Unreference a closure: decrement the reference count and free it when it
5146 * becomes zero.
5147 */
5148 void
5149partial_unref(partial_T *pt)
5150{
5151 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005152 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005153}
5154
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005155static int tv_equal_recurse_limit;
5156
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005157 static int
5158func_equal(
5159 typval_T *tv1,
5160 typval_T *tv2,
5161 int ic) /* ignore case */
5162{
5163 char_u *s1, *s2;
5164 dict_T *d1, *d2;
5165 int a1, a2;
5166 int i;
5167
5168 /* empty and NULL function name considered the same */
5169 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005170 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005171 if (s1 != NULL && *s1 == NUL)
5172 s1 = NULL;
5173 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005174 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005175 if (s2 != NULL && *s2 == NUL)
5176 s2 = NULL;
5177 if (s1 == NULL || s2 == NULL)
5178 {
5179 if (s1 != s2)
5180 return FALSE;
5181 }
5182 else if (STRCMP(s1, s2) != 0)
5183 return FALSE;
5184
5185 /* empty dict and NULL dict is different */
5186 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5187 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5188 if (d1 == NULL || d2 == NULL)
5189 {
5190 if (d1 != d2)
5191 return FALSE;
5192 }
5193 else if (!dict_equal(d1, d2, ic, TRUE))
5194 return FALSE;
5195
5196 /* empty list and no list considered the same */
5197 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5198 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5199 if (a1 != a2)
5200 return FALSE;
5201 for (i = 0; i < a1; ++i)
5202 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5203 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5204 return FALSE;
5205
5206 return TRUE;
5207}
5208
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005209/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005210 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005211 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005213 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005215tv_equal(
5216 typval_T *tv1,
5217 typval_T *tv2,
5218 int ic, /* ignore case */
5219 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005220{
5221 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005223 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005224 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005225
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005226 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005227 * recursiveness to a limit. We guess they are equal then.
5228 * A fixed limit has the problem of still taking an awful long time.
5229 * Reduce the limit every time running into it. That should work fine for
5230 * deeply linked structures that are not recursively linked and catch
5231 * recursiveness quickly. */
5232 if (!recursive)
5233 tv_equal_recurse_limit = 1000;
5234 if (recursive_cnt >= tv_equal_recurse_limit)
5235 {
5236 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005237 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005238 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005239
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005240 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5241 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005242 if ((tv1->v_type == VAR_FUNC
5243 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5244 && (tv2->v_type == VAR_FUNC
5245 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5246 {
5247 ++recursive_cnt;
5248 r = func_equal(tv1, tv2, ic);
5249 --recursive_cnt;
5250 return r;
5251 }
5252
5253 if (tv1->v_type != tv2->v_type)
5254 return FALSE;
5255
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005256 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005257 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005258 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005259 ++recursive_cnt;
5260 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5261 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005262 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005263
5264 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005265 ++recursive_cnt;
5266 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5267 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005268 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005269
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005270 case VAR_BLOB:
5271 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5272
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005273 case VAR_NUMBER:
5274 return tv1->vval.v_number == tv2->vval.v_number;
5275
5276 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005277 s1 = tv_get_string_buf(tv1, buf1);
5278 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005279 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005280
5281 case VAR_SPECIAL:
5282 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005283
5284 case VAR_FLOAT:
5285#ifdef FEAT_FLOAT
5286 return tv1->vval.v_float == tv2->vval.v_float;
5287#endif
5288 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005289#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005290 return tv1->vval.v_job == tv2->vval.v_job;
5291#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005292 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005293#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005294 return tv1->vval.v_channel == tv2->vval.v_channel;
5295#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005296 case VAR_FUNC:
5297 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005298 case VAR_UNKNOWN:
5299 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005301
Bram Moolenaara03f2332016-02-06 18:09:59 +01005302 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5303 * does not equal anything, not even itself. */
5304 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305}
5306
5307/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005308 * Return the next (unique) copy ID.
5309 * Used for serializing nested structures.
5310 */
5311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005312get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005313{
5314 current_copyID += COPYID_INC;
5315 return current_copyID;
5316}
5317
5318/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005319 * Garbage collection for lists and dictionaries.
5320 *
5321 * We use reference counts to be able to free most items right away when they
5322 * are no longer used. But for composite items it's possible that it becomes
5323 * unused while the reference count is > 0: When there is a recursive
5324 * reference. Example:
5325 * :let l = [1, 2, 3]
5326 * :let d = {9: l}
5327 * :let l[1] = d
5328 *
5329 * Since this is quite unusual we handle this with garbage collection: every
5330 * once in a while find out which lists and dicts are not referenced from any
5331 * variable.
5332 *
5333 * Here is a good reference text about garbage collection (refers to Python
5334 * but it applies to all reference-counting mechanisms):
5335 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005336 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005337
5338/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005339 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005340 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005341 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005342 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005343 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005344garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005345{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005346 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005347 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005348 buf_T *buf;
5349 win_T *wp;
5350 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005351 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005352 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005353
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005354 if (!testing)
5355 {
5356 /* Only do this once. */
5357 want_garbage_collect = FALSE;
5358 may_garbage_collect = FALSE;
5359 garbage_collect_at_exit = FALSE;
5360 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005361
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005362 /* We advance by two because we add one for items referenced through
5363 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005364 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005365
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005366 /*
5367 * 1. Go through all accessible variables and mark all lists and dicts
5368 * with copyID.
5369 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005370
5371 /* Don't free variables in the previous_funccal list unless they are only
5372 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005373 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005375
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376 /* script-local variables */
5377 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005378 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005379
5380 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005381 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005382 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5383 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005384
5385 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005386 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005387 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5388 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005389 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005390 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5391 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005392
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005393 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005394 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005395 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5396 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005398 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005399
5400 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005402
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005403 /* named functions (matters for closures) */
5404 abort = abort || set_ref_in_functions(copyID);
5405
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005406 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005408
Bram Moolenaard812df62008-11-09 12:46:09 +00005409 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005411
Bram Moolenaar1dced572012-04-05 16:54:08 +02005412#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005413 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005414#endif
5415
Bram Moolenaardb913952012-06-29 12:54:53 +02005416#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005417 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005418#endif
5419
5420#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005422#endif
5423
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005425 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005426 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005427#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005428#ifdef FEAT_NETBEANS_INTG
5429 abort = abort || set_ref_in_nb_channel(copyID);
5430#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005431
Bram Moolenaare3188e22016-05-31 21:13:04 +02005432#ifdef FEAT_TIMERS
5433 abort = abort || set_ref_in_timer(copyID);
5434#endif
5435
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005436#ifdef FEAT_QUICKFIX
5437 abort = abort || set_ref_in_quickfix(copyID);
5438#endif
5439
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005440#ifdef FEAT_TERMINAL
5441 abort = abort || set_ref_in_term(copyID);
5442#endif
5443
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005444 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005445 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005446 /*
5447 * 2. Free lists and dictionaries that are not referenced.
5448 */
5449 did_free = free_unref_items(copyID);
5450
5451 /*
5452 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005453 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005454 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005455 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005456 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005457 else if (p_verbose > 0)
5458 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005459 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005461
5462 return did_free;
5463}
5464
5465/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005466 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005467 */
5468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005469free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005470{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005471 int did_free = FALSE;
5472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005473 /* Let all "free" functions know that we are here. This means no
5474 * dictionaries, lists, channels or jobs are to be freed, because we will
5475 * do that here. */
5476 in_free_unref_items = TRUE;
5477
5478 /*
5479 * PASS 1: free the contents of the items. We don't free the items
5480 * themselves yet, so that it is possible to decrement refcount counters
5481 */
5482
Bram Moolenaarcd524592016-07-17 14:57:05 +02005483 /* Go through the list of dicts and free items without the copyID. */
5484 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005485
Bram Moolenaarda861d62016-07-17 15:46:27 +02005486 /* Go through the list of lists and free items without the copyID. */
5487 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005488
5489#ifdef FEAT_JOB_CHANNEL
5490 /* Go through the list of jobs and free items without the copyID. This
5491 * must happen before doing channels, because jobs refer to channels, but
5492 * the reference from the channel to the job isn't tracked. */
5493 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5494
5495 /* Go through the list of channels and free items without the copyID. */
5496 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5497#endif
5498
5499 /*
5500 * PASS 2: free the items themselves.
5501 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005502 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005503 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005504
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005505#ifdef FEAT_JOB_CHANNEL
5506 /* Go through the list of jobs and free items without the copyID. This
5507 * must happen before doing channels, because jobs refer to channels, but
5508 * the reference from the channel to the job isn't tracked. */
5509 free_unused_jobs(copyID, COPYID_MASK);
5510
5511 /* Go through the list of channels and free items without the copyID. */
5512 free_unused_channels(copyID, COPYID_MASK);
5513#endif
5514
5515 in_free_unref_items = FALSE;
5516
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005517 return did_free;
5518}
5519
5520/*
5521 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005522 * "list_stack" is used to add lists to be marked. Can be NULL.
5523 *
5524 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005525 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005528{
5529 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532 hashtab_T *cur_ht;
5533 ht_stack_T *ht_stack = NULL;
5534 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005535
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005536 cur_ht = ht;
5537 for (;;)
5538 {
5539 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005540 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005541 /* Mark each item in the hashtab. If the item contains a hashtab
5542 * it is added to ht_stack, if it contains a list it is added to
5543 * list_stack. */
5544 todo = (int)cur_ht->ht_used;
5545 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5546 if (!HASHITEM_EMPTY(hi))
5547 {
5548 --todo;
5549 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5550 &ht_stack, list_stack);
5551 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005552 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005553
5554 if (ht_stack == NULL)
5555 break;
5556
5557 /* take an item from the stack */
5558 cur_ht = ht_stack->ht;
5559 tempitem = ht_stack;
5560 ht_stack = ht_stack->prev;
5561 free(tempitem);
5562 }
5563
5564 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005565}
5566
5567/*
5568 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005569 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5570 *
5571 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005572 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005574set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005575{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576 listitem_T *li;
5577 int abort = FALSE;
5578 list_T *cur_l;
5579 list_stack_T *list_stack = NULL;
5580 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005581
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 cur_l = l;
5583 for (;;)
5584 {
5585 if (!abort)
5586 /* Mark each item in the list. If the item contains a hashtab
5587 * it is added to ht_stack, if it contains a list it is added to
5588 * list_stack. */
5589 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5590 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5591 ht_stack, &list_stack);
5592 if (list_stack == NULL)
5593 break;
5594
5595 /* take an item from the stack */
5596 cur_l = list_stack->list;
5597 tempitem = list_stack;
5598 list_stack = list_stack->prev;
5599 free(tempitem);
5600 }
5601
5602 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005603}
5604
5605/*
5606 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005607 * "list_stack" is used to add lists to be marked. Can be NULL.
5608 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5609 *
5610 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005611 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005613set_ref_in_item(
5614 typval_T *tv,
5615 int copyID,
5616 ht_stack_T **ht_stack,
5617 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005618{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005619 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005620
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005621 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005622 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005623 dict_T *dd = tv->vval.v_dict;
5624
Bram Moolenaara03f2332016-02-06 18:09:59 +01005625 if (dd != NULL && dd->dv_copyID != copyID)
5626 {
5627 /* Didn't see this dict yet. */
5628 dd->dv_copyID = copyID;
5629 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005630 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005631 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5632 }
5633 else
5634 {
5635 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5636 if (newitem == NULL)
5637 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005638 else
5639 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005640 newitem->ht = &dd->dv_hashtab;
5641 newitem->prev = *ht_stack;
5642 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005643 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005644 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005645 }
5646 }
5647 else if (tv->v_type == VAR_LIST)
5648 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005649 list_T *ll = tv->vval.v_list;
5650
Bram Moolenaara03f2332016-02-06 18:09:59 +01005651 if (ll != NULL && ll->lv_copyID != copyID)
5652 {
5653 /* Didn't see this list yet. */
5654 ll->lv_copyID = copyID;
5655 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005656 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005657 abort = set_ref_in_list(ll, copyID, ht_stack);
5658 }
5659 else
5660 {
5661 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005662 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005663 if (newitem == NULL)
5664 abort = TRUE;
5665 else
5666 {
5667 newitem->list = ll;
5668 newitem->prev = *list_stack;
5669 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005670 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005671 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005672 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005673 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005674 else if (tv->v_type == VAR_FUNC)
5675 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005676 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005677 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005678 else if (tv->v_type == VAR_PARTIAL)
5679 {
5680 partial_T *pt = tv->vval.v_partial;
5681 int i;
5682
5683 /* A partial does not have a copyID, because it cannot contain itself.
5684 */
5685 if (pt != NULL)
5686 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005687 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005688
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005689 if (pt->pt_dict != NULL)
5690 {
5691 typval_T dtv;
5692
5693 dtv.v_type = VAR_DICT;
5694 dtv.vval.v_dict = pt->pt_dict;
5695 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5696 }
5697
5698 for (i = 0; i < pt->pt_argc; ++i)
5699 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5700 ht_stack, list_stack);
5701 }
5702 }
5703#ifdef FEAT_JOB_CHANNEL
5704 else if (tv->v_type == VAR_JOB)
5705 {
5706 job_T *job = tv->vval.v_job;
5707 typval_T dtv;
5708
5709 if (job != NULL && job->jv_copyID != copyID)
5710 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005711 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005712 if (job->jv_channel != NULL)
5713 {
5714 dtv.v_type = VAR_CHANNEL;
5715 dtv.vval.v_channel = job->jv_channel;
5716 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5717 }
5718 if (job->jv_exit_partial != NULL)
5719 {
5720 dtv.v_type = VAR_PARTIAL;
5721 dtv.vval.v_partial = job->jv_exit_partial;
5722 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5723 }
5724 }
5725 }
5726 else if (tv->v_type == VAR_CHANNEL)
5727 {
5728 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005729 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005730 typval_T dtv;
5731 jsonq_T *jq;
5732 cbq_T *cq;
5733
5734 if (ch != NULL && ch->ch_copyID != copyID)
5735 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005736 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005737 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005738 {
5739 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5740 jq = jq->jq_next)
5741 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5742 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5743 cq = cq->cq_next)
5744 if (cq->cq_partial != NULL)
5745 {
5746 dtv.v_type = VAR_PARTIAL;
5747 dtv.vval.v_partial = cq->cq_partial;
5748 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5749 }
5750 if (ch->ch_part[part].ch_partial != NULL)
5751 {
5752 dtv.v_type = VAR_PARTIAL;
5753 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5754 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5755 }
5756 }
5757 if (ch->ch_partial != NULL)
5758 {
5759 dtv.v_type = VAR_PARTIAL;
5760 dtv.vval.v_partial = ch->ch_partial;
5761 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5762 }
5763 if (ch->ch_close_partial != NULL)
5764 {
5765 dtv.v_type = VAR_PARTIAL;
5766 dtv.vval.v_partial = ch->ch_close_partial;
5767 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5768 }
5769 }
5770 }
5771#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005772 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005773}
5774
Bram Moolenaar17a13432016-01-24 14:22:10 +01005775 static char *
5776get_var_special_name(int nr)
5777{
5778 switch (nr)
5779 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005780 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005781 case VVAL_TRUE: return "v:true";
5782 case VVAL_NONE: return "v:none";
5783 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005784 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005785 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005786 return "42";
5787}
5788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 * Return a string with the string representation of a variable.
5791 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005793 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005794 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5795 * stings as "string()", otherwise does not put quotes around strings, as
5796 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005797 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5798 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005799 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005801 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005802echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005803 typval_T *tv,
5804 char_u **tofree,
5805 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005806 int copyID,
5807 int echo_style,
5808 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005809 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005811 static int recurse = 0;
5812 char_u *r = NULL;
5813
Bram Moolenaar33570922005-01-25 22:26:29 +00005814 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005815 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005816 if (!did_echo_string_emsg)
5817 {
5818 /* Only give this message once for a recursive call to avoid
5819 * flooding the user with errors. And stop iterating over lists
5820 * and dicts. */
5821 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005822 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005823 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005824 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005825 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005826 }
5827 ++recurse;
5828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 switch (tv->v_type)
5830 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005831 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005832 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005833 {
5834 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005835 r = tv->vval.v_string;
5836 if (r == NULL)
5837 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005838 }
5839 else
5840 {
5841 *tofree = string_quote(tv->vval.v_string, FALSE);
5842 r = *tofree;
5843 }
5844 break;
5845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005847 if (echo_style)
5848 {
5849 *tofree = NULL;
5850 r = tv->vval.v_string;
5851 }
5852 else
5853 {
5854 *tofree = string_quote(tv->vval.v_string, TRUE);
5855 r = *tofree;
5856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005858
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005859 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005860 {
5861 partial_T *pt = tv->vval.v_partial;
5862 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005863 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005864 garray_T ga;
5865 int i;
5866 char_u *tf;
5867
5868 ga_init2(&ga, 1, 100);
5869 ga_concat(&ga, (char_u *)"function(");
5870 if (fname != NULL)
5871 {
5872 ga_concat(&ga, fname);
5873 vim_free(fname);
5874 }
5875 if (pt != NULL && pt->pt_argc > 0)
5876 {
5877 ga_concat(&ga, (char_u *)", [");
5878 for (i = 0; i < pt->pt_argc; ++i)
5879 {
5880 if (i > 0)
5881 ga_concat(&ga, (char_u *)", ");
5882 ga_concat(&ga,
5883 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5884 vim_free(tf);
5885 }
5886 ga_concat(&ga, (char_u *)"]");
5887 }
5888 if (pt != NULL && pt->pt_dict != NULL)
5889 {
5890 typval_T dtv;
5891
5892 ga_concat(&ga, (char_u *)", ");
5893 dtv.v_type = VAR_DICT;
5894 dtv.vval.v_dict = pt->pt_dict;
5895 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5896 vim_free(tf);
5897 }
5898 ga_concat(&ga, (char_u *)")");
5899
5900 *tofree = ga.ga_data;
5901 r = *tofree;
5902 break;
5903 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005904
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005905 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005906 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005907 break;
5908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005910 if (tv->vval.v_list == NULL)
5911 {
5912 *tofree = NULL;
5913 r = NULL;
5914 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005915 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5916 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005917 {
5918 *tofree = NULL;
5919 r = (char_u *)"[...]";
5920 }
5921 else
5922 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005923 int old_copyID = tv->vval.v_list->lv_copyID;
5924
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005925 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005926 *tofree = list2string(tv, copyID, restore_copyID);
5927 if (restore_copyID)
5928 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005929 r = *tofree;
5930 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005932
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005934 if (tv->vval.v_dict == NULL)
5935 {
5936 *tofree = NULL;
5937 r = NULL;
5938 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005939 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5940 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005941 {
5942 *tofree = NULL;
5943 r = (char_u *)"{...}";
5944 }
5945 else
5946 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005947 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005948 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005949 *tofree = dict2string(tv, copyID, restore_copyID);
5950 if (restore_copyID)
5951 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005952 r = *tofree;
5953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005954 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005957 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005958 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005959 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005960 break;
5961
Bram Moolenaar835dc632016-02-07 14:27:38 +01005962 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005963 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005964 *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 if (composite_val)
5967 {
5968 *tofree = string_quote(r, FALSE);
5969 r = *tofree;
5970 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005973 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005974#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005975 *tofree = NULL;
5976 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5977 r = numbuf;
5978 break;
5979#endif
5980
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005981 case VAR_SPECIAL:
5982 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005983 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005984 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005986
Bram Moolenaar8502c702014-06-17 12:51:16 +02005987 if (--recurse == 0)
5988 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990}
5991
5992/*
5993 * Return a string with the string representation of a variable.
5994 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5995 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005996 * Does not put quotes around strings, as ":echo" displays values.
5997 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5998 * May return NULL.
5999 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006000 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006001echo_string(
6002 typval_T *tv,
6003 char_u **tofree,
6004 char_u *numbuf,
6005 int copyID)
6006{
6007 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6008}
6009
6010/*
6011 * Return a string with the string representation of a variable.
6012 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6013 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006014 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006015 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006016 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006017 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018tv2string(
6019 typval_T *tv,
6020 char_u **tofree,
6021 char_u *numbuf,
6022 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006023{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006024 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025}
6026
6027/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 * Return string "str" in ' quotes, doubling ' characters.
6029 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006032 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 char_u *p, *r, *s;
6037
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 len = (function ? 13 : 3);
6039 if (str != NULL)
6040 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006041 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006042 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 if (*p == '\'')
6044 ++len;
6045 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006046 s = r = alloc(len);
6047 if (r != NULL)
6048 {
6049 if (function)
6050 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006051 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006052 r += 10;
6053 }
6054 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006055 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 if (str != NULL)
6057 for (p = str; *p != NUL; )
6058 {
6059 if (*p == '\'')
6060 *r++ = '\'';
6061 MB_COPY_CHAR(p, r);
6062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006063 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064 if (function)
6065 *r++ = ')';
6066 *r++ = NUL;
6067 }
6068 return s;
6069}
6070
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006071#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006072/*
6073 * Convert the string "text" to a floating point number.
6074 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6075 * this always uses a decimal point.
6076 * Returns the length of the text that was consumed.
6077 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006079string2float(
6080 char_u *text,
6081 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006082{
6083 char *s = (char *)text;
6084 float_T f;
6085
Bram Moolenaar62473612017-01-08 19:25:40 +01006086 /* MS-Windows does not deal with "inf" and "nan" properly. */
6087 if (STRNICMP(text, "inf", 3) == 0)
6088 {
6089 *value = INFINITY;
6090 return 3;
6091 }
6092 if (STRNICMP(text, "-inf", 3) == 0)
6093 {
6094 *value = -INFINITY;
6095 return 4;
6096 }
6097 if (STRNICMP(text, "nan", 3) == 0)
6098 {
6099 *value = NAN;
6100 return 3;
6101 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006102 f = strtod(s, &s);
6103 *value = f;
6104 return (int)((char_u *)s - text);
6105}
6106#endif
6107
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 * Get the value of an environment variable.
6110 * "arg" is pointing to the '$'. It is advanced to after the name.
6111 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006112 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 */
6114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006115get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 char_u *string = NULL;
6118 int len;
6119 int cc;
6120 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006121 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123 ++*arg;
6124 name = *arg;
6125 len = get_env_len(arg);
6126 if (evaluate)
6127 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006128 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006129 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006130
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006131 cc = name[len];
6132 name[len] = NUL;
6133 /* first try vim_getenv(), fast for normal environment vars */
6134 string = vim_getenv(name, &mustfree);
6135 if (string != NULL && *string != NUL)
6136 {
6137 if (!mustfree)
6138 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006140 else
6141 {
6142 if (mustfree)
6143 vim_free(string);
6144
6145 /* next try expanding things like $VIM and ${HOME} */
6146 string = expand_env_save(name - 1);
6147 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006148 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006149 }
6150 name[len] = cc;
6151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006152 rettv->v_type = VAR_STRING;
6153 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
6155
6156 return OK;
6157}
6158
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006159
6160
6161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006163 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166var2fpos(
6167 typval_T *varp,
6168 int dollar_lnum, /* TRUE when $ is last line */
6169 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006171 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006173 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
Bram Moolenaara5525202006-03-02 22:52:09 +00006175 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006176 if (varp->v_type == VAR_LIST)
6177 {
6178 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006179 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006180 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006181 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006182
6183 l = varp->vval.v_list;
6184 if (l == NULL)
6185 return NULL;
6186
6187 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006188 pos.lnum = list_find_nr(l, 0L, &error);
6189 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006190 return NULL; /* invalid line number */
6191
6192 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006193 pos.col = list_find_nr(l, 1L, &error);
6194 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006195 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006197
6198 /* We accept "$" for the column number: last column. */
6199 li = list_find(l, 1L);
6200 if (li != NULL && li->li_tv.v_type == VAR_STRING
6201 && li->li_tv.vval.v_string != NULL
6202 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6203 pos.col = len + 1;
6204
Bram Moolenaara5525202006-03-02 22:52:09 +00006205 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006206 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006207 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006208 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006209
Bram Moolenaara5525202006-03-02 22:52:09 +00006210#ifdef FEAT_VIRTUALEDIT
6211 /* Get the virtual offset. Defaults to zero. */
6212 pos.coladd = list_find_nr(l, 2L, &error);
6213 if (error)
6214 pos.coladd = 0;
6215#endif
6216
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006217 return &pos;
6218 }
6219
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006220 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006221 if (name == NULL)
6222 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006223 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006225 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6226 {
6227 if (VIsual_active)
6228 return &VIsual;
6229 return &curwin->w_cursor;
6230 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006231 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006233 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6235 return NULL;
6236 return pp;
6237 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006238
6239#ifdef FEAT_VIRTUALEDIT
6240 pos.coladd = 0;
6241#endif
6242
Bram Moolenaar477933c2007-07-17 14:32:23 +00006243 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006244 {
6245 pos.col = 0;
6246 if (name[1] == '0') /* "w0": first visible line */
6247 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006248 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006249 /* In silent Ex mode topline is zero, but that's not a valid line
6250 * number; use one instead. */
6251 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006252 return &pos;
6253 }
6254 else if (name[1] == '$') /* "w$": last visible line */
6255 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006256 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006257 /* In silent Ex mode botline is zero, return zero then. */
6258 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006259 return &pos;
6260 }
6261 }
6262 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006264 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
6266 pos.lnum = curbuf->b_ml.ml_line_count;
6267 pos.col = 0;
6268 }
6269 else
6270 {
6271 pos.lnum = curwin->w_cursor.lnum;
6272 pos.col = (colnr_T)STRLEN(ml_get_curline());
6273 }
6274 return &pos;
6275 }
6276 return NULL;
6277}
6278
6279/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006280 * Convert list in "arg" into a position and optional file number.
6281 * When "fnump" is NULL there is no file number, only 3 items.
6282 * Note that the column is passed on as-is, the caller may want to decrement
6283 * it to use 1 for the first column.
6284 * Return FAIL when conversion is not possible, doesn't check the position for
6285 * validity.
6286 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288list2fpos(
6289 typval_T *arg,
6290 pos_T *posp,
6291 int *fnump,
6292 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006293{
6294 list_T *l = arg->vval.v_list;
6295 long i = 0;
6296 long n;
6297
Bram Moolenaar493c1782014-05-28 14:34:46 +02006298 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6299 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006300 if (arg->v_type != VAR_LIST
6301 || l == NULL
6302 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006303 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006304 return FAIL;
6305
6306 if (fnump != NULL)
6307 {
6308 n = list_find_nr(l, i++, NULL); /* fnum */
6309 if (n < 0)
6310 return FAIL;
6311 if (n == 0)
6312 n = curbuf->b_fnum; /* current buffer */
6313 *fnump = n;
6314 }
6315
6316 n = list_find_nr(l, i++, NULL); /* lnum */
6317 if (n < 0)
6318 return FAIL;
6319 posp->lnum = n;
6320
6321 n = list_find_nr(l, i++, NULL); /* col */
6322 if (n < 0)
6323 return FAIL;
6324 posp->col = n;
6325
6326#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006327 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006328 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006329 posp->coladd = 0;
6330 else
6331 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006332#endif
6333
Bram Moolenaar493c1782014-05-28 14:34:46 +02006334 if (curswantp != NULL)
6335 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6336
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006337 return OK;
6338}
6339
6340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 * Get the length of an environment variable name.
6342 * Advance "arg" to the first character after the name.
6343 * Return 0 for error.
6344 */
6345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 char_u *p;
6349 int len;
6350
6351 for (p = *arg; vim_isIDc(*p); ++p)
6352 ;
6353 if (p == *arg) /* no name found */
6354 return 0;
6355
6356 len = (int)(p - *arg);
6357 *arg = p;
6358 return len;
6359}
6360
6361/*
6362 * Get the length of the name of a function or internal variable.
6363 * "arg" is advanced to the first non-white character after the name.
6364 * Return 0 if something is wrong.
6365 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006367get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 char_u *p;
6370 int len;
6371
6372 /* Find the end of the name. */
6373 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006374 {
6375 if (*p == ':')
6376 {
6377 /* "s:" is start of "s:var", but "n:" is not and can be used in
6378 * slice "[n:]". Also "xx:" is not a namespace. */
6379 len = (int)(p - *arg);
6380 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6381 || len > 1)
6382 break;
6383 }
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (p == *arg) /* no name found */
6386 return 0;
6387
6388 len = (int)(p - *arg);
6389 *arg = skipwhite(p);
6390
6391 return len;
6392}
6393
6394/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006395 * Get the length of the name of a variable or function.
6396 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006398 * Return -1 if curly braces expansion failed.
6399 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 * If the name contains 'magic' {}'s, expand them and return the
6401 * expanded name in an allocated string via 'alias' - caller must free.
6402 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006404get_name_len(
6405 char_u **arg,
6406 char_u **alias,
6407 int evaluate,
6408 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 char_u *p;
6412 char_u *expr_start;
6413 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 *alias = NULL; /* default to no alias */
6416
6417 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6418 && (*arg)[2] == (int)KE_SNR)
6419 {
6420 /* hard coded <SNR>, already translated */
6421 *arg += 3;
6422 return get_id_len(arg) + 3;
6423 }
6424 len = eval_fname_script(*arg);
6425 if (len > 0)
6426 {
6427 /* literal "<SID>", "s:" or "<SNR>" */
6428 *arg += len;
6429 }
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006432 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006434 p = find_name_end(*arg, &expr_start, &expr_end,
6435 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (expr_start != NULL)
6437 {
6438 char_u *temp_string;
6439
6440 if (!evaluate)
6441 {
6442 len += (int)(p - *arg);
6443 *arg = skipwhite(p);
6444 return len;
6445 }
6446
6447 /*
6448 * Include any <SID> etc in the expanded string:
6449 * Thus the -len here.
6450 */
6451 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6452 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006453 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 *alias = temp_string;
6455 *arg = skipwhite(p);
6456 return (int)STRLEN(temp_string);
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006460 // Only give an error when there is something, otherwise it will be
6461 // reported at a higher level.
6462 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006463 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 return len;
6466}
6467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006468/*
6469 * Find the end of a variable or function name, taking care of magic braces.
6470 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6471 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006472 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006473 * Return a pointer to just after the name. Equal to "arg" if there is no
6474 * valid name.
6475 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006477find_name_end(
6478 char_u *arg,
6479 char_u **expr_start,
6480 char_u **expr_end,
6481 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006483 int mb_nest = 0;
6484 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006486 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006488 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490 *expr_start = NULL;
6491 *expr_end = NULL;
6492 }
6493
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006494 /* Quick check for valid starting character. */
6495 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6496 return arg;
6497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006498 for (p = arg; *p != NUL
6499 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006500 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006501 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006502 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006503 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006505 if (*p == '\'')
6506 {
6507 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006508 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006509 ;
6510 if (*p == NUL)
6511 break;
6512 }
6513 else if (*p == '"')
6514 {
6515 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006516 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006517 if (*p == '\\' && p[1] != NUL)
6518 ++p;
6519 if (*p == NUL)
6520 break;
6521 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006522 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6523 {
6524 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006525 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006526 len = (int)(p - arg);
6527 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006528 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006529 break;
6530 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006534 if (*p == '[')
6535 ++br_nest;
6536 else if (*p == ']')
6537 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006540 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006542 if (*p == '{')
6543 {
6544 mb_nest++;
6545 if (expr_start != NULL && *expr_start == NULL)
6546 *expr_start = p;
6547 }
6548 else if (*p == '}')
6549 {
6550 mb_nest--;
6551 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6552 *expr_end = p;
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556
6557 return p;
6558}
6559
6560/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006561 * Expands out the 'magic' {}'s in a variable/function name.
6562 * Note that this can call itself recursively, to deal with
6563 * constructs like foo{bar}{baz}{bam}
6564 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6565 * "in_start" ^
6566 * "expr_start" ^
6567 * "expr_end" ^
6568 * "in_end" ^
6569 *
6570 * Returns a new allocated string, which the caller must free.
6571 * Returns NULL for failure.
6572 */
6573 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006574make_expanded_name(
6575 char_u *in_start,
6576 char_u *expr_start,
6577 char_u *expr_end,
6578 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006579{
6580 char_u c1;
6581 char_u *retval = NULL;
6582 char_u *temp_result;
6583 char_u *nextcmd = NULL;
6584
6585 if (expr_end == NULL || in_end == NULL)
6586 return NULL;
6587 *expr_start = NUL;
6588 *expr_end = NUL;
6589 c1 = *in_end;
6590 *in_end = NUL;
6591
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006592 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006593 if (temp_result != NULL && nextcmd == NULL)
6594 {
6595 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6596 + (in_end - expr_end) + 1));
6597 if (retval != NULL)
6598 {
6599 STRCPY(retval, in_start);
6600 STRCAT(retval, temp_result);
6601 STRCAT(retval, expr_end + 1);
6602 }
6603 }
6604 vim_free(temp_result);
6605
6606 *in_end = c1; /* put char back for error messages */
6607 *expr_start = '{';
6608 *expr_end = '}';
6609
6610 if (retval != NULL)
6611 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006612 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006613 if (expr_start != NULL)
6614 {
6615 /* Further expansion! */
6616 temp_result = make_expanded_name(retval, expr_start,
6617 expr_end, temp_result);
6618 vim_free(retval);
6619 retval = temp_result;
6620 }
6621 }
6622
6623 return retval;
6624}
6625
6626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006633 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6634}
6635
6636/*
6637 * Return TRUE if character "c" can be used as the first character in a
6638 * variable or function name (excluding '{' and '}').
6639 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006641eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006642{
6643 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
6646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 * Set number v: variable to "val".
6648 */
6649 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006650set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653}
6654
6655/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006656 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006658 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006659get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006661 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
6663
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006664/*
6665 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006666 * If the String variable has never been set, return an empty string.
6667 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006668 */
6669 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006671{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006672 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006673}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006676 * Get List v: variable value. Caller must take care of reference count when
6677 * needed.
6678 */
6679 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006680get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006681{
6682 return vimvars[idx].vv_list;
6683}
6684
6685/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006686 * Get Dict v: variable value. Caller must take care of reference count when
6687 * needed.
6688 */
6689 dict_T *
6690get_vim_var_dict(int idx)
6691{
6692 return vimvars[idx].vv_dict;
6693}
6694
6695/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006696 * Set v:char to character "c".
6697 */
6698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006699set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006700{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006701 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006702
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006703 if (has_mbyte)
6704 buf[(*mb_char2bytes)(c, buf)] = NUL;
6705 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006706 {
6707 buf[0] = c;
6708 buf[1] = NUL;
6709 }
6710 set_vim_var_string(VV_CHAR, buf, -1);
6711}
6712
6713/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006714 * Set v:count to "count" and v:count1 to "count1".
6715 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 */
6717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006718set_vcount(
6719 long count,
6720 long count1,
6721 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006723 if (set_prevcount)
6724 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 vimvars[VV_COUNT].vv_nr = count;
6726 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727}
6728
6729/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006730 * Save variables that might be changed as a side effect. Used when executing
6731 * a timer callback.
6732 */
6733 void
6734save_vimvars(vimvars_save_T *vvsave)
6735{
6736 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6737 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6738 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6739}
6740
6741/*
6742 * Restore variables saved by save_vimvars().
6743 */
6744 void
6745restore_vimvars(vimvars_save_T *vvsave)
6746{
6747 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6748 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6749 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6750}
6751
6752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 * Set string v: variable to a copy of "val".
6754 */
6755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006756set_vim_var_string(
6757 int idx,
6758 char_u *val,
6759 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
Bram Moolenaara542c682016-01-31 16:28:04 +01006761 clear_tv(&vimvars[idx].vv_di.di_tv);
6762 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006764 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006768 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769}
6770
6771/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006772 * Set List v: variable to "val".
6773 */
6774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006775set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006776{
Bram Moolenaara542c682016-01-31 16:28:04 +01006777 clear_tv(&vimvars[idx].vv_di.di_tv);
6778 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006779 vimvars[idx].vv_list = val;
6780 if (val != NULL)
6781 ++val->lv_refcount;
6782}
6783
6784/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006785 * Set Dictionary v: variable to "val".
6786 */
6787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006788set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006789{
Bram Moolenaara542c682016-01-31 16:28:04 +01006790 clear_tv(&vimvars[idx].vv_di.di_tv);
6791 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006792 vimvars[idx].vv_dict = val;
6793 if (val != NULL)
6794 {
6795 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006796 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006797 }
6798}
6799
6800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * Set v:register if needed.
6802 */
6803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 char_u regname;
6807
6808 if (c == 0 || c == ' ')
6809 regname = '"';
6810 else
6811 regname = c;
6812 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 set_vim_var_string(VV_REG, &regname, 1);
6815}
6816
6817/*
6818 * Get or set v:exception. If "oldval" == NULL, return the current value.
6819 * Otherwise, restore the value to "oldval" and return NULL.
6820 * Must always be called in pairs to save and restore v:exception! Does not
6821 * take care of memory allocations.
6822 */
6823 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006827 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828
Bram Moolenaare9a41262005-01-15 22:18:47 +00006829 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 return NULL;
6831}
6832
6833/*
6834 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6835 * Otherwise, restore the value to "oldval" and return NULL.
6836 * Must always be called in pairs to save and restore v:throwpoint! Does not
6837 * take care of memory allocations.
6838 */
6839 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 return NULL;
6847}
6848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849/*
6850 * Set v:cmdarg.
6851 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6852 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6853 * Must always be called in pairs!
6854 */
6855 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 char_u *oldval;
6859 char_u *newval;
6860 unsigned len;
6861
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006863 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006865 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006866 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006867 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006870 if (eap->force_bin == FORCE_BIN)
6871 len = 6;
6872 else if (eap->force_bin == FORCE_NOBIN)
6873 len = 8;
6874 else
6875 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006876
6877 if (eap->read_edit)
6878 len += 7;
6879
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006880 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006881 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006882 if (eap->force_enc != 0)
6883 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006884 if (eap->bad_char != 0)
6885 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006886
6887 newval = alloc(len + 1);
6888 if (newval == NULL)
6889 return NULL;
6890
6891 if (eap->force_bin == FORCE_BIN)
6892 sprintf((char *)newval, " ++bin");
6893 else if (eap->force_bin == FORCE_NOBIN)
6894 sprintf((char *)newval, " ++nobin");
6895 else
6896 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897
6898 if (eap->read_edit)
6899 STRCAT(newval, " ++edit");
6900
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006901 if (eap->force_ff != 0)
6902 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006903 eap->force_ff == 'u' ? "unix"
6904 : eap->force_ff == 'd' ? "dos"
6905 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006906 if (eap->force_enc != 0)
6907 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6908 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006909 if (eap->bad_char == BAD_KEEP)
6910 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6911 else if (eap->bad_char == BAD_DROP)
6912 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6913 else if (eap->bad_char != 0)
6914 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006915 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006916 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
6919/*
6920 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006921 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006924get_var_tv(
6925 char_u *name,
6926 int len, /* length of "name" */
6927 typval_T *rettv, /* NULL when only checking existence */
6928 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6929 int verbose, /* may give error message */
6930 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006933 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006934 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937 /* truncate the name, so that we can use strcmp() */
6938 cc = name[len];
6939 name[len] = NUL;
6940
6941 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 * Check for user-defined variables.
6943 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006944 v = find_var(name, NULL, no_autoload);
6945 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006947 tv = &v->di_tv;
6948 if (dip != NULL)
6949 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
6951
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006954 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 ret = FAIL;
6957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006958 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006959 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 name[len] = cc;
6962
6963 return ret;
6964}
6965
6966/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006967 * Check if variable "name[len]" is a local variable or an argument.
6968 * If so, "*eval_lavars_used" is set to TRUE.
6969 */
6970 static void
6971check_vars(char_u *name, int len)
6972{
6973 int cc;
6974 char_u *varname;
6975 hashtab_T *ht;
6976
6977 if (eval_lavars_used == NULL)
6978 return;
6979
6980 /* truncate the name, so that we can use strcmp() */
6981 cc = name[len];
6982 name[len] = NUL;
6983
6984 ht = find_var_ht(name, &varname);
6985 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6986 {
6987 if (find_var(name, NULL, TRUE) != NULL)
6988 *eval_lavars_used = TRUE;
6989 }
6990
6991 name[len] = cc;
6992}
6993
6994/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006995 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6996 * Also handle function call with Funcref variable: func(expr)
6997 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6998 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007000handle_subscript(
7001 char_u **arg,
7002 typval_T *rettv,
7003 int evaluate, /* do more than finding the end */
7004 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007005{
7006 int ret = OK;
7007 dict_T *selfdict = NULL;
7008 char_u *s;
7009 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007010 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007011
7012 while (ret == OK
7013 && (**arg == '['
7014 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007015 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7016 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007017 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007018 {
7019 if (**arg == '(')
7020 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007021 partial_T *pt = NULL;
7022
Bram Moolenaard9fba312005-06-26 22:34:35 +00007023 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007024 if (evaluate)
7025 {
7026 functv = *rettv;
7027 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007028
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007029 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007030 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007031 {
7032 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007033 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007034 }
7035 else
7036 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007037 }
7038 else
7039 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007040 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007042 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007043
7044 /* Clear the funcref afterwards, so that deleting it while
7045 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007046 if (evaluate)
7047 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007048
7049 /* Stop the expression evaluation when immediately aborting on
7050 * error, or when an interrupt occurred or an exception was thrown
7051 * but not caught. */
7052 if (aborting())
7053 {
7054 if (ret == OK)
7055 clear_tv(rettv);
7056 ret = FAIL;
7057 }
7058 dict_unref(selfdict);
7059 selfdict = NULL;
7060 }
7061 else /* **arg == '[' || **arg == '.' */
7062 {
7063 dict_unref(selfdict);
7064 if (rettv->v_type == VAR_DICT)
7065 {
7066 selfdict = rettv->vval.v_dict;
7067 if (selfdict != NULL)
7068 ++selfdict->dv_refcount;
7069 }
7070 else
7071 selfdict = NULL;
7072 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7073 {
7074 clear_tv(rettv);
7075 ret = FAIL;
7076 }
7077 }
7078 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007079
Bram Moolenaar1d429612016-05-24 15:44:17 +02007080 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7081 * Don't do this when "Func" is already a partial that was bound
7082 * explicitly (pt_auto is FALSE). */
7083 if (selfdict != NULL
7084 && (rettv->v_type == VAR_FUNC
7085 || (rettv->v_type == VAR_PARTIAL
7086 && (rettv->vval.v_partial->pt_auto
7087 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007088 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007089
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007090 dict_unref(selfdict);
7091 return ret;
7092}
7093
7094/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007095 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007096 * value).
7097 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007098 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007099alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100{
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007102}
7103
7104/*
7105 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 * The string "s" must have been allocated, it is consumed.
7107 * Return NULL for out of memory, the variable otherwise.
7108 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007114 rettv = alloc_tv();
7115 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007117 rettv->v_type = VAR_STRING;
7118 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120 else
7121 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007122 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123}
7124
7125/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007126 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007129free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 if (varp != NULL)
7132 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007133 switch (varp->v_type)
7134 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007135 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007136 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007137 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007138 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007139 vim_free(varp->vval.v_string);
7140 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007141 case VAR_PARTIAL:
7142 partial_unref(varp->vval.v_partial);
7143 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007144 case VAR_BLOB:
7145 blob_unref(varp->vval.v_blob);
7146 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007147 case VAR_LIST:
7148 list_unref(varp->vval.v_list);
7149 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007150 case VAR_DICT:
7151 dict_unref(varp->vval.v_dict);
7152 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007153 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007154#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007155 job_unref(varp->vval.v_job);
7156 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007157#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007158 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007159#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007160 channel_unref(varp->vval.v_channel);
7161 break;
7162#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007163 case VAR_NUMBER:
7164 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007165 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007166 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007167 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 vim_free(varp);
7170 }
7171}
7172
7173/*
7174 * Free the memory for a variable value and set the value to NULL or 0.
7175 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007177clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 if (varp != NULL)
7180 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007181 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007183 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007184 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007185 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007186 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007187 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007188 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007189 case VAR_PARTIAL:
7190 partial_unref(varp->vval.v_partial);
7191 varp->vval.v_partial = NULL;
7192 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007193 case VAR_BLOB:
7194 blob_unref(varp->vval.v_blob);
7195 varp->vval.v_blob = NULL;
7196 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007197 case VAR_LIST:
7198 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007200 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 case VAR_DICT:
7202 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007205 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007206 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007207 varp->vval.v_number = 0;
7208 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007209 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007210#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007211 varp->vval.v_float = 0.0;
7212 break;
7213#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007214 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007215#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007216 job_unref(varp->vval.v_job);
7217 varp->vval.v_job = NULL;
7218#endif
7219 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007220 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007221#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007222 channel_unref(varp->vval.v_channel);
7223 varp->vval.v_channel = NULL;
7224#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225 case VAR_UNKNOWN:
7226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
7230}
7231
7232/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007233 * Set the value of a variable to NULL without freeing items.
7234 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007237{
7238 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007240}
7241
7242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 * Get the number value of a variable.
7244 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007245 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007246 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007247 * caller of incompatible types: it sets *denote to TRUE if "denote"
7248 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007250 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007251tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007253 int error = FALSE;
7254
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007255 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007256}
7257
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007258 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007259tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007260{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007261 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007263 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007265 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007266 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007267 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007268#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007269 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007270 break;
7271#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007273 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007274 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 break;
7276 case VAR_STRING:
7277 if (varp->vval.v_string != NULL)
7278 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007279 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007280 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007281 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007282 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007283 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007284 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007285 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007286 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007287 case VAR_SPECIAL:
7288 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7289 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007290 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007291#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007292 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007293 break;
7294#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007295 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007297 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007298 break;
7299#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007300 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007302 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007303 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007304 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007307 if (denote == NULL) /* useful for values that must be unsigned */
7308 n = -1;
7309 else
7310 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007311 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312}
7313
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007314#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007316tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007317{
7318 switch (varp->v_type)
7319 {
7320 case VAR_NUMBER:
7321 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007322 case VAR_FLOAT:
7323 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007324 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007325 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007327 break;
7328 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007329 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007330 break;
7331 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007332 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007333 break;
7334 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007336 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007337 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007338 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007339 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007340 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007341# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007342 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007343 break;
7344# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007345 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007346# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007347 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007348 break;
7349# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007350 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007351 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007352 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007353 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007354 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007355 break;
7356 }
7357 return 0;
7358}
7359#endif
7360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 * Get the string value of a variable.
7363 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007364 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7365 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 * If the String variable has never been set, return an empty string.
7367 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007368 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007369 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007371 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007372tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 static char_u mybuf[NUMBUFLEN];
7375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007376 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377}
7378
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007379 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007380tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007382 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007383
7384 return res != NULL ? res : (char_u *)"";
7385}
7386
Bram Moolenaar7d647822014-04-05 21:28:56 +02007387/*
7388 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7389 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007390 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007391tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392{
7393 static char_u mybuf[NUMBUFLEN];
7394
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007395 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007396}
7397
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007398 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007399tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007400{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007403 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007404 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007405 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007406 return buf;
7407 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007408 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007409 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007410 break;
7411 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007412 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 break;
7414 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007415 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007417 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007418#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007419 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007420 break;
7421#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007422 case VAR_STRING:
7423 if (varp->vval.v_string != NULL)
7424 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007425 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007426 case VAR_SPECIAL:
7427 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7428 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007429 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007431 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007432 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007433#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007434 {
7435 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007436 char *status;
7437
7438 if (job == NULL)
7439 return (char_u *)"no process";
7440 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007441 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007442 : "run";
7443# ifdef UNIX
7444 vim_snprintf((char *)buf, NUMBUFLEN,
7445 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007446# elif defined(WIN32)
7447 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007448 "process %ld %s",
7449 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007450 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007451# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007452 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007453 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7454# endif
7455 return buf;
7456 }
7457#endif
7458 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007459 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007461 {
7462 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007463 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007464
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007465 if (channel == NULL)
7466 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7467 else
7468 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007469 "channel %d %s", channel->ch_id, status);
7470 return buf;
7471 }
7472#endif
7473 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007474 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007478 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479}
7480
7481/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007482 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7483 * string() on Dict, List, etc.
7484 */
7485 char_u *
7486tv_stringify(typval_T *varp, char_u *buf)
7487{
7488 if (varp->v_type == VAR_LIST
7489 || varp->v_type == VAR_DICT
7490 || varp->v_type == VAR_FUNC
7491 || varp->v_type == VAR_PARTIAL
7492 || varp->v_type == VAR_FLOAT)
7493 {
7494 typval_T tmp;
7495
7496 f_string(varp, &tmp);
7497 tv_get_string_buf(&tmp, buf);
7498 clear_tv(varp);
7499 *varp = tmp;
7500 return tmp.vval.v_string;
7501 }
7502 return tv_get_string_buf(varp, buf);
7503}
7504
7505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 * Find variable "name" in the list of variables.
7507 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007509 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007512 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007517 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
Bram Moolenaara7043832005-01-21 11:56:39 +00007519 ht = find_var_ht(name, &varname);
7520 if (htp != NULL)
7521 *htp = ht;
7522 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007524 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7525 if (ret != NULL)
7526 return ret;
7527
7528 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007529 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531
7532/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007533 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007534 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007536 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007537find_var_in_ht(
7538 hashtab_T *ht,
7539 int htname,
7540 char_u *varname,
7541 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007542{
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 hashitem_T *hi;
7544
7545 if (*varname == NUL)
7546 {
7547 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007548 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007550 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 case 'g': return &globvars_var;
7552 case 'v': return &vimvars_var;
7553 case 'b': return &curbuf->b_bufvar;
7554 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007555 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007556 case 'l': return get_funccal_local_var();
7557 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 }
7559 return NULL;
7560 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007561
7562 hi = hash_find(ht, varname);
7563 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007564 {
7565 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007566 * worked find the variable again. Don't auto-load a script if it was
7567 * loaded already, otherwise it would be loaded every time when
7568 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007569 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007570 {
7571 /* Note: script_autoload() may make "hi" invalid. It must either
7572 * be obtained again or not used. */
7573 if (!script_autoload(varname, FALSE) || aborting())
7574 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007575 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007576 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007577 if (HASHITEM_EMPTY(hi))
7578 return NULL;
7579 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007581}
7582
7583/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007585 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007586 * Set "varname" to the start of name without ':'.
7587 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007588 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007589find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007591 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007592 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007593
Bram Moolenaar73627d02015-08-11 15:46:09 +02007594 if (name[0] == NUL)
7595 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 if (name[1] != ':')
7597 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007598 /* The name must not start with a colon or #. */
7599 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 return NULL;
7601 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007602
7603 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007604 hi = hash_find(&compat_hashtab, name);
7605 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007606 return &compat_hashtab;
7607
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007608 ht = get_funccal_local_ht();
7609 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007611 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007614 if (*name == 'g') /* global variable */
7615 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007616 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7617 */
7618 if (vim_strchr(name + 2, ':') != NULL
7619 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007620 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007622 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007624 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007625 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007626 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 if (*name == 'v') /* v: variable */
7628 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007629 if (*name == 'a') /* a: function argument */
7630 return get_funccal_args_ht();
7631 if (*name == 'l') /* l: local function variable */
7632 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007634 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7635 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 return NULL;
7637}
7638
7639/*
7640 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007641 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 * Returns NULL when it doesn't exist.
7643 */
7644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007649 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (v == NULL)
7651 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007652 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
7655/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 * sourcing this script and when executing functions defined in the script.
7658 */
7659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007660new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
Bram Moolenaara7043832005-01-21 11:56:39 +00007662 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 hashtab_T *ht;
7664 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007665
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7667 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007668 /* Re-allocating ga_data means that an ht_array pointing to
7669 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007671 for (i = 1; i <= ga_scripts.ga_len; ++i)
7672 {
7673 ht = &SCRIPT_VARS(i);
7674 if (ht->ht_mask == HT_INIT_SIZE - 1)
7675 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007676 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007678 }
7679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 while (ga_scripts.ga_len < id)
7681 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007682 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007683 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007684 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687 }
7688}
7689
7690/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7692 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 */
7694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007698 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007699 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007700 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007701 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 dict_var->di_tv.vval.v_dict = dict;
7703 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007704 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7706 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707}
7708
7709/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007710 * Unreference a dictionary initialized by init_var_dict().
7711 */
7712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007713unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007714{
7715 /* Now the dict needs to be freed if no one else is using it, go back to
7716 * normal reference counting. */
7717 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7718 dict_unref(dict);
7719}
7720
7721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 * Frees all allocated variables and the value they contain.
7724 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 */
7726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007728{
7729 vars_clear_ext(ht, TRUE);
7730}
7731
7732/*
7733 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7734 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
Bram Moolenaara7043832005-01-21 11:56:39 +00007738 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 hashitem_T *hi;
7740 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007743 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007744 for (hi = ht->ht_array; todo > 0; ++hi)
7745 {
7746 if (!HASHITEM_EMPTY(hi))
7747 {
7748 --todo;
7749
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007751 * ht_array might change then. hash_clear() takes care of it
7752 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 v = HI2DI(hi);
7754 if (free_val)
7755 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007756 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007758 }
7759 }
7760 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007761 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762}
7763
Bram Moolenaara7043832005-01-21 11:56:39 +00007764/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007765 * Delete a variable from hashtab "ht" at item "hi".
7766 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007769delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007772
7773 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 clear_tv(&di->di_tv);
7775 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776}
7777
7778/*
7779 * List the value of one internal variable.
7780 */
7781 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007782list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007784 char_u *tofree;
7785 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007786 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007787
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007788 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007790 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007791 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792}
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007795list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007796 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007797 char_u *name,
7798 int type,
7799 char_u *string,
7800 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
Bram Moolenaar31859182007-08-14 20:41:13 +00007802 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7803 msg_start();
7804 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007806 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 msg_putchar(' ');
7808 msg_advance(22);
7809 if (type == VAR_NUMBER)
7810 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007811 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 msg_putchar('*');
7813 else if (type == VAR_LIST)
7814 {
7815 msg_putchar('[');
7816 if (*string == '[')
7817 ++string;
7818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007819 else if (type == VAR_DICT)
7820 {
7821 msg_putchar('{');
7822 if (*string == '{')
7823 ++string;
7824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 else
7826 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007830 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007831 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007832 if (*first)
7833 {
7834 msg_clr_eos();
7835 *first = FALSE;
7836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
7839/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 * If the variable already exists, the value is updated.
7842 * Otherwise the variable is created.
7843 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007845set_var(
7846 char_u *name,
7847 typval_T *tv,
7848 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007854 ht = find_var_ht(name, &varname);
7855 if (ht == NULL || *varname == NUL)
7856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007857 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007858 return;
7859 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007860 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007861
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007862 /* Search in parent scope which is possible to reference from lambda */
7863 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007864 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007865
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007866 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7867 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007868 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007873 if (var_check_ro(v->di_flags, name, FALSE)
7874 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007876
7877 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007878 * Handle setting internal v: variables separately where needed to
7879 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 */
7881 if (ht == &vimvarht)
7882 {
7883 if (v->di_tv.v_type == VAR_STRING)
7884 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007885 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007887 {
7888 char_u *val = tv_get_string(tv);
7889
7890 // Careful: when assigning to v:errmsg and tv_get_string()
7891 // causes an error message the variable will alrady be set.
7892 if (v->di_tv.vval.v_string == NULL)
7893 v->di_tv.vval.v_string = vim_strsave(val);
7894 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 else
7896 {
7897 /* Take over the string to avoid an extra alloc/free. */
7898 v->di_tv.vval.v_string = tv->vval.v_string;
7899 tv->vval.v_string = NULL;
7900 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007901 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007903 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906 if (STRCMP(varname, "searchforward") == 0)
7907 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007908#ifdef FEAT_SEARCH_EXTRA
7909 else if (STRCMP(varname, "hlsearch") == 0)
7910 {
7911 no_hlsearch = !v->di_tv.vval.v_number;
7912 redraw_all_later(SOME_VALID);
7913 }
7914#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007915 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007917 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007919 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007920 return;
7921 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 }
7923
7924 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 else /* add a new variable */
7927 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007928 /* Can't add "v:" variable. */
7929 if (ht == &vimvarht)
7930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007931 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007932 return;
7933 }
7934
Bram Moolenaar92124a32005-06-17 22:03:40 +00007935 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007936 if (!valid_varname(varname))
7937 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007938
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007939 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7940 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007941 if (v == NULL)
7942 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007946 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 return;
7948 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007949 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007951
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007952 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007954 else
7955 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007957 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960}
7961
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007963 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 * Also give an error message.
7965 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007967var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007968{
7969 if (flags & DI_FLAGS_RO)
7970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007971 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 return TRUE;
7973 }
7974 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007976 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 return TRUE;
7978 }
7979 return FALSE;
7980}
7981
7982/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007983 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7984 * Also give an error message.
7985 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007988{
7989 if (flags & DI_FLAGS_FIX)
7990 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007991 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007992 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007993 return TRUE;
7994 }
7995 return FALSE;
7996}
7997
7998/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007999 * Check if a funcref is assigned to a valid variable name.
8000 * Return TRUE and give an error if not.
8001 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003var_check_func_name(
8004 char_u *name, /* points to start of variable name */
8005 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008006{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008007 /* Allow for w: b: s: and t:. */
8008 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008009 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8010 ? name[2] : name[0]))
8011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008012 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008013 name);
8014 return TRUE;
8015 }
8016 /* Don't allow hiding a function. When "v" is not NULL we might be
8017 * assigning another function to the same var, the type is checked
8018 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008019 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008021 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008022 name);
8023 return TRUE;
8024 }
8025 return FALSE;
8026}
8027
8028/*
8029 * Check if a variable name is valid.
8030 * Return FALSE and give an error if not.
8031 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008033valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008034{
8035 char_u *p;
8036
8037 for (p = varname; *p != NUL; ++p)
8038 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8039 && *p != AUTOLOAD_CHAR)
8040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008041 semsg(_(e_illvar), varname);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008042 return FALSE;
8043 }
8044 return TRUE;
8045}
8046
8047/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008048 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008049 * Also give an error message, using "name" or _("name") when use_gettext is
8050 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008051 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008053tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008054{
8055 if (lock & VAR_LOCKED)
8056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008057 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008058 name == NULL ? (char_u *)_("Unknown")
8059 : use_gettext ? (char_u *)_(name)
8060 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008061 return TRUE;
8062 }
8063 if (lock & VAR_FIXED)
8064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008065 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008066 name == NULL ? (char_u *)_("Unknown")
8067 : use_gettext ? (char_u *)_(name)
8068 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008069 return TRUE;
8070 }
8071 return FALSE;
8072}
8073
8074/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008076 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008077 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008078 * It is OK for "from" and "to" to point to the same item. This is used to
8079 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008080 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008082copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008085 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 switch (from->v_type)
8087 {
8088 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008089 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008090 to->vval.v_number = from->vval.v_number;
8091 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008093#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008094 to->vval.v_float = from->vval.v_float;
8095 break;
8096#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008097 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008098#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008099 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008100 if (to->vval.v_job != NULL)
8101 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008102 break;
8103#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008104 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008106 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008107 if (to->vval.v_channel != NULL)
8108 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008109 break;
8110#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008111 case VAR_STRING:
8112 case VAR_FUNC:
8113 if (from->vval.v_string == NULL)
8114 to->vval.v_string = NULL;
8115 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008116 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008117 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008118 if (from->v_type == VAR_FUNC)
8119 func_ref(to->vval.v_string);
8120 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008122 case VAR_PARTIAL:
8123 if (from->vval.v_partial == NULL)
8124 to->vval.v_partial = NULL;
8125 else
8126 {
8127 to->vval.v_partial = from->vval.v_partial;
8128 ++to->vval.v_partial->pt_refcount;
8129 }
8130 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008131 case VAR_BLOB:
8132 if (from->vval.v_blob == NULL)
8133 to->vval.v_blob = NULL;
8134 else
8135 {
8136 to->vval.v_blob = from->vval.v_blob;
8137 ++to->vval.v_blob->bv_refcount;
8138 }
8139 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 case VAR_LIST:
8141 if (from->vval.v_list == NULL)
8142 to->vval.v_list = NULL;
8143 else
8144 {
8145 to->vval.v_list = from->vval.v_list;
8146 ++to->vval.v_list->lv_refcount;
8147 }
8148 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008149 case VAR_DICT:
8150 if (from->vval.v_dict == NULL)
8151 to->vval.v_dict = NULL;
8152 else
8153 {
8154 to->vval.v_dict = from->vval.v_dict;
8155 ++to->vval.v_dict->dv_refcount;
8156 }
8157 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008158 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008159 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008160 break;
8161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162}
8163
8164/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165 * Make a copy of an item.
8166 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008167 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8168 * reference to an already copied list/dict can be used.
8169 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008172item_copy(
8173 typval_T *from,
8174 typval_T *to,
8175 int deep,
8176 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177{
8178 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008179 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180
Bram Moolenaar33570922005-01-25 22:26:29 +00008181 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008183 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008184 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 }
8186 ++recurse;
8187
8188 switch (from->v_type)
8189 {
8190 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008191 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008192 case VAR_STRING:
8193 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008194 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008195 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008196 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008197 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008198 copy_tv(from, to);
8199 break;
8200 case VAR_LIST:
8201 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008202 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008203 if (from->vval.v_list == NULL)
8204 to->vval.v_list = NULL;
8205 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8206 {
8207 /* use the copy made earlier */
8208 to->vval.v_list = from->vval.v_list->lv_copylist;
8209 ++to->vval.v_list->lv_refcount;
8210 }
8211 else
8212 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8213 if (to->vval.v_list == NULL)
8214 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008215 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008216 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008217 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008218 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008219 case VAR_DICT:
8220 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008221 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008222 if (from->vval.v_dict == NULL)
8223 to->vval.v_dict = NULL;
8224 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8225 {
8226 /* use the copy made earlier */
8227 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8228 ++to->vval.v_dict->dv_refcount;
8229 }
8230 else
8231 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8232 if (to->vval.v_dict == NULL)
8233 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008234 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008235 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008236 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008237 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008238 }
8239 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008240 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241}
8242
8243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 * This function is used by f_input() and f_inputdialog() functions. The third
8245 * argument to f_input() specifies the type of completion to use at the
8246 * prompt. The third argument to f_inputdialog() specifies the value to return
8247 * when the user cancels the prompt.
8248 */
8249 void
8250get_user_input(
8251 typval_T *argvars,
8252 typval_T *rettv,
8253 int inputdialog,
8254 int secret)
8255{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 char_u *p = NULL;
8258 int c;
8259 char_u buf[NUMBUFLEN];
8260 int cmd_silent_save = cmd_silent;
8261 char_u *defstr = (char_u *)"";
8262 int xp_type = EXPAND_NOTHING;
8263 char_u *xp_arg = NULL;
8264
8265 rettv->v_type = VAR_STRING;
8266 rettv->vval.v_string = NULL;
8267
8268#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008269 /* While starting up, there is no place to enter text. When running tests
8270 * with --not-a-term we assume feedkeys() will be used. */
8271 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 return;
8273#endif
8274
8275 cmd_silent = FALSE; /* Want to see the prompt. */
8276 if (prompt != NULL)
8277 {
8278 /* Only the part of the message after the last NL is considered as
8279 * prompt for the command line */
8280 p = vim_strrchr(prompt, '\n');
8281 if (p == NULL)
8282 p = prompt;
8283 else
8284 {
8285 ++p;
8286 c = *p;
8287 *p = NUL;
8288 msg_start();
8289 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008290 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 msg_didout = FALSE;
8292 msg_starthere();
8293 *p = c;
8294 }
8295 cmdline_row = msg_row;
8296
8297 if (argvars[1].v_type != VAR_UNKNOWN)
8298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008299 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 if (defstr != NULL)
8301 stuffReadbuffSpec(defstr);
8302
8303 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8304 {
8305 char_u *xp_name;
8306 int xp_namelen;
8307 long argt;
8308
8309 /* input() with a third argument: completion */
8310 rettv->vval.v_string = NULL;
8311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008312 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 if (xp_name == NULL)
8314 return;
8315
8316 xp_namelen = (int)STRLEN(xp_name);
8317
8318 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8319 &xp_arg) == FAIL)
8320 return;
8321 }
8322 }
8323
8324 if (defstr != NULL)
8325 {
8326 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 ex_normal_busy = 0;
8329 rettv->vval.v_string =
8330 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8331 xp_type, xp_arg);
8332 ex_normal_busy = save_ex_normal_busy;
8333 }
8334 if (inputdialog && rettv->vval.v_string == NULL
8335 && argvars[1].v_type != VAR_UNKNOWN
8336 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 &argvars[2], buf));
8339
8340 vim_free(xp_arg);
8341
8342 /* since the user typed this, no need to wait for return */
8343 need_wait_return = FALSE;
8344 msg_didout = FALSE;
8345 }
8346 cmd_silent = cmd_silent_save;
8347}
8348
8349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 * ":echo expr1 ..." print each argument separated with a space, add a
8351 * newline at the end.
8352 * ":echon expr1 ..." print each argument plain.
8353 */
8354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008355ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008359 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 char_u *p;
8361 int needclr = TRUE;
8362 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008364 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008365 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
8367 if (eap->skip)
8368 ++emsg_skip;
8369 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8370 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371 /* If eval1() causes an error message the text from the command may
8372 * still need to be cleared. E.g., "echo 22,44". */
8373 need_clr_eos = needclr;
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {
8378 /*
8379 * Report the invalid expression unless the expression evaluation
8380 * has been cancelled due to an aborting error, an interrupt, or an
8381 * exception.
8382 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008383 if (!aborting() && did_emsg == did_emsg_before
8384 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 break;
8388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008389 need_clr_eos = FALSE;
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 if (!eap->skip)
8392 {
8393 if (atstart)
8394 {
8395 atstart = FALSE;
8396 /* Call msg_start() after eval1(), evaluating the expression
8397 * may cause a message to appear. */
8398 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008399 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008400 /* Mark the saved text as finishing the line, so that what
8401 * follows is displayed on a new line when scrolling back
8402 * at the more prompt. */
8403 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008408 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008409 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008410 if (p != NULL)
8411 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008413 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 if (*p != TAB && needclr)
8416 {
8417 /* remove any text still there from the command */
8418 msg_clr_eos();
8419 needclr = FALSE;
8420 }
8421 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
8423 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008424 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008425 if (has_mbyte)
8426 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008427 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008428
8429 (void)msg_outtrans_len_attr(p, i, echo_attr);
8430 p += i - 1;
8431 }
8432 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008433 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 arg = skipwhite(arg);
8440 }
8441 eap->nextcmd = check_nextcmd(arg);
8442
8443 if (eap->skip)
8444 --emsg_skip;
8445 else
8446 {
8447 /* remove text that may still be there from the command */
8448 if (needclr)
8449 msg_clr_eos();
8450 if (eap->cmdidx == CMD_echo)
8451 msg_end();
8452 }
8453}
8454
8455/*
8456 * ":echohl {name}".
8457 */
8458 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008459ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008461 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462}
8463
8464/*
8465 * ":execute expr1 ..." execute the result of an expression.
8466 * ":echomsg expr1 ..." Print a message
8467 * ":echoerr expr1 ..." Print an error
8468 * Each gets spaces around each argument and a newline at the end for
8469 * echo commands
8470 */
8471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008472ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
8474 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 int ret = OK;
8477 char_u *p;
8478 garray_T ga;
8479 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008480 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481
8482 ga_init2(&ga, 1, 80);
8483
8484 if (eap->skip)
8485 ++emsg_skip;
8486 while (*arg != NUL && *arg != '|' && *arg != '\n')
8487 {
8488 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008489 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8490 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
8493 if (!eap->skip)
8494 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008495 char_u buf[NUMBUFLEN];
8496
8497 if (eap->cmdidx == CMD_execute)
8498 p = tv_get_string_buf(&rettv, buf);
8499 else
8500 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 len = (int)STRLEN(p);
8502 if (ga_grow(&ga, len + 2) == FAIL)
8503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 ret = FAIL;
8506 break;
8507 }
8508 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 ga.ga_len += len;
8512 }
8513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 arg = skipwhite(arg);
8516 }
8517
8518 if (ret != FAIL && ga.ga_data != NULL)
8519 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008520 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8521 {
8522 /* Mark the already saved text as finishing the line, so that what
8523 * follows is displayed on a new line when scrolling back at the
8524 * more prompt. */
8525 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008526 }
8527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008529 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008530 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008531 out_flush();
8532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 else if (eap->cmdidx == CMD_echoerr)
8534 {
8535 /* We don't want to abort following commands, restore did_emsg. */
8536 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008537 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (!force_abort)
8539 did_emsg = save_did_emsg;
8540 }
8541 else if (eap->cmdidx == CMD_execute)
8542 do_cmdline((char_u *)ga.ga_data,
8543 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8544 }
8545
8546 ga_clear(&ga);
8547
8548 if (eap->skip)
8549 --emsg_skip;
8550
8551 eap->nextcmd = check_nextcmd(arg);
8552}
8553
8554/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 * Find window specified by "vp" in tabpage "tp".
8556 */
8557 win_T *
8558find_win_by_nr(
8559 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008560 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008563 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 if (nr < 0)
8566 return NULL;
8567 if (nr == 0)
8568 return curwin;
8569
Bram Moolenaar29323592016-07-24 22:04:11 +02008570 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8571 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 if (nr >= LOWEST_WIN_ID)
8573 {
8574 if (wp->w_id == nr)
8575 return wp;
8576 }
8577 else if (--nr <= 0)
8578 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008579 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580 if (nr >= LOWEST_WIN_ID)
8581 return NULL;
8582 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583}
8584
8585/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008586 * Find a window: When using a Window ID in any tab page, when using a number
8587 * in the current tab page.
8588 */
8589 win_T *
8590find_win_by_nr_or_id(typval_T *vp)
8591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008592 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008593
8594 if (nr >= LOWEST_WIN_ID)
8595 return win_id2wp(vp);
8596 return find_win_by_nr(vp, NULL);
8597}
8598
8599/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 * Find window specified by "wvp" in tabpage "tvp".
8601 */
8602 win_T *
8603find_tabwin(
8604 typval_T *wvp, /* VAR_UNKNOWN for current window */
8605 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8606{
8607 win_T *wp = NULL;
8608 tabpage_T *tp = NULL;
8609 long n;
8610
8611 if (wvp->v_type != VAR_UNKNOWN)
8612 {
8613 if (tvp->v_type != VAR_UNKNOWN)
8614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008615 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 if (n >= 0)
8617 tp = find_tabpage(n);
8618 }
8619 else
8620 tp = curtab;
8621
8622 if (tp != NULL)
8623 wp = find_win_by_nr(wvp, tp);
8624 }
8625 else
8626 wp = curwin;
8627
8628 return wp;
8629}
8630
8631/*
8632 * getwinvar() and gettabwinvar()
8633 */
8634 void
8635getwinvar(
8636 typval_T *argvars,
8637 typval_T *rettv,
8638 int off) /* 1 for gettabwinvar() */
8639{
8640 win_T *win;
8641 char_u *varname;
8642 dictitem_T *v;
8643 tabpage_T *tp = NULL;
8644 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 win_T *oldcurwin;
8646 tabpage_T *oldtabpage;
8647 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008650 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 else
8652 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008654 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 ++emsg_off;
8656
8657 rettv->v_type = VAR_STRING;
8658 rettv->vval.v_string = NULL;
8659
8660 if (win != NULL && varname != NULL)
8661 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 /* Set curwin to be our win, temporarily. Also set the tabpage,
8663 * otherwise the window is not valid. Only do this when needed,
8664 * autocommands get blocked. */
8665 need_switch_win = !(tp == curtab && win == curwin);
8666 if (!need_switch_win
8667 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008669 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008671 if (varname[1] == NUL)
8672 {
8673 /* get all window-local options in a dict */
8674 dict_T *opts = get_winbuf_options(FALSE);
8675
8676 if (opts != NULL)
8677 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008678 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008679 done = TRUE;
8680 }
8681 }
8682 else if (get_option_tv(&varname, rettv, 1) == OK)
8683 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684 done = TRUE;
8685 }
8686 else
8687 {
8688 /* Look up the variable. */
8689 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8690 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8691 varname, FALSE);
8692 if (v != NULL)
8693 {
8694 copy_tv(&v->di_tv, rettv);
8695 done = TRUE;
8696 }
8697 }
8698 }
8699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008700 if (need_switch_win)
8701 /* restore previous notion of curwin */
8702 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 }
8704
8705 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8706 /* use the default return value */
8707 copy_tv(&argvars[off + 2], rettv);
8708
8709 --emsg_off;
8710}
8711
8712/*
8713 * "setwinvar()" and "settabwinvar()" functions
8714 */
8715 void
8716setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8717{
8718 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 win_T *save_curwin;
8720 tabpage_T *save_curtab;
8721 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 char_u *varname, *winvarname;
8723 typval_T *varp;
8724 char_u nbuf[NUMBUFLEN];
8725 tabpage_T *tp = NULL;
8726
8727 if (check_restricted() || check_secure())
8728 return;
8729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008731 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 else
8733 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008735 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 varp = &argvars[off + 2];
8737
8738 if (win != NULL && varname != NULL && varp != NULL)
8739 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740 need_switch_win = !(tp == curtab && win == curwin);
8741 if (!need_switch_win
8742 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 {
8744 if (*varname == '&')
8745 {
8746 long numval;
8747 char_u *strval;
8748 int error = FALSE;
8749
8750 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008751 numval = (long)tv_get_number_chk(varp, &error);
8752 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 if (!error && strval != NULL)
8754 set_option_value(varname, numval, strval, OPT_LOCAL);
8755 }
8756 else
8757 {
8758 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8759 if (winvarname != NULL)
8760 {
8761 STRCPY(winvarname, "w:");
8762 STRCPY(winvarname + 2, varname);
8763 set_var(winvarname, varp, TRUE);
8764 vim_free(winvarname);
8765 }
8766 }
8767 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768 if (need_switch_win)
8769 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 }
8771}
8772
8773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8775 * "arg" points to the "&" or '+' when called, to "option" when returning.
8776 * Returns NULL when no option name found. Otherwise pointer to the char
8777 * after the option name.
8778 */
8779 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008780find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 char_u *p = *arg;
8783
8784 ++p;
8785 if (*p == 'g' && p[1] == ':')
8786 {
8787 *opt_flags = OPT_GLOBAL;
8788 p += 2;
8789 }
8790 else if (*p == 'l' && p[1] == ':')
8791 {
8792 *opt_flags = OPT_LOCAL;
8793 p += 2;
8794 }
8795 else
8796 *opt_flags = 0;
8797
8798 if (!ASCII_ISALPHA(*p))
8799 return NULL;
8800 *arg = p;
8801
8802 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8803 p += 4; /* termcap option */
8804 else
8805 while (ASCII_ISALPHA(*p))
8806 ++p;
8807 return p;
8808}
8809
8810/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008811 * Return the autoload script name for a function or variable name.
8812 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008814 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008815autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008816{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008817 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008818 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008819
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008820 /* Get the script file name: replace '#' with '/', append ".vim". */
8821 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8822 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008823 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008824 STRCPY(scriptname, "autoload/");
8825 STRCAT(scriptname, name);
8826 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8827 STRCAT(scriptname, ".vim");
8828 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8829 *p = '/';
8830 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008831}
8832
8833/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008834 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008835 * Return TRUE if a package was loaded.
8836 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008838script_autoload(
8839 char_u *name,
8840 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841{
8842 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008843 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008845 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008846
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008847 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008848 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008849 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 return FALSE;
8851
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008852 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008853
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008854 /* Find the name in the list of previously loaded package names. Skip
8855 * "autoload/", it's always the same. */
8856 for (i = 0; i < ga_loaded.ga_len; ++i)
8857 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8858 break;
8859 if (!reload && i < ga_loaded.ga_len)
8860 ret = FALSE; /* was loaded already */
8861 else
8862 {
8863 /* Remember the name if it wasn't loaded already. */
8864 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8865 {
8866 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8867 tofree = NULL;
8868 }
8869
8870 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008871 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008872 ret = TRUE;
8873 }
8874
8875 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008876 return ret;
8877}
8878
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8880typedef enum
8881{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008882 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8883 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8884 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885} var_flavour_T;
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008888var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 char_u *p = varname;
8891
8892 if (ASCII_ISUPPER(*p))
8893 {
8894 while (*(++p))
8895 if (ASCII_ISLOWER(*p))
8896 return VAR_FLAVOUR_SESSION;
8897 return VAR_FLAVOUR_VIMINFO;
8898 }
8899 else
8900 return VAR_FLAVOUR_DEFAULT;
8901}
8902#endif
8903
8904#if defined(FEAT_VIMINFO) || defined(PROTO)
8905/*
8906 * Restore global vars that start with a capital from the viminfo file
8907 */
8908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008909read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
8911 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008912 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008914 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
8916 if (!writing && (find_viminfo_parameter('!') != NULL))
8917 {
8918 tab = vim_strchr(virp->vir_line + 1, '\t');
8919 if (tab != NULL)
8920 {
8921 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008922 switch (*tab)
8923 {
8924 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008925#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008926 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008927#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008928 case 'D': type = VAR_DICT; break;
8929 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008930 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008931 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
8934 tab = vim_strchr(tab, '\t');
8935 if (tab != NULL)
8936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008937 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008938 if (type == VAR_STRING || type == VAR_DICT
8939 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008940 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008942#ifdef FEAT_FLOAT
8943 else if (type == VAR_FLOAT)
8944 (void)string2float(tab + 1, &tv.vval.v_float);
8945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008947 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008948 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008949 {
8950 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8951
8952 if (etv == NULL)
8953 /* Failed to parse back the dict or list, use it as a
8954 * string. */
8955 tv.v_type = VAR_STRING;
8956 else
8957 {
8958 vim_free(tv.vval.v_string);
8959 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008960 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008961 }
8962 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008963 else if (type == VAR_BLOB)
8964 {
8965 blob_T *blob = string2blob(tv.vval.v_string);
8966
8967 if (blob == NULL)
8968 // Failed to parse back the blob, use it as a string.
8969 tv.v_type = VAR_STRING;
8970 else
8971 {
8972 vim_free(tv.vval.v_string);
8973 tv.v_type = VAR_BLOB;
8974 tv.vval.v_blob = blob;
8975 }
8976 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008977
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008978 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008979 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008980 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008981 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008982
8983 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008984 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008985 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8986 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008987 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 }
8989 }
8990 }
8991
8992 return viminfo_readline(virp);
8993}
8994
8995/*
8996 * Write global vars that start with a capital to the viminfo file
8997 */
8998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008999write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 hashitem_T *hi;
9002 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009003 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009004 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009005 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
9009 if (find_viminfo_parameter('!') == NULL)
9010 return;
9011
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009012 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009014 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009017 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009019 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 this_var = HI2DI(hi);
9021 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009024 {
9025 case VAR_STRING: s = "STR"; break;
9026 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009027 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009028 case VAR_DICT: s = "DIC"; break;
9029 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009030 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009031 case VAR_SPECIAL: s = "XPL"; break;
9032
9033 case VAR_UNKNOWN:
9034 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009036 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009037 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009038 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009041 if (this_var->di_tv.v_type == VAR_SPECIAL)
9042 {
9043 sprintf((char *)numbuf, "%ld",
9044 (long)this_var->di_tv.vval.v_number);
9045 p = numbuf;
9046 tofree = NULL;
9047 }
9048 else
9049 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009050 if (p != NULL)
9051 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009052 vim_free(tofree);
9053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
9055 }
9056}
9057#endif
9058
9059#if defined(FEAT_SESSION) || defined(PROTO)
9060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009061store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 hashitem_T *hi;
9064 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009065 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 char_u *p, *t;
9067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009068 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009071 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009073 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 this_var = HI2DI(hi);
9075 if ((this_var->di_tv.v_type == VAR_NUMBER
9076 || this_var->di_tv.v_type == VAR_STRING)
9077 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009078 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009079 /* Escape special characters with a backslash. Turn a LF and
9080 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009082 (char_u *)"\\\"\n\r");
9083 if (p == NULL) /* out of memory */
9084 break;
9085 for (t = p; *t != NUL; ++t)
9086 if (*t == '\n')
9087 *t = 'n';
9088 else if (*t == '\r')
9089 *t = 'r';
9090 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 this_var->di_key,
9092 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9093 : ' ',
9094 p,
9095 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9096 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009097 || put_eol(fd) == FAIL)
9098 {
9099 vim_free(p);
9100 return FAIL;
9101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 vim_free(p);
9103 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009104#ifdef FEAT_FLOAT
9105 else if (this_var->di_tv.v_type == VAR_FLOAT
9106 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9107 {
9108 float_T f = this_var->di_tv.vval.v_float;
9109 int sign = ' ';
9110
9111 if (f < 0)
9112 {
9113 f = -f;
9114 sign = '-';
9115 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009116 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009117 this_var->di_key, sign, f) < 0)
9118 || put_eol(fd) == FAIL)
9119 return FAIL;
9120 }
9121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
9123 }
9124 return OK;
9125}
9126#endif
9127
Bram Moolenaar661b1822005-07-28 22:36:45 +00009128/*
9129 * Display script name where an item was last set.
9130 * Should only be invoked when 'verbose' is non-zero.
9131 */
9132 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009133last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009134{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009135 char_u *p;
9136
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009137 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009138 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009139 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009140 if (p != NULL)
9141 {
9142 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009143 msg_puts(_("\n\tLast set from "));
9144 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009145 if (script_ctx.sc_lnum > 0)
9146 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009147 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009148 msg_outnum((long)script_ctx.sc_lnum);
9149 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009150 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009151 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009152 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009153 }
9154}
9155
Bram Moolenaar53744302015-07-17 17:38:22 +02009156/* reset v:option_new, v:option_old and v:option_type */
9157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009158reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009159{
9160 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9161 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9162 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9163}
9164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165/*
9166 * Prepare "gap" for an assert error and add the sourcing position.
9167 */
9168 void
9169prepare_assert_error(garray_T *gap)
9170{
9171 char buf[NUMBUFLEN];
9172
9173 ga_init2(gap, 1, 100);
9174 if (sourcing_name != NULL)
9175 {
9176 ga_concat(gap, sourcing_name);
9177 if (sourcing_lnum > 0)
9178 ga_concat(gap, (char_u *)" ");
9179 }
9180 if (sourcing_lnum > 0)
9181 {
9182 sprintf(buf, "line %ld", (long)sourcing_lnum);
9183 ga_concat(gap, (char_u *)buf);
9184 }
9185 if (sourcing_name != NULL || sourcing_lnum > 0)
9186 ga_concat(gap, (char_u *)": ");
9187}
9188
9189/*
9190 * Add an assert error to v:errors.
9191 */
9192 void
9193assert_error(garray_T *gap)
9194{
9195 struct vimvar *vp = &vimvars[VV_ERRORS];
9196
9197 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9198 /* Make sure v:errors is a list. */
9199 set_vim_var_list(VV_ERRORS, list_alloc());
9200 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9201}
9202
Bram Moolenaar65a54642018-04-28 16:56:53 +02009203 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204assert_equal_common(typval_T *argvars, assert_type_T atype)
9205{
9206 garray_T ga;
9207
9208 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9209 != (atype == ASSERT_EQUAL))
9210 {
9211 prepare_assert_error(&ga);
9212 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9213 atype);
9214 assert_error(&ga);
9215 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009216 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009217 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009218 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219}
9220
Bram Moolenaar65a54642018-04-28 16:56:53 +02009221 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009222assert_equalfile(typval_T *argvars)
9223{
9224 char_u buf1[NUMBUFLEN];
9225 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009226 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9227 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009228 garray_T ga;
9229 FILE *fd1;
9230 FILE *fd2;
9231
9232 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009233 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009234
9235 IObuff[0] = NUL;
9236 fd1 = mch_fopen((char *)fname1, READBIN);
9237 if (fd1 == NULL)
9238 {
9239 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9240 }
9241 else
9242 {
9243 fd2 = mch_fopen((char *)fname2, READBIN);
9244 if (fd2 == NULL)
9245 {
9246 fclose(fd1);
9247 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9248 }
9249 else
9250 {
9251 int c1, c2;
9252 long count = 0;
9253
9254 for (;;)
9255 {
9256 c1 = fgetc(fd1);
9257 c2 = fgetc(fd2);
9258 if (c1 == EOF)
9259 {
9260 if (c2 != EOF)
9261 STRCPY(IObuff, "first file is shorter");
9262 break;
9263 }
9264 else if (c2 == EOF)
9265 {
9266 STRCPY(IObuff, "second file is shorter");
9267 break;
9268 }
9269 else if (c1 != c2)
9270 {
9271 vim_snprintf((char *)IObuff, IOSIZE,
9272 "difference at byte %ld", count);
9273 break;
9274 }
9275 ++count;
9276 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009277 fclose(fd1);
9278 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009279 }
9280 }
9281 if (IObuff[0] != NUL)
9282 {
9283 prepare_assert_error(&ga);
9284 ga_concat(&ga, IObuff);
9285 assert_error(&ga);
9286 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009287 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009288 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009289 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009290}
9291
Bram Moolenaar65a54642018-04-28 16:56:53 +02009292 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293assert_match_common(typval_T *argvars, assert_type_T atype)
9294{
9295 garray_T ga;
9296 char_u buf1[NUMBUFLEN];
9297 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009298 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9299 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300
9301 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009302 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9304 {
9305 prepare_assert_error(&ga);
9306 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9307 atype);
9308 assert_error(&ga);
9309 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009310 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009312 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313}
9314
Bram Moolenaar65a54642018-04-28 16:56:53 +02009315 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009316assert_inrange(typval_T *argvars)
9317{
9318 garray_T ga;
9319 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009320 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9321 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9322 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009323 char_u *tofree;
9324 char msg[200];
9325 char_u numbuf[NUMBUFLEN];
9326
9327 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009328 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009329 if (actual < lower || actual > upper)
9330 {
9331 prepare_assert_error(&ga);
9332 if (argvars[3].v_type != VAR_UNKNOWN)
9333 {
9334 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9335 vim_free(tofree);
9336 }
9337 else
9338 {
9339 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9340 (long)lower, (long)upper, (long)actual);
9341 ga_concat(&ga, (char_u *)msg);
9342 }
9343 assert_error(&ga);
9344 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009345 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009346 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009347 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009348}
9349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350/*
9351 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009352 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009354 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355assert_bool(typval_T *argvars, int isTrue)
9356{
9357 int error = FALSE;
9358 garray_T ga;
9359
9360 if (argvars[0].v_type == VAR_SPECIAL
9361 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009362 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009364 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 || error)
9366 {
9367 prepare_assert_error(&ga);
9368 fill_assert_error(&ga, &argvars[1],
9369 (char_u *)(isTrue ? "True" : "False"),
9370 NULL, &argvars[0], ASSERT_OTHER);
9371 assert_error(&ga);
9372 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009373 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009375 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376}
9377
Bram Moolenaar65a54642018-04-28 16:56:53 +02009378 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009379assert_report(typval_T *argvars)
9380{
9381 garray_T ga;
9382
9383 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009384 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009385 assert_error(&ga);
9386 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009387 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009388}
9389
Bram Moolenaar65a54642018-04-28 16:56:53 +02009390 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391assert_exception(typval_T *argvars)
9392{
9393 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009394 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395
9396 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9397 {
9398 prepare_assert_error(&ga);
9399 ga_concat(&ga, (char_u *)"v:exception is not set");
9400 assert_error(&ga);
9401 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009402 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009403 }
9404 else if (error != NULL
9405 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9406 {
9407 prepare_assert_error(&ga);
9408 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9409 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9410 assert_error(&ga);
9411 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009412 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009414 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415}
9416
Bram Moolenaar65a54642018-04-28 16:56:53 +02009417 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009418assert_beeps(typval_T *argvars)
9419{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009420 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009421 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009422 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009423
9424 called_vim_beep = FALSE;
9425 suppress_errthrow = TRUE;
9426 emsg_silent = FALSE;
9427 do_cmdline_cmd(cmd);
9428 if (!called_vim_beep)
9429 {
9430 prepare_assert_error(&ga);
9431 ga_concat(&ga, (char_u *)"command did not beep: ");
9432 ga_concat(&ga, cmd);
9433 assert_error(&ga);
9434 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009435 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009436 }
9437
9438 suppress_errthrow = FALSE;
9439 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009440 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009441}
9442
Bram Moolenaar65a54642018-04-28 16:56:53 +02009443 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009444assert_fails(typval_T *argvars)
9445{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009446 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009448 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009449 char_u numbuf[NUMBUFLEN];
9450 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451
9452 called_emsg = FALSE;
9453 suppress_errthrow = TRUE;
9454 emsg_silent = TRUE;
9455 do_cmdline_cmd(cmd);
9456 if (!called_emsg)
9457 {
9458 prepare_assert_error(&ga);
9459 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009460 if (argvars[1].v_type != VAR_UNKNOWN
9461 && argvars[2].v_type != VAR_UNKNOWN)
9462 {
9463 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9464 vim_free(tofree);
9465 }
9466 else
9467 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468 assert_error(&ga);
9469 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009470 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 }
9472 else if (argvars[1].v_type != VAR_UNKNOWN)
9473 {
9474 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009475 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476
9477 if (error == NULL
9478 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9479 {
9480 prepare_assert_error(&ga);
9481 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9482 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9483 assert_error(&ga);
9484 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009485 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486 }
9487 }
9488
9489 called_emsg = FALSE;
9490 suppress_errthrow = FALSE;
9491 emsg_silent = FALSE;
9492 emsg_on_display = FALSE;
9493 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009494 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495}
9496
9497/*
9498 * Append "str" to "gap", escaping unprintable characters.
9499 * Changes NL to \n, CR to \r, etc.
9500 */
9501 static void
9502ga_concat_esc(garray_T *gap, char_u *str)
9503{
9504 char_u *p;
9505 char_u buf[NUMBUFLEN];
9506
9507 if (str == NULL)
9508 {
9509 ga_concat(gap, (char_u *)"NULL");
9510 return;
9511 }
9512
9513 for (p = str; *p != NUL; ++p)
9514 switch (*p)
9515 {
9516 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9517 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9518 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9519 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9520 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9521 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9522 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9523 default:
9524 if (*p < ' ')
9525 {
9526 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9527 ga_concat(gap, buf);
9528 }
9529 else
9530 ga_append(gap, *p);
9531 break;
9532 }
9533}
9534
9535/*
9536 * Fill "gap" with information about an assert error.
9537 */
9538 void
9539fill_assert_error(
9540 garray_T *gap,
9541 typval_T *opt_msg_tv,
9542 char_u *exp_str,
9543 typval_T *exp_tv,
9544 typval_T *got_tv,
9545 assert_type_T atype)
9546{
9547 char_u numbuf[NUMBUFLEN];
9548 char_u *tofree;
9549
9550 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9551 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009552 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9553 vim_free(tofree);
9554 ga_concat(gap, (char_u *)": ");
9555 }
9556
9557 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9558 ga_concat(gap, (char_u *)"Pattern ");
9559 else if (atype == ASSERT_NOTEQUAL)
9560 ga_concat(gap, (char_u *)"Expected not equal to ");
9561 else
9562 ga_concat(gap, (char_u *)"Expected ");
9563 if (exp_str == NULL)
9564 {
9565 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 vim_free(tofree);
9567 }
9568 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009569 ga_concat_esc(gap, exp_str);
9570 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009572 if (atype == ASSERT_MATCH)
9573 ga_concat(gap, (char_u *)" does not match ");
9574 else if (atype == ASSERT_NOTMATCH)
9575 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009577 ga_concat(gap, (char_u *)" but got ");
9578 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9579 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009580 }
9581}
9582
Bram Moolenaar31988702018-02-13 12:57:42 +01009583/*
9584 * Compare "typ1" and "typ2". Put the result in "typ1".
9585 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009586 int
9587typval_compare(
9588 typval_T *typ1, /* first operand */
9589 typval_T *typ2, /* second operand */
9590 exptype_T type, /* operator */
9591 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009592 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009593{
9594 int i;
9595 varnumber_T n1, n2;
9596 char_u *s1, *s2;
9597 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9598
Bram Moolenaar31988702018-02-13 12:57:42 +01009599 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009600 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009601 /* For "is" a different type always means FALSE, for "notis"
9602 * it means TRUE. */
9603 n1 = (type == TYPE_NEQUAL);
9604 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009605 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9606 {
9607 if (type_is)
9608 {
9609 n1 = (typ1->v_type == typ2->v_type
9610 && typ1->vval.v_blob == typ2->vval.v_blob);
9611 if (type == TYPE_NEQUAL)
9612 n1 = !n1;
9613 }
9614 else if (typ1->v_type != typ2->v_type
9615 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9616 {
9617 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009618 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009619 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009620 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009621 clear_tv(typ1);
9622 return FAIL;
9623 }
9624 else
9625 {
9626 // Compare two Blobs for being equal or unequal.
9627 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9628 if (type == TYPE_NEQUAL)
9629 n1 = !n1;
9630 }
9631 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009632 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9633 {
9634 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009635 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009636 n1 = (typ1->v_type == typ2->v_type
9637 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009638 if (type == TYPE_NEQUAL)
9639 n1 = !n1;
9640 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009641 else if (typ1->v_type != typ2->v_type
9642 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009643 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009644 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009645 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009646 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009647 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009648 clear_tv(typ1);
9649 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009650 }
9651 else
9652 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009653 /* Compare two Lists for being equal or unequal. */
9654 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9655 ic, FALSE);
9656 if (type == TYPE_NEQUAL)
9657 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009658 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009659 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009660
9661 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9662 {
9663 if (type_is)
9664 {
9665 n1 = (typ1->v_type == typ2->v_type
9666 && typ1->vval.v_dict == typ2->vval.v_dict);
9667 if (type == TYPE_NEQUAL)
9668 n1 = !n1;
9669 }
9670 else if (typ1->v_type != typ2->v_type
9671 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9672 {
9673 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009674 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009675 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009676 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009677 clear_tv(typ1);
9678 return FAIL;
9679 }
9680 else
9681 {
9682 /* Compare two Dictionaries for being equal or unequal. */
9683 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9684 ic, FALSE);
9685 if (type == TYPE_NEQUAL)
9686 n1 = !n1;
9687 }
9688 }
9689
9690 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9691 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9692 {
9693 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009695 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009696 clear_tv(typ1);
9697 return FAIL;
9698 }
9699 if ((typ1->v_type == VAR_PARTIAL
9700 && typ1->vval.v_partial == NULL)
9701 || (typ2->v_type == VAR_PARTIAL
9702 && typ2->vval.v_partial == NULL))
9703 /* when a partial is NULL assume not equal */
9704 n1 = FALSE;
9705 else if (type_is)
9706 {
9707 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9708 /* strings are considered the same if their value is
9709 * the same */
9710 n1 = tv_equal(typ1, typ2, ic, FALSE);
9711 else if (typ1->v_type == VAR_PARTIAL
9712 && typ2->v_type == VAR_PARTIAL)
9713 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9714 else
9715 n1 = FALSE;
9716 }
9717 else
9718 n1 = tv_equal(typ1, typ2, ic, FALSE);
9719 if (type == TYPE_NEQUAL)
9720 n1 = !n1;
9721 }
9722
9723#ifdef FEAT_FLOAT
9724 /*
9725 * If one of the two variables is a float, compare as a float.
9726 * When using "=~" or "!~", always compare as string.
9727 */
9728 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9729 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9730 {
9731 float_T f1, f2;
9732
9733 if (typ1->v_type == VAR_FLOAT)
9734 f1 = typ1->vval.v_float;
9735 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009736 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009737 if (typ2->v_type == VAR_FLOAT)
9738 f2 = typ2->vval.v_float;
9739 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009740 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009741 n1 = FALSE;
9742 switch (type)
9743 {
9744 case TYPE_EQUAL: n1 = (f1 == f2); break;
9745 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9746 case TYPE_GREATER: n1 = (f1 > f2); break;
9747 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9748 case TYPE_SMALLER: n1 = (f1 < f2); break;
9749 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9750 case TYPE_UNKNOWN:
9751 case TYPE_MATCH:
9752 case TYPE_NOMATCH: break; /* avoid gcc warning */
9753 }
9754 }
9755#endif
9756
9757 /*
9758 * If one of the two variables is a number, compare as a number.
9759 * When using "=~" or "!~", always compare as string.
9760 */
9761 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9762 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009764 n1 = tv_get_number(typ1);
9765 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009766 switch (type)
9767 {
9768 case TYPE_EQUAL: n1 = (n1 == n2); break;
9769 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9770 case TYPE_GREATER: n1 = (n1 > n2); break;
9771 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9772 case TYPE_SMALLER: n1 = (n1 < n2); break;
9773 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9774 case TYPE_UNKNOWN:
9775 case TYPE_MATCH:
9776 case TYPE_NOMATCH: break; /* avoid gcc warning */
9777 }
9778 }
9779 else
9780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009781 s1 = tv_get_string_buf(typ1, buf1);
9782 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009783 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9784 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9785 else
9786 i = 0;
9787 n1 = FALSE;
9788 switch (type)
9789 {
9790 case TYPE_EQUAL: n1 = (i == 0); break;
9791 case TYPE_NEQUAL: n1 = (i != 0); break;
9792 case TYPE_GREATER: n1 = (i > 0); break;
9793 case TYPE_GEQUAL: n1 = (i >= 0); break;
9794 case TYPE_SMALLER: n1 = (i < 0); break;
9795 case TYPE_SEQUAL: n1 = (i <= 0); break;
9796
9797 case TYPE_MATCH:
9798 case TYPE_NOMATCH:
9799 n1 = pattern_match(s2, s1, ic);
9800 if (type == TYPE_NOMATCH)
9801 n1 = !n1;
9802 break;
9803
9804 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9805 }
9806 }
9807 clear_tv(typ1);
9808 typ1->v_type = VAR_NUMBER;
9809 typ1->vval.v_number = n1;
9810
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009811 return OK;
9812}
9813
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009814 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009815typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009816{
9817 char_u *tofree;
9818 char_u numbuf[NUMBUFLEN];
9819 char_u *ret = NULL;
9820
9821 if (arg == NULL)
9822 return vim_strsave((char_u *)"(does not exist)");
9823 ret = tv2string(arg, &tofree, numbuf, 0);
9824 /* Make a copy if we have a value but it's not in allocated memory. */
9825 if (ret != NULL && tofree == NULL)
9826 ret = vim_strsave(ret);
9827 return ret;
9828}
9829
9830 int
9831var_exists(char_u *var)
9832{
9833 char_u *name;
9834 char_u *tofree;
9835 typval_T tv;
9836 int len = 0;
9837 int n = FALSE;
9838
9839 /* get_name_len() takes care of expanding curly braces */
9840 name = var;
9841 len = get_name_len(&var, &tofree, TRUE, FALSE);
9842 if (len > 0)
9843 {
9844 if (tofree != NULL)
9845 name = tofree;
9846 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9847 if (n)
9848 {
9849 /* handle d.key, l[idx], f(expr) */
9850 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9851 if (n)
9852 clear_tv(&tv);
9853 }
9854 }
9855 if (*var != NUL)
9856 n = FALSE;
9857
9858 vim_free(tofree);
9859 return n;
9860}
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862#endif /* FEAT_EVAL */
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009865#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
9867#ifdef WIN3264
9868/*
9869 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
9872/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009873 * Get the short path (8.3) for the filename in "fnamep".
9874 * Only works for a valid file name.
9875 * When the path gets longer "fnamep" is changed and the allocated buffer
9876 * is put in "bufp".
9877 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9878 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 */
9880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009883 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 char_u *newbuf;
9885
9886 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009887 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 if (l > len - 1)
9889 {
9890 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009891 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 newbuf = vim_strnsave(*fnamep, l);
9893 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009894 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896 vim_free(*bufp);
9897 *fnamep = *bufp = newbuf;
9898
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009899 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009900 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 }
9902
9903 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009904 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
9907/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009908 * Get the short path (8.3) for the filename in "fname". The converted
9909 * path is returned in "bufp".
9910 *
9911 * Some of the directories specified in "fname" may not exist. This function
9912 * will shorten the existing directories at the beginning of the path and then
9913 * append the remaining non-existing path.
9914 *
9915 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009916 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009917 * bufp - Pointer to an allocated buffer for the filename.
9918 * fnamelen - Length of the filename pointed to by fname
9919 *
9920 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 */
9922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009923shortpath_for_invalid_fname(
9924 char_u **fname,
9925 char_u **bufp,
9926 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009928 char_u *short_fname, *save_fname, *pbuf_unused;
9929 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009931 int old_len, len;
9932 int new_len, sfx_len;
9933 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009936 old_len = *fnamelen;
9937 save_fname = vim_strnsave(*fname, old_len);
9938 pbuf_unused = NULL;
9939 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009941 endp = save_fname + old_len - 1; /* Find the end of the copy */
9942 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009944 /*
9945 * Try shortening the supplied path till it succeeds by removing one
9946 * directory at a time from the tail of the path.
9947 */
9948 len = 0;
9949 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009951 /* go back one path-separator */
9952 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9953 --endp;
9954 if (endp <= save_fname)
9955 break; /* processed the complete path */
9956
9957 /*
9958 * Replace the path separator with a NUL and try to shorten the
9959 * resulting path.
9960 */
9961 ch = *endp;
9962 *endp = 0;
9963 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009964 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009965 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9966 {
9967 retval = FAIL;
9968 goto theend;
9969 }
9970 *endp = ch; /* preserve the string */
9971
9972 if (len > 0)
9973 break; /* successfully shortened the path */
9974
9975 /* failed to shorten the path. Skip the path separator */
9976 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 }
9978
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009979 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009981 /*
9982 * Succeeded in shortening the path. Now concatenate the shortened
9983 * path with the remaining path at the tail.
9984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009986 /* Compute the length of the new path. */
9987 sfx_len = (int)(save_endp - endp) + 1;
9988 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009990 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009992 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009994 /* There is not enough space in the currently allocated string,
9995 * copy it to a buffer big enough. */
9996 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009998 {
9999 retval = FAIL;
10000 goto theend;
10001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003 else
10004 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010005 /* Transfer short_fname to the main buffer (it's big enough),
10006 * unless get_short_pathname() did its work in-place. */
10007 *fname = *bufp = save_fname;
10008 if (short_fname != save_fname)
10009 vim_strncpy(save_fname, short_fname, len);
10010 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010012
10013 /* concat the not-shortened part of the path */
10014 vim_strncpy(*fname + len, endp, sfx_len);
10015 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010017
10018theend:
10019 vim_free(pbuf_unused);
10020 vim_free(save_fname);
10021
10022 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023}
10024
10025/*
10026 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010027 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 */
10029 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010030shortpath_for_partial(
10031 char_u **fnamep,
10032 char_u **bufp,
10033 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035 int sepcount, len, tflen;
10036 char_u *p;
10037 char_u *pbuf, *tfname;
10038 int hasTilde;
10039
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010040 /* Count up the path separators from the RHS.. so we know which part
10041 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010043 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 if (vim_ispathsep(*p))
10045 ++sepcount;
10046
10047 /* Need full path first (use expand_env() to remove a "~/") */
10048 hasTilde = (**fnamep == '~');
10049 if (hasTilde)
10050 pbuf = tfname = expand_env_save(*fnamep);
10051 else
10052 pbuf = tfname = FullName_save(*fnamep, FALSE);
10053
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010054 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010056 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10057 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
10059 if (len == 0)
10060 {
10061 /* Don't have a valid filename, so shorten the rest of the
10062 * path if we can. This CAN give us invalid 8.3 filenames, but
10063 * there's not a lot of point in guessing what it might be.
10064 */
10065 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010066 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10067 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 }
10069
10070 /* Count the paths backward to find the beginning of the desired string. */
10071 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010072 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010073 if (has_mbyte)
10074 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (vim_ispathsep(*p))
10076 {
10077 if (sepcount == 0 || (hasTilde && sepcount == 1))
10078 break;
10079 else
10080 sepcount --;
10081 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (hasTilde)
10084 {
10085 --p;
10086 if (p >= tfname)
10087 *p = '~';
10088 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 }
10091 else
10092 ++p;
10093
10094 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10095 vim_free(*bufp);
10096 *fnamelen = (int)STRLEN(p);
10097 *bufp = pbuf;
10098 *fnamep = p;
10099
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010100 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101}
10102#endif /* WIN3264 */
10103
10104/*
10105 * Adjust a filename, according to a string of modifiers.
10106 * *fnamep must be NUL terminated when called. When returning, the length is
10107 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010108 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 * When there is an error, *fnamep is set to NULL.
10110 */
10111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010112modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010113 char_u *src, // string with modifiers
10114 int tilde_file, // "~" is a file name, not $HOME
10115 int *usedlen, // characters after src that are used
10116 char_u **fnamep, // file name so far
10117 char_u **bufp, // buffer for allocated file name or NULL
10118 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120 int valid = 0;
10121 char_u *tail;
10122 char_u *s, *p, *pbuf;
10123 char_u dirname[MAXPATHL];
10124 int c;
10125 int has_fullname = 0;
10126#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010127 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 int has_shortname = 0;
10129#endif
10130
10131repeat:
10132 /* ":p" - full path/file_name */
10133 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10134 {
10135 has_fullname = 1;
10136
10137 valid |= VALID_PATH;
10138 *usedlen += 2;
10139
10140 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10141 if ((*fnamep)[0] == '~'
10142#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10143 && ((*fnamep)[1] == '/'
10144# ifdef BACKSLASH_IN_FILENAME
10145 || (*fnamep)[1] == '\\'
10146# endif
10147 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010149 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 )
10151 {
10152 *fnamep = expand_env_save(*fnamep);
10153 vim_free(*bufp); /* free any allocated file name */
10154 *bufp = *fnamep;
10155 if (*fnamep == NULL)
10156 return -1;
10157 }
10158
10159 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010160 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 {
10162 if (vim_ispathsep(*p)
10163 && p[1] == '.'
10164 && (p[2] == NUL
10165 || vim_ispathsep(p[2])
10166 || (p[2] == '.'
10167 && (p[3] == NUL || vim_ispathsep(p[3])))))
10168 break;
10169 }
10170
10171 /* FullName_save() is slow, don't use it when not needed. */
10172 if (*p != NUL || !vim_isAbsName(*fnamep))
10173 {
10174 *fnamep = FullName_save(*fnamep, *p != NUL);
10175 vim_free(*bufp); /* free any allocated file name */
10176 *bufp = *fnamep;
10177 if (*fnamep == NULL)
10178 return -1;
10179 }
10180
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010181#ifdef WIN3264
10182# if _WIN32_WINNT >= 0x0500
10183 if (vim_strchr(*fnamep, '~') != NULL)
10184 {
10185 /* Expand 8.3 filename to full path. Needed to make sure the same
10186 * file does not have two different names.
10187 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10188 p = alloc(_MAX_PATH + 1);
10189 if (p != NULL)
10190 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010191 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010192 {
10193 vim_free(*bufp);
10194 *bufp = *fnamep = p;
10195 }
10196 else
10197 vim_free(p);
10198 }
10199 }
10200# endif
10201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 /* Append a path separator to a directory. */
10203 if (mch_isdir(*fnamep))
10204 {
10205 /* Make room for one or two extra characters. */
10206 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10207 vim_free(*bufp); /* free any allocated file name */
10208 *bufp = *fnamep;
10209 if (*fnamep == NULL)
10210 return -1;
10211 add_pathsep(*fnamep);
10212 }
10213 }
10214
10215 /* ":." - path relative to the current directory */
10216 /* ":~" - path relative to the home directory */
10217 /* ":8" - shortname path - postponed till after */
10218 while (src[*usedlen] == ':'
10219 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10220 {
10221 *usedlen += 2;
10222 if (c == '8')
10223 {
10224#ifdef WIN3264
10225 has_shortname = 1; /* Postpone this. */
10226#endif
10227 continue;
10228 }
10229 pbuf = NULL;
10230 /* Need full path first (use expand_env() to remove a "~/") */
10231 if (!has_fullname)
10232 {
10233 if (c == '.' && **fnamep == '~')
10234 p = pbuf = expand_env_save(*fnamep);
10235 else
10236 p = pbuf = FullName_save(*fnamep, FALSE);
10237 }
10238 else
10239 p = *fnamep;
10240
10241 has_fullname = 0;
10242
10243 if (p != NULL)
10244 {
10245 if (c == '.')
10246 {
10247 mch_dirname(dirname, MAXPATHL);
10248 s = shorten_fname(p, dirname);
10249 if (s != NULL)
10250 {
10251 *fnamep = s;
10252 if (pbuf != NULL)
10253 {
10254 vim_free(*bufp); /* free any allocated file name */
10255 *bufp = pbuf;
10256 pbuf = NULL;
10257 }
10258 }
10259 }
10260 else
10261 {
10262 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10263 /* Only replace it when it starts with '~' */
10264 if (*dirname == '~')
10265 {
10266 s = vim_strsave(dirname);
10267 if (s != NULL)
10268 {
10269 *fnamep = s;
10270 vim_free(*bufp);
10271 *bufp = s;
10272 }
10273 }
10274 }
10275 vim_free(pbuf);
10276 }
10277 }
10278
10279 tail = gettail(*fnamep);
10280 *fnamelen = (int)STRLEN(*fnamep);
10281
10282 /* ":h" - head, remove "/file_name", can be repeated */
10283 /* Don't remove the first "/" or "c:\" */
10284 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10285 {
10286 valid |= VALID_HEAD;
10287 *usedlen += 2;
10288 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010289 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010290 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 *fnamelen = (int)(tail - *fnamep);
10292#ifdef VMS
10293 if (*fnamelen > 0)
10294 *fnamelen += 1; /* the path separator is part of the path */
10295#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010296 if (*fnamelen == 0)
10297 {
10298 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10299 p = vim_strsave((char_u *)".");
10300 if (p == NULL)
10301 return -1;
10302 vim_free(*bufp);
10303 *bufp = *fnamep = tail = p;
10304 *fnamelen = 1;
10305 }
10306 else
10307 {
10308 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010309 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 }
10312
10313 /* ":8" - shortname */
10314 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10315 {
10316 *usedlen += 2;
10317#ifdef WIN3264
10318 has_shortname = 1;
10319#endif
10320 }
10321
10322#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010323 /*
10324 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 */
10326 if (has_shortname)
10327 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010328 /* Copy the string if it is shortened by :h and when it wasn't copied
10329 * yet, because we are going to change it in place. Avoids changing
10330 * the buffer name for "%:8". */
10331 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 {
10333 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010334 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 return -1;
10336 vim_free(*bufp);
10337 *bufp = *fnamep = p;
10338 }
10339
10340 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010341 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (!has_fullname && !vim_isAbsName(*fnamep))
10343 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010344 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 return -1;
10346 }
10347 else
10348 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010349 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350
Bram Moolenaardc935552011-08-17 15:23:23 +020010351 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010353 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 return -1;
10355
10356 if (l == 0)
10357 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010358 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010360 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 return -1;
10362 }
10363 *fnamelen = l;
10364 }
10365 }
10366#endif /* WIN3264 */
10367
10368 /* ":t" - tail, just the basename */
10369 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10370 {
10371 *usedlen += 2;
10372 *fnamelen -= (int)(tail - *fnamep);
10373 *fnamep = tail;
10374 }
10375
10376 /* ":e" - extension, can be repeated */
10377 /* ":r" - root, without extension, can be repeated */
10378 while (src[*usedlen] == ':'
10379 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10380 {
10381 /* find a '.' in the tail:
10382 * - for second :e: before the current fname
10383 * - otherwise: The last '.'
10384 */
10385 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10386 s = *fnamep - 2;
10387 else
10388 s = *fnamep + *fnamelen - 1;
10389 for ( ; s > tail; --s)
10390 if (s[0] == '.')
10391 break;
10392 if (src[*usedlen + 1] == 'e') /* :e */
10393 {
10394 if (s > tail)
10395 {
10396 *fnamelen += (int)(*fnamep - (s + 1));
10397 *fnamep = s + 1;
10398#ifdef VMS
10399 /* cut version from the extension */
10400 s = *fnamep + *fnamelen - 1;
10401 for ( ; s > *fnamep; --s)
10402 if (s[0] == ';')
10403 break;
10404 if (s > *fnamep)
10405 *fnamelen = s - *fnamep;
10406#endif
10407 }
10408 else if (*fnamep <= tail)
10409 *fnamelen = 0;
10410 }
10411 else /* :r */
10412 {
10413 if (s > tail) /* remove one extension */
10414 *fnamelen = (int)(s - *fnamep);
10415 }
10416 *usedlen += 2;
10417 }
10418
10419 /* ":s?pat?foo?" - substitute */
10420 /* ":gs?pat?foo?" - global substitute */
10421 if (src[*usedlen] == ':'
10422 && (src[*usedlen + 1] == 's'
10423 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10424 {
10425 char_u *str;
10426 char_u *pat;
10427 char_u *sub;
10428 int sep;
10429 char_u *flags;
10430 int didit = FALSE;
10431
10432 flags = (char_u *)"";
10433 s = src + *usedlen + 2;
10434 if (src[*usedlen + 1] == 'g')
10435 {
10436 flags = (char_u *)"g";
10437 ++s;
10438 }
10439
10440 sep = *s++;
10441 if (sep)
10442 {
10443 /* find end of pattern */
10444 p = vim_strchr(s, sep);
10445 if (p != NULL)
10446 {
10447 pat = vim_strnsave(s, (int)(p - s));
10448 if (pat != NULL)
10449 {
10450 s = p + 1;
10451 /* find end of substitution */
10452 p = vim_strchr(s, sep);
10453 if (p != NULL)
10454 {
10455 sub = vim_strnsave(s, (int)(p - s));
10456 str = vim_strnsave(*fnamep, *fnamelen);
10457 if (sub != NULL && str != NULL)
10458 {
10459 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010460 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 if (s != NULL)
10462 {
10463 *fnamep = s;
10464 *fnamelen = (int)STRLEN(s);
10465 vim_free(*bufp);
10466 *bufp = s;
10467 didit = TRUE;
10468 }
10469 }
10470 vim_free(sub);
10471 vim_free(str);
10472 }
10473 vim_free(pat);
10474 }
10475 }
10476 /* after using ":s", repeat all the modifiers */
10477 if (didit)
10478 goto repeat;
10479 }
10480 }
10481
Bram Moolenaar26df0922014-02-23 23:39:13 +010010482 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10483 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010484 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010485 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010486 if (c != NUL)
10487 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010488 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010489 if (c != NUL)
10490 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010491 if (p == NULL)
10492 return -1;
10493 vim_free(*bufp);
10494 *bufp = *fnamep = p;
10495 *fnamelen = (int)STRLEN(p);
10496 *usedlen += 2;
10497 }
10498
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 return valid;
10500}
10501
10502/*
10503 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010504 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 * "flags" can be "g" to do a global substitute.
10506 * Returns an allocated string, NULL for error.
10507 */
10508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010509do_string_sub(
10510 char_u *str,
10511 char_u *pat,
10512 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010513 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010514 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
10516 int sublen;
10517 regmatch_T regmatch;
10518 int i;
10519 int do_all;
10520 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010521 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 garray_T ga;
10523 char_u *ret;
10524 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010525 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526
10527 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10528 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010529 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
10531 ga_init2(&ga, 1, 200);
10532
10533 do_all = (flags[0] == 'g');
10534
10535 regmatch.rm_ic = p_ic;
10536 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10537 if (regmatch.regprog != NULL)
10538 {
10539 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010540 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10542 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010543 /* Skip empty match except for first match. */
10544 if (regmatch.startp[0] == regmatch.endp[0])
10545 {
10546 if (zero_width == regmatch.startp[0])
10547 {
10548 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010549 i = MB_PTR2LEN(tail);
10550 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10551 (size_t)i);
10552 ga.ga_len += i;
10553 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010554 continue;
10555 }
10556 zero_width = regmatch.startp[0];
10557 }
10558
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 /*
10560 * Get some space for a temporary buffer to do the substitution
10561 * into. It will contain:
10562 * - The text up to where the match is.
10563 * - The substituted text.
10564 * - The text after the match.
10565 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010566 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010567 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10569 {
10570 ga_clear(&ga);
10571 break;
10572 }
10573
10574 /* copy the text up to where the match is */
10575 i = (int)(regmatch.startp[0] - tail);
10576 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10577 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010578 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 + ga.ga_len + i, TRUE, TRUE, FALSE);
10580 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010581 tail = regmatch.endp[0];
10582 if (*tail == NUL)
10583 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 if (!do_all)
10585 break;
10586 }
10587
10588 if (ga.ga_data != NULL)
10589 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10590
Bram Moolenaar473de612013-06-08 18:19:48 +020010591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 }
10593
10594 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10595 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010596 if (p_cpo == empty_option)
10597 p_cpo = save_cpo;
10598 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010599 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010600 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601
10602 return ret;
10603}
10604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010605 static int
10606filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10607{
10608 typval_T rettv;
10609 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010610 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611
10612 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10613 argv[0] = vimvars[VV_KEY].vv_tv;
10614 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010615 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10616 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 if (map)
10618 {
10619 /* map(): replace the list item value */
10620 clear_tv(tv);
10621 rettv.v_lock = 0;
10622 *tv = rettv;
10623 }
10624 else
10625 {
10626 int error = FALSE;
10627
10628 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010629 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010630 clear_tv(&rettv);
10631 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010632 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 if (error)
10634 goto theend;
10635 }
10636 retval = OK;
10637theend:
10638 clear_tv(&vimvars[VV_VAL].vv_tv);
10639 return retval;
10640}
10641
10642
10643/*
10644 * Implementation of map() and filter().
10645 */
10646 void
10647filter_map(typval_T *argvars, typval_T *rettv, int map)
10648{
10649 typval_T *expr;
10650 listitem_T *li, *nli;
10651 list_T *l = NULL;
10652 dictitem_T *di;
10653 hashtab_T *ht;
10654 hashitem_T *hi;
10655 dict_T *d = NULL;
10656 typval_T save_val;
10657 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010658 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010659 int rem;
10660 int todo;
10661 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10662 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10663 : N_("filter() argument"));
10664 int save_did_emsg;
10665 int idx = 0;
10666
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010667 if (argvars[0].v_type == VAR_BLOB)
10668 {
10669 if ((b = argvars[0].vval.v_blob) == NULL)
10670 return;
10671 }
10672 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673 {
10674 if ((l = argvars[0].vval.v_list) == NULL
10675 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10676 return;
10677 }
10678 else if (argvars[0].v_type == VAR_DICT)
10679 {
10680 if ((d = argvars[0].vval.v_dict) == NULL
10681 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10682 return;
10683 }
10684 else
10685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010686 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 return;
10688 }
10689
10690 expr = &argvars[1];
10691 /* On type errors, the preceding call has already displayed an error
10692 * message. Avoid a misleading error message for an empty string that
10693 * was not passed as argument. */
10694 if (expr->v_type != VAR_UNKNOWN)
10695 {
10696 prepare_vimvar(VV_VAL, &save_val);
10697
10698 /* We reset "did_emsg" to be able to detect whether an error
10699 * occurred during evaluation of the expression. */
10700 save_did_emsg = did_emsg;
10701 did_emsg = FALSE;
10702
10703 prepare_vimvar(VV_KEY, &save_key);
10704 if (argvars[0].v_type == VAR_DICT)
10705 {
10706 vimvars[VV_KEY].vv_type = VAR_STRING;
10707
10708 ht = &d->dv_hashtab;
10709 hash_lock(ht);
10710 todo = (int)ht->ht_used;
10711 for (hi = ht->ht_array; todo > 0; ++hi)
10712 {
10713 if (!HASHITEM_EMPTY(hi))
10714 {
10715 int r;
10716
10717 --todo;
10718 di = HI2DI(hi);
10719 if (map &&
10720 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10721 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10722 break;
10723 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10724 r = filter_map_one(&di->di_tv, expr, map, &rem);
10725 clear_tv(&vimvars[VV_KEY].vv_tv);
10726 if (r == FAIL || did_emsg)
10727 break;
10728 if (!map && rem)
10729 {
10730 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10731 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10732 break;
10733 dictitem_remove(d, di);
10734 }
10735 }
10736 }
10737 hash_unlock(ht);
10738 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010739 else if (argvars[0].v_type == VAR_BLOB)
10740 {
10741 int i;
10742 typval_T tv;
10743
10744 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10745 for (i = 0; i < b->bv_ga.ga_len; i++)
10746 {
10747 tv.v_type = VAR_NUMBER;
10748 tv.vval.v_number = blob_get(b, i);
10749 vimvars[VV_KEY].vv_nr = idx;
10750 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10751 break;
10752 if (tv.v_type != VAR_NUMBER)
10753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010754 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010755 return;
10756 }
10757 tv.v_type = VAR_NUMBER;
10758 blob_set(b, i, tv.vval.v_number);
10759 if (!map && rem)
10760 {
10761 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10762
10763 mch_memmove(p + idx, p + i + 1,
10764 (size_t)b->bv_ga.ga_len - i - 1);
10765 --b->bv_ga.ga_len;
10766 --i;
10767 }
10768 }
10769 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 else
10771 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010772 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10774
10775 for (li = l->lv_first; li != NULL; li = nli)
10776 {
10777 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10778 break;
10779 nli = li->li_next;
10780 vimvars[VV_KEY].vv_nr = idx;
10781 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10782 || did_emsg)
10783 break;
10784 if (!map && rem)
10785 listitem_remove(l, li);
10786 ++idx;
10787 }
10788 }
10789
10790 restore_vimvar(VV_KEY, &save_key);
10791 restore_vimvar(VV_VAL, &save_val);
10792
10793 did_emsg |= save_did_emsg;
10794 }
10795
10796 copy_tv(&argvars[0], rettv);
10797}
10798
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */