blob: 046688f0b8288a1fdba93f1b092f872baa141459 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100248static void list_one_var(dictitem_T *v, char *prefix, int *first);
249static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100250static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253/* for VIM_VERSION_ defines */
254#include "version.h"
255
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100256
257#if defined(EBCDIC) || defined(PROTO)
258/*
259 * Compare struct fst by function name.
260 */
261 static int
262compare_func_name(const void *s1, const void *s2)
263{
264 struct fst *p1 = (struct fst *)s1;
265 struct fst *p2 = (struct fst *)s2;
266
267 return STRCMP(p1->f_name, p2->f_name);
268}
269
270/*
271 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100272 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100273 * On machines using EBCDIC we have to sort it.
274 */
275 static void
276sortFunctions(void)
277{
278 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
279
280 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
281}
282#endif
283
284
Bram Moolenaar33570922005-01-25 22:26:29 +0000285/*
286 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000287 */
288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100289eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000290{
Bram Moolenaar33570922005-01-25 22:26:29 +0000291 int i;
292 struct vimvar *p;
293
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200294 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
295 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200296 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000297 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300 for (i = 0; i < VV_LEN; ++i)
301 {
302 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200303 if (STRLEN(p->vv_name) > 16)
304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100305 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200306 getout(1);
307 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000308 STRCPY(p->vv_di.di_key, p->vv_name);
309 if (p->vv_flags & VV_RO)
310 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
311 else if (p->vv_flags & VV_RO_SBX)
312 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
313 else
314 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000315
316 /* add to v: scope dict, unless the value is not always available */
317 if (p->vv_type != VAR_UNKNOWN)
318 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000319 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000320 /* add to compat scope dict */
321 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000322 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100323 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
324
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000325 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100326 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100327 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100328 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100329 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100330
331 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
332 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
333 set_vim_var_nr(VV_NONE, VVAL_NONE);
334 set_vim_var_nr(VV_NULL, VVAL_NULL);
335
Bram Moolenaarf562e722016-07-19 17:25:25 +0200336 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
337 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
338 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
339 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
340 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
341 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
342 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
343 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
344 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
345 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100346 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200347
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200348 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200349
350#ifdef EBCDIC
351 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100352 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200353 */
354 sortFunctions();
355#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000356}
357
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000358#if defined(EXITFREE) || defined(PROTO)
359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100360eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000361{
362 int i;
363 struct vimvar *p;
364
365 for (i = 0; i < VV_LEN; ++i)
366 {
367 p = &vimvars[i];
368 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100369 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000370 else if (p->vv_di.di_tv.v_type == VAR_LIST)
371 {
372 list_unref(p->vv_list);
373 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000374 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000375 }
376 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000377 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000378 hash_clear(&compat_hashtab);
379
Bram Moolenaard9fba312005-06-26 22:34:35 +0000380 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100381# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200382 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100383# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000384
385 /* global variables */
386 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000387
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000388 /* autoloaded script names */
389 ga_clear_strings(&ga_loaded);
390
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 /* Script-local variables. First clear all the variables and in a second
392 * loop free the scriptvar_T, because a variable in one script might hold
393 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200395 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200396 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200398 ga_clear(&ga_scripts);
399
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000400 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200401 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000402
403 /* functions */
404 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000405}
406#endif
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 * Set an internal variable to a string value. Creates the variable if it does
411 * not already exist.
412 */
413 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100414set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000416 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000417 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419 val = vim_strsave(value);
420 if (val != NULL)
421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000422 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000423 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000425 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000426 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 }
428 }
429}
430
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000431static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200432#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000433static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000434static char_u *redir_endp = NULL;
435static char_u *redir_varname = NULL;
436
437/*
438 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100439 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000440 * Returns OK if successfully completed the setup. FAIL otherwise.
441 */
442 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100443var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000444{
445 int save_emsg;
446 int err;
447 typval_T tv;
448
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000449 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000450 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100452 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000453 return FAIL;
454 }
455
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000456 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000457 redir_varname = vim_strsave(name);
458 if (redir_varname == NULL)
459 return FAIL;
460
461 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
462 if (redir_lval == NULL)
463 {
464 var_redir_stop();
465 return FAIL;
466 }
467
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000468 /* The output is stored in growarray "redir_ga" until redirection ends. */
469 ga_init2(&redir_ga, (int)sizeof(char), 500);
470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100472 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000473 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
475 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200476 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477 if (redir_endp != NULL && *redir_endp != NUL)
478 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100479 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000480 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100481 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000482 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000483 var_redir_stop();
484 return FAIL;
485 }
486
487 /* check if we can write to the variable: set it to or append an empty
488 * string */
489 save_emsg = did_emsg;
490 did_emsg = FALSE;
491 tv.v_type = VAR_STRING;
492 tv.vval.v_string = (char_u *)"";
493 if (append)
494 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
495 else
496 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200497 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000499 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500 if (err)
501 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000502 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000503 var_redir_stop();
504 return FAIL;
505 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000506
507 return OK;
508}
509
510/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000511 * Append "value[value_len]" to the variable set by var_redir_start().
512 * The actual appending is postponed until redirection ends, because the value
513 * appended may in fact be the string we write to, changing it may cause freed
514 * memory to be used:
515 * :redir => foo
516 * :let foo
517 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 */
519 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100520var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000522 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000523
524 if (redir_lval == NULL)
525 return;
526
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000527 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000529 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000532 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000533 {
534 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000535 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000536 }
537 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539}
540
541/*
542 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000543 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544 */
545 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100546var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000547{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000548 typval_T tv;
549
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200550 if (EVALCMD_BUSY)
551 {
552 redir_lval = NULL;
553 return;
554 }
555
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000556 if (redir_lval != NULL)
557 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000558 /* If there was no error: assign the text to the variable. */
559 if (redir_endp != NULL)
560 {
561 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
562 tv.v_type = VAR_STRING;
563 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200564 /* Call get_lval() again, if it's inside a Dict or List it may
565 * have changed. */
566 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100567 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200568 if (redir_endp != NULL && redir_lval->ll_name != NULL)
569 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
570 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000571 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000572
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000573 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100574 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000575
Bram Moolenaard23a8232018-02-10 18:45:26 +0100576 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000577 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100578 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 int err = FALSE;
611
612 set_vim_var_string(VV_FNAME_IN, fname, -1);
613 set_vim_var_string(VV_CMDARG, args, -1);
614 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
615 err = TRUE;
616 set_vim_var_string(VV_FNAME_IN, NULL, -1);
617 set_vim_var_string(VV_CMDARG, NULL, -1);
618
619 if (err)
620 {
621 mch_remove(fname);
622 return FAIL;
623 }
624 return OK;
625}
626# endif
627
628# if defined(FEAT_DIFF) || defined(PROTO)
629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630eval_diff(
631 char_u *origfile,
632 char_u *newfile,
633 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int err = FALSE;
636
637 set_vim_var_string(VV_FNAME_IN, origfile, -1);
638 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
639 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
640 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
641 set_vim_var_string(VV_FNAME_IN, NULL, -1);
642 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
643 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
644}
645
646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100647eval_patch(
648 char_u *origfile,
649 char_u *difffile,
650 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err;
653
654 set_vim_var_string(VV_FNAME_IN, origfile, -1);
655 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
656 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
657 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
660 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
661}
662# endif
663
664/*
665 * Top level evaluation function, returning a boolean.
666 * Sets "error" to TRUE if there was an error.
667 * Return TRUE or FALSE.
668 */
669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100670eval_to_bool(
671 char_u *arg,
672 int *error,
673 char_u **nextcmd,
674 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
Bram Moolenaar33570922005-01-25 22:26:29 +0000676 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 if (skip)
680 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000681 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 else
684 {
685 *error = FALSE;
686 if (!skip)
687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100688 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000689 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691 }
692 if (skip)
693 --emsg_skip;
694
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200695 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100698/*
699 * Call eval1() and give an error message if not done at a lower level.
700 */
701 static int
702eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
703{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100704 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100705 int ret;
706 int did_emsg_before = did_emsg;
707 int called_emsg_before = called_emsg;
708
709 ret = eval1(arg, rettv, evaluate);
710 if (ret == FAIL)
711 {
712 // Report the invalid expression unless the expression evaluation has
713 // been cancelled due to an aborting error, an interrupt, or an
714 // exception, or we already gave a more specific error.
715 // Also check called_emsg for when using assert_fails().
716 if (!aborting() && did_emsg == did_emsg_before
717 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100718 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100719 }
720 return ret;
721}
722
Bram Moolenaar48570482017-10-30 21:48:41 +0100723 static int
724eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
725{
726 char_u *s;
727 int dummy;
728 char_u buf[NUMBUFLEN];
729
730 if (expr->v_type == VAR_FUNC)
731 {
732 s = expr->vval.v_string;
733 if (s == NULL || *s == NUL)
734 return FAIL;
735 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
736 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
737 return FAIL;
738 }
739 else if (expr->v_type == VAR_PARTIAL)
740 {
741 partial_T *partial = expr->vval.v_partial;
742
743 s = partial_name(partial);
744 if (s == NULL || *s == NUL)
745 return FAIL;
746 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
747 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
748 return FAIL;
749 }
750 else
751 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100752 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100753 if (s == NULL)
754 return FAIL;
755 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100756 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100757 return FAIL;
758 if (*s != NUL) /* check for trailing chars after expr */
759 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200760 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100761 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100762 return FAIL;
763 }
764 }
765 return OK;
766}
767
768/*
769 * Like eval_to_bool() but using a typval_T instead of a string.
770 * Works for string, funcref and partial.
771 */
772 int
773eval_expr_to_bool(typval_T *expr, int *error)
774{
775 typval_T rettv;
776 int res;
777
778 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
779 {
780 *error = TRUE;
781 return FALSE;
782 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100783 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100784 clear_tv(&rettv);
785 return res;
786}
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788/*
789 * Top level evaluation function, returning a string. If "skip" is TRUE,
790 * only parsing to "nextcmd" is done, without reporting errors. Return
791 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
792 */
793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100794eval_to_string_skip(
795 char_u *arg,
796 char_u **nextcmd,
797 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 char_u *retval;
801
802 if (skip)
803 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000804 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 retval = NULL;
806 else
807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100808 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811 if (skip)
812 --emsg_skip;
813
814 return retval;
815}
816
817/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000818 * Skip over an expression at "*pp".
819 * Return FAIL for an error, OK otherwise.
820 */
821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100822skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000823{
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000825
826 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000827 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000828}
829
830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000832 * When "convert" is TRUE convert a List into a sequence of lines and convert
833 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Return pointer to allocated memory, or NULL for failure.
835 */
836 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100837eval_to_string(
838 char_u *arg,
839 char_u **nextcmd,
840 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000845#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000846 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 retval = NULL;
851 else
852 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000853 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000854 {
855 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000856 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200857 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200858 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200859 if (tv.vval.v_list->lv_len > 0)
860 ga_append(&ga, NL);
861 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000862 ga_append(&ga, NUL);
863 retval = (char_u *)ga.ga_data;
864 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000865#ifdef FEAT_FLOAT
866 else if (convert && tv.v_type == VAR_FLOAT)
867 {
868 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
869 retval = vim_strsave(numbuf);
870 }
871#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000872 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100873 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
877 return retval;
878}
879
880/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000881 * Call eval_to_string() without using current local variables and using
882 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 */
884 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100885eval_to_string_safe(
886 char_u *arg,
887 char_u **nextcmd,
888 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200891 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200893 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000894 if (use_sandbox)
895 ++sandbox;
896 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000897 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000898 if (use_sandbox)
899 --sandbox;
900 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200901 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 return retval;
903}
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905/*
906 * Top level evaluation function, returning a number.
907 * Evaluates "expr" silently.
908 * Returns -1 for an error.
909 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200910 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100911eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200914 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000915 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 ++emsg_off;
918
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000919 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = -1;
921 else
922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100923 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 --emsg_off;
927
928 return retval;
929}
930
Bram Moolenaara40058a2005-07-11 22:42:07 +0000931/*
932 * Prepare v: variable "idx" to be used.
933 * Save the current typeval in "save_tv".
934 * When not used yet add the variable to the v: hashtable.
935 */
936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000938{
939 *save_tv = vimvars[idx].vv_tv;
940 if (vimvars[idx].vv_type == VAR_UNKNOWN)
941 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
942}
943
944/*
945 * Restore v: variable "idx" to typeval "save_tv".
946 * When no longer defined, remove the variable from the v: hashtable.
947 */
948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100949restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000950{
951 hashitem_T *hi;
952
Bram Moolenaara40058a2005-07-11 22:42:07 +0000953 vimvars[idx].vv_tv = *save_tv;
954 if (vimvars[idx].vv_type == VAR_UNKNOWN)
955 {
956 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
957 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100958 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000959 else
960 hash_remove(&vimvarht, hi);
961 }
962}
963
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000964#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000965/*
966 * Evaluate an expression to a list with suggestions.
967 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000968 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000969 */
970 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000972{
973 typval_T save_val;
974 typval_T rettv;
975 list_T *list = NULL;
976 char_u *p = skipwhite(expr);
977
978 /* Set "v:val" to the bad word. */
979 prepare_vimvar(VV_VAL, &save_val);
980 vimvars[VV_VAL].vv_type = VAR_STRING;
981 vimvars[VV_VAL].vv_str = badword;
982 if (p_verbose == 0)
983 ++emsg_off;
984
985 if (eval1(&p, &rettv, TRUE) == OK)
986 {
987 if (rettv.v_type != VAR_LIST)
988 clear_tv(&rettv);
989 else
990 list = rettv.vval.v_list;
991 }
992
993 if (p_verbose == 0)
994 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000995 restore_vimvar(VV_VAL, &save_val);
996
997 return list;
998}
999
1000/*
1001 * "list" is supposed to contain two items: a word and a number. Return the
1002 * word in "pp" and the number as the return value.
1003 * Return -1 if anything isn't right.
1004 * Used to get the good word and score from the eval_spell_expr() result.
1005 */
1006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001007get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001008{
1009 listitem_T *li;
1010
1011 li = list->lv_first;
1012 if (li == NULL)
1013 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001014 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001015
1016 li = li->li_next;
1017 if (li == NULL)
1018 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001019 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001020}
1021#endif
1022
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001023/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001024 * Top level evaluation function.
1025 * Returns an allocated typval_T with the result.
1026 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001027 */
1028 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001029eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001030{
1031 typval_T *tv;
1032
1033 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001034 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001035 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001036
1037 return tv;
1038}
1039
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001042 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001043 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1044 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001045 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048call_vim_function(
1049 char_u *func,
1050 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001051 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001052 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001055 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001057 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001058 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001060 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001061 if (ret == FAIL)
1062 clear_tv(rettv);
1063
1064 return ret;
1065}
1066
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001067/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001068 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001069 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001070 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1071 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001072 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001073 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074call_func_retnr(
1075 char_u *func,
1076 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001077 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001078{
1079 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001080 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001081
Bram Moolenaarded27a12018-08-01 19:06:03 +02001082 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001083 return -1;
1084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001085 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001086 clear_tv(&rettv);
1087 return retval;
1088}
1089
1090#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1091 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1092
Bram Moolenaar4f688582007-07-24 12:34:30 +00001093# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001095 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001096 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001097 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1098 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 */
1100 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001101call_func_retstr(
1102 char_u *func,
1103 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001104 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001105{
1106 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001107 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001108
Bram Moolenaarded27a12018-08-01 19:06:03 +02001109 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001110 return NULL;
1111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001112 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return retval;
1115}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001116# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001117
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001118/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001119 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001120 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1121 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001122 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001123 */
1124 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001125call_func_retlist(
1126 char_u *func,
1127 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001128 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001129{
1130 typval_T rettv;
1131
Bram Moolenaarded27a12018-08-01 19:06:03 +02001132 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001133 return NULL;
1134
1135 if (rettv.v_type != VAR_LIST)
1136 {
1137 clear_tv(&rettv);
1138 return NULL;
1139 }
1140
1141 return rettv.vval.v_list;
1142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144
Bram Moolenaar05159a02005-02-26 23:04:13 +00001145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#ifdef FEAT_FOLDING
1147/*
1148 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1149 * it in "*cp". Doesn't give error messages.
1150 */
1151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001152eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
Bram Moolenaar33570922005-01-25 22:26:29 +00001154 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001155 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001157 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1158 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001161 if (use_sandbox)
1162 ++sandbox;
1163 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001165 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 retval = 0;
1167 else
1168 {
1169 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 if (tv.v_type == VAR_NUMBER)
1171 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001172 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 retval = 0;
1174 else
1175 {
1176 /* If the result is a string, check if there is a non-digit before
1177 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (!VIM_ISDIGIT(*s) && *s != '-')
1180 *cp = *s++;
1181 retval = atol((char *)s);
1182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001186 if (use_sandbox)
1187 --sandbox;
1188 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
1192#endif
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 * ":let" list all variable values
1196 * ":let var1 var2" list variable values
1197 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001198 * ":let var += expr" assignment command.
1199 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001200 * ":let var *= expr" assignment command.
1201 * ":let var /= expr" assignment command.
1202 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001203 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 */
1206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001207ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208{
1209 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001211 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 int var_count = 0;
1214 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001215 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001216 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001217 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaardb552d602006-03-23 22:59:57 +00001219 argend = skip_var_list(arg, &var_count, &semicolon);
1220 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001221 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001222 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001223 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001224 expr = skipwhite(argend);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001225 if (*expr != '=' && !(vim_strchr((char_u *)"+-*/%.", *expr) != NULL
Bram Moolenaara3920382014-03-30 16:49:09 +02001226 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001228 /*
1229 * ":let" without "=": list variables
1230 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001231 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001232 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001233 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001235 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001236 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001239 list_glob_vars(&first);
1240 list_buf_vars(&first);
1241 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001242 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001243 list_script_vars(&first);
1244 list_func_vars(&first);
1245 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 eap->nextcmd = check_nextcmd(arg);
1248 }
1249 else
1250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251 op[0] = '=';
1252 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001253 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001254 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001255 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1256 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaara3920382014-03-30 16:49:09 +02001257 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001258 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001259 else
1260 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (eap->skip)
1263 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001264 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (eap->skip)
1266 {
1267 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 --emsg_skip;
1270 }
1271 else if (i != FAIL)
1272 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001273 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001275 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 }
1278}
1279
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001280/*
1281 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1282 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 * When "nextchars" is not NULL it points to a string with characters that
1284 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1285 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001286 * Returns OK or FAIL;
1287 */
1288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289ex_let_vars(
1290 char_u *arg_start,
1291 typval_T *tv,
1292 int copy, /* copy values from "tv", don't move */
1293 int semicolon, /* from skip_var_list() */
1294 int var_count, /* from skip_var_list() */
1295 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001296{
1297 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 listitem_T *item;
1301 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001302
1303 if (*arg != '[')
1304 {
1305 /*
1306 * ":let var = expr" or ":for var in list"
1307 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001308 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001309 return FAIL;
1310 return OK;
1311 }
1312
1313 /*
1314 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1315 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001316 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001318 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001319 return FAIL;
1320 }
1321
1322 i = list_len(l);
1323 if (semicolon == 0 && var_count < i)
1324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001325 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001326 return FAIL;
1327 }
1328 if (var_count - semicolon > i)
1329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001330 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001331 return FAIL;
1332 }
1333
1334 item = l->lv_first;
1335 while (*arg != ']')
1336 {
1337 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001339 item = item->li_next;
1340 if (arg == NULL)
1341 return FAIL;
1342
1343 arg = skipwhite(arg);
1344 if (*arg == ';')
1345 {
1346 /* Put the rest of the list (may be empty) in the var after ';'.
1347 * Create a new list for this. */
1348 l = list_alloc();
1349 if (l == NULL)
1350 return FAIL;
1351 while (item != NULL)
1352 {
1353 list_append_tv(l, &item->li_tv);
1354 item = item->li_next;
1355 }
1356
1357 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001358 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001359 ltv.vval.v_list = l;
1360 l->lv_refcount = 1;
1361
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1363 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364 clear_tv(&ltv);
1365 if (arg == NULL)
1366 return FAIL;
1367 break;
1368 }
1369 else if (*arg != ',' && *arg != ']')
1370 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001371 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 return FAIL;
1373 }
1374 }
1375
1376 return OK;
1377}
1378
1379/*
1380 * Skip over assignable variable "var" or list of variables "[var, var]".
1381 * Used for ":let varvar = expr" and ":for varvar in expr".
1382 * For "[var, var]" increment "*var_count" for each variable.
1383 * for "[var, var; var]" set "semicolon".
1384 * Return NULL for an error.
1385 */
1386 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001387skip_var_list(
1388 char_u *arg,
1389 int *var_count,
1390 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001391{
1392 char_u *p, *s;
1393
1394 if (*arg == '[')
1395 {
1396 /* "[var, var]": find the matching ']'. */
1397 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001398 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001399 {
1400 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1401 s = skip_var_one(p);
1402 if (s == p)
1403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001404 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405 return NULL;
1406 }
1407 ++*var_count;
1408
1409 p = skipwhite(s);
1410 if (*p == ']')
1411 break;
1412 else if (*p == ';')
1413 {
1414 if (*semicolon == 1)
1415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001416 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417 return NULL;
1418 }
1419 *semicolon = 1;
1420 }
1421 else if (*p != ',')
1422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001423 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424 return NULL;
1425 }
1426 }
1427 return p + 1;
1428 }
1429 else
1430 return skip_var_one(arg);
1431}
1432
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001433/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001434 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001435 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001436 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001438skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001439{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001440 if (*arg == '@' && arg[1] != NUL)
1441 return arg + 2;
1442 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1443 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444}
1445
Bram Moolenaara7043832005-01-21 11:56:39 +00001446/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001447 * List variables for hashtab "ht" with prefix "prefix".
1448 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001449 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001451list_hashtable_vars(
1452 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001453 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001454 int empty,
1455 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001456{
Bram Moolenaar33570922005-01-25 22:26:29 +00001457 hashitem_T *hi;
1458 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001459 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001460 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001461
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001462 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001463 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1464 {
1465 if (!HASHITEM_EMPTY(hi))
1466 {
1467 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001468 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001469
1470 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001471 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1472 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001473 if (message_filtered(buf))
1474 continue;
1475
Bram Moolenaar33570922005-01-25 22:26:29 +00001476 if (empty || di->di_tv.v_type != VAR_STRING
1477 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001478 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001479 }
1480 }
1481}
1482
1483/*
1484 * List global variables.
1485 */
1486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001488{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001489 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001490}
1491
1492/*
1493 * List buffer variables.
1494 */
1495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001497{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001498 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001499}
1500
1501/*
1502 * List window variables.
1503 */
1504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001506{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001507 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "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 Moolenaar32526b32019-01-19 17:43:09 +01001516 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001517}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001518
Bram Moolenaara7043832005-01-21 11:56:39 +00001519/*
1520 * List Vim variables.
1521 */
1522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001525 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526}
1527
1528/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001529 * List script-local variables, if there is a script.
1530 */
1531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001533{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001534 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1535 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001536 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001537}
1538
1539/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 * List variables in "arg".
1541 */
1542 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544{
1545 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001546 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001548 char_u *name_start;
1549 char_u *arg_subsc;
1550 char_u *tofree;
1551 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001552
1553 while (!ends_excmd(*arg) && !got_int)
1554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001555 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001556 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001557 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001558 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001559 {
1560 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001561 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001562 break;
1563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001567 /* get_name_len() takes care of expanding curly braces */
1568 name_start = name = arg;
1569 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1570 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 /* This is mainly to keep test 49 working: when expanding
1573 * curly braces fails overrule the exception error message. */
1574 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001576 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001577 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578 break;
1579 }
1580 error = TRUE;
1581 }
1582 else
1583 {
1584 if (tofree != NULL)
1585 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001586 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 else
1589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001590 /* handle d.key, l[idx], f(expr) */
1591 arg_subsc = arg;
1592 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001593 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001598 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001599 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001600 case 'g': list_glob_vars(first); break;
1601 case 'b': list_buf_vars(first); break;
1602 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001603 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001604 case 'v': list_vim_vars(first); break;
1605 case 's': list_script_vars(first); break;
1606 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001607 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001608 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001609 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001610 }
1611 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001612 {
1613 char_u numbuf[NUMBUFLEN];
1614 char_u *tf;
1615 int c;
1616 char_u *s;
1617
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001618 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001619 c = *arg;
1620 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001621 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001622 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001623 tv.v_type,
1624 s == NULL ? (char_u *)"" : s,
1625 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001626 *arg = c;
1627 vim_free(tf);
1628 }
1629 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 }
1632 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001633
1634 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636
1637 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 }
1639
1640 return arg;
1641}
1642
1643/*
1644 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1645 * Returns a pointer to the char just after the var name.
1646 * Returns NULL if there is an error.
1647 */
1648 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649ex_let_one(
1650 char_u *arg, /* points to variable name */
1651 typval_T *tv, /* value to assign to variable */
1652 int copy, /* copy value from "tv" */
1653 char_u *endchars, /* valid chars after variable name or NULL */
1654 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655{
1656 int c1;
1657 char_u *name;
1658 char_u *p;
1659 char_u *arg_end = NULL;
1660 int len;
1661 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663
1664 /*
1665 * ":let $VAR = expr": Set environment variable.
1666 */
1667 if (*arg == '$')
1668 {
1669 /* Find the end of the name. */
1670 ++arg;
1671 name = arg;
1672 len = get_env_len(&arg);
1673 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001674 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 else
1676 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001677 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001678 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001681 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001682 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 {
1684 c1 = name[len];
1685 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001686 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001687 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 {
1689 int mustfree = FALSE;
1690 char_u *s = vim_getenv(name, &mustfree);
1691
1692 if (s != NULL)
1693 {
1694 p = tofree = concat_str(s, p);
1695 if (mustfree)
1696 vim_free(s);
1697 }
1698 }
1699 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001702 if (STRICMP(name, "HOME") == 0)
1703 init_homedir();
1704 else if (didset_vim && STRICMP(name, "VIM") == 0)
1705 didset_vim = FALSE;
1706 else if (didset_vimruntime
1707 && STRICMP(name, "VIMRUNTIME") == 0)
1708 didset_vimruntime = FALSE;
1709 arg_end = arg;
1710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 }
1714 }
1715 }
1716
1717 /*
1718 * ":let &option = expr": Set option value.
1719 * ":let &l:option = expr": Set local option value.
1720 * ":let &g:option = expr": Set global option value.
1721 */
1722 else if (*arg == '&')
1723 {
1724 /* Find the end of the name. */
1725 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 if (p == NULL || (endchars != NULL
1727 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001728 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 else
1730 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 long n;
1732 int opt_type;
1733 long numval;
1734 char_u *stringval = NULL;
1735 char_u *s;
1736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001737 c1 = *p;
1738 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001740 n = (long)tv_get_number(tv);
1741 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001742 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001743 {
1744 opt_type = get_option_value(arg, &numval,
1745 &stringval, opt_flags);
1746 if ((opt_type == 1 && *op == '.')
1747 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001749 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001750 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 else
1753 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001754 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001756 switch (*op)
1757 {
1758 case '+': n = numval + n; break;
1759 case '-': n = numval - n; break;
1760 case '*': n = numval * n; break;
1761 case '/': n = numval / n; break;
1762 case '%': n = numval % n; break;
1763 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001765 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 {
1767 s = concat_str(stringval, s);
1768 vim_free(stringval);
1769 stringval = s;
1770 }
1771 }
1772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001773 if (s != NULL)
1774 {
1775 set_option_value(arg, n, s, opt_flags);
1776 arg_end = p;
1777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 }
1781 }
1782
1783 /*
1784 * ":let @r = expr": Set register contents.
1785 */
1786 else if (*arg == '@')
1787 {
1788 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001789 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001790 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001793 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 else
1795 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001796 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 char_u *s;
1798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001799 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001800 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001802 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 if (s != NULL)
1804 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001805 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 vim_free(s);
1807 }
1808 }
1809 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001811 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001812 arg_end = arg + 1;
1813 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001814 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 }
1816 }
1817
1818 /*
1819 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001820 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001822 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001824 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001826 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001829 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001830 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001831 else
1832 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001834 arg_end = p;
1835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 }
1839
1840 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001841 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842
1843 return arg_end;
1844}
1845
1846/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 * Get an lval: variable, Dict item or List item that can be assigned a value
1848 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1849 * "name.key", "name.key[expr]" etc.
1850 * Indexing only works if "name" is an existing List or Dictionary.
1851 * "name" points to the start of the name.
1852 * If "rettv" is not NULL it points to the value to be assigned.
1853 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1854 * wrong; must end in space or cmd separator.
1855 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001856 * flags:
1857 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001858 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001859 * GLV_NO_AUTOLOAD: do not use script autoloading
1860 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001861 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001863 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001866get_lval(
1867 char_u *name,
1868 typval_T *rettv,
1869 lval_T *lp,
1870 int unlet,
1871 int skip,
1872 int flags, /* GLV_ values */
1873 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001876 char_u *expr_start, *expr_end;
1877 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 dictitem_T *v;
1879 typval_T var1;
1880 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001883 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001884 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001886 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001890
1891 if (skip)
1892 {
1893 /* When skipping just find the end of the name. */
1894 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001895 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 }
1897
1898 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001899 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001900 if (expr_start != NULL)
1901 {
1902 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001903 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 && *p != '[' && *p != '.')
1905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001906 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001907 return NULL;
1908 }
1909
1910 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1911 if (lp->ll_exp_name == NULL)
1912 {
1913 /* Report an invalid expression in braces, unless the
1914 * expression evaluation has been cancelled due to an
1915 * aborting error, an interrupt, or an exception. */
1916 if (!aborting() && !quiet)
1917 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001918 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001919 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 return NULL;
1921 }
1922 }
1923 lp->ll_name = lp->ll_exp_name;
1924 }
1925 else
1926 lp->ll_name = name;
1927
1928 /* Without [idx] or .key we are done. */
1929 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1930 return p;
1931
1932 cc = *p;
1933 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001934 /* Only pass &ht when we would write to the variable, it prevents autoload
1935 * as well. */
1936 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1937 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 if (v == NULL)
1942 return NULL;
1943
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 /*
1945 * Loop until no more [idx] or .key is following.
1946 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001948 var1.v_type = VAR_UNKNOWN;
1949 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001952 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1953 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001954 && lp->ll_tv->vval.v_dict != NULL)
1955 && !(lp->ll_tv->v_type == VAR_BLOB
1956 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001965 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001968
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 len = -1;
1970 if (*p == '.')
1971 {
1972 key = p + 1;
1973 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1974 ;
1975 if (len == 0)
1976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001980 }
1981 p = key + len;
1982 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 else
1984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001985 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001986 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001987 if (*p == ':')
1988 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001989 else
1990 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001991 empty1 = FALSE;
1992 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001994 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 {
1996 /* not a number or string */
1997 clear_tv(&var1);
1998 return NULL;
1999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002000 }
2001
2002 /* Optionally get the second index [ :expr]. */
2003 if (*p == ':')
2004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002009 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002011 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002012 if (rettv != NULL
2013 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002014 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002015 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002016 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002019 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 }
2023 p = skipwhite(p + 1);
2024 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 else
2027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2030 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002031 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002034 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002035 {
2036 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002037 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002038 clear_tv(&var2);
2039 return NULL;
2040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002044 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002046
Bram Moolenaar8c711452005-01-14 21:53:12 +00002047 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002050 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002051 clear_tv(&var1);
2052 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 }
2055
2056 /* Skip to past ']'. */
2057 ++p;
2058 }
2059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 {
2062 if (len == -1)
2063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002065 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002066 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002068 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 }
2071 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002072 lp->ll_list = NULL;
2073 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002074 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002076 /* When assigning to a scope dictionary check that a function and
2077 * variable name is valid (only variable name unless it is l: or
2078 * g: dictionary). Disallow overwriting a builtin function. */
2079 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002080 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002081 int prevval;
2082 int wrong;
2083
2084 if (len != -1)
2085 {
2086 prevval = key[len];
2087 key[len] = NUL;
2088 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002089 else
2090 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002091 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2092 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002093 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002094 || !valid_varname(key);
2095 if (len != -1)
2096 key[len] = prevval;
2097 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002098 return NULL;
2099 }
2100
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002101 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002102 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002103 // Can't add "v:" or "a:" variable.
2104 if (lp->ll_dict == &vimvardict
2105 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002107 semsg(_(e_illvar), name);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002108 return NULL;
2109 }
2110
Bram Moolenaar31b81602019-02-10 22:14:27 +01002111 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002116 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002118 }
2119 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002121 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002123 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 p = NULL;
2126 break;
2127 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002128 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002129 else if ((flags & GLV_READ_ONLY) == 0
2130 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002131 {
2132 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002133 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002134 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002135
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002136 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002139 else if (lp->ll_tv->v_type == VAR_BLOB)
2140 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002141 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2142
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002143 /*
2144 * Get the number and item for the only or first index of the List.
2145 */
2146 if (empty1)
2147 lp->ll_n1 = 0;
2148 else
2149 // is number or string
2150 lp->ll_n1 = (long)tv_get_number(&var1);
2151 clear_tv(&var1);
2152
2153 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002154 || lp->ll_n1 > bloblen
2155 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002156 {
2157 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002159 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002160 return NULL;
2161 }
2162 if (lp->ll_range && !lp->ll_empty2)
2163 {
2164 lp->ll_n2 = (long)tv_get_number(&var2);
2165 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002166 if (lp->ll_n2 < 0
2167 || lp->ll_n2 >= bloblen
2168 || lp->ll_n2 < lp->ll_n1)
2169 {
2170 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002172 return NULL;
2173 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002174 }
2175 lp->ll_blob = lp->ll_tv->vval.v_blob;
2176 lp->ll_tv = NULL;
2177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 else
2179 {
2180 /*
2181 * Get the number and item for the only or first index of the List.
2182 */
2183 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002186 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002187 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002188 clear_tv(&var1);
2189
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002190 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 lp->ll_list = lp->ll_tv->vval.v_list;
2192 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2193 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002195 if (lp->ll_n1 < 0)
2196 {
2197 lp->ll_n1 = 0;
2198 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2199 }
2200 }
2201 if (lp->ll_li == NULL)
2202 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002203 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002204 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002205 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 }
2208
2209 /*
2210 * May need to find the item or absolute index for the second
2211 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 * When no index given: "lp->ll_empty2" is TRUE.
2213 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002218 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002224 {
2225 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002226 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002228 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002230 }
2231
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2233 if (lp->ll_n1 < 0)
2234 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2235 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002236 {
2237 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002238 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002240 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002241 }
2242
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 }
2246
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002247 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 return p;
2249}
2250
2251/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002255clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256{
2257 vim_free(lp->ll_exp_name);
2258 vim_free(lp->ll_newkey);
2259}
2260
2261/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002262 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002264 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2265 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 */
2267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268set_var_lval(
2269 lval_T *lp,
2270 char_u *endp,
2271 typval_T *rettv,
2272 int copy,
2273 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274{
2275 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 listitem_T *ri;
2277 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278
2279 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002281 cc = *endp;
2282 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002283 if (lp->ll_blob != NULL)
2284 {
2285 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002286
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002287 if (op != NULL && *op != '=')
2288 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002289 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002290 return;
2291 }
2292
2293 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2294 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002295 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002296
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002297 if (lp->ll_empty2)
2298 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2299
2300 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002302 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002303 return;
2304 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002305 if (lp->ll_empty2)
2306 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002307
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002308 ir = 0;
2309 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2310 blob_set(lp->ll_blob, il,
2311 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002312 }
2313 else
2314 {
2315 val = (int)tv_get_number_chk(rettv, &error);
2316 if (!error)
2317 {
2318 garray_T *gap = &lp->ll_blob->bv_ga;
2319
2320 // Allow for appending a byte. Setting a byte beyond
2321 // the end is an error otherwise.
2322 if (lp->ll_n1 < gap->ga_len
2323 || (lp->ll_n1 == gap->ga_len
2324 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2325 {
2326 blob_set(lp->ll_blob, lp->ll_n1, val);
2327 if (lp->ll_n1 == gap->ga_len)
2328 ++gap->ga_len;
2329 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002330 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002331 }
2332 }
2333 }
2334 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002336 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337
Bram Moolenaarff697e62019-02-12 22:28:33 +01002338 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002339 di = NULL;
2340 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2341 &tv, &di, TRUE, FALSE) == OK)
2342 {
2343 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002344 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2345 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002346 && tv_op(&tv, rettv, op) == OK)
2347 set_var(lp->ll_name, &tv, FALSE);
2348 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002351 else
2352 set_var(lp->ll_name, rettv, copy);
2353 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002355 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002356 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002357 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002358 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 else if (lp->ll_range)
2360 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002361 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002362 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002363
2364 /*
2365 * Check whether any of the list items is locked
2366 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002367 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002368 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002369 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002370 return;
2371 ri = ri->li_next;
2372 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2373 break;
2374 ll_li = ll_li->li_next;
2375 ++ll_n1;
2376 }
2377
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 /*
2379 * Assign the List values to the list items.
2380 */
2381 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 if (op != NULL && *op != '=')
2384 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2385 else
2386 {
2387 clear_tv(&lp->ll_li->li_tv);
2388 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2389 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 ri = ri->li_next;
2391 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2392 break;
2393 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002394 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002396 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002397 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 ri = NULL;
2399 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002400 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002401 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 lp->ll_li = lp->ll_li->li_next;
2403 ++lp->ll_n1;
2404 }
2405 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002406 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 else if (lp->ll_empty2
2408 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002410 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 }
2412 else
2413 {
2414 /*
2415 * Assign to a List or Dictionary item.
2416 */
2417 if (lp->ll_newkey != NULL)
2418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 if (op != NULL && *op != '=')
2420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002421 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 return;
2423 }
2424
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002426 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (di == NULL)
2428 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002429 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2430 {
2431 vim_free(di);
2432 return;
2433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002435 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 else if (op != NULL && *op != '=')
2437 {
2438 tv_op(lp->ll_tv, rettv, op);
2439 return;
2440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 /*
2445 * Assign the value to the variable or list item.
2446 */
2447 if (copy)
2448 copy_tv(rettv, lp->ll_tv);
2449 else
2450 {
2451 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002452 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 }
2455 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456}
2457
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002458/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002459 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2460 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 * Returns OK or FAIL.
2462 */
2463 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002464tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002466 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 char_u numbuf[NUMBUFLEN];
2468 char_u *s;
2469
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002470 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2471 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2472 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 {
2474 switch (tv1->v_type)
2475 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002476 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 case VAR_DICT:
2478 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002479 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002480 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002481 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002482 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 break;
2484
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002485 case VAR_BLOB:
2486 if (*op != '+' || tv2->v_type != VAR_BLOB)
2487 break;
2488 // BLOB += BLOB
2489 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2490 {
2491 blob_T *b1 = tv1->vval.v_blob;
2492 blob_T *b2 = tv2->vval.v_blob;
2493 int i, len = blob_len(b2);
2494 for (i = 0; i < len; i++)
2495 ga_append(&b1->bv_ga, blob_get(b2, i));
2496 }
2497 return OK;
2498
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 case VAR_LIST:
2500 if (*op != '+' || tv2->v_type != VAR_LIST)
2501 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002502 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2504 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2505 return OK;
2506
2507 case VAR_NUMBER:
2508 case VAR_STRING:
2509 if (tv2->v_type == VAR_LIST)
2510 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002511 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002513 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002514 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002515#ifdef FEAT_FLOAT
2516 if (tv2->v_type == VAR_FLOAT)
2517 {
2518 float_T f = n;
2519
Bram Moolenaarff697e62019-02-12 22:28:33 +01002520 if (*op == '%')
2521 break;
2522 switch (*op)
2523 {
2524 case '+': f += tv2->vval.v_float; break;
2525 case '-': f -= tv2->vval.v_float; break;
2526 case '*': f *= tv2->vval.v_float; break;
2527 case '/': f /= tv2->vval.v_float; break;
2528 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002529 clear_tv(tv1);
2530 tv1->v_type = VAR_FLOAT;
2531 tv1->vval.v_float = f;
2532 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002534#endif
2535 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002536 switch (*op)
2537 {
2538 case '+': n += tv_get_number(tv2); break;
2539 case '-': n -= tv_get_number(tv2); break;
2540 case '*': n *= tv_get_number(tv2); break;
2541 case '/': n /= tv_get_number(tv2); break;
2542 case '%': n %= tv_get_number(tv2); break;
2543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002544 clear_tv(tv1);
2545 tv1->v_type = VAR_NUMBER;
2546 tv1->vval.v_number = n;
2547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 }
2549 else
2550 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002551 if (tv2->v_type == VAR_FLOAT)
2552 break;
2553
Bram Moolenaarff697e62019-02-12 22:28:33 +01002554 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002555 s = tv_get_string(tv1);
2556 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 clear_tv(tv1);
2558 tv1->v_type = VAR_STRING;
2559 tv1->vval.v_string = s;
2560 }
2561 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002563 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002564#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002565 {
2566 float_T f;
2567
Bram Moolenaarff697e62019-02-12 22:28:33 +01002568 if (*op == '%' || *op == '.'
2569 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002570 && tv2->v_type != VAR_NUMBER
2571 && tv2->v_type != VAR_STRING))
2572 break;
2573 if (tv2->v_type == VAR_FLOAT)
2574 f = tv2->vval.v_float;
2575 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002576 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002577 switch (*op)
2578 {
2579 case '+': tv1->vval.v_float += f; break;
2580 case '-': tv1->vval.v_float -= f; break;
2581 case '*': tv1->vval.v_float *= f; break;
2582 case '/': tv1->vval.v_float /= f; break;
2583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002584 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002586 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002587 }
2588 }
2589
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002590 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 return FAIL;
2592}
2593
2594/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002595 * Evaluate the expression used in a ":for var in expr" command.
2596 * "arg" points to "var".
2597 * Set "*errp" to TRUE for an error, FALSE otherwise;
2598 * Return a pointer that holds the info. Null when there is an error.
2599 */
2600 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002601eval_for_line(
2602 char_u *arg,
2603 int *errp,
2604 char_u **nextcmdp,
2605 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002606{
Bram Moolenaar33570922005-01-25 22:26:29 +00002607 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002608 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002609 typval_T tv;
2610 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002611
2612 *errp = TRUE; /* default: there is an error */
2613
Bram Moolenaar33570922005-01-25 22:26:29 +00002614 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002615 if (fi == NULL)
2616 return NULL;
2617
2618 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2619 if (expr == NULL)
2620 return fi;
2621
2622 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002623 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002625 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002626 return fi;
2627 }
2628
2629 if (skip)
2630 ++emsg_skip;
2631 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2632 {
2633 *errp = FALSE;
2634 if (!skip)
2635 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002636 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002637 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002638 l = tv.vval.v_list;
2639 if (l == NULL)
2640 {
2641 // a null list is like an empty list: do nothing
2642 clear_tv(&tv);
2643 }
2644 else
2645 {
2646 // No need to increment the refcount, it's already set for
2647 // the list being used in "tv".
2648 fi->fi_list = l;
2649 list_add_watch(l, &fi->fi_lw);
2650 fi->fi_lw.lw_item = l->lv_first;
2651 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002652 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002653 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002654 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002655 fi->fi_bi = 0;
2656 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002657 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002658 typval_T btv;
2659
2660 // Make a copy, so that the iteration still works when the
2661 // blob is changed.
2662 blob_copy(&tv, &btv);
2663 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002664 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002665 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002666 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002667 else
2668 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002669 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002670 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002671 }
2672 }
2673 }
2674 if (skip)
2675 --emsg_skip;
2676
2677 return fi;
2678}
2679
2680/*
2681 * Use the first item in a ":for" list. Advance to the next.
2682 * Assign the values to the variable (list). "arg" points to the first one.
2683 * Return TRUE when a valid item was found, FALSE when at end of list or
2684 * something wrong.
2685 */
2686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002688{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002689 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002690 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002692
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002693 if (fi->fi_blob != NULL)
2694 {
2695 typval_T tv;
2696
2697 if (fi->fi_bi >= blob_len(fi->fi_blob))
2698 return FALSE;
2699 tv.v_type = VAR_NUMBER;
2700 tv.v_lock = VAR_FIXED;
2701 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2702 ++fi->fi_bi;
2703 return ex_let_vars(arg, &tv, TRUE,
2704 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2705 }
2706
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707 item = fi->fi_lw.lw_item;
2708 if (item == NULL)
2709 result = FALSE;
2710 else
2711 {
2712 fi->fi_lw.lw_item = item->li_next;
2713 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2714 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2715 }
2716 return result;
2717}
2718
2719/*
2720 * Free the structure used to store info used by ":for".
2721 */
2722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002723free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724{
Bram Moolenaar33570922005-01-25 22:26:29 +00002725 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002726
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002727 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002728 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002729 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002730 list_unref(fi->fi_list);
2731 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002732 if (fi != NULL && fi->fi_blob != NULL)
2733 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 vim_free(fi);
2735}
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2738
2739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002740set_context_for_expression(
2741 expand_T *xp,
2742 char_u *arg,
2743 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 int got_eq = FALSE;
2746 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749 if (cmdidx == CMD_let)
2750 {
2751 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002752 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753 {
2754 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002755 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756 {
2757 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002758 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002759 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002760 break;
2761 }
2762 return;
2763 }
2764 }
2765 else
2766 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2767 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 while ((xp->xp_pattern = vim_strpbrk(arg,
2769 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2770 {
2771 c = *xp->xp_pattern;
2772 if (c == '&')
2773 {
2774 c = xp->xp_pattern[1];
2775 if (c == '&')
2776 {
2777 ++xp->xp_pattern;
2778 xp->xp_context = cmdidx != CMD_let || got_eq
2779 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2780 }
2781 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002784 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2785 xp->xp_pattern += 2;
2786
2787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 else if (c == '$')
2790 {
2791 /* environment variable */
2792 xp->xp_context = EXPAND_ENV_VARS;
2793 }
2794 else if (c == '=')
2795 {
2796 got_eq = TRUE;
2797 xp->xp_context = EXPAND_EXPRESSION;
2798 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002799 else if (c == '#'
2800 && xp->xp_context == EXPAND_EXPRESSION)
2801 {
2802 /* Autoload function/variable contains '#'. */
2803 break;
2804 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002805 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 && xp->xp_context == EXPAND_FUNCTIONS
2807 && vim_strchr(xp->xp_pattern, '(') == NULL)
2808 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002809 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 break;
2811 }
2812 else if (cmdidx != CMD_let || got_eq)
2813 {
2814 if (c == '"') /* string */
2815 {
2816 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2817 if (c == '\\' && xp->xp_pattern[1] != NUL)
2818 ++xp->xp_pattern;
2819 xp->xp_context = EXPAND_NOTHING;
2820 }
2821 else if (c == '\'') /* literal string */
2822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2825 /* skip */ ;
2826 xp->xp_context = EXPAND_NOTHING;
2827 }
2828 else if (c == '|')
2829 {
2830 if (xp->xp_pattern[1] == '|')
2831 {
2832 ++xp->xp_pattern;
2833 xp->xp_context = EXPAND_EXPRESSION;
2834 }
2835 else
2836 xp->xp_context = EXPAND_COMMANDS;
2837 }
2838 else
2839 xp->xp_context = EXPAND_EXPRESSION;
2840 }
2841 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002842 /* Doesn't look like something valid, expand as an expression
2843 * anyway. */
2844 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 arg = xp->xp_pattern;
2846 if (*arg != NUL)
2847 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2848 /* skip */ ;
2849 }
2850 xp->xp_pattern = arg;
2851}
2852
2853#endif /* FEAT_CMDL_COMPL */
2854
2855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 * ":unlet[!] var1 ... " command.
2857 */
2858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002859ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002861 ex_unletlock(eap, eap->arg, 0);
2862}
2863
2864/*
2865 * ":lockvar" and ":unlockvar" commands
2866 */
2867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002871 int deep = 2;
2872
2873 if (eap->forceit)
2874 deep = -1;
2875 else if (vim_isdigit(*arg))
2876 {
2877 deep = getdigits(&arg);
2878 arg = skipwhite(arg);
2879 }
2880
2881 ex_unletlock(eap, arg, deep);
2882}
2883
2884/*
2885 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2886 */
2887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002888ex_unletlock(
2889 exarg_T *eap,
2890 char_u *argstart,
2891 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892{
2893 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 do
2899 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002900 if (*arg == '$')
2901 {
2902 char_u *name = ++arg;
2903
2904 if (get_env_len(&arg) == 0)
2905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002907 return;
2908 }
2909 vim_unsetenv(name);
2910 arg = skipwhite(arg);
2911 continue;
2912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002915 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002916 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 if (lv.ll_name == NULL)
2918 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002919 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 if (name_end != NULL)
2923 {
2924 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002925 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 }
2927 if (!(eap->skip || error))
2928 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 break;
2930 }
2931
2932 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 {
2934 if (eap->cmdidx == CMD_unlet)
2935 {
2936 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2937 error = TRUE;
2938 }
2939 else
2940 {
2941 if (do_lock_var(&lv, name_end, deep,
2942 eap->cmdidx == CMD_lockvar) == FAIL)
2943 error = TRUE;
2944 }
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 if (!eap->skip)
2948 clear_lval(&lv);
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 arg = skipwhite(name_end);
2951 } while (!ends_excmd(*arg));
2952
2953 eap->nextcmd = check_nextcmd(arg);
2954}
2955
Bram Moolenaar8c711452005-01-14 21:53:12 +00002956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957do_unlet_var(
2958 lval_T *lp,
2959 char_u *name_end,
2960 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002961{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 int ret = OK;
2963 int cc;
2964
2965 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 cc = *name_end;
2968 *name_end = NUL;
2969
2970 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002971 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002974 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002975 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002976 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002977 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002978 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 else if (lp->ll_range)
2981 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002982 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002983 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002984 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985
2986 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2987 {
2988 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002989 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002990 return FAIL;
2991 ll_li = li;
2992 ++ll_n1;
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994
2995 /* Delete a range of List items. */
2996 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2997 {
2998 li = lp->ll_li->li_next;
2999 listitem_remove(lp->ll_list, lp->ll_li);
3000 lp->ll_li = li;
3001 ++lp->ll_n1;
3002 }
3003 }
3004 else
3005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /* unlet a List item. */
3008 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003011 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 }
3013
3014 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003015}
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017/*
3018 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
3021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003022do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 hashtab_T *ht;
3025 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003026 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003027 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003028 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
Bram Moolenaar33570922005-01-25 22:26:29 +00003030 ht = find_var_ht(name, &varname);
3031 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003034 if (d == NULL)
3035 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 if (ht == &globvarht)
3037 d = &globvardict;
3038 else if (ht == &compat_hashtab)
3039 d = &vimvardict;
3040 else
3041 {
3042 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3043 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3044 }
3045 if (d == NULL)
3046 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003047 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048 return FAIL;
3049 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003052 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003053 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003054 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003055 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003056 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003057 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003058 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003059 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003060 return FAIL;
3061
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003062 delete_var(ht, hi);
3063 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003066 if (forceit)
3067 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003068 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return FAIL;
3070}
3071
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003072/*
3073 * Lock or unlock variable indicated by "lp".
3074 * "deep" is the levels to go (-1 for unlimited);
3075 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3076 */
3077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078do_lock_var(
3079 lval_T *lp,
3080 char_u *name_end,
3081 int deep,
3082 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003083{
3084 int ret = OK;
3085 int cc;
3086 dictitem_T *di;
3087
3088 if (deep == 0) /* nothing to do */
3089 return OK;
3090
3091 if (lp->ll_tv == NULL)
3092 {
3093 cc = *name_end;
3094 *name_end = NUL;
3095
3096 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003097 di = find_var(lp->ll_name, NULL, TRUE);
3098 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003099 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003100 else if ((di->di_flags & DI_FLAGS_FIX)
3101 && di->di_tv.v_type != VAR_DICT
3102 && di->di_tv.v_type != VAR_LIST)
3103 /* For historic reasons this error is not given for a list or dict.
3104 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003105 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 else
3107 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003108 if (lock)
3109 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003110 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003111 di->di_flags &= ~DI_FLAGS_LOCK;
3112 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003113 }
3114 *name_end = cc;
3115 }
3116 else if (lp->ll_range)
3117 {
3118 listitem_T *li = lp->ll_li;
3119
3120 /* (un)lock a range of List items. */
3121 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3122 {
3123 item_lock(&li->li_tv, deep, lock);
3124 li = li->li_next;
3125 ++lp->ll_n1;
3126 }
3127 }
3128 else if (lp->ll_list != NULL)
3129 /* (un)lock a List item. */
3130 item_lock(&lp->ll_li->li_tv, deep, lock);
3131 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003132 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003133 item_lock(&lp->ll_di->di_tv, deep, lock);
3134
3135 return ret;
3136}
3137
3138/*
3139 * Lock or unlock an item. "deep" is nr of levels to go.
3140 */
3141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003142item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003143{
3144 static int recurse = 0;
3145 list_T *l;
3146 listitem_T *li;
3147 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003148 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003149 hashitem_T *hi;
3150 int todo;
3151
3152 if (recurse >= DICT_MAXNEST)
3153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003154 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003155 return;
3156 }
3157 if (deep == 0)
3158 return;
3159 ++recurse;
3160
3161 /* lock/unlock the item itself */
3162 if (lock)
3163 tv->v_lock |= VAR_LOCKED;
3164 else
3165 tv->v_lock &= ~VAR_LOCKED;
3166
3167 switch (tv->v_type)
3168 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003169 case VAR_UNKNOWN:
3170 case VAR_NUMBER:
3171 case VAR_STRING:
3172 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003173 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003174 case VAR_FLOAT:
3175 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003176 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003177 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003178 break;
3179
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003180 case VAR_BLOB:
3181 if ((b = tv->vval.v_blob) != NULL)
3182 {
3183 if (lock)
3184 b->bv_lock |= VAR_LOCKED;
3185 else
3186 b->bv_lock &= ~VAR_LOCKED;
3187 }
3188 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189 case VAR_LIST:
3190 if ((l = tv->vval.v_list) != NULL)
3191 {
3192 if (lock)
3193 l->lv_lock |= VAR_LOCKED;
3194 else
3195 l->lv_lock &= ~VAR_LOCKED;
3196 if (deep < 0 || deep > 1)
3197 /* recursive: lock/unlock the items the List contains */
3198 for (li = l->lv_first; li != NULL; li = li->li_next)
3199 item_lock(&li->li_tv, deep - 1, lock);
3200 }
3201 break;
3202 case VAR_DICT:
3203 if ((d = tv->vval.v_dict) != NULL)
3204 {
3205 if (lock)
3206 d->dv_lock |= VAR_LOCKED;
3207 else
3208 d->dv_lock &= ~VAR_LOCKED;
3209 if (deep < 0 || deep > 1)
3210 {
3211 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003212 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003213 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3214 {
3215 if (!HASHITEM_EMPTY(hi))
3216 {
3217 --todo;
3218 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3219 }
3220 }
3221 }
3222 }
3223 }
3224 --recurse;
3225}
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3228/*
3229 * Delete all "menutrans_" variables.
3230 */
3231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003235 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003238 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003240 {
3241 if (!HASHITEM_EMPTY(hi))
3242 {
3243 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3245 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003246 }
3247 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249}
3250#endif
3251
3252#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3253
3254/*
3255 * Local string buffer for the next two functions to store a variable name
3256 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3257 * get_user_var_name().
3258 */
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260static char_u *varnamebuf = NULL;
3261static int varnamebuflen = 0;
3262
3263/*
3264 * Function to concatenate a prefix and a variable name.
3265 */
3266 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 int len;
3270
3271 len = (int)STRLEN(name) + 3;
3272 if (len > varnamebuflen)
3273 {
3274 vim_free(varnamebuf);
3275 len += 10; /* some additional space */
3276 varnamebuf = alloc(len);
3277 if (varnamebuf == NULL)
3278 {
3279 varnamebuflen = 0;
3280 return NULL;
3281 }
3282 varnamebuflen = len;
3283 }
3284 *varnamebuf = prefix;
3285 varnamebuf[1] = ':';
3286 STRCPY(varnamebuf + 2, name);
3287 return varnamebuf;
3288}
3289
3290/*
3291 * Function given to ExpandGeneric() to obtain the list of user defined
3292 * (global/buffer/window/built-in) variable names.
3293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003297 static long_u gdone;
3298 static long_u bdone;
3299 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003300 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003301 static int vidx;
3302 static hashitem_T *hi;
3303 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003306 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003307 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003309 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003310
3311 /* Global variables */
3312 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003314 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003316 else
3317 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003318 while (HASHITEM_EMPTY(hi))
3319 ++hi;
3320 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3321 return cat_prefix_varname('g', hi->hi_key);
3322 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003324
3325 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003326 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003329 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003331 else
3332 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003333 while (HASHITEM_EMPTY(hi))
3334 ++hi;
3335 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003337
3338 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003339 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003340 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003342 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003344 else
3345 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003346 while (HASHITEM_EMPTY(hi))
3347 ++hi;
3348 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003350
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003351 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003352 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003353 if (tdone < ht->ht_used)
3354 {
3355 if (tdone++ == 0)
3356 hi = ht->ht_array;
3357 else
3358 ++hi;
3359 while (HASHITEM_EMPTY(hi))
3360 ++hi;
3361 return cat_prefix_varname('t', hi->hi_key);
3362 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003363
Bram Moolenaar33570922005-01-25 22:26:29 +00003364 /* v: variables */
3365 if (vidx < VV_LEN)
3366 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
Bram Moolenaard23a8232018-02-10 18:45:26 +01003368 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 varnamebuflen = 0;
3370 return NULL;
3371}
3372
3373#endif /* FEAT_CMDL_COMPL */
3374
3375/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003376 * Return TRUE if "pat" matches "text".
3377 * Does not use 'cpo' and always uses 'magic'.
3378 */
3379 static int
3380pattern_match(char_u *pat, char_u *text, int ic)
3381{
3382 int matches = FALSE;
3383 char_u *save_cpo;
3384 regmatch_T regmatch;
3385
3386 /* avoid 'l' flag in 'cpoptions' */
3387 save_cpo = p_cpo;
3388 p_cpo = (char_u *)"";
3389 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3390 if (regmatch.regprog != NULL)
3391 {
3392 regmatch.rm_ic = ic;
3393 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3394 vim_regfree(regmatch.regprog);
3395 }
3396 p_cpo = save_cpo;
3397 return matches;
3398}
3399
3400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3404 */
3405
3406/*
3407 * Handle zero level expression.
3408 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003410 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * Return OK or FAIL.
3412 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414eval0(
3415 char_u *arg,
3416 typval_T *rettv,
3417 char_u **nextcmd,
3418 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 int ret;
3421 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003422 int did_emsg_before = did_emsg;
3423 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (ret == FAIL || !ends_excmd(*p))
3428 {
3429 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /*
3432 * Report the invalid expression unless the expression evaluation has
3433 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003434 * exception, or we already gave a more specific error.
3435 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003437 if (!aborting() && did_emsg == did_emsg_before
3438 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003439 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 ret = FAIL;
3441 }
3442 if (nextcmd != NULL)
3443 *nextcmd = check_nextcmd(p);
3444
3445 return ret;
3446}
3447
3448/*
3449 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003450 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 *
3452 * "arg" must point to the first non-white of the expression.
3453 * "arg" is advanced to the next non-white after the recognized expression.
3454 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003455 * Note: "rettv.v_lock" is not set.
3456 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * Return OK or FAIL.
3458 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003460eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
3466 * Get the first variable.
3467 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003468 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 return FAIL;
3470
3471 if ((*arg)[0] == '?')
3472 {
3473 result = FALSE;
3474 if (evaluate)
3475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003476 int error = FALSE;
3477
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003478 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003481 if (error)
3482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484
3485 /*
3486 * Get the second variable.
3487 */
3488 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003489 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 return FAIL;
3491
3492 /*
3493 * Check for the ":".
3494 */
3495 if ((*arg)[0] != ':')
3496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003497 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 return FAIL;
3501 }
3502
3503 /*
3504 * Get the third variable.
3505 */
3506 *arg = skipwhite(*arg + 1);
3507 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3508 {
3509 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003510 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return FAIL;
3512 }
3513 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003514 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516
3517 return OK;
3518}
3519
3520/*
3521 * Handle first level expression:
3522 * expr2 || expr2 || expr2 logical OR
3523 *
3524 * "arg" must point to the first non-white of the expression.
3525 * "arg" is advanced to the next non-white after the recognized expression.
3526 *
3527 * Return OK or FAIL.
3528 */
3529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003530eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 long result;
3534 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003535 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 /*
3538 * Get the first variable.
3539 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 return FAIL;
3542
3543 /*
3544 * Repeat until there is no following "||".
3545 */
3546 first = TRUE;
3547 result = FALSE;
3548 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3549 {
3550 if (evaluate && first)
3551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003552 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003555 if (error)
3556 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 first = FALSE;
3558 }
3559
3560 /*
3561 * Get the second variable.
3562 */
3563 *arg = skipwhite(*arg + 2);
3564 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3565 return FAIL;
3566
3567 /*
3568 * Compute the result.
3569 */
3570 if (evaluate && !result)
3571 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003572 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575 if (error)
3576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 if (evaluate)
3579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 rettv->v_type = VAR_NUMBER;
3581 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583 }
3584
3585 return OK;
3586}
3587
3588/*
3589 * Handle second level expression:
3590 * expr3 && expr3 && expr3 logical AND
3591 *
3592 * "arg" must point to the first non-white of the expression.
3593 * "arg" is advanced to the next non-white after the recognized expression.
3594 *
3595 * Return OK or FAIL.
3596 */
3597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 long result;
3602 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /*
3606 * Get the first variable.
3607 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 return FAIL;
3610
3611 /*
3612 * Repeat until there is no following "&&".
3613 */
3614 first = TRUE;
3615 result = TRUE;
3616 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3617 {
3618 if (evaluate && first)
3619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003620 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003622 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003623 if (error)
3624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 first = FALSE;
3626 }
3627
3628 /*
3629 * Get the second variable.
3630 */
3631 *arg = skipwhite(*arg + 2);
3632 if (eval4(arg, &var2, evaluate && result) == FAIL)
3633 return FAIL;
3634
3635 /*
3636 * Compute the result.
3637 */
3638 if (evaluate && result)
3639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003640 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003643 if (error)
3644 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 if (evaluate)
3647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003648 rettv->v_type = VAR_NUMBER;
3649 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 }
3652
3653 return OK;
3654}
3655
3656/*
3657 * Handle third level expression:
3658 * var1 == var2
3659 * var1 =~ var2
3660 * var1 != var2
3661 * var1 !~ var2
3662 * var1 > var2
3663 * var1 >= var2
3664 * var1 < var2
3665 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003666 * var1 is var2
3667 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 *
3669 * "arg" must point to the first non-white of the expression.
3670 * "arg" is advanced to the next non-white after the recognized expression.
3671 *
3672 * Return OK or FAIL.
3673 */
3674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003675eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
Bram Moolenaar33570922005-01-25 22:26:29 +00003677 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 char_u *p;
3679 int i;
3680 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003681 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 /*
3686 * Get the first variable.
3687 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 return FAIL;
3690
3691 p = *arg;
3692 switch (p[0])
3693 {
3694 case '=': if (p[1] == '=')
3695 type = TYPE_EQUAL;
3696 else if (p[1] == '~')
3697 type = TYPE_MATCH;
3698 break;
3699 case '!': if (p[1] == '=')
3700 type = TYPE_NEQUAL;
3701 else if (p[1] == '~')
3702 type = TYPE_NOMATCH;
3703 break;
3704 case '>': if (p[1] != '=')
3705 {
3706 type = TYPE_GREATER;
3707 len = 1;
3708 }
3709 else
3710 type = TYPE_GEQUAL;
3711 break;
3712 case '<': if (p[1] != '=')
3713 {
3714 type = TYPE_SMALLER;
3715 len = 1;
3716 }
3717 else
3718 type = TYPE_SEQUAL;
3719 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003720 case 'i': if (p[1] == 's')
3721 {
3722 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3723 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003724 i = p[len];
3725 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003726 {
3727 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3728 type_is = TRUE;
3729 }
3730 }
3731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733
3734 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
3737 if (type != TYPE_UNKNOWN)
3738 {
3739 /* extra question mark appended: ignore case */
3740 if (p[len] == '?')
3741 {
3742 ic = TRUE;
3743 ++len;
3744 }
3745 /* extra '#' appended: match case */
3746 else if (p[len] == '#')
3747 {
3748 ic = FALSE;
3749 ++len;
3750 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003751 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 else
3753 ic = p_ic;
3754
3755 /*
3756 * Get the second variable.
3757 */
3758 *arg = skipwhite(p + len);
3759 if (eval5(arg, &var2, evaluate) == FAIL)
3760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003761 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return FAIL;
3763 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003764 if (evaluate)
3765 {
3766 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3767
3768 clear_tv(&var2);
3769 return ret;
3770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772
3773 return OK;
3774}
3775
3776/*
3777 * Handle fourth level expression:
3778 * + number addition
3779 * - number subtraction
3780 * . string concatenation
3781 *
3782 * "arg" must point to the first non-white of the expression.
3783 * "arg" is advanced to the next non-white after the recognized expression.
3784 *
3785 * Return OK or FAIL.
3786 */
3787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003788eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789{
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 typval_T var2;
3791 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003793 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003794#ifdef FEAT_FLOAT
3795 float_T f1 = 0, f2 = 0;
3796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 char_u *s1, *s2;
3798 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3799 char_u *p;
3800
3801 /*
3802 * Get the first variable.
3803 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003804 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 return FAIL;
3806
3807 /*
3808 * Repeat computing, until no '+', '-' or '.' is following.
3809 */
3810 for (;;)
3811 {
3812 op = **arg;
3813 if (op != '+' && op != '-' && op != '.')
3814 break;
3815
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003816 if ((op != '+' || (rettv->v_type != VAR_LIST
3817 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003818#ifdef FEAT_FLOAT
3819 && (op == '.' || rettv->v_type != VAR_FLOAT)
3820#endif
3821 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003822 {
3823 /* For "list + ...", an illegal use of the first operand as
3824 * a number cannot be determined before evaluating the 2nd
3825 * operand: if this is also a list, all is ok.
3826 * For "something . ...", "something - ..." or "non-list + ...",
3827 * we know that the first operand needs to be a string or number
3828 * without evaluating the 2nd operand. So check before to avoid
3829 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003830 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003831 {
3832 clear_tv(rettv);
3833 return FAIL;
3834 }
3835 }
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 /*
3838 * Get the second variable.
3839 */
3840 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003841 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003843 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 return FAIL;
3845 }
3846
3847 if (evaluate)
3848 {
3849 /*
3850 * Compute the result.
3851 */
3852 if (op == '.')
3853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003854 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3855 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 if (s2 == NULL) /* type error ? */
3857 {
3858 clear_tv(rettv);
3859 clear_tv(&var2);
3860 return FAIL;
3861 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003862 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003863 clear_tv(rettv);
3864 rettv->v_type = VAR_STRING;
3865 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003867 else if (op == '+' && rettv->v_type == VAR_BLOB
3868 && var2.v_type == VAR_BLOB)
3869 {
3870 blob_T *b1 = rettv->vval.v_blob;
3871 blob_T *b2 = var2.vval.v_blob;
3872 blob_T *b = blob_alloc();
3873 int i;
3874
3875 if (b != NULL)
3876 {
3877 for (i = 0; i < blob_len(b1); i++)
3878 ga_append(&b->bv_ga, blob_get(b1, i));
3879 for (i = 0; i < blob_len(b2); i++)
3880 ga_append(&b->bv_ga, blob_get(b2, i));
3881
3882 clear_tv(rettv);
3883 rettv_blob_set(rettv, b);
3884 }
3885 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003886 else if (op == '+' && rettv->v_type == VAR_LIST
3887 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003888 {
3889 /* concatenate Lists */
3890 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3891 &var3) == FAIL)
3892 {
3893 clear_tv(rettv);
3894 clear_tv(&var2);
3895 return FAIL;
3896 }
3897 clear_tv(rettv);
3898 *rettv = var3;
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
3901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 int error = FALSE;
3903
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003904#ifdef FEAT_FLOAT
3905 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003907 f1 = rettv->vval.v_float;
3908 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003909 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003910 else
3911#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003913 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003914 if (error)
3915 {
3916 /* This can only happen for "list + non-list". For
3917 * "non-list + ..." or "something - ...", we returned
3918 * before evaluating the 2nd operand. */
3919 clear_tv(rettv);
3920 return FAIL;
3921 }
3922#ifdef FEAT_FLOAT
3923 if (var2.v_type == VAR_FLOAT)
3924 f1 = n1;
3925#endif
3926 }
3927#ifdef FEAT_FLOAT
3928 if (var2.v_type == VAR_FLOAT)
3929 {
3930 f2 = var2.vval.v_float;
3931 n2 = 0;
3932 }
3933 else
3934#endif
3935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003936 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003937 if (error)
3938 {
3939 clear_tv(rettv);
3940 clear_tv(&var2);
3941 return FAIL;
3942 }
3943#ifdef FEAT_FLOAT
3944 if (rettv->v_type == VAR_FLOAT)
3945 f2 = n2;
3946#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003949
3950#ifdef FEAT_FLOAT
3951 /* If there is a float on either side the result is a float. */
3952 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3953 {
3954 if (op == '+')
3955 f1 = f1 + f2;
3956 else
3957 f1 = f1 - f2;
3958 rettv->v_type = VAR_FLOAT;
3959 rettv->vval.v_float = f1;
3960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003962#endif
3963 {
3964 if (op == '+')
3965 n1 = n1 + n2;
3966 else
3967 n1 = n1 - n2;
3968 rettv->v_type = VAR_NUMBER;
3969 rettv->vval.v_number = n1;
3970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 }
3975 return OK;
3976}
3977
3978/*
3979 * Handle fifth level expression:
3980 * * number multiplication
3981 * / number division
3982 * % number modulo
3983 *
3984 * "arg" must point to the first non-white of the expression.
3985 * "arg" is advanced to the next non-white after the recognized expression.
3986 *
3987 * Return OK or FAIL.
3988 */
3989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003990eval6(
3991 char_u **arg,
3992 typval_T *rettv,
3993 int evaluate,
3994 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003998 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003999#ifdef FEAT_FLOAT
4000 int use_float = FALSE;
4001 float_T f1 = 0, f2;
4002#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004003 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005 /*
4006 * Get the first variable.
4007 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004008 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 return FAIL;
4010
4011 /*
4012 * Repeat computing, until no '*', '/' or '%' is following.
4013 */
4014 for (;;)
4015 {
4016 op = **arg;
4017 if (op != '*' && op != '/' && op != '%')
4018 break;
4019
4020 if (evaluate)
4021 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004022#ifdef FEAT_FLOAT
4023 if (rettv->v_type == VAR_FLOAT)
4024 {
4025 f1 = rettv->vval.v_float;
4026 use_float = TRUE;
4027 n1 = 0;
4028 }
4029 else
4030#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004031 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004033 if (error)
4034 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
4036 else
4037 n1 = 0;
4038
4039 /*
4040 * Get the second variable.
4041 */
4042 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004043 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045
4046 if (evaluate)
4047 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004048#ifdef FEAT_FLOAT
4049 if (var2.v_type == VAR_FLOAT)
4050 {
4051 if (!use_float)
4052 {
4053 f1 = n1;
4054 use_float = TRUE;
4055 }
4056 f2 = var2.vval.v_float;
4057 n2 = 0;
4058 }
4059 else
4060#endif
4061 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004062 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004063 clear_tv(&var2);
4064 if (error)
4065 return FAIL;
4066#ifdef FEAT_FLOAT
4067 if (use_float)
4068 f2 = n2;
4069#endif
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /*
4073 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076#ifdef FEAT_FLOAT
4077 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004079 if (op == '*')
4080 f1 = f1 * f2;
4081 else if (op == '/')
4082 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004083# ifdef VMS
4084 /* VMS crashes on divide by zero, work around it */
4085 if (f2 == 0.0)
4086 {
4087 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004088 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004089 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004090 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004091 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004092 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004093 }
4094 else
4095 f1 = f1 / f2;
4096# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004097 /* We rely on the floating point library to handle divide
4098 * by zero to result in "inf" and not a crash. */
4099 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004100# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004104 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004105 return FAIL;
4106 }
4107 rettv->v_type = VAR_FLOAT;
4108 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004113 if (op == '*')
4114 n1 = n1 * n2;
4115 else if (op == '/')
4116 {
4117 if (n2 == 0) /* give an error message? */
4118 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004119 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004120 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004121 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004122 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004123 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004124 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004125 }
4126 else
4127 n1 = n1 / n2;
4128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004130 {
4131 if (n2 == 0) /* give an error message? */
4132 n1 = 0;
4133 else
4134 n1 = n1 % n2;
4135 }
4136 rettv->v_type = VAR_NUMBER;
4137 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 }
4141
4142 return OK;
4143}
4144
4145/*
4146 * Handle sixth level expression:
4147 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004148 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004149 * "string" string constant
4150 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 * &option-name option value
4152 * @r register contents
4153 * identifier variable value
4154 * function() function call
4155 * $VAR environment variable
4156 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004157 * [expr, expr] List
4158 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * Also handle:
4161 * ! in front logical NOT
4162 * - in front unary minus
4163 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004164 * trailing [] subscript in String or List
4165 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 *
4167 * "arg" must point to the first non-white of the expression.
4168 * "arg" is advanced to the next non-white after the recognized expression.
4169 *
4170 * Return OK or FAIL.
4171 */
4172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004173eval7(
4174 char_u **arg,
4175 typval_T *rettv,
4176 int evaluate,
4177 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004179 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 int len;
4181 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 char_u *start_leader, *end_leader;
4183 int ret = OK;
4184 char_u *alias;
4185
4186 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004188 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004193 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 */
4195 start_leader = *arg;
4196 while (**arg == '!' || **arg == '-' || **arg == '+')
4197 *arg = skipwhite(*arg + 1);
4198 end_leader = *arg;
4199
4200 switch (**arg)
4201 {
4202 /*
4203 * Number constant.
4204 */
4205 case '0':
4206 case '1':
4207 case '2':
4208 case '3':
4209 case '4':
4210 case '5':
4211 case '6':
4212 case '7':
4213 case '8':
4214 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004215 {
4216#ifdef FEAT_FLOAT
4217 char_u *p = skipdigits(*arg + 1);
4218 int get_float = FALSE;
4219
4220 /* We accept a float when the format matches
4221 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004222 * strict to avoid backwards compatibility problems.
4223 * Don't look for a float after the "." operator, so that
4224 * ":let vers = 1.2.3" doesn't fail. */
4225 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004227 get_float = TRUE;
4228 p = skipdigits(p + 2);
4229 if (*p == 'e' || *p == 'E')
4230 {
4231 ++p;
4232 if (*p == '-' || *p == '+')
4233 ++p;
4234 if (!vim_isdigit(*p))
4235 get_float = FALSE;
4236 else
4237 p = skipdigits(p + 1);
4238 }
4239 if (ASCII_ISALPHA(*p) || *p == '.')
4240 get_float = FALSE;
4241 }
4242 if (get_float)
4243 {
4244 float_T f;
4245
4246 *arg += string2float(*arg, &f);
4247 if (evaluate)
4248 {
4249 rettv->v_type = VAR_FLOAT;
4250 rettv->vval.v_float = f;
4251 }
4252 }
4253 else
4254#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004255 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004256 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004257 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004258 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004259
4260 // Blob constant: 0z0123456789abcdef
4261 if (evaluate)
4262 blob = blob_alloc();
4263 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4264 {
4265 if (!vim_isxdigit(bp[1]))
4266 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004267 if (blob != NULL)
4268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004269 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004270 ga_clear(&blob->bv_ga);
4271 VIM_CLEAR(blob);
4272 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004273 ret = FAIL;
4274 break;
4275 }
4276 if (blob != NULL)
4277 ga_append(&blob->bv_ga,
4278 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004279 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4280 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004281 }
4282 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004283 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004284 *arg = bp;
4285 }
4286 else
4287 {
4288 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004289 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004290 *arg += len;
4291 if (evaluate)
4292 {
4293 rettv->v_type = VAR_NUMBER;
4294 rettv->vval.v_number = n;
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 /*
4301 * String constant: "string".
4302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 break;
4305
4306 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004310 break;
4311
4312 /*
4313 * List: [expr, expr]
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 break;
4317
4318 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004319 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004320 * Dictionary: {key: val, key: val}
4321 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004322 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4323 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004324 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 break;
4326
4327 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004328 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004330 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 break;
4332
4333 /*
4334 * Environment variable: $VAR.
4335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 break;
4338
4339 /*
4340 * Register contents: @r.
4341 */
4342 case '@': ++*arg;
4343 if (evaluate)
4344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004346 rettv->vval.v_string = get_reg_contents(**arg,
4347 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 if (**arg != NUL)
4350 ++*arg;
4351 break;
4352
4353 /*
4354 * nested expression: (expression).
4355 */
4356 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (**arg == ')')
4359 ++*arg;
4360 else if (ret == OK)
4361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004362 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 ret = FAIL;
4365 }
4366 break;
4367
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 break;
4370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004371
4372 if (ret == NOTDONE)
4373 {
4374 /*
4375 * Must be a variable or function name.
4376 * Can also be a curly-braces kind of name: {expr}.
4377 */
4378 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004379 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 if (alias != NULL)
4381 s = alias;
4382
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004383 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384 ret = FAIL;
4385 else
4386 {
4387 if (**arg == '(') /* recursive! */
4388 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004389 partial_T *partial;
4390
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004391 if (!evaluate)
4392 check_vars(s, len);
4393
Bram Moolenaar8c711452005-01-14 21:53:12 +00004394 /* If "s" is the name of a variable of type VAR_FUNC
4395 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004396 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004397
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004398 /* Need to make a copy, in case evaluating the arguments makes
4399 * the name invalid. */
4400 s = vim_strsave(s);
4401 if (s == NULL)
4402 ret = FAIL;
4403 else
4404 /* Invoke the function. */
4405 ret = get_func_tv(s, len, rettv, arg,
4406 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4407 &len, evaluate, partial, NULL);
4408 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004409
4410 /* If evaluate is FALSE rettv->v_type was not set in
4411 * get_func_tv, but it's needed in handle_subscript() to parse
4412 * what follows. So set it here. */
4413 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4414 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004415 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004416 rettv->v_type = VAR_FUNC;
4417 }
4418
Bram Moolenaar8c711452005-01-14 21:53:12 +00004419 /* Stop the expression evaluation when immediately
4420 * aborting on error, or when an interrupt occurred or
4421 * an exception was thrown but not caught. */
4422 if (aborting())
4423 {
4424 if (ret == OK)
4425 clear_tv(rettv);
4426 ret = FAIL;
4427 }
4428 }
4429 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004430 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004431 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004432 {
4433 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004434 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004435 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004437 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 }
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 *arg = skipwhite(*arg);
4441
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004442 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4443 * expr(expr). */
4444 if (ret == OK)
4445 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 /*
4448 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4449 */
4450 if (ret == OK && evaluate && end_leader > start_leader)
4451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004453 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454#ifdef FEAT_FLOAT
4455 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004456
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 if (rettv->v_type == VAR_FLOAT)
4458 f = rettv->vval.v_float;
4459 else
4460#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004461 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004462 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004464 clear_tv(rettv);
4465 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004467 else
4468 {
4469 while (end_leader > start_leader)
4470 {
4471 --end_leader;
4472 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 {
4474#ifdef FEAT_FLOAT
4475 if (rettv->v_type == VAR_FLOAT)
4476 f = !f;
4477 else
4478#endif
4479 val = !val;
4480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482 {
4483#ifdef FEAT_FLOAT
4484 if (rettv->v_type == VAR_FLOAT)
4485 f = -f;
4486 else
4487#endif
4488 val = -val;
4489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004490 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004491#ifdef FEAT_FLOAT
4492 if (rettv->v_type == VAR_FLOAT)
4493 {
4494 clear_tv(rettv);
4495 rettv->vval.v_float = f;
4496 }
4497 else
4498#endif
4499 {
4500 clear_tv(rettv);
4501 rettv->v_type = VAR_NUMBER;
4502 rettv->vval.v_number = val;
4503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506
4507 return ret;
4508}
4509
4510/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004511 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4512 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4514 */
4515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004516eval_index(
4517 char_u **arg,
4518 typval_T *rettv,
4519 int evaluate,
4520 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521{
4522 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004523 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004524 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004525 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004526 long len = -1;
4527 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004529 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530
Bram Moolenaara03f2332016-02-06 18:09:59 +01004531 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004533 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004534 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004535 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004536 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004537 return FAIL;
4538 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004539#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004540 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004541 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004542 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004543#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004544 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004545 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004546 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004547 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004548 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004549 return FAIL;
4550 case VAR_UNKNOWN:
4551 if (evaluate)
4552 return FAIL;
4553 /* FALLTHROUGH */
4554
4555 case VAR_STRING:
4556 case VAR_NUMBER:
4557 case VAR_LIST:
4558 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004559 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004560 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004563 init_tv(&var1);
4564 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 /*
4568 * dict.name
4569 */
4570 key = *arg + 1;
4571 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4572 ;
4573 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 }
4577 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004579 /*
4580 * something[idx]
4581 *
4582 * Get the (first) variable from inside the [].
4583 */
4584 *arg = skipwhite(*arg + 1);
4585 if (**arg == ':')
4586 empty1 = TRUE;
4587 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4588 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004589 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 {
4591 /* not a number or string */
4592 clear_tv(&var1);
4593 return FAIL;
4594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595
4596 /*
4597 * Get the second variable from inside the [:].
4598 */
4599 if (**arg == ':')
4600 {
4601 range = TRUE;
4602 *arg = skipwhite(*arg + 1);
4603 if (**arg == ']')
4604 empty2 = TRUE;
4605 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004607 if (!empty1)
4608 clear_tv(&var1);
4609 return FAIL;
4610 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004611 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004612 {
4613 /* not a number or string */
4614 if (!empty1)
4615 clear_tv(&var1);
4616 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 return FAIL;
4618 }
4619 }
4620
4621 /* Check for the ']'. */
4622 if (**arg != ']')
4623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004624 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004625 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 clear_tv(&var1);
4627 if (range)
4628 clear_tv(&var2);
4629 return FAIL;
4630 }
4631 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 }
4633
4634 if (evaluate)
4635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 n1 = 0;
4637 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004639 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 }
4642 if (range)
4643 {
4644 if (empty2)
4645 n2 = -1;
4646 else
4647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004648 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004649 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 }
4651 }
4652
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004655 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004656 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004657 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004658 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004659 case VAR_SPECIAL:
4660 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004661 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662 break; /* not evaluating, skipping over subscript */
4663
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664 case VAR_NUMBER:
4665 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004666 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 len = (long)STRLEN(s);
4668 if (range)
4669 {
4670 /* The resulting variable is a substring. If the indexes
4671 * are out of range the result is empty. */
4672 if (n1 < 0)
4673 {
4674 n1 = len + n1;
4675 if (n1 < 0)
4676 n1 = 0;
4677 }
4678 if (n2 < 0)
4679 n2 = len + n2;
4680 else if (n2 >= len)
4681 n2 = len;
4682 if (n1 >= len || n2 < 0 || n1 > n2)
4683 s = NULL;
4684 else
4685 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4686 }
4687 else
4688 {
4689 /* The resulting variable is a string of a single
4690 * character. If the index is too big or negative the
4691 * result is empty. */
4692 if (n1 >= len || n1 < 0)
4693 s = NULL;
4694 else
4695 s = vim_strnsave(s + n1, 1);
4696 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697 clear_tv(rettv);
4698 rettv->v_type = VAR_STRING;
4699 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 break;
4701
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004702 case VAR_BLOB:
4703 len = blob_len(rettv->vval.v_blob);
4704 if (range)
4705 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004706 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004707 // are out of range the result is empty.
4708 if (n1 < 0)
4709 {
4710 n1 = len + n1;
4711 if (n1 < 0)
4712 n1 = 0;
4713 }
4714 if (n2 < 0)
4715 n2 = len + n2;
4716 else if (n2 >= len)
4717 n2 = len - 1;
4718 if (n1 >= len || n2 < 0 || n1 > n2)
4719 {
4720 clear_tv(rettv);
4721 rettv->v_type = VAR_BLOB;
4722 rettv->vval.v_blob = NULL;
4723 }
4724 else
4725 {
4726 blob_T *blob = blob_alloc();
4727
4728 if (blob != NULL)
4729 {
4730 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4731 {
4732 blob_free(blob);
4733 return FAIL;
4734 }
4735 blob->bv_ga.ga_len = n2 - n1 + 1;
4736 for (i = n1; i <= n2; i++)
4737 blob_set(blob, i - n1,
4738 blob_get(rettv->vval.v_blob, i));
4739
4740 clear_tv(rettv);
4741 rettv_blob_set(rettv, blob);
4742 }
4743 }
4744 }
4745 else
4746 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004747 // The resulting variable is a byte value.
4748 // If the index is too big or negative that is an error.
4749 if (n1 < 0)
4750 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004751 if (n1 < len && n1 >= 0)
4752 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004753 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004754
4755 clear_tv(rettv);
4756 rettv->v_type = VAR_NUMBER;
4757 rettv->vval.v_number = v;
4758 }
4759 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004760 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004761 }
4762 break;
4763
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 if (n1 < 0)
4767 n1 = len + n1;
4768 if (!empty1 && (n1 < 0 || n1 >= len))
4769 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004770 /* For a range we allow invalid values and return an empty
4771 * list. A list index out of range is an error. */
4772 if (!range)
4773 {
4774 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004775 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004776 return FAIL;
4777 }
4778 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 }
4780 if (range)
4781 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004782 list_T *l;
4783 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784
4785 if (n2 < 0)
4786 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004787 else if (n2 >= len)
4788 n2 = len - 1;
4789 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004790 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 l = list_alloc();
4792 if (l == NULL)
4793 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 n1 <= n2; ++n1)
4796 {
4797 if (list_append_tv(l, &item->li_tv) == FAIL)
4798 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 return FAIL;
4801 }
4802 item = item->li_next;
4803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004805 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 }
4807 else
4808 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004809 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004810 clear_tv(rettv);
4811 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 }
4813 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814
4815 case VAR_DICT:
4816 if (range)
4817 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004818 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 if (len == -1)
4821 clear_tv(&var1);
4822 return FAIL;
4823 }
4824 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004825 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826
4827 if (len == -1)
4828 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004829 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004830 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832 clear_tv(&var1);
4833 return FAIL;
4834 }
4835 }
4836
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004837 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004839 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004840 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 if (len == -1)
4842 clear_tv(&var1);
4843 if (item == NULL)
4844 return FAIL;
4845
4846 copy_tv(&item->di_tv, &var1);
4847 clear_tv(rettv);
4848 *rettv = var1;
4849 }
4850 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 }
4852 }
4853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 return OK;
4855}
4856
4857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 * Get an option value.
4859 * "arg" points to the '&' or '+' before the option name.
4860 * "arg" is advanced to character after the option name.
4861 * Return OK or FAIL.
4862 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004864get_option_tv(
4865 char_u **arg,
4866 typval_T *rettv, /* when NULL, only check if option exists */
4867 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
4869 char_u *option_end;
4870 long numval;
4871 char_u *stringval;
4872 int opt_type;
4873 int c;
4874 int working = (**arg == '+'); /* has("+option") */
4875 int ret = OK;
4876 int opt_flags;
4877
4878 /*
4879 * Isolate the option name and find its value.
4880 */
4881 option_end = find_option_end(arg, &opt_flags);
4882 if (option_end == NULL)
4883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004885 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887 }
4888
4889 if (!evaluate)
4890 {
4891 *arg = option_end;
4892 return OK;
4893 }
4894
4895 c = *option_end;
4896 *option_end = NUL;
4897 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 if (opt_type == -3) /* invalid name */
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004903 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 ret = FAIL;
4905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 if (opt_type == -2) /* hidden string option */
4909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910 rettv->v_type = VAR_STRING;
4911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 else if (opt_type == -1) /* hidden number option */
4914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv->v_type = VAR_NUMBER;
4916 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else if (opt_type == 1) /* number option */
4919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 rettv->v_type = VAR_NUMBER;
4921 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else /* string option */
4924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 rettv->v_type = VAR_STRING;
4926 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 }
4929 else if (working && (opt_type == -2 || opt_type == -1))
4930 ret = FAIL;
4931
4932 *option_end = c; /* put back for error messages */
4933 *arg = option_end;
4934
4935 return ret;
4936}
4937
4938/*
4939 * Allocate a variable for a string constant.
4940 * Return OK or FAIL.
4941 */
4942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004943get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 char_u *p;
4946 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int extra = 0;
4948
4949 /*
4950 * Find the end of the string, skipping backslashed characters.
4951 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004952 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 if (*p == '\\' && p[1] != NUL)
4955 {
4956 ++p;
4957 /* A "\<x>" form occupies at least 4 characters, and produces up
4958 * to 6 characters: reserve space for 2 extra */
4959 if (*p == '<')
4960 extra += 2;
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963
4964 if (*p != '"')
4965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004966 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 return FAIL;
4968 }
4969
4970 /* If only parsing, set *arg and return here */
4971 if (!evaluate)
4972 {
4973 *arg = p + 1;
4974 return OK;
4975 }
4976
4977 /*
4978 * Copy the string into allocated memory, handling backslashed
4979 * characters.
4980 */
4981 name = alloc((unsigned)(p - *arg + extra));
4982 if (name == NULL)
4983 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 rettv->v_type = VAR_STRING;
4985 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 if (*p == '\\')
4990 {
4991 switch (*++p)
4992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 case 'b': *name++ = BS; ++p; break;
4994 case 'e': *name++ = ESC; ++p; break;
4995 case 'f': *name++ = FF; ++p; break;
4996 case 'n': *name++ = NL; ++p; break;
4997 case 'r': *name++ = CAR; ++p; break;
4998 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 case 'X': /* hex: "\x1", "\x12" */
5001 case 'x':
5002 case 'u': /* Unicode: "\u0023" */
5003 case 'U':
5004 if (vim_isxdigit(p[1]))
5005 {
5006 int n, nr;
5007 int c = toupper(*p);
5008
5009 if (c == 'X')
5010 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005011 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005013 else
5014 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 nr = 0;
5016 while (--n >= 0 && vim_isxdigit(p[1]))
5017 {
5018 ++p;
5019 nr = (nr << 4) + hex2nr(*p);
5020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 /* For "\u" store the number according to
5023 * 'encoding'. */
5024 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030
5031 /* octal: "\1", "\12", "\123" */
5032 case '0':
5033 case '1':
5034 case '2':
5035 case '3':
5036 case '4':
5037 case '5':
5038 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 case '7': *name = *p++ - '0';
5040 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 *name = (*name << 3) + *p++ - '0';
5043 if (*p >= '0' && *p <= '7')
5044 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 break;
5048
5049 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005050 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (extra != 0)
5052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055 }
5056 /* FALLTHROUGH */
5057
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 break;
5060 }
5061 }
5062 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005067 if (*p != NUL) /* just in case */
5068 ++p;
5069 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 return OK;
5072}
5073
5074/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 * Return OK or FAIL.
5077 */
5078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 int reduce = 0;
5084
5085 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005088 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 break;
5094 ++reduce;
5095 ++p;
5096 }
5097 }
5098
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005101 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 return FAIL;
5103 }
5104
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 if (!evaluate)
5107 {
5108 *arg = p + 1;
5109 return OK;
5110 }
5111
5112 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 */
5115 str = alloc((unsigned)((p - *arg) - reduce));
5116 if (str == NULL)
5117 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 rettv->v_type = VAR_STRING;
5119 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005124 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 break;
5127 ++p;
5128 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 *arg = p + 1;
5133
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134 return OK;
5135}
5136
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005137/*
5138 * Return the function name of the partial.
5139 */
5140 char_u *
5141partial_name(partial_T *pt)
5142{
5143 if (pt->pt_name != NULL)
5144 return pt->pt_name;
5145 return pt->pt_func->uf_name;
5146}
5147
Bram Moolenaarddecc252016-04-06 22:59:37 +02005148 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005150{
5151 int i;
5152
5153 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005154 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005155 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005156 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005157 if (pt->pt_name != NULL)
5158 {
5159 func_unref(pt->pt_name);
5160 vim_free(pt->pt_name);
5161 }
5162 else
5163 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005164 vim_free(pt);
5165}
5166
5167/*
5168 * Unreference a closure: decrement the reference count and free it when it
5169 * becomes zero.
5170 */
5171 void
5172partial_unref(partial_T *pt)
5173{
5174 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005175 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005176}
5177
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005178static int tv_equal_recurse_limit;
5179
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005180 static int
5181func_equal(
5182 typval_T *tv1,
5183 typval_T *tv2,
5184 int ic) /* ignore case */
5185{
5186 char_u *s1, *s2;
5187 dict_T *d1, *d2;
5188 int a1, a2;
5189 int i;
5190
5191 /* empty and NULL function name considered the same */
5192 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005193 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005194 if (s1 != NULL && *s1 == NUL)
5195 s1 = NULL;
5196 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005197 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005198 if (s2 != NULL && *s2 == NUL)
5199 s2 = NULL;
5200 if (s1 == NULL || s2 == NULL)
5201 {
5202 if (s1 != s2)
5203 return FALSE;
5204 }
5205 else if (STRCMP(s1, s2) != 0)
5206 return FALSE;
5207
5208 /* empty dict and NULL dict is different */
5209 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5210 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5211 if (d1 == NULL || d2 == NULL)
5212 {
5213 if (d1 != d2)
5214 return FALSE;
5215 }
5216 else if (!dict_equal(d1, d2, ic, TRUE))
5217 return FALSE;
5218
5219 /* empty list and no list considered the same */
5220 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5221 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5222 if (a1 != a2)
5223 return FALSE;
5224 for (i = 0; i < a1; ++i)
5225 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5226 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5227 return FALSE;
5228
5229 return TRUE;
5230}
5231
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005232/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005233 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005234 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005236 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005238tv_equal(
5239 typval_T *tv1,
5240 typval_T *tv2,
5241 int ic, /* ignore case */
5242 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005243{
5244 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005246 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005247 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005248
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005249 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005250 * recursiveness to a limit. We guess they are equal then.
5251 * A fixed limit has the problem of still taking an awful long time.
5252 * Reduce the limit every time running into it. That should work fine for
5253 * deeply linked structures that are not recursively linked and catch
5254 * recursiveness quickly. */
5255 if (!recursive)
5256 tv_equal_recurse_limit = 1000;
5257 if (recursive_cnt >= tv_equal_recurse_limit)
5258 {
5259 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005260 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005261 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005262
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005263 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5264 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005265 if ((tv1->v_type == VAR_FUNC
5266 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5267 && (tv2->v_type == VAR_FUNC
5268 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5269 {
5270 ++recursive_cnt;
5271 r = func_equal(tv1, tv2, ic);
5272 --recursive_cnt;
5273 return r;
5274 }
5275
5276 if (tv1->v_type != tv2->v_type)
5277 return FALSE;
5278
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005279 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005280 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005281 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005282 ++recursive_cnt;
5283 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5284 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005285 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005286
5287 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005288 ++recursive_cnt;
5289 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5290 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005291 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005292
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005293 case VAR_BLOB:
5294 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5295
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005296 case VAR_NUMBER:
5297 return tv1->vval.v_number == tv2->vval.v_number;
5298
5299 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005300 s1 = tv_get_string_buf(tv1, buf1);
5301 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005302 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005303
5304 case VAR_SPECIAL:
5305 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005306
5307 case VAR_FLOAT:
5308#ifdef FEAT_FLOAT
5309 return tv1->vval.v_float == tv2->vval.v_float;
5310#endif
5311 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005312#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005313 return tv1->vval.v_job == tv2->vval.v_job;
5314#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005315 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005316#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005317 return tv1->vval.v_channel == tv2->vval.v_channel;
5318#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005319 case VAR_FUNC:
5320 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005321 case VAR_UNKNOWN:
5322 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005323 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005324
Bram Moolenaara03f2332016-02-06 18:09:59 +01005325 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5326 * does not equal anything, not even itself. */
5327 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005328}
5329
5330/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005331 * Return the next (unique) copy ID.
5332 * Used for serializing nested structures.
5333 */
5334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005335get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005336{
5337 current_copyID += COPYID_INC;
5338 return current_copyID;
5339}
5340
5341/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005342 * Garbage collection for lists and dictionaries.
5343 *
5344 * We use reference counts to be able to free most items right away when they
5345 * are no longer used. But for composite items it's possible that it becomes
5346 * unused while the reference count is > 0: When there is a recursive
5347 * reference. Example:
5348 * :let l = [1, 2, 3]
5349 * :let d = {9: l}
5350 * :let l[1] = d
5351 *
5352 * Since this is quite unusual we handle this with garbage collection: every
5353 * once in a while find out which lists and dicts are not referenced from any
5354 * variable.
5355 *
5356 * Here is a good reference text about garbage collection (refers to Python
5357 * but it applies to all reference-counting mechanisms):
5358 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005359 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005360
5361/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005363 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005364 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005365 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005366 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005367garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005368{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005369 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005370 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371 buf_T *buf;
5372 win_T *wp;
5373 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005374 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005375 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005377 if (!testing)
5378 {
5379 /* Only do this once. */
5380 want_garbage_collect = FALSE;
5381 may_garbage_collect = FALSE;
5382 garbage_collect_at_exit = FALSE;
5383 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005384
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005385 /* We advance by two because we add one for items referenced through
5386 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005387 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005388
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 /*
5390 * 1. Go through all accessible variables and mark all lists and dicts
5391 * with copyID.
5392 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005393
5394 /* Don't free variables in the previous_funccal list unless they are only
5395 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005396 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005397 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005399 /* script-local variables */
5400 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005401 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005402
5403 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005404 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005405 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5406 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005407
5408 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005409 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5411 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005412 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005413 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5414 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005415
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005416 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005417 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5419 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005421 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005422
5423 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005424 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005425
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005426 /* named functions (matters for closures) */
5427 abort = abort || set_ref_in_functions(copyID);
5428
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005429 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005430 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005431
Bram Moolenaard812df62008-11-09 12:46:09 +00005432 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005434
Bram Moolenaar1dced572012-04-05 16:54:08 +02005435#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005436 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005437#endif
5438
Bram Moolenaardb913952012-06-29 12:54:53 +02005439#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005440 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005441#endif
5442
5443#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005444 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005445#endif
5446
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005447#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005448 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005449 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005450#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005451#ifdef FEAT_NETBEANS_INTG
5452 abort = abort || set_ref_in_nb_channel(copyID);
5453#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005454
Bram Moolenaare3188e22016-05-31 21:13:04 +02005455#ifdef FEAT_TIMERS
5456 abort = abort || set_ref_in_timer(copyID);
5457#endif
5458
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005459#ifdef FEAT_QUICKFIX
5460 abort = abort || set_ref_in_quickfix(copyID);
5461#endif
5462
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005463#ifdef FEAT_TERMINAL
5464 abort = abort || set_ref_in_term(copyID);
5465#endif
5466
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005467 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005468 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 /*
5470 * 2. Free lists and dictionaries that are not referenced.
5471 */
5472 did_free = free_unref_items(copyID);
5473
5474 /*
5475 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005476 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005477 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005479 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005480 else if (p_verbose > 0)
5481 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005482 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005483 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005484
5485 return did_free;
5486}
5487
5488/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005489 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005490 */
5491 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005492free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005493{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005494 int did_free = FALSE;
5495
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005496 /* Let all "free" functions know that we are here. This means no
5497 * dictionaries, lists, channels or jobs are to be freed, because we will
5498 * do that here. */
5499 in_free_unref_items = TRUE;
5500
5501 /*
5502 * PASS 1: free the contents of the items. We don't free the items
5503 * themselves yet, so that it is possible to decrement refcount counters
5504 */
5505
Bram Moolenaarcd524592016-07-17 14:57:05 +02005506 /* Go through the list of dicts and free items without the copyID. */
5507 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005508
Bram Moolenaarda861d62016-07-17 15:46:27 +02005509 /* Go through the list of lists and free items without the copyID. */
5510 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005511
5512#ifdef FEAT_JOB_CHANNEL
5513 /* Go through the list of jobs and free items without the copyID. This
5514 * must happen before doing channels, because jobs refer to channels, but
5515 * the reference from the channel to the job isn't tracked. */
5516 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5517
5518 /* Go through the list of channels and free items without the copyID. */
5519 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5520#endif
5521
5522 /*
5523 * PASS 2: free the items themselves.
5524 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005525 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005526 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005527
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005528#ifdef FEAT_JOB_CHANNEL
5529 /* Go through the list of jobs and free items without the copyID. This
5530 * must happen before doing channels, because jobs refer to channels, but
5531 * the reference from the channel to the job isn't tracked. */
5532 free_unused_jobs(copyID, COPYID_MASK);
5533
5534 /* Go through the list of channels and free items without the copyID. */
5535 free_unused_channels(copyID, COPYID_MASK);
5536#endif
5537
5538 in_free_unref_items = FALSE;
5539
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005540 return did_free;
5541}
5542
5543/*
5544 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005545 * "list_stack" is used to add lists to be marked. Can be NULL.
5546 *
5547 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005548 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005550set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551{
5552 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005553 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005554 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005555 hashtab_T *cur_ht;
5556 ht_stack_T *ht_stack = NULL;
5557 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005558
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005559 cur_ht = ht;
5560 for (;;)
5561 {
5562 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005563 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005564 /* Mark each item in the hashtab. If the item contains a hashtab
5565 * it is added to ht_stack, if it contains a list it is added to
5566 * list_stack. */
5567 todo = (int)cur_ht->ht_used;
5568 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5569 if (!HASHITEM_EMPTY(hi))
5570 {
5571 --todo;
5572 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5573 &ht_stack, list_stack);
5574 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005575 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005576
5577 if (ht_stack == NULL)
5578 break;
5579
5580 /* take an item from the stack */
5581 cur_ht = ht_stack->ht;
5582 tempitem = ht_stack;
5583 ht_stack = ht_stack->prev;
5584 free(tempitem);
5585 }
5586
5587 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005588}
5589
5590/*
5591 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005592 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5593 *
5594 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005595 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005597set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005598{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005599 listitem_T *li;
5600 int abort = FALSE;
5601 list_T *cur_l;
5602 list_stack_T *list_stack = NULL;
5603 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005604
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005605 cur_l = l;
5606 for (;;)
5607 {
5608 if (!abort)
5609 /* Mark each item in the list. If the item contains a hashtab
5610 * it is added to ht_stack, if it contains a list it is added to
5611 * list_stack. */
5612 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5613 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5614 ht_stack, &list_stack);
5615 if (list_stack == NULL)
5616 break;
5617
5618 /* take an item from the stack */
5619 cur_l = list_stack->list;
5620 tempitem = list_stack;
5621 list_stack = list_stack->prev;
5622 free(tempitem);
5623 }
5624
5625 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005626}
5627
5628/*
5629 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005630 * "list_stack" is used to add lists to be marked. Can be NULL.
5631 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5632 *
5633 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005634 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636set_ref_in_item(
5637 typval_T *tv,
5638 int copyID,
5639 ht_stack_T **ht_stack,
5640 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005641{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005642 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005643
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005644 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005645 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005646 dict_T *dd = tv->vval.v_dict;
5647
Bram Moolenaara03f2332016-02-06 18:09:59 +01005648 if (dd != NULL && dd->dv_copyID != copyID)
5649 {
5650 /* Didn't see this dict yet. */
5651 dd->dv_copyID = copyID;
5652 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005653 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005654 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5655 }
5656 else
5657 {
5658 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5659 if (newitem == NULL)
5660 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 else
5662 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005663 newitem->ht = &dd->dv_hashtab;
5664 newitem->prev = *ht_stack;
5665 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005666 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005667 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005668 }
5669 }
5670 else if (tv->v_type == VAR_LIST)
5671 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005672 list_T *ll = tv->vval.v_list;
5673
Bram Moolenaara03f2332016-02-06 18:09:59 +01005674 if (ll != NULL && ll->lv_copyID != copyID)
5675 {
5676 /* Didn't see this list yet. */
5677 ll->lv_copyID = copyID;
5678 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005679 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005680 abort = set_ref_in_list(ll, copyID, ht_stack);
5681 }
5682 else
5683 {
5684 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005685 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005686 if (newitem == NULL)
5687 abort = TRUE;
5688 else
5689 {
5690 newitem->list = ll;
5691 newitem->prev = *list_stack;
5692 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005693 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005694 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005695 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005696 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005697 else if (tv->v_type == VAR_FUNC)
5698 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005699 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005700 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005701 else if (tv->v_type == VAR_PARTIAL)
5702 {
5703 partial_T *pt = tv->vval.v_partial;
5704 int i;
5705
5706 /* A partial does not have a copyID, because it cannot contain itself.
5707 */
5708 if (pt != NULL)
5709 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005710 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005711
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005712 if (pt->pt_dict != NULL)
5713 {
5714 typval_T dtv;
5715
5716 dtv.v_type = VAR_DICT;
5717 dtv.vval.v_dict = pt->pt_dict;
5718 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5719 }
5720
5721 for (i = 0; i < pt->pt_argc; ++i)
5722 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5723 ht_stack, list_stack);
5724 }
5725 }
5726#ifdef FEAT_JOB_CHANNEL
5727 else if (tv->v_type == VAR_JOB)
5728 {
5729 job_T *job = tv->vval.v_job;
5730 typval_T dtv;
5731
5732 if (job != NULL && job->jv_copyID != copyID)
5733 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005734 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005735 if (job->jv_channel != NULL)
5736 {
5737 dtv.v_type = VAR_CHANNEL;
5738 dtv.vval.v_channel = job->jv_channel;
5739 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5740 }
5741 if (job->jv_exit_partial != NULL)
5742 {
5743 dtv.v_type = VAR_PARTIAL;
5744 dtv.vval.v_partial = job->jv_exit_partial;
5745 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5746 }
5747 }
5748 }
5749 else if (tv->v_type == VAR_CHANNEL)
5750 {
5751 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005752 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005753 typval_T dtv;
5754 jsonq_T *jq;
5755 cbq_T *cq;
5756
5757 if (ch != NULL && ch->ch_copyID != copyID)
5758 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005759 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005760 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005761 {
5762 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5763 jq = jq->jq_next)
5764 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5765 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5766 cq = cq->cq_next)
5767 if (cq->cq_partial != NULL)
5768 {
5769 dtv.v_type = VAR_PARTIAL;
5770 dtv.vval.v_partial = cq->cq_partial;
5771 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5772 }
5773 if (ch->ch_part[part].ch_partial != NULL)
5774 {
5775 dtv.v_type = VAR_PARTIAL;
5776 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5777 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5778 }
5779 }
5780 if (ch->ch_partial != NULL)
5781 {
5782 dtv.v_type = VAR_PARTIAL;
5783 dtv.vval.v_partial = ch->ch_partial;
5784 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5785 }
5786 if (ch->ch_close_partial != NULL)
5787 {
5788 dtv.v_type = VAR_PARTIAL;
5789 dtv.vval.v_partial = ch->ch_close_partial;
5790 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5791 }
5792 }
5793 }
5794#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005795 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005796}
5797
Bram Moolenaar17a13432016-01-24 14:22:10 +01005798 static char *
5799get_var_special_name(int nr)
5800{
5801 switch (nr)
5802 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005803 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005804 case VVAL_TRUE: return "v:true";
5805 case VVAL_NONE: return "v:none";
5806 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005807 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005808 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005809 return "42";
5810}
5811
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 * Return a string with the string representation of a variable.
5814 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005815 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005816 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005817 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5818 * stings as "string()", otherwise does not put quotes around strings, as
5819 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005820 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5821 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005822 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005824 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005825echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826 typval_T *tv,
5827 char_u **tofree,
5828 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005829 int copyID,
5830 int echo_style,
5831 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005832 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005834 static int recurse = 0;
5835 char_u *r = NULL;
5836
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005838 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005839 if (!did_echo_string_emsg)
5840 {
5841 /* Only give this message once for a recursive call to avoid
5842 * flooding the user with errors. And stop iterating over lists
5843 * and dicts. */
5844 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005845 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005846 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005847 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005848 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005849 }
5850 ++recurse;
5851
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 switch (tv->v_type)
5853 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005854 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005855 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005856 {
5857 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005858 r = tv->vval.v_string;
5859 if (r == NULL)
5860 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005861 }
5862 else
5863 {
5864 *tofree = string_quote(tv->vval.v_string, FALSE);
5865 r = *tofree;
5866 }
5867 break;
5868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005870 if (echo_style)
5871 {
5872 *tofree = NULL;
5873 r = tv->vval.v_string;
5874 }
5875 else
5876 {
5877 *tofree = string_quote(tv->vval.v_string, TRUE);
5878 r = *tofree;
5879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005881
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005882 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005883 {
5884 partial_T *pt = tv->vval.v_partial;
5885 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005886 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005887 garray_T ga;
5888 int i;
5889 char_u *tf;
5890
5891 ga_init2(&ga, 1, 100);
5892 ga_concat(&ga, (char_u *)"function(");
5893 if (fname != NULL)
5894 {
5895 ga_concat(&ga, fname);
5896 vim_free(fname);
5897 }
5898 if (pt != NULL && pt->pt_argc > 0)
5899 {
5900 ga_concat(&ga, (char_u *)", [");
5901 for (i = 0; i < pt->pt_argc; ++i)
5902 {
5903 if (i > 0)
5904 ga_concat(&ga, (char_u *)", ");
5905 ga_concat(&ga,
5906 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5907 vim_free(tf);
5908 }
5909 ga_concat(&ga, (char_u *)"]");
5910 }
5911 if (pt != NULL && pt->pt_dict != NULL)
5912 {
5913 typval_T dtv;
5914
5915 ga_concat(&ga, (char_u *)", ");
5916 dtv.v_type = VAR_DICT;
5917 dtv.vval.v_dict = pt->pt_dict;
5918 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5919 vim_free(tf);
5920 }
5921 ga_concat(&ga, (char_u *)")");
5922
5923 *tofree = ga.ga_data;
5924 r = *tofree;
5925 break;
5926 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005927
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005928 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005929 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005930 break;
5931
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005933 if (tv->vval.v_list == NULL)
5934 {
5935 *tofree = NULL;
5936 r = NULL;
5937 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005938 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5939 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005940 {
5941 *tofree = NULL;
5942 r = (char_u *)"[...]";
5943 }
5944 else
5945 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005946 int old_copyID = tv->vval.v_list->lv_copyID;
5947
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005948 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005949 *tofree = list2string(tv, copyID, restore_copyID);
5950 if (restore_copyID)
5951 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005952 r = *tofree;
5953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005954 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005955
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005957 if (tv->vval.v_dict == NULL)
5958 {
5959 *tofree = NULL;
5960 r = NULL;
5961 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005962 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5963 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005964 {
5965 *tofree = NULL;
5966 r = (char_u *)"{...}";
5967 }
5968 else
5969 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005970 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005971 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005972 *tofree = dict2string(tv, copyID, restore_copyID);
5973 if (restore_copyID)
5974 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005975 r = *tofree;
5976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005977 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005980 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005981 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005982 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005983 break;
5984
Bram Moolenaar835dc632016-02-07 14:27:38 +01005985 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005986 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005988 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005989 if (composite_val)
5990 {
5991 *tofree = string_quote(r, FALSE);
5992 r = *tofree;
5993 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005996 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005997#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005998 *tofree = NULL;
5999 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6000 r = numbuf;
6001 break;
6002#endif
6003
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006004 case VAR_SPECIAL:
6005 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006006 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006007 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009
Bram Moolenaar8502c702014-06-17 12:51:16 +02006010 if (--recurse == 0)
6011 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006012 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013}
6014
6015/*
6016 * Return a string with the string representation of a variable.
6017 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6018 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006019 * Does not put quotes around strings, as ":echo" displays values.
6020 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6021 * May return NULL.
6022 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006023 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006024echo_string(
6025 typval_T *tv,
6026 char_u **tofree,
6027 char_u *numbuf,
6028 int copyID)
6029{
6030 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6031}
6032
6033/*
6034 * Return a string with the string representation of a variable.
6035 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6036 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006038 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006040 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041tv2string(
6042 typval_T *tv,
6043 char_u **tofree,
6044 char_u *numbuf,
6045 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006046{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006047 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048}
6049
6050/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 * Return string "str" in ' quotes, doubling ' characters.
6052 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006053 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006055 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 char_u *p, *r, *s;
6060
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 len = (function ? 13 : 3);
6062 if (str != NULL)
6063 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006064 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006065 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 if (*p == '\'')
6067 ++len;
6068 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069 s = r = alloc(len);
6070 if (r != NULL)
6071 {
6072 if (function)
6073 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006074 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006075 r += 10;
6076 }
6077 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006078 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 if (str != NULL)
6080 for (p = str; *p != NUL; )
6081 {
6082 if (*p == '\'')
6083 *r++ = '\'';
6084 MB_COPY_CHAR(p, r);
6085 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006086 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 if (function)
6088 *r++ = ')';
6089 *r++ = NUL;
6090 }
6091 return s;
6092}
6093
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006094#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006095/*
6096 * Convert the string "text" to a floating point number.
6097 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6098 * this always uses a decimal point.
6099 * Returns the length of the text that was consumed.
6100 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102string2float(
6103 char_u *text,
6104 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006105{
6106 char *s = (char *)text;
6107 float_T f;
6108
Bram Moolenaar62473612017-01-08 19:25:40 +01006109 /* MS-Windows does not deal with "inf" and "nan" properly. */
6110 if (STRNICMP(text, "inf", 3) == 0)
6111 {
6112 *value = INFINITY;
6113 return 3;
6114 }
6115 if (STRNICMP(text, "-inf", 3) == 0)
6116 {
6117 *value = -INFINITY;
6118 return 4;
6119 }
6120 if (STRNICMP(text, "nan", 3) == 0)
6121 {
6122 *value = NAN;
6123 return 3;
6124 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006125 f = strtod(s, &s);
6126 *value = f;
6127 return (int)((char_u *)s - text);
6128}
6129#endif
6130
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 * Get the value of an environment variable.
6133 * "arg" is pointing to the '$'. It is advanced to after the name.
6134 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006135 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 */
6137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 char_u *string = NULL;
6141 int len;
6142 int cc;
6143 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006144 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
6146 ++*arg;
6147 name = *arg;
6148 len = get_env_len(arg);
6149 if (evaluate)
6150 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006151 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006152 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006153
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006154 cc = name[len];
6155 name[len] = NUL;
6156 /* first try vim_getenv(), fast for normal environment vars */
6157 string = vim_getenv(name, &mustfree);
6158 if (string != NULL && *string != NUL)
6159 {
6160 if (!mustfree)
6161 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006163 else
6164 {
6165 if (mustfree)
6166 vim_free(string);
6167
6168 /* next try expanding things like $VIM and ${HOME} */
6169 string = expand_env_save(name - 1);
6170 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006171 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006172 }
6173 name[len] = cc;
6174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006175 rettv->v_type = VAR_STRING;
6176 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178
6179 return OK;
6180}
6181
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006182
6183
6184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006186 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006189var2fpos(
6190 typval_T *varp,
6191 int dollar_lnum, /* TRUE when $ is last line */
6192 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006194 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006196 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaara5525202006-03-02 22:52:09 +00006198 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006199 if (varp->v_type == VAR_LIST)
6200 {
6201 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006202 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006203 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006204 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006205
6206 l = varp->vval.v_list;
6207 if (l == NULL)
6208 return NULL;
6209
6210 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006211 pos.lnum = list_find_nr(l, 0L, &error);
6212 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213 return NULL; /* invalid line number */
6214
6215 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006216 pos.col = list_find_nr(l, 1L, &error);
6217 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006218 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006219 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006220
6221 /* We accept "$" for the column number: last column. */
6222 li = list_find(l, 1L);
6223 if (li != NULL && li->li_tv.v_type == VAR_STRING
6224 && li->li_tv.vval.v_string != NULL
6225 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6226 pos.col = len + 1;
6227
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006229 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006230 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006231 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006232
Bram Moolenaara5525202006-03-02 22:52:09 +00006233 /* Get the virtual offset. Defaults to zero. */
6234 pos.coladd = list_find_nr(l, 2L, &error);
6235 if (error)
6236 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006237
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006238 return &pos;
6239 }
6240
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006241 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006242 if (name == NULL)
6243 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006244 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006246 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6247 {
6248 if (VIsual_active)
6249 return &VIsual;
6250 return &curwin->w_cursor;
6251 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006252 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006254 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6256 return NULL;
6257 return pp;
6258 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006259
Bram Moolenaara5525202006-03-02 22:52:09 +00006260 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006261
Bram Moolenaar477933c2007-07-17 14:32:23 +00006262 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006263 {
6264 pos.col = 0;
6265 if (name[1] == '0') /* "w0": first visible line */
6266 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006267 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006268 /* In silent Ex mode topline is zero, but that's not a valid line
6269 * number; use one instead. */
6270 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006271 return &pos;
6272 }
6273 else if (name[1] == '$') /* "w$": last visible line */
6274 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006275 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006276 /* In silent Ex mode botline is zero, return zero then. */
6277 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006278 return &pos;
6279 }
6280 }
6281 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006283 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
6285 pos.lnum = curbuf->b_ml.ml_line_count;
6286 pos.col = 0;
6287 }
6288 else
6289 {
6290 pos.lnum = curwin->w_cursor.lnum;
6291 pos.col = (colnr_T)STRLEN(ml_get_curline());
6292 }
6293 return &pos;
6294 }
6295 return NULL;
6296}
6297
6298/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006299 * Convert list in "arg" into a position and optional file number.
6300 * When "fnump" is NULL there is no file number, only 3 items.
6301 * Note that the column is passed on as-is, the caller may want to decrement
6302 * it to use 1 for the first column.
6303 * Return FAIL when conversion is not possible, doesn't check the position for
6304 * validity.
6305 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006307list2fpos(
6308 typval_T *arg,
6309 pos_T *posp,
6310 int *fnump,
6311 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006312{
6313 list_T *l = arg->vval.v_list;
6314 long i = 0;
6315 long n;
6316
Bram Moolenaar493c1782014-05-28 14:34:46 +02006317 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6318 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006319 if (arg->v_type != VAR_LIST
6320 || l == NULL
6321 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006322 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006323 return FAIL;
6324
6325 if (fnump != NULL)
6326 {
6327 n = list_find_nr(l, i++, NULL); /* fnum */
6328 if (n < 0)
6329 return FAIL;
6330 if (n == 0)
6331 n = curbuf->b_fnum; /* current buffer */
6332 *fnump = n;
6333 }
6334
6335 n = list_find_nr(l, i++, NULL); /* lnum */
6336 if (n < 0)
6337 return FAIL;
6338 posp->lnum = n;
6339
6340 n = list_find_nr(l, i++, NULL); /* col */
6341 if (n < 0)
6342 return FAIL;
6343 posp->col = n;
6344
Bram Moolenaar493c1782014-05-28 14:34:46 +02006345 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006346 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006347 posp->coladd = 0;
6348 else
6349 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006350
Bram Moolenaar493c1782014-05-28 14:34:46 +02006351 if (curswantp != NULL)
6352 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6353
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006354 return OK;
6355}
6356
6357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * Get the length of an environment variable name.
6359 * Advance "arg" to the first character after the name.
6360 * Return 0 for error.
6361 */
6362 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006363get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 char_u *p;
6366 int len;
6367
6368 for (p = *arg; vim_isIDc(*p); ++p)
6369 ;
6370 if (p == *arg) /* no name found */
6371 return 0;
6372
6373 len = (int)(p - *arg);
6374 *arg = p;
6375 return len;
6376}
6377
6378/*
6379 * Get the length of the name of a function or internal variable.
6380 * "arg" is advanced to the first non-white character after the name.
6381 * Return 0 if something is wrong.
6382 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006384get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 char_u *p;
6387 int len;
6388
6389 /* Find the end of the name. */
6390 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006391 {
6392 if (*p == ':')
6393 {
6394 /* "s:" is start of "s:var", but "n:" is not and can be used in
6395 * slice "[n:]". Also "xx:" is not a namespace. */
6396 len = (int)(p - *arg);
6397 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6398 || len > 1)
6399 break;
6400 }
6401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (p == *arg) /* no name found */
6403 return 0;
6404
6405 len = (int)(p - *arg);
6406 *arg = skipwhite(p);
6407
6408 return len;
6409}
6410
6411/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006412 * Get the length of the name of a variable or function.
6413 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006415 * Return -1 if curly braces expansion failed.
6416 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 * If the name contains 'magic' {}'s, expand them and return the
6418 * expanded name in an allocated string via 'alias' - caller must free.
6419 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006421get_name_len(
6422 char_u **arg,
6423 char_u **alias,
6424 int evaluate,
6425 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 char_u *p;
6429 char_u *expr_start;
6430 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 *alias = NULL; /* default to no alias */
6433
6434 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6435 && (*arg)[2] == (int)KE_SNR)
6436 {
6437 /* hard coded <SNR>, already translated */
6438 *arg += 3;
6439 return get_id_len(arg) + 3;
6440 }
6441 len = eval_fname_script(*arg);
6442 if (len > 0)
6443 {
6444 /* literal "<SID>", "s:" or "<SNR>" */
6445 *arg += len;
6446 }
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006449 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006451 p = find_name_end(*arg, &expr_start, &expr_end,
6452 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 if (expr_start != NULL)
6454 {
6455 char_u *temp_string;
6456
6457 if (!evaluate)
6458 {
6459 len += (int)(p - *arg);
6460 *arg = skipwhite(p);
6461 return len;
6462 }
6463
6464 /*
6465 * Include any <SID> etc in the expanded string:
6466 * Thus the -len here.
6467 */
6468 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6469 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006470 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 *alias = temp_string;
6472 *arg = skipwhite(p);
6473 return (int)STRLEN(temp_string);
6474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
6476 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006477 // Only give an error when there is something, otherwise it will be
6478 // reported at a higher level.
6479 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006480 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
6482 return len;
6483}
6484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006485/*
6486 * Find the end of a variable or function name, taking care of magic braces.
6487 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6488 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006489 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490 * Return a pointer to just after the name. Equal to "arg" if there is no
6491 * valid name.
6492 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006494find_name_end(
6495 char_u *arg,
6496 char_u **expr_start,
6497 char_u **expr_end,
6498 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006500 int mb_nest = 0;
6501 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006503 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006505 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006507 *expr_start = NULL;
6508 *expr_end = NULL;
6509 }
6510
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006511 /* Quick check for valid starting character. */
6512 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6513 return arg;
6514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006515 for (p = arg; *p != NUL
6516 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006518 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006520 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006521 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006522 if (*p == '\'')
6523 {
6524 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006525 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006526 ;
6527 if (*p == NUL)
6528 break;
6529 }
6530 else if (*p == '"')
6531 {
6532 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006533 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006534 if (*p == '\\' && p[1] != NUL)
6535 ++p;
6536 if (*p == NUL)
6537 break;
6538 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006539 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6540 {
6541 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006542 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006543 len = (int)(p - arg);
6544 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006545 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006546 break;
6547 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006549 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006551 if (*p == '[')
6552 ++br_nest;
6553 else if (*p == ']')
6554 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006557 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006559 if (*p == '{')
6560 {
6561 mb_nest++;
6562 if (expr_start != NULL && *expr_start == NULL)
6563 *expr_start = p;
6564 }
6565 else if (*p == '}')
6566 {
6567 mb_nest--;
6568 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6569 *expr_end = p;
6570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573
6574 return p;
6575}
6576
6577/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006578 * Expands out the 'magic' {}'s in a variable/function name.
6579 * Note that this can call itself recursively, to deal with
6580 * constructs like foo{bar}{baz}{bam}
6581 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6582 * "in_start" ^
6583 * "expr_start" ^
6584 * "expr_end" ^
6585 * "in_end" ^
6586 *
6587 * Returns a new allocated string, which the caller must free.
6588 * Returns NULL for failure.
6589 */
6590 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006591make_expanded_name(
6592 char_u *in_start,
6593 char_u *expr_start,
6594 char_u *expr_end,
6595 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006596{
6597 char_u c1;
6598 char_u *retval = NULL;
6599 char_u *temp_result;
6600 char_u *nextcmd = NULL;
6601
6602 if (expr_end == NULL || in_end == NULL)
6603 return NULL;
6604 *expr_start = NUL;
6605 *expr_end = NUL;
6606 c1 = *in_end;
6607 *in_end = NUL;
6608
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006609 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006610 if (temp_result != NULL && nextcmd == NULL)
6611 {
6612 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6613 + (in_end - expr_end) + 1));
6614 if (retval != NULL)
6615 {
6616 STRCPY(retval, in_start);
6617 STRCAT(retval, temp_result);
6618 STRCAT(retval, expr_end + 1);
6619 }
6620 }
6621 vim_free(temp_result);
6622
6623 *in_end = c1; /* put char back for error messages */
6624 *expr_start = '{';
6625 *expr_end = '}';
6626
6627 if (retval != NULL)
6628 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006629 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006630 if (expr_start != NULL)
6631 {
6632 /* Further expansion! */
6633 temp_result = make_expanded_name(retval, expr_start,
6634 expr_end, temp_result);
6635 vim_free(retval);
6636 retval = temp_result;
6637 }
6638 }
6639
6640 return retval;
6641}
6642
6643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006645 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006648eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006650 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6651}
6652
6653/*
6654 * Return TRUE if character "c" can be used as the first character in a
6655 * variable or function name (excluding '{' and '}').
6656 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006658eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006659{
6660 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661}
6662
6663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 * Set number v: variable to "val".
6665 */
6666 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006667set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006669 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670}
6671
6672/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006673 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006675 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006678 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006681/*
6682 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006683 * If the String variable has never been set, return an empty string.
6684 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006685 */
6686 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006687get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006688{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006689 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006690}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006691
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006693 * Get List v: variable value. Caller must take care of reference count when
6694 * needed.
6695 */
6696 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006697get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006698{
6699 return vimvars[idx].vv_list;
6700}
6701
6702/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006703 * Get Dict v: variable value. Caller must take care of reference count when
6704 * needed.
6705 */
6706 dict_T *
6707get_vim_var_dict(int idx)
6708{
6709 return vimvars[idx].vv_dict;
6710}
6711
6712/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006713 * Set v:char to character "c".
6714 */
6715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006716set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006717{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006718 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006719
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006720 if (has_mbyte)
6721 buf[(*mb_char2bytes)(c, buf)] = NUL;
6722 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006723 {
6724 buf[0] = c;
6725 buf[1] = NUL;
6726 }
6727 set_vim_var_string(VV_CHAR, buf, -1);
6728}
6729
6730/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006731 * Set v:count to "count" and v:count1 to "count1".
6732 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735set_vcount(
6736 long count,
6737 long count1,
6738 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006740 if (set_prevcount)
6741 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006742 vimvars[VV_COUNT].vv_nr = count;
6743 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
6746/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006747 * Save variables that might be changed as a side effect. Used when executing
6748 * a timer callback.
6749 */
6750 void
6751save_vimvars(vimvars_save_T *vvsave)
6752{
6753 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6754 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6755 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6756}
6757
6758/*
6759 * Restore variables saved by save_vimvars().
6760 */
6761 void
6762restore_vimvars(vimvars_save_T *vvsave)
6763{
6764 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6765 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6766 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6767}
6768
6769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 * Set string v: variable to a copy of "val".
6771 */
6772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006773set_vim_var_string(
6774 int idx,
6775 char_u *val,
6776 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
Bram Moolenaara542c682016-01-31 16:28:04 +01006778 clear_tv(&vimvars[idx].vv_di.di_tv);
6779 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006783 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006785 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786}
6787
6788/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006789 * Set List v: variable to "val".
6790 */
6791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006792set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006793{
Bram Moolenaara542c682016-01-31 16:28:04 +01006794 clear_tv(&vimvars[idx].vv_di.di_tv);
6795 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006796 vimvars[idx].vv_list = val;
6797 if (val != NULL)
6798 ++val->lv_refcount;
6799}
6800
6801/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006802 * Set Dictionary v: variable to "val".
6803 */
6804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006805set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006806{
Bram Moolenaara542c682016-01-31 16:28:04 +01006807 clear_tv(&vimvars[idx].vv_di.di_tv);
6808 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006809 vimvars[idx].vv_dict = val;
6810 if (val != NULL)
6811 {
6812 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006813 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006814 }
6815}
6816
6817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * Set v:register if needed.
6819 */
6820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 char_u regname;
6824
6825 if (c == 0 || c == ' ')
6826 regname = '"';
6827 else
6828 regname = c;
6829 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006830 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 set_vim_var_string(VV_REG, &regname, 1);
6832}
6833
6834/*
6835 * Get or set v:exception. If "oldval" == NULL, return the current value.
6836 * Otherwise, restore the value to "oldval" and return NULL.
6837 * Must always be called in pairs to save and restore v:exception! Does not
6838 * take care of memory allocations.
6839 */
6840 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 return NULL;
6848}
6849
6850/*
6851 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6852 * Otherwise, restore the value to "oldval" and return NULL.
6853 * Must always be called in pairs to save and restore v:throwpoint! Does not
6854 * take care of memory allocations.
6855 */
6856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006857v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 return NULL;
6864}
6865
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866/*
6867 * Set v:cmdarg.
6868 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6869 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6870 * Must always be called in pairs!
6871 */
6872 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006873set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875 char_u *oldval;
6876 char_u *newval;
6877 unsigned len;
6878
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006880 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006882 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006884 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006887 if (eap->force_bin == FORCE_BIN)
6888 len = 6;
6889 else if (eap->force_bin == FORCE_NOBIN)
6890 len = 8;
6891 else
6892 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893
6894 if (eap->read_edit)
6895 len += 7;
6896
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006897 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006898 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006899 if (eap->force_enc != 0)
6900 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006901 if (eap->bad_char != 0)
6902 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006903
6904 newval = alloc(len + 1);
6905 if (newval == NULL)
6906 return NULL;
6907
6908 if (eap->force_bin == FORCE_BIN)
6909 sprintf((char *)newval, " ++bin");
6910 else if (eap->force_bin == FORCE_NOBIN)
6911 sprintf((char *)newval, " ++nobin");
6912 else
6913 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006914
6915 if (eap->read_edit)
6916 STRCAT(newval, " ++edit");
6917
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006918 if (eap->force_ff != 0)
6919 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006920 eap->force_ff == 'u' ? "unix"
6921 : eap->force_ff == 'd' ? "dos"
6922 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006923 if (eap->force_enc != 0)
6924 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6925 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006926 if (eap->bad_char == BAD_KEEP)
6927 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6928 else if (eap->bad_char == BAD_DROP)
6929 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6930 else if (eap->bad_char != 0)
6931 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006932 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006933 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
6936/*
6937 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006938 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941get_var_tv(
6942 char_u *name,
6943 int len, /* length of "name" */
6944 typval_T *rettv, /* NULL when only checking existence */
6945 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6946 int verbose, /* may give error message */
6947 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
6949 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 /* truncate the name, so that we can use strcmp() */
6955 cc = name[len];
6956 name[len] = NUL;
6957
6958 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 * Check for user-defined variables.
6960 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006961 v = find_var(name, NULL, no_autoload);
6962 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006964 tv = &v->di_tv;
6965 if (dip != NULL)
6966 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006971 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006972 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 ret = FAIL;
6974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006975 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 name[len] = cc;
6979
6980 return ret;
6981}
6982
6983/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006984 * Check if variable "name[len]" is a local variable or an argument.
6985 * If so, "*eval_lavars_used" is set to TRUE.
6986 */
6987 static void
6988check_vars(char_u *name, int len)
6989{
6990 int cc;
6991 char_u *varname;
6992 hashtab_T *ht;
6993
6994 if (eval_lavars_used == NULL)
6995 return;
6996
6997 /* truncate the name, so that we can use strcmp() */
6998 cc = name[len];
6999 name[len] = NUL;
7000
7001 ht = find_var_ht(name, &varname);
7002 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7003 {
7004 if (find_var(name, NULL, TRUE) != NULL)
7005 *eval_lavars_used = TRUE;
7006 }
7007
7008 name[len] = cc;
7009}
7010
7011/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007012 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7013 * Also handle function call with Funcref variable: func(expr)
7014 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7015 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017handle_subscript(
7018 char_u **arg,
7019 typval_T *rettv,
7020 int evaluate, /* do more than finding the end */
7021 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007022{
7023 int ret = OK;
7024 dict_T *selfdict = NULL;
7025 char_u *s;
7026 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007027 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007028
7029 while (ret == OK
7030 && (**arg == '['
7031 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007032 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7033 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007034 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007035 {
7036 if (**arg == '(')
7037 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007038 partial_T *pt = NULL;
7039
Bram Moolenaard9fba312005-06-26 22:34:35 +00007040 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007041 if (evaluate)
7042 {
7043 functv = *rettv;
7044 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007045
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007046 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007047 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007048 {
7049 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007050 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007051 }
7052 else
7053 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007054 }
7055 else
7056 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007057 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007058 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007059 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007060
7061 /* Clear the funcref afterwards, so that deleting it while
7062 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007063 if (evaluate)
7064 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007065
7066 /* Stop the expression evaluation when immediately aborting on
7067 * error, or when an interrupt occurred or an exception was thrown
7068 * but not caught. */
7069 if (aborting())
7070 {
7071 if (ret == OK)
7072 clear_tv(rettv);
7073 ret = FAIL;
7074 }
7075 dict_unref(selfdict);
7076 selfdict = NULL;
7077 }
7078 else /* **arg == '[' || **arg == '.' */
7079 {
7080 dict_unref(selfdict);
7081 if (rettv->v_type == VAR_DICT)
7082 {
7083 selfdict = rettv->vval.v_dict;
7084 if (selfdict != NULL)
7085 ++selfdict->dv_refcount;
7086 }
7087 else
7088 selfdict = NULL;
7089 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7090 {
7091 clear_tv(rettv);
7092 ret = FAIL;
7093 }
7094 }
7095 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007096
Bram Moolenaar1d429612016-05-24 15:44:17 +02007097 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7098 * Don't do this when "Func" is already a partial that was bound
7099 * explicitly (pt_auto is FALSE). */
7100 if (selfdict != NULL
7101 && (rettv->v_type == VAR_FUNC
7102 || (rettv->v_type == VAR_PARTIAL
7103 && (rettv->vval.v_partial->pt_auto
7104 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007105 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007106
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007107 dict_unref(selfdict);
7108 return ret;
7109}
7110
7111/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007112 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113 * value).
7114 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007115 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007116alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007117{
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119}
7120
7121/*
7122 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 * The string "s" must have been allocated, it is consumed.
7124 * Return NULL for out of memory, the variable otherwise.
7125 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007127alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007131 rettv = alloc_tv();
7132 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007134 rettv->v_type = VAR_STRING;
7135 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
7137 else
7138 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007139 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140}
7141
7142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007143 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 if (varp != NULL)
7149 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007150 switch (varp->v_type)
7151 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007152 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007153 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007154 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007155 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 vim_free(varp->vval.v_string);
7157 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007158 case VAR_PARTIAL:
7159 partial_unref(varp->vval.v_partial);
7160 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007161 case VAR_BLOB:
7162 blob_unref(varp->vval.v_blob);
7163 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007164 case VAR_LIST:
7165 list_unref(varp->vval.v_list);
7166 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007167 case VAR_DICT:
7168 dict_unref(varp->vval.v_dict);
7169 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007170 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007172 job_unref(varp->vval.v_job);
7173 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007174#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007175 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007176#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007177 channel_unref(varp->vval.v_channel);
7178 break;
7179#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007180 case VAR_NUMBER:
7181 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007182 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007183 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007184 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 vim_free(varp);
7187 }
7188}
7189
7190/*
7191 * Free the memory for a variable value and set the value to NULL or 0.
7192 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 if (varp != NULL)
7197 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007198 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007200 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007201 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007202 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007203 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007204 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007205 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007206 case VAR_PARTIAL:
7207 partial_unref(varp->vval.v_partial);
7208 varp->vval.v_partial = NULL;
7209 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007210 case VAR_BLOB:
7211 blob_unref(varp->vval.v_blob);
7212 varp->vval.v_blob = NULL;
7213 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007214 case VAR_LIST:
7215 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 case VAR_DICT:
7219 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007222 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007223 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 varp->vval.v_number = 0;
7225 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007226 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007227#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007228 varp->vval.v_float = 0.0;
7229 break;
7230#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007231 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007233 job_unref(varp->vval.v_job);
7234 varp->vval.v_job = NULL;
7235#endif
7236 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007237 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007238#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007239 channel_unref(varp->vval.v_channel);
7240 varp->vval.v_channel = NULL;
7241#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242 case VAR_UNKNOWN:
7243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007245 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 }
7247}
7248
7249/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007250 * Set the value of a variable to NULL without freeing items.
7251 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007253init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007254{
7255 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007257}
7258
7259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 * Get the number value of a variable.
7261 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007263 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007264 * caller of incompatible types: it sets *denote to TRUE if "denote"
7265 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007267 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007268tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007270 int error = FALSE;
7271
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007272 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007273}
7274
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007275 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007276tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007277{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007278 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007283 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007284 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007285#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007286 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007287 break;
7288#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007289 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007290 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007291 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007292 break;
7293 case VAR_STRING:
7294 if (varp->vval.v_string != NULL)
7295 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007296 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007298 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007299 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007300 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007301 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007302 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007303 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007304 case VAR_SPECIAL:
7305 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7306 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007307 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007308#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007309 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007310 break;
7311#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007312 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007313#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007314 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007315 break;
7316#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007317 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007318 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007319 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007320 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007321 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 if (denote == NULL) /* useful for values that must be unsigned */
7325 n = -1;
7326 else
7327 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007328 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007331#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007333tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007334{
7335 switch (varp->v_type)
7336 {
7337 case VAR_NUMBER:
7338 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007339 case VAR_FLOAT:
7340 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007341 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007342 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007343 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007344 break;
7345 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007347 break;
7348 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007349 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007350 break;
7351 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007352 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007353 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007354 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007355 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007356 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007357 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007358# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007359 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007360 break;
7361# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007362 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007363# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007364 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007365 break;
7366# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007367 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007368 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007369 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007370 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007371 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007372 break;
7373 }
7374 return 0;
7375}
7376#endif
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 * Get the string value of a variable.
7380 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007381 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7382 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 * If the String variable has never been set, return an empty string.
7384 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007388 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 static char_u mybuf[NUMBUFLEN];
7392
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007393 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394}
7395
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007396 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007397tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007399 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007400
7401 return res != NULL ? res : (char_u *)"";
7402}
7403
Bram Moolenaar7d647822014-04-05 21:28:56 +02007404/*
7405 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7406 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007407 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409{
7410 static char_u mybuf[NUMBUFLEN];
7411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007412 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007413}
7414
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007415 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007416tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007417{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007421 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007422 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007423 return buf;
7424 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007425 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007426 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007427 break;
7428 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 break;
7431 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007432 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007433 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007434 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007435#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007436 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007437 break;
7438#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 case VAR_STRING:
7440 if (varp->vval.v_string != NULL)
7441 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007442 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007443 case VAR_SPECIAL:
7444 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7445 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007446 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007447 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007448 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007449 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007450#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007451 {
7452 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007453 char *status;
7454
7455 if (job == NULL)
7456 return (char_u *)"no process";
7457 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007458 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007459 : "run";
7460# ifdef UNIX
7461 vim_snprintf((char *)buf, NUMBUFLEN,
7462 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007463# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007464 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007465 "process %ld %s",
7466 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007467 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007468# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007469 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007470 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7471# endif
7472 return buf;
7473 }
7474#endif
7475 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007476 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007478 {
7479 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007480 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007481
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007482 if (channel == NULL)
7483 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7484 else
7485 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007486 "channel %d %s", channel->ch_id, status);
7487 return buf;
7488 }
7489#endif
7490 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007491 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007492 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007495 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496}
7497
7498/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007499 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7500 * string() on Dict, List, etc.
7501 */
7502 char_u *
7503tv_stringify(typval_T *varp, char_u *buf)
7504{
7505 if (varp->v_type == VAR_LIST
7506 || varp->v_type == VAR_DICT
7507 || varp->v_type == VAR_FUNC
7508 || varp->v_type == VAR_PARTIAL
7509 || varp->v_type == VAR_FLOAT)
7510 {
7511 typval_T tmp;
7512
7513 f_string(varp, &tmp);
7514 tv_get_string_buf(&tmp, buf);
7515 clear_tv(varp);
7516 *varp = tmp;
7517 return tmp.vval.v_string;
7518 }
7519 return tv_get_string_buf(varp, buf);
7520}
7521
7522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 * Find variable "name" in the list of variables.
7524 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007525 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007526 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007529 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007530find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007534 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
Bram Moolenaara7043832005-01-21 11:56:39 +00007536 ht = find_var_ht(name, &varname);
7537 if (htp != NULL)
7538 *htp = ht;
7539 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007541 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7542 if (ret != NULL)
7543 return ret;
7544
7545 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007546 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547}
7548
7549/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007550 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007551 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554find_var_in_ht(
7555 hashtab_T *ht,
7556 int htname,
7557 char_u *varname,
7558 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007559{
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 hashitem_T *hi;
7561
7562 if (*varname == NUL)
7563 {
7564 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007565 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007567 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 case 'g': return &globvars_var;
7569 case 'v': return &vimvars_var;
7570 case 'b': return &curbuf->b_bufvar;
7571 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007572 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007573 case 'l': return get_funccal_local_var();
7574 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 }
7576 return NULL;
7577 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007578
7579 hi = hash_find(ht, varname);
7580 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007581 {
7582 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007583 * worked find the variable again. Don't auto-load a script if it was
7584 * loaded already, otherwise it would be loaded every time when
7585 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007586 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007587 {
7588 /* Note: script_autoload() may make "hi" invalid. It must either
7589 * be obtained again or not used. */
7590 if (!script_autoload(varname, FALSE) || aborting())
7591 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007592 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007593 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007594 if (HASHITEM_EMPTY(hi))
7595 return NULL;
7596 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007598}
7599
7600/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007602 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007603 * Set "varname" to the start of name without ':'.
7604 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007605 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007606find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007608 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007609 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007610
Bram Moolenaar73627d02015-08-11 15:46:09 +02007611 if (name[0] == NUL)
7612 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (name[1] != ':')
7614 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007615 /* The name must not start with a colon or #. */
7616 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 return NULL;
7618 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007619
7620 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007621 hi = hash_find(&compat_hashtab, name);
7622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007623 return &compat_hashtab;
7624
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007625 ht = get_funccal_local_ht();
7626 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007628 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007631 if (*name == 'g') /* global variable */
7632 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007633 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7634 */
7635 if (vim_strchr(name + 2, ':') != NULL
7636 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007637 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007639 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007641 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007642 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007643 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 if (*name == 'v') /* v: variable */
7645 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007646 if (*name == 'a') /* a: function argument */
7647 return get_funccal_args_ht();
7648 if (*name == 'l') /* l: local function variable */
7649 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007651 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7652 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 return NULL;
7654}
7655
7656/*
7657 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007658 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 * Returns NULL when it doesn't exist.
7660 */
7661 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007662get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007666 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (v == NULL)
7668 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007669 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670}
7671
7672/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 * sourcing this script and when executing functions defined in the script.
7675 */
7676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007677new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
Bram Moolenaara7043832005-01-21 11:56:39 +00007679 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 hashtab_T *ht;
7681 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007682
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7684 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007685 /* Re-allocating ga_data means that an ht_array pointing to
7686 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007688 for (i = 1; i <= ga_scripts.ga_len; ++i)
7689 {
7690 ht = &SCRIPT_VARS(i);
7691 if (ht->ht_mask == HT_INIT_SIZE - 1)
7692 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007693 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007695 }
7696
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 while (ga_scripts.ga_len < id)
7698 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007699 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007700 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007701 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 }
7705}
7706
7707/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7709 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007715 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007716 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007717 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007718 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 dict_var->di_tv.vval.v_dict = dict;
7720 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007721 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7723 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724}
7725
7726/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007727 * Unreference a dictionary initialized by init_var_dict().
7728 */
7729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007730unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007731{
7732 /* Now the dict needs to be freed if no one else is using it, go back to
7733 * normal reference counting. */
7734 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7735 dict_unref(dict);
7736}
7737
7738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 * Frees all allocated variables and the value they contain.
7741 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 */
7743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007744vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007745{
7746 vars_clear_ext(ht, TRUE);
7747}
7748
7749/*
7750 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7751 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007753vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
Bram Moolenaara7043832005-01-21 11:56:39 +00007755 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 hashitem_T *hi;
7757 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007760 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007761 for (hi = ht->ht_array; todo > 0; ++hi)
7762 {
7763 if (!HASHITEM_EMPTY(hi))
7764 {
7765 --todo;
7766
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007768 * ht_array might change then. hash_clear() takes care of it
7769 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 v = HI2DI(hi);
7771 if (free_val)
7772 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007773 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007775 }
7776 }
7777 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007778 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779}
7780
Bram Moolenaara7043832005-01-21 11:56:39 +00007781/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 * Delete a variable from hashtab "ht" at item "hi".
7783 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007786delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007789
7790 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 clear_tv(&di->di_tv);
7792 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793}
7794
7795/*
7796 * List the value of one internal variable.
7797 */
7798 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007799list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 char_u *tofree;
7802 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007803 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007804
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007805 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007807 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007812list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007813 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814 char_u *name,
7815 int type,
7816 char_u *string,
7817 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
Bram Moolenaar31859182007-08-14 20:41:13 +00007819 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7820 msg_start();
7821 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007823 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 msg_putchar(' ');
7825 msg_advance(22);
7826 if (type == VAR_NUMBER)
7827 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007828 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 msg_putchar('*');
7830 else if (type == VAR_LIST)
7831 {
7832 msg_putchar('[');
7833 if (*string == '[')
7834 ++string;
7835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007836 else if (type == VAR_DICT)
7837 {
7838 msg_putchar('{');
7839 if (*string == '{')
7840 ++string;
7841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 else
7843 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007844
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007847 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007848 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007849 if (*first)
7850 {
7851 msg_clr_eos();
7852 *first = FALSE;
7853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 * If the variable already exists, the value is updated.
7859 * Otherwise the variable is created.
7860 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007862set_var(
7863 char_u *name,
7864 typval_T *tv,
7865 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007871 ht = find_var_ht(name, &varname);
7872 if (ht == NULL || *varname == NUL)
7873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007874 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007875 return;
7876 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007877 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007878
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007879 /* Search in parent scope which is possible to reference from lambda */
7880 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007881 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007882
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007883 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7884 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007885 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007886
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007890 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007891 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007893
7894 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007895 * Handle setting internal v: variables separately where needed to
7896 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 */
7898 if (ht == &vimvarht)
7899 {
7900 if (v->di_tv.v_type == VAR_STRING)
7901 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007902 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007904 {
7905 char_u *val = tv_get_string(tv);
7906
7907 // Careful: when assigning to v:errmsg and tv_get_string()
7908 // causes an error message the variable will alrady be set.
7909 if (v->di_tv.vval.v_string == NULL)
7910 v->di_tv.vval.v_string = vim_strsave(val);
7911 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 else
7913 {
7914 /* Take over the string to avoid an extra alloc/free. */
7915 v->di_tv.vval.v_string = tv->vval.v_string;
7916 tv->vval.v_string = NULL;
7917 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007918 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007920 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007922 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007923 if (STRCMP(varname, "searchforward") == 0)
7924 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007925#ifdef FEAT_SEARCH_EXTRA
7926 else if (STRCMP(varname, "hlsearch") == 0)
7927 {
7928 no_hlsearch = !v->di_tv.vval.v_number;
7929 redraw_all_later(SOME_VALID);
7930 }
7931#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007932 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007933 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007934 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007936 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007937 return;
7938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 }
7940
7941 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943 else /* add a new variable */
7944 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01007945 // Can't add "v:" or "a:" variable.
7946 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007947 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007948 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007949 return;
7950 }
7951
Bram Moolenaar31b81602019-02-10 22:14:27 +01007952 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007953 if (!valid_varname(varname))
7954 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007955
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007956 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7957 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007958 if (v == NULL)
7959 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007963 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 return;
7965 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007966 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007971 else
7972 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007974 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977}
7978
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007979/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007980 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 * Also give an error message.
7982 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007984var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007985{
7986 if (flags & DI_FLAGS_RO)
7987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007988 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 return TRUE;
7990 }
7991 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007993 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 return TRUE;
7995 }
7996 return FALSE;
7997}
7998
7999/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008000 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8001 * Also give an error message.
8002 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008005{
8006 if (flags & DI_FLAGS_FIX)
8007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008008 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008009 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008010 return TRUE;
8011 }
8012 return FALSE;
8013}
8014
8015/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008016 * Check if a funcref is assigned to a valid variable name.
8017 * Return TRUE and give an error if not.
8018 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008020var_check_func_name(
8021 char_u *name, /* points to start of variable name */
8022 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008023{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008024 /* Allow for w: b: s: and t:. */
8025 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008026 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8027 ? name[2] : name[0]))
8028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008029 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008030 name);
8031 return TRUE;
8032 }
8033 /* Don't allow hiding a function. When "v" is not NULL we might be
8034 * assigning another function to the same var, the type is checked
8035 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008036 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008038 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008039 name);
8040 return TRUE;
8041 }
8042 return FALSE;
8043}
8044
8045/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008046 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008047 * Also give an error message, using "name" or _("name") when use_gettext is
8048 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008049 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008050 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008051var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008052{
8053 if (lock & VAR_LOCKED)
8054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008055 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008056 name == NULL ? (char_u *)_("Unknown")
8057 : use_gettext ? (char_u *)_(name)
8058 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008059 return TRUE;
8060 }
8061 if (lock & VAR_FIXED)
8062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008063 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008064 name == NULL ? (char_u *)_("Unknown")
8065 : use_gettext ? (char_u *)_(name)
8066 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008067 return TRUE;
8068 }
8069 return FALSE;
8070}
8071
8072/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008073 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8074 * Also give an error message, using "name" or _("name") when use_gettext is
8075 * TRUE.
8076 */
8077 static int
8078tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8079{
8080 int lock = 0;
8081
8082 switch (tv->v_type)
8083 {
8084 case VAR_BLOB:
8085 if (tv->vval.v_blob != NULL)
8086 lock = tv->vval.v_blob->bv_lock;
8087 break;
8088 case VAR_LIST:
8089 if (tv->vval.v_list != NULL)
8090 lock = tv->vval.v_list->lv_lock;
8091 break;
8092 case VAR_DICT:
8093 if (tv->vval.v_dict != NULL)
8094 lock = tv->vval.v_dict->dv_lock;
8095 break;
8096 default:
8097 break;
8098 }
8099 return var_check_lock(tv->v_lock, name, use_gettext)
8100 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8101}
8102
8103/*
8104 * Check if a variable name is valid.
8105 * Return FALSE and give an error if not.
8106 */
8107 int
8108valid_varname(char_u *varname)
8109{
8110 char_u *p;
8111
8112 for (p = varname; *p != NUL; ++p)
8113 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8114 && *p != AUTOLOAD_CHAR)
8115 {
8116 semsg(_(e_illvar), varname);
8117 return FALSE;
8118 }
8119 return TRUE;
8120}
8121
8122/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008125 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008126 * It is OK for "from" and "to" to point to the same item. This is used to
8127 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008130copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008133 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 switch (from->v_type)
8135 {
8136 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008137 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 to->vval.v_number = from->vval.v_number;
8139 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008140 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008141#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008142 to->vval.v_float = from->vval.v_float;
8143 break;
8144#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008145 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008147 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008148 if (to->vval.v_job != NULL)
8149 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008150 break;
8151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008152 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008153#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008154 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008155 if (to->vval.v_channel != NULL)
8156 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008157 break;
8158#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008159 case VAR_STRING:
8160 case VAR_FUNC:
8161 if (from->vval.v_string == NULL)
8162 to->vval.v_string = NULL;
8163 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008164 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008166 if (from->v_type == VAR_FUNC)
8167 func_ref(to->vval.v_string);
8168 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008169 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008170 case VAR_PARTIAL:
8171 if (from->vval.v_partial == NULL)
8172 to->vval.v_partial = NULL;
8173 else
8174 {
8175 to->vval.v_partial = from->vval.v_partial;
8176 ++to->vval.v_partial->pt_refcount;
8177 }
8178 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008179 case VAR_BLOB:
8180 if (from->vval.v_blob == NULL)
8181 to->vval.v_blob = NULL;
8182 else
8183 {
8184 to->vval.v_blob = from->vval.v_blob;
8185 ++to->vval.v_blob->bv_refcount;
8186 }
8187 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188 case VAR_LIST:
8189 if (from->vval.v_list == NULL)
8190 to->vval.v_list = NULL;
8191 else
8192 {
8193 to->vval.v_list = from->vval.v_list;
8194 ++to->vval.v_list->lv_refcount;
8195 }
8196 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008197 case VAR_DICT:
8198 if (from->vval.v_dict == NULL)
8199 to->vval.v_dict = NULL;
8200 else
8201 {
8202 to->vval.v_dict = from->vval.v_dict;
8203 ++to->vval.v_dict->dv_refcount;
8204 }
8205 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008206 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008207 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208 break;
8209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210}
8211
8212/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 * Make a copy of an item.
8214 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008215 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8216 * reference to an already copied list/dict can be used.
8217 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008218 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008220item_copy(
8221 typval_T *from,
8222 typval_T *to,
8223 int deep,
8224 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008225{
8226 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008227 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008228
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008231 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008232 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008233 }
8234 ++recurse;
8235
8236 switch (from->v_type)
8237 {
8238 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008240 case VAR_STRING:
8241 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008242 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008243 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008244 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008245 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008246 copy_tv(from, to);
8247 break;
8248 case VAR_LIST:
8249 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008250 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008251 if (from->vval.v_list == NULL)
8252 to->vval.v_list = NULL;
8253 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8254 {
8255 /* use the copy made earlier */
8256 to->vval.v_list = from->vval.v_list->lv_copylist;
8257 ++to->vval.v_list->lv_refcount;
8258 }
8259 else
8260 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8261 if (to->vval.v_list == NULL)
8262 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008263 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008264 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008265 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008266 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008267 case VAR_DICT:
8268 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008269 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008270 if (from->vval.v_dict == NULL)
8271 to->vval.v_dict = NULL;
8272 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8273 {
8274 /* use the copy made earlier */
8275 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8276 ++to->vval.v_dict->dv_refcount;
8277 }
8278 else
8279 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8280 if (to->vval.v_dict == NULL)
8281 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008283 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008284 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008285 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 }
8287 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008288 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289}
8290
8291/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 * This function is used by f_input() and f_inputdialog() functions. The third
8293 * argument to f_input() specifies the type of completion to use at the
8294 * prompt. The third argument to f_inputdialog() specifies the value to return
8295 * when the user cancels the prompt.
8296 */
8297 void
8298get_user_input(
8299 typval_T *argvars,
8300 typval_T *rettv,
8301 int inputdialog,
8302 int secret)
8303{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 char_u *p = NULL;
8306 int c;
8307 char_u buf[NUMBUFLEN];
8308 int cmd_silent_save = cmd_silent;
8309 char_u *defstr = (char_u *)"";
8310 int xp_type = EXPAND_NOTHING;
8311 char_u *xp_arg = NULL;
8312
8313 rettv->v_type = VAR_STRING;
8314 rettv->vval.v_string = NULL;
8315
8316#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008317 /* While starting up, there is no place to enter text. When running tests
8318 * with --not-a-term we assume feedkeys() will be used. */
8319 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 return;
8321#endif
8322
8323 cmd_silent = FALSE; /* Want to see the prompt. */
8324 if (prompt != NULL)
8325 {
8326 /* Only the part of the message after the last NL is considered as
8327 * prompt for the command line */
8328 p = vim_strrchr(prompt, '\n');
8329 if (p == NULL)
8330 p = prompt;
8331 else
8332 {
8333 ++p;
8334 c = *p;
8335 *p = NUL;
8336 msg_start();
8337 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008338 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 msg_didout = FALSE;
8340 msg_starthere();
8341 *p = c;
8342 }
8343 cmdline_row = msg_row;
8344
8345 if (argvars[1].v_type != VAR_UNKNOWN)
8346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008347 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 if (defstr != NULL)
8349 stuffReadbuffSpec(defstr);
8350
8351 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8352 {
8353 char_u *xp_name;
8354 int xp_namelen;
8355 long argt;
8356
8357 /* input() with a third argument: completion */
8358 rettv->vval.v_string = NULL;
8359
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008360 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 if (xp_name == NULL)
8362 return;
8363
8364 xp_namelen = (int)STRLEN(xp_name);
8365
8366 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8367 &xp_arg) == FAIL)
8368 return;
8369 }
8370 }
8371
8372 if (defstr != NULL)
8373 {
8374 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008375
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 ex_normal_busy = 0;
8377 rettv->vval.v_string =
8378 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8379 xp_type, xp_arg);
8380 ex_normal_busy = save_ex_normal_busy;
8381 }
8382 if (inputdialog && rettv->vval.v_string == NULL
8383 && argvars[1].v_type != VAR_UNKNOWN
8384 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008385 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 &argvars[2], buf));
8387
8388 vim_free(xp_arg);
8389
8390 /* since the user typed this, no need to wait for return */
8391 need_wait_return = FALSE;
8392 msg_didout = FALSE;
8393 }
8394 cmd_silent = cmd_silent_save;
8395}
8396
8397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 * ":echo expr1 ..." print each argument separated with a space, add a
8399 * newline at the end.
8400 * ":echon expr1 ..." print each argument plain.
8401 */
8402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008403ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008406 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008407 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 char_u *p;
8409 int needclr = TRUE;
8410 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008411 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008412 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008413 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415 if (eap->skip)
8416 ++emsg_skip;
8417 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8418 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419 /* If eval1() causes an error message the text from the command may
8420 * still need to be cleared. E.g., "echo 22,44". */
8421 need_clr_eos = needclr;
8422
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008424 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
8426 /*
8427 * Report the invalid expression unless the expression evaluation
8428 * has been cancelled due to an aborting error, an interrupt, or an
8429 * exception.
8430 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008431 if (!aborting() && did_emsg == did_emsg_before
8432 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008434 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 break;
8436 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008437 need_clr_eos = FALSE;
8438
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 if (!eap->skip)
8440 {
8441 if (atstart)
8442 {
8443 atstart = FALSE;
8444 /* Call msg_start() after eval1(), evaluating the expression
8445 * may cause a message to appear. */
8446 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008447 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008448 /* Mark the saved text as finishing the line, so that what
8449 * follows is displayed on a new line when scrolling back
8450 * at the more prompt. */
8451 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008456 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008457 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008458 if (p != NULL)
8459 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008461 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008463 if (*p != TAB && needclr)
8464 {
8465 /* remove any text still there from the command */
8466 msg_clr_eos();
8467 needclr = FALSE;
8468 }
8469 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 }
8471 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008472 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008473 if (has_mbyte)
8474 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008475 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008476
8477 (void)msg_outtrans_len_attr(p, i, echo_attr);
8478 p += i - 1;
8479 }
8480 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008481 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 arg = skipwhite(arg);
8488 }
8489 eap->nextcmd = check_nextcmd(arg);
8490
8491 if (eap->skip)
8492 --emsg_skip;
8493 else
8494 {
8495 /* remove text that may still be there from the command */
8496 if (needclr)
8497 msg_clr_eos();
8498 if (eap->cmdidx == CMD_echo)
8499 msg_end();
8500 }
8501}
8502
8503/*
8504 * ":echohl {name}".
8505 */
8506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008507ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008509 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510}
8511
8512/*
8513 * ":execute expr1 ..." execute the result of an expression.
8514 * ":echomsg expr1 ..." Print a message
8515 * ":echoerr expr1 ..." Print an error
8516 * Each gets spaces around each argument and a newline at the end for
8517 * echo commands
8518 */
8519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008520ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
8522 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 int ret = OK;
8525 char_u *p;
8526 garray_T ga;
8527 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008528 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529
8530 ga_init2(&ga, 1, 80);
8531
8532 if (eap->skip)
8533 ++emsg_skip;
8534 while (*arg != NUL && *arg != '|' && *arg != '\n')
8535 {
8536 p = arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008537 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8538 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
8541 if (!eap->skip)
8542 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008543 char_u buf[NUMBUFLEN];
8544
8545 if (eap->cmdidx == CMD_execute)
8546 p = tv_get_string_buf(&rettv, buf);
8547 else
8548 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 len = (int)STRLEN(p);
8550 if (ga_grow(&ga, len + 2) == FAIL)
8551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 ret = FAIL;
8554 break;
8555 }
8556 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 ga.ga_len += len;
8560 }
8561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 arg = skipwhite(arg);
8564 }
8565
8566 if (ret != FAIL && ga.ga_data != NULL)
8567 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008568 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8569 {
8570 /* Mark the already saved text as finishing the line, so that what
8571 * follows is displayed on a new line when scrolling back at the
8572 * more prompt. */
8573 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008574 }
8575
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008577 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008578 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008579 out_flush();
8580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 else if (eap->cmdidx == CMD_echoerr)
8582 {
8583 /* We don't want to abort following commands, restore did_emsg. */
8584 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008585 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (!force_abort)
8587 did_emsg = save_did_emsg;
8588 }
8589 else if (eap->cmdidx == CMD_execute)
8590 do_cmdline((char_u *)ga.ga_data,
8591 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8592 }
8593
8594 ga_clear(&ga);
8595
8596 if (eap->skip)
8597 --emsg_skip;
8598
8599 eap->nextcmd = check_nextcmd(arg);
8600}
8601
8602/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 * Find window specified by "vp" in tabpage "tp".
8604 */
8605 win_T *
8606find_win_by_nr(
8607 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008608 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008611 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 if (nr < 0)
8614 return NULL;
8615 if (nr == 0)
8616 return curwin;
8617
Bram Moolenaar29323592016-07-24 22:04:11 +02008618 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8619 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620 if (nr >= LOWEST_WIN_ID)
8621 {
8622 if (wp->w_id == nr)
8623 return wp;
8624 }
8625 else if (--nr <= 0)
8626 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008627 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 if (nr >= LOWEST_WIN_ID)
8629 return NULL;
8630 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631}
8632
8633/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008634 * Find a window: When using a Window ID in any tab page, when using a number
8635 * in the current tab page.
8636 */
8637 win_T *
8638find_win_by_nr_or_id(typval_T *vp)
8639{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008641
8642 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008643 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008644 return find_win_by_nr(vp, NULL);
8645}
8646
8647/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 * Find window specified by "wvp" in tabpage "tvp".
8649 */
8650 win_T *
8651find_tabwin(
8652 typval_T *wvp, /* VAR_UNKNOWN for current window */
8653 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8654{
8655 win_T *wp = NULL;
8656 tabpage_T *tp = NULL;
8657 long n;
8658
8659 if (wvp->v_type != VAR_UNKNOWN)
8660 {
8661 if (tvp->v_type != VAR_UNKNOWN)
8662 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008663 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 if (n >= 0)
8665 tp = find_tabpage(n);
8666 }
8667 else
8668 tp = curtab;
8669
8670 if (tp != NULL)
8671 wp = find_win_by_nr(wvp, tp);
8672 }
8673 else
8674 wp = curwin;
8675
8676 return wp;
8677}
8678
8679/*
8680 * getwinvar() and gettabwinvar()
8681 */
8682 void
8683getwinvar(
8684 typval_T *argvars,
8685 typval_T *rettv,
8686 int off) /* 1 for gettabwinvar() */
8687{
8688 win_T *win;
8689 char_u *varname;
8690 dictitem_T *v;
8691 tabpage_T *tp = NULL;
8692 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 win_T *oldcurwin;
8694 tabpage_T *oldtabpage;
8695 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 else
8700 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008702 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 ++emsg_off;
8704
8705 rettv->v_type = VAR_STRING;
8706 rettv->vval.v_string = NULL;
8707
8708 if (win != NULL && varname != NULL)
8709 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 /* Set curwin to be our win, temporarily. Also set the tabpage,
8711 * otherwise the window is not valid. Only do this when needed,
8712 * autocommands get blocked. */
8713 need_switch_win = !(tp == curtab && win == curwin);
8714 if (!need_switch_win
8715 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008717 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008719 if (varname[1] == NUL)
8720 {
8721 /* get all window-local options in a dict */
8722 dict_T *opts = get_winbuf_options(FALSE);
8723
8724 if (opts != NULL)
8725 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008726 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008727 done = TRUE;
8728 }
8729 }
8730 else if (get_option_tv(&varname, rettv, 1) == OK)
8731 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 done = TRUE;
8733 }
8734 else
8735 {
8736 /* Look up the variable. */
8737 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8738 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8739 varname, FALSE);
8740 if (v != NULL)
8741 {
8742 copy_tv(&v->di_tv, rettv);
8743 done = TRUE;
8744 }
8745 }
8746 }
8747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748 if (need_switch_win)
8749 /* restore previous notion of curwin */
8750 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 }
8752
8753 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8754 /* use the default return value */
8755 copy_tv(&argvars[off + 2], rettv);
8756
8757 --emsg_off;
8758}
8759
8760/*
8761 * "setwinvar()" and "settabwinvar()" functions
8762 */
8763 void
8764setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8765{
8766 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 win_T *save_curwin;
8768 tabpage_T *save_curtab;
8769 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 char_u *varname, *winvarname;
8771 typval_T *varp;
8772 char_u nbuf[NUMBUFLEN];
8773 tabpage_T *tp = NULL;
8774
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008775 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 return;
8777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008779 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780 else
8781 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008783 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 varp = &argvars[off + 2];
8785
8786 if (win != NULL && varname != NULL && varp != NULL)
8787 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788 need_switch_win = !(tp == curtab && win == curwin);
8789 if (!need_switch_win
8790 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 {
8792 if (*varname == '&')
8793 {
8794 long numval;
8795 char_u *strval;
8796 int error = FALSE;
8797
8798 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008799 numval = (long)tv_get_number_chk(varp, &error);
8800 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 if (!error && strval != NULL)
8802 set_option_value(varname, numval, strval, OPT_LOCAL);
8803 }
8804 else
8805 {
8806 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8807 if (winvarname != NULL)
8808 {
8809 STRCPY(winvarname, "w:");
8810 STRCPY(winvarname + 2, varname);
8811 set_var(winvarname, varp, TRUE);
8812 vim_free(winvarname);
8813 }
8814 }
8815 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 if (need_switch_win)
8817 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 }
8819}
8820
8821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8823 * "arg" points to the "&" or '+' when called, to "option" when returning.
8824 * Returns NULL when no option name found. Otherwise pointer to the char
8825 * after the option name.
8826 */
8827 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008828find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 char_u *p = *arg;
8831
8832 ++p;
8833 if (*p == 'g' && p[1] == ':')
8834 {
8835 *opt_flags = OPT_GLOBAL;
8836 p += 2;
8837 }
8838 else if (*p == 'l' && p[1] == ':')
8839 {
8840 *opt_flags = OPT_LOCAL;
8841 p += 2;
8842 }
8843 else
8844 *opt_flags = 0;
8845
8846 if (!ASCII_ISALPHA(*p))
8847 return NULL;
8848 *arg = p;
8849
8850 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8851 p += 4; /* termcap option */
8852 else
8853 while (ASCII_ISALPHA(*p))
8854 ++p;
8855 return p;
8856}
8857
8858/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008859 * Return the autoload script name for a function or variable name.
8860 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008862 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008863autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008864{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008865 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008866 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008867
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008868 /* Get the script file name: replace '#' with '/', append ".vim". */
8869 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8870 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008871 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008872 STRCPY(scriptname, "autoload/");
8873 STRCAT(scriptname, name);
8874 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8875 STRCAT(scriptname, ".vim");
8876 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8877 *p = '/';
8878 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008879}
8880
8881/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008882 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008883 * Return TRUE if a package was loaded.
8884 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008886script_autoload(
8887 char_u *name,
8888 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008889{
8890 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008891 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008892 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008893 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008894
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008895 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008896 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008897 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008898 return FALSE;
8899
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008900 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008901
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008902 /* Find the name in the list of previously loaded package names. Skip
8903 * "autoload/", it's always the same. */
8904 for (i = 0; i < ga_loaded.ga_len; ++i)
8905 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8906 break;
8907 if (!reload && i < ga_loaded.ga_len)
8908 ret = FALSE; /* was loaded already */
8909 else
8910 {
8911 /* Remember the name if it wasn't loaded already. */
8912 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8913 {
8914 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8915 tofree = NULL;
8916 }
8917
8918 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008919 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008920 ret = TRUE;
8921 }
8922
8923 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008924 return ret;
8925}
8926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8928typedef enum
8929{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008930 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8931 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8932 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933} var_flavour_T;
8934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008936var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 char_u *p = varname;
8939
8940 if (ASCII_ISUPPER(*p))
8941 {
8942 while (*(++p))
8943 if (ASCII_ISLOWER(*p))
8944 return VAR_FLAVOUR_SESSION;
8945 return VAR_FLAVOUR_VIMINFO;
8946 }
8947 else
8948 return VAR_FLAVOUR_DEFAULT;
8949}
8950#endif
8951
8952#if defined(FEAT_VIMINFO) || defined(PROTO)
8953/*
8954 * Restore global vars that start with a capital from the viminfo file
8955 */
8956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008957read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008960 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008962 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
8964 if (!writing && (find_viminfo_parameter('!') != NULL))
8965 {
8966 tab = vim_strchr(virp->vir_line + 1, '\t');
8967 if (tab != NULL)
8968 {
8969 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008970 switch (*tab)
8971 {
8972 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008973#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008974 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008975#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008976 case 'D': type = VAR_DICT; break;
8977 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008978 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008979 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
8982 tab = vim_strchr(tab, '\t');
8983 if (tab != NULL)
8984 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008985 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008986 if (type == VAR_STRING || type == VAR_DICT
8987 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008988 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008990#ifdef FEAT_FLOAT
8991 else if (type == VAR_FLOAT)
8992 (void)string2float(tab + 1, &tv.vval.v_float);
8993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008995 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008996 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008997 {
8998 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8999
9000 if (etv == NULL)
9001 /* Failed to parse back the dict or list, use it as a
9002 * string. */
9003 tv.v_type = VAR_STRING;
9004 else
9005 {
9006 vim_free(tv.vval.v_string);
9007 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009008 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009009 }
9010 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009011 else if (type == VAR_BLOB)
9012 {
9013 blob_T *blob = string2blob(tv.vval.v_string);
9014
9015 if (blob == NULL)
9016 // Failed to parse back the blob, use it as a string.
9017 tv.v_type = VAR_STRING;
9018 else
9019 {
9020 vim_free(tv.vval.v_string);
9021 tv.v_type = VAR_BLOB;
9022 tv.vval.v_blob = blob;
9023 }
9024 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009025
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009026 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009027 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009028 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009029 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009030
9031 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009032 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009033 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9034 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009035 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 }
9037 }
9038 }
9039
9040 return viminfo_readline(virp);
9041}
9042
9043/*
9044 * Write global vars that start with a capital to the viminfo file
9045 */
9046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009047write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 hashitem_T *hi;
9050 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009051 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009052 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009053 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009055 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
9057 if (find_viminfo_parameter('!') == NULL)
9058 return;
9059
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009060 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009061
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009062 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009065 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009067 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 this_var = HI2DI(hi);
9069 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009071 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009072 {
9073 case VAR_STRING: s = "STR"; break;
9074 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009075 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009076 case VAR_DICT: s = "DIC"; break;
9077 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009078 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009079 case VAR_SPECIAL: s = "XPL"; break;
9080
9081 case VAR_UNKNOWN:
9082 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009083 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009084 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009085 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009086 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009089 if (this_var->di_tv.v_type == VAR_SPECIAL)
9090 {
9091 sprintf((char *)numbuf, "%ld",
9092 (long)this_var->di_tv.vval.v_number);
9093 p = numbuf;
9094 tofree = NULL;
9095 }
9096 else
9097 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009098 if (p != NULL)
9099 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009100 vim_free(tofree);
9101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 }
9103 }
9104}
9105#endif
9106
9107#if defined(FEAT_SESSION) || defined(PROTO)
9108 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009109store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 hashitem_T *hi;
9112 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009113 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 char_u *p, *t;
9115
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009116 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009119 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009121 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 this_var = HI2DI(hi);
9123 if ((this_var->di_tv.v_type == VAR_NUMBER
9124 || this_var->di_tv.v_type == VAR_STRING)
9125 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009126 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009127 /* Escape special characters with a backslash. Turn a LF and
9128 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009129 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009130 (char_u *)"\\\"\n\r");
9131 if (p == NULL) /* out of memory */
9132 break;
9133 for (t = p; *t != NUL; ++t)
9134 if (*t == '\n')
9135 *t = 'n';
9136 else if (*t == '\r')
9137 *t = 'r';
9138 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 this_var->di_key,
9140 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9141 : ' ',
9142 p,
9143 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9144 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009145 || put_eol(fd) == FAIL)
9146 {
9147 vim_free(p);
9148 return FAIL;
9149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 vim_free(p);
9151 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009152#ifdef FEAT_FLOAT
9153 else if (this_var->di_tv.v_type == VAR_FLOAT
9154 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9155 {
9156 float_T f = this_var->di_tv.vval.v_float;
9157 int sign = ' ';
9158
9159 if (f < 0)
9160 {
9161 f = -f;
9162 sign = '-';
9163 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009164 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009165 this_var->di_key, sign, f) < 0)
9166 || put_eol(fd) == FAIL)
9167 return FAIL;
9168 }
9169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 }
9171 }
9172 return OK;
9173}
9174#endif
9175
Bram Moolenaar661b1822005-07-28 22:36:45 +00009176/*
9177 * Display script name where an item was last set.
9178 * Should only be invoked when 'verbose' is non-zero.
9179 */
9180 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009181last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009182{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009183 char_u *p;
9184
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009185 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009186 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009187 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009188 if (p != NULL)
9189 {
9190 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009191 msg_puts(_("\n\tLast set from "));
9192 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009193 if (script_ctx.sc_lnum > 0)
9194 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009195 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009196 msg_outnum((long)script_ctx.sc_lnum);
9197 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009198 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009199 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009200 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009201 }
9202}
9203
Bram Moolenaar53744302015-07-17 17:38:22 +02009204/* reset v:option_new, v:option_old and v:option_type */
9205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009206reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009207{
9208 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9209 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9210 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9211}
9212
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213/*
9214 * Prepare "gap" for an assert error and add the sourcing position.
9215 */
9216 void
9217prepare_assert_error(garray_T *gap)
9218{
9219 char buf[NUMBUFLEN];
9220
9221 ga_init2(gap, 1, 100);
9222 if (sourcing_name != NULL)
9223 {
9224 ga_concat(gap, sourcing_name);
9225 if (sourcing_lnum > 0)
9226 ga_concat(gap, (char_u *)" ");
9227 }
9228 if (sourcing_lnum > 0)
9229 {
9230 sprintf(buf, "line %ld", (long)sourcing_lnum);
9231 ga_concat(gap, (char_u *)buf);
9232 }
9233 if (sourcing_name != NULL || sourcing_lnum > 0)
9234 ga_concat(gap, (char_u *)": ");
9235}
9236
9237/*
9238 * Add an assert error to v:errors.
9239 */
9240 void
9241assert_error(garray_T *gap)
9242{
9243 struct vimvar *vp = &vimvars[VV_ERRORS];
9244
9245 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9246 /* Make sure v:errors is a list. */
9247 set_vim_var_list(VV_ERRORS, list_alloc());
9248 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9249}
9250
Bram Moolenaar65a54642018-04-28 16:56:53 +02009251 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252assert_equal_common(typval_T *argvars, assert_type_T atype)
9253{
9254 garray_T ga;
9255
9256 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9257 != (atype == ASSERT_EQUAL))
9258 {
9259 prepare_assert_error(&ga);
9260 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9261 atype);
9262 assert_error(&ga);
9263 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009264 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009265 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009266 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267}
9268
Bram Moolenaar65a54642018-04-28 16:56:53 +02009269 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009270assert_equalfile(typval_T *argvars)
9271{
9272 char_u buf1[NUMBUFLEN];
9273 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009274 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9275 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009276 garray_T ga;
9277 FILE *fd1;
9278 FILE *fd2;
9279
9280 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009281 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009282
9283 IObuff[0] = NUL;
9284 fd1 = mch_fopen((char *)fname1, READBIN);
9285 if (fd1 == NULL)
9286 {
9287 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9288 }
9289 else
9290 {
9291 fd2 = mch_fopen((char *)fname2, READBIN);
9292 if (fd2 == NULL)
9293 {
9294 fclose(fd1);
9295 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9296 }
9297 else
9298 {
9299 int c1, c2;
9300 long count = 0;
9301
9302 for (;;)
9303 {
9304 c1 = fgetc(fd1);
9305 c2 = fgetc(fd2);
9306 if (c1 == EOF)
9307 {
9308 if (c2 != EOF)
9309 STRCPY(IObuff, "first file is shorter");
9310 break;
9311 }
9312 else if (c2 == EOF)
9313 {
9314 STRCPY(IObuff, "second file is shorter");
9315 break;
9316 }
9317 else if (c1 != c2)
9318 {
9319 vim_snprintf((char *)IObuff, IOSIZE,
9320 "difference at byte %ld", count);
9321 break;
9322 }
9323 ++count;
9324 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009325 fclose(fd1);
9326 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009327 }
9328 }
9329 if (IObuff[0] != NUL)
9330 {
9331 prepare_assert_error(&ga);
9332 ga_concat(&ga, IObuff);
9333 assert_error(&ga);
9334 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009335 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009336 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009337 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009338}
9339
Bram Moolenaar65a54642018-04-28 16:56:53 +02009340 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341assert_match_common(typval_T *argvars, assert_type_T atype)
9342{
9343 garray_T ga;
9344 char_u buf1[NUMBUFLEN];
9345 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009346 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9347 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348
9349 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009350 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9352 {
9353 prepare_assert_error(&ga);
9354 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9355 atype);
9356 assert_error(&ga);
9357 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009358 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009360 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361}
9362
Bram Moolenaar65a54642018-04-28 16:56:53 +02009363 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009364assert_inrange(typval_T *argvars)
9365{
9366 garray_T ga;
9367 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009368 char_u *tofree;
9369 char msg[200];
9370 char_u numbuf[NUMBUFLEN];
9371
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009372#ifdef FEAT_FLOAT
9373 if (argvars[0].v_type == VAR_FLOAT
9374 || argvars[1].v_type == VAR_FLOAT
9375 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009376 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009377 float_T flower = tv_get_float(&argvars[0]);
9378 float_T fupper = tv_get_float(&argvars[1]);
9379 float_T factual = tv_get_float(&argvars[2]);
9380
9381 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009382 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009383 prepare_assert_error(&ga);
9384 if (argvars[3].v_type != VAR_UNKNOWN)
9385 {
9386 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9387 vim_free(tofree);
9388 }
9389 else
9390 {
9391 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9392 flower, fupper, factual);
9393 ga_concat(&ga, (char_u *)msg);
9394 }
9395 assert_error(&ga);
9396 ga_clear(&ga);
9397 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009398 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009399 }
9400 else
9401#endif
9402 {
9403 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9404 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9405 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9406
9407 if (error)
9408 return 0;
9409 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009410 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009411 prepare_assert_error(&ga);
9412 if (argvars[3].v_type != VAR_UNKNOWN)
9413 {
9414 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9415 vim_free(tofree);
9416 }
9417 else
9418 {
9419 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009420 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009421 ga_concat(&ga, (char_u *)msg);
9422 }
9423 assert_error(&ga);
9424 ga_clear(&ga);
9425 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009426 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009427 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009428 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009429}
9430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431/*
9432 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009433 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009435 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436assert_bool(typval_T *argvars, int isTrue)
9437{
9438 int error = FALSE;
9439 garray_T ga;
9440
9441 if (argvars[0].v_type == VAR_SPECIAL
9442 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009443 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009444 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009445 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 || error)
9447 {
9448 prepare_assert_error(&ga);
9449 fill_assert_error(&ga, &argvars[1],
9450 (char_u *)(isTrue ? "True" : "False"),
9451 NULL, &argvars[0], ASSERT_OTHER);
9452 assert_error(&ga);
9453 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009454 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009456 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457}
9458
Bram Moolenaar65a54642018-04-28 16:56:53 +02009459 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009460assert_report(typval_T *argvars)
9461{
9462 garray_T ga;
9463
9464 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009465 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009466 assert_error(&ga);
9467 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009468 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009469}
9470
Bram Moolenaar65a54642018-04-28 16:56:53 +02009471 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472assert_exception(typval_T *argvars)
9473{
9474 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009475 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476
9477 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9478 {
9479 prepare_assert_error(&ga);
9480 ga_concat(&ga, (char_u *)"v:exception is not set");
9481 assert_error(&ga);
9482 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009483 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009484 }
9485 else if (error != NULL
9486 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9487 {
9488 prepare_assert_error(&ga);
9489 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9490 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9491 assert_error(&ga);
9492 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009493 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009495 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496}
9497
Bram Moolenaar65a54642018-04-28 16:56:53 +02009498 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009499assert_beeps(typval_T *argvars)
9500{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009501 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009502 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009503 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009504
9505 called_vim_beep = FALSE;
9506 suppress_errthrow = TRUE;
9507 emsg_silent = FALSE;
9508 do_cmdline_cmd(cmd);
9509 if (!called_vim_beep)
9510 {
9511 prepare_assert_error(&ga);
9512 ga_concat(&ga, (char_u *)"command did not beep: ");
9513 ga_concat(&ga, cmd);
9514 assert_error(&ga);
9515 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009516 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009517 }
9518
9519 suppress_errthrow = FALSE;
9520 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009521 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009522}
9523
Bram Moolenaar65a54642018-04-28 16:56:53 +02009524 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009525assert_fails(typval_T *argvars)
9526{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009527 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009529 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009530 char_u numbuf[NUMBUFLEN];
9531 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532
9533 called_emsg = FALSE;
9534 suppress_errthrow = TRUE;
9535 emsg_silent = TRUE;
9536 do_cmdline_cmd(cmd);
9537 if (!called_emsg)
9538 {
9539 prepare_assert_error(&ga);
9540 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009541 if (argvars[1].v_type != VAR_UNKNOWN
9542 && argvars[2].v_type != VAR_UNKNOWN)
9543 {
9544 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9545 vim_free(tofree);
9546 }
9547 else
9548 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549 assert_error(&ga);
9550 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009551 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 }
9553 else if (argvars[1].v_type != VAR_UNKNOWN)
9554 {
9555 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009556 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557
9558 if (error == NULL
9559 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9560 {
9561 prepare_assert_error(&ga);
9562 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9563 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9564 assert_error(&ga);
9565 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009566 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009567 }
9568 }
9569
9570 called_emsg = FALSE;
9571 suppress_errthrow = FALSE;
9572 emsg_silent = FALSE;
9573 emsg_on_display = FALSE;
9574 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009575 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576}
9577
9578/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009579 * Append "p[clen]" to "gap", escaping unprintable characters.
9580 * Changes NL to \n, CR to \r, etc.
9581 */
9582 static void
9583ga_concat_esc(garray_T *gap, char_u *p, int clen)
9584{
9585 char_u buf[NUMBUFLEN];
9586
9587 if (clen > 1)
9588 {
9589 mch_memmove(buf, p, clen);
9590 buf[clen] = NUL;
9591 ga_concat(gap, buf);
9592 }
9593 else switch (*p)
9594 {
9595 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9596 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9597 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9598 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9599 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9600 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9601 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9602 default:
9603 if (*p < ' ')
9604 {
9605 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9606 ga_concat(gap, buf);
9607 }
9608 else
9609 ga_append(gap, *p);
9610 break;
9611 }
9612}
9613
9614/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009615 * Append "str" to "gap", escaping unprintable characters.
9616 * Changes NL to \n, CR to \r, etc.
9617 */
9618 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009619ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620{
9621 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009622 char_u *s;
9623 int c;
9624 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009625 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009626 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627
9628 if (str == NULL)
9629 {
9630 ga_concat(gap, (char_u *)"NULL");
9631 return;
9632 }
9633
9634 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009635 {
9636 same_len = 1;
9637 s = p;
9638 c = mb_ptr2char_adv(&s);
9639 clen = s - p;
9640 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009642 ++same_len;
9643 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009644 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009645 if (same_len > 20)
9646 {
9647 ga_concat(gap, (char_u *)"\\[");
9648 ga_concat_esc(gap, p, clen);
9649 ga_concat(gap, (char_u *)" occurs ");
9650 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9651 ga_concat(gap, buf);
9652 ga_concat(gap, (char_u *)" times]");
9653 p = s - 1;
9654 }
9655 else
9656 ga_concat_esc(gap, p, clen);
9657 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658}
9659
9660/*
9661 * Fill "gap" with information about an assert error.
9662 */
9663 void
9664fill_assert_error(
9665 garray_T *gap,
9666 typval_T *opt_msg_tv,
9667 char_u *exp_str,
9668 typval_T *exp_tv,
9669 typval_T *got_tv,
9670 assert_type_T atype)
9671{
9672 char_u numbuf[NUMBUFLEN];
9673 char_u *tofree;
9674
9675 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9676 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009677 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9678 vim_free(tofree);
9679 ga_concat(gap, (char_u *)": ");
9680 }
9681
9682 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9683 ga_concat(gap, (char_u *)"Pattern ");
9684 else if (atype == ASSERT_NOTEQUAL)
9685 ga_concat(gap, (char_u *)"Expected not equal to ");
9686 else
9687 ga_concat(gap, (char_u *)"Expected ");
9688 if (exp_str == NULL)
9689 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009690 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 vim_free(tofree);
9692 }
9693 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009694 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009695 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009697 if (atype == ASSERT_MATCH)
9698 ga_concat(gap, (char_u *)" does not match ");
9699 else if (atype == ASSERT_NOTMATCH)
9700 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009702 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009703 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009704 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705 }
9706}
9707
Bram Moolenaar31988702018-02-13 12:57:42 +01009708/*
9709 * Compare "typ1" and "typ2". Put the result in "typ1".
9710 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009711 int
9712typval_compare(
9713 typval_T *typ1, /* first operand */
9714 typval_T *typ2, /* second operand */
9715 exptype_T type, /* operator */
9716 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009717 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009718{
9719 int i;
9720 varnumber_T n1, n2;
9721 char_u *s1, *s2;
9722 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9723
Bram Moolenaar31988702018-02-13 12:57:42 +01009724 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009725 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009726 /* For "is" a different type always means FALSE, for "notis"
9727 * it means TRUE. */
9728 n1 = (type == TYPE_NEQUAL);
9729 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009730 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9731 {
9732 if (type_is)
9733 {
9734 n1 = (typ1->v_type == typ2->v_type
9735 && typ1->vval.v_blob == typ2->vval.v_blob);
9736 if (type == TYPE_NEQUAL)
9737 n1 = !n1;
9738 }
9739 else if (typ1->v_type != typ2->v_type
9740 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9741 {
9742 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009743 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009744 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009745 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009746 clear_tv(typ1);
9747 return FAIL;
9748 }
9749 else
9750 {
9751 // Compare two Blobs for being equal or unequal.
9752 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9753 if (type == TYPE_NEQUAL)
9754 n1 = !n1;
9755 }
9756 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009757 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9758 {
9759 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009760 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009761 n1 = (typ1->v_type == typ2->v_type
9762 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009763 if (type == TYPE_NEQUAL)
9764 n1 = !n1;
9765 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009766 else if (typ1->v_type != typ2->v_type
9767 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009768 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009769 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009770 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009771 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009772 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009773 clear_tv(typ1);
9774 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009775 }
9776 else
9777 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009778 /* Compare two Lists for being equal or unequal. */
9779 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9780 ic, FALSE);
9781 if (type == TYPE_NEQUAL)
9782 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009783 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009784 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009785
9786 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9787 {
9788 if (type_is)
9789 {
9790 n1 = (typ1->v_type == typ2->v_type
9791 && typ1->vval.v_dict == typ2->vval.v_dict);
9792 if (type == TYPE_NEQUAL)
9793 n1 = !n1;
9794 }
9795 else if (typ1->v_type != typ2->v_type
9796 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9797 {
9798 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009799 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009800 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009801 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009802 clear_tv(typ1);
9803 return FAIL;
9804 }
9805 else
9806 {
9807 /* Compare two Dictionaries for being equal or unequal. */
9808 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9809 ic, FALSE);
9810 if (type == TYPE_NEQUAL)
9811 n1 = !n1;
9812 }
9813 }
9814
9815 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9816 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9817 {
9818 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009820 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009821 clear_tv(typ1);
9822 return FAIL;
9823 }
9824 if ((typ1->v_type == VAR_PARTIAL
9825 && typ1->vval.v_partial == NULL)
9826 || (typ2->v_type == VAR_PARTIAL
9827 && typ2->vval.v_partial == NULL))
9828 /* when a partial is NULL assume not equal */
9829 n1 = FALSE;
9830 else if (type_is)
9831 {
9832 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9833 /* strings are considered the same if their value is
9834 * the same */
9835 n1 = tv_equal(typ1, typ2, ic, FALSE);
9836 else if (typ1->v_type == VAR_PARTIAL
9837 && typ2->v_type == VAR_PARTIAL)
9838 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9839 else
9840 n1 = FALSE;
9841 }
9842 else
9843 n1 = tv_equal(typ1, typ2, ic, FALSE);
9844 if (type == TYPE_NEQUAL)
9845 n1 = !n1;
9846 }
9847
9848#ifdef FEAT_FLOAT
9849 /*
9850 * If one of the two variables is a float, compare as a float.
9851 * When using "=~" or "!~", always compare as string.
9852 */
9853 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9854 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9855 {
9856 float_T f1, f2;
9857
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009858 f1 = tv_get_float(typ1);
9859 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009860 n1 = FALSE;
9861 switch (type)
9862 {
9863 case TYPE_EQUAL: n1 = (f1 == f2); break;
9864 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9865 case TYPE_GREATER: n1 = (f1 > f2); break;
9866 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9867 case TYPE_SMALLER: n1 = (f1 < f2); break;
9868 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9869 case TYPE_UNKNOWN:
9870 case TYPE_MATCH:
9871 case TYPE_NOMATCH: break; /* avoid gcc warning */
9872 }
9873 }
9874#endif
9875
9876 /*
9877 * If one of the two variables is a number, compare as a number.
9878 * When using "=~" or "!~", always compare as string.
9879 */
9880 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9881 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009883 n1 = tv_get_number(typ1);
9884 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009885 switch (type)
9886 {
9887 case TYPE_EQUAL: n1 = (n1 == n2); break;
9888 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9889 case TYPE_GREATER: n1 = (n1 > n2); break;
9890 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9891 case TYPE_SMALLER: n1 = (n1 < n2); break;
9892 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9893 case TYPE_UNKNOWN:
9894 case TYPE_MATCH:
9895 case TYPE_NOMATCH: break; /* avoid gcc warning */
9896 }
9897 }
9898 else
9899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009900 s1 = tv_get_string_buf(typ1, buf1);
9901 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009902 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9903 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9904 else
9905 i = 0;
9906 n1 = FALSE;
9907 switch (type)
9908 {
9909 case TYPE_EQUAL: n1 = (i == 0); break;
9910 case TYPE_NEQUAL: n1 = (i != 0); break;
9911 case TYPE_GREATER: n1 = (i > 0); break;
9912 case TYPE_GEQUAL: n1 = (i >= 0); break;
9913 case TYPE_SMALLER: n1 = (i < 0); break;
9914 case TYPE_SEQUAL: n1 = (i <= 0); break;
9915
9916 case TYPE_MATCH:
9917 case TYPE_NOMATCH:
9918 n1 = pattern_match(s2, s1, ic);
9919 if (type == TYPE_NOMATCH)
9920 n1 = !n1;
9921 break;
9922
9923 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9924 }
9925 }
9926 clear_tv(typ1);
9927 typ1->v_type = VAR_NUMBER;
9928 typ1->vval.v_number = n1;
9929
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009930 return OK;
9931}
9932
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009933 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009934typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009935{
9936 char_u *tofree;
9937 char_u numbuf[NUMBUFLEN];
9938 char_u *ret = NULL;
9939
9940 if (arg == NULL)
9941 return vim_strsave((char_u *)"(does not exist)");
9942 ret = tv2string(arg, &tofree, numbuf, 0);
9943 /* Make a copy if we have a value but it's not in allocated memory. */
9944 if (ret != NULL && tofree == NULL)
9945 ret = vim_strsave(ret);
9946 return ret;
9947}
9948
9949 int
9950var_exists(char_u *var)
9951{
9952 char_u *name;
9953 char_u *tofree;
9954 typval_T tv;
9955 int len = 0;
9956 int n = FALSE;
9957
9958 /* get_name_len() takes care of expanding curly braces */
9959 name = var;
9960 len = get_name_len(&var, &tofree, TRUE, FALSE);
9961 if (len > 0)
9962 {
9963 if (tofree != NULL)
9964 name = tofree;
9965 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9966 if (n)
9967 {
9968 /* handle d.key, l[idx], f(expr) */
9969 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9970 if (n)
9971 clear_tv(&tv);
9972 }
9973 }
9974 if (*var != NUL)
9975 n = FALSE;
9976
9977 vim_free(tofree);
9978 return n;
9979}
9980
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981#endif /* FEAT_EVAL */
9982
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009984#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985
Bram Moolenaar4f974752019-02-17 17:44:42 +01009986#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987/*
9988 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
9991/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009992 * Get the short path (8.3) for the filename in "fnamep".
9993 * Only works for a valid file name.
9994 * When the path gets longer "fnamep" is changed and the allocated buffer
9995 * is put in "bufp".
9996 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9997 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 */
9999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010000get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010002 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 char_u *newbuf;
10004
10005 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010006 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (l > len - 1)
10008 {
10009 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010010 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 newbuf = vim_strnsave(*fnamep, l);
10012 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
10015 vim_free(*bufp);
10016 *fnamep = *bufp = newbuf;
10017
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010018 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010019 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021
10022 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010023 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024}
10025
10026/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010027 * Get the short path (8.3) for the filename in "fname". The converted
10028 * path is returned in "bufp".
10029 *
10030 * Some of the directories specified in "fname" may not exist. This function
10031 * will shorten the existing directories at the beginning of the path and then
10032 * append the remaining non-existing path.
10033 *
10034 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010035 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010036 * bufp - Pointer to an allocated buffer for the filename.
10037 * fnamelen - Length of the filename pointed to by fname
10038 *
10039 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 */
10041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010042shortpath_for_invalid_fname(
10043 char_u **fname,
10044 char_u **bufp,
10045 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010047 char_u *short_fname, *save_fname, *pbuf_unused;
10048 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010050 int old_len, len;
10051 int new_len, sfx_len;
10052 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010055 old_len = *fnamelen;
10056 save_fname = vim_strnsave(*fname, old_len);
10057 pbuf_unused = NULL;
10058 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010060 endp = save_fname + old_len - 1; /* Find the end of the copy */
10061 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010063 /*
10064 * Try shortening the supplied path till it succeeds by removing one
10065 * directory at a time from the tail of the path.
10066 */
10067 len = 0;
10068 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010070 /* go back one path-separator */
10071 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10072 --endp;
10073 if (endp <= save_fname)
10074 break; /* processed the complete path */
10075
10076 /*
10077 * Replace the path separator with a NUL and try to shorten the
10078 * resulting path.
10079 */
10080 ch = *endp;
10081 *endp = 0;
10082 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010083 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010084 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10085 {
10086 retval = FAIL;
10087 goto theend;
10088 }
10089 *endp = ch; /* preserve the string */
10090
10091 if (len > 0)
10092 break; /* successfully shortened the path */
10093
10094 /* failed to shorten the path. Skip the path separator */
10095 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 }
10097
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010098 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010100 /*
10101 * Succeeded in shortening the path. Now concatenate the shortened
10102 * path with the remaining path at the tail.
10103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010105 /* Compute the length of the new path. */
10106 sfx_len = (int)(save_endp - endp) + 1;
10107 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010109 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010111 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010113 /* There is not enough space in the currently allocated string,
10114 * copy it to a buffer big enough. */
10115 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010117 {
10118 retval = FAIL;
10119 goto theend;
10120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 }
10122 else
10123 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010124 /* Transfer short_fname to the main buffer (it's big enough),
10125 * unless get_short_pathname() did its work in-place. */
10126 *fname = *bufp = save_fname;
10127 if (short_fname != save_fname)
10128 vim_strncpy(save_fname, short_fname, len);
10129 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010131
10132 /* concat the not-shortened part of the path */
10133 vim_strncpy(*fname + len, endp, sfx_len);
10134 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010136
10137theend:
10138 vim_free(pbuf_unused);
10139 vim_free(save_fname);
10140
10141 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142}
10143
10144/*
10145 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010146 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 */
10148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010149shortpath_for_partial(
10150 char_u **fnamep,
10151 char_u **bufp,
10152 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 int sepcount, len, tflen;
10155 char_u *p;
10156 char_u *pbuf, *tfname;
10157 int hasTilde;
10158
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010159 /* Count up the path separators from the RHS.. so we know which part
10160 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010162 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (vim_ispathsep(*p))
10164 ++sepcount;
10165
10166 /* Need full path first (use expand_env() to remove a "~/") */
10167 hasTilde = (**fnamep == '~');
10168 if (hasTilde)
10169 pbuf = tfname = expand_env_save(*fnamep);
10170 else
10171 pbuf = tfname = FullName_save(*fnamep, FALSE);
10172
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010173 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010175 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177
10178 if (len == 0)
10179 {
10180 /* Don't have a valid filename, so shorten the rest of the
10181 * path if we can. This CAN give us invalid 8.3 filenames, but
10182 * there's not a lot of point in guessing what it might be.
10183 */
10184 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010185 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 }
10188
10189 /* Count the paths backward to find the beginning of the desired string. */
10190 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010191 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010192 if (has_mbyte)
10193 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (vim_ispathsep(*p))
10195 {
10196 if (sepcount == 0 || (hasTilde && sepcount == 1))
10197 break;
10198 else
10199 sepcount --;
10200 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 if (hasTilde)
10203 {
10204 --p;
10205 if (p >= tfname)
10206 *p = '~';
10207 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 }
10210 else
10211 ++p;
10212
10213 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10214 vim_free(*bufp);
10215 *fnamelen = (int)STRLEN(p);
10216 *bufp = pbuf;
10217 *fnamep = p;
10218
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010219 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010221#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
10223/*
10224 * Adjust a filename, according to a string of modifiers.
10225 * *fnamep must be NUL terminated when called. When returning, the length is
10226 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010227 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 * When there is an error, *fnamep is set to NULL.
10229 */
10230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010232 char_u *src, // string with modifiers
10233 int tilde_file, // "~" is a file name, not $HOME
10234 int *usedlen, // characters after src that are used
10235 char_u **fnamep, // file name so far
10236 char_u **bufp, // buffer for allocated file name or NULL
10237 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238{
10239 int valid = 0;
10240 char_u *tail;
10241 char_u *s, *p, *pbuf;
10242 char_u dirname[MAXPATHL];
10243 int c;
10244 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010245#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010246 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 int has_shortname = 0;
10248#endif
10249
10250repeat:
10251 /* ":p" - full path/file_name */
10252 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10253 {
10254 has_fullname = 1;
10255
10256 valid |= VALID_PATH;
10257 *usedlen += 2;
10258
10259 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10260 if ((*fnamep)[0] == '~'
10261#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10262 && ((*fnamep)[1] == '/'
10263# ifdef BACKSLASH_IN_FILENAME
10264 || (*fnamep)[1] == '\\'
10265# endif
10266 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010268 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 )
10270 {
10271 *fnamep = expand_env_save(*fnamep);
10272 vim_free(*bufp); /* free any allocated file name */
10273 *bufp = *fnamep;
10274 if (*fnamep == NULL)
10275 return -1;
10276 }
10277
10278 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010279 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 {
10281 if (vim_ispathsep(*p)
10282 && p[1] == '.'
10283 && (p[2] == NUL
10284 || vim_ispathsep(p[2])
10285 || (p[2] == '.'
10286 && (p[3] == NUL || vim_ispathsep(p[3])))))
10287 break;
10288 }
10289
10290 /* FullName_save() is slow, don't use it when not needed. */
10291 if (*p != NUL || !vim_isAbsName(*fnamep))
10292 {
10293 *fnamep = FullName_save(*fnamep, *p != NUL);
10294 vim_free(*bufp); /* free any allocated file name */
10295 *bufp = *fnamep;
10296 if (*fnamep == NULL)
10297 return -1;
10298 }
10299
Bram Moolenaar4f974752019-02-17 17:44:42 +010010300#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010301# if _WIN32_WINNT >= 0x0500
10302 if (vim_strchr(*fnamep, '~') != NULL)
10303 {
10304 /* Expand 8.3 filename to full path. Needed to make sure the same
10305 * file does not have two different names.
10306 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10307 p = alloc(_MAX_PATH + 1);
10308 if (p != NULL)
10309 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010310 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010311 {
10312 vim_free(*bufp);
10313 *bufp = *fnamep = p;
10314 }
10315 else
10316 vim_free(p);
10317 }
10318 }
10319# endif
10320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 /* Append a path separator to a directory. */
10322 if (mch_isdir(*fnamep))
10323 {
10324 /* Make room for one or two extra characters. */
10325 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10326 vim_free(*bufp); /* free any allocated file name */
10327 *bufp = *fnamep;
10328 if (*fnamep == NULL)
10329 return -1;
10330 add_pathsep(*fnamep);
10331 }
10332 }
10333
10334 /* ":." - path relative to the current directory */
10335 /* ":~" - path relative to the home directory */
10336 /* ":8" - shortname path - postponed till after */
10337 while (src[*usedlen] == ':'
10338 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10339 {
10340 *usedlen += 2;
10341 if (c == '8')
10342 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010343#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 has_shortname = 1; /* Postpone this. */
10345#endif
10346 continue;
10347 }
10348 pbuf = NULL;
10349 /* Need full path first (use expand_env() to remove a "~/") */
10350 if (!has_fullname)
10351 {
10352 if (c == '.' && **fnamep == '~')
10353 p = pbuf = expand_env_save(*fnamep);
10354 else
10355 p = pbuf = FullName_save(*fnamep, FALSE);
10356 }
10357 else
10358 p = *fnamep;
10359
10360 has_fullname = 0;
10361
10362 if (p != NULL)
10363 {
10364 if (c == '.')
10365 {
10366 mch_dirname(dirname, MAXPATHL);
10367 s = shorten_fname(p, dirname);
10368 if (s != NULL)
10369 {
10370 *fnamep = s;
10371 if (pbuf != NULL)
10372 {
10373 vim_free(*bufp); /* free any allocated file name */
10374 *bufp = pbuf;
10375 pbuf = NULL;
10376 }
10377 }
10378 }
10379 else
10380 {
10381 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10382 /* Only replace it when it starts with '~' */
10383 if (*dirname == '~')
10384 {
10385 s = vim_strsave(dirname);
10386 if (s != NULL)
10387 {
10388 *fnamep = s;
10389 vim_free(*bufp);
10390 *bufp = s;
10391 }
10392 }
10393 }
10394 vim_free(pbuf);
10395 }
10396 }
10397
10398 tail = gettail(*fnamep);
10399 *fnamelen = (int)STRLEN(*fnamep);
10400
10401 /* ":h" - head, remove "/file_name", can be repeated */
10402 /* Don't remove the first "/" or "c:\" */
10403 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10404 {
10405 valid |= VALID_HEAD;
10406 *usedlen += 2;
10407 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010408 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010409 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 *fnamelen = (int)(tail - *fnamep);
10411#ifdef VMS
10412 if (*fnamelen > 0)
10413 *fnamelen += 1; /* the path separator is part of the path */
10414#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010415 if (*fnamelen == 0)
10416 {
10417 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10418 p = vim_strsave((char_u *)".");
10419 if (p == NULL)
10420 return -1;
10421 vim_free(*bufp);
10422 *bufp = *fnamep = tail = p;
10423 *fnamelen = 1;
10424 }
10425 else
10426 {
10427 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010428 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 }
10431
10432 /* ":8" - shortname */
10433 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10434 {
10435 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010436#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 has_shortname = 1;
10438#endif
10439 }
10440
Bram Moolenaar4f974752019-02-17 17:44:42 +010010441#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010442 /*
10443 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 */
10445 if (has_shortname)
10446 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010447 /* Copy the string if it is shortened by :h and when it wasn't copied
10448 * yet, because we are going to change it in place. Avoids changing
10449 * the buffer name for "%:8". */
10450 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 {
10452 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010453 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 return -1;
10455 vim_free(*bufp);
10456 *bufp = *fnamep = p;
10457 }
10458
10459 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010460 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 if (!has_fullname && !vim_isAbsName(*fnamep))
10462 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010463 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 return -1;
10465 }
10466 else
10467 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010468 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469
Bram Moolenaardc935552011-08-17 15:23:23 +020010470 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010472 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 return -1;
10474
10475 if (l == 0)
10476 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010477 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010479 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 return -1;
10481 }
10482 *fnamelen = l;
10483 }
10484 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010485#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
10487 /* ":t" - tail, just the basename */
10488 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10489 {
10490 *usedlen += 2;
10491 *fnamelen -= (int)(tail - *fnamep);
10492 *fnamep = tail;
10493 }
10494
10495 /* ":e" - extension, can be repeated */
10496 /* ":r" - root, without extension, can be repeated */
10497 while (src[*usedlen] == ':'
10498 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10499 {
10500 /* find a '.' in the tail:
10501 * - for second :e: before the current fname
10502 * - otherwise: The last '.'
10503 */
10504 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10505 s = *fnamep - 2;
10506 else
10507 s = *fnamep + *fnamelen - 1;
10508 for ( ; s > tail; --s)
10509 if (s[0] == '.')
10510 break;
10511 if (src[*usedlen + 1] == 'e') /* :e */
10512 {
10513 if (s > tail)
10514 {
10515 *fnamelen += (int)(*fnamep - (s + 1));
10516 *fnamep = s + 1;
10517#ifdef VMS
10518 /* cut version from the extension */
10519 s = *fnamep + *fnamelen - 1;
10520 for ( ; s > *fnamep; --s)
10521 if (s[0] == ';')
10522 break;
10523 if (s > *fnamep)
10524 *fnamelen = s - *fnamep;
10525#endif
10526 }
10527 else if (*fnamep <= tail)
10528 *fnamelen = 0;
10529 }
10530 else /* :r */
10531 {
10532 if (s > tail) /* remove one extension */
10533 *fnamelen = (int)(s - *fnamep);
10534 }
10535 *usedlen += 2;
10536 }
10537
10538 /* ":s?pat?foo?" - substitute */
10539 /* ":gs?pat?foo?" - global substitute */
10540 if (src[*usedlen] == ':'
10541 && (src[*usedlen + 1] == 's'
10542 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10543 {
10544 char_u *str;
10545 char_u *pat;
10546 char_u *sub;
10547 int sep;
10548 char_u *flags;
10549 int didit = FALSE;
10550
10551 flags = (char_u *)"";
10552 s = src + *usedlen + 2;
10553 if (src[*usedlen + 1] == 'g')
10554 {
10555 flags = (char_u *)"g";
10556 ++s;
10557 }
10558
10559 sep = *s++;
10560 if (sep)
10561 {
10562 /* find end of pattern */
10563 p = vim_strchr(s, sep);
10564 if (p != NULL)
10565 {
10566 pat = vim_strnsave(s, (int)(p - s));
10567 if (pat != NULL)
10568 {
10569 s = p + 1;
10570 /* find end of substitution */
10571 p = vim_strchr(s, sep);
10572 if (p != NULL)
10573 {
10574 sub = vim_strnsave(s, (int)(p - s));
10575 str = vim_strnsave(*fnamep, *fnamelen);
10576 if (sub != NULL && str != NULL)
10577 {
10578 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010579 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 if (s != NULL)
10581 {
10582 *fnamep = s;
10583 *fnamelen = (int)STRLEN(s);
10584 vim_free(*bufp);
10585 *bufp = s;
10586 didit = TRUE;
10587 }
10588 }
10589 vim_free(sub);
10590 vim_free(str);
10591 }
10592 vim_free(pat);
10593 }
10594 }
10595 /* after using ":s", repeat all the modifiers */
10596 if (didit)
10597 goto repeat;
10598 }
10599 }
10600
Bram Moolenaar26df0922014-02-23 23:39:13 +010010601 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10602 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010603 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010604 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010605 if (c != NUL)
10606 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010607 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010608 if (c != NUL)
10609 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010610 if (p == NULL)
10611 return -1;
10612 vim_free(*bufp);
10613 *bufp = *fnamep = p;
10614 *fnamelen = (int)STRLEN(p);
10615 *usedlen += 2;
10616 }
10617
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 return valid;
10619}
10620
10621/*
10622 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010623 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 * "flags" can be "g" to do a global substitute.
10625 * Returns an allocated string, NULL for error.
10626 */
10627 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010628do_string_sub(
10629 char_u *str,
10630 char_u *pat,
10631 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010632 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010633 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
10635 int sublen;
10636 regmatch_T regmatch;
10637 int i;
10638 int do_all;
10639 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010640 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 garray_T ga;
10642 char_u *ret;
10643 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010644 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645
10646 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10647 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010648 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649
10650 ga_init2(&ga, 1, 200);
10651
10652 do_all = (flags[0] == 'g');
10653
10654 regmatch.rm_ic = p_ic;
10655 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10656 if (regmatch.regprog != NULL)
10657 {
10658 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010659 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10661 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010662 /* Skip empty match except for first match. */
10663 if (regmatch.startp[0] == regmatch.endp[0])
10664 {
10665 if (zero_width == regmatch.startp[0])
10666 {
10667 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010668 i = MB_PTR2LEN(tail);
10669 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10670 (size_t)i);
10671 ga.ga_len += i;
10672 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010673 continue;
10674 }
10675 zero_width = regmatch.startp[0];
10676 }
10677
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 /*
10679 * Get some space for a temporary buffer to do the substitution
10680 * into. It will contain:
10681 * - The text up to where the match is.
10682 * - The substituted text.
10683 * - The text after the match.
10684 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010685 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010686 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10688 {
10689 ga_clear(&ga);
10690 break;
10691 }
10692
10693 /* copy the text up to where the match is */
10694 i = (int)(regmatch.startp[0] - tail);
10695 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10696 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010697 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 + ga.ga_len + i, TRUE, TRUE, FALSE);
10699 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010700 tail = regmatch.endp[0];
10701 if (*tail == NUL)
10702 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 if (!do_all)
10704 break;
10705 }
10706
10707 if (ga.ga_data != NULL)
10708 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10709
Bram Moolenaar473de612013-06-08 18:19:48 +020010710 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 }
10712
10713 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10714 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010715 if (p_cpo == empty_option)
10716 p_cpo = save_cpo;
10717 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010718 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010719 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720
10721 return ret;
10722}
10723
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010724 static int
10725filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10726{
10727 typval_T rettv;
10728 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010729 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730
10731 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10732 argv[0] = vimvars[VV_KEY].vv_tv;
10733 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010734 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10735 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 if (map)
10737 {
10738 /* map(): replace the list item value */
10739 clear_tv(tv);
10740 rettv.v_lock = 0;
10741 *tv = rettv;
10742 }
10743 else
10744 {
10745 int error = FALSE;
10746
10747 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010748 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010749 clear_tv(&rettv);
10750 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010751 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 if (error)
10753 goto theend;
10754 }
10755 retval = OK;
10756theend:
10757 clear_tv(&vimvars[VV_VAL].vv_tv);
10758 return retval;
10759}
10760
10761
10762/*
10763 * Implementation of map() and filter().
10764 */
10765 void
10766filter_map(typval_T *argvars, typval_T *rettv, int map)
10767{
10768 typval_T *expr;
10769 listitem_T *li, *nli;
10770 list_T *l = NULL;
10771 dictitem_T *di;
10772 hashtab_T *ht;
10773 hashitem_T *hi;
10774 dict_T *d = NULL;
10775 typval_T save_val;
10776 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010777 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 int rem;
10779 int todo;
10780 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10781 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10782 : N_("filter() argument"));
10783 int save_did_emsg;
10784 int idx = 0;
10785
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010786 if (argvars[0].v_type == VAR_BLOB)
10787 {
10788 if ((b = argvars[0].vval.v_blob) == NULL)
10789 return;
10790 }
10791 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 {
10793 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010794 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010795 return;
10796 }
10797 else if (argvars[0].v_type == VAR_DICT)
10798 {
10799 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010800 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010801 return;
10802 }
10803 else
10804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010805 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010806 return;
10807 }
10808
10809 expr = &argvars[1];
10810 /* On type errors, the preceding call has already displayed an error
10811 * message. Avoid a misleading error message for an empty string that
10812 * was not passed as argument. */
10813 if (expr->v_type != VAR_UNKNOWN)
10814 {
10815 prepare_vimvar(VV_VAL, &save_val);
10816
10817 /* We reset "did_emsg" to be able to detect whether an error
10818 * occurred during evaluation of the expression. */
10819 save_did_emsg = did_emsg;
10820 did_emsg = FALSE;
10821
10822 prepare_vimvar(VV_KEY, &save_key);
10823 if (argvars[0].v_type == VAR_DICT)
10824 {
10825 vimvars[VV_KEY].vv_type = VAR_STRING;
10826
10827 ht = &d->dv_hashtab;
10828 hash_lock(ht);
10829 todo = (int)ht->ht_used;
10830 for (hi = ht->ht_array; todo > 0; ++hi)
10831 {
10832 if (!HASHITEM_EMPTY(hi))
10833 {
10834 int r;
10835
10836 --todo;
10837 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010838 if (map && (var_check_lock(di->di_tv.v_lock,
10839 arg_errmsg, TRUE)
10840 || var_check_ro(di->di_flags,
10841 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 break;
10843 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10844 r = filter_map_one(&di->di_tv, expr, map, &rem);
10845 clear_tv(&vimvars[VV_KEY].vv_tv);
10846 if (r == FAIL || did_emsg)
10847 break;
10848 if (!map && rem)
10849 {
10850 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10851 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10852 break;
10853 dictitem_remove(d, di);
10854 }
10855 }
10856 }
10857 hash_unlock(ht);
10858 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010859 else if (argvars[0].v_type == VAR_BLOB)
10860 {
10861 int i;
10862 typval_T tv;
10863
10864 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10865 for (i = 0; i < b->bv_ga.ga_len; i++)
10866 {
10867 tv.v_type = VAR_NUMBER;
10868 tv.vval.v_number = blob_get(b, i);
10869 vimvars[VV_KEY].vv_nr = idx;
10870 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10871 break;
10872 if (tv.v_type != VAR_NUMBER)
10873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010874 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010875 return;
10876 }
10877 tv.v_type = VAR_NUMBER;
10878 blob_set(b, i, tv.vval.v_number);
10879 if (!map && rem)
10880 {
10881 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10882
10883 mch_memmove(p + idx, p + i + 1,
10884 (size_t)b->bv_ga.ga_len - i - 1);
10885 --b->bv_ga.ga_len;
10886 --i;
10887 }
10888 }
10889 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 else
10891 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010892 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10894
10895 for (li = l->lv_first; li != NULL; li = nli)
10896 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010897 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898 break;
10899 nli = li->li_next;
10900 vimvars[VV_KEY].vv_nr = idx;
10901 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10902 || did_emsg)
10903 break;
10904 if (!map && rem)
10905 listitem_remove(l, li);
10906 ++idx;
10907 }
10908 }
10909
10910 restore_vimvar(VV_KEY, &save_key);
10911 restore_vimvar(VV_VAL, &save_val);
10912
10913 did_emsg |= save_did_emsg;
10914 }
10915
10916 copy_tv(&argvars[0], rettv);
10917}
10918
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */