blob: af1021bdc94e5370b8cf9012dbc867ca8fe72dc3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
248static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
249static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100255
256#if defined(EBCDIC) || defined(PROTO)
257/*
258 * Compare struct fst by function name.
259 */
260 static int
261compare_func_name(const void *s1, const void *s2)
262{
263 struct fst *p1 = (struct fst *)s1;
264 struct fst *p2 = (struct fst *)s2;
265
266 return STRCMP(p1->f_name, p2->f_name);
267}
268
269/*
270 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100271 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100272 * On machines using EBCDIC we have to sort it.
273 */
274 static void
275sortFunctions(void)
276{
277 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
278
279 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
280}
281#endif
282
283
Bram Moolenaar33570922005-01-25 22:26:29 +0000284/*
285 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000286 */
287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100288eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000289{
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 int i;
291 struct vimvar *p;
292
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200293 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
294 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200295 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000296 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299 for (i = 0; i < VV_LEN; ++i)
300 {
301 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 if (STRLEN(p->vv_name) > 16)
303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100304 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200305 getout(1);
306 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 STRCPY(p->vv_di.di_key, p->vv_name);
308 if (p->vv_flags & VV_RO)
309 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
310 else if (p->vv_flags & VV_RO_SBX)
311 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
312 else
313 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000314
315 /* add to v: scope dict, unless the value is not always available */
316 if (p->vv_type != VAR_UNKNOWN)
317 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000319 /* add to compat scope dict */
320 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000321 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100322 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
323
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000324 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100325 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100326 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100327 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100328 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329
330 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
331 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
332 set_vim_var_nr(VV_NONE, VVAL_NONE);
333 set_vim_var_nr(VV_NULL, VVAL_NULL);
334
Bram Moolenaarf562e722016-07-19 17:25:25 +0200335 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
336 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
337 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
338 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
339 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
340 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
341 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
342 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
343 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
344 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100345 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200346
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200347 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348
349#ifdef EBCDIC
350 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100351 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200352 */
353 sortFunctions();
354#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000355}
356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000357#if defined(EXITFREE) || defined(PROTO)
358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100359eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000360{
361 int i;
362 struct vimvar *p;
363
364 for (i = 0; i < VV_LEN; ++i)
365 {
366 p = &vimvars[i];
367 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100368 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000369 else if (p->vv_di.di_tv.v_type == VAR_LIST)
370 {
371 list_unref(p->vv_list);
372 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 }
375 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000376 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377 hash_clear(&compat_hashtab);
378
Bram Moolenaard9fba312005-06-26 22:34:35 +0000379 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100380# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200381 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100382# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000383
384 /* global variables */
385 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000386
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000387 /* autoloaded script names */
388 ga_clear_strings(&ga_loaded);
389
Bram Moolenaarcca74132013-09-25 21:00:28 +0200390 /* Script-local variables. First clear all the variables and in a second
391 * loop free the scriptvar_T, because a variable in one script might hold
392 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200395 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200396 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 ga_clear(&ga_scripts);
398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000399 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200400 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000401
402 /* functions */
403 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000404}
405#endif
406
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * Set an internal variable to a string value. Creates the variable if it does
410 * not already exist.
411 */
412 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100413set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000415 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000416 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418 val = vim_strsave(value);
419 if (val != NULL)
420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000424 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427 }
428}
429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200431#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000432static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433static char_u *redir_endp = NULL;
434static char_u *redir_varname = NULL;
435
436/*
437 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439 * Returns OK if successfully completed the setup. FAIL otherwise.
440 */
441 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100442var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000443{
444 int save_emsg;
445 int err;
446 typval_T tv;
447
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000448 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000449 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100451 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 return FAIL;
453 }
454
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000455 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000456 redir_varname = vim_strsave(name);
457 if (redir_varname == NULL)
458 return FAIL;
459
460 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
461 if (redir_lval == NULL)
462 {
463 var_redir_stop();
464 return FAIL;
465 }
466
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000467 /* The output is stored in growarray "redir_ga" until redirection ends. */
468 ga_init2(&redir_ga, (int)sizeof(char), 500);
469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100471 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000472 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
474 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200475 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 if (redir_endp != NULL && *redir_endp != NUL)
477 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100478 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100480 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000481 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482 var_redir_stop();
483 return FAIL;
484 }
485
486 /* check if we can write to the variable: set it to or append an empty
487 * string */
488 save_emsg = did_emsg;
489 did_emsg = FALSE;
490 tv.v_type = VAR_STRING;
491 tv.vval.v_string = (char_u *)"";
492 if (append)
493 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
494 else
495 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200496 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000498 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 if (err)
500 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000501 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 var_redir_stop();
503 return FAIL;
504 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000505
506 return OK;
507}
508
509/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000510 * Append "value[value_len]" to the variable set by var_redir_start().
511 * The actual appending is postponed until redirection ends, because the value
512 * appended may in fact be the string we write to, changing it may cause freed
513 * memory to be used:
514 * :redir => foo
515 * :let foo
516 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 */
518 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100519var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522
523 if (redir_lval == NULL)
524 return;
525
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000530
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 {
533 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000534 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000535 }
536 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538}
539
540/*
541 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547 typval_T tv;
548
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200549 if (EVALCMD_BUSY)
550 {
551 redir_lval = NULL;
552 return;
553 }
554
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000555 if (redir_lval != NULL)
556 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000557 /* If there was no error: assign the text to the variable. */
558 if (redir_endp != NULL)
559 {
560 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
561 tv.v_type = VAR_STRING;
562 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 /* Call get_lval() again, if it's inside a Dict or List it may
564 * have changed. */
565 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100566 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200567 if (redir_endp != NULL && redir_lval->ll_name != NULL)
568 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
569 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000570 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000572 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574
Bram Moolenaard23a8232018-02-10 18:45:26 +0100575 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100577 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100689 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100699/*
700 * Call eval1() and give an error message if not done at a lower level.
701 */
702 static int
703eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
704{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100705 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100706 int ret;
707 int did_emsg_before = did_emsg;
708 int called_emsg_before = called_emsg;
709
710 ret = eval1(arg, rettv, evaluate);
711 if (ret == FAIL)
712 {
713 // Report the invalid expression unless the expression evaluation has
714 // been cancelled due to an aborting error, an interrupt, or an
715 // exception, or we already gave a more specific error.
716 // Also check called_emsg for when using assert_fails().
717 if (!aborting() && did_emsg == did_emsg_before
718 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100719 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100720 }
721 return ret;
722}
723
Bram Moolenaar48570482017-10-30 21:48:41 +0100724 static int
725eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
726{
727 char_u *s;
728 int dummy;
729 char_u buf[NUMBUFLEN];
730
731 if (expr->v_type == VAR_FUNC)
732 {
733 s = expr->vval.v_string;
734 if (s == NULL || *s == NUL)
735 return FAIL;
736 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
737 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
738 return FAIL;
739 }
740 else if (expr->v_type == VAR_PARTIAL)
741 {
742 partial_T *partial = expr->vval.v_partial;
743
744 s = partial_name(partial);
745 if (s == NULL || *s == NUL)
746 return FAIL;
747 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
748 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
749 return FAIL;
750 }
751 else
752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100753 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100754 if (s == NULL)
755 return FAIL;
756 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100757 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100758 return FAIL;
759 if (*s != NUL) /* check for trailing chars after expr */
760 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200761 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100762 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100763 return FAIL;
764 }
765 }
766 return OK;
767}
768
769/*
770 * Like eval_to_bool() but using a typval_T instead of a string.
771 * Works for string, funcref and partial.
772 */
773 int
774eval_expr_to_bool(typval_T *expr, int *error)
775{
776 typval_T rettv;
777 int res;
778
779 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
780 {
781 *error = TRUE;
782 return FALSE;
783 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100784 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100785 clear_tv(&rettv);
786 return res;
787}
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789/*
790 * Top level evaluation function, returning a string. If "skip" is TRUE,
791 * only parsing to "nextcmd" is done, without reporting errors. Return
792 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
793 */
794 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100795eval_to_string_skip(
796 char_u *arg,
797 char_u **nextcmd,
798 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 char_u *retval;
802
803 if (skip)
804 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000805 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 retval = NULL;
807 else
808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100809 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000810 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 if (skip)
813 --emsg_skip;
814
815 return retval;
816}
817
818/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000819 * Skip over an expression at "*pp".
820 * Return FAIL for an error, OK otherwise.
821 */
822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000824{
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000826
827 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000828 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000829}
830
831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000833 * When "convert" is TRUE convert a List into a sequence of lines and convert
834 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * Return pointer to allocated memory, or NULL for failure.
836 */
837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838eval_to_string(
839 char_u *arg,
840 char_u **nextcmd,
841 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000846#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 retval = NULL;
852 else
853 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000854 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000855 {
856 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000857 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200858 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200859 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200860 if (tv.vval.v_list->lv_len > 0)
861 ga_append(&ga, NL);
862 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000863 ga_append(&ga, NUL);
864 retval = (char_u *)ga.ga_data;
865 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000866#ifdef FEAT_FLOAT
867 else if (convert && tv.v_type == VAR_FLOAT)
868 {
869 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
870 retval = vim_strsave(numbuf);
871 }
872#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000873 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100874 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877
878 return retval;
879}
880
881/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000882 * Call eval_to_string() without using current local variables and using
883 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 */
885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886eval_to_string_safe(
887 char_u *arg,
888 char_u **nextcmd,
889 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200892 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200894 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000895 if (use_sandbox)
896 ++sandbox;
897 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000898 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000899 if (use_sandbox)
900 --sandbox;
901 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200902 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return retval;
904}
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906/*
907 * Top level evaluation function, returning a number.
908 * Evaluates "expr" silently.
909 * Returns -1 for an error.
910 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200911 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913{
Bram Moolenaar33570922005-01-25 22:26:29 +0000914 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200915 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000916 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 ++emsg_off;
919
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000920 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = -1;
922 else
923 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100924 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927 --emsg_off;
928
929 return retval;
930}
931
Bram Moolenaara40058a2005-07-11 22:42:07 +0000932/*
933 * Prepare v: variable "idx" to be used.
934 * Save the current typeval in "save_tv".
935 * When not used yet add the variable to the v: hashtable.
936 */
937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100938prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000939{
940 *save_tv = vimvars[idx].vv_tv;
941 if (vimvars[idx].vv_type == VAR_UNKNOWN)
942 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
943}
944
945/*
946 * Restore v: variable "idx" to typeval "save_tv".
947 * When no longer defined, remove the variable from the v: hashtable.
948 */
949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100950restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000951{
952 hashitem_T *hi;
953
Bram Moolenaara40058a2005-07-11 22:42:07 +0000954 vimvars[idx].vv_tv = *save_tv;
955 if (vimvars[idx].vv_type == VAR_UNKNOWN)
956 {
957 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
958 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100959 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000960 else
961 hash_remove(&vimvarht, hi);
962 }
963}
964
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000965#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000966/*
967 * Evaluate an expression to a list with suggestions.
968 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000969 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000970 */
971 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000973{
974 typval_T save_val;
975 typval_T rettv;
976 list_T *list = NULL;
977 char_u *p = skipwhite(expr);
978
979 /* Set "v:val" to the bad word. */
980 prepare_vimvar(VV_VAL, &save_val);
981 vimvars[VV_VAL].vv_type = VAR_STRING;
982 vimvars[VV_VAL].vv_str = badword;
983 if (p_verbose == 0)
984 ++emsg_off;
985
986 if (eval1(&p, &rettv, TRUE) == OK)
987 {
988 if (rettv.v_type != VAR_LIST)
989 clear_tv(&rettv);
990 else
991 list = rettv.vval.v_list;
992 }
993
994 if (p_verbose == 0)
995 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000996 restore_vimvar(VV_VAL, &save_val);
997
998 return list;
999}
1000
1001/*
1002 * "list" is supposed to contain two items: a word and a number. Return the
1003 * word in "pp" and the number as the return value.
1004 * Return -1 if anything isn't right.
1005 * Used to get the good word and score from the eval_spell_expr() result.
1006 */
1007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001009{
1010 listitem_T *li;
1011
1012 li = list->lv_first;
1013 if (li == NULL)
1014 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001015 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001016
1017 li = li->li_next;
1018 if (li == NULL)
1019 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001020 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001021}
1022#endif
1023
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001024/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001025 * Top level evaluation function.
1026 * Returns an allocated typval_T with the result.
1027 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001028 */
1029 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001031{
1032 typval_T *tv;
1033
1034 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001035 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001036 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001037
1038 return tv;
1039}
1040
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001043 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001044 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1045 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001046 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049call_vim_function(
1050 char_u *func,
1051 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001052 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001053 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001056 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001058 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001059 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001061 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 if (ret == FAIL)
1063 clear_tv(rettv);
1064
1065 return ret;
1066}
1067
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001068/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001069 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001070 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001071 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1072 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001073 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001074 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001075call_func_retnr(
1076 char_u *func,
1077 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001078 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001079{
1080 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001081 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001082
Bram Moolenaarded27a12018-08-01 19:06:03 +02001083 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001084 return -1;
1085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001086 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001087 clear_tv(&rettv);
1088 return retval;
1089}
1090
1091#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1092 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1093
Bram Moolenaar4f688582007-07-24 12:34:30 +00001094# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001095/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001096 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001097 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001098 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1099 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 */
1101 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102call_func_retstr(
1103 char_u *func,
1104 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001105 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001106{
1107 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001108 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109
Bram Moolenaarded27a12018-08-01 19:06:03 +02001110 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001111 return NULL;
1112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001113 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 return retval;
1116}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001117# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001118
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001119/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001120 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001121 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1122 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001123 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001124 */
1125 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001126call_func_retlist(
1127 char_u *func,
1128 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001129 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001130{
1131 typval_T rettv;
1132
Bram Moolenaarded27a12018-08-01 19:06:03 +02001133 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001134 return NULL;
1135
1136 if (rettv.v_type != VAR_LIST)
1137 {
1138 clear_tv(&rettv);
1139 return NULL;
1140 }
1141
1142 return rettv.vval.v_list;
1143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145
Bram Moolenaar05159a02005-02-26 23:04:13 +00001146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#ifdef FEAT_FOLDING
1148/*
1149 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1150 * it in "*cp". Doesn't give error messages.
1151 */
1152 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001153eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
Bram Moolenaar33570922005-01-25 22:26:29 +00001155 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001156 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001158 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1159 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001162 if (use_sandbox)
1163 ++sandbox;
1164 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = 0;
1168 else
1169 {
1170 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 if (tv.v_type == VAR_NUMBER)
1172 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001173 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 retval = 0;
1175 else
1176 {
1177 /* If the result is a string, check if there is a non-digit before
1178 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (!VIM_ISDIGIT(*s) && *s != '-')
1181 *cp = *s++;
1182 retval = atol((char *)s);
1183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001187 if (use_sandbox)
1188 --sandbox;
1189 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001191 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192}
1193#endif
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 * ":let" list all variable values
1197 * ":let var1 var2" list variable values
1198 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001199 * ":let var += expr" assignment command.
1200 * ":let var -= expr" assignment command.
1201 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
1204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001205ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001209 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 int var_count = 0;
1212 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001213 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001214 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001215 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaardb552d602006-03-23 22:59:57 +00001217 argend = skip_var_list(arg, &var_count, &semicolon);
1218 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001219 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001220 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1221 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001222 expr = skipwhite(argend);
1223 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1224 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001226 /*
1227 * ":let" without "=": list variables
1228 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001229 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001230 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001231 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001233 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001236 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001237 list_glob_vars(&first);
1238 list_buf_vars(&first);
1239 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001240 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001241 list_script_vars(&first);
1242 list_func_vars(&first);
1243 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 eap->nextcmd = check_nextcmd(arg);
1246 }
1247 else
1248 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 op[0] = '=';
1250 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001251 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001252 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001253 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1254 op[0] = *expr; /* +=, -= or .= */
1255 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001256 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001257 else
1258 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (eap->skip)
1261 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001262 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (eap->skip)
1264 {
1265 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 --emsg_skip;
1268 }
1269 else if (i != FAIL)
1270 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 }
1276}
1277
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001278/*
1279 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1280 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 * When "nextchars" is not NULL it points to a string with characters that
1282 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1283 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001284 * Returns OK or FAIL;
1285 */
1286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287ex_let_vars(
1288 char_u *arg_start,
1289 typval_T *tv,
1290 int copy, /* copy values from "tv", don't move */
1291 int semicolon, /* from skip_var_list() */
1292 int var_count, /* from skip_var_list() */
1293 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001294{
1295 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001297 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 listitem_T *item;
1299 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001300
1301 if (*arg != '[')
1302 {
1303 /*
1304 * ":let var = expr" or ":for var in list"
1305 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001306 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307 return FAIL;
1308 return OK;
1309 }
1310
1311 /*
1312 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1313 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001314 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001316 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001317 return FAIL;
1318 }
1319
1320 i = list_len(l);
1321 if (semicolon == 0 && var_count < i)
1322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001323 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324 return FAIL;
1325 }
1326 if (var_count - semicolon > i)
1327 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001328 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001329 return FAIL;
1330 }
1331
1332 item = l->lv_first;
1333 while (*arg != ']')
1334 {
1335 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 item = item->li_next;
1338 if (arg == NULL)
1339 return FAIL;
1340
1341 arg = skipwhite(arg);
1342 if (*arg == ';')
1343 {
1344 /* Put the rest of the list (may be empty) in the var after ';'.
1345 * Create a new list for this. */
1346 l = list_alloc();
1347 if (l == NULL)
1348 return FAIL;
1349 while (item != NULL)
1350 {
1351 list_append_tv(l, &item->li_tv);
1352 item = item->li_next;
1353 }
1354
1355 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001356 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001357 ltv.vval.v_list = l;
1358 l->lv_refcount = 1;
1359
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1361 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001362 clear_tv(&ltv);
1363 if (arg == NULL)
1364 return FAIL;
1365 break;
1366 }
1367 else if (*arg != ',' && *arg != ']')
1368 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001369 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001370 return FAIL;
1371 }
1372 }
1373
1374 return OK;
1375}
1376
1377/*
1378 * Skip over assignable variable "var" or list of variables "[var, var]".
1379 * Used for ":let varvar = expr" and ":for varvar in expr".
1380 * For "[var, var]" increment "*var_count" for each variable.
1381 * for "[var, var; var]" set "semicolon".
1382 * Return NULL for an error.
1383 */
1384 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001385skip_var_list(
1386 char_u *arg,
1387 int *var_count,
1388 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001389{
1390 char_u *p, *s;
1391
1392 if (*arg == '[')
1393 {
1394 /* "[var, var]": find the matching ']'. */
1395 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001396 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001397 {
1398 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1399 s = skip_var_one(p);
1400 if (s == p)
1401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 return NULL;
1404 }
1405 ++*var_count;
1406
1407 p = skipwhite(s);
1408 if (*p == ']')
1409 break;
1410 else if (*p == ';')
1411 {
1412 if (*semicolon == 1)
1413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 return NULL;
1416 }
1417 *semicolon = 1;
1418 }
1419 else if (*p != ',')
1420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001421 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 return NULL;
1423 }
1424 }
1425 return p + 1;
1426 }
1427 else
1428 return skip_var_one(arg);
1429}
1430
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001431/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001432 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001433 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001434 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001436skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001438 if (*arg == '@' && arg[1] != NUL)
1439 return arg + 2;
1440 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1441 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442}
1443
Bram Moolenaara7043832005-01-21 11:56:39 +00001444/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001445 * List variables for hashtab "ht" with prefix "prefix".
1446 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001447 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001449list_hashtable_vars(
1450 hashtab_T *ht,
1451 char_u *prefix,
1452 int empty,
1453 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001454{
Bram Moolenaar33570922005-01-25 22:26:29 +00001455 hashitem_T *hi;
1456 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001457 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001458 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001459
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001460 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001461 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1462 {
1463 if (!HASHITEM_EMPTY(hi))
1464 {
1465 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001467
1468 // apply :filter /pat/ to variable name
1469 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1470 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1471 if (message_filtered(buf))
1472 continue;
1473
Bram Moolenaar33570922005-01-25 22:26:29 +00001474 if (empty || di->di_tv.v_type != VAR_STRING
1475 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001476 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001477 }
1478 }
1479}
1480
1481/*
1482 * List global variables.
1483 */
1484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001486{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001487 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001488}
1489
1490/*
1491 * List buffer variables.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001495{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001496 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001497 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001498}
1499
1500/*
1501 * List window variables.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001505{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001506 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001507 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001508}
1509
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001510/*
1511 * List tab page variables.
1512 */
1513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001515{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001516 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001517 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001518}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001519
Bram Moolenaara7043832005-01-21 11:56:39 +00001520/*
1521 * List Vim variables.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001526 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527}
1528
1529/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001530 * List script-local variables, if there is a script.
1531 */
1532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001534{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001535 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1536 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001537 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001538}
1539
1540/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001541 * List variables in "arg".
1542 */
1543 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001545{
1546 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001547 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001549 char_u *name_start;
1550 char_u *arg_subsc;
1551 char_u *tofree;
1552 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553
1554 while (!ends_excmd(*arg) && !got_int)
1555 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001556 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001558 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001559 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 {
1561 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001562 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 break;
1564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001565 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001568 /* get_name_len() takes care of expanding curly braces */
1569 name_start = name = arg;
1570 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1571 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001573 /* This is mainly to keep test 49 working: when expanding
1574 * curly braces fails overrule the exception error message. */
1575 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001578 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 break;
1580 }
1581 error = TRUE;
1582 }
1583 else
1584 {
1585 if (tofree != NULL)
1586 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001587 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589 else
1590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001591 /* handle d.key, l[idx], f(expr) */
1592 arg_subsc = arg;
1593 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001594 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001596 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001597 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001600 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001601 case 'g': list_glob_vars(first); break;
1602 case 'b': list_buf_vars(first); break;
1603 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001604 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001605 case 'v': list_vim_vars(first); break;
1606 case 's': list_script_vars(first); break;
1607 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001608 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001609 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001610 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001611 }
1612 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001613 {
1614 char_u numbuf[NUMBUFLEN];
1615 char_u *tf;
1616 int c;
1617 char_u *s;
1618
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001619 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001620 c = *arg;
1621 *arg = NUL;
1622 list_one_var_a((char_u *)"",
1623 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001624 tv.v_type,
1625 s == NULL ? (char_u *)"" : s,
1626 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 *arg = c;
1628 vim_free(tf);
1629 }
1630 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 }
1633 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634
1635 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001637
1638 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 }
1640
1641 return arg;
1642}
1643
1644/*
1645 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1646 * Returns a pointer to the char just after the var name.
1647 * Returns NULL if there is an error.
1648 */
1649 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001650ex_let_one(
1651 char_u *arg, /* points to variable name */
1652 typval_T *tv, /* value to assign to variable */
1653 int copy, /* copy value from "tv" */
1654 char_u *endchars, /* valid chars after variable name or NULL */
1655 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656{
1657 int c1;
1658 char_u *name;
1659 char_u *p;
1660 char_u *arg_end = NULL;
1661 int len;
1662 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664
1665 /*
1666 * ":let $VAR = expr": Set environment variable.
1667 */
1668 if (*arg == '$')
1669 {
1670 /* Find the end of the name. */
1671 ++arg;
1672 name = arg;
1673 len = get_env_len(&arg);
1674 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001675 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 else
1677 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001679 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001682 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001683 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 {
1685 c1 = name[len];
1686 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001687 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001688 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001689 {
1690 int mustfree = FALSE;
1691 char_u *s = vim_getenv(name, &mustfree);
1692
1693 if (s != NULL)
1694 {
1695 p = tofree = concat_str(s, p);
1696 if (mustfree)
1697 vim_free(s);
1698 }
1699 }
1700 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001703 if (STRICMP(name, "HOME") == 0)
1704 init_homedir();
1705 else if (didset_vim && STRICMP(name, "VIM") == 0)
1706 didset_vim = FALSE;
1707 else if (didset_vimruntime
1708 && STRICMP(name, "VIMRUNTIME") == 0)
1709 didset_vimruntime = FALSE;
1710 arg_end = arg;
1711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 }
1715 }
1716 }
1717
1718 /*
1719 * ":let &option = expr": Set option value.
1720 * ":let &l:option = expr": Set local option value.
1721 * ":let &g:option = expr": Set global option value.
1722 */
1723 else if (*arg == '&')
1724 {
1725 /* Find the end of the name. */
1726 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 if (p == NULL || (endchars != NULL
1728 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001729 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 else
1731 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 long n;
1733 int opt_type;
1734 long numval;
1735 char_u *stringval = NULL;
1736 char_u *s;
1737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 c1 = *p;
1739 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001741 n = (long)tv_get_number(tv);
1742 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001743 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001744 {
1745 opt_type = get_option_value(arg, &numval,
1746 &stringval, opt_flags);
1747 if ((opt_type == 1 && *op == '.')
1748 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001750 semsg(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001751 s = NULL; /* don't set the value */
1752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 else
1754 {
1755 if (opt_type == 1) /* number */
1756 {
1757 if (*op == '+')
1758 n = numval + n;
1759 else
1760 n = numval - n;
1761 }
1762 else if (opt_type == 0 && stringval != NULL) /* string */
1763 {
1764 s = concat_str(stringval, s);
1765 vim_free(stringval);
1766 stringval = s;
1767 }
1768 }
1769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 if (s != NULL)
1771 {
1772 set_option_value(arg, n, s, opt_flags);
1773 arg_end = p;
1774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 }
1778 }
1779
1780 /*
1781 * ":let @r = expr": Set register contents.
1782 */
1783 else if (*arg == '@')
1784 {
1785 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001786 if (op != NULL && (*op == '+' || *op == '-'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001788 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001790 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 else
1792 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001793 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 char_u *s;
1795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001796 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001798 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001799 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 if (s != NULL)
1801 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001802 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 vim_free(s);
1804 }
1805 }
1806 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001807 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001809 arg_end = arg + 1;
1810 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001811 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 }
1813 }
1814
1815 /*
1816 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001817 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001819 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001821 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001823 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001826 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001827 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001828 else
1829 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 arg_end = p;
1832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001834 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 }
1836
1837 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839
1840 return arg_end;
1841}
1842
1843/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001844 * Get an lval: variable, Dict item or List item that can be assigned a value
1845 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1846 * "name.key", "name.key[expr]" etc.
1847 * Indexing only works if "name" is an existing List or Dictionary.
1848 * "name" points to the start of the name.
1849 * If "rettv" is not NULL it points to the value to be assigned.
1850 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1851 * wrong; must end in space or cmd separator.
1852 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001853 * flags:
1854 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001855 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001856 * GLV_NO_AUTOLOAD: do not use script autoloading
1857 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001858 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001859 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001863get_lval(
1864 char_u *name,
1865 typval_T *rettv,
1866 lval_T *lp,
1867 int unlet,
1868 int skip,
1869 int flags, /* GLV_ values */
1870 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001873 char_u *expr_start, *expr_end;
1874 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001875 dictitem_T *v;
1876 typval_T var1;
1877 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001880 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001881 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001883 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001885 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001887
1888 if (skip)
1889 {
1890 /* When skipping just find the end of the name. */
1891 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001892 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001893 }
1894
1895 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001896 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 if (expr_start != NULL)
1898 {
1899 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001900 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 && *p != '[' && *p != '.')
1902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 return NULL;
1905 }
1906
1907 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1908 if (lp->ll_exp_name == NULL)
1909 {
1910 /* Report an invalid expression in braces, unless the
1911 * expression evaluation has been cancelled due to an
1912 * aborting error, an interrupt, or an exception. */
1913 if (!aborting() && !quiet)
1914 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001915 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001916 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 return NULL;
1918 }
1919 }
1920 lp->ll_name = lp->ll_exp_name;
1921 }
1922 else
1923 lp->ll_name = name;
1924
1925 /* Without [idx] or .key we are done. */
1926 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1927 return p;
1928
1929 cc = *p;
1930 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001931 /* Only pass &ht when we would write to the variable, it prevents autoload
1932 * as well. */
1933 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1934 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001935 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001936 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 if (v == NULL)
1939 return NULL;
1940
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 /*
1942 * Loop until no more [idx] or .key is following.
1943 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001945 var1.v_type = VAR_UNKNOWN;
1946 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001947 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1950 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001951 && lp->ll_tv->vval.v_dict != NULL)
1952 && !(lp->ll_tv->v_type == VAR_BLOB
1953 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001956 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001961 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001962 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001965
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 len = -1;
1967 if (*p == '.')
1968 {
1969 key = p + 1;
1970 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1971 ;
1972 if (len == 0)
1973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 }
1978 p = key + len;
1979 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001980 else
1981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001982 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001984 if (*p == ':')
1985 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001986 else
1987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001988 empty1 = FALSE;
1989 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001991 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 {
1993 /* not a number or string */
1994 clear_tv(&var1);
1995 return NULL;
1996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001997 }
1998
1999 /* Optionally get the second index [ :expr]. */
2000 if (*p == ':')
2001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002002 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002005 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002006 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002008 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002009 if (rettv != NULL
2010 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002011 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002012 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002013 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002015 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002017 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 }
2020 p = skipwhite(p + 1);
2021 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002023 else
2024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2027 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002028 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002029 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002031 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 {
2033 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002034 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002035 clear_tv(&var2);
2036 return NULL;
2037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002039 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002043
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002047 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002048 clear_tv(&var1);
2049 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002051 }
2052
2053 /* Skip to past ']'. */
2054 ++p;
2055 }
2056
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002058 {
2059 if (len == -1)
2060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002062 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002063 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002065 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 }
2068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 lp->ll_list = NULL;
2070 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002071 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002072
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002073 /* When assigning to a scope dictionary check that a function and
2074 * variable name is valid (only variable name unless it is l: or
2075 * g: dictionary). Disallow overwriting a builtin function. */
2076 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002077 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002078 int prevval;
2079 int wrong;
2080
2081 if (len != -1)
2082 {
2083 prevval = key[len];
2084 key[len] = NUL;
2085 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002086 else
2087 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002088 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2089 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002090 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002091 || !valid_varname(key);
2092 if (len != -1)
2093 key[len] = prevval;
2094 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002095 return NULL;
2096 }
2097
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002100 /* Can't add "v:" variable. */
2101 if (lp->ll_dict == &vimvardict)
2102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 semsg(_(e_illvar), name);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002104 return NULL;
2105 }
2106
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002107 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002111 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002112 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 }
2115 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002116 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002119 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002121 p = NULL;
2122 break;
2123 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002124 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002125 else if ((flags & GLV_READ_ONLY) == 0
2126 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002127 {
2128 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002129 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002130 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002131
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002132 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002135 else if (lp->ll_tv->v_type == VAR_BLOB)
2136 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002137 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2138
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002139 /*
2140 * Get the number and item for the only or first index of the List.
2141 */
2142 if (empty1)
2143 lp->ll_n1 = 0;
2144 else
2145 // is number or string
2146 lp->ll_n1 = (long)tv_get_number(&var1);
2147 clear_tv(&var1);
2148
2149 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002150 || lp->ll_n1 > bloblen
2151 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002152 {
2153 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002154 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002155 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002156 return NULL;
2157 }
2158 if (lp->ll_range && !lp->ll_empty2)
2159 {
2160 lp->ll_n2 = (long)tv_get_number(&var2);
2161 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002162 if (lp->ll_n2 < 0
2163 || lp->ll_n2 >= bloblen
2164 || lp->ll_n2 < lp->ll_n1)
2165 {
2166 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002167 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002168 return NULL;
2169 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002170 }
2171 lp->ll_blob = lp->ll_tv->vval.v_blob;
2172 lp->ll_tv = NULL;
2173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002174 else
2175 {
2176 /*
2177 * Get the number and item for the only or first index of the List.
2178 */
2179 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002181 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002182 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002183 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002184 clear_tv(&var1);
2185
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 lp->ll_list = lp->ll_tv->vval.v_list;
2188 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2189 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002191 if (lp->ll_n1 < 0)
2192 {
2193 lp->ll_n1 = 0;
2194 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2195 }
2196 }
2197 if (lp->ll_li == NULL)
2198 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002199 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002200 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 }
2204
2205 /*
2206 * May need to find the item or absolute index for the second
2207 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 * When no index given: "lp->ll_empty2" is TRUE.
2209 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002213 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002214 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002215 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002220 {
2221 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002222 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002224 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002226 }
2227
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2229 if (lp->ll_n1 < 0)
2230 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2231 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002232 {
2233 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002234 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002236 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002237 }
2238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 }
2242
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002243 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 return p;
2245}
2246
2247/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002251clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252{
2253 vim_free(lp->ll_exp_name);
2254 vim_free(lp->ll_newkey);
2255}
2256
2257/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002258 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002260 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 */
2262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002263set_var_lval(
2264 lval_T *lp,
2265 char_u *endp,
2266 typval_T *rettv,
2267 int copy,
2268 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269{
2270 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002271 listitem_T *ri;
2272 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273
2274 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002276 cc = *endp;
2277 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002278 if (lp->ll_blob != NULL)
2279 {
2280 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002281
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 if (op != NULL && *op != '=')
2283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002285 return;
2286 }
2287
2288 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2289 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002290 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002291
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002292 if (lp->ll_empty2)
2293 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2294
2295 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002297 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002298 return;
2299 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 if (lp->ll_empty2)
2301 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002302
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002303 ir = 0;
2304 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2305 blob_set(lp->ll_blob, il,
2306 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002307 }
2308 else
2309 {
2310 val = (int)tv_get_number_chk(rettv, &error);
2311 if (!error)
2312 {
2313 garray_T *gap = &lp->ll_blob->bv_ga;
2314
2315 // Allow for appending a byte. Setting a byte beyond
2316 // the end is an error otherwise.
2317 if (lp->ll_n1 < gap->ga_len
2318 || (lp->ll_n1 == gap->ga_len
2319 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2320 {
2321 blob_set(lp->ll_blob, lp->ll_n1, val);
2322 if (lp->ll_n1 == gap->ga_len)
2323 ++gap->ga_len;
2324 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002325 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002326 }
2327 }
2328 }
2329 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002331 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332
Bram Moolenaar79518e22017-02-17 16:31:35 +01002333 /* handle +=, -= and .= */
2334 di = NULL;
2335 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2336 &tv, &di, TRUE, FALSE) == OK)
2337 {
2338 if ((di == NULL
2339 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2340 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2341 FALSE)))
2342 && tv_op(&tv, rettv, op) == OK)
2343 set_var(lp->ll_name, &tv, FALSE);
2344 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002347 else
2348 set_var(lp->ll_name, rettv, copy);
2349 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002351 else if (tv_check_lock(lp->ll_newkey == NULL
2352 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002353 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002354 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 else if (lp->ll_range)
2356 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002357 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002358 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002359
2360 /*
2361 * Check whether any of the list items is locked
2362 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002363 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002364 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002365 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002366 return;
2367 ri = ri->li_next;
2368 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2369 break;
2370 ll_li = ll_li->li_next;
2371 ++ll_n1;
2372 }
2373
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 /*
2375 * Assign the List values to the list items.
2376 */
2377 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 if (op != NULL && *op != '=')
2380 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2381 else
2382 {
2383 clear_tv(&lp->ll_li->li_tv);
2384 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2385 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 ri = ri->li_next;
2387 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2388 break;
2389 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002390 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002392 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 ri = NULL;
2395 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002396 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002397 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 lp->ll_li = lp->ll_li->li_next;
2399 ++lp->ll_n1;
2400 }
2401 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002402 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 else if (lp->ll_empty2
2404 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002406 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 }
2408 else
2409 {
2410 /*
2411 * Assign to a List or Dictionary item.
2412 */
2413 if (lp->ll_newkey != NULL)
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && *op != '=')
2416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002417 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 return;
2419 }
2420
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002422 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (di == NULL)
2424 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002425 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2426 {
2427 vim_free(di);
2428 return;
2429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 else if (op != NULL && *op != '=')
2433 {
2434 tv_op(lp->ll_tv, rettv, op);
2435 return;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 /*
2441 * Assign the value to the variable or list item.
2442 */
2443 if (copy)
2444 copy_tv(rettv, lp->ll_tv);
2445 else
2446 {
2447 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002448 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 }
2451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452}
2453
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2456 * Returns OK or FAIL.
2457 */
2458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002459tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002461 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 char_u numbuf[NUMBUFLEN];
2463 char_u *s;
2464
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002465 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2466 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2467 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 {
2469 switch (tv1->v_type)
2470 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002471 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 case VAR_DICT:
2473 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002474 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002475 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002476 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002477 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 break;
2479
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002480 case VAR_BLOB:
2481 if (*op != '+' || tv2->v_type != VAR_BLOB)
2482 break;
2483 // BLOB += BLOB
2484 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2485 {
2486 blob_T *b1 = tv1->vval.v_blob;
2487 blob_T *b2 = tv2->vval.v_blob;
2488 int i, len = blob_len(b2);
2489 for (i = 0; i < len; i++)
2490 ga_append(&b1->bv_ga, blob_get(b2, i));
2491 }
2492 return OK;
2493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 case VAR_LIST:
2495 if (*op != '+' || tv2->v_type != VAR_LIST)
2496 break;
2497 /* List += List */
2498 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2499 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2500 return OK;
2501
2502 case VAR_NUMBER:
2503 case VAR_STRING:
2504 if (tv2->v_type == VAR_LIST)
2505 break;
2506 if (*op == '+' || *op == '-')
2507 {
2508 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002509 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002510#ifdef FEAT_FLOAT
2511 if (tv2->v_type == VAR_FLOAT)
2512 {
2513 float_T f = n;
2514
2515 if (*op == '+')
2516 f += tv2->vval.v_float;
2517 else
2518 f -= tv2->vval.v_float;
2519 clear_tv(tv1);
2520 tv1->v_type = VAR_FLOAT;
2521 tv1->vval.v_float = f;
2522 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524#endif
2525 {
2526 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002527 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002528 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002529 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002530 clear_tv(tv1);
2531 tv1->v_type = VAR_NUMBER;
2532 tv1->vval.v_number = n;
2533 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 }
2535 else
2536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 if (tv2->v_type == VAR_FLOAT)
2538 break;
2539
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002540 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 s = tv_get_string(tv1);
2542 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 clear_tv(tv1);
2544 tv1->v_type = VAR_STRING;
2545 tv1->vval.v_string = s;
2546 }
2547 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002548
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002549 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002550#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002551 {
2552 float_T f;
2553
2554 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2555 && tv2->v_type != VAR_NUMBER
2556 && tv2->v_type != VAR_STRING))
2557 break;
2558 if (tv2->v_type == VAR_FLOAT)
2559 f = tv2->vval.v_float;
2560 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002561 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562 if (*op == '+')
2563 tv1->vval.v_float += f;
2564 else
2565 tv1->vval.v_float -= f;
2566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002567#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002568 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 }
2570 }
2571
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002572 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002573 return FAIL;
2574}
2575
2576/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002577 * Evaluate the expression used in a ":for var in expr" command.
2578 * "arg" points to "var".
2579 * Set "*errp" to TRUE for an error, FALSE otherwise;
2580 * Return a pointer that holds the info. Null when there is an error.
2581 */
2582 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583eval_for_line(
2584 char_u *arg,
2585 int *errp,
2586 char_u **nextcmdp,
2587 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002588{
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002590 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 typval_T tv;
2592 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002593 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594
2595 *errp = TRUE; /* default: there is an error */
2596
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002598 if (fi == NULL)
2599 return NULL;
2600
2601 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2602 if (expr == NULL)
2603 return fi;
2604
2605 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002606 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002609 return fi;
2610 }
2611
2612 if (skip)
2613 ++emsg_skip;
2614 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2615 {
2616 *errp = FALSE;
2617 if (!skip)
2618 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002619 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002620 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002621 l = tv.vval.v_list;
2622 if (l == NULL)
2623 {
2624 // a null list is like an empty list: do nothing
2625 clear_tv(&tv);
2626 }
2627 else
2628 {
2629 // No need to increment the refcount, it's already set for
2630 // the list being used in "tv".
2631 fi->fi_list = l;
2632 list_add_watch(l, &fi->fi_lw);
2633 fi->fi_lw.lw_item = l->lv_first;
2634 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002635 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002636 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002637 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002638 b = tv.vval.v_blob;
2639 if (b == NULL)
2640 clear_tv(&tv);
2641 else
2642 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002643 // No need to increment the refcount, it's already set for
2644 // the blob being used in "tv".
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002645 fi->fi_blob = b;
2646 fi->fi_bi = 0;
2647 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002648 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649 else
2650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002652 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002653 }
2654 }
2655 }
2656 if (skip)
2657 --emsg_skip;
2658
2659 return fi;
2660}
2661
2662/*
2663 * Use the first item in a ":for" list. Advance to the next.
2664 * Assign the values to the variable (list). "arg" points to the first one.
2665 * Return TRUE when a valid item was found, FALSE when at end of list or
2666 * something wrong.
2667 */
2668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002669next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002670{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002671 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002674
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002675 if (fi->fi_blob != NULL)
2676 {
2677 typval_T tv;
2678
2679 if (fi->fi_bi >= blob_len(fi->fi_blob))
2680 return FALSE;
2681 tv.v_type = VAR_NUMBER;
2682 tv.v_lock = VAR_FIXED;
2683 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2684 ++fi->fi_bi;
2685 return ex_let_vars(arg, &tv, TRUE,
2686 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2687 }
2688
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002689 item = fi->fi_lw.lw_item;
2690 if (item == NULL)
2691 result = FALSE;
2692 else
2693 {
2694 fi->fi_lw.lw_item = item->li_next;
2695 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2696 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2697 }
2698 return result;
2699}
2700
2701/*
2702 * Free the structure used to store info used by ":for".
2703 */
2704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002705free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706{
Bram Moolenaar33570922005-01-25 22:26:29 +00002707 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002708
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002709 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002710 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002711 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002712 list_unref(fi->fi_list);
2713 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002714 if (fi != NULL && fi->fi_blob != NULL)
2715 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002716 vim_free(fi);
2717}
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2720
2721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002722set_context_for_expression(
2723 expand_T *xp,
2724 char_u *arg,
2725 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 int got_eq = FALSE;
2728 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002729 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002731 if (cmdidx == CMD_let)
2732 {
2733 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002734 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002735 {
2736 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002737 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738 {
2739 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002740 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002741 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742 break;
2743 }
2744 return;
2745 }
2746 }
2747 else
2748 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2749 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 while ((xp->xp_pattern = vim_strpbrk(arg,
2751 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2752 {
2753 c = *xp->xp_pattern;
2754 if (c == '&')
2755 {
2756 c = xp->xp_pattern[1];
2757 if (c == '&')
2758 {
2759 ++xp->xp_pattern;
2760 xp->xp_context = cmdidx != CMD_let || got_eq
2761 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2762 }
2763 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002766 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2767 xp->xp_pattern += 2;
2768
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else if (c == '$')
2772 {
2773 /* environment variable */
2774 xp->xp_context = EXPAND_ENV_VARS;
2775 }
2776 else if (c == '=')
2777 {
2778 got_eq = TRUE;
2779 xp->xp_context = EXPAND_EXPRESSION;
2780 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002781 else if (c == '#'
2782 && xp->xp_context == EXPAND_EXPRESSION)
2783 {
2784 /* Autoload function/variable contains '#'. */
2785 break;
2786 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002787 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 && xp->xp_context == EXPAND_FUNCTIONS
2789 && vim_strchr(xp->xp_pattern, '(') == NULL)
2790 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002791 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 break;
2793 }
2794 else if (cmdidx != CMD_let || got_eq)
2795 {
2796 if (c == '"') /* string */
2797 {
2798 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2799 if (c == '\\' && xp->xp_pattern[1] != NUL)
2800 ++xp->xp_pattern;
2801 xp->xp_context = EXPAND_NOTHING;
2802 }
2803 else if (c == '\'') /* literal string */
2804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2807 /* skip */ ;
2808 xp->xp_context = EXPAND_NOTHING;
2809 }
2810 else if (c == '|')
2811 {
2812 if (xp->xp_pattern[1] == '|')
2813 {
2814 ++xp->xp_pattern;
2815 xp->xp_context = EXPAND_EXPRESSION;
2816 }
2817 else
2818 xp->xp_context = EXPAND_COMMANDS;
2819 }
2820 else
2821 xp->xp_context = EXPAND_EXPRESSION;
2822 }
2823 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002824 /* Doesn't look like something valid, expand as an expression
2825 * anyway. */
2826 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 arg = xp->xp_pattern;
2828 if (*arg != NUL)
2829 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2830 /* skip */ ;
2831 }
2832 xp->xp_pattern = arg;
2833}
2834
2835#endif /* FEAT_CMDL_COMPL */
2836
2837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 * ":unlet[!] var1 ... " command.
2839 */
2840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002843 ex_unletlock(eap, eap->arg, 0);
2844}
2845
2846/*
2847 * ":lockvar" and ":unlockvar" commands
2848 */
2849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002850ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002853 int deep = 2;
2854
2855 if (eap->forceit)
2856 deep = -1;
2857 else if (vim_isdigit(*arg))
2858 {
2859 deep = getdigits(&arg);
2860 arg = skipwhite(arg);
2861 }
2862
2863 ex_unletlock(eap, arg, deep);
2864}
2865
2866/*
2867 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2868 */
2869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002870ex_unletlock(
2871 exarg_T *eap,
2872 char_u *argstart,
2873 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874{
2875 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002878 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 do
2881 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002882 if (*arg == '$')
2883 {
2884 char_u *name = ++arg;
2885
2886 if (get_env_len(&arg) == 0)
2887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002888 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002889 return;
2890 }
2891 vim_unsetenv(name);
2892 arg = skipwhite(arg);
2893 continue;
2894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002897 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002898 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (lv.ll_name == NULL)
2900 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002901 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (name_end != NULL)
2905 {
2906 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002907 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 }
2909 if (!(eap->skip || error))
2910 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 break;
2912 }
2913
2914 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 {
2916 if (eap->cmdidx == CMD_unlet)
2917 {
2918 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2919 error = TRUE;
2920 }
2921 else
2922 {
2923 if (do_lock_var(&lv, name_end, deep,
2924 eap->cmdidx == CMD_lockvar) == FAIL)
2925 error = TRUE;
2926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 if (!eap->skip)
2930 clear_lval(&lv);
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 arg = skipwhite(name_end);
2933 } while (!ends_excmd(*arg));
2934
2935 eap->nextcmd = check_nextcmd(arg);
2936}
2937
Bram Moolenaar8c711452005-01-14 21:53:12 +00002938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939do_unlet_var(
2940 lval_T *lp,
2941 char_u *name_end,
2942 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002943{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 int ret = OK;
2945 int cc;
2946
2947 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 cc = *name_end;
2950 *name_end = NUL;
2951
2952 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002953 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002956 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002957 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002958 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002959 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002960 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 else if (lp->ll_range)
2963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002966 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002967
2968 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2969 {
2970 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002971 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 return FAIL;
2973 ll_li = li;
2974 ++ll_n1;
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976
2977 /* Delete a range of List items. */
2978 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2979 {
2980 li = lp->ll_li->li_next;
2981 listitem_remove(lp->ll_list, lp->ll_li);
2982 lp->ll_li = li;
2983 ++lp->ll_n1;
2984 }
2985 }
2986 else
2987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /* unlet a List item. */
2990 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002993 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 }
2995
2996 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999/*
3000 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003001 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
3003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003004do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 hashtab_T *ht;
3007 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003008 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003009 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003010 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 ht = find_var_ht(name, &varname);
3013 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003016 if (d == NULL)
3017 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018 if (ht == &globvarht)
3019 d = &globvardict;
3020 else if (ht == &compat_hashtab)
3021 d = &vimvardict;
3022 else
3023 {
3024 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3025 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3026 }
3027 if (d == NULL)
3028 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003029 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 return FAIL;
3031 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003033 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003034 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003035 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003036 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003037 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003038 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003039 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003040 || var_check_ro(di->di_flags, name, FALSE)
3041 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003042 return FAIL;
3043
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003044 delete_var(ht, hi);
3045 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003048 if (forceit)
3049 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 return FAIL;
3052}
3053
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003054/*
3055 * Lock or unlock variable indicated by "lp".
3056 * "deep" is the levels to go (-1 for unlimited);
3057 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3058 */
3059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060do_lock_var(
3061 lval_T *lp,
3062 char_u *name_end,
3063 int deep,
3064 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003065{
3066 int ret = OK;
3067 int cc;
3068 dictitem_T *di;
3069
3070 if (deep == 0) /* nothing to do */
3071 return OK;
3072
3073 if (lp->ll_tv == NULL)
3074 {
3075 cc = *name_end;
3076 *name_end = NUL;
3077
3078 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003079 di = find_var(lp->ll_name, NULL, TRUE);
3080 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003081 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003082 else if ((di->di_flags & DI_FLAGS_FIX)
3083 && di->di_tv.v_type != VAR_DICT
3084 && di->di_tv.v_type != VAR_LIST)
3085 /* For historic reasons this error is not given for a list or dict.
3086 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003087 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003088 else
3089 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003090 if (lock)
3091 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003092 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003093 di->di_flags &= ~DI_FLAGS_LOCK;
3094 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003095 }
3096 *name_end = cc;
3097 }
3098 else if (lp->ll_range)
3099 {
3100 listitem_T *li = lp->ll_li;
3101
3102 /* (un)lock a range of List items. */
3103 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3104 {
3105 item_lock(&li->li_tv, deep, lock);
3106 li = li->li_next;
3107 ++lp->ll_n1;
3108 }
3109 }
3110 else if (lp->ll_list != NULL)
3111 /* (un)lock a List item. */
3112 item_lock(&lp->ll_li->li_tv, deep, lock);
3113 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003114 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 item_lock(&lp->ll_di->di_tv, deep, lock);
3116
3117 return ret;
3118}
3119
3120/*
3121 * Lock or unlock an item. "deep" is nr of levels to go.
3122 */
3123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003124item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003125{
3126 static int recurse = 0;
3127 list_T *l;
3128 listitem_T *li;
3129 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003130 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003131 hashitem_T *hi;
3132 int todo;
3133
3134 if (recurse >= DICT_MAXNEST)
3135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003136 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003137 return;
3138 }
3139 if (deep == 0)
3140 return;
3141 ++recurse;
3142
3143 /* lock/unlock the item itself */
3144 if (lock)
3145 tv->v_lock |= VAR_LOCKED;
3146 else
3147 tv->v_lock &= ~VAR_LOCKED;
3148
3149 switch (tv->v_type)
3150 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003151 case VAR_UNKNOWN:
3152 case VAR_NUMBER:
3153 case VAR_STRING:
3154 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003155 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003156 case VAR_FLOAT:
3157 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003158 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003159 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003160 break;
3161
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003162 case VAR_BLOB:
3163 if ((b = tv->vval.v_blob) != NULL)
3164 {
3165 if (lock)
3166 b->bv_lock |= VAR_LOCKED;
3167 else
3168 b->bv_lock &= ~VAR_LOCKED;
3169 }
3170 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003171 case VAR_LIST:
3172 if ((l = tv->vval.v_list) != NULL)
3173 {
3174 if (lock)
3175 l->lv_lock |= VAR_LOCKED;
3176 else
3177 l->lv_lock &= ~VAR_LOCKED;
3178 if (deep < 0 || deep > 1)
3179 /* recursive: lock/unlock the items the List contains */
3180 for (li = l->lv_first; li != NULL; li = li->li_next)
3181 item_lock(&li->li_tv, deep - 1, lock);
3182 }
3183 break;
3184 case VAR_DICT:
3185 if ((d = tv->vval.v_dict) != NULL)
3186 {
3187 if (lock)
3188 d->dv_lock |= VAR_LOCKED;
3189 else
3190 d->dv_lock &= ~VAR_LOCKED;
3191 if (deep < 0 || deep > 1)
3192 {
3193 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003194 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003195 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3196 {
3197 if (!HASHITEM_EMPTY(hi))
3198 {
3199 --todo;
3200 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3201 }
3202 }
3203 }
3204 }
3205 }
3206 --recurse;
3207}
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3210/*
3211 * Delete all "menutrans_" variables.
3212 */
3213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003217 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003220 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003222 {
3223 if (!HASHITEM_EMPTY(hi))
3224 {
3225 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3227 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003228 }
3229 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231}
3232#endif
3233
3234#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3235
3236/*
3237 * Local string buffer for the next two functions to store a variable name
3238 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3239 * get_user_var_name().
3240 */
3241
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242static char_u *varnamebuf = NULL;
3243static int varnamebuflen = 0;
3244
3245/*
3246 * Function to concatenate a prefix and a variable name.
3247 */
3248 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003249cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 int len;
3252
3253 len = (int)STRLEN(name) + 3;
3254 if (len > varnamebuflen)
3255 {
3256 vim_free(varnamebuf);
3257 len += 10; /* some additional space */
3258 varnamebuf = alloc(len);
3259 if (varnamebuf == NULL)
3260 {
3261 varnamebuflen = 0;
3262 return NULL;
3263 }
3264 varnamebuflen = len;
3265 }
3266 *varnamebuf = prefix;
3267 varnamebuf[1] = ':';
3268 STRCPY(varnamebuf + 2, name);
3269 return varnamebuf;
3270}
3271
3272/*
3273 * Function given to ExpandGeneric() to obtain the list of user defined
3274 * (global/buffer/window/built-in) variable names.
3275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003277get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003279 static long_u gdone;
3280 static long_u bdone;
3281 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003282 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003283 static int vidx;
3284 static hashitem_T *hi;
3285 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003288 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003289 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003291 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003292
3293 /* Global variables */
3294 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003296 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003298 else
3299 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003300 while (HASHITEM_EMPTY(hi))
3301 ++hi;
3302 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3303 return cat_prefix_varname('g', hi->hi_key);
3304 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003306
3307 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003308 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003311 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003312 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003313 else
3314 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003315 while (HASHITEM_EMPTY(hi))
3316 ++hi;
3317 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003319
3320 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003321 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003324 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003326 else
3327 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003328 while (HASHITEM_EMPTY(hi))
3329 ++hi;
3330 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003332
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003333 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003334 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003335 if (tdone < ht->ht_used)
3336 {
3337 if (tdone++ == 0)
3338 hi = ht->ht_array;
3339 else
3340 ++hi;
3341 while (HASHITEM_EMPTY(hi))
3342 ++hi;
3343 return cat_prefix_varname('t', hi->hi_key);
3344 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003345
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 /* v: variables */
3347 if (vidx < VV_LEN)
3348 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaard23a8232018-02-10 18:45:26 +01003350 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 varnamebuflen = 0;
3352 return NULL;
3353}
3354
3355#endif /* FEAT_CMDL_COMPL */
3356
3357/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003358 * Return TRUE if "pat" matches "text".
3359 * Does not use 'cpo' and always uses 'magic'.
3360 */
3361 static int
3362pattern_match(char_u *pat, char_u *text, int ic)
3363{
3364 int matches = FALSE;
3365 char_u *save_cpo;
3366 regmatch_T regmatch;
3367
3368 /* avoid 'l' flag in 'cpoptions' */
3369 save_cpo = p_cpo;
3370 p_cpo = (char_u *)"";
3371 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3372 if (regmatch.regprog != NULL)
3373 {
3374 regmatch.rm_ic = ic;
3375 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3376 vim_regfree(regmatch.regprog);
3377 }
3378 p_cpo = save_cpo;
3379 return matches;
3380}
3381
3382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3386 */
3387
3388/*
3389 * Handle zero level expression.
3390 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003391 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003392 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Return OK or FAIL.
3394 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003396eval0(
3397 char_u *arg,
3398 typval_T *rettv,
3399 char_u **nextcmd,
3400 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 int ret;
3403 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003404 int did_emsg_before = did_emsg;
3405 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (ret == FAIL || !ends_excmd(*p))
3410 {
3411 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 /*
3414 * Report the invalid expression unless the expression evaluation has
3415 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003416 * exception, or we already gave a more specific error.
3417 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003419 if (!aborting() && did_emsg == did_emsg_before
3420 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003421 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ret = FAIL;
3423 }
3424 if (nextcmd != NULL)
3425 *nextcmd = check_nextcmd(p);
3426
3427 return ret;
3428}
3429
3430/*
3431 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003432 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 *
3434 * "arg" must point to the first non-white of the expression.
3435 * "arg" is advanced to the next non-white after the recognized expression.
3436 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003437 * Note: "rettv.v_lock" is not set.
3438 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Return OK or FAIL.
3440 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003442eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /*
3448 * Get the first variable.
3449 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return FAIL;
3452
3453 if ((*arg)[0] == '?')
3454 {
3455 result = FALSE;
3456 if (evaluate)
3457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003458 int error = FALSE;
3459
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003460 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003463 if (error)
3464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466
3467 /*
3468 * Get the second variable.
3469 */
3470 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473
3474 /*
3475 * Check for the ":".
3476 */
3477 if ((*arg)[0] != ':')
3478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003479 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return FAIL;
3483 }
3484
3485 /*
3486 * Get the third variable.
3487 */
3488 *arg = skipwhite(*arg + 1);
3489 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3490 {
3491 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return FAIL;
3494 }
3495 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
3498
3499 return OK;
3500}
3501
3502/*
3503 * Handle first level expression:
3504 * expr2 || expr2 || expr2 logical OR
3505 *
3506 * "arg" must point to the first non-white of the expression.
3507 * "arg" is advanced to the next non-white after the recognized expression.
3508 *
3509 * Return OK or FAIL.
3510 */
3511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
Bram Moolenaar33570922005-01-25 22:26:29 +00003514 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 long result;
3516 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 /*
3520 * Get the first variable.
3521 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 return FAIL;
3524
3525 /*
3526 * Repeat until there is no following "||".
3527 */
3528 first = TRUE;
3529 result = FALSE;
3530 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3531 {
3532 if (evaluate && first)
3533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003534 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003536 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003537 if (error)
3538 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 first = FALSE;
3540 }
3541
3542 /*
3543 * Get the second variable.
3544 */
3545 *arg = skipwhite(*arg + 2);
3546 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3547 return FAIL;
3548
3549 /*
3550 * Compute the result.
3551 */
3552 if (evaluate && !result)
3553 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003554 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003557 if (error)
3558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 if (evaluate)
3561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 rettv->v_type = VAR_NUMBER;
3563 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
3566
3567 return OK;
3568}
3569
3570/*
3571 * Handle second level expression:
3572 * expr3 && expr3 && expr3 logical AND
3573 *
3574 * "arg" must point to the first non-white of the expression.
3575 * "arg" is advanced to the next non-white after the recognized expression.
3576 *
3577 * Return OK or FAIL.
3578 */
3579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003580eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
Bram Moolenaar33570922005-01-25 22:26:29 +00003582 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 long result;
3584 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003585 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 /*
3588 * Get the first variable.
3589 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003590 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return FAIL;
3592
3593 /*
3594 * Repeat until there is no following "&&".
3595 */
3596 first = TRUE;
3597 result = TRUE;
3598 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3599 {
3600 if (evaluate && first)
3601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003602 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003605 if (error)
3606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 first = FALSE;
3608 }
3609
3610 /*
3611 * Get the second variable.
3612 */
3613 *arg = skipwhite(*arg + 2);
3614 if (eval4(arg, &var2, evaluate && result) == FAIL)
3615 return FAIL;
3616
3617 /*
3618 * Compute the result.
3619 */
3620 if (evaluate && result)
3621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003622 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003624 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003625 if (error)
3626 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628 if (evaluate)
3629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003630 rettv->v_type = VAR_NUMBER;
3631 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 }
3634
3635 return OK;
3636}
3637
3638/*
3639 * Handle third level expression:
3640 * var1 == var2
3641 * var1 =~ var2
3642 * var1 != var2
3643 * var1 !~ var2
3644 * var1 > var2
3645 * var1 >= var2
3646 * var1 < var2
3647 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003648 * var1 is var2
3649 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 *
3651 * "arg" must point to the first non-white of the expression.
3652 * "arg" is advanced to the next non-white after the recognized expression.
3653 *
3654 * Return OK or FAIL.
3655 */
3656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 char_u *p;
3661 int i;
3662 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003663 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 /*
3668 * Get the first variable.
3669 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672
3673 p = *arg;
3674 switch (p[0])
3675 {
3676 case '=': if (p[1] == '=')
3677 type = TYPE_EQUAL;
3678 else if (p[1] == '~')
3679 type = TYPE_MATCH;
3680 break;
3681 case '!': if (p[1] == '=')
3682 type = TYPE_NEQUAL;
3683 else if (p[1] == '~')
3684 type = TYPE_NOMATCH;
3685 break;
3686 case '>': if (p[1] != '=')
3687 {
3688 type = TYPE_GREATER;
3689 len = 1;
3690 }
3691 else
3692 type = TYPE_GEQUAL;
3693 break;
3694 case '<': if (p[1] != '=')
3695 {
3696 type = TYPE_SMALLER;
3697 len = 1;
3698 }
3699 else
3700 type = TYPE_SEQUAL;
3701 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003702 case 'i': if (p[1] == 's')
3703 {
3704 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3705 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003706 i = p[len];
3707 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003708 {
3709 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3710 type_is = TRUE;
3711 }
3712 }
3713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715
3716 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003717 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
3719 if (type != TYPE_UNKNOWN)
3720 {
3721 /* extra question mark appended: ignore case */
3722 if (p[len] == '?')
3723 {
3724 ic = TRUE;
3725 ++len;
3726 }
3727 /* extra '#' appended: match case */
3728 else if (p[len] == '#')
3729 {
3730 ic = FALSE;
3731 ++len;
3732 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 else
3735 ic = p_ic;
3736
3737 /*
3738 * Get the second variable.
3739 */
3740 *arg = skipwhite(p + len);
3741 if (eval5(arg, &var2, evaluate) == FAIL)
3742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 return FAIL;
3745 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003746 if (evaluate)
3747 {
3748 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3749
3750 clear_tv(&var2);
3751 return ret;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754
3755 return OK;
3756}
3757
3758/*
3759 * Handle fourth level expression:
3760 * + number addition
3761 * - number subtraction
3762 * . string concatenation
3763 *
3764 * "arg" must point to the first non-white of the expression.
3765 * "arg" is advanced to the next non-white after the recognized expression.
3766 *
3767 * Return OK or FAIL.
3768 */
3769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003770eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar33570922005-01-25 22:26:29 +00003772 typval_T var2;
3773 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003775 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003776#ifdef FEAT_FLOAT
3777 float_T f1 = 0, f2 = 0;
3778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 char_u *s1, *s2;
3780 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3781 char_u *p;
3782
3783 /*
3784 * Get the first variable.
3785 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003786 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return FAIL;
3788
3789 /*
3790 * Repeat computing, until no '+', '-' or '.' is following.
3791 */
3792 for (;;)
3793 {
3794 op = **arg;
3795 if (op != '+' && op != '-' && op != '.')
3796 break;
3797
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003798 if ((op != '+' || (rettv->v_type != VAR_LIST
3799 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800#ifdef FEAT_FLOAT
3801 && (op == '.' || rettv->v_type != VAR_FLOAT)
3802#endif
3803 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003804 {
3805 /* For "list + ...", an illegal use of the first operand as
3806 * a number cannot be determined before evaluating the 2nd
3807 * operand: if this is also a list, all is ok.
3808 * For "something . ...", "something - ..." or "non-list + ...",
3809 * we know that the first operand needs to be a string or number
3810 * without evaluating the 2nd operand. So check before to avoid
3811 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003812 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 {
3814 clear_tv(rettv);
3815 return FAIL;
3816 }
3817 }
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /*
3820 * Get the second variable.
3821 */
3822 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003823 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return FAIL;
3827 }
3828
3829 if (evaluate)
3830 {
3831 /*
3832 * Compute the result.
3833 */
3834 if (op == '.')
3835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003836 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3837 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003838 if (s2 == NULL) /* type error ? */
3839 {
3840 clear_tv(rettv);
3841 clear_tv(&var2);
3842 return FAIL;
3843 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003844 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 clear_tv(rettv);
3846 rettv->v_type = VAR_STRING;
3847 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003849 else if (op == '+' && rettv->v_type == VAR_BLOB
3850 && var2.v_type == VAR_BLOB)
3851 {
3852 blob_T *b1 = rettv->vval.v_blob;
3853 blob_T *b2 = var2.vval.v_blob;
3854 blob_T *b = blob_alloc();
3855 int i;
3856
3857 if (b != NULL)
3858 {
3859 for (i = 0; i < blob_len(b1); i++)
3860 ga_append(&b->bv_ga, blob_get(b1, i));
3861 for (i = 0; i < blob_len(b2); i++)
3862 ga_append(&b->bv_ga, blob_get(b2, i));
3863
3864 clear_tv(rettv);
3865 rettv_blob_set(rettv, b);
3866 }
3867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003868 else if (op == '+' && rettv->v_type == VAR_LIST
3869 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003870 {
3871 /* concatenate Lists */
3872 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3873 &var3) == FAIL)
3874 {
3875 clear_tv(rettv);
3876 clear_tv(&var2);
3877 return FAIL;
3878 }
3879 clear_tv(rettv);
3880 *rettv = var3;
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else
3883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003884 int error = FALSE;
3885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003886#ifdef FEAT_FLOAT
3887 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003889 f1 = rettv->vval.v_float;
3890 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003892 else
3893#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003896 if (error)
3897 {
3898 /* This can only happen for "list + non-list". For
3899 * "non-list + ..." or "something - ...", we returned
3900 * before evaluating the 2nd operand. */
3901 clear_tv(rettv);
3902 return FAIL;
3903 }
3904#ifdef FEAT_FLOAT
3905 if (var2.v_type == VAR_FLOAT)
3906 f1 = n1;
3907#endif
3908 }
3909#ifdef FEAT_FLOAT
3910 if (var2.v_type == VAR_FLOAT)
3911 {
3912 f2 = var2.vval.v_float;
3913 n2 = 0;
3914 }
3915 else
3916#endif
3917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003918 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003919 if (error)
3920 {
3921 clear_tv(rettv);
3922 clear_tv(&var2);
3923 return FAIL;
3924 }
3925#ifdef FEAT_FLOAT
3926 if (rettv->v_type == VAR_FLOAT)
3927 f2 = n2;
3928#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931
3932#ifdef FEAT_FLOAT
3933 /* If there is a float on either side the result is a float. */
3934 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3935 {
3936 if (op == '+')
3937 f1 = f1 + f2;
3938 else
3939 f1 = f1 - f2;
3940 rettv->v_type = VAR_FLOAT;
3941 rettv->vval.v_float = f1;
3942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003944#endif
3945 {
3946 if (op == '+')
3947 n1 = n1 + n2;
3948 else
3949 n1 = n1 - n2;
3950 rettv->v_type = VAR_NUMBER;
3951 rettv->vval.v_number = n1;
3952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 }
3957 return OK;
3958}
3959
3960/*
3961 * Handle fifth level expression:
3962 * * number multiplication
3963 * / number division
3964 * % number modulo
3965 *
3966 * "arg" must point to the first non-white of the expression.
3967 * "arg" is advanced to the next non-white after the recognized expression.
3968 *
3969 * Return OK or FAIL.
3970 */
3971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003972eval6(
3973 char_u **arg,
3974 typval_T *rettv,
3975 int evaluate,
3976 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003980 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003981#ifdef FEAT_FLOAT
3982 int use_float = FALSE;
3983 float_T f1 = 0, f2;
3984#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003985 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 /*
3988 * Get the first variable.
3989 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003990 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 return FAIL;
3992
3993 /*
3994 * Repeat computing, until no '*', '/' or '%' is following.
3995 */
3996 for (;;)
3997 {
3998 op = **arg;
3999 if (op != '*' && op != '/' && op != '%')
4000 break;
4001
4002 if (evaluate)
4003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004004#ifdef FEAT_FLOAT
4005 if (rettv->v_type == VAR_FLOAT)
4006 {
4007 f1 = rettv->vval.v_float;
4008 use_float = TRUE;
4009 n1 = 0;
4010 }
4011 else
4012#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004013 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (error)
4016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 else
4019 n1 = 0;
4020
4021 /*
4022 * Get the second variable.
4023 */
4024 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004025 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return FAIL;
4027
4028 if (evaluate)
4029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004030#ifdef FEAT_FLOAT
4031 if (var2.v_type == VAR_FLOAT)
4032 {
4033 if (!use_float)
4034 {
4035 f1 = n1;
4036 use_float = TRUE;
4037 }
4038 f2 = var2.vval.v_float;
4039 n2 = 0;
4040 }
4041 else
4042#endif
4043 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004044 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045 clear_tv(&var2);
4046 if (error)
4047 return FAIL;
4048#ifdef FEAT_FLOAT
4049 if (use_float)
4050 f2 = n2;
4051#endif
4052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 /*
4055 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004056 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058#ifdef FEAT_FLOAT
4059 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004061 if (op == '*')
4062 f1 = f1 * f2;
4063 else if (op == '/')
4064 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004065# ifdef VMS
4066 /* VMS crashes on divide by zero, work around it */
4067 if (f2 == 0.0)
4068 {
4069 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004070 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004071 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004072 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004073 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004074 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004075 }
4076 else
4077 f1 = f1 / f2;
4078# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004079 /* We rely on the floating point library to handle divide
4080 * by zero to result in "inf" and not a crash. */
4081 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004082# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004086 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 return FAIL;
4088 }
4089 rettv->v_type = VAR_FLOAT;
4090 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004095 if (op == '*')
4096 n1 = n1 * n2;
4097 else if (op == '/')
4098 {
4099 if (n2 == 0) /* give an error message? */
4100 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004101 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004102 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004103 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004104 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004105 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004106 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004107 }
4108 else
4109 n1 = n1 / n2;
4110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004112 {
4113 if (n2 == 0) /* give an error message? */
4114 n1 = 0;
4115 else
4116 n1 = n1 % n2;
4117 }
4118 rettv->v_type = VAR_NUMBER;
4119 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * Handle sixth level expression:
4129 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004130 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004131 * "string" string constant
4132 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 * &option-name option value
4134 * @r register contents
4135 * identifier variable value
4136 * function() function call
4137 * $VAR environment variable
4138 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004139 * [expr, expr] List
4140 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 *
4142 * Also handle:
4143 * ! in front logical NOT
4144 * - in front unary minus
4145 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 * trailing [] subscript in String or List
4147 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 *
4149 * "arg" must point to the first non-white of the expression.
4150 * "arg" is advanced to the next non-white after the recognized expression.
4151 *
4152 * Return OK or FAIL.
4153 */
4154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155eval7(
4156 char_u **arg,
4157 typval_T *rettv,
4158 int evaluate,
4159 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004161 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int len;
4163 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u *start_leader, *end_leader;
4165 int ret = OK;
4166 char_u *alias;
4167
4168 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004170 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004175 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 */
4177 start_leader = *arg;
4178 while (**arg == '!' || **arg == '-' || **arg == '+')
4179 *arg = skipwhite(*arg + 1);
4180 end_leader = *arg;
4181
4182 switch (**arg)
4183 {
4184 /*
4185 * Number constant.
4186 */
4187 case '0':
4188 case '1':
4189 case '2':
4190 case '3':
4191 case '4':
4192 case '5':
4193 case '6':
4194 case '7':
4195 case '8':
4196 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004197 {
4198#ifdef FEAT_FLOAT
4199 char_u *p = skipdigits(*arg + 1);
4200 int get_float = FALSE;
4201
4202 /* We accept a float when the format matches
4203 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004204 * strict to avoid backwards compatibility problems.
4205 * Don't look for a float after the "." operator, so that
4206 * ":let vers = 1.2.3" doesn't fail. */
4207 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004209 get_float = TRUE;
4210 p = skipdigits(p + 2);
4211 if (*p == 'e' || *p == 'E')
4212 {
4213 ++p;
4214 if (*p == '-' || *p == '+')
4215 ++p;
4216 if (!vim_isdigit(*p))
4217 get_float = FALSE;
4218 else
4219 p = skipdigits(p + 1);
4220 }
4221 if (ASCII_ISALPHA(*p) || *p == '.')
4222 get_float = FALSE;
4223 }
4224 if (get_float)
4225 {
4226 float_T f;
4227
4228 *arg += string2float(*arg, &f);
4229 if (evaluate)
4230 {
4231 rettv->v_type = VAR_FLOAT;
4232 rettv->vval.v_float = f;
4233 }
4234 }
4235 else
4236#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004237 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004238 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004239 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004240 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004241
4242 // Blob constant: 0z0123456789abcdef
4243 if (evaluate)
4244 blob = blob_alloc();
4245 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4246 {
4247 if (!vim_isxdigit(bp[1]))
4248 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004249 if (blob != NULL)
4250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004252 ga_clear(&blob->bv_ga);
4253 VIM_CLEAR(blob);
4254 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004255 ret = FAIL;
4256 break;
4257 }
4258 if (blob != NULL)
4259 ga_append(&blob->bv_ga,
4260 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4261 }
4262 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004263 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004264 *arg = bp;
4265 }
4266 else
4267 {
4268 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004269 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004270 *arg += len;
4271 if (evaluate)
4272 {
4273 rettv->v_type = VAR_NUMBER;
4274 rettv->vval.v_number = n;
4275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 /*
4281 * String constant: "string".
4282 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 break;
4285
4286 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004287 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004290 break;
4291
4292 /*
4293 * List: [expr, expr]
4294 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 break;
4297
4298 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004299 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300 * Dictionary: {key: val, key: val}
4301 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004302 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4303 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004304 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004305 break;
4306
4307 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004308 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004310 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 break;
4312
4313 /*
4314 * Environment variable: $VAR.
4315 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 break;
4318
4319 /*
4320 * Register contents: @r.
4321 */
4322 case '@': ++*arg;
4323 if (evaluate)
4324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004326 rettv->vval.v_string = get_reg_contents(**arg,
4327 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329 if (**arg != NUL)
4330 ++*arg;
4331 break;
4332
4333 /*
4334 * nested expression: (expression).
4335 */
4336 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 if (**arg == ')')
4339 ++*arg;
4340 else if (ret == OK)
4341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004342 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 ret = FAIL;
4345 }
4346 break;
4347
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 break;
4350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004351
4352 if (ret == NOTDONE)
4353 {
4354 /*
4355 * Must be a variable or function name.
4356 * Can also be a curly-braces kind of name: {expr}.
4357 */
4358 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004359 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004360 if (alias != NULL)
4361 s = alias;
4362
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004363 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004364 ret = FAIL;
4365 else
4366 {
4367 if (**arg == '(') /* recursive! */
4368 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004369 partial_T *partial;
4370
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004371 if (!evaluate)
4372 check_vars(s, len);
4373
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 /* If "s" is the name of a variable of type VAR_FUNC
4375 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004376 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004378 /* Need to make a copy, in case evaluating the arguments makes
4379 * the name invalid. */
4380 s = vim_strsave(s);
4381 if (s == NULL)
4382 ret = FAIL;
4383 else
4384 /* Invoke the function. */
4385 ret = get_func_tv(s, len, rettv, arg,
4386 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4387 &len, evaluate, partial, NULL);
4388 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004389
4390 /* If evaluate is FALSE rettv->v_type was not set in
4391 * get_func_tv, but it's needed in handle_subscript() to parse
4392 * what follows. So set it here. */
4393 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4394 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004395 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004396 rettv->v_type = VAR_FUNC;
4397 }
4398
Bram Moolenaar8c711452005-01-14 21:53:12 +00004399 /* Stop the expression evaluation when immediately
4400 * aborting on error, or when an interrupt occurred or
4401 * an exception was thrown but not caught. */
4402 if (aborting())
4403 {
4404 if (ret == OK)
4405 clear_tv(rettv);
4406 ret = FAIL;
4407 }
4408 }
4409 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004410 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004411 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004412 {
4413 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004414 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004416 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004417 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 }
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 *arg = skipwhite(*arg);
4421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004422 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4423 * expr(expr). */
4424 if (ret == OK)
4425 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 /*
4428 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4429 */
4430 if (ret == OK && evaluate && end_leader > start_leader)
4431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004432 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004433 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004434#ifdef FEAT_FLOAT
4435 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004436
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 if (rettv->v_type == VAR_FLOAT)
4438 f = rettv->vval.v_float;
4439 else
4440#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004441 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004444 clear_tv(rettv);
4445 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004447 else
4448 {
4449 while (end_leader > start_leader)
4450 {
4451 --end_leader;
4452 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004453 {
4454#ifdef FEAT_FLOAT
4455 if (rettv->v_type == VAR_FLOAT)
4456 f = !f;
4457 else
4458#endif
4459 val = !val;
4460 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004461 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004462 {
4463#ifdef FEAT_FLOAT
4464 if (rettv->v_type == VAR_FLOAT)
4465 f = -f;
4466 else
4467#endif
4468 val = -val;
4469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004470 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471#ifdef FEAT_FLOAT
4472 if (rettv->v_type == VAR_FLOAT)
4473 {
4474 clear_tv(rettv);
4475 rettv->vval.v_float = f;
4476 }
4477 else
4478#endif
4479 {
4480 clear_tv(rettv);
4481 rettv->v_type = VAR_NUMBER;
4482 rettv->vval.v_number = val;
4483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486
4487 return ret;
4488}
4489
4490/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004491 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4492 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004493 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4494 */
4495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004496eval_index(
4497 char_u **arg,
4498 typval_T *rettv,
4499 int evaluate,
4500 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004501{
4502 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004503 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004504 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 long len = -1;
4507 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004510
Bram Moolenaara03f2332016-02-06 18:09:59 +01004511 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004512 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004513 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004514 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004516 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004517 return FAIL;
4518 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004519#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004520 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004521 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004522 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004523#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004524 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004525 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004526 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004527 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004528 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004529 return FAIL;
4530 case VAR_UNKNOWN:
4531 if (evaluate)
4532 return FAIL;
4533 /* FALLTHROUGH */
4534
4535 case VAR_STRING:
4536 case VAR_NUMBER:
4537 case VAR_LIST:
4538 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004539 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004540 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004541 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004543 init_tv(&var1);
4544 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 /*
4548 * dict.name
4549 */
4550 key = *arg + 1;
4551 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4552 ;
4553 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 }
4557 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 /*
4560 * something[idx]
4561 *
4562 * Get the (first) variable from inside the [].
4563 */
4564 *arg = skipwhite(*arg + 1);
4565 if (**arg == ':')
4566 empty1 = TRUE;
4567 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4568 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004569 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 {
4571 /* not a number or string */
4572 clear_tv(&var1);
4573 return FAIL;
4574 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575
4576 /*
4577 * Get the second variable from inside the [:].
4578 */
4579 if (**arg == ':')
4580 {
4581 range = TRUE;
4582 *arg = skipwhite(*arg + 1);
4583 if (**arg == ']')
4584 empty2 = TRUE;
4585 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004587 if (!empty1)
4588 clear_tv(&var1);
4589 return FAIL;
4590 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004591 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004592 {
4593 /* not a number or string */
4594 if (!empty1)
4595 clear_tv(&var1);
4596 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 return FAIL;
4598 }
4599 }
4600
4601 /* Check for the ']'. */
4602 if (**arg != ']')
4603 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004604 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004605 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 clear_tv(&var1);
4607 if (range)
4608 clear_tv(&var2);
4609 return FAIL;
4610 }
4611 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 }
4613
4614 if (evaluate)
4615 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 n1 = 0;
4617 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004619 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 }
4622 if (range)
4623 {
4624 if (empty2)
4625 n2 = -1;
4626 else
4627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004628 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004630 }
4631 }
4632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004635 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004636 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004637 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004638 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004639 case VAR_SPECIAL:
4640 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004641 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004642 break; /* not evaluating, skipping over subscript */
4643
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 case VAR_NUMBER:
4645 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004646 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 len = (long)STRLEN(s);
4648 if (range)
4649 {
4650 /* The resulting variable is a substring. If the indexes
4651 * are out of range the result is empty. */
4652 if (n1 < 0)
4653 {
4654 n1 = len + n1;
4655 if (n1 < 0)
4656 n1 = 0;
4657 }
4658 if (n2 < 0)
4659 n2 = len + n2;
4660 else if (n2 >= len)
4661 n2 = len;
4662 if (n1 >= len || n2 < 0 || n1 > n2)
4663 s = NULL;
4664 else
4665 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4666 }
4667 else
4668 {
4669 /* The resulting variable is a string of a single
4670 * character. If the index is too big or negative the
4671 * result is empty. */
4672 if (n1 >= len || n1 < 0)
4673 s = NULL;
4674 else
4675 s = vim_strnsave(s + n1, 1);
4676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
4678 rettv->v_type = VAR_STRING;
4679 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 break;
4681
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004682 case VAR_BLOB:
4683 len = blob_len(rettv->vval.v_blob);
4684 if (range)
4685 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004686 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004687 // are out of range the result is empty.
4688 if (n1 < 0)
4689 {
4690 n1 = len + n1;
4691 if (n1 < 0)
4692 n1 = 0;
4693 }
4694 if (n2 < 0)
4695 n2 = len + n2;
4696 else if (n2 >= len)
4697 n2 = len - 1;
4698 if (n1 >= len || n2 < 0 || n1 > n2)
4699 {
4700 clear_tv(rettv);
4701 rettv->v_type = VAR_BLOB;
4702 rettv->vval.v_blob = NULL;
4703 }
4704 else
4705 {
4706 blob_T *blob = blob_alloc();
4707
4708 if (blob != NULL)
4709 {
4710 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4711 {
4712 blob_free(blob);
4713 return FAIL;
4714 }
4715 blob->bv_ga.ga_len = n2 - n1 + 1;
4716 for (i = n1; i <= n2; i++)
4717 blob_set(blob, i - n1,
4718 blob_get(rettv->vval.v_blob, i));
4719
4720 clear_tv(rettv);
4721 rettv_blob_set(rettv, blob);
4722 }
4723 }
4724 }
4725 else
4726 {
4727 // The resulting variable is a string of a single
4728 // character. If the index is too big or negative the
4729 // result is empty.
4730 if (n1 < len && n1 >= 0)
4731 {
4732 int v = (int)blob_get(rettv->vval.v_blob, n1);
4733
4734 clear_tv(rettv);
4735 rettv->v_type = VAR_NUMBER;
4736 rettv->vval.v_number = v;
4737 }
4738 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004739 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004740 }
4741 break;
4742
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 if (n1 < 0)
4746 n1 = len + n1;
4747 if (!empty1 && (n1 < 0 || n1 >= len))
4748 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004749 /* For a range we allow invalid values and return an empty
4750 * list. A list index out of range is an error. */
4751 if (!range)
4752 {
4753 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004754 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004755 return FAIL;
4756 }
4757 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758 }
4759 if (range)
4760 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004761 list_T *l;
4762 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763
4764 if (n2 < 0)
4765 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004766 else if (n2 >= len)
4767 n2 = len - 1;
4768 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004769 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 l = list_alloc();
4771 if (l == NULL)
4772 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 n1 <= n2; ++n1)
4775 {
4776 if (list_append_tv(l, &item->li_tv) == FAIL)
4777 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 return FAIL;
4780 }
4781 item = item->li_next;
4782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004784 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 }
4786 else
4787 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004788 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 clear_tv(rettv);
4790 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 }
4792 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793
4794 case VAR_DICT:
4795 if (range)
4796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004797 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004798 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 if (len == -1)
4800 clear_tv(&var1);
4801 return FAIL;
4802 }
4803 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805
4806 if (len == -1)
4807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004808 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004809 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811 clear_tv(&var1);
4812 return FAIL;
4813 }
4814 }
4815
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004816 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004818 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 if (len == -1)
4821 clear_tv(&var1);
4822 if (item == NULL)
4823 return FAIL;
4824
4825 copy_tv(&item->di_tv, &var1);
4826 clear_tv(rettv);
4827 *rettv = var1;
4828 }
4829 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 }
4831 }
4832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 return OK;
4834}
4835
4836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 * Get an option value.
4838 * "arg" points to the '&' or '+' before the option name.
4839 * "arg" is advanced to character after the option name.
4840 * Return OK or FAIL.
4841 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004843get_option_tv(
4844 char_u **arg,
4845 typval_T *rettv, /* when NULL, only check if option exists */
4846 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
4848 char_u *option_end;
4849 long numval;
4850 char_u *stringval;
4851 int opt_type;
4852 int c;
4853 int working = (**arg == '+'); /* has("+option") */
4854 int ret = OK;
4855 int opt_flags;
4856
4857 /*
4858 * Isolate the option name and find its value.
4859 */
4860 option_end = find_option_end(arg, &opt_flags);
4861 if (option_end == NULL)
4862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 return FAIL;
4866 }
4867
4868 if (!evaluate)
4869 {
4870 *arg = option_end;
4871 return OK;
4872 }
4873
4874 c = *option_end;
4875 *option_end = NUL;
4876 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 if (opt_type == -3) /* invalid name */
4880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004882 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 ret = FAIL;
4884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
4887 if (opt_type == -2) /* hidden string option */
4888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 rettv->v_type = VAR_STRING;
4890 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 else if (opt_type == -1) /* hidden number option */
4893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 rettv->v_type = VAR_NUMBER;
4895 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 else if (opt_type == 1) /* number option */
4898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 rettv->v_type = VAR_NUMBER;
4900 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else /* string option */
4903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 rettv->v_type = VAR_STRING;
4905 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 }
4908 else if (working && (opt_type == -2 || opt_type == -1))
4909 ret = FAIL;
4910
4911 *option_end = c; /* put back for error messages */
4912 *arg = option_end;
4913
4914 return ret;
4915}
4916
4917/*
4918 * Allocate a variable for a string constant.
4919 * Return OK or FAIL.
4920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 char_u *p;
4925 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 int extra = 0;
4927
4928 /*
4929 * Find the end of the string, skipping backslashed characters.
4930 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004931 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
4933 if (*p == '\\' && p[1] != NUL)
4934 {
4935 ++p;
4936 /* A "\<x>" form occupies at least 4 characters, and produces up
4937 * to 6 characters: reserve space for 2 extra */
4938 if (*p == '<')
4939 extra += 2;
4940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942
4943 if (*p != '"')
4944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004945 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 return FAIL;
4947 }
4948
4949 /* If only parsing, set *arg and return here */
4950 if (!evaluate)
4951 {
4952 *arg = p + 1;
4953 return OK;
4954 }
4955
4956 /*
4957 * Copy the string into allocated memory, handling backslashed
4958 * characters.
4959 */
4960 name = alloc((unsigned)(p - *arg + extra));
4961 if (name == NULL)
4962 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 rettv->v_type = VAR_STRING;
4964 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 if (*p == '\\')
4969 {
4970 switch (*++p)
4971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 case 'b': *name++ = BS; ++p; break;
4973 case 'e': *name++ = ESC; ++p; break;
4974 case 'f': *name++ = FF; ++p; break;
4975 case 'n': *name++ = NL; ++p; break;
4976 case 'r': *name++ = CAR; ++p; break;
4977 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979 case 'X': /* hex: "\x1", "\x12" */
4980 case 'x':
4981 case 'u': /* Unicode: "\u0023" */
4982 case 'U':
4983 if (vim_isxdigit(p[1]))
4984 {
4985 int n, nr;
4986 int c = toupper(*p);
4987
4988 if (c == 'X')
4989 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004990 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004992 else
4993 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 nr = 0;
4995 while (--n >= 0 && vim_isxdigit(p[1]))
4996 {
4997 ++p;
4998 nr = (nr << 4) + hex2nr(*p);
4999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001#ifdef FEAT_MBYTE
5002 /* For "\u" store the number according to
5003 * 'encoding'. */
5004 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
5007#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 break;
5011
5012 /* octal: "\1", "\12", "\123" */
5013 case '0':
5014 case '1':
5015 case '2':
5016 case '3':
5017 case '4':
5018 case '5':
5019 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 case '7': *name = *p++ - '0';
5021 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 *name = (*name << 3) + *p++ - '0';
5024 if (*p >= '0' && *p <= '7')
5025 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029
5030 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005031 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (extra != 0)
5033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036 }
5037 /* FALLTHROUGH */
5038
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 break;
5041 }
5042 }
5043 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005048 if (*p != NUL) /* just in case */
5049 ++p;
5050 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 return OK;
5053}
5054
5055/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 * Return OK or FAIL.
5058 */
5059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061{
5062 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 int reduce = 0;
5065
5066 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005069 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 break;
5075 ++reduce;
5076 ++p;
5077 }
5078 }
5079
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005082 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 return FAIL;
5084 }
5085
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 if (!evaluate)
5088 {
5089 *arg = p + 1;
5090 return OK;
5091 }
5092
5093 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095 */
5096 str = alloc((unsigned)((p - *arg) - reduce));
5097 if (str == NULL)
5098 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 rettv->v_type = VAR_STRING;
5100 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 break;
5108 ++p;
5109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 *arg = p + 1;
5114
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 return OK;
5116}
5117
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005118/*
5119 * Return the function name of the partial.
5120 */
5121 char_u *
5122partial_name(partial_T *pt)
5123{
5124 if (pt->pt_name != NULL)
5125 return pt->pt_name;
5126 return pt->pt_func->uf_name;
5127}
5128
Bram Moolenaarddecc252016-04-06 22:59:37 +02005129 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005130partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005131{
5132 int i;
5133
5134 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005135 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005136 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005137 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005138 if (pt->pt_name != NULL)
5139 {
5140 func_unref(pt->pt_name);
5141 vim_free(pt->pt_name);
5142 }
5143 else
5144 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005145 vim_free(pt);
5146}
5147
5148/*
5149 * Unreference a closure: decrement the reference count and free it when it
5150 * becomes zero.
5151 */
5152 void
5153partial_unref(partial_T *pt)
5154{
5155 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005156 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005157}
5158
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005159static int tv_equal_recurse_limit;
5160
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005161 static int
5162func_equal(
5163 typval_T *tv1,
5164 typval_T *tv2,
5165 int ic) /* ignore case */
5166{
5167 char_u *s1, *s2;
5168 dict_T *d1, *d2;
5169 int a1, a2;
5170 int i;
5171
5172 /* empty and NULL function name considered the same */
5173 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005174 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005175 if (s1 != NULL && *s1 == NUL)
5176 s1 = NULL;
5177 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005178 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005179 if (s2 != NULL && *s2 == NUL)
5180 s2 = NULL;
5181 if (s1 == NULL || s2 == NULL)
5182 {
5183 if (s1 != s2)
5184 return FALSE;
5185 }
5186 else if (STRCMP(s1, s2) != 0)
5187 return FALSE;
5188
5189 /* empty dict and NULL dict is different */
5190 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5191 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5192 if (d1 == NULL || d2 == NULL)
5193 {
5194 if (d1 != d2)
5195 return FALSE;
5196 }
5197 else if (!dict_equal(d1, d2, ic, TRUE))
5198 return FALSE;
5199
5200 /* empty list and no list considered the same */
5201 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5202 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5203 if (a1 != a2)
5204 return FALSE;
5205 for (i = 0; i < a1; ++i)
5206 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5207 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5208 return FALSE;
5209
5210 return TRUE;
5211}
5212
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005213/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005214 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005215 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005216 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005217 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005219tv_equal(
5220 typval_T *tv1,
5221 typval_T *tv2,
5222 int ic, /* ignore case */
5223 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005224{
5225 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005227 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005228 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005229
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005230 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005231 * recursiveness to a limit. We guess they are equal then.
5232 * A fixed limit has the problem of still taking an awful long time.
5233 * Reduce the limit every time running into it. That should work fine for
5234 * deeply linked structures that are not recursively linked and catch
5235 * recursiveness quickly. */
5236 if (!recursive)
5237 tv_equal_recurse_limit = 1000;
5238 if (recursive_cnt >= tv_equal_recurse_limit)
5239 {
5240 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005241 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005242 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005243
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005244 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5245 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005246 if ((tv1->v_type == VAR_FUNC
5247 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5248 && (tv2->v_type == VAR_FUNC
5249 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5250 {
5251 ++recursive_cnt;
5252 r = func_equal(tv1, tv2, ic);
5253 --recursive_cnt;
5254 return r;
5255 }
5256
5257 if (tv1->v_type != tv2->v_type)
5258 return FALSE;
5259
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005260 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005261 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005262 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005263 ++recursive_cnt;
5264 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5265 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005266 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005267
5268 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005269 ++recursive_cnt;
5270 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5271 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005272 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005273
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005274 case VAR_BLOB:
5275 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5276
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005277 case VAR_NUMBER:
5278 return tv1->vval.v_number == tv2->vval.v_number;
5279
5280 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005281 s1 = tv_get_string_buf(tv1, buf1);
5282 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005283 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005284
5285 case VAR_SPECIAL:
5286 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005287
5288 case VAR_FLOAT:
5289#ifdef FEAT_FLOAT
5290 return tv1->vval.v_float == tv2->vval.v_float;
5291#endif
5292 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005293#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005294 return tv1->vval.v_job == tv2->vval.v_job;
5295#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005296 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005297#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005298 return tv1->vval.v_channel == tv2->vval.v_channel;
5299#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005300 case VAR_FUNC:
5301 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005302 case VAR_UNKNOWN:
5303 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005304 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005305
Bram Moolenaara03f2332016-02-06 18:09:59 +01005306 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5307 * does not equal anything, not even itself. */
5308 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309}
5310
5311/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005312 * Return the next (unique) copy ID.
5313 * Used for serializing nested structures.
5314 */
5315 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005316get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005317{
5318 current_copyID += COPYID_INC;
5319 return current_copyID;
5320}
5321
5322/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005323 * Garbage collection for lists and dictionaries.
5324 *
5325 * We use reference counts to be able to free most items right away when they
5326 * are no longer used. But for composite items it's possible that it becomes
5327 * unused while the reference count is > 0: When there is a recursive
5328 * reference. Example:
5329 * :let l = [1, 2, 3]
5330 * :let d = {9: l}
5331 * :let l[1] = d
5332 *
5333 * Since this is quite unusual we handle this with garbage collection: every
5334 * once in a while find out which lists and dicts are not referenced from any
5335 * variable.
5336 *
5337 * Here is a good reference text about garbage collection (refers to Python
5338 * but it applies to all reference-counting mechanisms):
5339 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005340 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005341
5342/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005343 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005344 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005346 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005348garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005349{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005350 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005351 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005352 buf_T *buf;
5353 win_T *wp;
5354 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005355 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005356 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005357
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005358 if (!testing)
5359 {
5360 /* Only do this once. */
5361 want_garbage_collect = FALSE;
5362 may_garbage_collect = FALSE;
5363 garbage_collect_at_exit = FALSE;
5364 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005365
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005366 /* We advance by two because we add one for items referenced through
5367 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005368 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005369
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005370 /*
5371 * 1. Go through all accessible variables and mark all lists and dicts
5372 * with copyID.
5373 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005374
5375 /* Don't free variables in the previous_funccal list unless they are only
5376 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005377 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005378 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005379
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005380 /* script-local variables */
5381 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005382 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383
5384 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005385 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005386 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5387 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388
5389 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005390 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005391 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5392 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005393 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5395 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005396
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005397 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005398 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005399 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5400 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005401 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403
5404 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005406
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005407 /* named functions (matters for closures) */
5408 abort = abort || set_ref_in_functions(copyID);
5409
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005410 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005411 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005412
Bram Moolenaard812df62008-11-09 12:46:09 +00005413 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005414 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005415
Bram Moolenaar1dced572012-04-05 16:54:08 +02005416#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005417 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005418#endif
5419
Bram Moolenaardb913952012-06-29 12:54:53 +02005420#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005422#endif
5423
5424#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005426#endif
5427
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005429 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005430 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005431#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005432#ifdef FEAT_NETBEANS_INTG
5433 abort = abort || set_ref_in_nb_channel(copyID);
5434#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005435
Bram Moolenaare3188e22016-05-31 21:13:04 +02005436#ifdef FEAT_TIMERS
5437 abort = abort || set_ref_in_timer(copyID);
5438#endif
5439
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005440#ifdef FEAT_QUICKFIX
5441 abort = abort || set_ref_in_quickfix(copyID);
5442#endif
5443
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005444#ifdef FEAT_TERMINAL
5445 abort = abort || set_ref_in_term(copyID);
5446#endif
5447
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005448 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005449 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 /*
5451 * 2. Free lists and dictionaries that are not referenced.
5452 */
5453 did_free = free_unref_items(copyID);
5454
5455 /*
5456 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005457 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005458 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005459 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005460 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005461 else if (p_verbose > 0)
5462 {
5463 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5464 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005465
5466 return did_free;
5467}
5468
5469/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005470 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005471 */
5472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005473free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005474{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005475 int did_free = FALSE;
5476
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005477 /* Let all "free" functions know that we are here. This means no
5478 * dictionaries, lists, channels or jobs are to be freed, because we will
5479 * do that here. */
5480 in_free_unref_items = TRUE;
5481
5482 /*
5483 * PASS 1: free the contents of the items. We don't free the items
5484 * themselves yet, so that it is possible to decrement refcount counters
5485 */
5486
Bram Moolenaarcd524592016-07-17 14:57:05 +02005487 /* Go through the list of dicts and free items without the copyID. */
5488 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005489
Bram Moolenaarda861d62016-07-17 15:46:27 +02005490 /* Go through the list of lists and free items without the copyID. */
5491 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005492
5493#ifdef FEAT_JOB_CHANNEL
5494 /* Go through the list of jobs and free items without the copyID. This
5495 * must happen before doing channels, because jobs refer to channels, but
5496 * the reference from the channel to the job isn't tracked. */
5497 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5498
5499 /* Go through the list of channels and free items without the copyID. */
5500 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5501#endif
5502
5503 /*
5504 * PASS 2: free the items themselves.
5505 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005506 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005507 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005509#ifdef FEAT_JOB_CHANNEL
5510 /* Go through the list of jobs and free items without the copyID. This
5511 * must happen before doing channels, because jobs refer to channels, but
5512 * the reference from the channel to the job isn't tracked. */
5513 free_unused_jobs(copyID, COPYID_MASK);
5514
5515 /* Go through the list of channels and free items without the copyID. */
5516 free_unused_channels(copyID, COPYID_MASK);
5517#endif
5518
5519 in_free_unref_items = FALSE;
5520
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005521 return did_free;
5522}
5523
5524/*
5525 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 * "list_stack" is used to add lists to be marked. Can be NULL.
5527 *
5528 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005529 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005530 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005531set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005532{
5533 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005534 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005535 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005536 hashtab_T *cur_ht;
5537 ht_stack_T *ht_stack = NULL;
5538 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005539
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005540 cur_ht = ht;
5541 for (;;)
5542 {
5543 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005544 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005545 /* Mark each item in the hashtab. If the item contains a hashtab
5546 * it is added to ht_stack, if it contains a list it is added to
5547 * list_stack. */
5548 todo = (int)cur_ht->ht_used;
5549 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5550 if (!HASHITEM_EMPTY(hi))
5551 {
5552 --todo;
5553 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5554 &ht_stack, list_stack);
5555 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005556 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005557
5558 if (ht_stack == NULL)
5559 break;
5560
5561 /* take an item from the stack */
5562 cur_ht = ht_stack->ht;
5563 tempitem = ht_stack;
5564 ht_stack = ht_stack->prev;
5565 free(tempitem);
5566 }
5567
5568 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005569}
5570
5571/*
5572 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005573 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5574 *
5575 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005576 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005578set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005579{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005580 listitem_T *li;
5581 int abort = FALSE;
5582 list_T *cur_l;
5583 list_stack_T *list_stack = NULL;
5584 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005585
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 cur_l = l;
5587 for (;;)
5588 {
5589 if (!abort)
5590 /* Mark each item in the list. If the item contains a hashtab
5591 * it is added to ht_stack, if it contains a list it is added to
5592 * list_stack. */
5593 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5594 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5595 ht_stack, &list_stack);
5596 if (list_stack == NULL)
5597 break;
5598
5599 /* take an item from the stack */
5600 cur_l = list_stack->list;
5601 tempitem = list_stack;
5602 list_stack = list_stack->prev;
5603 free(tempitem);
5604 }
5605
5606 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005607}
5608
5609/*
5610 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005611 * "list_stack" is used to add lists to be marked. Can be NULL.
5612 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5613 *
5614 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005615 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005617set_ref_in_item(
5618 typval_T *tv,
5619 int copyID,
5620 ht_stack_T **ht_stack,
5621 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005622{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005623 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005624
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005625 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005626 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005627 dict_T *dd = tv->vval.v_dict;
5628
Bram Moolenaara03f2332016-02-06 18:09:59 +01005629 if (dd != NULL && dd->dv_copyID != copyID)
5630 {
5631 /* Didn't see this dict yet. */
5632 dd->dv_copyID = copyID;
5633 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005634 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005635 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5636 }
5637 else
5638 {
5639 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5640 if (newitem == NULL)
5641 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005642 else
5643 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005644 newitem->ht = &dd->dv_hashtab;
5645 newitem->prev = *ht_stack;
5646 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005648 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005649 }
5650 }
5651 else if (tv->v_type == VAR_LIST)
5652 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005653 list_T *ll = tv->vval.v_list;
5654
Bram Moolenaara03f2332016-02-06 18:09:59 +01005655 if (ll != NULL && ll->lv_copyID != copyID)
5656 {
5657 /* Didn't see this list yet. */
5658 ll->lv_copyID = copyID;
5659 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005660 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005661 abort = set_ref_in_list(ll, copyID, ht_stack);
5662 }
5663 else
5664 {
5665 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005666 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005667 if (newitem == NULL)
5668 abort = TRUE;
5669 else
5670 {
5671 newitem->list = ll;
5672 newitem->prev = *list_stack;
5673 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005674 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005675 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005676 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005677 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005678 else if (tv->v_type == VAR_FUNC)
5679 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005680 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005681 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005682 else if (tv->v_type == VAR_PARTIAL)
5683 {
5684 partial_T *pt = tv->vval.v_partial;
5685 int i;
5686
5687 /* A partial does not have a copyID, because it cannot contain itself.
5688 */
5689 if (pt != NULL)
5690 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005691 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005692
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005693 if (pt->pt_dict != NULL)
5694 {
5695 typval_T dtv;
5696
5697 dtv.v_type = VAR_DICT;
5698 dtv.vval.v_dict = pt->pt_dict;
5699 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5700 }
5701
5702 for (i = 0; i < pt->pt_argc; ++i)
5703 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5704 ht_stack, list_stack);
5705 }
5706 }
5707#ifdef FEAT_JOB_CHANNEL
5708 else if (tv->v_type == VAR_JOB)
5709 {
5710 job_T *job = tv->vval.v_job;
5711 typval_T dtv;
5712
5713 if (job != NULL && job->jv_copyID != copyID)
5714 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005715 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005716 if (job->jv_channel != NULL)
5717 {
5718 dtv.v_type = VAR_CHANNEL;
5719 dtv.vval.v_channel = job->jv_channel;
5720 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5721 }
5722 if (job->jv_exit_partial != NULL)
5723 {
5724 dtv.v_type = VAR_PARTIAL;
5725 dtv.vval.v_partial = job->jv_exit_partial;
5726 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5727 }
5728 }
5729 }
5730 else if (tv->v_type == VAR_CHANNEL)
5731 {
5732 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005733 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005734 typval_T dtv;
5735 jsonq_T *jq;
5736 cbq_T *cq;
5737
5738 if (ch != NULL && ch->ch_copyID != copyID)
5739 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005740 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005741 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005742 {
5743 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5744 jq = jq->jq_next)
5745 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5746 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5747 cq = cq->cq_next)
5748 if (cq->cq_partial != NULL)
5749 {
5750 dtv.v_type = VAR_PARTIAL;
5751 dtv.vval.v_partial = cq->cq_partial;
5752 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5753 }
5754 if (ch->ch_part[part].ch_partial != NULL)
5755 {
5756 dtv.v_type = VAR_PARTIAL;
5757 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5758 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5759 }
5760 }
5761 if (ch->ch_partial != NULL)
5762 {
5763 dtv.v_type = VAR_PARTIAL;
5764 dtv.vval.v_partial = ch->ch_partial;
5765 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5766 }
5767 if (ch->ch_close_partial != NULL)
5768 {
5769 dtv.v_type = VAR_PARTIAL;
5770 dtv.vval.v_partial = ch->ch_close_partial;
5771 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5772 }
5773 }
5774 }
5775#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005777}
5778
Bram Moolenaar17a13432016-01-24 14:22:10 +01005779 static char *
5780get_var_special_name(int nr)
5781{
5782 switch (nr)
5783 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005784 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005785 case VVAL_TRUE: return "v:true";
5786 case VVAL_NONE: return "v:none";
5787 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005788 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005789 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005790 return "42";
5791}
5792
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794 * Return a string with the string representation of a variable.
5795 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005796 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005797 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005798 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5799 * stings as "string()", otherwise does not put quotes around strings, as
5800 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005801 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5802 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005803 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005805 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005806echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005807 typval_T *tv,
5808 char_u **tofree,
5809 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005810 int copyID,
5811 int echo_style,
5812 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005813 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005815 static int recurse = 0;
5816 char_u *r = NULL;
5817
Bram Moolenaar33570922005-01-25 22:26:29 +00005818 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005819 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005820 if (!did_echo_string_emsg)
5821 {
5822 /* Only give this message once for a recursive call to avoid
5823 * flooding the user with errors. And stop iterating over lists
5824 * and dicts. */
5825 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005826 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005827 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005828 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005829 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005830 }
5831 ++recurse;
5832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 switch (tv->v_type)
5834 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005835 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005836 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005837 {
5838 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005839 r = tv->vval.v_string;
5840 if (r == NULL)
5841 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005842 }
5843 else
5844 {
5845 *tofree = string_quote(tv->vval.v_string, FALSE);
5846 r = *tofree;
5847 }
5848 break;
5849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005851 if (echo_style)
5852 {
5853 *tofree = NULL;
5854 r = tv->vval.v_string;
5855 }
5856 else
5857 {
5858 *tofree = string_quote(tv->vval.v_string, TRUE);
5859 r = *tofree;
5860 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005861 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005862
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005863 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005864 {
5865 partial_T *pt = tv->vval.v_partial;
5866 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005867 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005868 garray_T ga;
5869 int i;
5870 char_u *tf;
5871
5872 ga_init2(&ga, 1, 100);
5873 ga_concat(&ga, (char_u *)"function(");
5874 if (fname != NULL)
5875 {
5876 ga_concat(&ga, fname);
5877 vim_free(fname);
5878 }
5879 if (pt != NULL && pt->pt_argc > 0)
5880 {
5881 ga_concat(&ga, (char_u *)", [");
5882 for (i = 0; i < pt->pt_argc; ++i)
5883 {
5884 if (i > 0)
5885 ga_concat(&ga, (char_u *)", ");
5886 ga_concat(&ga,
5887 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5888 vim_free(tf);
5889 }
5890 ga_concat(&ga, (char_u *)"]");
5891 }
5892 if (pt != NULL && pt->pt_dict != NULL)
5893 {
5894 typval_T dtv;
5895
5896 ga_concat(&ga, (char_u *)", ");
5897 dtv.v_type = VAR_DICT;
5898 dtv.vval.v_dict = pt->pt_dict;
5899 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5900 vim_free(tf);
5901 }
5902 ga_concat(&ga, (char_u *)")");
5903
5904 *tofree = ga.ga_data;
5905 r = *tofree;
5906 break;
5907 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005908
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005909 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005910 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005911 break;
5912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005914 if (tv->vval.v_list == NULL)
5915 {
5916 *tofree = NULL;
5917 r = NULL;
5918 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005919 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5920 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005921 {
5922 *tofree = NULL;
5923 r = (char_u *)"[...]";
5924 }
5925 else
5926 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005927 int old_copyID = tv->vval.v_list->lv_copyID;
5928
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005929 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005930 *tofree = list2string(tv, copyID, restore_copyID);
5931 if (restore_copyID)
5932 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005933 r = *tofree;
5934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005936
Bram Moolenaar8c711452005-01-14 21:53:12 +00005937 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005938 if (tv->vval.v_dict == NULL)
5939 {
5940 *tofree = NULL;
5941 r = NULL;
5942 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005943 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5944 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005945 {
5946 *tofree = NULL;
5947 r = (char_u *)"{...}";
5948 }
5949 else
5950 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005951 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005952 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005953 *tofree = dict2string(tv, copyID, restore_copyID);
5954 if (restore_copyID)
5955 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005956 r = *tofree;
5957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005958 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005959
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005961 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005962 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005963 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005964 break;
5965
Bram Moolenaar835dc632016-02-07 14:27:38 +01005966 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005967 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005969 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005970 if (composite_val)
5971 {
5972 *tofree = string_quote(r, FALSE);
5973 r = *tofree;
5974 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005976
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005977 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005978#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005979 *tofree = NULL;
5980 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5981 r = numbuf;
5982 break;
5983#endif
5984
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005985 case VAR_SPECIAL:
5986 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005987 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005988 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990
Bram Moolenaar8502c702014-06-17 12:51:16 +02005991 if (--recurse == 0)
5992 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005994}
5995
5996/*
5997 * Return a string with the string representation of a variable.
5998 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5999 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006000 * Does not put quotes around strings, as ":echo" displays values.
6001 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6002 * May return NULL.
6003 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006004 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006005echo_string(
6006 typval_T *tv,
6007 char_u **tofree,
6008 char_u *numbuf,
6009 int copyID)
6010{
6011 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6012}
6013
6014/*
6015 * Return a string with the string representation of a variable.
6016 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6017 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006019 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006020 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006021 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022tv2string(
6023 typval_T *tv,
6024 char_u **tofree,
6025 char_u *numbuf,
6026 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006027{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006028 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029}
6030
6031/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 * Return string "str" in ' quotes, doubling ' characters.
6033 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006035 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006037string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006038{
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006040 char_u *p, *r, *s;
6041
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 len = (function ? 13 : 3);
6043 if (str != NULL)
6044 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006045 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006046 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 if (*p == '\'')
6048 ++len;
6049 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006050 s = r = alloc(len);
6051 if (r != NULL)
6052 {
6053 if (function)
6054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006055 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 r += 10;
6057 }
6058 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006059 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 if (str != NULL)
6061 for (p = str; *p != NUL; )
6062 {
6063 if (*p == '\'')
6064 *r++ = '\'';
6065 MB_COPY_CHAR(p, r);
6066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006067 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006068 if (function)
6069 *r++ = ')';
6070 *r++ = NUL;
6071 }
6072 return s;
6073}
6074
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006075#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006076/*
6077 * Convert the string "text" to a floating point number.
6078 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6079 * this always uses a decimal point.
6080 * Returns the length of the text that was consumed.
6081 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006083string2float(
6084 char_u *text,
6085 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006086{
6087 char *s = (char *)text;
6088 float_T f;
6089
Bram Moolenaar62473612017-01-08 19:25:40 +01006090 /* MS-Windows does not deal with "inf" and "nan" properly. */
6091 if (STRNICMP(text, "inf", 3) == 0)
6092 {
6093 *value = INFINITY;
6094 return 3;
6095 }
6096 if (STRNICMP(text, "-inf", 3) == 0)
6097 {
6098 *value = -INFINITY;
6099 return 4;
6100 }
6101 if (STRNICMP(text, "nan", 3) == 0)
6102 {
6103 *value = NAN;
6104 return 3;
6105 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006106 f = strtod(s, &s);
6107 *value = f;
6108 return (int)((char_u *)s - text);
6109}
6110#endif
6111
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 * Get the value of an environment variable.
6114 * "arg" is pointing to the '$'. It is advanced to after the name.
6115 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006116 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 */
6118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 char_u *string = NULL;
6122 int len;
6123 int cc;
6124 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006125 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 ++*arg;
6128 name = *arg;
6129 len = get_env_len(arg);
6130 if (evaluate)
6131 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006132 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006133 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006134
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006135 cc = name[len];
6136 name[len] = NUL;
6137 /* first try vim_getenv(), fast for normal environment vars */
6138 string = vim_getenv(name, &mustfree);
6139 if (string != NULL && *string != NUL)
6140 {
6141 if (!mustfree)
6142 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006144 else
6145 {
6146 if (mustfree)
6147 vim_free(string);
6148
6149 /* next try expanding things like $VIM and ${HOME} */
6150 string = expand_env_save(name - 1);
6151 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006152 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006153 }
6154 name[len] = cc;
6155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006156 rettv->v_type = VAR_STRING;
6157 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159
6160 return OK;
6161}
6162
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006163
6164
6165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006167 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170var2fpos(
6171 typval_T *varp,
6172 int dollar_lnum, /* TRUE when $ is last line */
6173 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006175 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006177 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
Bram Moolenaara5525202006-03-02 22:52:09 +00006179 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006180 if (varp->v_type == VAR_LIST)
6181 {
6182 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006183 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006184 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006185 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006186
6187 l = varp->vval.v_list;
6188 if (l == NULL)
6189 return NULL;
6190
6191 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006192 pos.lnum = list_find_nr(l, 0L, &error);
6193 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006194 return NULL; /* invalid line number */
6195
6196 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006197 pos.col = list_find_nr(l, 1L, &error);
6198 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006199 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006200 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006201
6202 /* We accept "$" for the column number: last column. */
6203 li = list_find(l, 1L);
6204 if (li != NULL && li->li_tv.v_type == VAR_STRING
6205 && li->li_tv.vval.v_string != NULL
6206 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6207 pos.col = len + 1;
6208
Bram Moolenaara5525202006-03-02 22:52:09 +00006209 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006210 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006211 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006212 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213
Bram Moolenaara5525202006-03-02 22:52:09 +00006214#ifdef FEAT_VIRTUALEDIT
6215 /* Get the virtual offset. Defaults to zero. */
6216 pos.coladd = list_find_nr(l, 2L, &error);
6217 if (error)
6218 pos.coladd = 0;
6219#endif
6220
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006221 return &pos;
6222 }
6223
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006224 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006225 if (name == NULL)
6226 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006227 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006229 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6230 {
6231 if (VIsual_active)
6232 return &VIsual;
6233 return &curwin->w_cursor;
6234 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006235 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006237 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6239 return NULL;
6240 return pp;
6241 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006242
6243#ifdef FEAT_VIRTUALEDIT
6244 pos.coladd = 0;
6245#endif
6246
Bram Moolenaar477933c2007-07-17 14:32:23 +00006247 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006248 {
6249 pos.col = 0;
6250 if (name[1] == '0') /* "w0": first visible line */
6251 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006252 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006253 /* In silent Ex mode topline is zero, but that's not a valid line
6254 * number; use one instead. */
6255 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006256 return &pos;
6257 }
6258 else if (name[1] == '$') /* "w$": last visible line */
6259 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006260 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006261 /* In silent Ex mode botline is zero, return zero then. */
6262 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006263 return &pos;
6264 }
6265 }
6266 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006268 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
6270 pos.lnum = curbuf->b_ml.ml_line_count;
6271 pos.col = 0;
6272 }
6273 else
6274 {
6275 pos.lnum = curwin->w_cursor.lnum;
6276 pos.col = (colnr_T)STRLEN(ml_get_curline());
6277 }
6278 return &pos;
6279 }
6280 return NULL;
6281}
6282
6283/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006284 * Convert list in "arg" into a position and optional file number.
6285 * When "fnump" is NULL there is no file number, only 3 items.
6286 * Note that the column is passed on as-is, the caller may want to decrement
6287 * it to use 1 for the first column.
6288 * Return FAIL when conversion is not possible, doesn't check the position for
6289 * validity.
6290 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006292list2fpos(
6293 typval_T *arg,
6294 pos_T *posp,
6295 int *fnump,
6296 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006297{
6298 list_T *l = arg->vval.v_list;
6299 long i = 0;
6300 long n;
6301
Bram Moolenaar493c1782014-05-28 14:34:46 +02006302 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6303 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006304 if (arg->v_type != VAR_LIST
6305 || l == NULL
6306 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006307 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006308 return FAIL;
6309
6310 if (fnump != NULL)
6311 {
6312 n = list_find_nr(l, i++, NULL); /* fnum */
6313 if (n < 0)
6314 return FAIL;
6315 if (n == 0)
6316 n = curbuf->b_fnum; /* current buffer */
6317 *fnump = n;
6318 }
6319
6320 n = list_find_nr(l, i++, NULL); /* lnum */
6321 if (n < 0)
6322 return FAIL;
6323 posp->lnum = n;
6324
6325 n = list_find_nr(l, i++, NULL); /* col */
6326 if (n < 0)
6327 return FAIL;
6328 posp->col = n;
6329
6330#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006331 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006332 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006333 posp->coladd = 0;
6334 else
6335 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006336#endif
6337
Bram Moolenaar493c1782014-05-28 14:34:46 +02006338 if (curswantp != NULL)
6339 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6340
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006341 return OK;
6342}
6343
6344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 * Get the length of an environment variable name.
6346 * Advance "arg" to the first character after the name.
6347 * Return 0 for error.
6348 */
6349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 char_u *p;
6353 int len;
6354
6355 for (p = *arg; vim_isIDc(*p); ++p)
6356 ;
6357 if (p == *arg) /* no name found */
6358 return 0;
6359
6360 len = (int)(p - *arg);
6361 *arg = p;
6362 return len;
6363}
6364
6365/*
6366 * Get the length of the name of a function or internal variable.
6367 * "arg" is advanced to the first non-white character after the name.
6368 * Return 0 if something is wrong.
6369 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 char_u *p;
6374 int len;
6375
6376 /* Find the end of the name. */
6377 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006378 {
6379 if (*p == ':')
6380 {
6381 /* "s:" is start of "s:var", but "n:" is not and can be used in
6382 * slice "[n:]". Also "xx:" is not a namespace. */
6383 len = (int)(p - *arg);
6384 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6385 || len > 1)
6386 break;
6387 }
6388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (p == *arg) /* no name found */
6390 return 0;
6391
6392 len = (int)(p - *arg);
6393 *arg = skipwhite(p);
6394
6395 return len;
6396}
6397
6398/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006399 * Get the length of the name of a variable or function.
6400 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006402 * Return -1 if curly braces expansion failed.
6403 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 * If the name contains 'magic' {}'s, expand them and return the
6405 * expanded name in an allocated string via 'alias' - caller must free.
6406 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006408get_name_len(
6409 char_u **arg,
6410 char_u **alias,
6411 int evaluate,
6412 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 char_u *p;
6416 char_u *expr_start;
6417 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419 *alias = NULL; /* default to no alias */
6420
6421 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6422 && (*arg)[2] == (int)KE_SNR)
6423 {
6424 /* hard coded <SNR>, already translated */
6425 *arg += 3;
6426 return get_id_len(arg) + 3;
6427 }
6428 len = eval_fname_script(*arg);
6429 if (len > 0)
6430 {
6431 /* literal "<SID>", "s:" or "<SNR>" */
6432 *arg += len;
6433 }
6434
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006436 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006438 p = find_name_end(*arg, &expr_start, &expr_end,
6439 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (expr_start != NULL)
6441 {
6442 char_u *temp_string;
6443
6444 if (!evaluate)
6445 {
6446 len += (int)(p - *arg);
6447 *arg = skipwhite(p);
6448 return len;
6449 }
6450
6451 /*
6452 * Include any <SID> etc in the expanded string:
6453 * Thus the -len here.
6454 */
6455 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6456 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006457 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 *alias = temp_string;
6459 *arg = skipwhite(p);
6460 return (int)STRLEN(temp_string);
6461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
6463 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006464 // Only give an error when there is something, otherwise it will be
6465 // reported at a higher level.
6466 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006467 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
6469 return len;
6470}
6471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006472/*
6473 * Find the end of a variable or function name, taking care of magic braces.
6474 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6475 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006476 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006477 * Return a pointer to just after the name. Equal to "arg" if there is no
6478 * valid name.
6479 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006480 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006481find_name_end(
6482 char_u *arg,
6483 char_u **expr_start,
6484 char_u **expr_end,
6485 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 int mb_nest = 0;
6488 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006490 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006494 *expr_start = NULL;
6495 *expr_end = NULL;
6496 }
6497
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006498 /* Quick check for valid starting character. */
6499 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6500 return arg;
6501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006502 for (p = arg; *p != NUL
6503 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006505 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006506 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006507 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006509 if (*p == '\'')
6510 {
6511 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006512 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006513 ;
6514 if (*p == NUL)
6515 break;
6516 }
6517 else if (*p == '"')
6518 {
6519 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006520 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006521 if (*p == '\\' && p[1] != NUL)
6522 ++p;
6523 if (*p == NUL)
6524 break;
6525 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006526 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6527 {
6528 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006529 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006530 len = (int)(p - arg);
6531 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006532 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006533 break;
6534 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006536 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 if (*p == '[')
6539 ++br_nest;
6540 else if (*p == ']')
6541 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006544 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 if (*p == '{')
6547 {
6548 mb_nest++;
6549 if (expr_start != NULL && *expr_start == NULL)
6550 *expr_start = p;
6551 }
6552 else if (*p == '}')
6553 {
6554 mb_nest--;
6555 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6556 *expr_end = p;
6557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
6560
6561 return p;
6562}
6563
6564/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006565 * Expands out the 'magic' {}'s in a variable/function name.
6566 * Note that this can call itself recursively, to deal with
6567 * constructs like foo{bar}{baz}{bam}
6568 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6569 * "in_start" ^
6570 * "expr_start" ^
6571 * "expr_end" ^
6572 * "in_end" ^
6573 *
6574 * Returns a new allocated string, which the caller must free.
6575 * Returns NULL for failure.
6576 */
6577 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006578make_expanded_name(
6579 char_u *in_start,
6580 char_u *expr_start,
6581 char_u *expr_end,
6582 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006583{
6584 char_u c1;
6585 char_u *retval = NULL;
6586 char_u *temp_result;
6587 char_u *nextcmd = NULL;
6588
6589 if (expr_end == NULL || in_end == NULL)
6590 return NULL;
6591 *expr_start = NUL;
6592 *expr_end = NUL;
6593 c1 = *in_end;
6594 *in_end = NUL;
6595
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006596 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006597 if (temp_result != NULL && nextcmd == NULL)
6598 {
6599 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6600 + (in_end - expr_end) + 1));
6601 if (retval != NULL)
6602 {
6603 STRCPY(retval, in_start);
6604 STRCAT(retval, temp_result);
6605 STRCAT(retval, expr_end + 1);
6606 }
6607 }
6608 vim_free(temp_result);
6609
6610 *in_end = c1; /* put char back for error messages */
6611 *expr_start = '{';
6612 *expr_end = '}';
6613
6614 if (retval != NULL)
6615 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006616 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006617 if (expr_start != NULL)
6618 {
6619 /* Further expansion! */
6620 temp_result = make_expanded_name(retval, expr_start,
6621 expr_end, temp_result);
6622 vim_free(retval);
6623 retval = temp_result;
6624 }
6625 }
6626
6627 return retval;
6628}
6629
6630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006637 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6638}
6639
6640/*
6641 * Return TRUE if character "c" can be used as the first character in a
6642 * variable or function name (excluding '{' and '}').
6643 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006646{
6647 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649
6650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 * Set number v: variable to "val".
6652 */
6653 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006654set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006656 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658
6659/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006660 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006662 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006665 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666}
6667
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006668/*
6669 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006670 * If the String variable has never been set, return an empty string.
6671 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006672 */
6673 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006675{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006676 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006677}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006678
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006680 * Get List v: variable value. Caller must take care of reference count when
6681 * needed.
6682 */
6683 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006684get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006685{
6686 return vimvars[idx].vv_list;
6687}
6688
6689/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006690 * Get Dict v: variable value. Caller must take care of reference count when
6691 * needed.
6692 */
6693 dict_T *
6694get_vim_var_dict(int idx)
6695{
6696 return vimvars[idx].vv_dict;
6697}
6698
6699/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006700 * Set v:char to character "c".
6701 */
6702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006704{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006705 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006706
6707#ifdef FEAT_MBYTE
6708 if (has_mbyte)
6709 buf[(*mb_char2bytes)(c, buf)] = NUL;
6710 else
6711#endif
6712 {
6713 buf[0] = c;
6714 buf[1] = NUL;
6715 }
6716 set_vim_var_string(VV_CHAR, buf, -1);
6717}
6718
6719/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006720 * Set v:count to "count" and v:count1 to "count1".
6721 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 */
6723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006724set_vcount(
6725 long count,
6726 long count1,
6727 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006729 if (set_prevcount)
6730 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006731 vimvars[VV_COUNT].vv_nr = count;
6732 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
6735/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006736 * Save variables that might be changed as a side effect. Used when executing
6737 * a timer callback.
6738 */
6739 void
6740save_vimvars(vimvars_save_T *vvsave)
6741{
6742 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6743 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6744 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6745}
6746
6747/*
6748 * Restore variables saved by save_vimvars().
6749 */
6750 void
6751restore_vimvars(vimvars_save_T *vvsave)
6752{
6753 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6754 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6755 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6756}
6757
6758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 * Set string v: variable to a copy of "val".
6760 */
6761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006762set_vim_var_string(
6763 int idx,
6764 char_u *val,
6765 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
Bram Moolenaara542c682016-01-31 16:28:04 +01006767 clear_tv(&vimvars[idx].vv_di.di_tv);
6768 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006770 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006772 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
6776
6777/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006778 * Set List v: variable to "val".
6779 */
6780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006781set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006782{
Bram Moolenaara542c682016-01-31 16:28:04 +01006783 clear_tv(&vimvars[idx].vv_di.di_tv);
6784 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006785 vimvars[idx].vv_list = val;
6786 if (val != NULL)
6787 ++val->lv_refcount;
6788}
6789
6790/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006791 * Set Dictionary v: variable to "val".
6792 */
6793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006794set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006795{
Bram Moolenaara542c682016-01-31 16:28:04 +01006796 clear_tv(&vimvars[idx].vv_di.di_tv);
6797 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006798 vimvars[idx].vv_dict = val;
6799 if (val != NULL)
6800 {
6801 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006802 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006803 }
6804}
6805
6806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 * Set v:register if needed.
6808 */
6809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006810set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 char_u regname;
6813
6814 if (c == 0 || c == ' ')
6815 regname = '"';
6816 else
6817 regname = c;
6818 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006819 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 set_vim_var_string(VV_REG, &regname, 1);
6821}
6822
6823/*
6824 * Get or set v:exception. If "oldval" == NULL, return the current value.
6825 * Otherwise, restore the value to "oldval" and return NULL.
6826 * Must always be called in pairs to save and restore v:exception! Does not
6827 * take care of memory allocations.
6828 */
6829 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006830v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006833 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 return NULL;
6837}
6838
6839/*
6840 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6841 * Otherwise, restore the value to "oldval" and return NULL.
6842 * Must always be called in pairs to save and restore v:throwpoint! Does not
6843 * take care of memory allocations.
6844 */
6845 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006846v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 return NULL;
6853}
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855/*
6856 * Set v:cmdarg.
6857 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6858 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6859 * Must always be called in pairs!
6860 */
6861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006862set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 char_u *oldval;
6865 char_u *newval;
6866 unsigned len;
6867
Bram Moolenaare9a41262005-01-15 22:18:47 +00006868 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006869 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006871 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006873 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006876 if (eap->force_bin == FORCE_BIN)
6877 len = 6;
6878 else if (eap->force_bin == FORCE_NOBIN)
6879 len = 8;
6880 else
6881 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882
6883 if (eap->read_edit)
6884 len += 7;
6885
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006886 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006887 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006888# ifdef FEAT_MBYTE
6889 if (eap->force_enc != 0)
6890 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006891 if (eap->bad_char != 0)
6892 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006893# endif
6894
6895 newval = alloc(len + 1);
6896 if (newval == NULL)
6897 return NULL;
6898
6899 if (eap->force_bin == FORCE_BIN)
6900 sprintf((char *)newval, " ++bin");
6901 else if (eap->force_bin == FORCE_NOBIN)
6902 sprintf((char *)newval, " ++nobin");
6903 else
6904 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905
6906 if (eap->read_edit)
6907 STRCAT(newval, " ++edit");
6908
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006909 if (eap->force_ff != 0)
6910 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006911 eap->force_ff == 'u' ? "unix"
6912 : eap->force_ff == 'd' ? "dos"
6913 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006914#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006915 if (eap->force_enc != 0)
6916 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6917 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006918 if (eap->bad_char == BAD_KEEP)
6919 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6920 else if (eap->bad_char == BAD_DROP)
6921 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6922 else if (eap->bad_char != 0)
6923 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006924#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006925 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006926 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
6929/*
6930 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006931 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006934get_var_tv(
6935 char_u *name,
6936 int len, /* length of "name" */
6937 typval_T *rettv, /* NULL when only checking existence */
6938 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6939 int verbose, /* may give error message */
6940 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
6942 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947 /* truncate the name, so that we can use strcmp() */
6948 cc = name[len];
6949 name[len] = NUL;
6950
6951 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 * Check for user-defined variables.
6953 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006954 v = find_var(name, NULL, no_autoload);
6955 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006957 tv = &v->di_tv;
6958 if (dip != NULL)
6959 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006964 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006965 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 ret = FAIL;
6967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006968 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 name[len] = cc;
6972
6973 return ret;
6974}
6975
6976/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006977 * Check if variable "name[len]" is a local variable or an argument.
6978 * If so, "*eval_lavars_used" is set to TRUE.
6979 */
6980 static void
6981check_vars(char_u *name, int len)
6982{
6983 int cc;
6984 char_u *varname;
6985 hashtab_T *ht;
6986
6987 if (eval_lavars_used == NULL)
6988 return;
6989
6990 /* truncate the name, so that we can use strcmp() */
6991 cc = name[len];
6992 name[len] = NUL;
6993
6994 ht = find_var_ht(name, &varname);
6995 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6996 {
6997 if (find_var(name, NULL, TRUE) != NULL)
6998 *eval_lavars_used = TRUE;
6999 }
7000
7001 name[len] = cc;
7002}
7003
7004/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007005 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7006 * Also handle function call with Funcref variable: func(expr)
7007 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7008 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007010handle_subscript(
7011 char_u **arg,
7012 typval_T *rettv,
7013 int evaluate, /* do more than finding the end */
7014 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007015{
7016 int ret = OK;
7017 dict_T *selfdict = NULL;
7018 char_u *s;
7019 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007020 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007021
7022 while (ret == OK
7023 && (**arg == '['
7024 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007025 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7026 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007027 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007028 {
7029 if (**arg == '(')
7030 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007031 partial_T *pt = NULL;
7032
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007034 if (evaluate)
7035 {
7036 functv = *rettv;
7037 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007038
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007039 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007040 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007041 {
7042 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007043 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007044 }
7045 else
7046 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007047 }
7048 else
7049 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007050 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007051 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007052 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007053
7054 /* Clear the funcref afterwards, so that deleting it while
7055 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007056 if (evaluate)
7057 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007058
7059 /* Stop the expression evaluation when immediately aborting on
7060 * error, or when an interrupt occurred or an exception was thrown
7061 * but not caught. */
7062 if (aborting())
7063 {
7064 if (ret == OK)
7065 clear_tv(rettv);
7066 ret = FAIL;
7067 }
7068 dict_unref(selfdict);
7069 selfdict = NULL;
7070 }
7071 else /* **arg == '[' || **arg == '.' */
7072 {
7073 dict_unref(selfdict);
7074 if (rettv->v_type == VAR_DICT)
7075 {
7076 selfdict = rettv->vval.v_dict;
7077 if (selfdict != NULL)
7078 ++selfdict->dv_refcount;
7079 }
7080 else
7081 selfdict = NULL;
7082 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7083 {
7084 clear_tv(rettv);
7085 ret = FAIL;
7086 }
7087 }
7088 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007089
Bram Moolenaar1d429612016-05-24 15:44:17 +02007090 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7091 * Don't do this when "Func" is already a partial that was bound
7092 * explicitly (pt_auto is FALSE). */
7093 if (selfdict != NULL
7094 && (rettv->v_type == VAR_FUNC
7095 || (rettv->v_type == VAR_PARTIAL
7096 && (rettv->vval.v_partial->pt_auto
7097 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007098 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007099
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007100 dict_unref(selfdict);
7101 return ret;
7102}
7103
7104/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007105 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007106 * value).
7107 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007108 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007110{
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112}
7113
7114/*
7115 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 * The string "s" must have been allocated, it is consumed.
7117 * Return NULL for out of memory, the variable otherwise.
7118 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007120alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007124 rettv = alloc_tv();
7125 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007127 rettv->v_type = VAR_STRING;
7128 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130 else
7131 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007132 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133}
7134
7135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007136 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 if (varp != NULL)
7142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007143 switch (varp->v_type)
7144 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007145 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007146 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007147 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007148 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007149 vim_free(varp->vval.v_string);
7150 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007151 case VAR_PARTIAL:
7152 partial_unref(varp->vval.v_partial);
7153 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007154 case VAR_BLOB:
7155 blob_unref(varp->vval.v_blob);
7156 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007157 case VAR_LIST:
7158 list_unref(varp->vval.v_list);
7159 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007160 case VAR_DICT:
7161 dict_unref(varp->vval.v_dict);
7162 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007163 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007164#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007165 job_unref(varp->vval.v_job);
7166 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007167#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007168 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007169#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007170 channel_unref(varp->vval.v_channel);
7171 break;
7172#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007173 case VAR_NUMBER:
7174 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007175 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007176 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007177 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 vim_free(varp);
7180 }
7181}
7182
7183/*
7184 * Free the memory for a variable value and set the value to NULL or 0.
7185 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 if (varp != NULL)
7190 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007191 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007193 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007194 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007195 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007196 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007197 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007198 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007199 case VAR_PARTIAL:
7200 partial_unref(varp->vval.v_partial);
7201 varp->vval.v_partial = NULL;
7202 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007203 case VAR_BLOB:
7204 blob_unref(varp->vval.v_blob);
7205 varp->vval.v_blob = NULL;
7206 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007207 case VAR_LIST:
7208 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007210 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211 case VAR_DICT:
7212 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007215 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007216 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 varp->vval.v_number = 0;
7218 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007219 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007220#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007221 varp->vval.v_float = 0.0;
7222 break;
7223#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007224 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007225#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007226 job_unref(varp->vval.v_job);
7227 varp->vval.v_job = NULL;
7228#endif
7229 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007230 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007231#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007232 channel_unref(varp->vval.v_channel);
7233 varp->vval.v_channel = NULL;
7234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007235 case VAR_UNKNOWN:
7236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007238 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 }
7240}
7241
7242/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 * Set the value of a variable to NULL without freeing items.
7244 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007246init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007247{
7248 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007250}
7251
7252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 * Get the number value of a variable.
7254 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007255 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007256 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007257 * caller of incompatible types: it sets *denote to TRUE if "denote"
7258 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007260 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007261tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007263 int error = FALSE;
7264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007265 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007266}
7267
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007268 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007269tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007270{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007271 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007276 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007277 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007278#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007279 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007280 break;
7281#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007283 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007284 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007285 break;
7286 case VAR_STRING:
7287 if (varp->vval.v_string != NULL)
7288 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007289 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007290 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007291 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007292 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007293 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007294 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007295 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007296 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007297 case VAR_SPECIAL:
7298 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7299 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007300 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007302 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007303 break;
7304#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007305 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007306#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007307 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007308 break;
7309#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007310 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007311 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007312 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007313 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007314 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007315 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 if (denote == NULL) /* useful for values that must be unsigned */
7318 n = -1;
7319 else
7320 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007321 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
7323
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007324#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007326tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007327{
7328 switch (varp->v_type)
7329 {
7330 case VAR_NUMBER:
7331 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007332 case VAR_FLOAT:
7333 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007334 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007335 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007336 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007337 break;
7338 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007339 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007340 break;
7341 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007342 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007343 break;
7344 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007345 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007346 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007347 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007348 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007349 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007350 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007351# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007352 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007353 break;
7354# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007355 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007356# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007357 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007358 break;
7359# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007360 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007361 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007362 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007363 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007364 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007365 break;
7366 }
7367 return 0;
7368}
7369#endif
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 * Get the string value of a variable.
7373 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007374 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7375 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 * If the String variable has never been set, return an empty string.
7377 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007378 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007379 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007381 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007382tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 static char_u mybuf[NUMBUFLEN];
7385
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007386 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387}
7388
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007389 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007390tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007392 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007393
7394 return res != NULL ? res : (char_u *)"";
7395}
7396
Bram Moolenaar7d647822014-04-05 21:28:56 +02007397/*
7398 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7399 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007400 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007401tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007402{
7403 static char_u mybuf[NUMBUFLEN];
7404
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007405 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406}
7407
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007408 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007409tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007413 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007414 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007415 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007416 return buf;
7417 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007418 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007419 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420 break;
7421 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007422 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 break;
7424 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007425 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007426 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007427 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007428#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007430 break;
7431#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 case VAR_STRING:
7433 if (varp->vval.v_string != NULL)
7434 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007435 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007436 case VAR_SPECIAL:
7437 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7438 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007439 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007440 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007441 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007442 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007443#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007444 {
7445 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007446 char *status;
7447
7448 if (job == NULL)
7449 return (char_u *)"no process";
7450 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007451 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007452 : "run";
7453# ifdef UNIX
7454 vim_snprintf((char *)buf, NUMBUFLEN,
7455 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007456# elif defined(WIN32)
7457 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007458 "process %ld %s",
7459 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007460 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007461# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007462 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007463 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7464# endif
7465 return buf;
7466 }
7467#endif
7468 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007469 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007470#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007471 {
7472 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007473 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007474
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007475 if (channel == NULL)
7476 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7477 else
7478 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007479 "channel %d %s", channel->ch_id, status);
7480 return buf;
7481 }
7482#endif
7483 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007484 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007485 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007488 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007492 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7493 * string() on Dict, List, etc.
7494 */
7495 char_u *
7496tv_stringify(typval_T *varp, char_u *buf)
7497{
7498 if (varp->v_type == VAR_LIST
7499 || varp->v_type == VAR_DICT
7500 || varp->v_type == VAR_FUNC
7501 || varp->v_type == VAR_PARTIAL
7502 || varp->v_type == VAR_FLOAT)
7503 {
7504 typval_T tmp;
7505
7506 f_string(varp, &tmp);
7507 tv_get_string_buf(&tmp, buf);
7508 clear_tv(varp);
7509 *varp = tmp;
7510 return tmp.vval.v_string;
7511 }
7512 return tv_get_string_buf(varp, buf);
7513}
7514
7515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 * Find variable "name" in the list of variables.
7517 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007518 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007519 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007522 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007527 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
Bram Moolenaara7043832005-01-21 11:56:39 +00007529 ht = find_var_ht(name, &varname);
7530 if (htp != NULL)
7531 *htp = ht;
7532 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007534 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7535 if (ret != NULL)
7536 return ret;
7537
7538 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007539 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540}
7541
7542/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007543 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007544 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547find_var_in_ht(
7548 hashtab_T *ht,
7549 int htname,
7550 char_u *varname,
7551 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007552{
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 hashitem_T *hi;
7554
7555 if (*varname == NUL)
7556 {
7557 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007558 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007560 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 case 'g': return &globvars_var;
7562 case 'v': return &vimvars_var;
7563 case 'b': return &curbuf->b_bufvar;
7564 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007565 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007566 case 'l': return get_funccal_local_var();
7567 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 }
7569 return NULL;
7570 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007571
7572 hi = hash_find(ht, varname);
7573 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007574 {
7575 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007576 * worked find the variable again. Don't auto-load a script if it was
7577 * loaded already, otherwise it would be loaded every time when
7578 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007579 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007580 {
7581 /* Note: script_autoload() may make "hi" invalid. It must either
7582 * be obtained again or not used. */
7583 if (!script_autoload(varname, FALSE) || aborting())
7584 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007585 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007586 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 if (HASHITEM_EMPTY(hi))
7588 return NULL;
7589 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007591}
7592
7593/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007595 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007596 * Set "varname" to the start of name without ':'.
7597 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007598 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007601 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007602 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007603
Bram Moolenaar73627d02015-08-11 15:46:09 +02007604 if (name[0] == NUL)
7605 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (name[1] != ':')
7607 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007608 /* The name must not start with a colon or #. */
7609 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 return NULL;
7611 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007612
7613 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007614 hi = hash_find(&compat_hashtab, name);
7615 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007616 return &compat_hashtab;
7617
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007618 ht = get_funccal_local_ht();
7619 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007621 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
7623 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007624 if (*name == 'g') /* global variable */
7625 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007626 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7627 */
7628 if (vim_strchr(name + 2, ':') != NULL
7629 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007630 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007632 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007634 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007635 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007636 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 if (*name == 'v') /* v: variable */
7638 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007639 if (*name == 'a') /* a: function argument */
7640 return get_funccal_args_ht();
7641 if (*name == 'l') /* l: local function variable */
7642 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007644 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7645 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 return NULL;
7647}
7648
7649/*
7650 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007651 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 * Returns NULL when it doesn't exist.
7653 */
7654 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007655get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007659 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 if (v == NULL)
7661 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007662 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663}
7664
7665/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 * sourcing this script and when executing functions defined in the script.
7668 */
7669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007670new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
Bram Moolenaara7043832005-01-21 11:56:39 +00007672 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 hashtab_T *ht;
7674 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7677 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007678 /* Re-allocating ga_data means that an ht_array pointing to
7679 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007681 for (i = 1; i <= ga_scripts.ga_len; ++i)
7682 {
7683 ht = &SCRIPT_VARS(i);
7684 if (ht->ht_mask == HT_INIT_SIZE - 1)
7685 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007686 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007688 }
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 while (ga_scripts.ga_len < id)
7691 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007692 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007693 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007694 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
7697 }
7698}
7699
7700/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7702 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 */
7704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007708 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007709 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007710 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007711 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 dict_var->di_tv.vval.v_dict = dict;
7713 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007714 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7716 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717}
7718
7719/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007720 * Unreference a dictionary initialized by init_var_dict().
7721 */
7722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007723unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007724{
7725 /* Now the dict needs to be freed if no one else is using it, go back to
7726 * normal reference counting. */
7727 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7728 dict_unref(dict);
7729}
7730
7731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 * Frees all allocated variables and the value they contain.
7734 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 */
7736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007737vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007738{
7739 vars_clear_ext(ht, TRUE);
7740}
7741
7742/*
7743 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7744 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007745 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007746vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
Bram Moolenaara7043832005-01-21 11:56:39 +00007748 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 hashitem_T *hi;
7750 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007753 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007754 for (hi = ht->ht_array; todo > 0; ++hi)
7755 {
7756 if (!HASHITEM_EMPTY(hi))
7757 {
7758 --todo;
7759
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007761 * ht_array might change then. hash_clear() takes care of it
7762 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 v = HI2DI(hi);
7764 if (free_val)
7765 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007766 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007768 }
7769 }
7770 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007771 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772}
7773
Bram Moolenaara7043832005-01-21 11:56:39 +00007774/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 * Delete a variable from hashtab "ht" at item "hi".
7776 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007779delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007782
7783 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 clear_tv(&di->di_tv);
7785 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786}
7787
7788/*
7789 * List the value of one internal variable.
7790 */
7791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007792list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 char_u *tofree;
7795 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007796 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007798 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007800 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802}
7803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805list_one_var_a(
7806 char_u *prefix,
7807 char_u *name,
7808 int type,
7809 char_u *string,
7810 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
Bram Moolenaar31859182007-08-14 20:41:13 +00007812 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7813 msg_start();
7814 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 if (name != NULL) /* "a:" vars don't have a name stored */
7816 msg_puts(name);
7817 msg_putchar(' ');
7818 msg_advance(22);
7819 if (type == VAR_NUMBER)
7820 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007821 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822 msg_putchar('*');
7823 else if (type == VAR_LIST)
7824 {
7825 msg_putchar('[');
7826 if (*string == '[')
7827 ++string;
7828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829 else if (type == VAR_DICT)
7830 {
7831 msg_putchar('{');
7832 if (*string == '{')
7833 ++string;
7834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 else
7836 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007837
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007840 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007842 if (*first)
7843 {
7844 msg_clr_eos();
7845 *first = FALSE;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847}
7848
7849/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 * If the variable already exists, the value is updated.
7852 * Otherwise the variable is created.
7853 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007855set_var(
7856 char_u *name,
7857 typval_T *tv,
7858 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007864 ht = find_var_ht(name, &varname);
7865 if (ht == NULL || *varname == NUL)
7866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007867 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007868 return;
7869 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007870 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007871
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007872 /* Search in parent scope which is possible to reference from lambda */
7873 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007874 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007875
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007876 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7877 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007878 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007883 if (var_check_ro(v->di_flags, name, FALSE)
7884 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007886
7887 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007888 * Handle setting internal v: variables separately where needed to
7889 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 */
7891 if (ht == &vimvarht)
7892 {
7893 if (v->di_tv.v_type == VAR_STRING)
7894 {
7895 vim_free(v->di_tv.vval.v_string);
7896 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007897 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007898 else
7899 {
7900 /* Take over the string to avoid an extra alloc/free. */
7901 v->di_tv.vval.v_string = tv->vval.v_string;
7902 tv->vval.v_string = NULL;
7903 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007904 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007906 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007908 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909 if (STRCMP(varname, "searchforward") == 0)
7910 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007911#ifdef FEAT_SEARCH_EXTRA
7912 else if (STRCMP(varname, "hlsearch") == 0)
7913 {
7914 no_hlsearch = !v->di_tv.vval.v_number;
7915 redraw_all_later(SOME_VALID);
7916 }
7917#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007918 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007920 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007922 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007923 return;
7924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 }
7926
7927 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929 else /* add a new variable */
7930 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007931 /* Can't add "v:" variable. */
7932 if (ht == &vimvarht)
7933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007934 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007935 return;
7936 }
7937
Bram Moolenaar92124a32005-06-17 22:03:40 +00007938 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007939 if (!valid_varname(varname))
7940 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007941
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007942 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7943 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007944 if (v == NULL)
7945 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007949 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 return;
7951 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007952 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007954
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007955 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007957 else
7958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007960 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007965/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007966 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007967 * Also give an error message.
7968 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007970var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007971{
7972 if (flags & DI_FLAGS_RO)
7973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007974 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 return TRUE;
7976 }
7977 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007979 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 return TRUE;
7981 }
7982 return FALSE;
7983}
7984
7985/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007986 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7987 * Also give an error message.
7988 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007990var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007991{
7992 if (flags & DI_FLAGS_FIX)
7993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007994 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007995 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007996 return TRUE;
7997 }
7998 return FALSE;
7999}
8000
8001/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008002 * Check if a funcref is assigned to a valid variable name.
8003 * Return TRUE and give an error if not.
8004 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008005 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008006var_check_func_name(
8007 char_u *name, /* points to start of variable name */
8008 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008009{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008010 /* Allow for w: b: s: and t:. */
8011 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008012 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8013 ? name[2] : name[0]))
8014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008015 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008016 name);
8017 return TRUE;
8018 }
8019 /* Don't allow hiding a function. When "v" is not NULL we might be
8020 * assigning another function to the same var, the type is checked
8021 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008022 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008024 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008025 name);
8026 return TRUE;
8027 }
8028 return FALSE;
8029}
8030
8031/*
8032 * Check if a variable name is valid.
8033 * Return FALSE and give an error if not.
8034 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008036valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008037{
8038 char_u *p;
8039
8040 for (p = varname; *p != NUL; ++p)
8041 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8042 && *p != AUTOLOAD_CHAR)
8043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008044 semsg(_(e_illvar), varname);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008045 return FALSE;
8046 }
8047 return TRUE;
8048}
8049
8050/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008051 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008052 * Also give an error message, using "name" or _("name") when use_gettext is
8053 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008054 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008056tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008057{
8058 if (lock & VAR_LOCKED)
8059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008060 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008061 name == NULL ? (char_u *)_("Unknown")
8062 : use_gettext ? (char_u *)_(name)
8063 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008064 return TRUE;
8065 }
8066 if (lock & VAR_FIXED)
8067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008068 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008069 name == NULL ? (char_u *)_("Unknown")
8070 : use_gettext ? (char_u *)_(name)
8071 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008072 return TRUE;
8073 }
8074 return FALSE;
8075}
8076
8077/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008079 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008080 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008081 * It is OK for "from" and "to" to point to the same item. This is used to
8082 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008083 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008088 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 switch (from->v_type)
8090 {
8091 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008092 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008093 to->vval.v_number = from->vval.v_number;
8094 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008096#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097 to->vval.v_float = from->vval.v_float;
8098 break;
8099#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008100 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008101#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008102 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008103 if (to->vval.v_job != NULL)
8104 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008105 break;
8106#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008107 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008108#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008109 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008110 if (to->vval.v_channel != NULL)
8111 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008112 break;
8113#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114 case VAR_STRING:
8115 case VAR_FUNC:
8116 if (from->vval.v_string == NULL)
8117 to->vval.v_string = NULL;
8118 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008119 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008120 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008121 if (from->v_type == VAR_FUNC)
8122 func_ref(to->vval.v_string);
8123 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008125 case VAR_PARTIAL:
8126 if (from->vval.v_partial == NULL)
8127 to->vval.v_partial = NULL;
8128 else
8129 {
8130 to->vval.v_partial = from->vval.v_partial;
8131 ++to->vval.v_partial->pt_refcount;
8132 }
8133 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008134 case VAR_BLOB:
8135 if (from->vval.v_blob == NULL)
8136 to->vval.v_blob = NULL;
8137 else
8138 {
8139 to->vval.v_blob = from->vval.v_blob;
8140 ++to->vval.v_blob->bv_refcount;
8141 }
8142 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 case VAR_LIST:
8144 if (from->vval.v_list == NULL)
8145 to->vval.v_list = NULL;
8146 else
8147 {
8148 to->vval.v_list = from->vval.v_list;
8149 ++to->vval.v_list->lv_refcount;
8150 }
8151 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008152 case VAR_DICT:
8153 if (from->vval.v_dict == NULL)
8154 to->vval.v_dict = NULL;
8155 else
8156 {
8157 to->vval.v_dict = from->vval.v_dict;
8158 ++to->vval.v_dict->dv_refcount;
8159 }
8160 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008161 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008162 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008163 break;
8164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
8167/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 * Make a copy of an item.
8169 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008170 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8171 * reference to an already copied list/dict can be used.
8172 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008173 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008175item_copy(
8176 typval_T *from,
8177 typval_T *to,
8178 int deep,
8179 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180{
8181 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008182 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008183
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008186 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008187 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 }
8189 ++recurse;
8190
8191 switch (from->v_type)
8192 {
8193 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008194 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008195 case VAR_STRING:
8196 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008197 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008198 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008199 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008200 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 copy_tv(from, to);
8202 break;
8203 case VAR_LIST:
8204 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008205 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008206 if (from->vval.v_list == NULL)
8207 to->vval.v_list = NULL;
8208 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8209 {
8210 /* use the copy made earlier */
8211 to->vval.v_list = from->vval.v_list->lv_copylist;
8212 ++to->vval.v_list->lv_refcount;
8213 }
8214 else
8215 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8216 if (to->vval.v_list == NULL)
8217 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008219 case VAR_BLOB:
8220 to->v_type = VAR_BLOB;
8221 if (from->vval.v_blob == NULL)
8222 to->vval.v_blob = NULL;
8223 else if (rettv_blob_alloc(to) == FAIL)
8224 ret = FAIL;
8225 else
8226 {
8227 int len = from->vval.v_blob->bv_ga.ga_len;
8228
8229 to->vval.v_blob->bv_ga.ga_data =
8230 vim_memsave(from->vval.v_blob->bv_ga.ga_data, len);
8231 to->vval.v_blob->bv_ga.ga_len = len;
8232 }
8233 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008234 case VAR_DICT:
8235 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008236 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008237 if (from->vval.v_dict == NULL)
8238 to->vval.v_dict = NULL;
8239 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8240 {
8241 /* use the copy made earlier */
8242 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8243 ++to->vval.v_dict->dv_refcount;
8244 }
8245 else
8246 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8247 if (to->vval.v_dict == NULL)
8248 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008249 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008250 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008251 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008252 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008253 }
8254 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008255 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008256}
8257
8258/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 * This function is used by f_input() and f_inputdialog() functions. The third
8260 * argument to f_input() specifies the type of completion to use at the
8261 * prompt. The third argument to f_inputdialog() specifies the value to return
8262 * when the user cancels the prompt.
8263 */
8264 void
8265get_user_input(
8266 typval_T *argvars,
8267 typval_T *rettv,
8268 int inputdialog,
8269 int secret)
8270{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008271 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 char_u *p = NULL;
8273 int c;
8274 char_u buf[NUMBUFLEN];
8275 int cmd_silent_save = cmd_silent;
8276 char_u *defstr = (char_u *)"";
8277 int xp_type = EXPAND_NOTHING;
8278 char_u *xp_arg = NULL;
8279
8280 rettv->v_type = VAR_STRING;
8281 rettv->vval.v_string = NULL;
8282
8283#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008284 /* While starting up, there is no place to enter text. When running tests
8285 * with --not-a-term we assume feedkeys() will be used. */
8286 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 return;
8288#endif
8289
8290 cmd_silent = FALSE; /* Want to see the prompt. */
8291 if (prompt != NULL)
8292 {
8293 /* Only the part of the message after the last NL is considered as
8294 * prompt for the command line */
8295 p = vim_strrchr(prompt, '\n');
8296 if (p == NULL)
8297 p = prompt;
8298 else
8299 {
8300 ++p;
8301 c = *p;
8302 *p = NUL;
8303 msg_start();
8304 msg_clr_eos();
8305 msg_puts_attr(prompt, echo_attr);
8306 msg_didout = FALSE;
8307 msg_starthere();
8308 *p = c;
8309 }
8310 cmdline_row = msg_row;
8311
8312 if (argvars[1].v_type != VAR_UNKNOWN)
8313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 if (defstr != NULL)
8316 stuffReadbuffSpec(defstr);
8317
8318 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8319 {
8320 char_u *xp_name;
8321 int xp_namelen;
8322 long argt;
8323
8324 /* input() with a third argument: completion */
8325 rettv->vval.v_string = NULL;
8326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008327 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 if (xp_name == NULL)
8329 return;
8330
8331 xp_namelen = (int)STRLEN(xp_name);
8332
8333 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8334 &xp_arg) == FAIL)
8335 return;
8336 }
8337 }
8338
8339 if (defstr != NULL)
8340 {
8341 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008342
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 ex_normal_busy = 0;
8344 rettv->vval.v_string =
8345 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8346 xp_type, xp_arg);
8347 ex_normal_busy = save_ex_normal_busy;
8348 }
8349 if (inputdialog && rettv->vval.v_string == NULL
8350 && argvars[1].v_type != VAR_UNKNOWN
8351 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008352 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 &argvars[2], buf));
8354
8355 vim_free(xp_arg);
8356
8357 /* since the user typed this, no need to wait for return */
8358 need_wait_return = FALSE;
8359 msg_didout = FALSE;
8360 }
8361 cmd_silent = cmd_silent_save;
8362}
8363
8364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 * ":echo expr1 ..." print each argument separated with a space, add a
8366 * newline at the end.
8367 * ":echon expr1 ..." print each argument plain.
8368 */
8369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008370ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 char_u *p;
8376 int needclr = TRUE;
8377 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008378 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008379 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008380 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
8382 if (eap->skip)
8383 ++emsg_skip;
8384 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8385 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386 /* If eval1() causes an error message the text from the command may
8387 * still need to be cleared. E.g., "echo 22,44". */
8388 need_clr_eos = needclr;
8389
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {
8393 /*
8394 * Report the invalid expression unless the expression evaluation
8395 * has been cancelled due to an aborting error, an interrupt, or an
8396 * exception.
8397 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008398 if (!aborting() && did_emsg == did_emsg_before
8399 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008400 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 break;
8403 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404 need_clr_eos = FALSE;
8405
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 if (!eap->skip)
8407 {
8408 if (atstart)
8409 {
8410 atstart = FALSE;
8411 /* Call msg_start() after eval1(), evaluating the expression
8412 * may cause a message to appear. */
8413 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008414 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008415 /* Mark the saved text as finishing the line, so that what
8416 * follows is displayed on a new line when scrolling back
8417 * at the more prompt. */
8418 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422 else if (eap->cmdidx == CMD_echo)
8423 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008424 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008425 if (p != NULL)
8426 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008428 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008430 if (*p != TAB && needclr)
8431 {
8432 /* remove any text still there from the command */
8433 msg_clr_eos();
8434 needclr = FALSE;
8435 }
8436 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
8438 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439 {
8440#ifdef FEAT_MBYTE
8441 if (has_mbyte)
8442 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008443 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008444
8445 (void)msg_outtrans_len_attr(p, i, echo_attr);
8446 p += i - 1;
8447 }
8448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008450 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008453 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008455 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 arg = skipwhite(arg);
8457 }
8458 eap->nextcmd = check_nextcmd(arg);
8459
8460 if (eap->skip)
8461 --emsg_skip;
8462 else
8463 {
8464 /* remove text that may still be there from the command */
8465 if (needclr)
8466 msg_clr_eos();
8467 if (eap->cmdidx == CMD_echo)
8468 msg_end();
8469 }
8470}
8471
8472/*
8473 * ":echohl {name}".
8474 */
8475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008476ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008478 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479}
8480
8481/*
8482 * ":execute expr1 ..." execute the result of an expression.
8483 * ":echomsg expr1 ..." Print a message
8484 * ":echoerr expr1 ..." Print an error
8485 * Each gets spaces around each argument and a newline at the end for
8486 * echo commands
8487 */
8488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008489ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 int ret = OK;
8494 char_u *p;
8495 garray_T ga;
8496 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008497 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498
8499 ga_init2(&ga, 1, 80);
8500
8501 if (eap->skip)
8502 ++emsg_skip;
8503 while (*arg != NUL && *arg != '|' && *arg != '\n')
8504 {
8505 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008506 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8507 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
8510 if (!eap->skip)
8511 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008512 char_u buf[NUMBUFLEN];
8513
8514 if (eap->cmdidx == CMD_execute)
8515 p = tv_get_string_buf(&rettv, buf);
8516 else
8517 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 len = (int)STRLEN(p);
8519 if (ga_grow(&ga, len + 2) == FAIL)
8520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 ret = FAIL;
8523 break;
8524 }
8525 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 ga.ga_len += len;
8529 }
8530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 arg = skipwhite(arg);
8533 }
8534
8535 if (ret != FAIL && ga.ga_data != NULL)
8536 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008537 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8538 {
8539 /* Mark the already saved text as finishing the line, so that what
8540 * follows is displayed on a new line when scrolling back at the
8541 * more prompt. */
8542 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008543 }
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008548 out_flush();
8549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 else if (eap->cmdidx == CMD_echoerr)
8551 {
8552 /* We don't want to abort following commands, restore did_emsg. */
8553 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008554 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (!force_abort)
8556 did_emsg = save_did_emsg;
8557 }
8558 else if (eap->cmdidx == CMD_execute)
8559 do_cmdline((char_u *)ga.ga_data,
8560 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8561 }
8562
8563 ga_clear(&ga);
8564
8565 if (eap->skip)
8566 --emsg_skip;
8567
8568 eap->nextcmd = check_nextcmd(arg);
8569}
8570
8571/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 * Find window specified by "vp" in tabpage "tp".
8573 */
8574 win_T *
8575find_win_by_nr(
8576 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008577 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 if (nr < 0)
8583 return NULL;
8584 if (nr == 0)
8585 return curwin;
8586
Bram Moolenaar29323592016-07-24 22:04:11 +02008587 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8588 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 if (nr >= LOWEST_WIN_ID)
8590 {
8591 if (wp->w_id == nr)
8592 return wp;
8593 }
8594 else if (--nr <= 0)
8595 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008596 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 if (nr >= LOWEST_WIN_ID)
8598 return NULL;
8599 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600}
8601
8602/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008603 * Find a window: When using a Window ID in any tab page, when using a number
8604 * in the current tab page.
8605 */
8606 win_T *
8607find_win_by_nr_or_id(typval_T *vp)
8608{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008609 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008610
8611 if (nr >= LOWEST_WIN_ID)
8612 return win_id2wp(vp);
8613 return find_win_by_nr(vp, NULL);
8614}
8615
8616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 * Find window specified by "wvp" in tabpage "tvp".
8618 */
8619 win_T *
8620find_tabwin(
8621 typval_T *wvp, /* VAR_UNKNOWN for current window */
8622 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8623{
8624 win_T *wp = NULL;
8625 tabpage_T *tp = NULL;
8626 long n;
8627
8628 if (wvp->v_type != VAR_UNKNOWN)
8629 {
8630 if (tvp->v_type != VAR_UNKNOWN)
8631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008632 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633 if (n >= 0)
8634 tp = find_tabpage(n);
8635 }
8636 else
8637 tp = curtab;
8638
8639 if (tp != NULL)
8640 wp = find_win_by_nr(wvp, tp);
8641 }
8642 else
8643 wp = curwin;
8644
8645 return wp;
8646}
8647
8648/*
8649 * getwinvar() and gettabwinvar()
8650 */
8651 void
8652getwinvar(
8653 typval_T *argvars,
8654 typval_T *rettv,
8655 int off) /* 1 for gettabwinvar() */
8656{
8657 win_T *win;
8658 char_u *varname;
8659 dictitem_T *v;
8660 tabpage_T *tp = NULL;
8661 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 win_T *oldcurwin;
8663 tabpage_T *oldtabpage;
8664 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008667 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 else
8669 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008671 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 ++emsg_off;
8673
8674 rettv->v_type = VAR_STRING;
8675 rettv->vval.v_string = NULL;
8676
8677 if (win != NULL && varname != NULL)
8678 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 /* Set curwin to be our win, temporarily. Also set the tabpage,
8680 * otherwise the window is not valid. Only do this when needed,
8681 * autocommands get blocked. */
8682 need_switch_win = !(tp == curtab && win == curwin);
8683 if (!need_switch_win
8684 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008686 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008688 if (varname[1] == NUL)
8689 {
8690 /* get all window-local options in a dict */
8691 dict_T *opts = get_winbuf_options(FALSE);
8692
8693 if (opts != NULL)
8694 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008695 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008696 done = TRUE;
8697 }
8698 }
8699 else if (get_option_tv(&varname, rettv, 1) == OK)
8700 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 done = TRUE;
8702 }
8703 else
8704 {
8705 /* Look up the variable. */
8706 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8707 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8708 varname, FALSE);
8709 if (v != NULL)
8710 {
8711 copy_tv(&v->di_tv, rettv);
8712 done = TRUE;
8713 }
8714 }
8715 }
8716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 if (need_switch_win)
8718 /* restore previous notion of curwin */
8719 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 }
8721
8722 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8723 /* use the default return value */
8724 copy_tv(&argvars[off + 2], rettv);
8725
8726 --emsg_off;
8727}
8728
8729/*
8730 * "setwinvar()" and "settabwinvar()" functions
8731 */
8732 void
8733setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8734{
8735 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 win_T *save_curwin;
8737 tabpage_T *save_curtab;
8738 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 char_u *varname, *winvarname;
8740 typval_T *varp;
8741 char_u nbuf[NUMBUFLEN];
8742 tabpage_T *tp = NULL;
8743
8744 if (check_restricted() || check_secure())
8745 return;
8746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008748 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 else
8750 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008752 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 varp = &argvars[off + 2];
8754
8755 if (win != NULL && varname != NULL && varp != NULL)
8756 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 need_switch_win = !(tp == curtab && win == curwin);
8758 if (!need_switch_win
8759 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 {
8761 if (*varname == '&')
8762 {
8763 long numval;
8764 char_u *strval;
8765 int error = FALSE;
8766
8767 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008768 numval = (long)tv_get_number_chk(varp, &error);
8769 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 if (!error && strval != NULL)
8771 set_option_value(varname, numval, strval, OPT_LOCAL);
8772 }
8773 else
8774 {
8775 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8776 if (winvarname != NULL)
8777 {
8778 STRCPY(winvarname, "w:");
8779 STRCPY(winvarname + 2, varname);
8780 set_var(winvarname, varp, TRUE);
8781 vim_free(winvarname);
8782 }
8783 }
8784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 if (need_switch_win)
8786 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 }
8788}
8789
8790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8792 * "arg" points to the "&" or '+' when called, to "option" when returning.
8793 * Returns NULL when no option name found. Otherwise pointer to the char
8794 * after the option name.
8795 */
8796 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008797find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 char_u *p = *arg;
8800
8801 ++p;
8802 if (*p == 'g' && p[1] == ':')
8803 {
8804 *opt_flags = OPT_GLOBAL;
8805 p += 2;
8806 }
8807 else if (*p == 'l' && p[1] == ':')
8808 {
8809 *opt_flags = OPT_LOCAL;
8810 p += 2;
8811 }
8812 else
8813 *opt_flags = 0;
8814
8815 if (!ASCII_ISALPHA(*p))
8816 return NULL;
8817 *arg = p;
8818
8819 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8820 p += 4; /* termcap option */
8821 else
8822 while (ASCII_ISALPHA(*p))
8823 ++p;
8824 return p;
8825}
8826
8827/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008828 * Return the autoload script name for a function or variable name.
8829 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008831 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008832autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008833{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008834 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008835 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008836
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008837 /* Get the script file name: replace '#' with '/', append ".vim". */
8838 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8839 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008840 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008841 STRCPY(scriptname, "autoload/");
8842 STRCAT(scriptname, name);
8843 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8844 STRCAT(scriptname, ".vim");
8845 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8846 *p = '/';
8847 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848}
8849
8850/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008851 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 * Return TRUE if a package was loaded.
8853 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008855script_autoload(
8856 char_u *name,
8857 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008858{
8859 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008860 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008861 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008862 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008863
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008864 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008865 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008866 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008867 return FALSE;
8868
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008869 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008870
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008871 /* Find the name in the list of previously loaded package names. Skip
8872 * "autoload/", it's always the same. */
8873 for (i = 0; i < ga_loaded.ga_len; ++i)
8874 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8875 break;
8876 if (!reload && i < ga_loaded.ga_len)
8877 ret = FALSE; /* was loaded already */
8878 else
8879 {
8880 /* Remember the name if it wasn't loaded already. */
8881 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8882 {
8883 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8884 tofree = NULL;
8885 }
8886
8887 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008888 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008889 ret = TRUE;
8890 }
8891
8892 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008893 return ret;
8894}
8895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8897typedef enum
8898{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8900 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8901 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902} var_flavour_T;
8903
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008905var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 char_u *p = varname;
8908
8909 if (ASCII_ISUPPER(*p))
8910 {
8911 while (*(++p))
8912 if (ASCII_ISLOWER(*p))
8913 return VAR_FLAVOUR_SESSION;
8914 return VAR_FLAVOUR_VIMINFO;
8915 }
8916 else
8917 return VAR_FLAVOUR_DEFAULT;
8918}
8919#endif
8920
8921#if defined(FEAT_VIMINFO) || defined(PROTO)
8922/*
8923 * Restore global vars that start with a capital from the viminfo file
8924 */
8925 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008926read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008931 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
8933 if (!writing && (find_viminfo_parameter('!') != NULL))
8934 {
8935 tab = vim_strchr(virp->vir_line + 1, '\t');
8936 if (tab != NULL)
8937 {
8938 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008939 switch (*tab)
8940 {
8941 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008942#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008943 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008945 case 'D': type = VAR_DICT; break;
8946 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008947 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008948 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 tab = vim_strchr(tab, '\t');
8952 if (tab != NULL)
8953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008954 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008955 if (type == VAR_STRING || type == VAR_DICT
8956 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008957 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008959#ifdef FEAT_FLOAT
8960 else if (type == VAR_FLOAT)
8961 (void)string2float(tab + 1, &tv.vval.v_float);
8962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008964 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008965 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008966 {
8967 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8968
8969 if (etv == NULL)
8970 /* Failed to parse back the dict or list, use it as a
8971 * string. */
8972 tv.v_type = VAR_STRING;
8973 else
8974 {
8975 vim_free(tv.vval.v_string);
8976 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008977 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008978 }
8979 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008980 else if (type == VAR_BLOB)
8981 {
8982 blob_T *blob = string2blob(tv.vval.v_string);
8983
8984 if (blob == NULL)
8985 // Failed to parse back the blob, use it as a string.
8986 tv.v_type = VAR_STRING;
8987 else
8988 {
8989 vim_free(tv.vval.v_string);
8990 tv.v_type = VAR_BLOB;
8991 tv.vval.v_blob = blob;
8992 }
8993 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008994
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008995 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008996 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008997 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008998 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008999
9000 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009001 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009002 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9003 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009004 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 }
9006 }
9007 }
9008
9009 return viminfo_readline(virp);
9010}
9011
9012/*
9013 * Write global vars that start with a capital to the viminfo file
9014 */
9015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009016write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 hashitem_T *hi;
9019 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009020 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009021 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009022 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009024 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025
9026 if (find_viminfo_parameter('!') == NULL)
9027 return;
9028
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009029 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009030
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009031 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009034 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009036 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 this_var = HI2DI(hi);
9038 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009041 {
9042 case VAR_STRING: s = "STR"; break;
9043 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009044 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009045 case VAR_DICT: s = "DIC"; break;
9046 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009047 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009048 case VAR_SPECIAL: s = "XPL"; break;
9049
9050 case VAR_UNKNOWN:
9051 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009052 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009053 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009054 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009055 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009057 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009058 if (this_var->di_tv.v_type == VAR_SPECIAL)
9059 {
9060 sprintf((char *)numbuf, "%ld",
9061 (long)this_var->di_tv.vval.v_number);
9062 p = numbuf;
9063 tofree = NULL;
9064 }
9065 else
9066 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009067 if (p != NULL)
9068 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009069 vim_free(tofree);
9070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 }
9072 }
9073}
9074#endif
9075
9076#if defined(FEAT_SESSION) || defined(PROTO)
9077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009078store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 hashitem_T *hi;
9081 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009082 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 char_u *p, *t;
9084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009085 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009088 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009090 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 this_var = HI2DI(hi);
9092 if ((this_var->di_tv.v_type == VAR_NUMBER
9093 || this_var->di_tv.v_type == VAR_STRING)
9094 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009095 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009096 /* Escape special characters with a backslash. Turn a LF and
9097 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009098 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009099 (char_u *)"\\\"\n\r");
9100 if (p == NULL) /* out of memory */
9101 break;
9102 for (t = p; *t != NUL; ++t)
9103 if (*t == '\n')
9104 *t = 'n';
9105 else if (*t == '\r')
9106 *t = 'r';
9107 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 this_var->di_key,
9109 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9110 : ' ',
9111 p,
9112 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9113 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009114 || put_eol(fd) == FAIL)
9115 {
9116 vim_free(p);
9117 return FAIL;
9118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 vim_free(p);
9120 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009121#ifdef FEAT_FLOAT
9122 else if (this_var->di_tv.v_type == VAR_FLOAT
9123 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9124 {
9125 float_T f = this_var->di_tv.vval.v_float;
9126 int sign = ' ';
9127
9128 if (f < 0)
9129 {
9130 f = -f;
9131 sign = '-';
9132 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009133 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009134 this_var->di_key, sign, f) < 0)
9135 || put_eol(fd) == FAIL)
9136 return FAIL;
9137 }
9138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
9140 }
9141 return OK;
9142}
9143#endif
9144
Bram Moolenaar661b1822005-07-28 22:36:45 +00009145/*
9146 * Display script name where an item was last set.
9147 * Should only be invoked when 'verbose' is non-zero.
9148 */
9149 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009150last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009151{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009152 char_u *p;
9153
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009154 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009155 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009156 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009157 if (p != NULL)
9158 {
9159 verbose_enter();
9160 MSG_PUTS(_("\n\tLast set from "));
9161 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009162 if (script_ctx.sc_lnum > 0)
9163 {
9164 MSG_PUTS(_(" line "));
9165 msg_outnum((long)script_ctx.sc_lnum);
9166 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009167 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009168 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009169 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009170 }
9171}
9172
Bram Moolenaar53744302015-07-17 17:38:22 +02009173/* reset v:option_new, v:option_old and v:option_type */
9174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009175reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009176{
9177 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9178 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9179 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9180}
9181
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182/*
9183 * Prepare "gap" for an assert error and add the sourcing position.
9184 */
9185 void
9186prepare_assert_error(garray_T *gap)
9187{
9188 char buf[NUMBUFLEN];
9189
9190 ga_init2(gap, 1, 100);
9191 if (sourcing_name != NULL)
9192 {
9193 ga_concat(gap, sourcing_name);
9194 if (sourcing_lnum > 0)
9195 ga_concat(gap, (char_u *)" ");
9196 }
9197 if (sourcing_lnum > 0)
9198 {
9199 sprintf(buf, "line %ld", (long)sourcing_lnum);
9200 ga_concat(gap, (char_u *)buf);
9201 }
9202 if (sourcing_name != NULL || sourcing_lnum > 0)
9203 ga_concat(gap, (char_u *)": ");
9204}
9205
9206/*
9207 * Add an assert error to v:errors.
9208 */
9209 void
9210assert_error(garray_T *gap)
9211{
9212 struct vimvar *vp = &vimvars[VV_ERRORS];
9213
9214 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9215 /* Make sure v:errors is a list. */
9216 set_vim_var_list(VV_ERRORS, list_alloc());
9217 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9218}
9219
Bram Moolenaar65a54642018-04-28 16:56:53 +02009220 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221assert_equal_common(typval_T *argvars, assert_type_T atype)
9222{
9223 garray_T ga;
9224
9225 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9226 != (atype == ASSERT_EQUAL))
9227 {
9228 prepare_assert_error(&ga);
9229 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9230 atype);
9231 assert_error(&ga);
9232 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009233 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009234 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009235 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236}
9237
Bram Moolenaar65a54642018-04-28 16:56:53 +02009238 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009239assert_equalfile(typval_T *argvars)
9240{
9241 char_u buf1[NUMBUFLEN];
9242 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009243 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9244 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009245 garray_T ga;
9246 FILE *fd1;
9247 FILE *fd2;
9248
9249 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009250 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009251
9252 IObuff[0] = NUL;
9253 fd1 = mch_fopen((char *)fname1, READBIN);
9254 if (fd1 == NULL)
9255 {
9256 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9257 }
9258 else
9259 {
9260 fd2 = mch_fopen((char *)fname2, READBIN);
9261 if (fd2 == NULL)
9262 {
9263 fclose(fd1);
9264 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9265 }
9266 else
9267 {
9268 int c1, c2;
9269 long count = 0;
9270
9271 for (;;)
9272 {
9273 c1 = fgetc(fd1);
9274 c2 = fgetc(fd2);
9275 if (c1 == EOF)
9276 {
9277 if (c2 != EOF)
9278 STRCPY(IObuff, "first file is shorter");
9279 break;
9280 }
9281 else if (c2 == EOF)
9282 {
9283 STRCPY(IObuff, "second file is shorter");
9284 break;
9285 }
9286 else if (c1 != c2)
9287 {
9288 vim_snprintf((char *)IObuff, IOSIZE,
9289 "difference at byte %ld", count);
9290 break;
9291 }
9292 ++count;
9293 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009294 fclose(fd1);
9295 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009296 }
9297 }
9298 if (IObuff[0] != NUL)
9299 {
9300 prepare_assert_error(&ga);
9301 ga_concat(&ga, IObuff);
9302 assert_error(&ga);
9303 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009304 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009305 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009306 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009307}
9308
Bram Moolenaar65a54642018-04-28 16:56:53 +02009309 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310assert_match_common(typval_T *argvars, assert_type_T atype)
9311{
9312 garray_T ga;
9313 char_u buf1[NUMBUFLEN];
9314 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009315 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9316 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317
9318 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009319 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009320 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9321 {
9322 prepare_assert_error(&ga);
9323 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9324 atype);
9325 assert_error(&ga);
9326 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009327 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009329 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330}
9331
Bram Moolenaar65a54642018-04-28 16:56:53 +02009332 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009333assert_inrange(typval_T *argvars)
9334{
9335 garray_T ga;
9336 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009337 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9338 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9339 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009340 char_u *tofree;
9341 char msg[200];
9342 char_u numbuf[NUMBUFLEN];
9343
9344 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009345 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009346 if (actual < lower || actual > upper)
9347 {
9348 prepare_assert_error(&ga);
9349 if (argvars[3].v_type != VAR_UNKNOWN)
9350 {
9351 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9352 vim_free(tofree);
9353 }
9354 else
9355 {
9356 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9357 (long)lower, (long)upper, (long)actual);
9358 ga_concat(&ga, (char_u *)msg);
9359 }
9360 assert_error(&ga);
9361 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009362 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009363 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009364 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009365}
9366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367/*
9368 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009369 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009370 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009371 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009372assert_bool(typval_T *argvars, int isTrue)
9373{
9374 int error = FALSE;
9375 garray_T ga;
9376
9377 if (argvars[0].v_type == VAR_SPECIAL
9378 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009379 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009381 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382 || error)
9383 {
9384 prepare_assert_error(&ga);
9385 fill_assert_error(&ga, &argvars[1],
9386 (char_u *)(isTrue ? "True" : "False"),
9387 NULL, &argvars[0], ASSERT_OTHER);
9388 assert_error(&ga);
9389 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009390 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009392 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393}
9394
Bram Moolenaar65a54642018-04-28 16:56:53 +02009395 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009396assert_report(typval_T *argvars)
9397{
9398 garray_T ga;
9399
9400 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009401 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009402 assert_error(&ga);
9403 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009404 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009405}
9406
Bram Moolenaar65a54642018-04-28 16:56:53 +02009407 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408assert_exception(typval_T *argvars)
9409{
9410 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009411 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412
9413 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9414 {
9415 prepare_assert_error(&ga);
9416 ga_concat(&ga, (char_u *)"v:exception is not set");
9417 assert_error(&ga);
9418 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009419 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420 }
9421 else if (error != NULL
9422 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9423 {
9424 prepare_assert_error(&ga);
9425 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9426 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9427 assert_error(&ga);
9428 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009429 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009430 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009431 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432}
9433
Bram Moolenaar65a54642018-04-28 16:56:53 +02009434 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009435assert_beeps(typval_T *argvars)
9436{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009437 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009438 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009439 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009440
9441 called_vim_beep = FALSE;
9442 suppress_errthrow = TRUE;
9443 emsg_silent = FALSE;
9444 do_cmdline_cmd(cmd);
9445 if (!called_vim_beep)
9446 {
9447 prepare_assert_error(&ga);
9448 ga_concat(&ga, (char_u *)"command did not beep: ");
9449 ga_concat(&ga, cmd);
9450 assert_error(&ga);
9451 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009452 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009453 }
9454
9455 suppress_errthrow = FALSE;
9456 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009457 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009458}
9459
Bram Moolenaar65a54642018-04-28 16:56:53 +02009460 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461assert_fails(typval_T *argvars)
9462{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009463 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009465 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009466 char_u numbuf[NUMBUFLEN];
9467 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468
9469 called_emsg = FALSE;
9470 suppress_errthrow = TRUE;
9471 emsg_silent = TRUE;
9472 do_cmdline_cmd(cmd);
9473 if (!called_emsg)
9474 {
9475 prepare_assert_error(&ga);
9476 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009477 if (argvars[1].v_type != VAR_UNKNOWN
9478 && argvars[2].v_type != VAR_UNKNOWN)
9479 {
9480 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9481 vim_free(tofree);
9482 }
9483 else
9484 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485 assert_error(&ga);
9486 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009487 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488 }
9489 else if (argvars[1].v_type != VAR_UNKNOWN)
9490 {
9491 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009492 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493
9494 if (error == NULL
9495 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9496 {
9497 prepare_assert_error(&ga);
9498 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9499 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9500 assert_error(&ga);
9501 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009502 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503 }
9504 }
9505
9506 called_emsg = FALSE;
9507 suppress_errthrow = FALSE;
9508 emsg_silent = FALSE;
9509 emsg_on_display = FALSE;
9510 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009511 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009512}
9513
9514/*
9515 * Append "str" to "gap", escaping unprintable characters.
9516 * Changes NL to \n, CR to \r, etc.
9517 */
9518 static void
9519ga_concat_esc(garray_T *gap, char_u *str)
9520{
9521 char_u *p;
9522 char_u buf[NUMBUFLEN];
9523
9524 if (str == NULL)
9525 {
9526 ga_concat(gap, (char_u *)"NULL");
9527 return;
9528 }
9529
9530 for (p = str; *p != NUL; ++p)
9531 switch (*p)
9532 {
9533 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9534 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9535 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9536 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9537 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9538 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9539 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9540 default:
9541 if (*p < ' ')
9542 {
9543 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9544 ga_concat(gap, buf);
9545 }
9546 else
9547 ga_append(gap, *p);
9548 break;
9549 }
9550}
9551
9552/*
9553 * Fill "gap" with information about an assert error.
9554 */
9555 void
9556fill_assert_error(
9557 garray_T *gap,
9558 typval_T *opt_msg_tv,
9559 char_u *exp_str,
9560 typval_T *exp_tv,
9561 typval_T *got_tv,
9562 assert_type_T atype)
9563{
9564 char_u numbuf[NUMBUFLEN];
9565 char_u *tofree;
9566
9567 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9568 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009569 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9570 vim_free(tofree);
9571 ga_concat(gap, (char_u *)": ");
9572 }
9573
9574 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9575 ga_concat(gap, (char_u *)"Pattern ");
9576 else if (atype == ASSERT_NOTEQUAL)
9577 ga_concat(gap, (char_u *)"Expected not equal to ");
9578 else
9579 ga_concat(gap, (char_u *)"Expected ");
9580 if (exp_str == NULL)
9581 {
9582 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009583 vim_free(tofree);
9584 }
9585 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009586 ga_concat_esc(gap, exp_str);
9587 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009589 if (atype == ASSERT_MATCH)
9590 ga_concat(gap, (char_u *)" does not match ");
9591 else if (atype == ASSERT_NOTMATCH)
9592 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009594 ga_concat(gap, (char_u *)" but got ");
9595 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9596 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 }
9598}
9599
Bram Moolenaar31988702018-02-13 12:57:42 +01009600/*
9601 * Compare "typ1" and "typ2". Put the result in "typ1".
9602 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009603 int
9604typval_compare(
9605 typval_T *typ1, /* first operand */
9606 typval_T *typ2, /* second operand */
9607 exptype_T type, /* operator */
9608 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009609 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009610{
9611 int i;
9612 varnumber_T n1, n2;
9613 char_u *s1, *s2;
9614 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9615
Bram Moolenaar31988702018-02-13 12:57:42 +01009616 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009617 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009618 /* For "is" a different type always means FALSE, for "notis"
9619 * it means TRUE. */
9620 n1 = (type == TYPE_NEQUAL);
9621 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009622 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9623 {
9624 if (type_is)
9625 {
9626 n1 = (typ1->v_type == typ2->v_type
9627 && typ1->vval.v_blob == typ2->vval.v_blob);
9628 if (type == TYPE_NEQUAL)
9629 n1 = !n1;
9630 }
9631 else if (typ1->v_type != typ2->v_type
9632 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9633 {
9634 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009635 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009636 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009637 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009638 clear_tv(typ1);
9639 return FAIL;
9640 }
9641 else
9642 {
9643 // Compare two Blobs for being equal or unequal.
9644 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9645 if (type == TYPE_NEQUAL)
9646 n1 = !n1;
9647 }
9648 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009649 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9650 {
9651 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009652 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009653 n1 = (typ1->v_type == typ2->v_type
9654 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009655 if (type == TYPE_NEQUAL)
9656 n1 = !n1;
9657 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009658 else if (typ1->v_type != typ2->v_type
9659 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009660 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009661 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009662 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009663 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009664 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009665 clear_tv(typ1);
9666 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009667 }
9668 else
9669 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009670 /* Compare two Lists for being equal or unequal. */
9671 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9672 ic, FALSE);
9673 if (type == TYPE_NEQUAL)
9674 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009675 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009676 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009677
9678 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9679 {
9680 if (type_is)
9681 {
9682 n1 = (typ1->v_type == typ2->v_type
9683 && typ1->vval.v_dict == typ2->vval.v_dict);
9684 if (type == TYPE_NEQUAL)
9685 n1 = !n1;
9686 }
9687 else if (typ1->v_type != typ2->v_type
9688 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9689 {
9690 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009691 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009692 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009693 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009694 clear_tv(typ1);
9695 return FAIL;
9696 }
9697 else
9698 {
9699 /* Compare two Dictionaries for being equal or unequal. */
9700 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9701 ic, FALSE);
9702 if (type == TYPE_NEQUAL)
9703 n1 = !n1;
9704 }
9705 }
9706
9707 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9708 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9709 {
9710 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009712 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009713 clear_tv(typ1);
9714 return FAIL;
9715 }
9716 if ((typ1->v_type == VAR_PARTIAL
9717 && typ1->vval.v_partial == NULL)
9718 || (typ2->v_type == VAR_PARTIAL
9719 && typ2->vval.v_partial == NULL))
9720 /* when a partial is NULL assume not equal */
9721 n1 = FALSE;
9722 else if (type_is)
9723 {
9724 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9725 /* strings are considered the same if their value is
9726 * the same */
9727 n1 = tv_equal(typ1, typ2, ic, FALSE);
9728 else if (typ1->v_type == VAR_PARTIAL
9729 && typ2->v_type == VAR_PARTIAL)
9730 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9731 else
9732 n1 = FALSE;
9733 }
9734 else
9735 n1 = tv_equal(typ1, typ2, ic, FALSE);
9736 if (type == TYPE_NEQUAL)
9737 n1 = !n1;
9738 }
9739
9740#ifdef FEAT_FLOAT
9741 /*
9742 * If one of the two variables is a float, compare as a float.
9743 * When using "=~" or "!~", always compare as string.
9744 */
9745 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9746 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9747 {
9748 float_T f1, f2;
9749
9750 if (typ1->v_type == VAR_FLOAT)
9751 f1 = typ1->vval.v_float;
9752 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009753 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009754 if (typ2->v_type == VAR_FLOAT)
9755 f2 = typ2->vval.v_float;
9756 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009757 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009758 n1 = FALSE;
9759 switch (type)
9760 {
9761 case TYPE_EQUAL: n1 = (f1 == f2); break;
9762 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9763 case TYPE_GREATER: n1 = (f1 > f2); break;
9764 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9765 case TYPE_SMALLER: n1 = (f1 < f2); break;
9766 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9767 case TYPE_UNKNOWN:
9768 case TYPE_MATCH:
9769 case TYPE_NOMATCH: break; /* avoid gcc warning */
9770 }
9771 }
9772#endif
9773
9774 /*
9775 * If one of the two variables is a number, compare as a number.
9776 * When using "=~" or "!~", always compare as string.
9777 */
9778 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9779 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009781 n1 = tv_get_number(typ1);
9782 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009783 switch (type)
9784 {
9785 case TYPE_EQUAL: n1 = (n1 == n2); break;
9786 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9787 case TYPE_GREATER: n1 = (n1 > n2); break;
9788 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9789 case TYPE_SMALLER: n1 = (n1 < n2); break;
9790 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9791 case TYPE_UNKNOWN:
9792 case TYPE_MATCH:
9793 case TYPE_NOMATCH: break; /* avoid gcc warning */
9794 }
9795 }
9796 else
9797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009798 s1 = tv_get_string_buf(typ1, buf1);
9799 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009800 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9801 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9802 else
9803 i = 0;
9804 n1 = FALSE;
9805 switch (type)
9806 {
9807 case TYPE_EQUAL: n1 = (i == 0); break;
9808 case TYPE_NEQUAL: n1 = (i != 0); break;
9809 case TYPE_GREATER: n1 = (i > 0); break;
9810 case TYPE_GEQUAL: n1 = (i >= 0); break;
9811 case TYPE_SMALLER: n1 = (i < 0); break;
9812 case TYPE_SEQUAL: n1 = (i <= 0); break;
9813
9814 case TYPE_MATCH:
9815 case TYPE_NOMATCH:
9816 n1 = pattern_match(s2, s1, ic);
9817 if (type == TYPE_NOMATCH)
9818 n1 = !n1;
9819 break;
9820
9821 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9822 }
9823 }
9824 clear_tv(typ1);
9825 typ1->v_type = VAR_NUMBER;
9826 typ1->vval.v_number = n1;
9827
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009828 return OK;
9829}
9830
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009831 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009832typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009833{
9834 char_u *tofree;
9835 char_u numbuf[NUMBUFLEN];
9836 char_u *ret = NULL;
9837
9838 if (arg == NULL)
9839 return vim_strsave((char_u *)"(does not exist)");
9840 ret = tv2string(arg, &tofree, numbuf, 0);
9841 /* Make a copy if we have a value but it's not in allocated memory. */
9842 if (ret != NULL && tofree == NULL)
9843 ret = vim_strsave(ret);
9844 return ret;
9845}
9846
9847 int
9848var_exists(char_u *var)
9849{
9850 char_u *name;
9851 char_u *tofree;
9852 typval_T tv;
9853 int len = 0;
9854 int n = FALSE;
9855
9856 /* get_name_len() takes care of expanding curly braces */
9857 name = var;
9858 len = get_name_len(&var, &tofree, TRUE, FALSE);
9859 if (len > 0)
9860 {
9861 if (tofree != NULL)
9862 name = tofree;
9863 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9864 if (n)
9865 {
9866 /* handle d.key, l[idx], f(expr) */
9867 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9868 if (n)
9869 clear_tv(&tv);
9870 }
9871 }
9872 if (*var != NUL)
9873 n = FALSE;
9874
9875 vim_free(tofree);
9876 return n;
9877}
9878
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879#endif /* FEAT_EVAL */
9880
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009882#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
9884#ifdef WIN3264
9885/*
9886 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
9889/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009890 * Get the short path (8.3) for the filename in "fnamep".
9891 * Only works for a valid file name.
9892 * When the path gets longer "fnamep" is changed and the allocated buffer
9893 * is put in "bufp".
9894 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9895 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 */
9897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009898get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009900 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 char_u *newbuf;
9902
9903 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009904 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (l > len - 1)
9906 {
9907 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009908 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 newbuf = vim_strnsave(*fnamep, l);
9910 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
9913 vim_free(*bufp);
9914 *fnamep = *bufp = newbuf;
9915
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009916 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009917 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 }
9919
9920 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009921 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922}
9923
9924/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009925 * Get the short path (8.3) for the filename in "fname". The converted
9926 * path is returned in "bufp".
9927 *
9928 * Some of the directories specified in "fname" may not exist. This function
9929 * will shorten the existing directories at the beginning of the path and then
9930 * append the remaining non-existing path.
9931 *
9932 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009933 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009934 * bufp - Pointer to an allocated buffer for the filename.
9935 * fnamelen - Length of the filename pointed to by fname
9936 *
9937 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 */
9939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009940shortpath_for_invalid_fname(
9941 char_u **fname,
9942 char_u **bufp,
9943 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009945 char_u *short_fname, *save_fname, *pbuf_unused;
9946 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009948 int old_len, len;
9949 int new_len, sfx_len;
9950 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951
9952 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009953 old_len = *fnamelen;
9954 save_fname = vim_strnsave(*fname, old_len);
9955 pbuf_unused = NULL;
9956 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009958 endp = save_fname + old_len - 1; /* Find the end of the copy */
9959 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009961 /*
9962 * Try shortening the supplied path till it succeeds by removing one
9963 * directory at a time from the tail of the path.
9964 */
9965 len = 0;
9966 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009968 /* go back one path-separator */
9969 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9970 --endp;
9971 if (endp <= save_fname)
9972 break; /* processed the complete path */
9973
9974 /*
9975 * Replace the path separator with a NUL and try to shorten the
9976 * resulting path.
9977 */
9978 ch = *endp;
9979 *endp = 0;
9980 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009981 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009982 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9983 {
9984 retval = FAIL;
9985 goto theend;
9986 }
9987 *endp = ch; /* preserve the string */
9988
9989 if (len > 0)
9990 break; /* successfully shortened the path */
9991
9992 /* failed to shorten the path. Skip the path separator */
9993 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 }
9995
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009996 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009998 /*
9999 * Succeeded in shortening the path. Now concatenate the shortened
10000 * path with the remaining path at the tail.
10001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010003 /* Compute the length of the new path. */
10004 sfx_len = (int)(save_endp - endp) + 1;
10005 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010007 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010009 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010011 /* There is not enough space in the currently allocated string,
10012 * copy it to a buffer big enough. */
10013 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010015 {
10016 retval = FAIL;
10017 goto theend;
10018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020 else
10021 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010022 /* Transfer short_fname to the main buffer (it's big enough),
10023 * unless get_short_pathname() did its work in-place. */
10024 *fname = *bufp = save_fname;
10025 if (short_fname != save_fname)
10026 vim_strncpy(save_fname, short_fname, len);
10027 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010029
10030 /* concat the not-shortened part of the path */
10031 vim_strncpy(*fname + len, endp, sfx_len);
10032 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010034
10035theend:
10036 vim_free(pbuf_unused);
10037 vim_free(save_fname);
10038
10039 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
10041
10042/*
10043 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010044 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 */
10046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010047shortpath_for_partial(
10048 char_u **fnamep,
10049 char_u **bufp,
10050 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
10052 int sepcount, len, tflen;
10053 char_u *p;
10054 char_u *pbuf, *tfname;
10055 int hasTilde;
10056
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010057 /* Count up the path separators from the RHS.. so we know which part
10058 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010060 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 if (vim_ispathsep(*p))
10062 ++sepcount;
10063
10064 /* Need full path first (use expand_env() to remove a "~/") */
10065 hasTilde = (**fnamep == '~');
10066 if (hasTilde)
10067 pbuf = tfname = expand_env_save(*fnamep);
10068 else
10069 pbuf = tfname = FullName_save(*fnamep, FALSE);
10070
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010071 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010073 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10074 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
10076 if (len == 0)
10077 {
10078 /* Don't have a valid filename, so shorten the rest of the
10079 * path if we can. This CAN give us invalid 8.3 filenames, but
10080 * there's not a lot of point in guessing what it might be.
10081 */
10082 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010083 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 }
10086
10087 /* Count the paths backward to find the beginning of the desired string. */
10088 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010089 {
10090#ifdef FEAT_MBYTE
10091 if (has_mbyte)
10092 p -= mb_head_off(tfname, p);
10093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 if (vim_ispathsep(*p))
10095 {
10096 if (sepcount == 0 || (hasTilde && sepcount == 1))
10097 break;
10098 else
10099 sepcount --;
10100 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 if (hasTilde)
10103 {
10104 --p;
10105 if (p >= tfname)
10106 *p = '~';
10107 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010108 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110 else
10111 ++p;
10112
10113 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10114 vim_free(*bufp);
10115 *fnamelen = (int)STRLEN(p);
10116 *bufp = pbuf;
10117 *fnamep = p;
10118
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010119 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121#endif /* WIN3264 */
10122
10123/*
10124 * Adjust a filename, according to a string of modifiers.
10125 * *fnamep must be NUL terminated when called. When returning, the length is
10126 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010127 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 * When there is an error, *fnamep is set to NULL.
10129 */
10130 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010131modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010132 char_u *src, // string with modifiers
10133 int tilde_file, // "~" is a file name, not $HOME
10134 int *usedlen, // characters after src that are used
10135 char_u **fnamep, // file name so far
10136 char_u **bufp, // buffer for allocated file name or NULL
10137 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138{
10139 int valid = 0;
10140 char_u *tail;
10141 char_u *s, *p, *pbuf;
10142 char_u dirname[MAXPATHL];
10143 int c;
10144 int has_fullname = 0;
10145#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010146 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 int has_shortname = 0;
10148#endif
10149
10150repeat:
10151 /* ":p" - full path/file_name */
10152 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10153 {
10154 has_fullname = 1;
10155
10156 valid |= VALID_PATH;
10157 *usedlen += 2;
10158
10159 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10160 if ((*fnamep)[0] == '~'
10161#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10162 && ((*fnamep)[1] == '/'
10163# ifdef BACKSLASH_IN_FILENAME
10164 || (*fnamep)[1] == '\\'
10165# endif
10166 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010168 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 )
10170 {
10171 *fnamep = expand_env_save(*fnamep);
10172 vim_free(*bufp); /* free any allocated file name */
10173 *bufp = *fnamep;
10174 if (*fnamep == NULL)
10175 return -1;
10176 }
10177
10178 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010179 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 {
10181 if (vim_ispathsep(*p)
10182 && p[1] == '.'
10183 && (p[2] == NUL
10184 || vim_ispathsep(p[2])
10185 || (p[2] == '.'
10186 && (p[3] == NUL || vim_ispathsep(p[3])))))
10187 break;
10188 }
10189
10190 /* FullName_save() is slow, don't use it when not needed. */
10191 if (*p != NUL || !vim_isAbsName(*fnamep))
10192 {
10193 *fnamep = FullName_save(*fnamep, *p != NUL);
10194 vim_free(*bufp); /* free any allocated file name */
10195 *bufp = *fnamep;
10196 if (*fnamep == NULL)
10197 return -1;
10198 }
10199
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010200#ifdef WIN3264
10201# if _WIN32_WINNT >= 0x0500
10202 if (vim_strchr(*fnamep, '~') != NULL)
10203 {
10204 /* Expand 8.3 filename to full path. Needed to make sure the same
10205 * file does not have two different names.
10206 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10207 p = alloc(_MAX_PATH + 1);
10208 if (p != NULL)
10209 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010210 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010211 {
10212 vim_free(*bufp);
10213 *bufp = *fnamep = p;
10214 }
10215 else
10216 vim_free(p);
10217 }
10218 }
10219# endif
10220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 /* Append a path separator to a directory. */
10222 if (mch_isdir(*fnamep))
10223 {
10224 /* Make room for one or two extra characters. */
10225 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10226 vim_free(*bufp); /* free any allocated file name */
10227 *bufp = *fnamep;
10228 if (*fnamep == NULL)
10229 return -1;
10230 add_pathsep(*fnamep);
10231 }
10232 }
10233
10234 /* ":." - path relative to the current directory */
10235 /* ":~" - path relative to the home directory */
10236 /* ":8" - shortname path - postponed till after */
10237 while (src[*usedlen] == ':'
10238 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10239 {
10240 *usedlen += 2;
10241 if (c == '8')
10242 {
10243#ifdef WIN3264
10244 has_shortname = 1; /* Postpone this. */
10245#endif
10246 continue;
10247 }
10248 pbuf = NULL;
10249 /* Need full path first (use expand_env() to remove a "~/") */
10250 if (!has_fullname)
10251 {
10252 if (c == '.' && **fnamep == '~')
10253 p = pbuf = expand_env_save(*fnamep);
10254 else
10255 p = pbuf = FullName_save(*fnamep, FALSE);
10256 }
10257 else
10258 p = *fnamep;
10259
10260 has_fullname = 0;
10261
10262 if (p != NULL)
10263 {
10264 if (c == '.')
10265 {
10266 mch_dirname(dirname, MAXPATHL);
10267 s = shorten_fname(p, dirname);
10268 if (s != NULL)
10269 {
10270 *fnamep = s;
10271 if (pbuf != NULL)
10272 {
10273 vim_free(*bufp); /* free any allocated file name */
10274 *bufp = pbuf;
10275 pbuf = NULL;
10276 }
10277 }
10278 }
10279 else
10280 {
10281 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10282 /* Only replace it when it starts with '~' */
10283 if (*dirname == '~')
10284 {
10285 s = vim_strsave(dirname);
10286 if (s != NULL)
10287 {
10288 *fnamep = s;
10289 vim_free(*bufp);
10290 *bufp = s;
10291 }
10292 }
10293 }
10294 vim_free(pbuf);
10295 }
10296 }
10297
10298 tail = gettail(*fnamep);
10299 *fnamelen = (int)STRLEN(*fnamep);
10300
10301 /* ":h" - head, remove "/file_name", can be repeated */
10302 /* Don't remove the first "/" or "c:\" */
10303 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10304 {
10305 valid |= VALID_HEAD;
10306 *usedlen += 2;
10307 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010308 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010309 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 *fnamelen = (int)(tail - *fnamep);
10311#ifdef VMS
10312 if (*fnamelen > 0)
10313 *fnamelen += 1; /* the path separator is part of the path */
10314#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010315 if (*fnamelen == 0)
10316 {
10317 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10318 p = vim_strsave((char_u *)".");
10319 if (p == NULL)
10320 return -1;
10321 vim_free(*bufp);
10322 *bufp = *fnamep = tail = p;
10323 *fnamelen = 1;
10324 }
10325 else
10326 {
10327 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010328 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 }
10331
10332 /* ":8" - shortname */
10333 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10334 {
10335 *usedlen += 2;
10336#ifdef WIN3264
10337 has_shortname = 1;
10338#endif
10339 }
10340
10341#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010342 /*
10343 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 */
10345 if (has_shortname)
10346 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010347 /* Copy the string if it is shortened by :h and when it wasn't copied
10348 * yet, because we are going to change it in place. Avoids changing
10349 * the buffer name for "%:8". */
10350 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 {
10352 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010353 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 return -1;
10355 vim_free(*bufp);
10356 *bufp = *fnamep = p;
10357 }
10358
10359 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010360 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 if (!has_fullname && !vim_isAbsName(*fnamep))
10362 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010363 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 return -1;
10365 }
10366 else
10367 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010368 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369
Bram Moolenaardc935552011-08-17 15:23:23 +020010370 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010372 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 return -1;
10374
10375 if (l == 0)
10376 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010377 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010379 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 return -1;
10381 }
10382 *fnamelen = l;
10383 }
10384 }
10385#endif /* WIN3264 */
10386
10387 /* ":t" - tail, just the basename */
10388 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10389 {
10390 *usedlen += 2;
10391 *fnamelen -= (int)(tail - *fnamep);
10392 *fnamep = tail;
10393 }
10394
10395 /* ":e" - extension, can be repeated */
10396 /* ":r" - root, without extension, can be repeated */
10397 while (src[*usedlen] == ':'
10398 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10399 {
10400 /* find a '.' in the tail:
10401 * - for second :e: before the current fname
10402 * - otherwise: The last '.'
10403 */
10404 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10405 s = *fnamep - 2;
10406 else
10407 s = *fnamep + *fnamelen - 1;
10408 for ( ; s > tail; --s)
10409 if (s[0] == '.')
10410 break;
10411 if (src[*usedlen + 1] == 'e') /* :e */
10412 {
10413 if (s > tail)
10414 {
10415 *fnamelen += (int)(*fnamep - (s + 1));
10416 *fnamep = s + 1;
10417#ifdef VMS
10418 /* cut version from the extension */
10419 s = *fnamep + *fnamelen - 1;
10420 for ( ; s > *fnamep; --s)
10421 if (s[0] == ';')
10422 break;
10423 if (s > *fnamep)
10424 *fnamelen = s - *fnamep;
10425#endif
10426 }
10427 else if (*fnamep <= tail)
10428 *fnamelen = 0;
10429 }
10430 else /* :r */
10431 {
10432 if (s > tail) /* remove one extension */
10433 *fnamelen = (int)(s - *fnamep);
10434 }
10435 *usedlen += 2;
10436 }
10437
10438 /* ":s?pat?foo?" - substitute */
10439 /* ":gs?pat?foo?" - global substitute */
10440 if (src[*usedlen] == ':'
10441 && (src[*usedlen + 1] == 's'
10442 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10443 {
10444 char_u *str;
10445 char_u *pat;
10446 char_u *sub;
10447 int sep;
10448 char_u *flags;
10449 int didit = FALSE;
10450
10451 flags = (char_u *)"";
10452 s = src + *usedlen + 2;
10453 if (src[*usedlen + 1] == 'g')
10454 {
10455 flags = (char_u *)"g";
10456 ++s;
10457 }
10458
10459 sep = *s++;
10460 if (sep)
10461 {
10462 /* find end of pattern */
10463 p = vim_strchr(s, sep);
10464 if (p != NULL)
10465 {
10466 pat = vim_strnsave(s, (int)(p - s));
10467 if (pat != NULL)
10468 {
10469 s = p + 1;
10470 /* find end of substitution */
10471 p = vim_strchr(s, sep);
10472 if (p != NULL)
10473 {
10474 sub = vim_strnsave(s, (int)(p - s));
10475 str = vim_strnsave(*fnamep, *fnamelen);
10476 if (sub != NULL && str != NULL)
10477 {
10478 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010479 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 if (s != NULL)
10481 {
10482 *fnamep = s;
10483 *fnamelen = (int)STRLEN(s);
10484 vim_free(*bufp);
10485 *bufp = s;
10486 didit = TRUE;
10487 }
10488 }
10489 vim_free(sub);
10490 vim_free(str);
10491 }
10492 vim_free(pat);
10493 }
10494 }
10495 /* after using ":s", repeat all the modifiers */
10496 if (didit)
10497 goto repeat;
10498 }
10499 }
10500
Bram Moolenaar26df0922014-02-23 23:39:13 +010010501 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10502 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010503 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010504 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010505 if (c != NUL)
10506 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010507 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010508 if (c != NUL)
10509 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010510 if (p == NULL)
10511 return -1;
10512 vim_free(*bufp);
10513 *bufp = *fnamep = p;
10514 *fnamelen = (int)STRLEN(p);
10515 *usedlen += 2;
10516 }
10517
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 return valid;
10519}
10520
10521/*
10522 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010523 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 * "flags" can be "g" to do a global substitute.
10525 * Returns an allocated string, NULL for error.
10526 */
10527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010528do_string_sub(
10529 char_u *str,
10530 char_u *pat,
10531 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010532 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010533 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 int sublen;
10536 regmatch_T regmatch;
10537 int i;
10538 int do_all;
10539 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010540 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 garray_T ga;
10542 char_u *ret;
10543 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010544 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545
10546 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10547 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010548 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549
10550 ga_init2(&ga, 1, 200);
10551
10552 do_all = (flags[0] == 'g');
10553
10554 regmatch.rm_ic = p_ic;
10555 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10556 if (regmatch.regprog != NULL)
10557 {
10558 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010559 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10561 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010562 /* Skip empty match except for first match. */
10563 if (regmatch.startp[0] == regmatch.endp[0])
10564 {
10565 if (zero_width == regmatch.startp[0])
10566 {
10567 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010568 i = MB_PTR2LEN(tail);
10569 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10570 (size_t)i);
10571 ga.ga_len += i;
10572 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010573 continue;
10574 }
10575 zero_width = regmatch.startp[0];
10576 }
10577
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 /*
10579 * Get some space for a temporary buffer to do the substitution
10580 * into. It will contain:
10581 * - The text up to where the match is.
10582 * - The substituted text.
10583 * - The text after the match.
10584 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010585 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010586 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10588 {
10589 ga_clear(&ga);
10590 break;
10591 }
10592
10593 /* copy the text up to where the match is */
10594 i = (int)(regmatch.startp[0] - tail);
10595 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10596 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010597 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 + ga.ga_len + i, TRUE, TRUE, FALSE);
10599 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010600 tail = regmatch.endp[0];
10601 if (*tail == NUL)
10602 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 if (!do_all)
10604 break;
10605 }
10606
10607 if (ga.ga_data != NULL)
10608 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10609
Bram Moolenaar473de612013-06-08 18:19:48 +020010610 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 }
10612
10613 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10614 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010615 if (p_cpo == empty_option)
10616 p_cpo = save_cpo;
10617 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010618 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010619 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620
10621 return ret;
10622}
10623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 static int
10625filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10626{
10627 typval_T rettv;
10628 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010630
10631 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10632 argv[0] = vimvars[VV_KEY].vv_tv;
10633 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010634 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10635 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010636 if (map)
10637 {
10638 /* map(): replace the list item value */
10639 clear_tv(tv);
10640 rettv.v_lock = 0;
10641 *tv = rettv;
10642 }
10643 else
10644 {
10645 int error = FALSE;
10646
10647 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010648 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 clear_tv(&rettv);
10650 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010651 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 if (error)
10653 goto theend;
10654 }
10655 retval = OK;
10656theend:
10657 clear_tv(&vimvars[VV_VAL].vv_tv);
10658 return retval;
10659}
10660
10661
10662/*
10663 * Implementation of map() and filter().
10664 */
10665 void
10666filter_map(typval_T *argvars, typval_T *rettv, int map)
10667{
10668 typval_T *expr;
10669 listitem_T *li, *nli;
10670 list_T *l = NULL;
10671 dictitem_T *di;
10672 hashtab_T *ht;
10673 hashitem_T *hi;
10674 dict_T *d = NULL;
10675 typval_T save_val;
10676 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010677 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 int rem;
10679 int todo;
10680 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10681 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10682 : N_("filter() argument"));
10683 int save_did_emsg;
10684 int idx = 0;
10685
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010686 if (argvars[0].v_type == VAR_BLOB)
10687 {
10688 if ((b = argvars[0].vval.v_blob) == NULL)
10689 return;
10690 }
10691 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010692 {
10693 if ((l = argvars[0].vval.v_list) == NULL
10694 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10695 return;
10696 }
10697 else if (argvars[0].v_type == VAR_DICT)
10698 {
10699 if ((d = argvars[0].vval.v_dict) == NULL
10700 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10701 return;
10702 }
10703 else
10704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010705 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706 return;
10707 }
10708
10709 expr = &argvars[1];
10710 /* On type errors, the preceding call has already displayed an error
10711 * message. Avoid a misleading error message for an empty string that
10712 * was not passed as argument. */
10713 if (expr->v_type != VAR_UNKNOWN)
10714 {
10715 prepare_vimvar(VV_VAL, &save_val);
10716
10717 /* We reset "did_emsg" to be able to detect whether an error
10718 * occurred during evaluation of the expression. */
10719 save_did_emsg = did_emsg;
10720 did_emsg = FALSE;
10721
10722 prepare_vimvar(VV_KEY, &save_key);
10723 if (argvars[0].v_type == VAR_DICT)
10724 {
10725 vimvars[VV_KEY].vv_type = VAR_STRING;
10726
10727 ht = &d->dv_hashtab;
10728 hash_lock(ht);
10729 todo = (int)ht->ht_used;
10730 for (hi = ht->ht_array; todo > 0; ++hi)
10731 {
10732 if (!HASHITEM_EMPTY(hi))
10733 {
10734 int r;
10735
10736 --todo;
10737 di = HI2DI(hi);
10738 if (map &&
10739 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10740 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10741 break;
10742 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10743 r = filter_map_one(&di->di_tv, expr, map, &rem);
10744 clear_tv(&vimvars[VV_KEY].vv_tv);
10745 if (r == FAIL || did_emsg)
10746 break;
10747 if (!map && rem)
10748 {
10749 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10750 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10751 break;
10752 dictitem_remove(d, di);
10753 }
10754 }
10755 }
10756 hash_unlock(ht);
10757 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010758 else if (argvars[0].v_type == VAR_BLOB)
10759 {
10760 int i;
10761 typval_T tv;
10762
10763 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10764 for (i = 0; i < b->bv_ga.ga_len; i++)
10765 {
10766 tv.v_type = VAR_NUMBER;
10767 tv.vval.v_number = blob_get(b, i);
10768 vimvars[VV_KEY].vv_nr = idx;
10769 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10770 break;
10771 if (tv.v_type != VAR_NUMBER)
10772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010773 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010774 return;
10775 }
10776 tv.v_type = VAR_NUMBER;
10777 blob_set(b, i, tv.vval.v_number);
10778 if (!map && rem)
10779 {
10780 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10781
10782 mch_memmove(p + idx, p + i + 1,
10783 (size_t)b->bv_ga.ga_len - i - 1);
10784 --b->bv_ga.ga_len;
10785 --i;
10786 }
10787 }
10788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010789 else
10790 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010791 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10793
10794 for (li = l->lv_first; li != NULL; li = nli)
10795 {
10796 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10797 break;
10798 nli = li->li_next;
10799 vimvars[VV_KEY].vv_nr = idx;
10800 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10801 || did_emsg)
10802 break;
10803 if (!map && rem)
10804 listitem_remove(l, li);
10805 ++idx;
10806 }
10807 }
10808
10809 restore_vimvar(VV_KEY, &save_key);
10810 restore_vimvar(VV_VAL, &save_val);
10811
10812 did_emsg |= save_did_emsg;
10813 }
10814
10815 copy_tv(&argvars[0], rettv);
10816}
10817
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */