blob: 835e44ccea366ff36814478119c21d71cb90bf34 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
248static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
249static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252/* for VIM_VERSION_ defines */
253#include "version.h"
254
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100255
256#if defined(EBCDIC) || defined(PROTO)
257/*
258 * Compare struct fst by function name.
259 */
260 static int
261compare_func_name(const void *s1, const void *s2)
262{
263 struct fst *p1 = (struct fst *)s1;
264 struct fst *p2 = (struct fst *)s2;
265
266 return STRCMP(p1->f_name, p2->f_name);
267}
268
269/*
270 * Sort the function table by function name.
271 * The sorting of the table above is ASCII dependant.
272 * On machines using EBCDIC we have to sort it.
273 */
274 static void
275sortFunctions(void)
276{
277 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
278
279 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
280}
281#endif
282
283
Bram Moolenaar33570922005-01-25 22:26:29 +0000284/*
285 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000286 */
287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100288eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000289{
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 int i;
291 struct vimvar *p;
292
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200293 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
294 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200295 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000296 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000298
299 for (i = 0; i < VV_LEN; ++i)
300 {
301 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 if (STRLEN(p->vv_name) > 16)
303 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100304 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200305 getout(1);
306 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 STRCPY(p->vv_di.di_key, p->vv_name);
308 if (p->vv_flags & VV_RO)
309 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
310 else if (p->vv_flags & VV_RO_SBX)
311 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
312 else
313 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000314
315 /* add to v: scope dict, unless the value is not always available */
316 if (p->vv_type != VAR_UNKNOWN)
317 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000319 /* add to compat scope dict */
320 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000321 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100322 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
323
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000324 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100325 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100326 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100327 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100328 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329
330 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
331 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
332 set_vim_var_nr(VV_NONE, VVAL_NONE);
333 set_vim_var_nr(VV_NULL, VVAL_NULL);
334
Bram Moolenaarf562e722016-07-19 17:25:25 +0200335 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
336 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
337 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
338 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
339 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
340 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
341 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
342 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
343 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
344 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100345 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200346
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200347 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348
349#ifdef EBCDIC
350 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100351 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200352 */
353 sortFunctions();
354#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000355}
356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000357#if defined(EXITFREE) || defined(PROTO)
358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100359eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000360{
361 int i;
362 struct vimvar *p;
363
364 for (i = 0; i < VV_LEN; ++i)
365 {
366 p = &vimvars[i];
367 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100368 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000369 else if (p->vv_di.di_tv.v_type == VAR_LIST)
370 {
371 list_unref(p->vv_list);
372 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000373 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 }
375 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000376 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000377 hash_clear(&compat_hashtab);
378
Bram Moolenaard9fba312005-06-26 22:34:35 +0000379 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100380# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200381 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100382# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000383
384 /* global variables */
385 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000386
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000387 /* autoloaded script names */
388 ga_clear_strings(&ga_loaded);
389
Bram Moolenaarcca74132013-09-25 21:00:28 +0200390 /* Script-local variables. First clear all the variables and in a second
391 * loop free the scriptvar_T, because a variable in one script might hold
392 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200395 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200396 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200397 ga_clear(&ga_scripts);
398
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000399 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200400 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000401
402 /* functions */
403 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000404}
405#endif
406
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * Set an internal variable to a string value. Creates the variable if it does
410 * not already exist.
411 */
412 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100413set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000415 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000416 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418 val = vim_strsave(value);
419 if (val != NULL)
420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000422 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000424 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427 }
428}
429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200431#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000432static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000433static char_u *redir_endp = NULL;
434static char_u *redir_varname = NULL;
435
436/*
437 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439 * Returns OK if successfully completed the setup. FAIL otherwise.
440 */
441 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100442var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000443{
444 int save_emsg;
445 int err;
446 typval_T tv;
447
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000448 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000449 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000450 {
451 EMSG(_(e_invarg));
452 return FAIL;
453 }
454
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000455 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000456 redir_varname = vim_strsave(name);
457 if (redir_varname == NULL)
458 return FAIL;
459
460 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
461 if (redir_lval == NULL)
462 {
463 var_redir_stop();
464 return FAIL;
465 }
466
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000467 /* The output is stored in growarray "redir_ga" until redirection ends. */
468 ga_init2(&redir_ga, (int)sizeof(char), 500);
469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100471 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000472 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
474 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200475 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476 if (redir_endp != NULL && *redir_endp != NUL)
477 /* Trailing characters are present after the variable name */
478 EMSG(_(e_trailing));
479 else
480 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000481 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482 var_redir_stop();
483 return FAIL;
484 }
485
486 /* check if we can write to the variable: set it to or append an empty
487 * string */
488 save_emsg = did_emsg;
489 did_emsg = FALSE;
490 tv.v_type = VAR_STRING;
491 tv.vval.v_string = (char_u *)"";
492 if (append)
493 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
494 else
495 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200496 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000498 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 if (err)
500 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000501 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 var_redir_stop();
503 return FAIL;
504 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000505
506 return OK;
507}
508
509/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000510 * Append "value[value_len]" to the variable set by var_redir_start().
511 * The actual appending is postponed until redirection ends, because the value
512 * appended may in fact be the string we write to, changing it may cause freed
513 * memory to be used:
514 * :redir => foo
515 * :let foo
516 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 */
518 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100519var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000521 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522
523 if (redir_lval == NULL)
524 return;
525
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000526 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000529 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000530
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 {
533 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000534 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000535 }
536 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538}
539
540/*
541 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000547 typval_T tv;
548
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200549 if (EVALCMD_BUSY)
550 {
551 redir_lval = NULL;
552 return;
553 }
554
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000555 if (redir_lval != NULL)
556 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000557 /* If there was no error: assign the text to the variable. */
558 if (redir_endp != NULL)
559 {
560 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
561 tv.v_type = VAR_STRING;
562 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 /* Call get_lval() again, if it's inside a Dict or List it may
564 * have changed. */
565 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100566 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200567 if (redir_endp != NULL && redir_lval->ll_name != NULL)
568 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
569 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000570 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000571
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000572 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574
Bram Moolenaard23a8232018-02-10 18:45:26 +0100575 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100577 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100689 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaar48570482017-10-30 21:48:41 +0100699 static int
700eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
701{
702 char_u *s;
703 int dummy;
704 char_u buf[NUMBUFLEN];
705
706 if (expr->v_type == VAR_FUNC)
707 {
708 s = expr->vval.v_string;
709 if (s == NULL || *s == NUL)
710 return FAIL;
711 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
712 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
713 return FAIL;
714 }
715 else if (expr->v_type == VAR_PARTIAL)
716 {
717 partial_T *partial = expr->vval.v_partial;
718
719 s = partial_name(partial);
720 if (s == NULL || *s == NUL)
721 return FAIL;
722 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
723 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
724 return FAIL;
725 }
726 else
727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100728 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100729 if (s == NULL)
730 return FAIL;
731 s = skipwhite(s);
732 if (eval1(&s, rettv, TRUE) == FAIL)
733 return FAIL;
734 if (*s != NUL) /* check for trailing chars after expr */
735 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200736 clear_tv(rettv);
Bram Moolenaar48570482017-10-30 21:48:41 +0100737 EMSG2(_(e_invexpr2), s);
738 return FAIL;
739 }
740 }
741 return OK;
742}
743
744/*
745 * Like eval_to_bool() but using a typval_T instead of a string.
746 * Works for string, funcref and partial.
747 */
748 int
749eval_expr_to_bool(typval_T *expr, int *error)
750{
751 typval_T rettv;
752 int res;
753
754 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
755 {
756 *error = TRUE;
757 return FALSE;
758 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100759 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100760 clear_tv(&rettv);
761 return res;
762}
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764/*
765 * Top level evaluation function, returning a string. If "skip" is TRUE,
766 * only parsing to "nextcmd" is done, without reporting errors. Return
767 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
768 */
769 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100770eval_to_string_skip(
771 char_u *arg,
772 char_u **nextcmd,
773 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 char_u *retval;
777
778 if (skip)
779 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000780 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 retval = NULL;
782 else
783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100784 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 if (skip)
788 --emsg_skip;
789
790 return retval;
791}
792
793/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000794 * Skip over an expression at "*pp".
795 * Return FAIL for an error, OK otherwise.
796 */
797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100798skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000801
802 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000804}
805
806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000808 * When "convert" is TRUE convert a List into a sequence of lines and convert
809 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * Return pointer to allocated memory, or NULL for failure.
811 */
812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813eval_to_string(
814 char_u *arg,
815 char_u **nextcmd,
816 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000820 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000821#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000822 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 retval = NULL;
827 else
828 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000829 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000830 {
831 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000832 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200833 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200834 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200835 if (tv.vval.v_list->lv_len > 0)
836 ga_append(&ga, NL);
837 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838 ga_append(&ga, NUL);
839 retval = (char_u *)ga.ga_data;
840 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000841#ifdef FEAT_FLOAT
842 else if (convert && tv.v_type == VAR_FLOAT)
843 {
844 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
845 retval = vim_strsave(numbuf);
846 }
847#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000848 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100849 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852
853 return retval;
854}
855
856/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000857 * Call eval_to_string() without using current local variables and using
858 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100861eval_to_string_safe(
862 char_u *arg,
863 char_u **nextcmd,
864 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200867 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200869 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000870 if (use_sandbox)
871 ++sandbox;
872 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000873 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000874 if (use_sandbox)
875 --sandbox;
876 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200877 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 return retval;
879}
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881/*
882 * Top level evaluation function, returning a number.
883 * Evaluates "expr" silently.
884 * Returns -1 for an error.
885 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200886 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100887eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
Bram Moolenaar33570922005-01-25 22:26:29 +0000889 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200890 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000891 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 ++emsg_off;
894
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 retval = -1;
897 else
898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100899 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 --emsg_off;
903
904 return retval;
905}
906
Bram Moolenaara40058a2005-07-11 22:42:07 +0000907/*
908 * Prepare v: variable "idx" to be used.
909 * Save the current typeval in "save_tv".
910 * When not used yet add the variable to the v: hashtable.
911 */
912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100913prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000914{
915 *save_tv = vimvars[idx].vv_tv;
916 if (vimvars[idx].vv_type == VAR_UNKNOWN)
917 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
918}
919
920/*
921 * Restore v: variable "idx" to typeval "save_tv".
922 * When no longer defined, remove the variable from the v: hashtable.
923 */
924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000926{
927 hashitem_T *hi;
928
Bram Moolenaara40058a2005-07-11 22:42:07 +0000929 vimvars[idx].vv_tv = *save_tv;
930 if (vimvars[idx].vv_type == VAR_UNKNOWN)
931 {
932 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
933 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100934 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000935 else
936 hash_remove(&vimvarht, hi);
937 }
938}
939
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000940#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000941/*
942 * Evaluate an expression to a list with suggestions.
943 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000944 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000945 */
946 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100947eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000948{
949 typval_T save_val;
950 typval_T rettv;
951 list_T *list = NULL;
952 char_u *p = skipwhite(expr);
953
954 /* Set "v:val" to the bad word. */
955 prepare_vimvar(VV_VAL, &save_val);
956 vimvars[VV_VAL].vv_type = VAR_STRING;
957 vimvars[VV_VAL].vv_str = badword;
958 if (p_verbose == 0)
959 ++emsg_off;
960
961 if (eval1(&p, &rettv, TRUE) == OK)
962 {
963 if (rettv.v_type != VAR_LIST)
964 clear_tv(&rettv);
965 else
966 list = rettv.vval.v_list;
967 }
968
969 if (p_verbose == 0)
970 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000971 restore_vimvar(VV_VAL, &save_val);
972
973 return list;
974}
975
976/*
977 * "list" is supposed to contain two items: a word and a number. Return the
978 * word in "pp" and the number as the return value.
979 * Return -1 if anything isn't right.
980 * Used to get the good word and score from the eval_spell_expr() result.
981 */
982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000984{
985 listitem_T *li;
986
987 li = list->lv_first;
988 if (li == NULL)
989 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100990 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000991
992 li = li->li_next;
993 if (li == NULL)
994 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000996}
997#endif
998
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000999/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001000 * Top level evaluation function.
1001 * Returns an allocated typval_T with the result.
1002 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001003 */
1004 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001006{
1007 typval_T *tv;
1008
1009 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001010 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001011 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001012
1013 return tv;
1014}
1015
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001018 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001019 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1020 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001021 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001024call_vim_function(
1025 char_u *func,
1026 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001027 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001028 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001031 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001033 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001034 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001036 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001037 if (ret == FAIL)
1038 clear_tv(rettv);
1039
1040 return ret;
1041}
1042
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001043/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001044 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001045 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001046 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1047 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001048 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001049 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050call_func_retnr(
1051 char_u *func,
1052 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001053 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001054{
1055 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001056 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001057
Bram Moolenaarded27a12018-08-01 19:06:03 +02001058 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001059 return -1;
1060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001062 clear_tv(&rettv);
1063 return retval;
1064}
1065
1066#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1067 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1068
Bram Moolenaar4f688582007-07-24 12:34:30 +00001069# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001070/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001071 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001072 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001073 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1074 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001075 */
1076 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001077call_func_retstr(
1078 char_u *func,
1079 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001080 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001081{
1082 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001083 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084
Bram Moolenaarded27a12018-08-01 19:06:03 +02001085 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 return NULL;
1087
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001088 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 return retval;
1091}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001092# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001094/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001095 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001096 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1097 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001098 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 */
1100 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001101call_func_retlist(
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;
1107
Bram Moolenaarded27a12018-08-01 19:06:03 +02001108 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109 return NULL;
1110
1111 if (rettv.v_type != VAR_LIST)
1112 {
1113 clear_tv(&rettv);
1114 return NULL;
1115 }
1116
1117 return rettv.vval.v_list;
1118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
1120
Bram Moolenaar05159a02005-02-26 23:04:13 +00001121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#ifdef FEAT_FOLDING
1123/*
1124 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1125 * it in "*cp". Doesn't give error messages.
1126 */
1127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001128eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001131 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001133 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1134 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001137 if (use_sandbox)
1138 ++sandbox;
1139 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 retval = 0;
1143 else
1144 {
1145 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 if (tv.v_type == VAR_NUMBER)
1147 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001148 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 retval = 0;
1150 else
1151 {
1152 /* If the result is a string, check if there is a non-digit before
1153 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!VIM_ISDIGIT(*s) && *s != '-')
1156 *cp = *s++;
1157 retval = atol((char *)s);
1158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001162 if (use_sandbox)
1163 --sandbox;
1164 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001166 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167}
1168#endif
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 * ":let" list all variable values
1172 * ":let var1 var2" list variable values
1173 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001174 * ":let var += expr" assignment command.
1175 * ":let var -= expr" assignment command.
1176 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001177 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 */
1179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001180ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001186 int var_count = 0;
1187 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001189 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001190 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaardb552d602006-03-23 22:59:57 +00001192 argend = skip_var_list(arg, &var_count, &semicolon);
1193 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001194 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001195 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1196 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001197 expr = skipwhite(argend);
1198 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1199 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001201 /*
1202 * ":let" without "=": list variables
1203 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001204 if (*arg == '[')
1205 EMSG(_(e_invarg));
1206 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001208 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001212 list_glob_vars(&first);
1213 list_buf_vars(&first);
1214 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001215 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001216 list_script_vars(&first);
1217 list_func_vars(&first);
1218 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 eap->nextcmd = check_nextcmd(arg);
1221 }
1222 else
1223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 op[0] = '=';
1225 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001226 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001227 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001228 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1229 op[0] = *expr; /* +=, -= or .= */
1230 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001231 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001232 else
1233 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (eap->skip)
1236 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001237 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (eap->skip)
1239 {
1240 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001241 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 --emsg_skip;
1243 }
1244 else if (i != FAIL)
1245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001246 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001248 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 }
1251}
1252
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001253/*
1254 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1255 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001256 * When "nextchars" is not NULL it points to a string with characters that
1257 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1258 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259 * Returns OK or FAIL;
1260 */
1261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001262ex_let_vars(
1263 char_u *arg_start,
1264 typval_T *tv,
1265 int copy, /* copy values from "tv", don't move */
1266 int semicolon, /* from skip_var_list() */
1267 int var_count, /* from skip_var_list() */
1268 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001269{
1270 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001272 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 listitem_T *item;
1274 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001275
1276 if (*arg != '[')
1277 {
1278 /*
1279 * ":let var = expr" or ":for var in list"
1280 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001282 return FAIL;
1283 return OK;
1284 }
1285
1286 /*
1287 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1288 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001289 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001290 {
1291 EMSG(_(e_listreq));
1292 return FAIL;
1293 }
1294
1295 i = list_len(l);
1296 if (semicolon == 0 && var_count < i)
1297 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001298 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 return FAIL;
1300 }
1301 if (var_count - semicolon > i)
1302 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001303 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001304 return FAIL;
1305 }
1306
1307 item = l->lv_first;
1308 while (*arg != ']')
1309 {
1310 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001311 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001312 item = item->li_next;
1313 if (arg == NULL)
1314 return FAIL;
1315
1316 arg = skipwhite(arg);
1317 if (*arg == ';')
1318 {
1319 /* Put the rest of the list (may be empty) in the var after ';'.
1320 * Create a new list for this. */
1321 l = list_alloc();
1322 if (l == NULL)
1323 return FAIL;
1324 while (item != NULL)
1325 {
1326 list_append_tv(l, &item->li_tv);
1327 item = item->li_next;
1328 }
1329
1330 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001331 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332 ltv.vval.v_list = l;
1333 l->lv_refcount = 1;
1334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1336 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001337 clear_tv(&ltv);
1338 if (arg == NULL)
1339 return FAIL;
1340 break;
1341 }
1342 else if (*arg != ',' && *arg != ']')
1343 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001344 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001345 return FAIL;
1346 }
1347 }
1348
1349 return OK;
1350}
1351
1352/*
1353 * Skip over assignable variable "var" or list of variables "[var, var]".
1354 * Used for ":let varvar = expr" and ":for varvar in expr".
1355 * For "[var, var]" increment "*var_count" for each variable.
1356 * for "[var, var; var]" set "semicolon".
1357 * Return NULL for an error.
1358 */
1359 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360skip_var_list(
1361 char_u *arg,
1362 int *var_count,
1363 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364{
1365 char_u *p, *s;
1366
1367 if (*arg == '[')
1368 {
1369 /* "[var, var]": find the matching ']'. */
1370 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001371 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 {
1373 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1374 s = skip_var_one(p);
1375 if (s == p)
1376 {
1377 EMSG2(_(e_invarg2), p);
1378 return NULL;
1379 }
1380 ++*var_count;
1381
1382 p = skipwhite(s);
1383 if (*p == ']')
1384 break;
1385 else if (*p == ';')
1386 {
1387 if (*semicolon == 1)
1388 {
1389 EMSG(_("Double ; in list of variables"));
1390 return NULL;
1391 }
1392 *semicolon = 1;
1393 }
1394 else if (*p != ',')
1395 {
1396 EMSG2(_(e_invarg2), p);
1397 return NULL;
1398 }
1399 }
1400 return p + 1;
1401 }
1402 else
1403 return skip_var_one(arg);
1404}
1405
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001406/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001407 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001408 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001409 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001411skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001413 if (*arg == '@' && arg[1] != NUL)
1414 return arg + 2;
1415 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1416 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417}
1418
Bram Moolenaara7043832005-01-21 11:56:39 +00001419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 * List variables for hashtab "ht" with prefix "prefix".
1421 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001422 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424list_hashtable_vars(
1425 hashtab_T *ht,
1426 char_u *prefix,
1427 int empty,
1428 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001429{
Bram Moolenaar33570922005-01-25 22:26:29 +00001430 hashitem_T *hi;
1431 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001432 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001433 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001434
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001435 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001436 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1437 {
1438 if (!HASHITEM_EMPTY(hi))
1439 {
1440 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001441 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001442
1443 // apply :filter /pat/ to variable name
1444 vim_strncpy((char_u *) buf, prefix, IOSIZE - 1);
1445 vim_strcat((char_u *) buf, di->di_key, IOSIZE);
1446 if (message_filtered(buf))
1447 continue;
1448
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 if (empty || di->di_tv.v_type != VAR_STRING
1450 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001451 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001452 }
1453 }
1454}
1455
1456/*
1457 * List global variables.
1458 */
1459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001461{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001462 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001463}
1464
1465/*
1466 * List buffer variables.
1467 */
1468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001469list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001470{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001471 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001472 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001473}
1474
1475/*
1476 * List window variables.
1477 */
1478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001479list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001480{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001481 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001482 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001483}
1484
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001485/*
1486 * List tab page variables.
1487 */
1488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001490{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001491 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001492 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001493}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001494
Bram Moolenaara7043832005-01-21 11:56:39 +00001495/*
1496 * List Vim variables.
1497 */
1498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001499list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001501 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502}
1503
1504/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001505 * List script-local variables, if there is a script.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001509{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001510 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1511 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001512 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001513}
1514
1515/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 * List variables in "arg".
1517 */
1518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520{
1521 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001522 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524 char_u *name_start;
1525 char_u *arg_subsc;
1526 char_u *tofree;
1527 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528
1529 while (!ends_excmd(*arg) && !got_int)
1530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001531 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001533 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001534 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001535 {
1536 emsg_severe = TRUE;
1537 EMSG(_(e_trailing));
1538 break;
1539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001543 /* get_name_len() takes care of expanding curly braces */
1544 name_start = name = arg;
1545 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1546 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001548 /* This is mainly to keep test 49 working: when expanding
1549 * curly braces fails overrule the exception error message. */
1550 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001551 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 emsg_severe = TRUE;
1553 EMSG2(_(e_invarg2), arg);
1554 break;
1555 }
1556 error = TRUE;
1557 }
1558 else
1559 {
1560 if (tofree != NULL)
1561 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001562 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001563 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 else
1565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001566 /* handle d.key, l[idx], f(expr) */
1567 arg_subsc = arg;
1568 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001571 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001573 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001574 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001576 case 'g': list_glob_vars(first); break;
1577 case 'b': list_buf_vars(first); break;
1578 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001579 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001580 case 'v': list_vim_vars(first); break;
1581 case 's': list_script_vars(first); break;
1582 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001583 default:
1584 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001585 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001586 }
1587 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 {
1589 char_u numbuf[NUMBUFLEN];
1590 char_u *tf;
1591 int c;
1592 char_u *s;
1593
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001594 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595 c = *arg;
1596 *arg = NUL;
1597 list_one_var_a((char_u *)"",
1598 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001599 tv.v_type,
1600 s == NULL ? (char_u *)"" : s,
1601 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001602 *arg = c;
1603 vim_free(tf);
1604 }
1605 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 }
1608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001609
1610 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001612
1613 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 }
1615
1616 return arg;
1617}
1618
1619/*
1620 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1621 * Returns a pointer to the char just after the var name.
1622 * Returns NULL if there is an error.
1623 */
1624 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001625ex_let_one(
1626 char_u *arg, /* points to variable name */
1627 typval_T *tv, /* value to assign to variable */
1628 int copy, /* copy value from "tv" */
1629 char_u *endchars, /* valid chars after variable name or NULL */
1630 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631{
1632 int c1;
1633 char_u *name;
1634 char_u *p;
1635 char_u *arg_end = NULL;
1636 int len;
1637 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639
1640 /*
1641 * ":let $VAR = expr": Set environment variable.
1642 */
1643 if (*arg == '$')
1644 {
1645 /* Find the end of the name. */
1646 ++arg;
1647 name = arg;
1648 len = get_env_len(&arg);
1649 if (len == 0)
1650 EMSG2(_(e_invarg2), name - 1);
1651 else
1652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 if (op != NULL && (*op == '+' || *op == '-'))
1654 EMSG2(_(e_letwrong), op);
1655 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001658 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 {
1660 c1 = name[len];
1661 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001662 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 {
1665 int mustfree = FALSE;
1666 char_u *s = vim_getenv(name, &mustfree);
1667
1668 if (s != NULL)
1669 {
1670 p = tofree = concat_str(s, p);
1671 if (mustfree)
1672 vim_free(s);
1673 }
1674 }
1675 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001678 if (STRICMP(name, "HOME") == 0)
1679 init_homedir();
1680 else if (didset_vim && STRICMP(name, "VIM") == 0)
1681 didset_vim = FALSE;
1682 else if (didset_vimruntime
1683 && STRICMP(name, "VIMRUNTIME") == 0)
1684 didset_vimruntime = FALSE;
1685 arg_end = arg;
1686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 }
1690 }
1691 }
1692
1693 /*
1694 * ":let &option = expr": Set option value.
1695 * ":let &l:option = expr": Set local option value.
1696 * ":let &g:option = expr": Set global option value.
1697 */
1698 else if (*arg == '&')
1699 {
1700 /* Find the end of the name. */
1701 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 if (p == NULL || (endchars != NULL
1703 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 EMSG(_(e_letunexp));
1705 else
1706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707 long n;
1708 int opt_type;
1709 long numval;
1710 char_u *stringval = NULL;
1711 char_u *s;
1712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 c1 = *p;
1714 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001716 n = (long)tv_get_number(tv);
1717 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 opt_type = get_option_value(arg, &numval,
1721 &stringval, opt_flags);
1722 if ((opt_type == 1 && *op == '.')
1723 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001726 s = NULL; /* don't set the value */
1727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 else
1729 {
1730 if (opt_type == 1) /* number */
1731 {
1732 if (*op == '+')
1733 n = numval + n;
1734 else
1735 n = numval - n;
1736 }
1737 else if (opt_type == 0 && stringval != NULL) /* string */
1738 {
1739 s = concat_str(stringval, s);
1740 vim_free(stringval);
1741 stringval = s;
1742 }
1743 }
1744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001745 if (s != NULL)
1746 {
1747 set_option_value(arg, n, s, opt_flags);
1748 arg_end = p;
1749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 }
1753 }
1754
1755 /*
1756 * ":let @r = expr": Set register contents.
1757 */
1758 else if (*arg == '@')
1759 {
1760 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 if (op != NULL && (*op == '+' || *op == '-'))
1762 EMSG2(_(e_letwrong), op);
1763 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 EMSG(_(e_letunexp));
1766 else
1767 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 char_u *s;
1770
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001771 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001772 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001774 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 if (s != NULL)
1776 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001777 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 vim_free(s);
1779 }
1780 }
1781 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001784 arg_end = arg + 1;
1785 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001786 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 }
1788 }
1789
1790 /*
1791 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001794 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001798 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001799 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001801 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1802 EMSG(_(e_letunexp));
1803 else
1804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 arg_end = p;
1807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001809 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 }
1811
1812 else
1813 EMSG2(_(e_invarg2), arg);
1814
1815 return arg_end;
1816}
1817
1818/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 * Get an lval: variable, Dict item or List item that can be assigned a value
1820 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1821 * "name.key", "name.key[expr]" etc.
1822 * Indexing only works if "name" is an existing List or Dictionary.
1823 * "name" points to the start of the name.
1824 * If "rettv" is not NULL it points to the value to be assigned.
1825 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1826 * wrong; must end in space or cmd separator.
1827 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001828 * flags:
1829 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001830 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001831 * GLV_NO_AUTOLOAD: do not use script autoloading
1832 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001833 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001834 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838get_lval(
1839 char_u *name,
1840 typval_T *rettv,
1841 lval_T *lp,
1842 int unlet,
1843 int skip,
1844 int flags, /* GLV_ values */
1845 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001848 char_u *expr_start, *expr_end;
1849 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 dictitem_T *v;
1851 typval_T var1;
1852 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001855 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001856 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001858 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862
1863 if (skip)
1864 {
1865 /* When skipping just find the end of the name. */
1866 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001867 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 }
1869
1870 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001871 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 if (expr_start != NULL)
1873 {
1874 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001875 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001876 && *p != '[' && *p != '.')
1877 {
1878 EMSG(_(e_trailing));
1879 return NULL;
1880 }
1881
1882 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1883 if (lp->ll_exp_name == NULL)
1884 {
1885 /* Report an invalid expression in braces, unless the
1886 * expression evaluation has been cancelled due to an
1887 * aborting error, an interrupt, or an exception. */
1888 if (!aborting() && !quiet)
1889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001890 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 EMSG2(_(e_invarg2), name);
1892 return NULL;
1893 }
1894 }
1895 lp->ll_name = lp->ll_exp_name;
1896 }
1897 else
1898 lp->ll_name = name;
1899
1900 /* Without [idx] or .key we are done. */
1901 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1902 return p;
1903
1904 cc = *p;
1905 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001906 /* Only pass &ht when we would write to the variable, it prevents autoload
1907 * as well. */
1908 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1909 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 if (v == NULL && !quiet)
1911 EMSG2(_(e_undefvar), lp->ll_name);
1912 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 if (v == NULL)
1914 return NULL;
1915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001916 /*
1917 * Loop until no more [idx] or .key is following.
1918 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001920 var1.v_type = VAR_UNKNOWN;
1921 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1925 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001926 && lp->ll_tv->vval.v_dict != NULL)
1927 && !(lp->ll_tv->v_type == VAR_BLOB
1928 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 if (!quiet)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001931 EMSG(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001932 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001934 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001936 if (!quiet)
1937 EMSG(_("E708: [:] must come last"));
1938 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001940
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 len = -1;
1942 if (*p == '.')
1943 {
1944 key = p + 1;
1945 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1946 ;
1947 if (len == 0)
1948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 if (!quiet)
1950 EMSG(_(e_emptykey));
1951 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001952 }
1953 p = key + len;
1954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001955 else
1956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001957 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001958 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001959 if (*p == ':')
1960 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001961 else
1962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001963 empty1 = FALSE;
1964 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001966 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 {
1968 /* not a number or string */
1969 clear_tv(&var1);
1970 return NULL;
1971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 }
1973
1974 /* Optionally get the second index [ :expr]. */
1975 if (*p == ':')
1976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001984 if (rettv != NULL
1985 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001986 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001988 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 if (!quiet)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001991 EMSG(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001994 }
1995 p = skipwhite(p + 1);
1996 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001998 else
1999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2002 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002003 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002006 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 {
2008 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002009 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 clear_tv(&var2);
2011 return NULL;
2012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002014 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002018
Bram Moolenaar8c711452005-01-14 21:53:12 +00002019 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (!quiet)
2022 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002023 clear_tv(&var1);
2024 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 }
2027
2028 /* Skip to past ']'. */
2029 ++p;
2030 }
2031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 {
2034 if (len == -1)
2035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002036 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002037 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002038 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 }
2043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 lp->ll_list = NULL;
2045 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002046 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002047
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002048 /* When assigning to a scope dictionary check that a function and
2049 * variable name is valid (only variable name unless it is l: or
2050 * g: dictionary). Disallow overwriting a builtin function. */
2051 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002052 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002053 int prevval;
2054 int wrong;
2055
2056 if (len != -1)
2057 {
2058 prevval = key[len];
2059 key[len] = NUL;
2060 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002061 else
2062 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002063 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2064 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002065 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002066 || !valid_varname(key);
2067 if (len != -1)
2068 key[len] = prevval;
2069 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002070 return NULL;
2071 }
2072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002075 /* Can't add "v:" variable. */
2076 if (lp->ll_dict == &vimvardict)
2077 {
2078 EMSG2(_(e_illvar), name);
2079 return NULL;
2080 }
2081
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002082 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002087 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 }
2090 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002094 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002096 p = NULL;
2097 break;
2098 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002099 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002100 else if ((flags & GLV_READ_ONLY) == 0
2101 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002102 {
2103 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002104 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002105 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002106
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002109 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002110 else if (lp->ll_tv->v_type == VAR_BLOB)
2111 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002112 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2113
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002114 /*
2115 * Get the number and item for the only or first index of the List.
2116 */
2117 if (empty1)
2118 lp->ll_n1 = 0;
2119 else
2120 // is number or string
2121 lp->ll_n1 = (long)tv_get_number(&var1);
2122 clear_tv(&var1);
2123
2124 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002125 || lp->ll_n1 > bloblen
2126 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002127 {
2128 if (!quiet)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002129 EMSGN(_(e_blobidx), lp->ll_n1);
2130 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002131 return NULL;
2132 }
2133 if (lp->ll_range && !lp->ll_empty2)
2134 {
2135 lp->ll_n2 = (long)tv_get_number(&var2);
2136 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002137 if (lp->ll_n2 < 0
2138 || lp->ll_n2 >= bloblen
2139 || lp->ll_n2 < lp->ll_n1)
2140 {
2141 if (!quiet)
2142 EMSGN(_(e_blobidx), lp->ll_n2);
2143 return NULL;
2144 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002145 }
2146 lp->ll_blob = lp->ll_tv->vval.v_blob;
2147 lp->ll_tv = NULL;
2148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 else
2150 {
2151 /*
2152 * Get the number and item for the only or first index of the List.
2153 */
2154 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002157 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002159 clear_tv(&var1);
2160
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 lp->ll_list = lp->ll_tv->vval.v_list;
2163 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2164 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002165 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002166 if (lp->ll_n1 < 0)
2167 {
2168 lp->ll_n1 = 0;
2169 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2170 }
2171 }
2172 if (lp->ll_li == NULL)
2173 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002174 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002175 if (!quiet)
2176 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002178 }
2179
2180 /*
2181 * May need to find the item or absolute index for the second
2182 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 * When no index given: "lp->ll_empty2" is TRUE.
2184 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002185 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002188 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002189 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002195 {
2196 if (!quiet)
2197 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 }
2202
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2204 if (lp->ll_n1 < 0)
2205 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2206 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002207 {
2208 if (!quiet)
2209 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002211 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002212 }
2213
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
2217
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002218 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 return p;
2220}
2221
2222/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002223 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227{
2228 vim_free(lp->ll_exp_name);
2229 vim_free(lp->ll_newkey);
2230}
2231
2232/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002233 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002235 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238set_var_lval(
2239 lval_T *lp,
2240 char_u *endp,
2241 typval_T *rettv,
2242 int copy,
2243 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244{
2245 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 listitem_T *ri;
2247 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248
2249 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002251 cc = *endp;
2252 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002253 if (lp->ll_blob != NULL)
2254 {
2255 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002256
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002257 if (op != NULL && *op != '=')
2258 {
2259 EMSG2(_(e_letwrong), op);
2260 return;
2261 }
2262
2263 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2264 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002265 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002266
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002267 if (lp->ll_empty2)
2268 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2269
2270 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002271 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002272 EMSG(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002273 return;
2274 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002275 if (lp->ll_empty2)
2276 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002277
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002278 ir = 0;
2279 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2280 blob_set(lp->ll_blob, il,
2281 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 }
2283 else
2284 {
2285 val = (int)tv_get_number_chk(rettv, &error);
2286 if (!error)
2287 {
2288 garray_T *gap = &lp->ll_blob->bv_ga;
2289
2290 // Allow for appending a byte. Setting a byte beyond
2291 // the end is an error otherwise.
2292 if (lp->ll_n1 < gap->ga_len
2293 || (lp->ll_n1 == gap->ga_len
2294 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2295 {
2296 blob_set(lp->ll_blob, lp->ll_n1, val);
2297 if (lp->ll_n1 == gap->ga_len)
2298 ++gap->ga_len;
2299 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002300 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002301 }
2302 }
2303 }
2304 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002306 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307
Bram Moolenaar79518e22017-02-17 16:31:35 +01002308 /* handle +=, -= and .= */
2309 di = NULL;
2310 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2311 &tv, &di, TRUE, FALSE) == OK)
2312 {
2313 if ((di == NULL
2314 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2315 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2316 FALSE)))
2317 && tv_op(&tv, rettv, op) == OK)
2318 set_var(lp->ll_name, &tv, FALSE);
2319 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002322 else
2323 set_var(lp->ll_name, rettv, copy);
2324 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002326 else if (tv_check_lock(lp->ll_newkey == NULL
2327 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002328 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002329 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 else if (lp->ll_range)
2331 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002332 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002333 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002334
2335 /*
2336 * Check whether any of the list items is locked
2337 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002338 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002339 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002340 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002341 return;
2342 ri = ri->li_next;
2343 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2344 break;
2345 ll_li = ll_li->li_next;
2346 ++ll_n1;
2347 }
2348
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 /*
2350 * Assign the List values to the list items.
2351 */
2352 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 if (op != NULL && *op != '=')
2355 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2356 else
2357 {
2358 clear_tv(&lp->ll_li->li_tv);
2359 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2360 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 ri = ri->li_next;
2362 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2363 break;
2364 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002367 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 ri = NULL;
2370 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 lp->ll_li = lp->ll_li->li_next;
2374 ++lp->ll_n1;
2375 }
2376 if (ri != NULL)
2377 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 else if (lp->ll_empty2
2379 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 : lp->ll_n1 != lp->ll_n2)
2381 EMSG(_("E711: List value has not enough items"));
2382 }
2383 else
2384 {
2385 /*
2386 * Assign to a List or Dictionary item.
2387 */
2388 if (lp->ll_newkey != NULL)
2389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 if (op != NULL && *op != '=')
2391 {
2392 EMSG2(_(e_letwrong), op);
2393 return;
2394 }
2395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002397 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 if (di == NULL)
2399 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002400 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2401 {
2402 vim_free(di);
2403 return;
2404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002406 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 else if (op != NULL && *op != '=')
2408 {
2409 tv_op(lp->ll_tv, rettv, op);
2410 return;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 /*
2416 * Assign the value to the variable or list item.
2417 */
2418 if (copy)
2419 copy_tv(rettv, lp->ll_tv);
2420 else
2421 {
2422 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002423 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427}
2428
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2431 * Returns OK or FAIL.
2432 */
2433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002436 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u numbuf[NUMBUFLEN];
2438 char_u *s;
2439
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002440 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2441 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2442 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 switch (tv1->v_type)
2445 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002446 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 case VAR_DICT:
2448 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002449 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002450 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002451 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002452 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 break;
2454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002455 case VAR_BLOB:
2456 if (*op != '+' || tv2->v_type != VAR_BLOB)
2457 break;
2458 // BLOB += BLOB
2459 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2460 {
2461 blob_T *b1 = tv1->vval.v_blob;
2462 blob_T *b2 = tv2->vval.v_blob;
2463 int i, len = blob_len(b2);
2464 for (i = 0; i < len; i++)
2465 ga_append(&b1->bv_ga, blob_get(b2, i));
2466 }
2467 return OK;
2468
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 case VAR_LIST:
2470 if (*op != '+' || tv2->v_type != VAR_LIST)
2471 break;
2472 /* List += List */
2473 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2474 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2475 return OK;
2476
2477 case VAR_NUMBER:
2478 case VAR_STRING:
2479 if (tv2->v_type == VAR_LIST)
2480 break;
2481 if (*op == '+' || *op == '-')
2482 {
2483 /* nr += nr or nr -= nr*/
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002484 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002485#ifdef FEAT_FLOAT
2486 if (tv2->v_type == VAR_FLOAT)
2487 {
2488 float_T f = n;
2489
2490 if (*op == '+')
2491 f += tv2->vval.v_float;
2492 else
2493 f -= tv2->vval.v_float;
2494 clear_tv(tv1);
2495 tv1->v_type = VAR_FLOAT;
2496 tv1->vval.v_float = f;
2497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002499#endif
2500 {
2501 if (*op == '+')
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002502 n += tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002503 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 n -= tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505 clear_tv(tv1);
2506 tv1->v_type = VAR_NUMBER;
2507 tv1->vval.v_number = n;
2508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 }
2510 else
2511 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002512 if (tv2->v_type == VAR_FLOAT)
2513 break;
2514
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 /* str .= str */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002516 s = tv_get_string(tv1);
2517 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 clear_tv(tv1);
2519 tv1->v_type = VAR_STRING;
2520 tv1->vval.v_string = s;
2521 }
2522 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002523
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002524 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002525#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002526 {
2527 float_T f;
2528
2529 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2530 && tv2->v_type != VAR_NUMBER
2531 && tv2->v_type != VAR_STRING))
2532 break;
2533 if (tv2->v_type == VAR_FLOAT)
2534 f = tv2->vval.v_float;
2535 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 f = tv_get_number(tv2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002537 if (*op == '+')
2538 tv1->vval.v_float += f;
2539 else
2540 tv1->vval.v_float -= f;
2541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002542#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002543 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 }
2545 }
2546
2547 EMSG2(_(e_letwrong), op);
2548 return FAIL;
2549}
2550
2551/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002552 * Evaluate the expression used in a ":for var in expr" command.
2553 * "arg" points to "var".
2554 * Set "*errp" to TRUE for an error, FALSE otherwise;
2555 * Return a pointer that holds the info. Null when there is an error.
2556 */
2557 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002558eval_for_line(
2559 char_u *arg,
2560 int *errp,
2561 char_u **nextcmdp,
2562 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002563{
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002565 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 typval_T tv;
2567 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002568 blob_T *b;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002569
2570 *errp = TRUE; /* default: there is an error */
2571
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573 if (fi == NULL)
2574 return NULL;
2575
2576 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2577 if (expr == NULL)
2578 return fi;
2579
2580 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002581 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002582 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002583 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002584 return fi;
2585 }
2586
2587 if (skip)
2588 ++emsg_skip;
2589 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2590 {
2591 *errp = FALSE;
2592 if (!skip)
2593 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002594 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002595 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002596 l = tv.vval.v_list;
2597 if (l == NULL)
2598 {
2599 // a null list is like an empty list: do nothing
2600 clear_tv(&tv);
2601 }
2602 else
2603 {
2604 // No need to increment the refcount, it's already set for
2605 // the list being used in "tv".
2606 fi->fi_list = l;
2607 list_add_watch(l, &fi->fi_lw);
2608 fi->fi_lw.lw_item = l->lv_first;
2609 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002610 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002611 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002612 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002613 b = tv.vval.v_blob;
2614 if (b == NULL)
2615 clear_tv(&tv);
2616 else
2617 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002618 // No need to increment the refcount, it's already set for
2619 // the blob being used in "tv".
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002620 fi->fi_blob = b;
2621 fi->fi_bi = 0;
2622 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002623 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002624 else
2625 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002626 EMSG(_(e_listreq));
2627 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002628 }
2629 }
2630 }
2631 if (skip)
2632 --emsg_skip;
2633
2634 return fi;
2635}
2636
2637/*
2638 * Use the first item in a ":for" list. Advance to the next.
2639 * Assign the values to the variable (list). "arg" points to the first one.
2640 * Return TRUE when a valid item was found, FALSE when at end of list or
2641 * something wrong.
2642 */
2643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002644next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002645{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002646 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002647 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002649
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002650 if (fi->fi_blob != NULL)
2651 {
2652 typval_T tv;
2653
2654 if (fi->fi_bi >= blob_len(fi->fi_blob))
2655 return FALSE;
2656 tv.v_type = VAR_NUMBER;
2657 tv.v_lock = VAR_FIXED;
2658 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2659 ++fi->fi_bi;
2660 return ex_let_vars(arg, &tv, TRUE,
2661 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2662 }
2663
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002664 item = fi->fi_lw.lw_item;
2665 if (item == NULL)
2666 result = FALSE;
2667 else
2668 {
2669 fi->fi_lw.lw_item = item->li_next;
2670 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2671 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2672 }
2673 return result;
2674}
2675
2676/*
2677 * Free the structure used to store info used by ":for".
2678 */
2679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002680free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681{
Bram Moolenaar33570922005-01-25 22:26:29 +00002682 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002683
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002684 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002685 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002686 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002687 list_unref(fi->fi_list);
2688 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002689 if (fi != NULL && fi->fi_blob != NULL)
2690 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 vim_free(fi);
2692}
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2695
2696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002697set_context_for_expression(
2698 expand_T *xp,
2699 char_u *arg,
2700 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 int got_eq = FALSE;
2703 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706 if (cmdidx == CMD_let)
2707 {
2708 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002709 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710 {
2711 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002712 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002713 {
2714 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002715 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002716 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 break;
2718 }
2719 return;
2720 }
2721 }
2722 else
2723 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2724 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 while ((xp->xp_pattern = vim_strpbrk(arg,
2726 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2727 {
2728 c = *xp->xp_pattern;
2729 if (c == '&')
2730 {
2731 c = xp->xp_pattern[1];
2732 if (c == '&')
2733 {
2734 ++xp->xp_pattern;
2735 xp->xp_context = cmdidx != CMD_let || got_eq
2736 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2737 }
2738 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002741 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2742 xp->xp_pattern += 2;
2743
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 else if (c == '$')
2747 {
2748 /* environment variable */
2749 xp->xp_context = EXPAND_ENV_VARS;
2750 }
2751 else if (c == '=')
2752 {
2753 got_eq = TRUE;
2754 xp->xp_context = EXPAND_EXPRESSION;
2755 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002756 else if (c == '#'
2757 && xp->xp_context == EXPAND_EXPRESSION)
2758 {
2759 /* Autoload function/variable contains '#'. */
2760 break;
2761 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002762 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 && xp->xp_context == EXPAND_FUNCTIONS
2764 && vim_strchr(xp->xp_pattern, '(') == NULL)
2765 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002766 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 break;
2768 }
2769 else if (cmdidx != CMD_let || got_eq)
2770 {
2771 if (c == '"') /* string */
2772 {
2773 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2774 if (c == '\\' && xp->xp_pattern[1] != NUL)
2775 ++xp->xp_pattern;
2776 xp->xp_context = EXPAND_NOTHING;
2777 }
2778 else if (c == '\'') /* literal string */
2779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2782 /* skip */ ;
2783 xp->xp_context = EXPAND_NOTHING;
2784 }
2785 else if (c == '|')
2786 {
2787 if (xp->xp_pattern[1] == '|')
2788 {
2789 ++xp->xp_pattern;
2790 xp->xp_context = EXPAND_EXPRESSION;
2791 }
2792 else
2793 xp->xp_context = EXPAND_COMMANDS;
2794 }
2795 else
2796 xp->xp_context = EXPAND_EXPRESSION;
2797 }
2798 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 /* Doesn't look like something valid, expand as an expression
2800 * anyway. */
2801 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 arg = xp->xp_pattern;
2803 if (*arg != NUL)
2804 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2805 /* skip */ ;
2806 }
2807 xp->xp_pattern = arg;
2808}
2809
2810#endif /* FEAT_CMDL_COMPL */
2811
2812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 * ":unlet[!] var1 ... " command.
2814 */
2815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 ex_unletlock(eap, eap->arg, 0);
2819}
2820
2821/*
2822 * ":lockvar" and ":unlockvar" commands
2823 */
2824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002825ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002826{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002828 int deep = 2;
2829
2830 if (eap->forceit)
2831 deep = -1;
2832 else if (vim_isdigit(*arg))
2833 {
2834 deep = getdigits(&arg);
2835 arg = skipwhite(arg);
2836 }
2837
2838 ex_unletlock(eap, arg, deep);
2839}
2840
2841/*
2842 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2843 */
2844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845ex_unletlock(
2846 exarg_T *eap,
2847 char_u *argstart,
2848 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849{
2850 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002853 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 do
2856 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002857 if (*arg == '$')
2858 {
2859 char_u *name = ++arg;
2860
2861 if (get_env_len(&arg) == 0)
2862 {
2863 EMSG2(_(e_invarg2), name - 1);
2864 return;
2865 }
2866 vim_unsetenv(name);
2867 arg = skipwhite(arg);
2868 continue;
2869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002872 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002873 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 if (lv.ll_name == NULL)
2875 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002876 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (name_end != NULL)
2880 {
2881 emsg_severe = TRUE;
2882 EMSG(_(e_trailing));
2883 }
2884 if (!(eap->skip || error))
2885 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 break;
2887 }
2888
2889 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002890 {
2891 if (eap->cmdidx == CMD_unlet)
2892 {
2893 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2894 error = TRUE;
2895 }
2896 else
2897 {
2898 if (do_lock_var(&lv, name_end, deep,
2899 eap->cmdidx == CMD_lockvar) == FAIL)
2900 error = TRUE;
2901 }
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!eap->skip)
2905 clear_lval(&lv);
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 arg = skipwhite(name_end);
2908 } while (!ends_excmd(*arg));
2909
2910 eap->nextcmd = check_nextcmd(arg);
2911}
2912
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914do_unlet_var(
2915 lval_T *lp,
2916 char_u *name_end,
2917 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 int ret = OK;
2920 int cc;
2921
2922 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 cc = *name_end;
2925 *name_end = NUL;
2926
2927 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002928 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002932 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002933 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002934 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002935 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 else if (lp->ll_range)
2938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002940 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002941 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002942
2943 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2944 {
2945 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002946 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002947 return FAIL;
2948 ll_li = li;
2949 ++ll_n1;
2950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951
2952 /* Delete a range of List items. */
2953 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2954 {
2955 li = lp->ll_li->li_next;
2956 listitem_remove(lp->ll_list, lp->ll_li);
2957 lp->ll_li = li;
2958 ++lp->ll_n1;
2959 }
2960 }
2961 else
2962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* unlet a List item. */
2965 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 }
2970
2971 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972}
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974/*
2975 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 */
2978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002979do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 hashtab_T *ht;
2982 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002983 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002984 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002985 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 ht = find_var_ht(name, &varname);
2988 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002991 if (d == NULL)
2992 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 if (ht == &globvarht)
2994 d = &globvardict;
2995 else if (ht == &compat_hashtab)
2996 d = &vimvardict;
2997 else
2998 {
2999 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3000 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3001 }
3002 if (d == NULL)
3003 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003004 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 return FAIL;
3006 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003009 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003010 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003011 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003012 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003013 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003014 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003015 || var_check_ro(di->di_flags, name, FALSE)
3016 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003017 return FAIL;
3018
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003019 delete_var(ht, hi);
3020 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 if (forceit)
3024 return OK;
3025 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 return FAIL;
3027}
3028
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003029/*
3030 * Lock or unlock variable indicated by "lp".
3031 * "deep" is the levels to go (-1 for unlimited);
3032 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3033 */
3034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003035do_lock_var(
3036 lval_T *lp,
3037 char_u *name_end,
3038 int deep,
3039 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003040{
3041 int ret = OK;
3042 int cc;
3043 dictitem_T *di;
3044
3045 if (deep == 0) /* nothing to do */
3046 return OK;
3047
3048 if (lp->ll_tv == NULL)
3049 {
3050 cc = *name_end;
3051 *name_end = NUL;
3052
3053 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003054 di = find_var(lp->ll_name, NULL, TRUE);
3055 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003056 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003057 else if ((di->di_flags & DI_FLAGS_FIX)
3058 && di->di_tv.v_type != VAR_DICT
3059 && di->di_tv.v_type != VAR_LIST)
3060 /* For historic reasons this error is not given for a list or dict.
3061 * E.g., the b: dict could be locked/unlocked. */
3062 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003063 else
3064 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003065 if (lock)
3066 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003067 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003068 di->di_flags &= ~DI_FLAGS_LOCK;
3069 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003070 }
3071 *name_end = cc;
3072 }
3073 else if (lp->ll_range)
3074 {
3075 listitem_T *li = lp->ll_li;
3076
3077 /* (un)lock a range of List items. */
3078 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3079 {
3080 item_lock(&li->li_tv, deep, lock);
3081 li = li->li_next;
3082 ++lp->ll_n1;
3083 }
3084 }
3085 else if (lp->ll_list != NULL)
3086 /* (un)lock a List item. */
3087 item_lock(&lp->ll_li->li_tv, deep, lock);
3088 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003089 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003090 item_lock(&lp->ll_di->di_tv, deep, lock);
3091
3092 return ret;
3093}
3094
3095/*
3096 * Lock or unlock an item. "deep" is nr of levels to go.
3097 */
3098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003100{
3101 static int recurse = 0;
3102 list_T *l;
3103 listitem_T *li;
3104 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003105 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 hashitem_T *hi;
3107 int todo;
3108
3109 if (recurse >= DICT_MAXNEST)
3110 {
3111 EMSG(_("E743: variable nested too deep for (un)lock"));
3112 return;
3113 }
3114 if (deep == 0)
3115 return;
3116 ++recurse;
3117
3118 /* lock/unlock the item itself */
3119 if (lock)
3120 tv->v_lock |= VAR_LOCKED;
3121 else
3122 tv->v_lock &= ~VAR_LOCKED;
3123
3124 switch (tv->v_type)
3125 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003126 case VAR_UNKNOWN:
3127 case VAR_NUMBER:
3128 case VAR_STRING:
3129 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003130 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003131 case VAR_FLOAT:
3132 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003133 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003134 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003135 break;
3136
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003137 case VAR_BLOB:
3138 if ((b = tv->vval.v_blob) != NULL)
3139 {
3140 if (lock)
3141 b->bv_lock |= VAR_LOCKED;
3142 else
3143 b->bv_lock &= ~VAR_LOCKED;
3144 }
3145 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003146 case VAR_LIST:
3147 if ((l = tv->vval.v_list) != NULL)
3148 {
3149 if (lock)
3150 l->lv_lock |= VAR_LOCKED;
3151 else
3152 l->lv_lock &= ~VAR_LOCKED;
3153 if (deep < 0 || deep > 1)
3154 /* recursive: lock/unlock the items the List contains */
3155 for (li = l->lv_first; li != NULL; li = li->li_next)
3156 item_lock(&li->li_tv, deep - 1, lock);
3157 }
3158 break;
3159 case VAR_DICT:
3160 if ((d = tv->vval.v_dict) != NULL)
3161 {
3162 if (lock)
3163 d->dv_lock |= VAR_LOCKED;
3164 else
3165 d->dv_lock &= ~VAR_LOCKED;
3166 if (deep < 0 || deep > 1)
3167 {
3168 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003169 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3171 {
3172 if (!HASHITEM_EMPTY(hi))
3173 {
3174 --todo;
3175 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3176 }
3177 }
3178 }
3179 }
3180 }
3181 --recurse;
3182}
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3185/*
3186 * Delete all "menutrans_" variables.
3187 */
3188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003189del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003192 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003195 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003197 {
3198 if (!HASHITEM_EMPTY(hi))
3199 {
3200 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3202 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003203 }
3204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206}
3207#endif
3208
3209#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3210
3211/*
3212 * Local string buffer for the next two functions to store a variable name
3213 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3214 * get_user_var_name().
3215 */
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217static char_u *varnamebuf = NULL;
3218static int varnamebuflen = 0;
3219
3220/*
3221 * Function to concatenate a prefix and a variable name.
3222 */
3223 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 int len;
3227
3228 len = (int)STRLEN(name) + 3;
3229 if (len > varnamebuflen)
3230 {
3231 vim_free(varnamebuf);
3232 len += 10; /* some additional space */
3233 varnamebuf = alloc(len);
3234 if (varnamebuf == NULL)
3235 {
3236 varnamebuflen = 0;
3237 return NULL;
3238 }
3239 varnamebuflen = len;
3240 }
3241 *varnamebuf = prefix;
3242 varnamebuf[1] = ':';
3243 STRCPY(varnamebuf + 2, name);
3244 return varnamebuf;
3245}
3246
3247/*
3248 * Function given to ExpandGeneric() to obtain the list of user defined
3249 * (global/buffer/window/built-in) variable names.
3250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003254 static long_u gdone;
3255 static long_u bdone;
3256 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003257 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003258 static int vidx;
3259 static hashitem_T *hi;
3260 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003263 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003265 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003266 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003267
3268 /* Global variables */
3269 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003273 else
3274 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 while (HASHITEM_EMPTY(hi))
3276 ++hi;
3277 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3278 return cat_prefix_varname('g', hi->hi_key);
3279 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003281
3282 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003283 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003288 else
3289 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003290 while (HASHITEM_EMPTY(hi))
3291 ++hi;
3292 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003294
3295 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003296 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003299 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003301 else
3302 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003303 while (HASHITEM_EMPTY(hi))
3304 ++hi;
3305 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003307
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003309 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310 if (tdone < ht->ht_used)
3311 {
3312 if (tdone++ == 0)
3313 hi = ht->ht_array;
3314 else
3315 ++hi;
3316 while (HASHITEM_EMPTY(hi))
3317 ++hi;
3318 return cat_prefix_varname('t', hi->hi_key);
3319 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003320
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 /* v: variables */
3322 if (vidx < VV_LEN)
3323 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaard23a8232018-02-10 18:45:26 +01003325 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 varnamebuflen = 0;
3327 return NULL;
3328}
3329
3330#endif /* FEAT_CMDL_COMPL */
3331
3332/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003333 * Return TRUE if "pat" matches "text".
3334 * Does not use 'cpo' and always uses 'magic'.
3335 */
3336 static int
3337pattern_match(char_u *pat, char_u *text, int ic)
3338{
3339 int matches = FALSE;
3340 char_u *save_cpo;
3341 regmatch_T regmatch;
3342
3343 /* avoid 'l' flag in 'cpoptions' */
3344 save_cpo = p_cpo;
3345 p_cpo = (char_u *)"";
3346 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3347 if (regmatch.regprog != NULL)
3348 {
3349 regmatch.rm_ic = ic;
3350 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3351 vim_regfree(regmatch.regprog);
3352 }
3353 p_cpo = save_cpo;
3354 return matches;
3355}
3356
3357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3361 */
3362
3363/*
3364 * Handle zero level expression.
3365 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003367 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Return OK or FAIL.
3369 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371eval0(
3372 char_u *arg,
3373 typval_T *rettv,
3374 char_u **nextcmd,
3375 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 int ret;
3378 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003379 int did_emsg_before = did_emsg;
3380 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (ret == FAIL || !ends_excmd(*p))
3385 {
3386 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 /*
3389 * Report the invalid expression unless the expression evaluation has
3390 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003391 * exception, or we already gave a more specific error.
3392 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003394 if (!aborting() && did_emsg == did_emsg_before
3395 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 EMSG2(_(e_invexpr2), arg);
3397 ret = FAIL;
3398 }
3399 if (nextcmd != NULL)
3400 *nextcmd = check_nextcmd(p);
3401
3402 return ret;
3403}
3404
3405/*
3406 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003407 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 *
3409 * "arg" must point to the first non-white of the expression.
3410 * "arg" is advanced to the next non-white after the recognized expression.
3411 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003412 * Note: "rettv.v_lock" is not set.
3413 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Return OK or FAIL.
3415 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003417eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003420 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /*
3423 * Get the first variable.
3424 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 return FAIL;
3427
3428 if ((*arg)[0] == '?')
3429 {
3430 result = FALSE;
3431 if (evaluate)
3432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 int error = FALSE;
3434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003435 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003438 if (error)
3439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441
3442 /*
3443 * Get the second variable.
3444 */
3445 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return FAIL;
3448
3449 /*
3450 * Check for the ":".
3451 */
3452 if ((*arg)[0] != ':')
3453 {
3454 EMSG(_("E109: Missing ':' after '?'"));
3455 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 return FAIL;
3458 }
3459
3460 /*
3461 * Get the third variable.
3462 */
3463 *arg = skipwhite(*arg + 1);
3464 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3465 {
3466 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return FAIL;
3469 }
3470 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473
3474 return OK;
3475}
3476
3477/*
3478 * Handle first level expression:
3479 * expr2 || expr2 || expr2 logical OR
3480 *
3481 * "arg" must point to the first non-white of the expression.
3482 * "arg" is advanced to the next non-white after the recognized expression.
3483 *
3484 * Return OK or FAIL.
3485 */
3486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003487eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 long result;
3491 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 /*
3495 * Get the first variable.
3496 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return FAIL;
3499
3500 /*
3501 * Repeat until there is no following "||".
3502 */
3503 first = TRUE;
3504 result = FALSE;
3505 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3506 {
3507 if (evaluate && first)
3508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003511 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003512 if (error)
3513 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 first = FALSE;
3515 }
3516
3517 /*
3518 * Get the second variable.
3519 */
3520 *arg = skipwhite(*arg + 2);
3521 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3522 return FAIL;
3523
3524 /*
3525 * Compute the result.
3526 */
3527 if (evaluate && !result)
3528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003529 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 if (error)
3533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 if (evaluate)
3536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 rettv->v_type = VAR_NUMBER;
3538 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540 }
3541
3542 return OK;
3543}
3544
3545/*
3546 * Handle second level expression:
3547 * expr3 && expr3 && expr3 logical AND
3548 *
3549 * "arg" must point to the first non-white of the expression.
3550 * "arg" is advanced to the next non-white after the recognized expression.
3551 *
3552 * Return OK or FAIL.
3553 */
3554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
Bram Moolenaar33570922005-01-25 22:26:29 +00003557 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 long result;
3559 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 /*
3563 * Get the first variable.
3564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 return FAIL;
3567
3568 /*
3569 * Repeat until there is no following "&&".
3570 */
3571 first = TRUE;
3572 result = TRUE;
3573 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3574 {
3575 if (evaluate && first)
3576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003577 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 if (error)
3581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 first = FALSE;
3583 }
3584
3585 /*
3586 * Get the second variable.
3587 */
3588 *arg = skipwhite(*arg + 2);
3589 if (eval4(arg, &var2, evaluate && result) == FAIL)
3590 return FAIL;
3591
3592 /*
3593 * Compute the result.
3594 */
3595 if (evaluate && result)
3596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003597 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 if (error)
3601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 if (evaluate)
3604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003605 rettv->v_type = VAR_NUMBER;
3606 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 }
3609
3610 return OK;
3611}
3612
3613/*
3614 * Handle third level expression:
3615 * var1 == var2
3616 * var1 =~ var2
3617 * var1 != var2
3618 * var1 !~ var2
3619 * var1 > var2
3620 * var1 >= var2
3621 * var1 < var2
3622 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003623 * var1 is var2
3624 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 *
3626 * "arg" must point to the first non-white of the expression.
3627 * "arg" is advanced to the next non-white after the recognized expression.
3628 *
3629 * Return OK or FAIL.
3630 */
3631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar33570922005-01-25 22:26:29 +00003634 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 char_u *p;
3636 int i;
3637 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003638 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
3643 * Get the first variable.
3644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003645 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return FAIL;
3647
3648 p = *arg;
3649 switch (p[0])
3650 {
3651 case '=': if (p[1] == '=')
3652 type = TYPE_EQUAL;
3653 else if (p[1] == '~')
3654 type = TYPE_MATCH;
3655 break;
3656 case '!': if (p[1] == '=')
3657 type = TYPE_NEQUAL;
3658 else if (p[1] == '~')
3659 type = TYPE_NOMATCH;
3660 break;
3661 case '>': if (p[1] != '=')
3662 {
3663 type = TYPE_GREATER;
3664 len = 1;
3665 }
3666 else
3667 type = TYPE_GEQUAL;
3668 break;
3669 case '<': if (p[1] != '=')
3670 {
3671 type = TYPE_SMALLER;
3672 len = 1;
3673 }
3674 else
3675 type = TYPE_SEQUAL;
3676 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003677 case 'i': if (p[1] == 's')
3678 {
3679 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3680 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003681 i = p[len];
3682 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003683 {
3684 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3685 type_is = TRUE;
3686 }
3687 }
3688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003692 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
3694 if (type != TYPE_UNKNOWN)
3695 {
3696 /* extra question mark appended: ignore case */
3697 if (p[len] == '?')
3698 {
3699 ic = TRUE;
3700 ++len;
3701 }
3702 /* extra '#' appended: match case */
3703 else if (p[len] == '#')
3704 {
3705 ic = FALSE;
3706 ++len;
3707 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003708 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else
3710 ic = p_ic;
3711
3712 /*
3713 * Get the second variable.
3714 */
3715 *arg = skipwhite(p + len);
3716 if (eval5(arg, &var2, evaluate) == FAIL)
3717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return FAIL;
3720 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003721 if (evaluate)
3722 {
3723 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3724
3725 clear_tv(&var2);
3726 return ret;
3727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
3730 return OK;
3731}
3732
3733/*
3734 * Handle fourth level expression:
3735 * + number addition
3736 * - number subtraction
3737 * . string concatenation
3738 *
3739 * "arg" must point to the first non-white of the expression.
3740 * "arg" is advanced to the next non-white after the recognized expression.
3741 *
3742 * Return OK or FAIL.
3743 */
3744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 typval_T var2;
3748 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003750 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003751#ifdef FEAT_FLOAT
3752 float_T f1 = 0, f2 = 0;
3753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 char_u *s1, *s2;
3755 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3756 char_u *p;
3757
3758 /*
3759 * Get the first variable.
3760 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003761 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return FAIL;
3763
3764 /*
3765 * Repeat computing, until no '+', '-' or '.' is following.
3766 */
3767 for (;;)
3768 {
3769 op = **arg;
3770 if (op != '+' && op != '-' && op != '.')
3771 break;
3772
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003773 if ((op != '+' || (rettv->v_type != VAR_LIST
3774 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775#ifdef FEAT_FLOAT
3776 && (op == '.' || rettv->v_type != VAR_FLOAT)
3777#endif
3778 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 {
3780 /* For "list + ...", an illegal use of the first operand as
3781 * a number cannot be determined before evaluating the 2nd
3782 * operand: if this is also a list, all is ok.
3783 * For "something . ...", "something - ..." or "non-list + ...",
3784 * we know that the first operand needs to be a string or number
3785 * without evaluating the 2nd operand. So check before to avoid
3786 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003787 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 {
3789 clear_tv(rettv);
3790 return FAIL;
3791 }
3792 }
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 /*
3795 * Get the second variable.
3796 */
3797 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003798 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802 }
3803
3804 if (evaluate)
3805 {
3806 /*
3807 * Compute the result.
3808 */
3809 if (op == '.')
3810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003811 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3812 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 if (s2 == NULL) /* type error ? */
3814 {
3815 clear_tv(rettv);
3816 clear_tv(&var2);
3817 return FAIL;
3818 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003819 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
3821 rettv->v_type = VAR_STRING;
3822 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003824 else if (op == '+' && rettv->v_type == VAR_BLOB
3825 && var2.v_type == VAR_BLOB)
3826 {
3827 blob_T *b1 = rettv->vval.v_blob;
3828 blob_T *b2 = var2.vval.v_blob;
3829 blob_T *b = blob_alloc();
3830 int i;
3831
3832 if (b != NULL)
3833 {
3834 for (i = 0; i < blob_len(b1); i++)
3835 ga_append(&b->bv_ga, blob_get(b1, i));
3836 for (i = 0; i < blob_len(b2); i++)
3837 ga_append(&b->bv_ga, blob_get(b2, i));
3838
3839 clear_tv(rettv);
3840 rettv_blob_set(rettv, b);
3841 }
3842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003843 else if (op == '+' && rettv->v_type == VAR_LIST
3844 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003845 {
3846 /* concatenate Lists */
3847 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3848 &var3) == FAIL)
3849 {
3850 clear_tv(rettv);
3851 clear_tv(&var2);
3852 return FAIL;
3853 }
3854 clear_tv(rettv);
3855 *rettv = var3;
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else
3858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 int error = FALSE;
3860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003861#ifdef FEAT_FLOAT
3862 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003864 f1 = rettv->vval.v_float;
3865 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003867 else
3868#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003870 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003871 if (error)
3872 {
3873 /* This can only happen for "list + non-list". For
3874 * "non-list + ..." or "something - ...", we returned
3875 * before evaluating the 2nd operand. */
3876 clear_tv(rettv);
3877 return FAIL;
3878 }
3879#ifdef FEAT_FLOAT
3880 if (var2.v_type == VAR_FLOAT)
3881 f1 = n1;
3882#endif
3883 }
3884#ifdef FEAT_FLOAT
3885 if (var2.v_type == VAR_FLOAT)
3886 {
3887 f2 = var2.vval.v_float;
3888 n2 = 0;
3889 }
3890 else
3891#endif
3892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003894 if (error)
3895 {
3896 clear_tv(rettv);
3897 clear_tv(&var2);
3898 return FAIL;
3899 }
3900#ifdef FEAT_FLOAT
3901 if (rettv->v_type == VAR_FLOAT)
3902 f2 = n2;
3903#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003906
3907#ifdef FEAT_FLOAT
3908 /* If there is a float on either side the result is a float. */
3909 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3910 {
3911 if (op == '+')
3912 f1 = f1 + f2;
3913 else
3914 f1 = f1 - f2;
3915 rettv->v_type = VAR_FLOAT;
3916 rettv->vval.v_float = f1;
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003919#endif
3920 {
3921 if (op == '+')
3922 n1 = n1 + n2;
3923 else
3924 n1 = n1 - n2;
3925 rettv->v_type = VAR_NUMBER;
3926 rettv->vval.v_number = n1;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003929 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 }
3932 return OK;
3933}
3934
3935/*
3936 * Handle fifth level expression:
3937 * * number multiplication
3938 * / number division
3939 * % number modulo
3940 *
3941 * "arg" must point to the first non-white of the expression.
3942 * "arg" is advanced to the next non-white after the recognized expression.
3943 *
3944 * Return OK or FAIL.
3945 */
3946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003947eval6(
3948 char_u **arg,
3949 typval_T *rettv,
3950 int evaluate,
3951 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003955 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003956#ifdef FEAT_FLOAT
3957 int use_float = FALSE;
3958 float_T f1 = 0, f2;
3959#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003960 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /*
3963 * Get the first variable.
3964 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003965 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FAIL;
3967
3968 /*
3969 * Repeat computing, until no '*', '/' or '%' is following.
3970 */
3971 for (;;)
3972 {
3973 op = **arg;
3974 if (op != '*' && op != '/' && op != '%')
3975 break;
3976
3977 if (evaluate)
3978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003979#ifdef FEAT_FLOAT
3980 if (rettv->v_type == VAR_FLOAT)
3981 {
3982 f1 = rettv->vval.v_float;
3983 use_float = TRUE;
3984 n1 = 0;
3985 }
3986 else
3987#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003988 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 if (error)
3991 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
3994 n1 = 0;
3995
3996 /*
3997 * Get the second variable.
3998 */
3999 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004000 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return FAIL;
4002
4003 if (evaluate)
4004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004005#ifdef FEAT_FLOAT
4006 if (var2.v_type == VAR_FLOAT)
4007 {
4008 if (!use_float)
4009 {
4010 f1 = n1;
4011 use_float = TRUE;
4012 }
4013 f2 = var2.vval.v_float;
4014 n2 = 0;
4015 }
4016 else
4017#endif
4018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004020 clear_tv(&var2);
4021 if (error)
4022 return FAIL;
4023#ifdef FEAT_FLOAT
4024 if (use_float)
4025 f2 = n2;
4026#endif
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
4030 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004031 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004033#ifdef FEAT_FLOAT
4034 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004036 if (op == '*')
4037 f1 = f1 * f2;
4038 else if (op == '/')
4039 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004040# ifdef VMS
4041 /* VMS crashes on divide by zero, work around it */
4042 if (f2 == 0.0)
4043 {
4044 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004045 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004046 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004047 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004048 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004049 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004050 }
4051 else
4052 f1 = f1 / f2;
4053# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004054 /* We rely on the floating point library to handle divide
4055 * by zero to result in "inf" and not a crash. */
4056 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004057# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004061 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 return FAIL;
4063 }
4064 rettv->v_type = VAR_FLOAT;
4065 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070 if (op == '*')
4071 n1 = n1 * n2;
4072 else if (op == '/')
4073 {
4074 if (n2 == 0) /* give an error message? */
4075 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004076 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004077 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004078 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004079 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004080 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004081 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 }
4083 else
4084 n1 = n1 / n2;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004087 {
4088 if (n2 == 0) /* give an error message? */
4089 n1 = 0;
4090 else
4091 n1 = n1 % n2;
4092 }
4093 rettv->v_type = VAR_NUMBER;
4094 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 }
4098
4099 return OK;
4100}
4101
4102/*
4103 * Handle sixth level expression:
4104 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004105 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004106 * "string" string constant
4107 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * &option-name option value
4109 * @r register contents
4110 * identifier variable value
4111 * function() function call
4112 * $VAR environment variable
4113 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004114 * [expr, expr] List
4115 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * Also handle:
4118 * ! in front logical NOT
4119 * - in front unary minus
4120 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 * trailing [] subscript in String or List
4122 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * "arg" must point to the first non-white of the expression.
4125 * "arg" is advanced to the next non-white after the recognized expression.
4126 *
4127 * Return OK or FAIL.
4128 */
4129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004130eval7(
4131 char_u **arg,
4132 typval_T *rettv,
4133 int evaluate,
4134 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004136 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 int len;
4138 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u *start_leader, *end_leader;
4140 int ret = OK;
4141 char_u *alias;
4142
4143 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004150 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 */
4152 start_leader = *arg;
4153 while (**arg == '!' || **arg == '-' || **arg == '+')
4154 *arg = skipwhite(*arg + 1);
4155 end_leader = *arg;
4156
4157 switch (**arg)
4158 {
4159 /*
4160 * Number constant.
4161 */
4162 case '0':
4163 case '1':
4164 case '2':
4165 case '3':
4166 case '4':
4167 case '5':
4168 case '6':
4169 case '7':
4170 case '8':
4171 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004172 {
4173#ifdef FEAT_FLOAT
4174 char_u *p = skipdigits(*arg + 1);
4175 int get_float = FALSE;
4176
4177 /* We accept a float when the format matches
4178 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004179 * strict to avoid backwards compatibility problems.
4180 * Don't look for a float after the "." operator, so that
4181 * ":let vers = 1.2.3" doesn't fail. */
4182 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004184 get_float = TRUE;
4185 p = skipdigits(p + 2);
4186 if (*p == 'e' || *p == 'E')
4187 {
4188 ++p;
4189 if (*p == '-' || *p == '+')
4190 ++p;
4191 if (!vim_isdigit(*p))
4192 get_float = FALSE;
4193 else
4194 p = skipdigits(p + 1);
4195 }
4196 if (ASCII_ISALPHA(*p) || *p == '.')
4197 get_float = FALSE;
4198 }
4199 if (get_float)
4200 {
4201 float_T f;
4202
4203 *arg += string2float(*arg, &f);
4204 if (evaluate)
4205 {
4206 rettv->v_type = VAR_FLOAT;
4207 rettv->vval.v_float = f;
4208 }
4209 }
4210 else
4211#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004212 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004213 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004214 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004215 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004216
4217 // Blob constant: 0z0123456789abcdef
4218 if (evaluate)
4219 blob = blob_alloc();
4220 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4221 {
4222 if (!vim_isxdigit(bp[1]))
4223 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004224 if (blob != NULL)
4225 {
4226 EMSG(_("E973: Blob literal should have an even number of hex characters"));
4227 ga_clear(&blob->bv_ga);
4228 VIM_CLEAR(blob);
4229 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004230 ret = FAIL;
4231 break;
4232 }
4233 if (blob != NULL)
4234 ga_append(&blob->bv_ga,
4235 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
4236 }
4237 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004238 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004239 *arg = bp;
4240 }
4241 else
4242 {
4243 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004244 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 *arg += len;
4246 if (evaluate)
4247 {
4248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = n;
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /*
4256 * String constant: "string".
4257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 break;
4260
4261 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 break;
4266
4267 /*
4268 * List: [expr, expr]
4269 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272
4273 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004274 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004275 * Dictionary: {key: val, key: val}
4276 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004277 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4278 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004279 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004280 break;
4281
4282 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004283 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004285 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 break;
4287
4288 /*
4289 * Environment variable: $VAR.
4290 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293
4294 /*
4295 * Register contents: @r.
4296 */
4297 case '@': ++*arg;
4298 if (evaluate)
4299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004301 rettv->vval.v_string = get_reg_contents(**arg,
4302 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 if (**arg != NUL)
4305 ++*arg;
4306 break;
4307
4308 /*
4309 * nested expression: (expression).
4310 */
4311 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (**arg == ')')
4314 ++*arg;
4315 else if (ret == OK)
4316 {
4317 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ret = FAIL;
4320 }
4321 break;
4322
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326
4327 if (ret == NOTDONE)
4328 {
4329 /*
4330 * Must be a variable or function name.
4331 * Can also be a curly-braces kind of name: {expr}.
4332 */
4333 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004334 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004335 if (alias != NULL)
4336 s = alias;
4337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004338 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 ret = FAIL;
4340 else
4341 {
4342 if (**arg == '(') /* recursive! */
4343 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004344 partial_T *partial;
4345
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004346 if (!evaluate)
4347 check_vars(s, len);
4348
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 /* If "s" is the name of a variable of type VAR_FUNC
4350 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004351 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004353 /* Need to make a copy, in case evaluating the arguments makes
4354 * the name invalid. */
4355 s = vim_strsave(s);
4356 if (s == NULL)
4357 ret = FAIL;
4358 else
4359 /* Invoke the function. */
4360 ret = get_func_tv(s, len, rettv, arg,
4361 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4362 &len, evaluate, partial, NULL);
4363 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004364
4365 /* If evaluate is FALSE rettv->v_type was not set in
4366 * get_func_tv, but it's needed in handle_subscript() to parse
4367 * what follows. So set it here. */
4368 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4369 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004370 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004371 rettv->v_type = VAR_FUNC;
4372 }
4373
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 /* Stop the expression evaluation when immediately
4375 * aborting on error, or when an interrupt occurred or
4376 * an exception was thrown but not caught. */
4377 if (aborting())
4378 {
4379 if (ret == OK)
4380 clear_tv(rettv);
4381 ret = FAIL;
4382 }
4383 }
4384 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004385 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004386 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004387 {
4388 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004389 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004391 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004392 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393 }
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 *arg = skipwhite(*arg);
4396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004397 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4398 * expr(expr). */
4399 if (ret == OK)
4400 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /*
4403 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4404 */
4405 if (ret == OK && evaluate && end_leader > start_leader)
4406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004408 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004409#ifdef FEAT_FLOAT
4410 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004412 if (rettv->v_type == VAR_FLOAT)
4413 f = rettv->vval.v_float;
4414 else
4415#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004416 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 clear_tv(rettv);
4420 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 else
4423 {
4424 while (end_leader > start_leader)
4425 {
4426 --end_leader;
4427 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428 {
4429#ifdef FEAT_FLOAT
4430 if (rettv->v_type == VAR_FLOAT)
4431 f = !f;
4432 else
4433#endif
4434 val = !val;
4435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004436 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 {
4438#ifdef FEAT_FLOAT
4439 if (rettv->v_type == VAR_FLOAT)
4440 f = -f;
4441 else
4442#endif
4443 val = -val;
4444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004446#ifdef FEAT_FLOAT
4447 if (rettv->v_type == VAR_FLOAT)
4448 {
4449 clear_tv(rettv);
4450 rettv->vval.v_float = f;
4451 }
4452 else
4453#endif
4454 {
4455 clear_tv(rettv);
4456 rettv->v_type = VAR_NUMBER;
4457 rettv->vval.v_number = val;
4458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 return ret;
4463}
4464
4465/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004466 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4467 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4469 */
4470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004471eval_index(
4472 char_u **arg,
4473 typval_T *rettv,
4474 int evaluate,
4475 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476{
4477 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004479 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004480 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 long len = -1;
4482 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004483 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485
Bram Moolenaara03f2332016-02-06 18:09:59 +01004486 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004488 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004489 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004490 if (verbose)
4491 EMSG(_("E695: Cannot index a Funcref"));
4492 return FAIL;
4493 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004494#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004495 if (verbose)
4496 EMSG(_(e_float_as_string));
4497 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004498#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004499 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004500 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004501 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004502 if (verbose)
4503 EMSG(_("E909: Cannot index a special variable"));
4504 return FAIL;
4505 case VAR_UNKNOWN:
4506 if (evaluate)
4507 return FAIL;
4508 /* FALLTHROUGH */
4509
4510 case VAR_STRING:
4511 case VAR_NUMBER:
4512 case VAR_LIST:
4513 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004514 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004518 init_tv(&var1);
4519 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 /*
4523 * dict.name
4524 */
4525 key = *arg + 1;
4526 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4527 ;
4528 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 }
4532 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 /*
4535 * something[idx]
4536 *
4537 * Get the (first) variable from inside the [].
4538 */
4539 *arg = skipwhite(*arg + 1);
4540 if (**arg == ':')
4541 empty1 = TRUE;
4542 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4543 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004544 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 {
4546 /* not a number or string */
4547 clear_tv(&var1);
4548 return FAIL;
4549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550
4551 /*
4552 * Get the second variable from inside the [:].
4553 */
4554 if (**arg == ':')
4555 {
4556 range = TRUE;
4557 *arg = skipwhite(*arg + 1);
4558 if (**arg == ']')
4559 empty2 = TRUE;
4560 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 if (!empty1)
4563 clear_tv(&var1);
4564 return FAIL;
4565 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004566 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 {
4568 /* not a number or string */
4569 if (!empty1)
4570 clear_tv(&var1);
4571 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 return FAIL;
4573 }
4574 }
4575
4576 /* Check for the ']'. */
4577 if (**arg != ']')
4578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004579 if (verbose)
4580 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 clear_tv(&var1);
4582 if (range)
4583 clear_tv(&var2);
4584 return FAIL;
4585 }
4586 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 }
4588
4589 if (evaluate)
4590 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 n1 = 0;
4592 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 }
4597 if (range)
4598 {
4599 if (empty2)
4600 n2 = -1;
4601 else
4602 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004603 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 }
4606 }
4607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004610 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004611 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004612 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004613 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004614 case VAR_SPECIAL:
4615 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004616 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004617 break; /* not evaluating, skipping over subscript */
4618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 case VAR_NUMBER:
4620 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 len = (long)STRLEN(s);
4623 if (range)
4624 {
4625 /* The resulting variable is a substring. If the indexes
4626 * are out of range the result is empty. */
4627 if (n1 < 0)
4628 {
4629 n1 = len + n1;
4630 if (n1 < 0)
4631 n1 = 0;
4632 }
4633 if (n2 < 0)
4634 n2 = len + n2;
4635 else if (n2 >= len)
4636 n2 = len;
4637 if (n1 >= len || n2 < 0 || n1 > n2)
4638 s = NULL;
4639 else
4640 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4641 }
4642 else
4643 {
4644 /* The resulting variable is a string of a single
4645 * character. If the index is too big or negative the
4646 * result is empty. */
4647 if (n1 >= len || n1 < 0)
4648 s = NULL;
4649 else
4650 s = vim_strnsave(s + n1, 1);
4651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(rettv);
4653 rettv->v_type = VAR_STRING;
4654 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 break;
4656
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004657 case VAR_BLOB:
4658 len = blob_len(rettv->vval.v_blob);
4659 if (range)
4660 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004661 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004662 // are out of range the result is empty.
4663 if (n1 < 0)
4664 {
4665 n1 = len + n1;
4666 if (n1 < 0)
4667 n1 = 0;
4668 }
4669 if (n2 < 0)
4670 n2 = len + n2;
4671 else if (n2 >= len)
4672 n2 = len - 1;
4673 if (n1 >= len || n2 < 0 || n1 > n2)
4674 {
4675 clear_tv(rettv);
4676 rettv->v_type = VAR_BLOB;
4677 rettv->vval.v_blob = NULL;
4678 }
4679 else
4680 {
4681 blob_T *blob = blob_alloc();
4682
4683 if (blob != NULL)
4684 {
4685 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4686 {
4687 blob_free(blob);
4688 return FAIL;
4689 }
4690 blob->bv_ga.ga_len = n2 - n1 + 1;
4691 for (i = n1; i <= n2; i++)
4692 blob_set(blob, i - n1,
4693 blob_get(rettv->vval.v_blob, i));
4694
4695 clear_tv(rettv);
4696 rettv_blob_set(rettv, blob);
4697 }
4698 }
4699 }
4700 else
4701 {
4702 // The resulting variable is a string of a single
4703 // character. If the index is too big or negative the
4704 // result is empty.
4705 if (n1 < len && n1 >= 0)
4706 {
4707 int v = (int)blob_get(rettv->vval.v_blob, n1);
4708
4709 clear_tv(rettv);
4710 rettv->v_type = VAR_NUMBER;
4711 rettv->vval.v_number = v;
4712 }
4713 else
4714 EMSGN(_(e_blobidx), n1);
4715 }
4716 break;
4717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 if (n1 < 0)
4721 n1 = len + n1;
4722 if (!empty1 && (n1 < 0 || n1 >= len))
4723 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004724 /* For a range we allow invalid values and return an empty
4725 * list. A list index out of range is an error. */
4726 if (!range)
4727 {
4728 if (verbose)
4729 EMSGN(_(e_listidx), n1);
4730 return FAIL;
4731 }
4732 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 }
4734 if (range)
4735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 list_T *l;
4737 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738
4739 if (n2 < 0)
4740 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004741 else if (n2 >= len)
4742 n2 = len - 1;
4743 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004744 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 l = list_alloc();
4746 if (l == NULL)
4747 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 n1 <= n2; ++n1)
4750 {
4751 if (list_append_tv(l, &item->li_tv) == FAIL)
4752 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004753 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 return FAIL;
4755 }
4756 item = item->li_next;
4757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004759 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761 else
4762 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004763 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004766 }
4767 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004768
4769 case VAR_DICT:
4770 if (range)
4771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004772 if (verbose)
4773 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 if (len == -1)
4775 clear_tv(&var1);
4776 return FAIL;
4777 }
4778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780
4781 if (len == -1)
4782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004784 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 clear_tv(&var1);
4787 return FAIL;
4788 }
4789 }
4790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004791 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004792
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004793 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795 if (len == -1)
4796 clear_tv(&var1);
4797 if (item == NULL)
4798 return FAIL;
4799
4800 copy_tv(&item->di_tv, &var1);
4801 clear_tv(rettv);
4802 *rettv = var1;
4803 }
4804 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806 }
4807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 return OK;
4809}
4810
4811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 * Get an option value.
4813 * "arg" points to the '&' or '+' before the option name.
4814 * "arg" is advanced to character after the option name.
4815 * Return OK or FAIL.
4816 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004818get_option_tv(
4819 char_u **arg,
4820 typval_T *rettv, /* when NULL, only check if option exists */
4821 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 char_u *option_end;
4824 long numval;
4825 char_u *stringval;
4826 int opt_type;
4827 int c;
4828 int working = (**arg == '+'); /* has("+option") */
4829 int ret = OK;
4830 int opt_flags;
4831
4832 /*
4833 * Isolate the option name and find its value.
4834 */
4835 option_end = find_option_end(arg, &opt_flags);
4836 if (option_end == NULL)
4837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 EMSG2(_("E112: Option name missing: %s"), *arg);
4840 return FAIL;
4841 }
4842
4843 if (!evaluate)
4844 {
4845 *arg = option_end;
4846 return OK;
4847 }
4848
4849 c = *option_end;
4850 *option_end = NUL;
4851 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 if (opt_type == -3) /* invalid name */
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 EMSG2(_("E113: Unknown option: %s"), *arg);
4858 ret = FAIL;
4859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 if (opt_type == -2) /* hidden string option */
4863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 rettv->v_type = VAR_STRING;
4865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 else if (opt_type == -1) /* hidden number option */
4868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 rettv->v_type = VAR_NUMBER;
4870 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 else if (opt_type == 1) /* number option */
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 rettv->v_type = VAR_NUMBER;
4875 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 else /* string option */
4878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 rettv->v_type = VAR_STRING;
4880 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 }
4883 else if (working && (opt_type == -2 || opt_type == -1))
4884 ret = FAIL;
4885
4886 *option_end = c; /* put back for error messages */
4887 *arg = option_end;
4888
4889 return ret;
4890}
4891
4892/*
4893 * Allocate a variable for a string constant.
4894 * Return OK or FAIL.
4895 */
4896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004897get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 char_u *p;
4900 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int extra = 0;
4902
4903 /*
4904 * Find the end of the string, skipping backslashed characters.
4905 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004906 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
4908 if (*p == '\\' && p[1] != NUL)
4909 {
4910 ++p;
4911 /* A "\<x>" form occupies at least 4 characters, and produces up
4912 * to 6 characters: reserve space for 2 extra */
4913 if (*p == '<')
4914 extra += 2;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917
4918 if (*p != '"')
4919 {
4920 EMSG2(_("E114: Missing quote: %s"), *arg);
4921 return FAIL;
4922 }
4923
4924 /* If only parsing, set *arg and return here */
4925 if (!evaluate)
4926 {
4927 *arg = p + 1;
4928 return OK;
4929 }
4930
4931 /*
4932 * Copy the string into allocated memory, handling backslashed
4933 * characters.
4934 */
4935 name = alloc((unsigned)(p - *arg + extra));
4936 if (name == NULL)
4937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 rettv->v_type = VAR_STRING;
4939 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 if (*p == '\\')
4944 {
4945 switch (*++p)
4946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 case 'b': *name++ = BS; ++p; break;
4948 case 'e': *name++ = ESC; ++p; break;
4949 case 'f': *name++ = FF; ++p; break;
4950 case 'n': *name++ = NL; ++p; break;
4951 case 'r': *name++ = CAR; ++p; break;
4952 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 case 'X': /* hex: "\x1", "\x12" */
4955 case 'x':
4956 case 'u': /* Unicode: "\u0023" */
4957 case 'U':
4958 if (vim_isxdigit(p[1]))
4959 {
4960 int n, nr;
4961 int c = toupper(*p);
4962
4963 if (c == 'X')
4964 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004965 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004967 else
4968 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 nr = 0;
4970 while (--n >= 0 && vim_isxdigit(p[1]))
4971 {
4972 ++p;
4973 nr = (nr << 4) + hex2nr(*p);
4974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#ifdef FEAT_MBYTE
4977 /* For "\u" store the number according to
4978 * 'encoding'. */
4979 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
4982#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
4986
4987 /* octal: "\1", "\12", "\123" */
4988 case '0':
4989 case '1':
4990 case '2':
4991 case '3':
4992 case '4':
4993 case '5':
4994 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 case '7': *name = *p++ - '0';
4996 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 *name = (*name << 3) + *p++ - '0';
4999 if (*p >= '0' && *p <= '7')
5000 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004
5005 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005006 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (extra != 0)
5008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 break;
5011 }
5012 /* FALLTHROUGH */
5013
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 break;
5016 }
5017 }
5018 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005023 if (*p != NUL) /* just in case */
5024 ++p;
5025 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 return OK;
5028}
5029
5030/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * Return OK or FAIL.
5033 */
5034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005035get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005038 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 int reduce = 0;
5040
5041 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005044 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 break;
5050 ++reduce;
5051 ++p;
5052 }
5053 }
5054
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 return FAIL;
5059 }
5060
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 if (!evaluate)
5063 {
5064 *arg = p + 1;
5065 return OK;
5066 }
5067
5068 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 */
5071 str = alloc((unsigned)((p - *arg) - reduce));
5072 if (str == NULL)
5073 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 rettv->v_type = VAR_STRING;
5075 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 break;
5083 ++p;
5084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 *arg = p + 1;
5089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 return OK;
5091}
5092
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005093/*
5094 * Return the function name of the partial.
5095 */
5096 char_u *
5097partial_name(partial_T *pt)
5098{
5099 if (pt->pt_name != NULL)
5100 return pt->pt_name;
5101 return pt->pt_func->uf_name;
5102}
5103
Bram Moolenaarddecc252016-04-06 22:59:37 +02005104 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005106{
5107 int i;
5108
5109 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005111 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005113 if (pt->pt_name != NULL)
5114 {
5115 func_unref(pt->pt_name);
5116 vim_free(pt->pt_name);
5117 }
5118 else
5119 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005120 vim_free(pt);
5121}
5122
5123/*
5124 * Unreference a closure: decrement the reference count and free it when it
5125 * becomes zero.
5126 */
5127 void
5128partial_unref(partial_T *pt)
5129{
5130 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005131 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005132}
5133
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005134static int tv_equal_recurse_limit;
5135
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005136 static int
5137func_equal(
5138 typval_T *tv1,
5139 typval_T *tv2,
5140 int ic) /* ignore case */
5141{
5142 char_u *s1, *s2;
5143 dict_T *d1, *d2;
5144 int a1, a2;
5145 int i;
5146
5147 /* empty and NULL function name considered the same */
5148 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005149 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005150 if (s1 != NULL && *s1 == NUL)
5151 s1 = NULL;
5152 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005153 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005154 if (s2 != NULL && *s2 == NUL)
5155 s2 = NULL;
5156 if (s1 == NULL || s2 == NULL)
5157 {
5158 if (s1 != s2)
5159 return FALSE;
5160 }
5161 else if (STRCMP(s1, s2) != 0)
5162 return FALSE;
5163
5164 /* empty dict and NULL dict is different */
5165 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5166 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5167 if (d1 == NULL || d2 == NULL)
5168 {
5169 if (d1 != d2)
5170 return FALSE;
5171 }
5172 else if (!dict_equal(d1, d2, ic, TRUE))
5173 return FALSE;
5174
5175 /* empty list and no list considered the same */
5176 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5177 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5178 if (a1 != a2)
5179 return FALSE;
5180 for (i = 0; i < a1; ++i)
5181 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5182 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5183 return FALSE;
5184
5185 return TRUE;
5186}
5187
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005188/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005190 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005191 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005194tv_equal(
5195 typval_T *tv1,
5196 typval_T *tv2,
5197 int ic, /* ignore case */
5198 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005199{
5200 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005202 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005203 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005204
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005205 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005206 * recursiveness to a limit. We guess they are equal then.
5207 * A fixed limit has the problem of still taking an awful long time.
5208 * Reduce the limit every time running into it. That should work fine for
5209 * deeply linked structures that are not recursively linked and catch
5210 * recursiveness quickly. */
5211 if (!recursive)
5212 tv_equal_recurse_limit = 1000;
5213 if (recursive_cnt >= tv_equal_recurse_limit)
5214 {
5215 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005216 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005217 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005218
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005219 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5220 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005221 if ((tv1->v_type == VAR_FUNC
5222 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5223 && (tv2->v_type == VAR_FUNC
5224 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5225 {
5226 ++recursive_cnt;
5227 r = func_equal(tv1, tv2, ic);
5228 --recursive_cnt;
5229 return r;
5230 }
5231
5232 if (tv1->v_type != tv2->v_type)
5233 return FALSE;
5234
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005235 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005236 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005237 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005238 ++recursive_cnt;
5239 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005242
5243 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005244 ++recursive_cnt;
5245 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5246 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005247 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005248
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005249 case VAR_BLOB:
5250 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5251
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005252 case VAR_NUMBER:
5253 return tv1->vval.v_number == tv2->vval.v_number;
5254
5255 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005256 s1 = tv_get_string_buf(tv1, buf1);
5257 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005258 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005259
5260 case VAR_SPECIAL:
5261 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005262
5263 case VAR_FLOAT:
5264#ifdef FEAT_FLOAT
5265 return tv1->vval.v_float == tv2->vval.v_float;
5266#endif
5267 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005269 return tv1->vval.v_job == tv2->vval.v_job;
5270#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005273 return tv1->vval.v_channel == tv2->vval.v_channel;
5274#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005275 case VAR_FUNC:
5276 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005277 case VAR_UNKNOWN:
5278 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005279 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005280
Bram Moolenaara03f2332016-02-06 18:09:59 +01005281 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5282 * does not equal anything, not even itself. */
5283 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005284}
5285
5286/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005287 * Return the next (unique) copy ID.
5288 * Used for serializing nested structures.
5289 */
5290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005292{
5293 current_copyID += COPYID_INC;
5294 return current_copyID;
5295}
5296
5297/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298 * Garbage collection for lists and dictionaries.
5299 *
5300 * We use reference counts to be able to free most items right away when they
5301 * are no longer used. But for composite items it's possible that it becomes
5302 * unused while the reference count is > 0: When there is a recursive
5303 * reference. Example:
5304 * :let l = [1, 2, 3]
5305 * :let d = {9: l}
5306 * :let l[1] = d
5307 *
5308 * Since this is quite unusual we handle this with garbage collection: every
5309 * once in a while find out which lists and dicts are not referenced from any
5310 * variable.
5311 *
5312 * Here is a good reference text about garbage collection (refers to Python
5313 * but it applies to all reference-counting mechanisms):
5314 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005316
5317/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005318 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005319 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005320 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005321 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005322 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005323garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005324{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005325 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327 buf_T *buf;
5328 win_T *wp;
5329 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005330 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005331 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005332
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005333 if (!testing)
5334 {
5335 /* Only do this once. */
5336 want_garbage_collect = FALSE;
5337 may_garbage_collect = FALSE;
5338 garbage_collect_at_exit = FALSE;
5339 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005340
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005341 /* We advance by two because we add one for items referenced through
5342 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005343 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005344
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005345 /*
5346 * 1. Go through all accessible variables and mark all lists and dicts
5347 * with copyID.
5348 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005349
5350 /* Don't free variables in the previous_funccal list unless they are only
5351 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005352 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005354
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005355 /* script-local variables */
5356 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005358
5359 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005360 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005361 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5362 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005363
5364 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5367 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005368 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005369 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5370 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005372 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005373 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005374 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5375 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005376 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005377 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005378
5379 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005382 /* named functions (matters for closures) */
5383 abort = abort || set_ref_in_functions(copyID);
5384
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005385 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005387
Bram Moolenaard812df62008-11-09 12:46:09 +00005388 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005390
Bram Moolenaar1dced572012-04-05 16:54:08 +02005391#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005392 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005393#endif
5394
Bram Moolenaardb913952012-06-29 12:54:53 +02005395#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005397#endif
5398
5399#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005400 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005401#endif
5402
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005403#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005404 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005405 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005406#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005407#ifdef FEAT_NETBEANS_INTG
5408 abort = abort || set_ref_in_nb_channel(copyID);
5409#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005410
Bram Moolenaare3188e22016-05-31 21:13:04 +02005411#ifdef FEAT_TIMERS
5412 abort = abort || set_ref_in_timer(copyID);
5413#endif
5414
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005415#ifdef FEAT_QUICKFIX
5416 abort = abort || set_ref_in_quickfix(copyID);
5417#endif
5418
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005419#ifdef FEAT_TERMINAL
5420 abort = abort || set_ref_in_term(copyID);
5421#endif
5422
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005423 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005424 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 /*
5426 * 2. Free lists and dictionaries that are not referenced.
5427 */
5428 did_free = free_unref_items(copyID);
5429
5430 /*
5431 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005432 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005435 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005436 else if (p_verbose > 0)
5437 {
5438 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5439 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005440
5441 return did_free;
5442}
5443
5444/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005445 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005446 */
5447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005448free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005449{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005450 int did_free = FALSE;
5451
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 /* Let all "free" functions know that we are here. This means no
5453 * dictionaries, lists, channels or jobs are to be freed, because we will
5454 * do that here. */
5455 in_free_unref_items = TRUE;
5456
5457 /*
5458 * PASS 1: free the contents of the items. We don't free the items
5459 * themselves yet, so that it is possible to decrement refcount counters
5460 */
5461
Bram Moolenaarcd524592016-07-17 14:57:05 +02005462 /* Go through the list of dicts and free items without the copyID. */
5463 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005464
Bram Moolenaarda861d62016-07-17 15:46:27 +02005465 /* Go through the list of lists and free items without the copyID. */
5466 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005467
5468#ifdef FEAT_JOB_CHANNEL
5469 /* Go through the list of jobs and free items without the copyID. This
5470 * must happen before doing channels, because jobs refer to channels, but
5471 * the reference from the channel to the job isn't tracked. */
5472 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5473
5474 /* Go through the list of channels and free items without the copyID. */
5475 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5476#endif
5477
5478 /*
5479 * PASS 2: free the items themselves.
5480 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005481 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005482 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005484#ifdef FEAT_JOB_CHANNEL
5485 /* Go through the list of jobs and free items without the copyID. This
5486 * must happen before doing channels, because jobs refer to channels, but
5487 * the reference from the channel to the job isn't tracked. */
5488 free_unused_jobs(copyID, COPYID_MASK);
5489
5490 /* Go through the list of channels and free items without the copyID. */
5491 free_unused_channels(copyID, COPYID_MASK);
5492#endif
5493
5494 in_free_unref_items = FALSE;
5495
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005496 return did_free;
5497}
5498
5499/*
5500 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 * "list_stack" is used to add lists to be marked. Can be NULL.
5502 *
5503 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005504 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005506set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005507{
5508 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005509 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005510 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 hashtab_T *cur_ht;
5512 ht_stack_T *ht_stack = NULL;
5513 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005514
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 cur_ht = ht;
5516 for (;;)
5517 {
5518 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005519 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005520 /* Mark each item in the hashtab. If the item contains a hashtab
5521 * it is added to ht_stack, if it contains a list it is added to
5522 * list_stack. */
5523 todo = (int)cur_ht->ht_used;
5524 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5525 if (!HASHITEM_EMPTY(hi))
5526 {
5527 --todo;
5528 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5529 &ht_stack, list_stack);
5530 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532
5533 if (ht_stack == NULL)
5534 break;
5535
5536 /* take an item from the stack */
5537 cur_ht = ht_stack->ht;
5538 tempitem = ht_stack;
5539 ht_stack = ht_stack->prev;
5540 free(tempitem);
5541 }
5542
5543 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005544}
5545
5546/*
5547 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005548 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5549 *
5550 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005554{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005555 listitem_T *li;
5556 int abort = FALSE;
5557 list_T *cur_l;
5558 list_stack_T *list_stack = NULL;
5559 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005560
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005561 cur_l = l;
5562 for (;;)
5563 {
5564 if (!abort)
5565 /* Mark each item in the list. If the item contains a hashtab
5566 * it is added to ht_stack, if it contains a list it is added to
5567 * list_stack. */
5568 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5569 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5570 ht_stack, &list_stack);
5571 if (list_stack == NULL)
5572 break;
5573
5574 /* take an item from the stack */
5575 cur_l = list_stack->list;
5576 tempitem = list_stack;
5577 list_stack = list_stack->prev;
5578 free(tempitem);
5579 }
5580
5581 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582}
5583
5584/*
5585 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005586 * "list_stack" is used to add lists to be marked. Can be NULL.
5587 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5588 *
5589 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005590 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005592set_ref_in_item(
5593 typval_T *tv,
5594 int copyID,
5595 ht_stack_T **ht_stack,
5596 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005597{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005598 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005599
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005600 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005601 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005602 dict_T *dd = tv->vval.v_dict;
5603
Bram Moolenaara03f2332016-02-06 18:09:59 +01005604 if (dd != NULL && dd->dv_copyID != copyID)
5605 {
5606 /* Didn't see this dict yet. */
5607 dd->dv_copyID = copyID;
5608 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005609 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005610 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5611 }
5612 else
5613 {
5614 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5615 if (newitem == NULL)
5616 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005617 else
5618 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005619 newitem->ht = &dd->dv_hashtab;
5620 newitem->prev = *ht_stack;
5621 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005622 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005623 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005624 }
5625 }
5626 else if (tv->v_type == VAR_LIST)
5627 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005628 list_T *ll = tv->vval.v_list;
5629
Bram Moolenaara03f2332016-02-06 18:09:59 +01005630 if (ll != NULL && ll->lv_copyID != copyID)
5631 {
5632 /* Didn't see this list yet. */
5633 ll->lv_copyID = copyID;
5634 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005635 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005636 abort = set_ref_in_list(ll, copyID, ht_stack);
5637 }
5638 else
5639 {
5640 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005641 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005642 if (newitem == NULL)
5643 abort = TRUE;
5644 else
5645 {
5646 newitem->list = ll;
5647 newitem->prev = *list_stack;
5648 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005650 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005651 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005652 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005653 else if (tv->v_type == VAR_FUNC)
5654 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005655 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005656 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005657 else if (tv->v_type == VAR_PARTIAL)
5658 {
5659 partial_T *pt = tv->vval.v_partial;
5660 int i;
5661
5662 /* A partial does not have a copyID, because it cannot contain itself.
5663 */
5664 if (pt != NULL)
5665 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005666 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005667
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005668 if (pt->pt_dict != NULL)
5669 {
5670 typval_T dtv;
5671
5672 dtv.v_type = VAR_DICT;
5673 dtv.vval.v_dict = pt->pt_dict;
5674 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5675 }
5676
5677 for (i = 0; i < pt->pt_argc; ++i)
5678 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5679 ht_stack, list_stack);
5680 }
5681 }
5682#ifdef FEAT_JOB_CHANNEL
5683 else if (tv->v_type == VAR_JOB)
5684 {
5685 job_T *job = tv->vval.v_job;
5686 typval_T dtv;
5687
5688 if (job != NULL && job->jv_copyID != copyID)
5689 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005690 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005691 if (job->jv_channel != NULL)
5692 {
5693 dtv.v_type = VAR_CHANNEL;
5694 dtv.vval.v_channel = job->jv_channel;
5695 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5696 }
5697 if (job->jv_exit_partial != NULL)
5698 {
5699 dtv.v_type = VAR_PARTIAL;
5700 dtv.vval.v_partial = job->jv_exit_partial;
5701 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5702 }
5703 }
5704 }
5705 else if (tv->v_type == VAR_CHANNEL)
5706 {
5707 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005708 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005709 typval_T dtv;
5710 jsonq_T *jq;
5711 cbq_T *cq;
5712
5713 if (ch != NULL && ch->ch_copyID != copyID)
5714 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005715 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005716 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005717 {
5718 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5719 jq = jq->jq_next)
5720 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5721 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5722 cq = cq->cq_next)
5723 if (cq->cq_partial != NULL)
5724 {
5725 dtv.v_type = VAR_PARTIAL;
5726 dtv.vval.v_partial = cq->cq_partial;
5727 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5728 }
5729 if (ch->ch_part[part].ch_partial != NULL)
5730 {
5731 dtv.v_type = VAR_PARTIAL;
5732 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5733 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5734 }
5735 }
5736 if (ch->ch_partial != NULL)
5737 {
5738 dtv.v_type = VAR_PARTIAL;
5739 dtv.vval.v_partial = ch->ch_partial;
5740 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5741 }
5742 if (ch->ch_close_partial != NULL)
5743 {
5744 dtv.v_type = VAR_PARTIAL;
5745 dtv.vval.v_partial = ch->ch_close_partial;
5746 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5747 }
5748 }
5749 }
5750#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005751 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005752}
5753
Bram Moolenaar17a13432016-01-24 14:22:10 +01005754 static char *
5755get_var_special_name(int nr)
5756{
5757 switch (nr)
5758 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005759 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005760 case VVAL_TRUE: return "v:true";
5761 case VVAL_NONE: return "v:none";
5762 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005763 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005764 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005765 return "42";
5766}
5767
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 * Return a string with the string representation of a variable.
5770 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005772 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005773 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5774 * stings as "string()", otherwise does not put quotes around strings, as
5775 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005776 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5777 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005778 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005780 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005781echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005782 typval_T *tv,
5783 char_u **tofree,
5784 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005785 int copyID,
5786 int echo_style,
5787 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005788 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 static int recurse = 0;
5791 char_u *r = NULL;
5792
Bram Moolenaar33570922005-01-25 22:26:29 +00005793 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005794 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005795 if (!did_echo_string_emsg)
5796 {
5797 /* Only give this message once for a recursive call to avoid
5798 * flooding the user with errors. And stop iterating over lists
5799 * and dicts. */
5800 did_echo_string_emsg = TRUE;
5801 EMSG(_("E724: variable nested too deep for displaying"));
5802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005804 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005805 }
5806 ++recurse;
5807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 switch (tv->v_type)
5809 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005810 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005811 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005812 {
5813 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005814 r = tv->vval.v_string;
5815 if (r == NULL)
5816 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005817 }
5818 else
5819 {
5820 *tofree = string_quote(tv->vval.v_string, FALSE);
5821 r = *tofree;
5822 }
5823 break;
5824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005826 if (echo_style)
5827 {
5828 *tofree = NULL;
5829 r = tv->vval.v_string;
5830 }
5831 else
5832 {
5833 *tofree = string_quote(tv->vval.v_string, TRUE);
5834 r = *tofree;
5835 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005837
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005838 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005839 {
5840 partial_T *pt = tv->vval.v_partial;
5841 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005842 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005843 garray_T ga;
5844 int i;
5845 char_u *tf;
5846
5847 ga_init2(&ga, 1, 100);
5848 ga_concat(&ga, (char_u *)"function(");
5849 if (fname != NULL)
5850 {
5851 ga_concat(&ga, fname);
5852 vim_free(fname);
5853 }
5854 if (pt != NULL && pt->pt_argc > 0)
5855 {
5856 ga_concat(&ga, (char_u *)", [");
5857 for (i = 0; i < pt->pt_argc; ++i)
5858 {
5859 if (i > 0)
5860 ga_concat(&ga, (char_u *)", ");
5861 ga_concat(&ga,
5862 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5863 vim_free(tf);
5864 }
5865 ga_concat(&ga, (char_u *)"]");
5866 }
5867 if (pt != NULL && pt->pt_dict != NULL)
5868 {
5869 typval_T dtv;
5870
5871 ga_concat(&ga, (char_u *)", ");
5872 dtv.v_type = VAR_DICT;
5873 dtv.vval.v_dict = pt->pt_dict;
5874 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5875 vim_free(tf);
5876 }
5877 ga_concat(&ga, (char_u *)")");
5878
5879 *tofree = ga.ga_data;
5880 r = *tofree;
5881 break;
5882 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005883
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005884 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005885 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005886 break;
5887
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005889 if (tv->vval.v_list == NULL)
5890 {
5891 *tofree = NULL;
5892 r = NULL;
5893 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005894 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5895 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005896 {
5897 *tofree = NULL;
5898 r = (char_u *)"[...]";
5899 }
5900 else
5901 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005902 int old_copyID = tv->vval.v_list->lv_copyID;
5903
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005905 *tofree = list2string(tv, copyID, restore_copyID);
5906 if (restore_copyID)
5907 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005908 r = *tofree;
5909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005911
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005913 if (tv->vval.v_dict == NULL)
5914 {
5915 *tofree = NULL;
5916 r = NULL;
5917 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005918 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5919 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005920 {
5921 *tofree = NULL;
5922 r = (char_u *)"{...}";
5923 }
5924 else
5925 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005926 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005927 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005928 *tofree = dict2string(tv, copyID, restore_copyID);
5929 if (restore_copyID)
5930 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005931 r = *tofree;
5932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005936 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005937 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005938 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005939 break;
5940
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005942 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005943 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005944 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005945 if (composite_val)
5946 {
5947 *tofree = string_quote(r, FALSE);
5948 r = *tofree;
5949 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005952 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005953#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005954 *tofree = NULL;
5955 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5956 r = numbuf;
5957 break;
5958#endif
5959
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005960 case VAR_SPECIAL:
5961 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005962 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005963 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965
Bram Moolenaar8502c702014-06-17 12:51:16 +02005966 if (--recurse == 0)
5967 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969}
5970
5971/*
5972 * Return a string with the string representation of a variable.
5973 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5974 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005975 * Does not put quotes around strings, as ":echo" displays values.
5976 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5977 * May return NULL.
5978 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005979 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005980echo_string(
5981 typval_T *tv,
5982 char_u **tofree,
5983 char_u *numbuf,
5984 int copyID)
5985{
5986 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5987}
5988
5989/*
5990 * Return a string with the string representation of a variable.
5991 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5992 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005994 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005996 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997tv2string(
5998 typval_T *tv,
5999 char_u **tofree,
6000 char_u *numbuf,
6001 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006002{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006003 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004}
6005
6006/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 * Return string "str" in ' quotes, doubling ' characters.
6008 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 char_u *p, *r, *s;
6016
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 len = (function ? 13 : 3);
6018 if (str != NULL)
6019 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006020 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006021 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 if (*p == '\'')
6023 ++len;
6024 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025 s = r = alloc(len);
6026 if (r != NULL)
6027 {
6028 if (function)
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 r += 10;
6032 }
6033 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 if (str != NULL)
6036 for (p = str; *p != NUL; )
6037 {
6038 if (*p == '\'')
6039 *r++ = '\'';
6040 MB_COPY_CHAR(p, r);
6041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006043 if (function)
6044 *r++ = ')';
6045 *r++ = NUL;
6046 }
6047 return s;
6048}
6049
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006050#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006051/*
6052 * Convert the string "text" to a floating point number.
6053 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6054 * this always uses a decimal point.
6055 * Returns the length of the text that was consumed.
6056 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058string2float(
6059 char_u *text,
6060 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006061{
6062 char *s = (char *)text;
6063 float_T f;
6064
Bram Moolenaar62473612017-01-08 19:25:40 +01006065 /* MS-Windows does not deal with "inf" and "nan" properly. */
6066 if (STRNICMP(text, "inf", 3) == 0)
6067 {
6068 *value = INFINITY;
6069 return 3;
6070 }
6071 if (STRNICMP(text, "-inf", 3) == 0)
6072 {
6073 *value = -INFINITY;
6074 return 4;
6075 }
6076 if (STRNICMP(text, "nan", 3) == 0)
6077 {
6078 *value = NAN;
6079 return 3;
6080 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006081 f = strtod(s, &s);
6082 *value = f;
6083 return (int)((char_u *)s - text);
6084}
6085#endif
6086
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 * Get the value of an environment variable.
6089 * "arg" is pointing to the '$'. It is advanced to after the name.
6090 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006091 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 */
6093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *string = NULL;
6097 int len;
6098 int cc;
6099 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006100 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 ++*arg;
6103 name = *arg;
6104 len = get_env_len(arg);
6105 if (evaluate)
6106 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006107 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006108 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006109
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006110 cc = name[len];
6111 name[len] = NUL;
6112 /* first try vim_getenv(), fast for normal environment vars */
6113 string = vim_getenv(name, &mustfree);
6114 if (string != NULL && *string != NUL)
6115 {
6116 if (!mustfree)
6117 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006119 else
6120 {
6121 if (mustfree)
6122 vim_free(string);
6123
6124 /* next try expanding things like $VIM and ${HOME} */
6125 string = expand_env_save(name - 1);
6126 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006127 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006128 }
6129 name[len] = cc;
6130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006131 rettv->v_type = VAR_STRING;
6132 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 }
6134
6135 return OK;
6136}
6137
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006138
6139
6140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006142 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006145var2fpos(
6146 typval_T *varp,
6147 int dollar_lnum, /* TRUE when $ is last line */
6148 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006150 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006152 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaara5525202006-03-02 22:52:09 +00006154 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006155 if (varp->v_type == VAR_LIST)
6156 {
6157 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006158 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006159 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006160 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006161
6162 l = varp->vval.v_list;
6163 if (l == NULL)
6164 return NULL;
6165
6166 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006167 pos.lnum = list_find_nr(l, 0L, &error);
6168 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006169 return NULL; /* invalid line number */
6170
6171 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006172 pos.col = list_find_nr(l, 1L, &error);
6173 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006174 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006175 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006176
6177 /* We accept "$" for the column number: last column. */
6178 li = list_find(l, 1L);
6179 if (li != NULL && li->li_tv.v_type == VAR_STRING
6180 && li->li_tv.vval.v_string != NULL
6181 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6182 pos.col = len + 1;
6183
Bram Moolenaara5525202006-03-02 22:52:09 +00006184 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006185 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006186 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006187 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006188
Bram Moolenaara5525202006-03-02 22:52:09 +00006189#ifdef FEAT_VIRTUALEDIT
6190 /* Get the virtual offset. Defaults to zero. */
6191 pos.coladd = list_find_nr(l, 2L, &error);
6192 if (error)
6193 pos.coladd = 0;
6194#endif
6195
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 return &pos;
6197 }
6198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006199 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006200 if (name == NULL)
6201 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006202 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006204 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6205 {
6206 if (VIsual_active)
6207 return &VIsual;
6208 return &curwin->w_cursor;
6209 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006210 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006212 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6214 return NULL;
6215 return pp;
6216 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006217
6218#ifdef FEAT_VIRTUALEDIT
6219 pos.coladd = 0;
6220#endif
6221
Bram Moolenaar477933c2007-07-17 14:32:23 +00006222 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006223 {
6224 pos.col = 0;
6225 if (name[1] == '0') /* "w0": first visible line */
6226 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006227 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006228 /* In silent Ex mode topline is zero, but that's not a valid line
6229 * number; use one instead. */
6230 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006231 return &pos;
6232 }
6233 else if (name[1] == '$') /* "w$": last visible line */
6234 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006235 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006236 /* In silent Ex mode botline is zero, return zero then. */
6237 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006238 return &pos;
6239 }
6240 }
6241 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006243 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 pos.lnum = curbuf->b_ml.ml_line_count;
6246 pos.col = 0;
6247 }
6248 else
6249 {
6250 pos.lnum = curwin->w_cursor.lnum;
6251 pos.col = (colnr_T)STRLEN(ml_get_curline());
6252 }
6253 return &pos;
6254 }
6255 return NULL;
6256}
6257
6258/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006259 * Convert list in "arg" into a position and optional file number.
6260 * When "fnump" is NULL there is no file number, only 3 items.
6261 * Note that the column is passed on as-is, the caller may want to decrement
6262 * it to use 1 for the first column.
6263 * Return FAIL when conversion is not possible, doesn't check the position for
6264 * validity.
6265 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006267list2fpos(
6268 typval_T *arg,
6269 pos_T *posp,
6270 int *fnump,
6271 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006272{
6273 list_T *l = arg->vval.v_list;
6274 long i = 0;
6275 long n;
6276
Bram Moolenaar493c1782014-05-28 14:34:46 +02006277 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6278 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006279 if (arg->v_type != VAR_LIST
6280 || l == NULL
6281 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006282 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006283 return FAIL;
6284
6285 if (fnump != NULL)
6286 {
6287 n = list_find_nr(l, i++, NULL); /* fnum */
6288 if (n < 0)
6289 return FAIL;
6290 if (n == 0)
6291 n = curbuf->b_fnum; /* current buffer */
6292 *fnump = n;
6293 }
6294
6295 n = list_find_nr(l, i++, NULL); /* lnum */
6296 if (n < 0)
6297 return FAIL;
6298 posp->lnum = n;
6299
6300 n = list_find_nr(l, i++, NULL); /* col */
6301 if (n < 0)
6302 return FAIL;
6303 posp->col = n;
6304
6305#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006306 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006307 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006308 posp->coladd = 0;
6309 else
6310 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006311#endif
6312
Bram Moolenaar493c1782014-05-28 14:34:46 +02006313 if (curswantp != NULL)
6314 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6315
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006316 return OK;
6317}
6318
6319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 * Get the length of an environment variable name.
6321 * Advance "arg" to the first character after the name.
6322 * Return 0 for error.
6323 */
6324 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u *p;
6328 int len;
6329
6330 for (p = *arg; vim_isIDc(*p); ++p)
6331 ;
6332 if (p == *arg) /* no name found */
6333 return 0;
6334
6335 len = (int)(p - *arg);
6336 *arg = p;
6337 return len;
6338}
6339
6340/*
6341 * Get the length of the name of a function or internal variable.
6342 * "arg" is advanced to the first non-white character after the name.
6343 * Return 0 if something is wrong.
6344 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 char_u *p;
6349 int len;
6350
6351 /* Find the end of the name. */
6352 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006353 {
6354 if (*p == ':')
6355 {
6356 /* "s:" is start of "s:var", but "n:" is not and can be used in
6357 * slice "[n:]". Also "xx:" is not a namespace. */
6358 len = (int)(p - *arg);
6359 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6360 || len > 1)
6361 break;
6362 }
6363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 if (p == *arg) /* no name found */
6365 return 0;
6366
6367 len = (int)(p - *arg);
6368 *arg = skipwhite(p);
6369
6370 return len;
6371}
6372
6373/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006374 * Get the length of the name of a variable or function.
6375 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006377 * Return -1 if curly braces expansion failed.
6378 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 * If the name contains 'magic' {}'s, expand them and return the
6380 * expanded name in an allocated string via 'alias' - caller must free.
6381 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383get_name_len(
6384 char_u **arg,
6385 char_u **alias,
6386 int evaluate,
6387 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 char_u *p;
6391 char_u *expr_start;
6392 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394 *alias = NULL; /* default to no alias */
6395
6396 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6397 && (*arg)[2] == (int)KE_SNR)
6398 {
6399 /* hard coded <SNR>, already translated */
6400 *arg += 3;
6401 return get_id_len(arg) + 3;
6402 }
6403 len = eval_fname_script(*arg);
6404 if (len > 0)
6405 {
6406 /* literal "<SID>", "s:" or "<SNR>" */
6407 *arg += len;
6408 }
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006411 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006413 p = find_name_end(*arg, &expr_start, &expr_end,
6414 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (expr_start != NULL)
6416 {
6417 char_u *temp_string;
6418
6419 if (!evaluate)
6420 {
6421 len += (int)(p - *arg);
6422 *arg = skipwhite(p);
6423 return len;
6424 }
6425
6426 /*
6427 * Include any <SID> etc in the expanded string:
6428 * Thus the -len here.
6429 */
6430 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6431 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006432 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 *alias = temp_string;
6434 *arg = skipwhite(p);
6435 return (int)STRLEN(temp_string);
6436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006439 // Only give an error when there is something, otherwise it will be
6440 // reported at a higher level.
6441 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 EMSG2(_(e_invexpr2), *arg);
6443
6444 return len;
6445}
6446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006447/*
6448 * Find the end of a variable or function name, taking care of magic braces.
6449 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6450 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006451 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006452 * Return a pointer to just after the name. Equal to "arg" if there is no
6453 * valid name.
6454 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006455 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456find_name_end(
6457 char_u *arg,
6458 char_u **expr_start,
6459 char_u **expr_end,
6460 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 int mb_nest = 0;
6463 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006465 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006469 *expr_start = NULL;
6470 *expr_end = NULL;
6471 }
6472
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006473 /* Quick check for valid starting character. */
6474 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6475 return arg;
6476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006477 for (p = arg; *p != NUL
6478 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006479 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006480 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006482 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006483 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006484 if (*p == '\'')
6485 {
6486 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006487 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006488 ;
6489 if (*p == NUL)
6490 break;
6491 }
6492 else if (*p == '"')
6493 {
6494 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006495 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006496 if (*p == '\\' && p[1] != NUL)
6497 ++p;
6498 if (*p == NUL)
6499 break;
6500 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006501 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6502 {
6503 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006504 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006505 len = (int)(p - arg);
6506 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006507 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006508 break;
6509 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006511 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006513 if (*p == '[')
6514 ++br_nest;
6515 else if (*p == ']')
6516 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006521 if (*p == '{')
6522 {
6523 mb_nest++;
6524 if (expr_start != NULL && *expr_start == NULL)
6525 *expr_start = p;
6526 }
6527 else if (*p == '}')
6528 {
6529 mb_nest--;
6530 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6531 *expr_end = p;
6532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535
6536 return p;
6537}
6538
6539/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006540 * Expands out the 'magic' {}'s in a variable/function name.
6541 * Note that this can call itself recursively, to deal with
6542 * constructs like foo{bar}{baz}{bam}
6543 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6544 * "in_start" ^
6545 * "expr_start" ^
6546 * "expr_end" ^
6547 * "in_end" ^
6548 *
6549 * Returns a new allocated string, which the caller must free.
6550 * Returns NULL for failure.
6551 */
6552 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553make_expanded_name(
6554 char_u *in_start,
6555 char_u *expr_start,
6556 char_u *expr_end,
6557 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006558{
6559 char_u c1;
6560 char_u *retval = NULL;
6561 char_u *temp_result;
6562 char_u *nextcmd = NULL;
6563
6564 if (expr_end == NULL || in_end == NULL)
6565 return NULL;
6566 *expr_start = NUL;
6567 *expr_end = NUL;
6568 c1 = *in_end;
6569 *in_end = NUL;
6570
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006571 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006572 if (temp_result != NULL && nextcmd == NULL)
6573 {
6574 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6575 + (in_end - expr_end) + 1));
6576 if (retval != NULL)
6577 {
6578 STRCPY(retval, in_start);
6579 STRCAT(retval, temp_result);
6580 STRCAT(retval, expr_end + 1);
6581 }
6582 }
6583 vim_free(temp_result);
6584
6585 *in_end = c1; /* put char back for error messages */
6586 *expr_start = '{';
6587 *expr_end = '}';
6588
6589 if (retval != NULL)
6590 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006591 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006592 if (expr_start != NULL)
6593 {
6594 /* Further expansion! */
6595 temp_result = make_expanded_name(retval, expr_start,
6596 expr_end, temp_result);
6597 vim_free(retval);
6598 retval = temp_result;
6599 }
6600 }
6601
6602 return retval;
6603}
6604
6605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006612 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6613}
6614
6615/*
6616 * Return TRUE if character "c" can be used as the first character in a
6617 * variable or function name (excluding '{' and '}').
6618 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006621{
6622 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
6625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 * Set number v: variable to "val".
6627 */
6628 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006629set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006631 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633
6634/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006635 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006637 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006638get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006640 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006643/*
6644 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006645 * If the String variable has never been set, return an empty string.
6646 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006647 */
6648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006649get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006650{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006651 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006652}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006655 * Get List v: variable value. Caller must take care of reference count when
6656 * needed.
6657 */
6658 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006659get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006660{
6661 return vimvars[idx].vv_list;
6662}
6663
6664/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006665 * Get Dict v: variable value. Caller must take care of reference count when
6666 * needed.
6667 */
6668 dict_T *
6669get_vim_var_dict(int idx)
6670{
6671 return vimvars[idx].vv_dict;
6672}
6673
6674/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006675 * Set v:char to character "c".
6676 */
6677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006679{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006680 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006681
6682#ifdef FEAT_MBYTE
6683 if (has_mbyte)
6684 buf[(*mb_char2bytes)(c, buf)] = NUL;
6685 else
6686#endif
6687 {
6688 buf[0] = c;
6689 buf[1] = NUL;
6690 }
6691 set_vim_var_string(VV_CHAR, buf, -1);
6692}
6693
6694/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006695 * Set v:count to "count" and v:count1 to "count1".
6696 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 */
6698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006699set_vcount(
6700 long count,
6701 long count1,
6702 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006704 if (set_prevcount)
6705 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706 vimvars[VV_COUNT].vv_nr = count;
6707 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708}
6709
6710/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006711 * Save variables that might be changed as a side effect. Used when executing
6712 * a timer callback.
6713 */
6714 void
6715save_vimvars(vimvars_save_T *vvsave)
6716{
6717 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6718 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6719 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6720}
6721
6722/*
6723 * Restore variables saved by save_vimvars().
6724 */
6725 void
6726restore_vimvars(vimvars_save_T *vvsave)
6727{
6728 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6729 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6730 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6731}
6732
6733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 * Set string v: variable to a copy of "val".
6735 */
6736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737set_vim_var_string(
6738 int idx,
6739 char_u *val,
6740 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
Bram Moolenaara542c682016-01-31 16:28:04 +01006742 clear_tv(&vimvars[idx].vv_di.di_tv);
6743 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006747 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006749 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750}
6751
6752/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006753 * Set List v: variable to "val".
6754 */
6755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006756set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006757{
Bram Moolenaara542c682016-01-31 16:28:04 +01006758 clear_tv(&vimvars[idx].vv_di.di_tv);
6759 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006760 vimvars[idx].vv_list = val;
6761 if (val != NULL)
6762 ++val->lv_refcount;
6763}
6764
6765/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006766 * Set Dictionary v: variable to "val".
6767 */
6768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006769set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006770{
Bram Moolenaara542c682016-01-31 16:28:04 +01006771 clear_tv(&vimvars[idx].vv_di.di_tv);
6772 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006773 vimvars[idx].vv_dict = val;
6774 if (val != NULL)
6775 {
6776 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006777 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006778 }
6779}
6780
6781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 * Set v:register if needed.
6783 */
6784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006785set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 char_u regname;
6788
6789 if (c == 0 || c == ' ')
6790 regname = '"';
6791 else
6792 regname = c;
6793 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006794 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 set_vim_var_string(VV_REG, &regname, 1);
6796}
6797
6798/*
6799 * Get or set v:exception. If "oldval" == NULL, return the current value.
6800 * Otherwise, restore the value to "oldval" and return NULL.
6801 * Must always be called in pairs to save and restore v:exception! Does not
6802 * take care of memory allocations.
6803 */
6804 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006805v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006808 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 return NULL;
6812}
6813
6814/*
6815 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6816 * Otherwise, restore the value to "oldval" and return NULL.
6817 * Must always be called in pairs to save and restore v:throwpoint! Does not
6818 * take care of memory allocations.
6819 */
6820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaare9a41262005-01-15 22:18:47 +00006826 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 return NULL;
6828}
6829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830/*
6831 * Set v:cmdarg.
6832 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6833 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6834 * Must always be called in pairs!
6835 */
6836 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006837set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 char_u *oldval;
6840 char_u *newval;
6841 unsigned len;
6842
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006844 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006846 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006848 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006851 if (eap->force_bin == FORCE_BIN)
6852 len = 6;
6853 else if (eap->force_bin == FORCE_NOBIN)
6854 len = 8;
6855 else
6856 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006857
6858 if (eap->read_edit)
6859 len += 7;
6860
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006861 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006862 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006863# ifdef FEAT_MBYTE
6864 if (eap->force_enc != 0)
6865 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006866 if (eap->bad_char != 0)
6867 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006868# endif
6869
6870 newval = alloc(len + 1);
6871 if (newval == NULL)
6872 return NULL;
6873
6874 if (eap->force_bin == FORCE_BIN)
6875 sprintf((char *)newval, " ++bin");
6876 else if (eap->force_bin == FORCE_NOBIN)
6877 sprintf((char *)newval, " ++nobin");
6878 else
6879 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006880
6881 if (eap->read_edit)
6882 STRCAT(newval, " ++edit");
6883
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006884 if (eap->force_ff != 0)
6885 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006886 eap->force_ff == 'u' ? "unix"
6887 : eap->force_ff == 'd' ? "dos"
6888 : "mac");
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006889#ifdef FEAT_MBYTE
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006890 if (eap->force_enc != 0)
6891 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6892 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006893 if (eap->bad_char == BAD_KEEP)
6894 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6895 else if (eap->bad_char == BAD_DROP)
6896 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6897 else if (eap->bad_char != 0)
6898 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006899#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006901 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904/*
6905 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006906 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006909get_var_tv(
6910 char_u *name,
6911 int len, /* length of "name" */
6912 typval_T *rettv, /* NULL when only checking existence */
6913 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6914 int verbose, /* may give error message */
6915 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
6917 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 /* truncate the name, so that we can use strcmp() */
6923 cc = name[len];
6924 name[len] = NUL;
6925
6926 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 * Check for user-defined variables.
6928 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006929 v = find_var(name, NULL, no_autoload);
6930 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006932 tv = &v->di_tv;
6933 if (dip != NULL)
6934 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006939 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006940 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 ret = FAIL;
6942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006943 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006944 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 name[len] = cc;
6947
6948 return ret;
6949}
6950
6951/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006952 * Check if variable "name[len]" is a local variable or an argument.
6953 * If so, "*eval_lavars_used" is set to TRUE.
6954 */
6955 static void
6956check_vars(char_u *name, int len)
6957{
6958 int cc;
6959 char_u *varname;
6960 hashtab_T *ht;
6961
6962 if (eval_lavars_used == NULL)
6963 return;
6964
6965 /* truncate the name, so that we can use strcmp() */
6966 cc = name[len];
6967 name[len] = NUL;
6968
6969 ht = find_var_ht(name, &varname);
6970 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6971 {
6972 if (find_var(name, NULL, TRUE) != NULL)
6973 *eval_lavars_used = TRUE;
6974 }
6975
6976 name[len] = cc;
6977}
6978
6979/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006980 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6981 * Also handle function call with Funcref variable: func(expr)
6982 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6983 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006985handle_subscript(
6986 char_u **arg,
6987 typval_T *rettv,
6988 int evaluate, /* do more than finding the end */
6989 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006990{
6991 int ret = OK;
6992 dict_T *selfdict = NULL;
6993 char_u *s;
6994 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006996
6997 while (ret == OK
6998 && (**arg == '['
6999 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007000 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7001 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007002 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007003 {
7004 if (**arg == '(')
7005 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007006 partial_T *pt = NULL;
7007
Bram Moolenaard9fba312005-06-26 22:34:35 +00007008 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007009 if (evaluate)
7010 {
7011 functv = *rettv;
7012 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007013
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007014 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007015 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007016 {
7017 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007018 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007019 }
7020 else
7021 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007022 }
7023 else
7024 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007025 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007026 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007027 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007028
7029 /* Clear the funcref afterwards, so that deleting it while
7030 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007031 if (evaluate)
7032 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007033
7034 /* Stop the expression evaluation when immediately aborting on
7035 * error, or when an interrupt occurred or an exception was thrown
7036 * but not caught. */
7037 if (aborting())
7038 {
7039 if (ret == OK)
7040 clear_tv(rettv);
7041 ret = FAIL;
7042 }
7043 dict_unref(selfdict);
7044 selfdict = NULL;
7045 }
7046 else /* **arg == '[' || **arg == '.' */
7047 {
7048 dict_unref(selfdict);
7049 if (rettv->v_type == VAR_DICT)
7050 {
7051 selfdict = rettv->vval.v_dict;
7052 if (selfdict != NULL)
7053 ++selfdict->dv_refcount;
7054 }
7055 else
7056 selfdict = NULL;
7057 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7058 {
7059 clear_tv(rettv);
7060 ret = FAIL;
7061 }
7062 }
7063 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007064
Bram Moolenaar1d429612016-05-24 15:44:17 +02007065 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7066 * Don't do this when "Func" is already a partial that was bound
7067 * explicitly (pt_auto is FALSE). */
7068 if (selfdict != NULL
7069 && (rettv->v_type == VAR_FUNC
7070 || (rettv->v_type == VAR_PARTIAL
7071 && (rettv->vval.v_partial->pt_auto
7072 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007073 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007075 dict_unref(selfdict);
7076 return ret;
7077}
7078
7079/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007080 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007081 * value).
7082 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007083 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007084alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007085{
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007087}
7088
7089/*
7090 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 * The string "s" must have been allocated, it is consumed.
7092 * Return NULL for out of memory, the variable otherwise.
7093 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007094 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007099 rettv = alloc_tv();
7100 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007102 rettv->v_type = VAR_STRING;
7103 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 }
7105 else
7106 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007107 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108}
7109
7110/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007111 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007114free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 if (varp != NULL)
7117 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 switch (varp->v_type)
7119 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007120 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007121 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007122 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007123 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007124 vim_free(varp->vval.v_string);
7125 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007126 case VAR_PARTIAL:
7127 partial_unref(varp->vval.v_partial);
7128 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007129 case VAR_BLOB:
7130 blob_unref(varp->vval.v_blob);
7131 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007132 case VAR_LIST:
7133 list_unref(varp->vval.v_list);
7134 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007135 case VAR_DICT:
7136 dict_unref(varp->vval.v_dict);
7137 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007138 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007140 job_unref(varp->vval.v_job);
7141 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007142#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007143 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007145 channel_unref(varp->vval.v_channel);
7146 break;
7147#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007148 case VAR_NUMBER:
7149 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007150 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007151 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007152 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 vim_free(varp);
7155 }
7156}
7157
7158/*
7159 * Free the memory for a variable value and set the value to NULL or 0.
7160 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007162clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 if (varp != NULL)
7165 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007169 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007170 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007171 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007172 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007173 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007174 case VAR_PARTIAL:
7175 partial_unref(varp->vval.v_partial);
7176 varp->vval.v_partial = NULL;
7177 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007178 case VAR_BLOB:
7179 blob_unref(varp->vval.v_blob);
7180 varp->vval.v_blob = NULL;
7181 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 case VAR_LIST:
7183 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007185 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186 case VAR_DICT:
7187 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007190 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007191 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007192 varp->vval.v_number = 0;
7193 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007194 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007195#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007196 varp->vval.v_float = 0.0;
7197 break;
7198#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007199 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007200#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007201 job_unref(varp->vval.v_job);
7202 varp->vval.v_job = NULL;
7203#endif
7204 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007205 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007206#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007207 channel_unref(varp->vval.v_channel);
7208 varp->vval.v_channel = NULL;
7209#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007210 case VAR_UNKNOWN:
7211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007213 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215}
7216
7217/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007218 * Set the value of a variable to NULL without freeing items.
7219 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007222{
7223 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225}
7226
7227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 * Get the number value of a variable.
7229 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007230 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007231 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007232 * caller of incompatible types: it sets *denote to TRUE if "denote"
7233 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007235 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007236tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007238 int error = FALSE;
7239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007240 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007241}
7242
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007243 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007244tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007245{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007246 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007248 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007250 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007251 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007252 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007253#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007254 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007255 break;
7256#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007257 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007258 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007259 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007260 break;
7261 case VAR_STRING:
7262 if (varp->vval.v_string != NULL)
7263 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007264 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007266 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007267 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007268 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007269 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007270 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007271 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007272 case VAR_SPECIAL:
7273 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7274 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007275 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007276#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007277 EMSG(_("E910: Using a Job as a Number"));
7278 break;
7279#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007280 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007281#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007282 EMSG(_("E913: Using a Channel as a Number"));
7283 break;
7284#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007285 case VAR_BLOB:
7286 EMSG(_("E974: Using a Blob as a Number"));
7287 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007288 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007292 if (denote == NULL) /* useful for values that must be unsigned */
7293 n = -1;
7294 else
7295 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007296 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297}
7298
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007299#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007301tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007302{
7303 switch (varp->v_type)
7304 {
7305 case VAR_NUMBER:
7306 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007307 case VAR_FLOAT:
7308 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007309 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007310 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007311 EMSG(_("E891: Using a Funcref as a Float"));
7312 break;
7313 case VAR_STRING:
7314 EMSG(_("E892: Using a String as a Float"));
7315 break;
7316 case VAR_LIST:
7317 EMSG(_("E893: Using a List as a Float"));
7318 break;
7319 case VAR_DICT:
7320 EMSG(_("E894: Using a Dictionary as a Float"));
7321 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007322 case VAR_SPECIAL:
7323 EMSG(_("E907: Using a special value as a Float"));
7324 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007326# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007327 EMSG(_("E911: Using a Job as a Float"));
7328 break;
7329# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007330 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007331# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007332 EMSG(_("E914: Using a Channel as a Float"));
7333 break;
7334# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007335 case VAR_BLOB:
7336 EMSG(_("E975: Using a Blob as a Float"));
7337 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007338 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007339 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007340 break;
7341 }
7342 return 0;
7343}
7344#endif
7345
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 * Get the string value of a variable.
7348 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007349 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7350 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 * If the String variable has never been set, return an empty string.
7352 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007353 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007354 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007356 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007357tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
7359 static char_u mybuf[NUMBUFLEN];
7360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007361 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
7363
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007364 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007367 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007368
7369 return res != NULL ? res : (char_u *)"";
7370}
7371
Bram Moolenaar7d647822014-04-05 21:28:56 +02007372/*
7373 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7374 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007375 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007376tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007377{
7378 static char_u mybuf[NUMBUFLEN];
7379
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007380 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007381}
7382
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007383 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007384tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007385{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007389 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +02007390 (long long)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 return buf;
7392 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007393 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007394 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 break;
7396 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007397 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 break;
7399 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007400 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007402 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007403#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007404 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007405 break;
7406#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007407 case VAR_STRING:
7408 if (varp->vval.v_string != NULL)
7409 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007410 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007411 case VAR_SPECIAL:
7412 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7413 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007414 case VAR_BLOB:
7415 EMSG(_("E976: using Blob as a String"));
7416 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007417 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007418#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007419 {
7420 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007421 char *status;
7422
7423 if (job == NULL)
7424 return (char_u *)"no process";
7425 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007426 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007427 : "run";
7428# ifdef UNIX
7429 vim_snprintf((char *)buf, NUMBUFLEN,
7430 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007431# elif defined(WIN32)
7432 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007433 "process %ld %s",
7434 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007435 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007436# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007437 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007438 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7439# endif
7440 return buf;
7441 }
7442#endif
7443 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007444 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007445#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007446 {
7447 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007448 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007449
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007450 if (channel == NULL)
7451 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7452 else
7453 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007454 "channel %d %s", channel->ch_id, status);
7455 return buf;
7456 }
7457#endif
7458 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007459 case VAR_UNKNOWN:
7460 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007463 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464}
7465
7466/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007467 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7468 * string() on Dict, List, etc.
7469 */
7470 char_u *
7471tv_stringify(typval_T *varp, char_u *buf)
7472{
7473 if (varp->v_type == VAR_LIST
7474 || varp->v_type == VAR_DICT
7475 || varp->v_type == VAR_FUNC
7476 || varp->v_type == VAR_PARTIAL
7477 || varp->v_type == VAR_FLOAT)
7478 {
7479 typval_T tmp;
7480
7481 f_string(varp, &tmp);
7482 tv_get_string_buf(&tmp, buf);
7483 clear_tv(varp);
7484 *varp = tmp;
7485 return tmp.vval.v_string;
7486 }
7487 return tv_get_string_buf(varp, buf);
7488}
7489
7490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 * Find variable "name" in the list of variables.
7492 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007493 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007494 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007497 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007498find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007502 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaara7043832005-01-21 11:56:39 +00007504 ht = find_var_ht(name, &varname);
7505 if (htp != NULL)
7506 *htp = ht;
7507 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007509 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7510 if (ret != NULL)
7511 return ret;
7512
7513 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007514 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515}
7516
7517/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007518 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007519 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522find_var_in_ht(
7523 hashtab_T *ht,
7524 int htname,
7525 char_u *varname,
7526 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007527{
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 hashitem_T *hi;
7529
7530 if (*varname == NUL)
7531 {
7532 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007533 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007534 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007535 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007536 case 'g': return &globvars_var;
7537 case 'v': return &vimvars_var;
7538 case 'b': return &curbuf->b_bufvar;
7539 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007540 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007541 case 'l': return get_funccal_local_var();
7542 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 }
7544 return NULL;
7545 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007546
7547 hi = hash_find(ht, varname);
7548 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007549 {
7550 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007551 * worked find the variable again. Don't auto-load a script if it was
7552 * loaded already, otherwise it would be loaded every time when
7553 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007554 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007555 {
7556 /* Note: script_autoload() may make "hi" invalid. It must either
7557 * be obtained again or not used. */
7558 if (!script_autoload(varname, FALSE) || aborting())
7559 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007560 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007561 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007562 if (HASHITEM_EMPTY(hi))
7563 return NULL;
7564 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007566}
7567
7568/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007570 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007571 * Set "varname" to the start of name without ':'.
7572 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007573 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007576 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007577 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007578
Bram Moolenaar73627d02015-08-11 15:46:09 +02007579 if (name[0] == NUL)
7580 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (name[1] != ':')
7582 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007583 /* The name must not start with a colon or #. */
7584 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 return NULL;
7586 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007587
7588 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007589 hi = hash_find(&compat_hashtab, name);
7590 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007591 return &compat_hashtab;
7592
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007593 ht = get_funccal_local_ht();
7594 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007596 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
7598 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007599 if (*name == 'g') /* global variable */
7600 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007601 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7602 */
7603 if (vim_strchr(name + 2, ':') != NULL
7604 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007605 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007607 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007609 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007610 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007611 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 if (*name == 'v') /* v: variable */
7613 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007614 if (*name == 'a') /* a: function argument */
7615 return get_funccal_args_ht();
7616 if (*name == 'l') /* l: local function variable */
7617 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007619 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7620 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 return NULL;
7622}
7623
7624/*
7625 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007626 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 * Returns NULL when it doesn't exist.
7628 */
7629 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007630get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007634 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (v == NULL)
7636 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007637 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638}
7639
7640/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 * sourcing this script and when executing functions defined in the script.
7643 */
7644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
Bram Moolenaara7043832005-01-21 11:56:39 +00007647 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 hashtab_T *ht;
7649 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007650
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7652 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007653 /* Re-allocating ga_data means that an ht_array pointing to
7654 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007656 for (i = 1; i <= ga_scripts.ga_len; ++i)
7657 {
7658 ht = &SCRIPT_VARS(i);
7659 if (ht->ht_mask == HT_INIT_SIZE - 1)
7660 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007661 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007663 }
7664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 while (ga_scripts.ga_len < id)
7666 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007667 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007668 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007669 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672 }
7673}
7674
7675/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7677 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 */
7679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007683 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007684 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007685 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007686 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 dict_var->di_tv.vval.v_dict = dict;
7688 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007689 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7691 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692}
7693
7694/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007695 * Unreference a dictionary initialized by init_var_dict().
7696 */
7697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007698unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007699{
7700 /* Now the dict needs to be freed if no one else is using it, go back to
7701 * normal reference counting. */
7702 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7703 dict_unref(dict);
7704}
7705
7706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 * Frees all allocated variables and the value they contain.
7709 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007712vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007713{
7714 vars_clear_ext(ht, TRUE);
7715}
7716
7717/*
7718 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7719 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007721vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
Bram Moolenaara7043832005-01-21 11:56:39 +00007723 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 hashitem_T *hi;
7725 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007728 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007729 for (hi = ht->ht_array; todo > 0; ++hi)
7730 {
7731 if (!HASHITEM_EMPTY(hi))
7732 {
7733 --todo;
7734
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007736 * ht_array might change then. hash_clear() takes care of it
7737 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 v = HI2DI(hi);
7739 if (free_val)
7740 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007741 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007743 }
7744 }
7745 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007746 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747}
7748
Bram Moolenaara7043832005-01-21 11:56:39 +00007749/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 * Delete a variable from hashtab "ht" at item "hi".
7751 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007754delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007757
7758 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 clear_tv(&di->di_tv);
7760 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761}
7762
7763/*
7764 * List the value of one internal variable.
7765 */
7766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007767list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 char_u *tofree;
7770 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007773 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007775 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777}
7778
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007780list_one_var_a(
7781 char_u *prefix,
7782 char_u *name,
7783 int type,
7784 char_u *string,
7785 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
Bram Moolenaar31859182007-08-14 20:41:13 +00007787 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7788 msg_start();
7789 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 if (name != NULL) /* "a:" vars don't have a name stored */
7791 msg_puts(name);
7792 msg_putchar(' ');
7793 msg_advance(22);
7794 if (type == VAR_NUMBER)
7795 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007796 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797 msg_putchar('*');
7798 else if (type == VAR_LIST)
7799 {
7800 msg_putchar('[');
7801 if (*string == '[')
7802 ++string;
7803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007804 else if (type == VAR_DICT)
7805 {
7806 msg_putchar('{');
7807 if (*string == '{')
7808 ++string;
7809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 else
7811 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007815 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007817 if (*first)
7818 {
7819 msg_clr_eos();
7820 *first = FALSE;
7821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
7824/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 * If the variable already exists, the value is updated.
7827 * Otherwise the variable is created.
7828 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007830set_var(
7831 char_u *name,
7832 typval_T *tv,
7833 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
Bram Moolenaar33570922005-01-25 22:26:29 +00007835 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007837 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007839 ht = find_var_ht(name, &varname);
7840 if (ht == NULL || *varname == NUL)
7841 {
7842 EMSG2(_(e_illvar), name);
7843 return;
7844 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007845 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007846
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007847 /* Search in parent scope which is possible to reference from lambda */
7848 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007849 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007850
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007851 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7852 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007853 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007858 if (var_check_ro(v->di_flags, name, FALSE)
7859 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007861
7862 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007863 * Handle setting internal v: variables separately where needed to
7864 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 */
7866 if (ht == &vimvarht)
7867 {
7868 if (v->di_tv.v_type == VAR_STRING)
7869 {
7870 vim_free(v->di_tv.vval.v_string);
7871 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007872 v->di_tv.vval.v_string = vim_strsave(tv_get_string(tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 else
7874 {
7875 /* Take over the string to avoid an extra alloc/free. */
7876 v->di_tv.vval.v_string = tv->vval.v_string;
7877 tv->vval.v_string = NULL;
7878 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007879 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007881 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007883 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007884 if (STRCMP(varname, "searchforward") == 0)
7885 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007886#ifdef FEAT_SEARCH_EXTRA
7887 else if (STRCMP(varname, "hlsearch") == 0)
7888 {
7889 no_hlsearch = !v->di_tv.vval.v_number;
7890 redraw_all_later(SOME_VALID);
7891 }
7892#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007893 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007895 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007896 {
Bram Moolenaar74ea88c2018-12-04 22:37:49 +01007897 EMSG2(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007898 return;
7899 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 }
7901
7902 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 else /* add a new variable */
7905 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007906 /* Can't add "v:" variable. */
7907 if (ht == &vimvarht)
7908 {
7909 EMSG2(_(e_illvar), name);
7910 return;
7911 }
7912
Bram Moolenaar92124a32005-06-17 22:03:40 +00007913 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007914 if (!valid_varname(varname))
7915 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007916
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007917 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7918 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007919 if (v == NULL)
7920 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007924 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 return;
7926 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007927 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007929
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007930 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007932 else
7933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007934 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007935 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938}
7939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007941 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 * Also give an error message.
7943 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007945var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007946{
7947 if (flags & DI_FLAGS_RO)
7948 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007949 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 return TRUE;
7951 }
7952 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7953 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007954 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 return TRUE;
7956 }
7957 return FALSE;
7958}
7959
7960/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007961 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7962 * Also give an error message.
7963 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007965var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007966{
7967 if (flags & DI_FLAGS_FIX)
7968 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007969 EMSG2(_("E795: Cannot delete variable %s"),
7970 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007971 return TRUE;
7972 }
7973 return FALSE;
7974}
7975
7976/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007977 * Check if a funcref is assigned to a valid variable name.
7978 * Return TRUE and give an error if not.
7979 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007981var_check_func_name(
7982 char_u *name, /* points to start of variable name */
7983 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007984{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007985 /* Allow for w: b: s: and t:. */
7986 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007987 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7988 ? name[2] : name[0]))
7989 {
7990 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7991 name);
7992 return TRUE;
7993 }
7994 /* Don't allow hiding a function. When "v" is not NULL we might be
7995 * assigning another function to the same var, the type is checked
7996 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007997 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007998 {
7999 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
8000 name);
8001 return TRUE;
8002 }
8003 return FALSE;
8004}
8005
8006/*
8007 * Check if a variable name is valid.
8008 * Return FALSE and give an error if not.
8009 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008011valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008012{
8013 char_u *p;
8014
8015 for (p = varname; *p != NUL; ++p)
8016 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8017 && *p != AUTOLOAD_CHAR)
8018 {
8019 EMSG2(_(e_illvar), varname);
8020 return FALSE;
8021 }
8022 return TRUE;
8023}
8024
8025/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008026 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008027 * Also give an error message, using "name" or _("name") when use_gettext is
8028 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008029 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008031tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008032{
8033 if (lock & VAR_LOCKED)
8034 {
8035 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008036 name == NULL ? (char_u *)_("Unknown")
8037 : use_gettext ? (char_u *)_(name)
8038 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008039 return TRUE;
8040 }
8041 if (lock & VAR_FIXED)
8042 {
8043 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008044 name == NULL ? (char_u *)_("Unknown")
8045 : use_gettext ? (char_u *)_(name)
8046 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008047 return TRUE;
8048 }
8049 return FALSE;
8050}
8051
8052/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008056 * It is OK for "from" and "to" to point to the same item. This is used to
8057 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008058 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008060copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008062 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008063 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008064 switch (from->v_type)
8065 {
8066 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008067 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 to->vval.v_number = from->vval.v_number;
8069 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008071#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072 to->vval.v_float = from->vval.v_float;
8073 break;
8074#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008075 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008076#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008077 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008078 if (to->vval.v_job != NULL)
8079 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008080 break;
8081#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008082 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008083#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008084 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008085 if (to->vval.v_channel != NULL)
8086 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008087 break;
8088#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 case VAR_STRING:
8090 case VAR_FUNC:
8091 if (from->vval.v_string == NULL)
8092 to->vval.v_string = NULL;
8093 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008094 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008096 if (from->v_type == VAR_FUNC)
8097 func_ref(to->vval.v_string);
8098 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008099 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008100 case VAR_PARTIAL:
8101 if (from->vval.v_partial == NULL)
8102 to->vval.v_partial = NULL;
8103 else
8104 {
8105 to->vval.v_partial = from->vval.v_partial;
8106 ++to->vval.v_partial->pt_refcount;
8107 }
8108 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008109 case VAR_BLOB:
8110 if (from->vval.v_blob == NULL)
8111 to->vval.v_blob = NULL;
8112 else
8113 {
8114 to->vval.v_blob = from->vval.v_blob;
8115 ++to->vval.v_blob->bv_refcount;
8116 }
8117 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008118 case VAR_LIST:
8119 if (from->vval.v_list == NULL)
8120 to->vval.v_list = NULL;
8121 else
8122 {
8123 to->vval.v_list = from->vval.v_list;
8124 ++to->vval.v_list->lv_refcount;
8125 }
8126 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008127 case VAR_DICT:
8128 if (from->vval.v_dict == NULL)
8129 to->vval.v_dict = NULL;
8130 else
8131 {
8132 to->vval.v_dict = from->vval.v_dict;
8133 ++to->vval.v_dict->dv_refcount;
8134 }
8135 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008136 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008137 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 break;
8139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140}
8141
8142/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143 * Make a copy of an item.
8144 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008145 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8146 * reference to an already copied list/dict can be used.
8147 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008149 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008150item_copy(
8151 typval_T *from,
8152 typval_T *to,
8153 int deep,
8154 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155{
8156 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 {
8161 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008162 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 }
8164 ++recurse;
8165
8166 switch (from->v_type)
8167 {
8168 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 case VAR_STRING:
8171 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008172 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008173 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008174 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008175 case VAR_CHANNEL:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008176 case VAR_BLOB:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177 copy_tv(from, to);
8178 break;
8179 case VAR_LIST:
8180 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008181 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008182 if (from->vval.v_list == NULL)
8183 to->vval.v_list = NULL;
8184 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8185 {
8186 /* use the copy made earlier */
8187 to->vval.v_list = from->vval.v_list->lv_copylist;
8188 ++to->vval.v_list->lv_refcount;
8189 }
8190 else
8191 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8192 if (to->vval.v_list == NULL)
8193 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 break;
8195 case VAR_DICT:
8196 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008197 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008198 if (from->vval.v_dict == NULL)
8199 to->vval.v_dict = NULL;
8200 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8201 {
8202 /* use the copy made earlier */
8203 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8204 ++to->vval.v_dict->dv_refcount;
8205 }
8206 else
8207 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8208 if (to->vval.v_dict == NULL)
8209 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008210 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008211 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008212 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008213 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 }
8215 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008216 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008217}
8218
8219/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 * This function is used by f_input() and f_inputdialog() functions. The third
8221 * argument to f_input() specifies the type of completion to use at the
8222 * prompt. The third argument to f_inputdialog() specifies the value to return
8223 * when the user cancels the prompt.
8224 */
8225 void
8226get_user_input(
8227 typval_T *argvars,
8228 typval_T *rettv,
8229 int inputdialog,
8230 int secret)
8231{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008232 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 char_u *p = NULL;
8234 int c;
8235 char_u buf[NUMBUFLEN];
8236 int cmd_silent_save = cmd_silent;
8237 char_u *defstr = (char_u *)"";
8238 int xp_type = EXPAND_NOTHING;
8239 char_u *xp_arg = NULL;
8240
8241 rettv->v_type = VAR_STRING;
8242 rettv->vval.v_string = NULL;
8243
8244#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008245 /* While starting up, there is no place to enter text. When running tests
8246 * with --not-a-term we assume feedkeys() will be used. */
8247 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 return;
8249#endif
8250
8251 cmd_silent = FALSE; /* Want to see the prompt. */
8252 if (prompt != NULL)
8253 {
8254 /* Only the part of the message after the last NL is considered as
8255 * prompt for the command line */
8256 p = vim_strrchr(prompt, '\n');
8257 if (p == NULL)
8258 p = prompt;
8259 else
8260 {
8261 ++p;
8262 c = *p;
8263 *p = NUL;
8264 msg_start();
8265 msg_clr_eos();
8266 msg_puts_attr(prompt, echo_attr);
8267 msg_didout = FALSE;
8268 msg_starthere();
8269 *p = c;
8270 }
8271 cmdline_row = msg_row;
8272
8273 if (argvars[1].v_type != VAR_UNKNOWN)
8274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008275 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 if (defstr != NULL)
8277 stuffReadbuffSpec(defstr);
8278
8279 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8280 {
8281 char_u *xp_name;
8282 int xp_namelen;
8283 long argt;
8284
8285 /* input() with a third argument: completion */
8286 rettv->vval.v_string = NULL;
8287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008288 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (xp_name == NULL)
8290 return;
8291
8292 xp_namelen = (int)STRLEN(xp_name);
8293
8294 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8295 &xp_arg) == FAIL)
8296 return;
8297 }
8298 }
8299
8300 if (defstr != NULL)
8301 {
8302 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 ex_normal_busy = 0;
8305 rettv->vval.v_string =
8306 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8307 xp_type, xp_arg);
8308 ex_normal_busy = save_ex_normal_busy;
8309 }
8310 if (inputdialog && rettv->vval.v_string == NULL
8311 && argvars[1].v_type != VAR_UNKNOWN
8312 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008313 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 &argvars[2], buf));
8315
8316 vim_free(xp_arg);
8317
8318 /* since the user typed this, no need to wait for return */
8319 need_wait_return = FALSE;
8320 msg_didout = FALSE;
8321 }
8322 cmd_silent = cmd_silent_save;
8323}
8324
8325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 * ":echo expr1 ..." print each argument separated with a space, add a
8327 * newline at the end.
8328 * ":echon expr1 ..." print each argument plain.
8329 */
8330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008331ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008335 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 char_u *p;
8337 int needclr = TRUE;
8338 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008339 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008340 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008341 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
8343 if (eap->skip)
8344 ++emsg_skip;
8345 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8346 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347 /* If eval1() causes an error message the text from the command may
8348 * still need to be cleared. E.g., "echo 22,44". */
8349 need_clr_eos = needclr;
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
8354 /*
8355 * Report the invalid expression unless the expression evaluation
8356 * has been cancelled due to an aborting error, an interrupt, or an
8357 * exception.
8358 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008359 if (!aborting() && did_emsg == did_emsg_before
8360 && called_emsg == called_emsg_before)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008362 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 break;
8364 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008365 need_clr_eos = FALSE;
8366
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (!eap->skip)
8368 {
8369 if (atstart)
8370 {
8371 atstart = FALSE;
8372 /* Call msg_start() after eval1(), evaluating the expression
8373 * may cause a message to appear. */
8374 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008375 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008376 /* Mark the saved text as finishing the line, so that what
8377 * follows is displayed on a new line when scrolling back
8378 * at the more prompt. */
8379 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 else if (eap->cmdidx == CMD_echo)
8384 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008385 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008386 if (p != NULL)
8387 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008389 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008391 if (*p != TAB && needclr)
8392 {
8393 /* remove any text still there from the command */
8394 msg_clr_eos();
8395 needclr = FALSE;
8396 }
8397 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 }
8399 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008400 {
8401#ifdef FEAT_MBYTE
8402 if (has_mbyte)
8403 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008404 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008405
8406 (void)msg_outtrans_len_attr(p, i, echo_attr);
8407 p += i - 1;
8408 }
8409 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008411 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008414 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008416 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 arg = skipwhite(arg);
8418 }
8419 eap->nextcmd = check_nextcmd(arg);
8420
8421 if (eap->skip)
8422 --emsg_skip;
8423 else
8424 {
8425 /* remove text that may still be there from the command */
8426 if (needclr)
8427 msg_clr_eos();
8428 if (eap->cmdidx == CMD_echo)
8429 msg_end();
8430 }
8431}
8432
8433/*
8434 * ":echohl {name}".
8435 */
8436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008437ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008439 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440}
8441
8442/*
8443 * ":execute expr1 ..." execute the result of an expression.
8444 * ":echomsg expr1 ..." Print a message
8445 * ":echoerr expr1 ..." Print an error
8446 * Each gets spaces around each argument and a newline at the end for
8447 * echo commands
8448 */
8449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008450ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008453 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 int ret = OK;
8455 char_u *p;
8456 garray_T ga;
8457 int len;
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008458 int save_did_emsg = did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459
8460 ga_init2(&ga, 1, 80);
8461
8462 if (eap->skip)
8463 ++emsg_skip;
8464 while (*arg != NUL && *arg != '|' && *arg != '\n')
8465 {
8466 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {
8469 /*
8470 * Report the invalid expression unless the expression evaluation
8471 * has been cancelled due to an aborting error, an interrupt, or an
8472 * exception.
8473 */
Bram Moolenaar8ff5af92018-11-28 21:20:38 +01008474 if (!aborting() && did_emsg == save_did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 EMSG2(_(e_invexpr2), p);
8476 ret = FAIL;
8477 break;
8478 }
8479
8480 if (!eap->skip)
8481 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008482 char_u buf[NUMBUFLEN];
8483
8484 if (eap->cmdidx == CMD_execute)
8485 p = tv_get_string_buf(&rettv, buf);
8486 else
8487 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 len = (int)STRLEN(p);
8489 if (ga_grow(&ga, len + 2) == FAIL)
8490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 ret = FAIL;
8493 break;
8494 }
8495 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 ga.ga_len += len;
8499 }
8500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 arg = skipwhite(arg);
8503 }
8504
8505 if (ret != FAIL && ga.ga_data != NULL)
8506 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008507 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8508 {
8509 /* Mark the already saved text as finishing the line, so that what
8510 * follows is displayed on a new line when scrolling back at the
8511 * more prompt. */
8512 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008513 }
8514
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008518 out_flush();
8519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 else if (eap->cmdidx == CMD_echoerr)
8521 {
8522 /* We don't want to abort following commands, restore did_emsg. */
8523 save_did_emsg = did_emsg;
8524 EMSG((char_u *)ga.ga_data);
8525 if (!force_abort)
8526 did_emsg = save_did_emsg;
8527 }
8528 else if (eap->cmdidx == CMD_execute)
8529 do_cmdline((char_u *)ga.ga_data,
8530 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8531 }
8532
8533 ga_clear(&ga);
8534
8535 if (eap->skip)
8536 --emsg_skip;
8537
8538 eap->nextcmd = check_nextcmd(arg);
8539}
8540
8541/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 * Find window specified by "vp" in tabpage "tp".
8543 */
8544 win_T *
8545find_win_by_nr(
8546 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008547 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008550 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 if (nr < 0)
8553 return NULL;
8554 if (nr == 0)
8555 return curwin;
8556
Bram Moolenaar29323592016-07-24 22:04:11 +02008557 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8558 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 if (nr >= LOWEST_WIN_ID)
8560 {
8561 if (wp->w_id == nr)
8562 return wp;
8563 }
8564 else if (--nr <= 0)
8565 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008566 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 if (nr >= LOWEST_WIN_ID)
8568 return NULL;
8569 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570}
8571
8572/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008573 * Find a window: When using a Window ID in any tab page, when using a number
8574 * in the current tab page.
8575 */
8576 win_T *
8577find_win_by_nr_or_id(typval_T *vp)
8578{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008579 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008580
8581 if (nr >= LOWEST_WIN_ID)
8582 return win_id2wp(vp);
8583 return find_win_by_nr(vp, NULL);
8584}
8585
8586/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 * Find window specified by "wvp" in tabpage "tvp".
8588 */
8589 win_T *
8590find_tabwin(
8591 typval_T *wvp, /* VAR_UNKNOWN for current window */
8592 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8593{
8594 win_T *wp = NULL;
8595 tabpage_T *tp = NULL;
8596 long n;
8597
8598 if (wvp->v_type != VAR_UNKNOWN)
8599 {
8600 if (tvp->v_type != VAR_UNKNOWN)
8601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008602 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 if (n >= 0)
8604 tp = find_tabpage(n);
8605 }
8606 else
8607 tp = curtab;
8608
8609 if (tp != NULL)
8610 wp = find_win_by_nr(wvp, tp);
8611 }
8612 else
8613 wp = curwin;
8614
8615 return wp;
8616}
8617
8618/*
8619 * getwinvar() and gettabwinvar()
8620 */
8621 void
8622getwinvar(
8623 typval_T *argvars,
8624 typval_T *rettv,
8625 int off) /* 1 for gettabwinvar() */
8626{
8627 win_T *win;
8628 char_u *varname;
8629 dictitem_T *v;
8630 tabpage_T *tp = NULL;
8631 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 win_T *oldcurwin;
8633 tabpage_T *oldtabpage;
8634 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 else
8639 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008641 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 ++emsg_off;
8643
8644 rettv->v_type = VAR_STRING;
8645 rettv->vval.v_string = NULL;
8646
8647 if (win != NULL && varname != NULL)
8648 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 /* Set curwin to be our win, temporarily. Also set the tabpage,
8650 * otherwise the window is not valid. Only do this when needed,
8651 * autocommands get blocked. */
8652 need_switch_win = !(tp == curtab && win == curwin);
8653 if (!need_switch_win
8654 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008656 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008658 if (varname[1] == NUL)
8659 {
8660 /* get all window-local options in a dict */
8661 dict_T *opts = get_winbuf_options(FALSE);
8662
8663 if (opts != NULL)
8664 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008665 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008666 done = TRUE;
8667 }
8668 }
8669 else if (get_option_tv(&varname, rettv, 1) == OK)
8670 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 done = TRUE;
8672 }
8673 else
8674 {
8675 /* Look up the variable. */
8676 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8677 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8678 varname, FALSE);
8679 if (v != NULL)
8680 {
8681 copy_tv(&v->di_tv, rettv);
8682 done = TRUE;
8683 }
8684 }
8685 }
8686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 if (need_switch_win)
8688 /* restore previous notion of curwin */
8689 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 }
8691
8692 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8693 /* use the default return value */
8694 copy_tv(&argvars[off + 2], rettv);
8695
8696 --emsg_off;
8697}
8698
8699/*
8700 * "setwinvar()" and "settabwinvar()" functions
8701 */
8702 void
8703setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8704{
8705 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 win_T *save_curwin;
8707 tabpage_T *save_curtab;
8708 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 char_u *varname, *winvarname;
8710 typval_T *varp;
8711 char_u nbuf[NUMBUFLEN];
8712 tabpage_T *tp = NULL;
8713
8714 if (check_restricted() || check_secure())
8715 return;
8716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 else
8720 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 varp = &argvars[off + 2];
8724
8725 if (win != NULL && varname != NULL && varp != NULL)
8726 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727 need_switch_win = !(tp == curtab && win == curwin);
8728 if (!need_switch_win
8729 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 {
8731 if (*varname == '&')
8732 {
8733 long numval;
8734 char_u *strval;
8735 int error = FALSE;
8736
8737 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008738 numval = (long)tv_get_number_chk(varp, &error);
8739 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740 if (!error && strval != NULL)
8741 set_option_value(varname, numval, strval, OPT_LOCAL);
8742 }
8743 else
8744 {
8745 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8746 if (winvarname != NULL)
8747 {
8748 STRCPY(winvarname, "w:");
8749 STRCPY(winvarname + 2, varname);
8750 set_var(winvarname, varp, TRUE);
8751 vim_free(winvarname);
8752 }
8753 }
8754 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 if (need_switch_win)
8756 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 }
8758}
8759
8760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8762 * "arg" points to the "&" or '+' when called, to "option" when returning.
8763 * Returns NULL when no option name found. Otherwise pointer to the char
8764 * after the option name.
8765 */
8766 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008767find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 char_u *p = *arg;
8770
8771 ++p;
8772 if (*p == 'g' && p[1] == ':')
8773 {
8774 *opt_flags = OPT_GLOBAL;
8775 p += 2;
8776 }
8777 else if (*p == 'l' && p[1] == ':')
8778 {
8779 *opt_flags = OPT_LOCAL;
8780 p += 2;
8781 }
8782 else
8783 *opt_flags = 0;
8784
8785 if (!ASCII_ISALPHA(*p))
8786 return NULL;
8787 *arg = p;
8788
8789 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8790 p += 4; /* termcap option */
8791 else
8792 while (ASCII_ISALPHA(*p))
8793 ++p;
8794 return p;
8795}
8796
8797/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008798 * Return the autoload script name for a function or variable name.
8799 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008801 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008802autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008803{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008804 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008805 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008806
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008807 /* Get the script file name: replace '#' with '/', append ".vim". */
8808 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8809 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008810 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008811 STRCPY(scriptname, "autoload/");
8812 STRCAT(scriptname, name);
8813 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8814 STRCAT(scriptname, ".vim");
8815 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8816 *p = '/';
8817 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008818}
8819
8820/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008821 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008822 * Return TRUE if a package was loaded.
8823 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008825script_autoload(
8826 char_u *name,
8827 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008828{
8829 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008830 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008831 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008832 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008834 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008835 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008836 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008837 return FALSE;
8838
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008839 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008840
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008841 /* Find the name in the list of previously loaded package names. Skip
8842 * "autoload/", it's always the same. */
8843 for (i = 0; i < ga_loaded.ga_len; ++i)
8844 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8845 break;
8846 if (!reload && i < ga_loaded.ga_len)
8847 ret = FALSE; /* was loaded already */
8848 else
8849 {
8850 /* Remember the name if it wasn't loaded already. */
8851 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8852 {
8853 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8854 tofree = NULL;
8855 }
8856
8857 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008858 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008859 ret = TRUE;
8860 }
8861
8862 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008863 return ret;
8864}
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8867typedef enum
8868{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008869 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8870 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8871 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872} var_flavour_T;
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008875var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 char_u *p = varname;
8878
8879 if (ASCII_ISUPPER(*p))
8880 {
8881 while (*(++p))
8882 if (ASCII_ISLOWER(*p))
8883 return VAR_FLAVOUR_SESSION;
8884 return VAR_FLAVOUR_VIMINFO;
8885 }
8886 else
8887 return VAR_FLAVOUR_DEFAULT;
8888}
8889#endif
8890
8891#if defined(FEAT_VIMINFO) || defined(PROTO)
8892/*
8893 * Restore global vars that start with a capital from the viminfo file
8894 */
8895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008896read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008900 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008901 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902
8903 if (!writing && (find_viminfo_parameter('!') != NULL))
8904 {
8905 tab = vim_strchr(virp->vir_line + 1, '\t');
8906 if (tab != NULL)
8907 {
8908 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008909 switch (*tab)
8910 {
8911 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008912#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008913 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008914#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008915 case 'D': type = VAR_DICT; break;
8916 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008917 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008918 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
8921 tab = vim_strchr(tab, '\t');
8922 if (tab != NULL)
8923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008925 if (type == VAR_STRING || type == VAR_DICT
8926 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008927 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929#ifdef FEAT_FLOAT
8930 else if (type == VAR_FLOAT)
8931 (void)string2float(tab + 1, &tv.vval.v_float);
8932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008934 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008935 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008936 {
8937 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8938
8939 if (etv == NULL)
8940 /* Failed to parse back the dict or list, use it as a
8941 * string. */
8942 tv.v_type = VAR_STRING;
8943 else
8944 {
8945 vim_free(tv.vval.v_string);
8946 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008947 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008948 }
8949 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01008950 else if (type == VAR_BLOB)
8951 {
8952 blob_T *blob = string2blob(tv.vval.v_string);
8953
8954 if (blob == NULL)
8955 // Failed to parse back the blob, use it as a string.
8956 tv.v_type = VAR_STRING;
8957 else
8958 {
8959 vim_free(tv.vval.v_string);
8960 tv.v_type = VAR_BLOB;
8961 tv.vval.v_blob = blob;
8962 }
8963 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008964
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008965 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008966 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008967 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008968 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008969
8970 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008971 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008972 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
8973 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008974 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 }
8976 }
8977 }
8978
8979 return viminfo_readline(virp);
8980}
8981
8982/*
8983 * Write global vars that start with a capital to the viminfo file
8984 */
8985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 hashitem_T *hi;
8989 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008990 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008991 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008992 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008994 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995
8996 if (find_viminfo_parameter('!') == NULL)
8997 return;
8998
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008999 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009001 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009006 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 this_var = HI2DI(hi);
9008 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009011 {
9012 case VAR_STRING: s = "STR"; break;
9013 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009014 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009015 case VAR_DICT: s = "DIC"; break;
9016 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009017 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009018 case VAR_SPECIAL: s = "XPL"; break;
9019
9020 case VAR_UNKNOWN:
9021 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009022 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009023 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009024 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009025 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009028 if (this_var->di_tv.v_type == VAR_SPECIAL)
9029 {
9030 sprintf((char *)numbuf, "%ld",
9031 (long)this_var->di_tv.vval.v_number);
9032 p = numbuf;
9033 tofree = NULL;
9034 }
9035 else
9036 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009037 if (p != NULL)
9038 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009039 vim_free(tofree);
9040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 }
9042 }
9043}
9044#endif
9045
9046#if defined(FEAT_SESSION) || defined(PROTO)
9047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009048store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 hashitem_T *hi;
9051 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009052 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 char_u *p, *t;
9054
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009055 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009058 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009060 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 this_var = HI2DI(hi);
9062 if ((this_var->di_tv.v_type == VAR_NUMBER
9063 || this_var->di_tv.v_type == VAR_STRING)
9064 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009065 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009066 /* Escape special characters with a backslash. Turn a LF and
9067 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009068 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009069 (char_u *)"\\\"\n\r");
9070 if (p == NULL) /* out of memory */
9071 break;
9072 for (t = p; *t != NUL; ++t)
9073 if (*t == '\n')
9074 *t = 'n';
9075 else if (*t == '\r')
9076 *t = 'r';
9077 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 this_var->di_key,
9079 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9080 : ' ',
9081 p,
9082 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9083 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009084 || put_eol(fd) == FAIL)
9085 {
9086 vim_free(p);
9087 return FAIL;
9088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 vim_free(p);
9090 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009091#ifdef FEAT_FLOAT
9092 else if (this_var->di_tv.v_type == VAR_FLOAT
9093 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9094 {
9095 float_T f = this_var->di_tv.vval.v_float;
9096 int sign = ' ';
9097
9098 if (f < 0)
9099 {
9100 f = -f;
9101 sign = '-';
9102 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009103 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009104 this_var->di_key, sign, f) < 0)
9105 || put_eol(fd) == FAIL)
9106 return FAIL;
9107 }
9108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110 }
9111 return OK;
9112}
9113#endif
9114
Bram Moolenaar661b1822005-07-28 22:36:45 +00009115/*
9116 * Display script name where an item was last set.
9117 * Should only be invoked when 'verbose' is non-zero.
9118 */
9119 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009120last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009121{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009122 char_u *p;
9123
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009124 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009125 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009126 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009127 if (p != NULL)
9128 {
9129 verbose_enter();
9130 MSG_PUTS(_("\n\tLast set from "));
9131 MSG_PUTS(p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009132 if (script_ctx.sc_lnum > 0)
9133 {
9134 MSG_PUTS(_(" line "));
9135 msg_outnum((long)script_ctx.sc_lnum);
9136 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009137 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009138 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009139 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009140 }
9141}
9142
Bram Moolenaar53744302015-07-17 17:38:22 +02009143/* reset v:option_new, v:option_old and v:option_type */
9144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009145reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009146{
9147 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9148 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9149 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9150}
9151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152/*
9153 * Prepare "gap" for an assert error and add the sourcing position.
9154 */
9155 void
9156prepare_assert_error(garray_T *gap)
9157{
9158 char buf[NUMBUFLEN];
9159
9160 ga_init2(gap, 1, 100);
9161 if (sourcing_name != NULL)
9162 {
9163 ga_concat(gap, sourcing_name);
9164 if (sourcing_lnum > 0)
9165 ga_concat(gap, (char_u *)" ");
9166 }
9167 if (sourcing_lnum > 0)
9168 {
9169 sprintf(buf, "line %ld", (long)sourcing_lnum);
9170 ga_concat(gap, (char_u *)buf);
9171 }
9172 if (sourcing_name != NULL || sourcing_lnum > 0)
9173 ga_concat(gap, (char_u *)": ");
9174}
9175
9176/*
9177 * Add an assert error to v:errors.
9178 */
9179 void
9180assert_error(garray_T *gap)
9181{
9182 struct vimvar *vp = &vimvars[VV_ERRORS];
9183
9184 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9185 /* Make sure v:errors is a list. */
9186 set_vim_var_list(VV_ERRORS, list_alloc());
9187 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9188}
9189
Bram Moolenaar65a54642018-04-28 16:56:53 +02009190 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191assert_equal_common(typval_T *argvars, assert_type_T atype)
9192{
9193 garray_T ga;
9194
9195 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9196 != (atype == ASSERT_EQUAL))
9197 {
9198 prepare_assert_error(&ga);
9199 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9200 atype);
9201 assert_error(&ga);
9202 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009203 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009205 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206}
9207
Bram Moolenaar65a54642018-04-28 16:56:53 +02009208 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009209assert_equalfile(typval_T *argvars)
9210{
9211 char_u buf1[NUMBUFLEN];
9212 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009213 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9214 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009215 garray_T ga;
9216 FILE *fd1;
9217 FILE *fd2;
9218
9219 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009220 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009221
9222 IObuff[0] = NUL;
9223 fd1 = mch_fopen((char *)fname1, READBIN);
9224 if (fd1 == NULL)
9225 {
9226 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9227 }
9228 else
9229 {
9230 fd2 = mch_fopen((char *)fname2, READBIN);
9231 if (fd2 == NULL)
9232 {
9233 fclose(fd1);
9234 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9235 }
9236 else
9237 {
9238 int c1, c2;
9239 long count = 0;
9240
9241 for (;;)
9242 {
9243 c1 = fgetc(fd1);
9244 c2 = fgetc(fd2);
9245 if (c1 == EOF)
9246 {
9247 if (c2 != EOF)
9248 STRCPY(IObuff, "first file is shorter");
9249 break;
9250 }
9251 else if (c2 == EOF)
9252 {
9253 STRCPY(IObuff, "second file is shorter");
9254 break;
9255 }
9256 else if (c1 != c2)
9257 {
9258 vim_snprintf((char *)IObuff, IOSIZE,
9259 "difference at byte %ld", count);
9260 break;
9261 }
9262 ++count;
9263 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009264 fclose(fd1);
9265 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009266 }
9267 }
9268 if (IObuff[0] != NUL)
9269 {
9270 prepare_assert_error(&ga);
9271 ga_concat(&ga, IObuff);
9272 assert_error(&ga);
9273 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009274 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009275 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009276 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009277}
9278
Bram Moolenaar65a54642018-04-28 16:56:53 +02009279 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280assert_match_common(typval_T *argvars, assert_type_T atype)
9281{
9282 garray_T ga;
9283 char_u buf1[NUMBUFLEN];
9284 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009285 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9286 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287
9288 if (pat == NULL || text == NULL)
9289 EMSG(_(e_invarg));
9290 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9291 {
9292 prepare_assert_error(&ga);
9293 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9294 atype);
9295 assert_error(&ga);
9296 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009297 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009299 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300}
9301
Bram Moolenaar65a54642018-04-28 16:56:53 +02009302 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009303assert_inrange(typval_T *argvars)
9304{
9305 garray_T ga;
9306 int error = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009307 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9308 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9309 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar61c04492016-07-23 15:35:35 +02009310 char_u *tofree;
9311 char msg[200];
9312 char_u numbuf[NUMBUFLEN];
9313
9314 if (error)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009315 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009316 if (actual < lower || actual > upper)
9317 {
9318 prepare_assert_error(&ga);
9319 if (argvars[3].v_type != VAR_UNKNOWN)
9320 {
9321 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9322 vim_free(tofree);
9323 }
9324 else
9325 {
9326 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9327 (long)lower, (long)upper, (long)actual);
9328 ga_concat(&ga, (char_u *)msg);
9329 }
9330 assert_error(&ga);
9331 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009332 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009333 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009334 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009335}
9336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337/*
9338 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009339 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009341 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342assert_bool(typval_T *argvars, int isTrue)
9343{
9344 int error = FALSE;
9345 garray_T ga;
9346
9347 if (argvars[0].v_type == VAR_SPECIAL
9348 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009349 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009351 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 || error)
9353 {
9354 prepare_assert_error(&ga);
9355 fill_assert_error(&ga, &argvars[1],
9356 (char_u *)(isTrue ? "True" : "False"),
9357 NULL, &argvars[0], ASSERT_OTHER);
9358 assert_error(&ga);
9359 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009360 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009362 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363}
9364
Bram Moolenaar65a54642018-04-28 16:56:53 +02009365 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009366assert_report(typval_T *argvars)
9367{
9368 garray_T ga;
9369
9370 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009371 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009372 assert_error(&ga);
9373 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009374 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009375}
9376
Bram Moolenaar65a54642018-04-28 16:56:53 +02009377 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378assert_exception(typval_T *argvars)
9379{
9380 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009381 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382
9383 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9384 {
9385 prepare_assert_error(&ga);
9386 ga_concat(&ga, (char_u *)"v:exception is not set");
9387 assert_error(&ga);
9388 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009389 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 }
9391 else if (error != NULL
9392 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9393 {
9394 prepare_assert_error(&ga);
9395 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9396 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9397 assert_error(&ga);
9398 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009399 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009401 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402}
9403
Bram Moolenaar65a54642018-04-28 16:56:53 +02009404 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009405assert_beeps(typval_T *argvars)
9406{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009407 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009408 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009409 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009410
9411 called_vim_beep = FALSE;
9412 suppress_errthrow = TRUE;
9413 emsg_silent = FALSE;
9414 do_cmdline_cmd(cmd);
9415 if (!called_vim_beep)
9416 {
9417 prepare_assert_error(&ga);
9418 ga_concat(&ga, (char_u *)"command did not beep: ");
9419 ga_concat(&ga, cmd);
9420 assert_error(&ga);
9421 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009422 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009423 }
9424
9425 suppress_errthrow = FALSE;
9426 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009427 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009428}
9429
Bram Moolenaar65a54642018-04-28 16:56:53 +02009430 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431assert_fails(typval_T *argvars)
9432{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009433 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009435 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009436 char_u numbuf[NUMBUFLEN];
9437 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438
9439 called_emsg = FALSE;
9440 suppress_errthrow = TRUE;
9441 emsg_silent = TRUE;
9442 do_cmdline_cmd(cmd);
9443 if (!called_emsg)
9444 {
9445 prepare_assert_error(&ga);
9446 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009447 if (argvars[1].v_type != VAR_UNKNOWN
9448 && argvars[2].v_type != VAR_UNKNOWN)
9449 {
9450 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9451 vim_free(tofree);
9452 }
9453 else
9454 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455 assert_error(&ga);
9456 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009457 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 }
9459 else if (argvars[1].v_type != VAR_UNKNOWN)
9460 {
9461 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009462 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463
9464 if (error == NULL
9465 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9466 {
9467 prepare_assert_error(&ga);
9468 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9469 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9470 assert_error(&ga);
9471 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009472 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 }
9474 }
9475
9476 called_emsg = FALSE;
9477 suppress_errthrow = FALSE;
9478 emsg_silent = FALSE;
9479 emsg_on_display = FALSE;
9480 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009481 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482}
9483
9484/*
9485 * Append "str" to "gap", escaping unprintable characters.
9486 * Changes NL to \n, CR to \r, etc.
9487 */
9488 static void
9489ga_concat_esc(garray_T *gap, char_u *str)
9490{
9491 char_u *p;
9492 char_u buf[NUMBUFLEN];
9493
9494 if (str == NULL)
9495 {
9496 ga_concat(gap, (char_u *)"NULL");
9497 return;
9498 }
9499
9500 for (p = str; *p != NUL; ++p)
9501 switch (*p)
9502 {
9503 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9504 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9505 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9506 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9507 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9508 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9509 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9510 default:
9511 if (*p < ' ')
9512 {
9513 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9514 ga_concat(gap, buf);
9515 }
9516 else
9517 ga_append(gap, *p);
9518 break;
9519 }
9520}
9521
9522/*
9523 * Fill "gap" with information about an assert error.
9524 */
9525 void
9526fill_assert_error(
9527 garray_T *gap,
9528 typval_T *opt_msg_tv,
9529 char_u *exp_str,
9530 typval_T *exp_tv,
9531 typval_T *got_tv,
9532 assert_type_T atype)
9533{
9534 char_u numbuf[NUMBUFLEN];
9535 char_u *tofree;
9536
9537 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9538 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009539 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9540 vim_free(tofree);
9541 ga_concat(gap, (char_u *)": ");
9542 }
9543
9544 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9545 ga_concat(gap, (char_u *)"Pattern ");
9546 else if (atype == ASSERT_NOTEQUAL)
9547 ga_concat(gap, (char_u *)"Expected not equal to ");
9548 else
9549 ga_concat(gap, (char_u *)"Expected ");
9550 if (exp_str == NULL)
9551 {
9552 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 vim_free(tofree);
9554 }
9555 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009556 ga_concat_esc(gap, exp_str);
9557 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009559 if (atype == ASSERT_MATCH)
9560 ga_concat(gap, (char_u *)" does not match ");
9561 else if (atype == ASSERT_NOTMATCH)
9562 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009564 ga_concat(gap, (char_u *)" but got ");
9565 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9566 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009567 }
9568}
9569
Bram Moolenaar31988702018-02-13 12:57:42 +01009570/*
9571 * Compare "typ1" and "typ2". Put the result in "typ1".
9572 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009573 int
9574typval_compare(
9575 typval_T *typ1, /* first operand */
9576 typval_T *typ2, /* second operand */
9577 exptype_T type, /* operator */
9578 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009579 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009580{
9581 int i;
9582 varnumber_T n1, n2;
9583 char_u *s1, *s2;
9584 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9585
Bram Moolenaar31988702018-02-13 12:57:42 +01009586 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009587 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009588 /* For "is" a different type always means FALSE, for "notis"
9589 * it means TRUE. */
9590 n1 = (type == TYPE_NEQUAL);
9591 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009592 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9593 {
9594 if (type_is)
9595 {
9596 n1 = (typ1->v_type == typ2->v_type
9597 && typ1->vval.v_blob == typ2->vval.v_blob);
9598 if (type == TYPE_NEQUAL)
9599 n1 = !n1;
9600 }
9601 else if (typ1->v_type != typ2->v_type
9602 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9603 {
9604 if (typ1->v_type != typ2->v_type)
9605 EMSG(_("E977: Can only compare Blob with Blob"));
9606 else
9607 EMSG(_(e_invalblob));
9608 clear_tv(typ1);
9609 return FAIL;
9610 }
9611 else
9612 {
9613 // Compare two Blobs for being equal or unequal.
9614 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9615 if (type == TYPE_NEQUAL)
9616 n1 = !n1;
9617 }
9618 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009619 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9620 {
9621 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009622 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009623 n1 = (typ1->v_type == typ2->v_type
9624 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009625 if (type == TYPE_NEQUAL)
9626 n1 = !n1;
9627 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009628 else if (typ1->v_type != typ2->v_type
9629 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009630 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009631 if (typ1->v_type != typ2->v_type)
9632 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009633 else
Bram Moolenaar31988702018-02-13 12:57:42 +01009634 EMSG(_("E692: Invalid operation for List"));
9635 clear_tv(typ1);
9636 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009637 }
9638 else
9639 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009640 /* Compare two Lists for being equal or unequal. */
9641 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9642 ic, FALSE);
9643 if (type == TYPE_NEQUAL)
9644 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009645 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009646 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009647
9648 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9649 {
9650 if (type_is)
9651 {
9652 n1 = (typ1->v_type == typ2->v_type
9653 && typ1->vval.v_dict == typ2->vval.v_dict);
9654 if (type == TYPE_NEQUAL)
9655 n1 = !n1;
9656 }
9657 else if (typ1->v_type != typ2->v_type
9658 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9659 {
9660 if (typ1->v_type != typ2->v_type)
9661 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9662 else
9663 EMSG(_("E736: Invalid operation for Dictionary"));
9664 clear_tv(typ1);
9665 return FAIL;
9666 }
9667 else
9668 {
9669 /* Compare two Dictionaries for being equal or unequal. */
9670 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9671 ic, FALSE);
9672 if (type == TYPE_NEQUAL)
9673 n1 = !n1;
9674 }
9675 }
9676
9677 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9678 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9679 {
9680 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9681 {
9682 EMSG(_("E694: Invalid operation for Funcrefs"));
9683 clear_tv(typ1);
9684 return FAIL;
9685 }
9686 if ((typ1->v_type == VAR_PARTIAL
9687 && typ1->vval.v_partial == NULL)
9688 || (typ2->v_type == VAR_PARTIAL
9689 && typ2->vval.v_partial == NULL))
9690 /* when a partial is NULL assume not equal */
9691 n1 = FALSE;
9692 else if (type_is)
9693 {
9694 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9695 /* strings are considered the same if their value is
9696 * the same */
9697 n1 = tv_equal(typ1, typ2, ic, FALSE);
9698 else if (typ1->v_type == VAR_PARTIAL
9699 && typ2->v_type == VAR_PARTIAL)
9700 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9701 else
9702 n1 = FALSE;
9703 }
9704 else
9705 n1 = tv_equal(typ1, typ2, ic, FALSE);
9706 if (type == TYPE_NEQUAL)
9707 n1 = !n1;
9708 }
9709
9710#ifdef FEAT_FLOAT
9711 /*
9712 * If one of the two variables is a float, compare as a float.
9713 * When using "=~" or "!~", always compare as string.
9714 */
9715 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9716 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9717 {
9718 float_T f1, f2;
9719
9720 if (typ1->v_type == VAR_FLOAT)
9721 f1 = typ1->vval.v_float;
9722 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009723 f1 = tv_get_number(typ1);
Bram Moolenaar31988702018-02-13 12:57:42 +01009724 if (typ2->v_type == VAR_FLOAT)
9725 f2 = typ2->vval.v_float;
9726 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009727 f2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009728 n1 = FALSE;
9729 switch (type)
9730 {
9731 case TYPE_EQUAL: n1 = (f1 == f2); break;
9732 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9733 case TYPE_GREATER: n1 = (f1 > f2); break;
9734 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9735 case TYPE_SMALLER: n1 = (f1 < f2); break;
9736 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9737 case TYPE_UNKNOWN:
9738 case TYPE_MATCH:
9739 case TYPE_NOMATCH: break; /* avoid gcc warning */
9740 }
9741 }
9742#endif
9743
9744 /*
9745 * If one of the two variables is a number, compare as a number.
9746 * When using "=~" or "!~", always compare as string.
9747 */
9748 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9749 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9750 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009751 n1 = tv_get_number(typ1);
9752 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009753 switch (type)
9754 {
9755 case TYPE_EQUAL: n1 = (n1 == n2); break;
9756 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9757 case TYPE_GREATER: n1 = (n1 > n2); break;
9758 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9759 case TYPE_SMALLER: n1 = (n1 < n2); break;
9760 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9761 case TYPE_UNKNOWN:
9762 case TYPE_MATCH:
9763 case TYPE_NOMATCH: break; /* avoid gcc warning */
9764 }
9765 }
9766 else
9767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009768 s1 = tv_get_string_buf(typ1, buf1);
9769 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009770 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9771 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9772 else
9773 i = 0;
9774 n1 = FALSE;
9775 switch (type)
9776 {
9777 case TYPE_EQUAL: n1 = (i == 0); break;
9778 case TYPE_NEQUAL: n1 = (i != 0); break;
9779 case TYPE_GREATER: n1 = (i > 0); break;
9780 case TYPE_GEQUAL: n1 = (i >= 0); break;
9781 case TYPE_SMALLER: n1 = (i < 0); break;
9782 case TYPE_SEQUAL: n1 = (i <= 0); break;
9783
9784 case TYPE_MATCH:
9785 case TYPE_NOMATCH:
9786 n1 = pattern_match(s2, s1, ic);
9787 if (type == TYPE_NOMATCH)
9788 n1 = !n1;
9789 break;
9790
9791 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9792 }
9793 }
9794 clear_tv(typ1);
9795 typ1->v_type = VAR_NUMBER;
9796 typ1->vval.v_number = n1;
9797
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009798 return OK;
9799}
9800
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009801 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009802typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009803{
9804 char_u *tofree;
9805 char_u numbuf[NUMBUFLEN];
9806 char_u *ret = NULL;
9807
9808 if (arg == NULL)
9809 return vim_strsave((char_u *)"(does not exist)");
9810 ret = tv2string(arg, &tofree, numbuf, 0);
9811 /* Make a copy if we have a value but it's not in allocated memory. */
9812 if (ret != NULL && tofree == NULL)
9813 ret = vim_strsave(ret);
9814 return ret;
9815}
9816
9817 int
9818var_exists(char_u *var)
9819{
9820 char_u *name;
9821 char_u *tofree;
9822 typval_T tv;
9823 int len = 0;
9824 int n = FALSE;
9825
9826 /* get_name_len() takes care of expanding curly braces */
9827 name = var;
9828 len = get_name_len(&var, &tofree, TRUE, FALSE);
9829 if (len > 0)
9830 {
9831 if (tofree != NULL)
9832 name = tofree;
9833 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9834 if (n)
9835 {
9836 /* handle d.key, l[idx], f(expr) */
9837 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9838 if (n)
9839 clear_tv(&tv);
9840 }
9841 }
9842 if (*var != NUL)
9843 n = FALSE;
9844
9845 vim_free(tofree);
9846 return n;
9847}
9848
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849#endif /* FEAT_EVAL */
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009852#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
9854#ifdef WIN3264
9855/*
9856 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858
9859/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009860 * Get the short path (8.3) for the filename in "fnamep".
9861 * Only works for a valid file name.
9862 * When the path gets longer "fnamep" is changed and the allocated buffer
9863 * is put in "bufp".
9864 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9865 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 */
9867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009868get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009870 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 char_u *newbuf;
9872
9873 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009874 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 if (l > len - 1)
9876 {
9877 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009878 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 newbuf = vim_strnsave(*fnamep, l);
9880 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009881 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
9883 vim_free(*bufp);
9884 *fnamep = *bufp = newbuf;
9885
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009886 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009887 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 }
9889
9890 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009891 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892}
9893
9894/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009895 * Get the short path (8.3) for the filename in "fname". The converted
9896 * path is returned in "bufp".
9897 *
9898 * Some of the directories specified in "fname" may not exist. This function
9899 * will shorten the existing directories at the beginning of the path and then
9900 * append the remaining non-existing path.
9901 *
9902 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009903 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009904 * bufp - Pointer to an allocated buffer for the filename.
9905 * fnamelen - Length of the filename pointed to by fname
9906 *
9907 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 */
9909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009910shortpath_for_invalid_fname(
9911 char_u **fname,
9912 char_u **bufp,
9913 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009915 char_u *short_fname, *save_fname, *pbuf_unused;
9916 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009918 int old_len, len;
9919 int new_len, sfx_len;
9920 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
9922 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009923 old_len = *fnamelen;
9924 save_fname = vim_strnsave(*fname, old_len);
9925 pbuf_unused = NULL;
9926 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009928 endp = save_fname + old_len - 1; /* Find the end of the copy */
9929 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009931 /*
9932 * Try shortening the supplied path till it succeeds by removing one
9933 * directory at a time from the tail of the path.
9934 */
9935 len = 0;
9936 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009938 /* go back one path-separator */
9939 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9940 --endp;
9941 if (endp <= save_fname)
9942 break; /* processed the complete path */
9943
9944 /*
9945 * Replace the path separator with a NUL and try to shorten the
9946 * resulting path.
9947 */
9948 ch = *endp;
9949 *endp = 0;
9950 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009951 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009952 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9953 {
9954 retval = FAIL;
9955 goto theend;
9956 }
9957 *endp = ch; /* preserve the string */
9958
9959 if (len > 0)
9960 break; /* successfully shortened the path */
9961
9962 /* failed to shorten the path. Skip the path separator */
9963 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 }
9965
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009966 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009968 /*
9969 * Succeeded in shortening the path. Now concatenate the shortened
9970 * path with the remaining path at the tail.
9971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009973 /* Compute the length of the new path. */
9974 sfx_len = (int)(save_endp - endp) + 1;
9975 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009977 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009979 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009981 /* There is not enough space in the currently allocated string,
9982 * copy it to a buffer big enough. */
9983 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009985 {
9986 retval = FAIL;
9987 goto theend;
9988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 }
9990 else
9991 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009992 /* Transfer short_fname to the main buffer (it's big enough),
9993 * unless get_short_pathname() did its work in-place. */
9994 *fname = *bufp = save_fname;
9995 if (short_fname != save_fname)
9996 vim_strncpy(save_fname, short_fname, len);
9997 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009999
10000 /* concat the not-shortened part of the path */
10001 vim_strncpy(*fname + len, endp, sfx_len);
10002 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010004
10005theend:
10006 vim_free(pbuf_unused);
10007 vim_free(save_fname);
10008
10009 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010}
10011
10012/*
10013 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010014 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 */
10016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010017shortpath_for_partial(
10018 char_u **fnamep,
10019 char_u **bufp,
10020 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 int sepcount, len, tflen;
10023 char_u *p;
10024 char_u *pbuf, *tfname;
10025 int hasTilde;
10026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010027 /* Count up the path separators from the RHS.. so we know which part
10028 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010030 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 if (vim_ispathsep(*p))
10032 ++sepcount;
10033
10034 /* Need full path first (use expand_env() to remove a "~/") */
10035 hasTilde = (**fnamep == '~');
10036 if (hasTilde)
10037 pbuf = tfname = expand_env_save(*fnamep);
10038 else
10039 pbuf = tfname = FullName_save(*fnamep, FALSE);
10040
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010041 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010043 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10044 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 if (len == 0)
10047 {
10048 /* Don't have a valid filename, so shorten the rest of the
10049 * path if we can. This CAN give us invalid 8.3 filenames, but
10050 * there's not a lot of point in guessing what it might be.
10051 */
10052 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010053 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10054 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056
10057 /* Count the paths backward to find the beginning of the desired string. */
10058 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010059 {
10060#ifdef FEAT_MBYTE
10061 if (has_mbyte)
10062 p -= mb_head_off(tfname, p);
10063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 if (vim_ispathsep(*p))
10065 {
10066 if (sepcount == 0 || (hasTilde && sepcount == 1))
10067 break;
10068 else
10069 sepcount --;
10070 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (hasTilde)
10073 {
10074 --p;
10075 if (p >= tfname)
10076 *p = '~';
10077 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080 else
10081 ++p;
10082
10083 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10084 vim_free(*bufp);
10085 *fnamelen = (int)STRLEN(p);
10086 *bufp = pbuf;
10087 *fnamep = p;
10088
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010089 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090}
10091#endif /* WIN3264 */
10092
10093/*
10094 * Adjust a filename, according to a string of modifiers.
10095 * *fnamep must be NUL terminated when called. When returning, the length is
10096 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010097 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 * When there is an error, *fnamep is set to NULL.
10099 */
10100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010101modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010102 char_u *src, // string with modifiers
10103 int tilde_file, // "~" is a file name, not $HOME
10104 int *usedlen, // characters after src that are used
10105 char_u **fnamep, // file name so far
10106 char_u **bufp, // buffer for allocated file name or NULL
10107 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
10109 int valid = 0;
10110 char_u *tail;
10111 char_u *s, *p, *pbuf;
10112 char_u dirname[MAXPATHL];
10113 int c;
10114 int has_fullname = 0;
10115#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010116 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 int has_shortname = 0;
10118#endif
10119
10120repeat:
10121 /* ":p" - full path/file_name */
10122 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10123 {
10124 has_fullname = 1;
10125
10126 valid |= VALID_PATH;
10127 *usedlen += 2;
10128
10129 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10130 if ((*fnamep)[0] == '~'
10131#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10132 && ((*fnamep)[1] == '/'
10133# ifdef BACKSLASH_IN_FILENAME
10134 || (*fnamep)[1] == '\\'
10135# endif
10136 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010138 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 )
10140 {
10141 *fnamep = expand_env_save(*fnamep);
10142 vim_free(*bufp); /* free any allocated file name */
10143 *bufp = *fnamep;
10144 if (*fnamep == NULL)
10145 return -1;
10146 }
10147
10148 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010149 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 {
10151 if (vim_ispathsep(*p)
10152 && p[1] == '.'
10153 && (p[2] == NUL
10154 || vim_ispathsep(p[2])
10155 || (p[2] == '.'
10156 && (p[3] == NUL || vim_ispathsep(p[3])))))
10157 break;
10158 }
10159
10160 /* FullName_save() is slow, don't use it when not needed. */
10161 if (*p != NUL || !vim_isAbsName(*fnamep))
10162 {
10163 *fnamep = FullName_save(*fnamep, *p != NUL);
10164 vim_free(*bufp); /* free any allocated file name */
10165 *bufp = *fnamep;
10166 if (*fnamep == NULL)
10167 return -1;
10168 }
10169
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010170#ifdef WIN3264
10171# if _WIN32_WINNT >= 0x0500
10172 if (vim_strchr(*fnamep, '~') != NULL)
10173 {
10174 /* Expand 8.3 filename to full path. Needed to make sure the same
10175 * file does not have two different names.
10176 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
10177 p = alloc(_MAX_PATH + 1);
10178 if (p != NULL)
10179 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010180 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010181 {
10182 vim_free(*bufp);
10183 *bufp = *fnamep = p;
10184 }
10185 else
10186 vim_free(p);
10187 }
10188 }
10189# endif
10190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 /* Append a path separator to a directory. */
10192 if (mch_isdir(*fnamep))
10193 {
10194 /* Make room for one or two extra characters. */
10195 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10196 vim_free(*bufp); /* free any allocated file name */
10197 *bufp = *fnamep;
10198 if (*fnamep == NULL)
10199 return -1;
10200 add_pathsep(*fnamep);
10201 }
10202 }
10203
10204 /* ":." - path relative to the current directory */
10205 /* ":~" - path relative to the home directory */
10206 /* ":8" - shortname path - postponed till after */
10207 while (src[*usedlen] == ':'
10208 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10209 {
10210 *usedlen += 2;
10211 if (c == '8')
10212 {
10213#ifdef WIN3264
10214 has_shortname = 1; /* Postpone this. */
10215#endif
10216 continue;
10217 }
10218 pbuf = NULL;
10219 /* Need full path first (use expand_env() to remove a "~/") */
10220 if (!has_fullname)
10221 {
10222 if (c == '.' && **fnamep == '~')
10223 p = pbuf = expand_env_save(*fnamep);
10224 else
10225 p = pbuf = FullName_save(*fnamep, FALSE);
10226 }
10227 else
10228 p = *fnamep;
10229
10230 has_fullname = 0;
10231
10232 if (p != NULL)
10233 {
10234 if (c == '.')
10235 {
10236 mch_dirname(dirname, MAXPATHL);
10237 s = shorten_fname(p, dirname);
10238 if (s != NULL)
10239 {
10240 *fnamep = s;
10241 if (pbuf != NULL)
10242 {
10243 vim_free(*bufp); /* free any allocated file name */
10244 *bufp = pbuf;
10245 pbuf = NULL;
10246 }
10247 }
10248 }
10249 else
10250 {
10251 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10252 /* Only replace it when it starts with '~' */
10253 if (*dirname == '~')
10254 {
10255 s = vim_strsave(dirname);
10256 if (s != NULL)
10257 {
10258 *fnamep = s;
10259 vim_free(*bufp);
10260 *bufp = s;
10261 }
10262 }
10263 }
10264 vim_free(pbuf);
10265 }
10266 }
10267
10268 tail = gettail(*fnamep);
10269 *fnamelen = (int)STRLEN(*fnamep);
10270
10271 /* ":h" - head, remove "/file_name", can be repeated */
10272 /* Don't remove the first "/" or "c:\" */
10273 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10274 {
10275 valid |= VALID_HEAD;
10276 *usedlen += 2;
10277 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010278 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010279 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 *fnamelen = (int)(tail - *fnamep);
10281#ifdef VMS
10282 if (*fnamelen > 0)
10283 *fnamelen += 1; /* the path separator is part of the path */
10284#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010285 if (*fnamelen == 0)
10286 {
10287 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10288 p = vim_strsave((char_u *)".");
10289 if (p == NULL)
10290 return -1;
10291 vim_free(*bufp);
10292 *bufp = *fnamep = tail = p;
10293 *fnamelen = 1;
10294 }
10295 else
10296 {
10297 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010298 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 }
10301
10302 /* ":8" - shortname */
10303 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10304 {
10305 *usedlen += 2;
10306#ifdef WIN3264
10307 has_shortname = 1;
10308#endif
10309 }
10310
10311#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020010312 /*
10313 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 */
10315 if (has_shortname)
10316 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010317 /* Copy the string if it is shortened by :h and when it wasn't copied
10318 * yet, because we are going to change it in place. Avoids changing
10319 * the buffer name for "%:8". */
10320 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
10322 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010323 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 return -1;
10325 vim_free(*bufp);
10326 *bufp = *fnamep = p;
10327 }
10328
10329 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010330 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 if (!has_fullname && !vim_isAbsName(*fnamep))
10332 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010333 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 return -1;
10335 }
10336 else
10337 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010338 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339
Bram Moolenaardc935552011-08-17 15:23:23 +020010340 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010342 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 return -1;
10344
10345 if (l == 0)
10346 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010347 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010349 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 return -1;
10351 }
10352 *fnamelen = l;
10353 }
10354 }
10355#endif /* WIN3264 */
10356
10357 /* ":t" - tail, just the basename */
10358 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10359 {
10360 *usedlen += 2;
10361 *fnamelen -= (int)(tail - *fnamep);
10362 *fnamep = tail;
10363 }
10364
10365 /* ":e" - extension, can be repeated */
10366 /* ":r" - root, without extension, can be repeated */
10367 while (src[*usedlen] == ':'
10368 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10369 {
10370 /* find a '.' in the tail:
10371 * - for second :e: before the current fname
10372 * - otherwise: The last '.'
10373 */
10374 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10375 s = *fnamep - 2;
10376 else
10377 s = *fnamep + *fnamelen - 1;
10378 for ( ; s > tail; --s)
10379 if (s[0] == '.')
10380 break;
10381 if (src[*usedlen + 1] == 'e') /* :e */
10382 {
10383 if (s > tail)
10384 {
10385 *fnamelen += (int)(*fnamep - (s + 1));
10386 *fnamep = s + 1;
10387#ifdef VMS
10388 /* cut version from the extension */
10389 s = *fnamep + *fnamelen - 1;
10390 for ( ; s > *fnamep; --s)
10391 if (s[0] == ';')
10392 break;
10393 if (s > *fnamep)
10394 *fnamelen = s - *fnamep;
10395#endif
10396 }
10397 else if (*fnamep <= tail)
10398 *fnamelen = 0;
10399 }
10400 else /* :r */
10401 {
10402 if (s > tail) /* remove one extension */
10403 *fnamelen = (int)(s - *fnamep);
10404 }
10405 *usedlen += 2;
10406 }
10407
10408 /* ":s?pat?foo?" - substitute */
10409 /* ":gs?pat?foo?" - global substitute */
10410 if (src[*usedlen] == ':'
10411 && (src[*usedlen + 1] == 's'
10412 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10413 {
10414 char_u *str;
10415 char_u *pat;
10416 char_u *sub;
10417 int sep;
10418 char_u *flags;
10419 int didit = FALSE;
10420
10421 flags = (char_u *)"";
10422 s = src + *usedlen + 2;
10423 if (src[*usedlen + 1] == 'g')
10424 {
10425 flags = (char_u *)"g";
10426 ++s;
10427 }
10428
10429 sep = *s++;
10430 if (sep)
10431 {
10432 /* find end of pattern */
10433 p = vim_strchr(s, sep);
10434 if (p != NULL)
10435 {
10436 pat = vim_strnsave(s, (int)(p - s));
10437 if (pat != NULL)
10438 {
10439 s = p + 1;
10440 /* find end of substitution */
10441 p = vim_strchr(s, sep);
10442 if (p != NULL)
10443 {
10444 sub = vim_strnsave(s, (int)(p - s));
10445 str = vim_strnsave(*fnamep, *fnamelen);
10446 if (sub != NULL && str != NULL)
10447 {
10448 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010449 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (s != NULL)
10451 {
10452 *fnamep = s;
10453 *fnamelen = (int)STRLEN(s);
10454 vim_free(*bufp);
10455 *bufp = s;
10456 didit = TRUE;
10457 }
10458 }
10459 vim_free(sub);
10460 vim_free(str);
10461 }
10462 vim_free(pat);
10463 }
10464 }
10465 /* after using ":s", repeat all the modifiers */
10466 if (didit)
10467 goto repeat;
10468 }
10469 }
10470
Bram Moolenaar26df0922014-02-23 23:39:13 +010010471 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10472 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010473 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010474 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010475 if (c != NUL)
10476 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010477 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010478 if (c != NUL)
10479 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010480 if (p == NULL)
10481 return -1;
10482 vim_free(*bufp);
10483 *bufp = *fnamep = p;
10484 *fnamelen = (int)STRLEN(p);
10485 *usedlen += 2;
10486 }
10487
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 return valid;
10489}
10490
10491/*
10492 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010493 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 * "flags" can be "g" to do a global substitute.
10495 * Returns an allocated string, NULL for error.
10496 */
10497 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010498do_string_sub(
10499 char_u *str,
10500 char_u *pat,
10501 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010502 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010503 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
10505 int sublen;
10506 regmatch_T regmatch;
10507 int i;
10508 int do_all;
10509 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010510 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 garray_T ga;
10512 char_u *ret;
10513 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010514 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515
10516 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10517 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010518 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
10520 ga_init2(&ga, 1, 200);
10521
10522 do_all = (flags[0] == 'g');
10523
10524 regmatch.rm_ic = p_ic;
10525 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10526 if (regmatch.regprog != NULL)
10527 {
10528 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010529 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10531 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010532 /* Skip empty match except for first match. */
10533 if (regmatch.startp[0] == regmatch.endp[0])
10534 {
10535 if (zero_width == regmatch.startp[0])
10536 {
10537 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010538 i = MB_PTR2LEN(tail);
10539 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10540 (size_t)i);
10541 ga.ga_len += i;
10542 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010543 continue;
10544 }
10545 zero_width = regmatch.startp[0];
10546 }
10547
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 /*
10549 * Get some space for a temporary buffer to do the substitution
10550 * into. It will contain:
10551 * - The text up to where the match is.
10552 * - The substituted text.
10553 * - The text after the match.
10554 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010555 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010556 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10558 {
10559 ga_clear(&ga);
10560 break;
10561 }
10562
10563 /* copy the text up to where the match is */
10564 i = (int)(regmatch.startp[0] - tail);
10565 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10566 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010567 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 + ga.ga_len + i, TRUE, TRUE, FALSE);
10569 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010570 tail = regmatch.endp[0];
10571 if (*tail == NUL)
10572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 if (!do_all)
10574 break;
10575 }
10576
10577 if (ga.ga_data != NULL)
10578 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10579
Bram Moolenaar473de612013-06-08 18:19:48 +020010580 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 }
10582
10583 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10584 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010585 if (p_cpo == empty_option)
10586 p_cpo = save_cpo;
10587 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010588 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010589 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590
10591 return ret;
10592}
10593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010594 static int
10595filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10596{
10597 typval_T rettv;
10598 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010599 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600
10601 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10602 argv[0] = vimvars[VV_KEY].vv_tv;
10603 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010604 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10605 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 if (map)
10607 {
10608 /* map(): replace the list item value */
10609 clear_tv(tv);
10610 rettv.v_lock = 0;
10611 *tv = rettv;
10612 }
10613 else
10614 {
10615 int error = FALSE;
10616
10617 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010618 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 clear_tv(&rettv);
10620 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010621 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 if (error)
10623 goto theend;
10624 }
10625 retval = OK;
10626theend:
10627 clear_tv(&vimvars[VV_VAL].vv_tv);
10628 return retval;
10629}
10630
10631
10632/*
10633 * Implementation of map() and filter().
10634 */
10635 void
10636filter_map(typval_T *argvars, typval_T *rettv, int map)
10637{
10638 typval_T *expr;
10639 listitem_T *li, *nli;
10640 list_T *l = NULL;
10641 dictitem_T *di;
10642 hashtab_T *ht;
10643 hashitem_T *hi;
10644 dict_T *d = NULL;
10645 typval_T save_val;
10646 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010647 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648 int rem;
10649 int todo;
10650 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10651 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10652 : N_("filter() argument"));
10653 int save_did_emsg;
10654 int idx = 0;
10655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010656 if (argvars[0].v_type == VAR_BLOB)
10657 {
10658 if ((b = argvars[0].vval.v_blob) == NULL)
10659 return;
10660 }
10661 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010662 {
10663 if ((l = argvars[0].vval.v_list) == NULL
10664 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10665 return;
10666 }
10667 else if (argvars[0].v_type == VAR_DICT)
10668 {
10669 if ((d = argvars[0].vval.v_dict) == NULL
10670 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10671 return;
10672 }
10673 else
10674 {
10675 EMSG2(_(e_listdictarg), ermsg);
10676 return;
10677 }
10678
10679 expr = &argvars[1];
10680 /* On type errors, the preceding call has already displayed an error
10681 * message. Avoid a misleading error message for an empty string that
10682 * was not passed as argument. */
10683 if (expr->v_type != VAR_UNKNOWN)
10684 {
10685 prepare_vimvar(VV_VAL, &save_val);
10686
10687 /* We reset "did_emsg" to be able to detect whether an error
10688 * occurred during evaluation of the expression. */
10689 save_did_emsg = did_emsg;
10690 did_emsg = FALSE;
10691
10692 prepare_vimvar(VV_KEY, &save_key);
10693 if (argvars[0].v_type == VAR_DICT)
10694 {
10695 vimvars[VV_KEY].vv_type = VAR_STRING;
10696
10697 ht = &d->dv_hashtab;
10698 hash_lock(ht);
10699 todo = (int)ht->ht_used;
10700 for (hi = ht->ht_array; todo > 0; ++hi)
10701 {
10702 if (!HASHITEM_EMPTY(hi))
10703 {
10704 int r;
10705
10706 --todo;
10707 di = HI2DI(hi);
10708 if (map &&
10709 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10710 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10711 break;
10712 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10713 r = filter_map_one(&di->di_tv, expr, map, &rem);
10714 clear_tv(&vimvars[VV_KEY].vv_tv);
10715 if (r == FAIL || did_emsg)
10716 break;
10717 if (!map && rem)
10718 {
10719 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10720 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10721 break;
10722 dictitem_remove(d, di);
10723 }
10724 }
10725 }
10726 hash_unlock(ht);
10727 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010728 else if (argvars[0].v_type == VAR_BLOB)
10729 {
10730 int i;
10731 typval_T tv;
10732
10733 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10734 for (i = 0; i < b->bv_ga.ga_len; i++)
10735 {
10736 tv.v_type = VAR_NUMBER;
10737 tv.vval.v_number = blob_get(b, i);
10738 vimvars[VV_KEY].vv_nr = idx;
10739 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10740 break;
10741 if (tv.v_type != VAR_NUMBER)
10742 {
10743 EMSG(_(e_invalblob));
10744 return;
10745 }
10746 tv.v_type = VAR_NUMBER;
10747 blob_set(b, i, tv.vval.v_number);
10748 if (!map && rem)
10749 {
10750 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10751
10752 mch_memmove(p + idx, p + i + 1,
10753 (size_t)b->bv_ga.ga_len - i - 1);
10754 --b->bv_ga.ga_len;
10755 --i;
10756 }
10757 }
10758 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 else
10760 {
10761 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10762
10763 for (li = l->lv_first; li != NULL; li = nli)
10764 {
10765 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10766 break;
10767 nli = li->li_next;
10768 vimvars[VV_KEY].vv_nr = idx;
10769 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10770 || did_emsg)
10771 break;
10772 if (!map && rem)
10773 listitem_remove(l, li);
10774 ++idx;
10775 }
10776 }
10777
10778 restore_vimvar(VV_KEY, &save_key);
10779 restore_vimvar(VV_VAL, &save_val);
10780
10781 did_emsg |= save_did_emsg;
10782 }
10783
10784 copy_tv(&argvars[0], rettv);
10785}
10786
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */