blob: 0f9c95fa0d876afbc85e4f03c0685bb88582ad9b [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 Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000035
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010036#define NAMESPACE_CHAR (char_u *)"abglstvw"
37
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020038static dictitem_T globvars_var; /* variable used for g: */
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},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200174 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
175 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
176 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200177 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100178 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100179 {VV_NAME("false", VAR_SPECIAL), VV_RO},
180 {VV_NAME("true", VAR_SPECIAL), VV_RO},
181 {VV_NAME("null", VAR_SPECIAL), VV_RO},
182 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100183 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200184 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200185 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100195 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200196 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
197 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200198 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200199 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
200 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
201 {VV_NAME("event", VAR_DICT), VV_RO},
202 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000203};
204
205/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000206#define vv_type vv_di.di_tv.v_type
207#define vv_nr vv_di.di_tv.vval.v_number
208#define vv_float vv_di.di_tv.vval.v_float
209#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000210#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200211#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100212#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000213#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000214
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200215static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000216#define vimvarht vimvardict.dv_hashtab
217
Bram Moolenaar9937a052019-06-15 15:45:06 +0200218static void ex_let_const(exarg_T *eap, int is_const);
219static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
221static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static void list_glob_vars(int *first);
223static void list_buf_vars(int *first);
224static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_vim_vars(int *first);
227static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200229static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
230static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
233static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
234static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
235static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000236
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int eval2(char_u **arg, typval_T *rettv, int evaluate);
238static int eval3(char_u **arg, typval_T *rettv, int evaluate);
239static int eval4(char_u **arg, typval_T *rettv, int evaluate);
240static int eval5(char_u **arg, typval_T *rettv, int evaluate);
241static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
242static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000243
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
245static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static 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 +0200250static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100252static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100253static void list_one_var(dictitem_T *v, char *prefix, int *first);
254static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200255static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100256static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100257static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200259/* for VIM_VERSION_ defines */
260#include "version.h"
261
Bram Moolenaare21c1582019-03-02 11:57:09 +0100262/*
263 * Return "n1" divided by "n2", taking care of dividing by zero.
264 */
265 static varnumber_T
266num_divide(varnumber_T n1, varnumber_T n2)
267{
268 varnumber_T result;
269
270 if (n2 == 0) // give an error message?
271 {
272 if (n1 == 0)
273 result = VARNUM_MIN; // similar to NaN
274 else if (n1 < 0)
275 result = -VARNUM_MAX;
276 else
277 result = VARNUM_MAX;
278 }
279 else
280 result = n1 / n2;
281
282 return result;
283}
284
285/*
286 * Return "n1" modulus "n2", taking care of dividing by zero.
287 */
288 static varnumber_T
289num_modulus(varnumber_T n1, varnumber_T n2)
290{
291 // Give an error when n2 is 0?
292 return (n2 == 0) ? 0 : (n1 % n2);
293}
294
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100295
296#if defined(EBCDIC) || defined(PROTO)
297/*
298 * Compare struct fst by function name.
299 */
300 static int
301compare_func_name(const void *s1, const void *s2)
302{
303 struct fst *p1 = (struct fst *)s1;
304 struct fst *p2 = (struct fst *)s2;
305
306 return STRCMP(p1->f_name, p2->f_name);
307}
308
309/*
310 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100311 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100312 * On machines using EBCDIC we have to sort it.
313 */
314 static void
315sortFunctions(void)
316{
317 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
318
319 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
320}
321#endif
322
323
Bram Moolenaar33570922005-01-25 22:26:29 +0000324/*
325 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000326 */
327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000329{
Bram Moolenaar33570922005-01-25 22:26:29 +0000330 int i;
331 struct vimvar *p;
332
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200333 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
334 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200335 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000336 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000338
339 for (i = 0; i < VV_LEN; ++i)
340 {
341 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200342 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100344 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200345 getout(1);
346 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000347 STRCPY(p->vv_di.di_key, p->vv_name);
348 if (p->vv_flags & VV_RO)
349 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
350 else if (p->vv_flags & VV_RO_SBX)
351 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
352 else
353 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000354
355 /* add to v: scope dict, unless the value is not always available */
356 if (p->vv_type != VAR_UNKNOWN)
357 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000359 /* add to compat scope dict */
360 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000361 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100362 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200363 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100364
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000365 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100366 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100367 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100368 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100369 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370
371 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
372 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
373 set_vim_var_nr(VV_NONE, VVAL_NONE);
374 set_vim_var_nr(VV_NULL, VVAL_NULL);
375
Bram Moolenaarf562e722016-07-19 17:25:25 +0200376 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
377 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
378 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
379 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
380 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
381 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
382 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
383 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
384 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
385 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100386 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200387
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200388 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200389
390#ifdef EBCDIC
391 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100392 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200393 */
394 sortFunctions();
395#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000396}
397
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000398#if defined(EXITFREE) || defined(PROTO)
399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100400eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000401{
402 int i;
403 struct vimvar *p;
404
405 for (i = 0; i < VV_LEN; ++i)
406 {
407 p = &vimvars[i];
408 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100409 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000410 else if (p->vv_di.di_tv.v_type == VAR_LIST)
411 {
412 list_unref(p->vv_list);
413 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000414 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000415 }
416 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000417 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000418 hash_clear(&compat_hashtab);
419
Bram Moolenaard9fba312005-06-26 22:34:35 +0000420 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100421# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200422 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100423# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000424
425 /* global variables */
426 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000427
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000428 /* autoloaded script names */
429 ga_clear_strings(&ga_loaded);
430
Bram Moolenaarcca74132013-09-25 21:00:28 +0200431 /* Script-local variables. First clear all the variables and in a second
432 * loop free the scriptvar_T, because a variable in one script might hold
433 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200434 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200436 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200437 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200438 ga_clear(&ga_scripts);
439
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200440 // unreferenced lists and dicts
441 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200442
443 // functions not garbage collected
444 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000445}
446#endif
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 * Set an internal variable to a string value. Creates the variable if it does
451 * not already exist.
452 */
453 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100454set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000456 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000457 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
459 val = vim_strsave(value);
460 if (val != NULL)
461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000463 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000465 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000466 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 }
468 }
469}
470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200472#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000473static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474static char_u *redir_endp = NULL;
475static char_u *redir_varname = NULL;
476
477/*
478 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100479 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000480 * Returns OK if successfully completed the setup. FAIL otherwise.
481 */
482 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100483var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000484{
485 int save_emsg;
486 int err;
487 typval_T tv;
488
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000489 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000490 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100492 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 return FAIL;
494 }
495
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000496 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 redir_varname = vim_strsave(name);
498 if (redir_varname == NULL)
499 return FAIL;
500
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200501 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 if (redir_lval == NULL)
503 {
504 var_redir_stop();
505 return FAIL;
506 }
507
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000508 /* The output is stored in growarray "redir_ga" until redirection ends. */
509 ga_init2(&redir_ga, (int)sizeof(char), 500);
510
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100512 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000513 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
515 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200516 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 if (redir_endp != NULL && *redir_endp != NUL)
518 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100519 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100521 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000522 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000523 var_redir_stop();
524 return FAIL;
525 }
526
527 /* check if we can write to the variable: set it to or append an empty
528 * string */
529 save_emsg = did_emsg;
530 did_emsg = FALSE;
531 tv.v_type = VAR_STRING;
532 tv.vval.v_string = (char_u *)"";
533 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200534 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200536 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200537 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000539 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 if (err)
541 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 var_redir_stop();
544 return FAIL;
545 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546
547 return OK;
548}
549
550/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000551 * Append "value[value_len]" to the variable set by var_redir_start().
552 * The actual appending is postponed until redirection ends, because the value
553 * appended may in fact be the string we write to, changing it may cause freed
554 * memory to be used:
555 * :redir => foo
556 * :let foo
557 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558 */
559 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100560var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000561{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000562 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563
564 if (redir_lval == NULL)
565 return;
566
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000568 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000569 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000570 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000572 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000573 {
574 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000575 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000576 }
577 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579}
580
581/*
582 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000583 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000584 */
585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100586var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000587{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000588 typval_T tv;
589
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200590 if (EVALCMD_BUSY)
591 {
592 redir_lval = NULL;
593 return;
594 }
595
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000596 if (redir_lval != NULL)
597 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000598 /* If there was no error: assign the text to the variable. */
599 if (redir_endp != NULL)
600 {
601 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
602 tv.v_type = VAR_STRING;
603 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200604 /* Call get_lval() again, if it's inside a Dict or List it may
605 * have changed. */
606 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100607 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200608 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200609 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
610 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200611 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000612 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000613
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000614 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100615 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000616
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100619 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100623eval_charconvert(
624 char_u *enc_from,
625 char_u *enc_to,
626 char_u *fname_from,
627 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 int err = FALSE;
630
631 set_vim_var_string(VV_CC_FROM, enc_from, -1);
632 set_vim_var_string(VV_CC_TO, enc_to, -1);
633 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
634 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
635 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
636 err = TRUE;
637 set_vim_var_string(VV_CC_FROM, NULL, -1);
638 set_vim_var_string(VV_CC_TO, NULL, -1);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641
642 if (err)
643 return FAIL;
644 return OK;
645}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 int err = FALSE;
652
653 set_vim_var_string(VV_FNAME_IN, fname, -1);
654 set_vim_var_string(VV_CMDARG, args, -1);
655 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
656 err = TRUE;
657 set_vim_var_string(VV_FNAME_IN, NULL, -1);
658 set_vim_var_string(VV_CMDARG, NULL, -1);
659
660 if (err)
661 {
662 mch_remove(fname);
663 return FAIL;
664 }
665 return OK;
666}
667# endif
668
669# if defined(FEAT_DIFF) || defined(PROTO)
670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_diff(
672 char_u *origfile,
673 char_u *newfile,
674 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 int err = FALSE;
677
678 set_vim_var_string(VV_FNAME_IN, origfile, -1);
679 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
680 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
681 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
682 set_vim_var_string(VV_FNAME_IN, NULL, -1);
683 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
684 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
685}
686
687 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100688eval_patch(
689 char_u *origfile,
690 char_u *difffile,
691 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 int err;
694
695 set_vim_var_string(VV_FNAME_IN, origfile, -1);
696 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
697 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
698 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
699 set_vim_var_string(VV_FNAME_IN, NULL, -1);
700 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
701 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
702}
703# endif
704
705/*
706 * Top level evaluation function, returning a boolean.
707 * Sets "error" to TRUE if there was an error.
708 * Return TRUE or FALSE.
709 */
710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100711eval_to_bool(
712 char_u *arg,
713 int *error,
714 char_u **nextcmd,
715 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200718 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 if (skip)
721 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000722 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
725 {
726 *error = FALSE;
727 if (!skip)
728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100729 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732 }
733 if (skip)
734 --emsg_skip;
735
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200736 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100739/*
740 * Call eval1() and give an error message if not done at a lower level.
741 */
742 static int
743eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
744{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100745 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100746 int ret;
747 int did_emsg_before = did_emsg;
748 int called_emsg_before = called_emsg;
749
750 ret = eval1(arg, rettv, evaluate);
751 if (ret == FAIL)
752 {
753 // Report the invalid expression unless the expression evaluation has
754 // been cancelled due to an aborting error, an interrupt, or an
755 // exception, or we already gave a more specific error.
756 // Also check called_emsg for when using assert_fails().
757 if (!aborting() && did_emsg == did_emsg_before
758 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100759 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100760 }
761 return ret;
762}
763
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200764 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100765eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
766{
767 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100768 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200769 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100770
771 if (expr->v_type == VAR_FUNC)
772 {
773 s = expr->vval.v_string;
774 if (s == NULL || *s == NUL)
775 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200776 vim_memset(&funcexe, 0, sizeof(funcexe));
777 funcexe.evaluate = TRUE;
778 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100779 return FAIL;
780 }
781 else if (expr->v_type == VAR_PARTIAL)
782 {
783 partial_T *partial = expr->vval.v_partial;
784
785 s = partial_name(partial);
786 if (s == NULL || *s == NUL)
787 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200788 vim_memset(&funcexe, 0, sizeof(funcexe));
789 funcexe.evaluate = TRUE;
790 funcexe.partial = partial;
791 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100792 return FAIL;
793 }
794 else
795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100796 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100797 if (s == NULL)
798 return FAIL;
799 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100800 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100801 return FAIL;
802 if (*s != NUL) /* check for trailing chars after expr */
803 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200804 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100806 return FAIL;
807 }
808 }
809 return OK;
810}
811
812/*
813 * Like eval_to_bool() but using a typval_T instead of a string.
814 * Works for string, funcref and partial.
815 */
816 int
817eval_expr_to_bool(typval_T *expr, int *error)
818{
819 typval_T rettv;
820 int res;
821
822 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
823 {
824 *error = TRUE;
825 return FALSE;
826 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100827 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100828 clear_tv(&rettv);
829 return res;
830}
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832/*
833 * Top level evaluation function, returning a string. If "skip" is TRUE,
834 * only parsing to "nextcmd" is done, without reporting errors. Return
835 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
836 */
837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838eval_to_string_skip(
839 char_u *arg,
840 char_u **nextcmd,
841 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 char_u *retval;
845
846 if (skip)
847 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 retval = NULL;
850 else
851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100852 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 if (skip)
856 --emsg_skip;
857
858 return retval;
859}
860
861/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000862 * Skip over an expression at "*pp".
863 * Return FAIL for an error, OK otherwise.
864 */
865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100866skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867{
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000869
870 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000872}
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000876 * When "convert" is TRUE convert a List into a sequence of lines and convert
877 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Return pointer to allocated memory, or NULL for failure.
879 */
880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100881eval_to_string(
882 char_u *arg,
883 char_u **nextcmd,
884 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000888 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000889#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 retval = NULL;
895 else
896 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000897 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000898 {
899 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000900 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200901 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200902 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200903 if (tv.vval.v_list->lv_len > 0)
904 ga_append(&ga, NL);
905 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000906 ga_append(&ga, NUL);
907 retval = (char_u *)ga.ga_data;
908 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000909#ifdef FEAT_FLOAT
910 else if (convert && tv.v_type == VAR_FLOAT)
911 {
912 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
913 retval = vim_strsave(numbuf);
914 }
915#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000916 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100917 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 return retval;
922}
923
924/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000925 * Call eval_to_string() without using current local variables and using
926 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 */
928 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100929eval_to_string_safe(
930 char_u *arg,
931 char_u **nextcmd,
932 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200935 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200937 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000938 if (use_sandbox)
939 ++sandbox;
940 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000941 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000942 if (use_sandbox)
943 --sandbox;
944 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200945 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 return retval;
947}
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949/*
950 * Top level evaluation function, returning a number.
951 * Evaluates "expr" silently.
952 * Returns -1 for an error.
953 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200954 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200958 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000959 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 ++emsg_off;
962
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000963 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 retval = -1;
965 else
966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100967 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 --emsg_off;
971
972 return retval;
973}
974
Bram Moolenaara40058a2005-07-11 22:42:07 +0000975/*
976 * Prepare v: variable "idx" to be used.
977 * Save the current typeval in "save_tv".
978 * When not used yet add the variable to the v: hashtable.
979 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100981prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000982{
983 *save_tv = vimvars[idx].vv_tv;
984 if (vimvars[idx].vv_type == VAR_UNKNOWN)
985 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
986}
987
988/*
989 * Restore v: variable "idx" to typeval "save_tv".
990 * When no longer defined, remove the variable from the v: hashtable.
991 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100993restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000994{
995 hashitem_T *hi;
996
Bram Moolenaara40058a2005-07-11 22:42:07 +0000997 vimvars[idx].vv_tv = *save_tv;
998 if (vimvars[idx].vv_type == VAR_UNKNOWN)
999 {
1000 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1001 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001002 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001003 else
1004 hash_remove(&vimvarht, hi);
1005 }
1006}
1007
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001008#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001009/*
1010 * Evaluate an expression to a list with suggestions.
1011 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001012 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001013 */
1014 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001015eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001016{
1017 typval_T save_val;
1018 typval_T rettv;
1019 list_T *list = NULL;
1020 char_u *p = skipwhite(expr);
1021
1022 /* Set "v:val" to the bad word. */
1023 prepare_vimvar(VV_VAL, &save_val);
1024 vimvars[VV_VAL].vv_type = VAR_STRING;
1025 vimvars[VV_VAL].vv_str = badword;
1026 if (p_verbose == 0)
1027 ++emsg_off;
1028
1029 if (eval1(&p, &rettv, TRUE) == OK)
1030 {
1031 if (rettv.v_type != VAR_LIST)
1032 clear_tv(&rettv);
1033 else
1034 list = rettv.vval.v_list;
1035 }
1036
1037 if (p_verbose == 0)
1038 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001039 restore_vimvar(VV_VAL, &save_val);
1040
1041 return list;
1042}
1043
1044/*
1045 * "list" is supposed to contain two items: a word and a number. Return the
1046 * word in "pp" and the number as the return value.
1047 * Return -1 if anything isn't right.
1048 * Used to get the good word and score from the eval_spell_expr() result.
1049 */
1050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001051get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001052{
1053 listitem_T *li;
1054
1055 li = list->lv_first;
1056 if (li == NULL)
1057 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001058 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001059
1060 li = li->li_next;
1061 if (li == NULL)
1062 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001063 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001064}
1065#endif
1066
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001067/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001068 * Top level evaluation function.
1069 * Returns an allocated typval_T with the result.
1070 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001071 */
1072 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001074{
1075 typval_T *tv;
1076
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001077 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001078 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001079 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001080
1081 return tv;
1082}
1083
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001086 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001087 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1088 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001092call_vim_function(
1093 char_u *func,
1094 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001095 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001096 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001099 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001101 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001102 vim_memset(&funcexe, 0, sizeof(funcexe));
1103 funcexe.firstline = curwin->w_cursor.lnum;
1104 funcexe.lastline = curwin->w_cursor.lnum;
1105 funcexe.evaluate = TRUE;
1106 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001107 if (ret == FAIL)
1108 clear_tv(rettv);
1109
1110 return ret;
1111}
1112
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001113/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001114 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001115 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001116 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1117 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001118 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001119 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001120call_func_retnr(
1121 char_u *func,
1122 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001123 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001124{
1125 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001126 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001127
Bram Moolenaarded27a12018-08-01 19:06:03 +02001128 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001129 return -1;
1130
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001131 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001132 clear_tv(&rettv);
1133 return retval;
1134}
1135
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001136#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001137 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1138
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001139# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001140/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001141 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001142 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001143 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1144 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001145 */
1146 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001147call_func_retstr(
1148 char_u *func,
1149 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001150 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001151{
1152 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001153 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001154
Bram Moolenaarded27a12018-08-01 19:06:03 +02001155 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156 return NULL;
1157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001158 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001159 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 return retval;
1161}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001162# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001163
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001164/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001165 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001166 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1167 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001168 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001169 */
1170 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171call_func_retlist(
1172 char_u *func,
1173 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001174 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001175{
1176 typval_T rettv;
1177
Bram Moolenaarded27a12018-08-01 19:06:03 +02001178 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001179 return NULL;
1180
1181 if (rettv.v_type != VAR_LIST)
1182 {
1183 clear_tv(&rettv);
1184 return NULL;
1185 }
1186
1187 return rettv.vval.v_list;
1188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#endif
1190
Bram Moolenaar05159a02005-02-26 23:04:13 +00001191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#ifdef FEAT_FOLDING
1193/*
1194 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1195 * it in "*cp". Doesn't give error messages.
1196 */
1197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001198eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
Bram Moolenaar33570922005-01-25 22:26:29 +00001200 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001201 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001203 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1204 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001207 if (use_sandbox)
1208 ++sandbox;
1209 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 retval = 0;
1213 else
1214 {
1215 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001216 if (tv.v_type == VAR_NUMBER)
1217 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001218 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 retval = 0;
1220 else
1221 {
1222 /* If the result is a string, check if there is a non-digit before
1223 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (!VIM_ISDIGIT(*s) && *s != '-')
1226 *cp = *s++;
1227 retval = atol((char *)s);
1228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001232 if (use_sandbox)
1233 --sandbox;
1234 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001236 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237}
1238#endif
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001241 * Get a list of lines from a HERE document. The here document is a list of
1242 * lines surrounded by a marker.
1243 * cmd << {marker}
1244 * {line1}
1245 * {line2}
1246 * ....
1247 * {marker}
1248 *
1249 * The {marker} is a string. If the optional 'trim' word is supplied before the
1250 * marker, then the leading indentation before the lines (matching the
1251 * indentation in the 'cmd' line) is stripped.
1252 * Returns a List with {lines} or NULL.
1253 */
1254 static list_T *
1255heredoc_get(exarg_T *eap, char_u *cmd)
1256{
1257 char_u *theline;
1258 char_u *marker;
1259 list_T *l;
1260 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001261 int marker_indent_len = 0;
1262 int text_indent_len = 0;
1263 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001264
1265 if (eap->getline == NULL)
1266 {
1267 emsg(_("E991: cannot use =<< here"));
1268 return NULL;
1269 }
1270
1271 // Check for the optional 'trim' word before the marker
1272 cmd = skipwhite(cmd);
1273 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1274 {
1275 cmd = skipwhite(cmd + 4);
1276
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001277 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001278 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001279 // the first line after the :let command line. To find the end marker
1280 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001281 p = *eap->cmdlinep;
1282 while (VIM_ISWHITE(*p))
1283 {
1284 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001285 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001286 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001287 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001288 }
1289
Bram Moolenaar24582002019-07-21 14:14:26 +02001290 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001291 if (*cmd != NUL && *cmd != '"')
1292 {
1293 marker = skipwhite(cmd);
1294 p = skiptowhite(marker);
1295 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1296 {
1297 emsg(_(e_trailing));
1298 return NULL;
1299 }
1300 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001301 if (vim_islower(*marker))
1302 {
1303 emsg(_("E221: Marker cannot start with lower case letter"));
1304 return NULL;
1305 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001306 }
1307 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001308 {
1309 emsg(_("E172: Missing marker"));
1310 return NULL;
1311 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001312
1313 l = list_alloc();
1314 if (l == NULL)
1315 return NULL;
1316
1317 for (;;)
1318 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001319 int mi = 0;
1320 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001321
Bram Moolenaare96a2492019-06-25 04:12:16 +02001322 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001323 if (theline == NULL)
1324 {
1325 semsg(_("E990: Missing end marker '%s'"), marker);
1326 break;
1327 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001328
1329 // with "trim": skip the indent matching the :let line to find the
1330 // marker
1331 if (marker_indent_len > 0
1332 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1333 mi = marker_indent_len;
1334 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001335 {
1336 vim_free(theline);
1337 break;
1338 }
1339
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001340 if (text_indent_len == -1 && *theline != NUL)
1341 {
1342 // set the text indent from the first line.
1343 p = theline;
1344 text_indent_len = 0;
1345 while (VIM_ISWHITE(*p))
1346 {
1347 p++;
1348 text_indent_len++;
1349 }
1350 text_indent = vim_strnsave(theline, text_indent_len);
1351 }
1352 // with "trim": skip the indent matching the first line
1353 if (text_indent != NULL)
1354 for (ti = 0; ti < text_indent_len; ++ti)
1355 if (theline[ti] != text_indent[ti])
1356 break;
1357
1358 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001359 break;
1360 vim_free(theline);
1361 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001362 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001363
1364 return l;
1365}
1366
1367/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 * ":let" list all variable values
1369 * ":let var1 var2" list variable values
1370 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 * ":let var += expr" assignment command.
1372 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001373 * ":let var *= expr" assignment command.
1374 * ":let var /= expr" assignment command.
1375 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001376 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001377 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 */
1380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001381ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001383 ex_let_const(eap, FALSE);
1384}
1385
1386/*
1387 * ":const" list all variable values
1388 * ":const var1 var2" list variable values
1389 * ":const var = expr" assignment command.
1390 * ":const [var1, var2] = expr" unpack list.
1391 */
1392 void
1393ex_const(exarg_T *eap)
1394{
1395 ex_let_const(eap, TRUE);
1396}
1397
1398 static void
1399ex_let_const(exarg_T *eap, int is_const)
1400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001403 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 int var_count = 0;
1406 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001407 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001408 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001409 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001410 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaardb552d602006-03-23 22:59:57 +00001412 argend = skip_var_list(arg, &var_count, &semicolon);
1413 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001415 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001416 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001417 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001418 concat = expr[0] == '.'
1419 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1420 || (expr[1] == '.' && expr[2] == '='));
1421 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1422 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001424 /*
1425 * ":let" without "=": list variables
1426 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001428 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001429 else if (expr[0] == '.')
1430 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001433 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001437 list_glob_vars(&first);
1438 list_buf_vars(&first);
1439 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001440 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001441 list_script_vars(&first);
1442 list_func_vars(&first);
1443 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 eap->nextcmd = check_nextcmd(arg);
1446 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001447 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1448 {
1449 list_T *l;
1450
1451 // HERE document
1452 l = heredoc_get(eap, expr + 3);
1453 if (l != NULL)
1454 {
1455 rettv_list_set(&rettv, l);
1456 op[0] = '=';
1457 op[1] = NUL;
1458 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001459 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001460 clear_tv(&rettv);
1461 }
1462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 else
1464 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 op[0] = '=';
1466 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001467 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001469 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001470 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001471 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001472 if (expr[0] == '.' && expr[1] == '.') // ..=
1473 ++expr;
1474 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001475 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001477 else
1478 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (eap->skip)
1481 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001482 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (eap->skip)
1484 {
1485 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 --emsg_skip;
1488 }
1489 else if (i != FAIL)
1490 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001491 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001492 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495 }
1496}
1497
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498/*
1499 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1500 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001501 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1503 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001504 * Returns OK or FAIL;
1505 */
1506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507ex_let_vars(
1508 char_u *arg_start,
1509 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001510 int copy, // copy values from "tv", don't move
1511 int semicolon, // from skip_var_list()
1512 int var_count, // from skip_var_list()
1513 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001514 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515{
1516 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001517 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001518 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001519 listitem_T *item;
1520 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521
1522 if (*arg != '[')
1523 {
1524 /*
1525 * ":let var = expr" or ":for var in list"
1526 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001527 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528 return FAIL;
1529 return OK;
1530 }
1531
1532 /*
1533 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1534 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001535 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001537 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 return FAIL;
1539 }
1540
1541 i = list_len(l);
1542 if (semicolon == 0 && var_count < i)
1543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001545 return FAIL;
1546 }
1547 if (var_count - semicolon > i)
1548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001550 return FAIL;
1551 }
1552
1553 item = l->lv_first;
1554 while (*arg != ']')
1555 {
1556 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001557 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001558 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001559 item = item->li_next;
1560 if (arg == NULL)
1561 return FAIL;
1562
1563 arg = skipwhite(arg);
1564 if (*arg == ';')
1565 {
1566 /* Put the rest of the list (may be empty) in the var after ';'.
1567 * Create a new list for this. */
1568 l = list_alloc();
1569 if (l == NULL)
1570 return FAIL;
1571 while (item != NULL)
1572 {
1573 list_append_tv(l, &item->li_tv);
1574 item = item->li_next;
1575 }
1576
1577 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001578 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 ltv.vval.v_list = l;
1580 l->lv_refcount = 1;
1581
Bram Moolenaar9937a052019-06-15 15:45:06 +02001582 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001583 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 clear_tv(&ltv);
1585 if (arg == NULL)
1586 return FAIL;
1587 break;
1588 }
1589 else if (*arg != ',' && *arg != ']')
1590 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001591 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592 return FAIL;
1593 }
1594 }
1595
1596 return OK;
1597}
1598
1599/*
1600 * Skip over assignable variable "var" or list of variables "[var, var]".
1601 * Used for ":let varvar = expr" and ":for varvar in expr".
1602 * For "[var, var]" increment "*var_count" for each variable.
1603 * for "[var, var; var]" set "semicolon".
1604 * Return NULL for an error.
1605 */
1606 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607skip_var_list(
1608 char_u *arg,
1609 int *var_count,
1610 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611{
1612 char_u *p, *s;
1613
1614 if (*arg == '[')
1615 {
1616 /* "[var, var]": find the matching ']'. */
1617 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 {
1620 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1621 s = skip_var_one(p);
1622 if (s == p)
1623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001624 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 return NULL;
1626 }
1627 ++*var_count;
1628
1629 p = skipwhite(s);
1630 if (*p == ']')
1631 break;
1632 else if (*p == ';')
1633 {
1634 if (*semicolon == 1)
1635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001636 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001637 return NULL;
1638 }
1639 *semicolon = 1;
1640 }
1641 else if (*p != ',')
1642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644 return NULL;
1645 }
1646 }
1647 return p + 1;
1648 }
1649 else
1650 return skip_var_one(arg);
1651}
1652
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001653/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001654 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001655 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001656 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001660 if (*arg == '@' && arg[1] != NUL)
1661 return arg + 2;
1662 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1663 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664}
1665
Bram Moolenaara7043832005-01-21 11:56:39 +00001666/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 * List variables for hashtab "ht" with prefix "prefix".
1668 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001669 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671list_hashtable_vars(
1672 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001673 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001674 int empty,
1675 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001676{
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 hashitem_T *hi;
1678 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001679 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001680 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001681
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001682 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001683 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1684 {
1685 if (!HASHITEM_EMPTY(hi))
1686 {
1687 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001688 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001689
1690 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001691 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1692 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001693 if (message_filtered(buf))
1694 continue;
1695
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 if (empty || di->di_tv.v_type != VAR_STRING
1697 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001698 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001699 }
1700 }
1701}
1702
1703/*
1704 * List global variables.
1705 */
1706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001707list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001708{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001709 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001710}
1711
1712/*
1713 * List buffer variables.
1714 */
1715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001716list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001717{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001718 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001719}
1720
1721/*
1722 * List window variables.
1723 */
1724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001726{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001727 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001728}
1729
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001730/*
1731 * List tab page variables.
1732 */
1733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001735{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001736 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001737}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001738
Bram Moolenaara7043832005-01-21 11:56:39 +00001739/*
1740 * List Vim variables.
1741 */
1742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001743list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001745 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746}
1747
1748/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001749 * List script-local variables, if there is a script.
1750 */
1751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001753{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001754 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1755 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001756 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001757}
1758
1759/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 * List variables in "arg".
1761 */
1762 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764{
1765 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001766 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001768 char_u *name_start;
1769 char_u *arg_subsc;
1770 char_u *tofree;
1771 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772
1773 while (!ends_excmd(*arg) && !got_int)
1774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001775 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001777 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001778 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001779 {
1780 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001781 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001782 break;
1783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001787 /* get_name_len() takes care of expanding curly braces */
1788 name_start = name = arg;
1789 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1790 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001792 /* This is mainly to keep test 49 working: when expanding
1793 * curly braces fails overrule the exception error message. */
1794 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001796 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001797 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001798 break;
1799 }
1800 error = TRUE;
1801 }
1802 else
1803 {
1804 if (tofree != NULL)
1805 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001806 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 else
1809 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001810 /* handle d.key, l[idx], f(expr) */
1811 arg_subsc = arg;
1812 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001813 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001815 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001816 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001819 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001820 case 'g': list_glob_vars(first); break;
1821 case 'b': list_buf_vars(first); break;
1822 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001823 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001824 case 'v': list_vim_vars(first); break;
1825 case 's': list_script_vars(first); break;
1826 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001828 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001829 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001830 }
1831 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001832 {
1833 char_u numbuf[NUMBUFLEN];
1834 char_u *tf;
1835 int c;
1836 char_u *s;
1837
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001838 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001839 c = *arg;
1840 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001841 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001842 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 tv.v_type,
1844 s == NULL ? (char_u *)"" : s,
1845 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 *arg = c;
1847 vim_free(tf);
1848 }
1849 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 }
1852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001853
1854 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001856
1857 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 }
1859
1860 return arg;
1861}
1862
1863/*
1864 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1865 * Returns a pointer to the char just after the var name.
1866 * Returns NULL if there is an error.
1867 */
1868 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001870 char_u *arg, // points to variable name
1871 typval_T *tv, // value to assign to variable
1872 int copy, // copy value from "tv"
1873 int is_const, // lock variable for const
1874 char_u *endchars, // valid chars after variable name or NULL
1875 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876{
1877 int c1;
1878 char_u *name;
1879 char_u *p;
1880 char_u *arg_end = NULL;
1881 int len;
1882 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884
1885 /*
1886 * ":let $VAR = expr": Set environment variable.
1887 */
1888 if (*arg == '$')
1889 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001890 if (is_const)
1891 {
1892 emsg(_("E996: Cannot lock an environment variable"));
1893 return NULL;
1894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 /* Find the end of the name. */
1896 ++arg;
1897 name = arg;
1898 len = get_env_len(&arg);
1899 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001900 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 else
1902 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001903 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001907 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001908 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 {
1910 c1 = name[len];
1911 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001912 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001913 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 {
1915 int mustfree = FALSE;
1916 char_u *s = vim_getenv(name, &mustfree);
1917
1918 if (s != NULL)
1919 {
1920 p = tofree = concat_str(s, p);
1921 if (mustfree)
1922 vim_free(s);
1923 }
1924 }
1925 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001926 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001928 if (STRICMP(name, "HOME") == 0)
1929 init_homedir();
1930 else if (didset_vim && STRICMP(name, "VIM") == 0)
1931 didset_vim = FALSE;
1932 else if (didset_vimruntime
1933 && STRICMP(name, "VIMRUNTIME") == 0)
1934 didset_vimruntime = FALSE;
1935 arg_end = arg;
1936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
1940 }
1941 }
1942
1943 /*
1944 * ":let &option = expr": Set option value.
1945 * ":let &l:option = expr": Set local option value.
1946 * ":let &g:option = expr": Set global option value.
1947 */
1948 else if (*arg == '&')
1949 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001950 if (is_const)
1951 {
1952 emsg(_("E996: Cannot lock an option"));
1953 return NULL;
1954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 /* Find the end of the name. */
1956 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 if (p == NULL || (endchars != NULL
1958 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 else
1961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 long n;
1963 int opt_type;
1964 long numval;
1965 char_u *stringval = NULL;
1966 char_u *s;
1967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 c1 = *p;
1969 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001971 n = (long)tv_get_number(tv);
1972 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001973 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 {
1975 opt_type = get_option_value(arg, &numval,
1976 &stringval, opt_flags);
1977 if ((opt_type == 1 && *op == '.')
1978 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001981 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 else
1984 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001985 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001987 switch (*op)
1988 {
1989 case '+': n = numval + n; break;
1990 case '-': n = numval - n; break;
1991 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001992 case '/': n = (long)num_divide(numval, n); break;
1993 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001994 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001996 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 {
1998 s = concat_str(stringval, s);
1999 vim_free(stringval);
2000 stringval = s;
2001 }
2002 }
2003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002004 if (s != NULL)
2005 {
2006 set_option_value(arg, n, s, opt_flags);
2007 arg_end = p;
2008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002011 }
2012 }
2013
2014 /*
2015 * ":let @r = expr": Set register contents.
2016 */
2017 else if (*arg == '@')
2018 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002019 if (is_const)
2020 {
2021 emsg(_("E996: Cannot lock a register"));
2022 return NULL;
2023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002025 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 else
2031 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002032 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 char_u *s;
2034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002035 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002038 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002039 if (s != NULL)
2040 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002041 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 vim_free(s);
2043 }
2044 }
2045 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002048 arg_end = arg + 1;
2049 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002050 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 }
2052 }
2053
2054 /*
2055 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002058 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002060 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002062 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002066 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 else
2068 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002069 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 arg_end = p;
2071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 }
2075
2076 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078
2079 return arg_end;
2080}
2081
2082/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 * Get an lval: variable, Dict item or List item that can be assigned a value
2084 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2085 * "name.key", "name.key[expr]" etc.
2086 * Indexing only works if "name" is an existing List or Dictionary.
2087 * "name" points to the start of the name.
2088 * If "rettv" is not NULL it points to the value to be assigned.
2089 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2090 * wrong; must end in space or cmd separator.
2091 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002092 * flags:
2093 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002094 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002095 * GLV_NO_AUTOLOAD: do not use script autoloading
2096 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002102get_lval(
2103 char_u *name,
2104 typval_T *rettv,
2105 lval_T *lp,
2106 int unlet,
2107 int skip,
2108 int flags, /* GLV_ values */
2109 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 char_u *expr_start, *expr_end;
2113 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 dictitem_T *v;
2115 typval_T var1;
2116 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002122 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126
2127 if (skip)
2128 {
2129 /* When skipping just find the end of the name. */
2130 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002131 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 }
2133
2134 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002135 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (expr_start != NULL)
2137 {
2138 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002139 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 && *p != '[' && *p != '.')
2141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002142 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 return NULL;
2144 }
2145
2146 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2147 if (lp->ll_exp_name == NULL)
2148 {
2149 /* Report an invalid expression in braces, unless the
2150 * expression evaluation has been cancelled due to an
2151 * aborting error, an interrupt, or an exception. */
2152 if (!aborting() && !quiet)
2153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002154 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002155 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 return NULL;
2157 }
2158 }
2159 lp->ll_name = lp->ll_exp_name;
2160 }
2161 else
2162 lp->ll_name = name;
2163
2164 /* Without [idx] or .key we are done. */
2165 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2166 return p;
2167
2168 cc = *p;
2169 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002170 /* Only pass &ht when we would write to the variable, it prevents autoload
2171 * as well. */
2172 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2173 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002175 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 if (v == NULL)
2178 return NULL;
2179
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 /*
2181 * Loop until no more [idx] or .key is following.
2182 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002184 var1.v_type = VAR_UNKNOWN;
2185 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2189 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002190 && lp->ll_tv->vval.v_dict != NULL)
2191 && !(lp->ll_tv->v_type == VAR_BLOB
2192 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002195 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002204
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 len = -1;
2206 if (*p == '.')
2207 {
2208 key = p + 1;
2209 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2210 ;
2211 if (len == 0)
2212 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002214 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 }
2217 p = key + len;
2218 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002219 else
2220 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002221 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002222 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 if (*p == ':')
2224 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002225 else
2226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 empty1 = FALSE;
2228 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002230 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002231 {
2232 /* not a number or string */
2233 clear_tv(&var1);
2234 return NULL;
2235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002236 }
2237
2238 /* Optionally get the second index [ :expr]. */
2239 if (*p == ':')
2240 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002244 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002245 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002247 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002248 if (rettv != NULL
2249 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002250 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002251 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002252 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002255 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002256 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258 }
2259 p = skipwhite(p + 1);
2260 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002262 else
2263 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2266 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002267 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002269 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002270 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002271 {
2272 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002273 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 clear_tv(&var2);
2275 return NULL;
2276 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002277 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002282
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002284 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002286 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002287 clear_tv(&var1);
2288 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002290 }
2291
2292 /* Skip to past ']'. */
2293 ++p;
2294 }
2295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002297 {
2298 if (len == -1)
2299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002301 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002302 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002303 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002304 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306 }
2307 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 lp->ll_list = NULL;
2309 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002310 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002311
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002312 /* When assigning to a scope dictionary check that a function and
2313 * variable name is valid (only variable name unless it is l: or
2314 * g: dictionary). Disallow overwriting a builtin function. */
2315 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002316 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002317 int prevval;
2318 int wrong;
2319
2320 if (len != -1)
2321 {
2322 prevval = key[len];
2323 key[len] = NUL;
2324 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002325 else
2326 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002327 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2328 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002329 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002330 || !valid_varname(key);
2331 if (len != -1)
2332 key[len] = prevval;
2333 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002334 return NULL;
2335 }
2336
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002339 // Can't add "v:" or "a:" variable.
2340 if (lp->ll_dict == &vimvardict
2341 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002343 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002344 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002345 return NULL;
2346 }
2347
Bram Moolenaar31b81602019-02-10 22:14:27 +01002348 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002352 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002353 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 }
2356 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002360 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 p = NULL;
2363 break;
2364 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002365 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002366 else if ((flags & GLV_READ_ONLY) == 0
2367 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002368 {
2369 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002370 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002371 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002372
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002373 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002376 else if (lp->ll_tv->v_type == VAR_BLOB)
2377 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002378 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2379
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002380 /*
2381 * Get the number and item for the only or first index of the List.
2382 */
2383 if (empty1)
2384 lp->ll_n1 = 0;
2385 else
2386 // is number or string
2387 lp->ll_n1 = (long)tv_get_number(&var1);
2388 clear_tv(&var1);
2389
2390 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002391 || lp->ll_n1 > bloblen
2392 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002393 {
2394 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002395 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002396 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002397 return NULL;
2398 }
2399 if (lp->ll_range && !lp->ll_empty2)
2400 {
2401 lp->ll_n2 = (long)tv_get_number(&var2);
2402 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002403 if (lp->ll_n2 < 0
2404 || lp->ll_n2 >= bloblen
2405 || lp->ll_n2 < lp->ll_n1)
2406 {
2407 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002409 return NULL;
2410 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002411 }
2412 lp->ll_blob = lp->ll_tv->vval.v_blob;
2413 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002414 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 else
2417 {
2418 /*
2419 * Get the number and item for the only or first index of the List.
2420 */
2421 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002424 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002425 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002426 clear_tv(&var1);
2427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_list = lp->ll_tv->vval.v_list;
2430 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2431 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002433 if (lp->ll_n1 < 0)
2434 {
2435 lp->ll_n1 = 0;
2436 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2437 }
2438 }
2439 if (lp->ll_li == NULL)
2440 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002441 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002442 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002443 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 }
2446
2447 /*
2448 * May need to find the item or absolute index for the second
2449 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 * When no index given: "lp->ll_empty2" is TRUE.
2451 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002455 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002456 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002462 {
2463 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002464 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 }
2469
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2471 if (lp->ll_n1 < 0)
2472 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2473 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002474 {
2475 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002478 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 }
2480
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002485 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 return p;
2487}
2488
2489/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002490 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002493clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494{
2495 vim_free(lp->ll_exp_name);
2496 vim_free(lp->ll_newkey);
2497}
2498
2499/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002500 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002502 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2503 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 */
2505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002506set_var_lval(
2507 lval_T *lp,
2508 char_u *endp,
2509 typval_T *rettv,
2510 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002511 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002512 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513{
2514 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 listitem_T *ri;
2516 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517
2518 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002520 cc = *endp;
2521 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002522 if (lp->ll_blob != NULL)
2523 {
2524 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002525
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002526 if (op != NULL && *op != '=')
2527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002528 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002529 return;
2530 }
2531
2532 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2533 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002534 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002535
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002536 if (lp->ll_empty2)
2537 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2538
2539 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002541 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002542 return;
2543 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002544 if (lp->ll_empty2)
2545 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002546
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002547 ir = 0;
2548 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2549 blob_set(lp->ll_blob, il,
2550 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002551 }
2552 else
2553 {
2554 val = (int)tv_get_number_chk(rettv, &error);
2555 if (!error)
2556 {
2557 garray_T *gap = &lp->ll_blob->bv_ga;
2558
2559 // Allow for appending a byte. Setting a byte beyond
2560 // the end is an error otherwise.
2561 if (lp->ll_n1 < gap->ga_len
2562 || (lp->ll_n1 == gap->ga_len
2563 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2564 {
2565 blob_set(lp->ll_blob, lp->ll_n1, val);
2566 if (lp->ll_n1 == gap->ga_len)
2567 ++gap->ga_len;
2568 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002569 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002570 }
2571 }
2572 }
2573 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002575 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002576
Bram Moolenaar9937a052019-06-15 15:45:06 +02002577 if (is_const)
2578 {
2579 emsg(_(e_cannot_mod));
2580 *endp = cc;
2581 return;
2582 }
2583
Bram Moolenaarff697e62019-02-12 22:28:33 +01002584 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002585 di = NULL;
2586 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2587 &tv, &di, TRUE, FALSE) == OK)
2588 {
2589 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002590 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2591 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002592 && tv_op(&tv, rettv, op) == OK)
2593 set_var(lp->ll_name, &tv, FALSE);
2594 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002597 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002598 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002599 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002601 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002602 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002603 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002604 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 else if (lp->ll_range)
2606 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002607 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002608 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002609
Bram Moolenaar9937a052019-06-15 15:45:06 +02002610 if (is_const)
2611 {
2612 emsg(_("E996: Cannot lock a range"));
2613 return;
2614 }
2615
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002616 /*
2617 * Check whether any of the list items is locked
2618 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002619 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002620 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002621 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002622 return;
2623 ri = ri->li_next;
2624 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2625 break;
2626 ll_li = ll_li->li_next;
2627 ++ll_n1;
2628 }
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 /*
2631 * Assign the List values to the list items.
2632 */
2633 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635 if (op != NULL && *op != '=')
2636 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2637 else
2638 {
2639 clear_tv(&lp->ll_li->li_tv);
2640 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 ri = ri->li_next;
2643 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2644 break;
2645 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002648 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 ri = NULL;
2651 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_li = lp->ll_li->li_next;
2655 ++lp->ll_n1;
2656 }
2657 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002658 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 else if (lp->ll_empty2
2660 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002662 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 }
2664 else
2665 {
2666 /*
2667 * Assign to a List or Dictionary item.
2668 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002669 if (is_const)
2670 {
2671 emsg(_("E996: Cannot lock a list or dict"));
2672 return;
2673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_newkey != NULL)
2675 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002676 if (op != NULL && *op != '=')
2677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 return;
2680 }
2681
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002683 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (di == NULL)
2685 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002686 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2687 {
2688 vim_free(di);
2689 return;
2690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 else if (op != NULL && *op != '=')
2694 {
2695 tv_op(lp->ll_tv, rettv, op);
2696 return;
2697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 /*
2702 * Assign the value to the variable or list item.
2703 */
2704 if (copy)
2705 copy_tv(rettv, lp->ll_tv);
2706 else
2707 {
2708 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002709 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 }
2712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713}
2714
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002715/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002716 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2717 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 * Returns OK or FAIL.
2719 */
2720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002721tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002723 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 char_u numbuf[NUMBUFLEN];
2725 char_u *s;
2726
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002727 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2728 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2729 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 {
2731 switch (tv1->v_type)
2732 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002733 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 case VAR_DICT:
2735 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002736 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002737 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002738 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002739 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002740 break;
2741
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002742 case VAR_BLOB:
2743 if (*op != '+' || tv2->v_type != VAR_BLOB)
2744 break;
2745 // BLOB += BLOB
2746 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2747 {
2748 blob_T *b1 = tv1->vval.v_blob;
2749 blob_T *b2 = tv2->vval.v_blob;
2750 int i, len = blob_len(b2);
2751 for (i = 0; i < len; i++)
2752 ga_append(&b1->bv_ga, blob_get(b2, i));
2753 }
2754 return OK;
2755
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 case VAR_LIST:
2757 if (*op != '+' || tv2->v_type != VAR_LIST)
2758 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002759 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2761 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2762 return OK;
2763
2764 case VAR_NUMBER:
2765 case VAR_STRING:
2766 if (tv2->v_type == VAR_LIST)
2767 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002768 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002770 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002771 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002772#ifdef FEAT_FLOAT
2773 if (tv2->v_type == VAR_FLOAT)
2774 {
2775 float_T f = n;
2776
Bram Moolenaarff697e62019-02-12 22:28:33 +01002777 if (*op == '%')
2778 break;
2779 switch (*op)
2780 {
2781 case '+': f += tv2->vval.v_float; break;
2782 case '-': f -= tv2->vval.v_float; break;
2783 case '*': f *= tv2->vval.v_float; break;
2784 case '/': f /= tv2->vval.v_float; break;
2785 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002786 clear_tv(tv1);
2787 tv1->v_type = VAR_FLOAT;
2788 tv1->vval.v_float = f;
2789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002790 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002791#endif
2792 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002793 switch (*op)
2794 {
2795 case '+': n += tv_get_number(tv2); break;
2796 case '-': n -= tv_get_number(tv2); break;
2797 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002798 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2799 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002800 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002801 clear_tv(tv1);
2802 tv1->v_type = VAR_NUMBER;
2803 tv1->vval.v_number = n;
2804 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002805 }
2806 else
2807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808 if (tv2->v_type == VAR_FLOAT)
2809 break;
2810
Bram Moolenaarff697e62019-02-12 22:28:33 +01002811 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002812 s = tv_get_string(tv1);
2813 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 clear_tv(tv1);
2815 tv1->v_type = VAR_STRING;
2816 tv1->vval.v_string = s;
2817 }
2818 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002819
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002820 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002821#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002822 {
2823 float_T f;
2824
Bram Moolenaarff697e62019-02-12 22:28:33 +01002825 if (*op == '%' || *op == '.'
2826 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002827 && tv2->v_type != VAR_NUMBER
2828 && tv2->v_type != VAR_STRING))
2829 break;
2830 if (tv2->v_type == VAR_FLOAT)
2831 f = tv2->vval.v_float;
2832 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002833 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002834 switch (*op)
2835 {
2836 case '+': tv1->vval.v_float += f; break;
2837 case '-': tv1->vval.v_float -= f; break;
2838 case '*': tv1->vval.v_float *= f; break;
2839 case '/': tv1->vval.v_float /= f; break;
2840 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002842#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002843 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 }
2845 }
2846
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002847 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002848 return FAIL;
2849}
2850
2851/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 * Evaluate the expression used in a ":for var in expr" command.
2853 * "arg" points to "var".
2854 * Set "*errp" to TRUE for an error, FALSE otherwise;
2855 * Return a pointer that holds the info. Null when there is an error.
2856 */
2857 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002858eval_for_line(
2859 char_u *arg,
2860 int *errp,
2861 char_u **nextcmdp,
2862 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863{
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 typval_T tv;
2867 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002868
2869 *errp = TRUE; /* default: there is an error */
2870
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002871 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 if (fi == NULL)
2873 return NULL;
2874
2875 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2876 if (expr == NULL)
2877 return fi;
2878
2879 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002880 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002882 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883 return fi;
2884 }
2885
2886 if (skip)
2887 ++emsg_skip;
2888 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2889 {
2890 *errp = FALSE;
2891 if (!skip)
2892 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002893 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002894 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002895 l = tv.vval.v_list;
2896 if (l == NULL)
2897 {
2898 // a null list is like an empty list: do nothing
2899 clear_tv(&tv);
2900 }
2901 else
2902 {
2903 // No need to increment the refcount, it's already set for
2904 // the list being used in "tv".
2905 fi->fi_list = l;
2906 list_add_watch(l, &fi->fi_lw);
2907 fi->fi_lw.lw_item = l->lv_first;
2908 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002909 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002910 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002911 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002912 fi->fi_bi = 0;
2913 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002914 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002915 typval_T btv;
2916
2917 // Make a copy, so that the iteration still works when the
2918 // blob is changed.
2919 blob_copy(&tv, &btv);
2920 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002921 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002922 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002923 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 else
2925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002926 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002927 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 }
2929 }
2930 }
2931 if (skip)
2932 --emsg_skip;
2933
2934 return fi;
2935}
2936
2937/*
2938 * Use the first item in a ":for" list. Advance to the next.
2939 * Assign the values to the variable (list). "arg" points to the first one.
2940 * Return TRUE when a valid item was found, FALSE when at end of list or
2941 * something wrong.
2942 */
2943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002946 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002949
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002950 if (fi->fi_blob != NULL)
2951 {
2952 typval_T tv;
2953
2954 if (fi->fi_bi >= blob_len(fi->fi_blob))
2955 return FALSE;
2956 tv.v_type = VAR_NUMBER;
2957 tv.v_lock = VAR_FIXED;
2958 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2959 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002960 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2961 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002962 }
2963
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002964 item = fi->fi_lw.lw_item;
2965 if (item == NULL)
2966 result = FALSE;
2967 else
2968 {
2969 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002970 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2971 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 }
2973 return result;
2974}
2975
2976/*
2977 * Free the structure used to store info used by ":for".
2978 */
2979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002980free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981{
Bram Moolenaar33570922005-01-25 22:26:29 +00002982 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002983
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002984 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002985 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002987 list_unref(fi->fi_list);
2988 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002989 if (fi != NULL && fi->fi_blob != NULL)
2990 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 vim_free(fi);
2992}
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2995
2996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002997set_context_for_expression(
2998 expand_T *xp,
2999 char_u *arg,
3000 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 int got_eq = FALSE;
3003 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 if (cmdidx == CMD_let)
3007 {
3008 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003009 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 {
3011 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003012 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 {
3014 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003015 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003016 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003017 break;
3018 }
3019 return;
3020 }
3021 }
3022 else
3023 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3024 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 while ((xp->xp_pattern = vim_strpbrk(arg,
3026 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3027 {
3028 c = *xp->xp_pattern;
3029 if (c == '&')
3030 {
3031 c = xp->xp_pattern[1];
3032 if (c == '&')
3033 {
3034 ++xp->xp_pattern;
3035 xp->xp_context = cmdidx != CMD_let || got_eq
3036 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3037 }
3038 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003041 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3042 xp->xp_pattern += 2;
3043
3044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046 else if (c == '$')
3047 {
3048 /* environment variable */
3049 xp->xp_context = EXPAND_ENV_VARS;
3050 }
3051 else if (c == '=')
3052 {
3053 got_eq = TRUE;
3054 xp->xp_context = EXPAND_EXPRESSION;
3055 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003056 else if (c == '#'
3057 && xp->xp_context == EXPAND_EXPRESSION)
3058 {
3059 /* Autoload function/variable contains '#'. */
3060 break;
3061 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003062 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 && xp->xp_context == EXPAND_FUNCTIONS
3064 && vim_strchr(xp->xp_pattern, '(') == NULL)
3065 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003066 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 break;
3068 }
3069 else if (cmdidx != CMD_let || got_eq)
3070 {
3071 if (c == '"') /* string */
3072 {
3073 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3074 if (c == '\\' && xp->xp_pattern[1] != NUL)
3075 ++xp->xp_pattern;
3076 xp->xp_context = EXPAND_NOTHING;
3077 }
3078 else if (c == '\'') /* literal string */
3079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003080 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3082 /* skip */ ;
3083 xp->xp_context = EXPAND_NOTHING;
3084 }
3085 else if (c == '|')
3086 {
3087 if (xp->xp_pattern[1] == '|')
3088 {
3089 ++xp->xp_pattern;
3090 xp->xp_context = EXPAND_EXPRESSION;
3091 }
3092 else
3093 xp->xp_context = EXPAND_COMMANDS;
3094 }
3095 else
3096 xp->xp_context = EXPAND_EXPRESSION;
3097 }
3098 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099 /* Doesn't look like something valid, expand as an expression
3100 * anyway. */
3101 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 arg = xp->xp_pattern;
3103 if (*arg != NUL)
3104 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3105 /* skip */ ;
3106 }
3107 xp->xp_pattern = arg;
3108}
3109
3110#endif /* FEAT_CMDL_COMPL */
3111
3112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * ":unlet[!] var1 ... " command.
3114 */
3115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003118 ex_unletlock(eap, eap->arg, 0);
3119}
3120
3121/*
3122 * ":lockvar" and ":unlockvar" commands
3123 */
3124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003125ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003126{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003128 int deep = 2;
3129
3130 if (eap->forceit)
3131 deep = -1;
3132 else if (vim_isdigit(*arg))
3133 {
3134 deep = getdigits(&arg);
3135 arg = skipwhite(arg);
3136 }
3137
3138 ex_unletlock(eap, arg, deep);
3139}
3140
3141/*
3142 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3143 */
3144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003145ex_unletlock(
3146 exarg_T *eap,
3147 char_u *argstart,
3148 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003149{
3150 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003153 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 do
3156 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003157 if (*arg == '$')
3158 {
3159 char_u *name = ++arg;
3160
3161 if (get_env_len(&arg) == 0)
3162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003163 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003164 return;
3165 }
3166 vim_unsetenv(name);
3167 arg = skipwhite(arg);
3168 continue;
3169 }
3170
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003171 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003172 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003173 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 if (lv.ll_name == NULL)
3175 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003176 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003179 if (name_end != NULL)
3180 {
3181 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003182 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003183 }
3184 if (!(eap->skip || error))
3185 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 break;
3187 }
3188
3189 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003190 {
3191 if (eap->cmdidx == CMD_unlet)
3192 {
3193 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3194 error = TRUE;
3195 }
3196 else
3197 {
3198 if (do_lock_var(&lv, name_end, deep,
3199 eap->cmdidx == CMD_lockvar) == FAIL)
3200 error = TRUE;
3201 }
3202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 if (!eap->skip)
3205 clear_lval(&lv);
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 arg = skipwhite(name_end);
3208 } while (!ends_excmd(*arg));
3209
3210 eap->nextcmd = check_nextcmd(arg);
3211}
3212
Bram Moolenaar8c711452005-01-14 21:53:12 +00003213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214do_unlet_var(
3215 lval_T *lp,
3216 char_u *name_end,
3217 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 int ret = OK;
3220 int cc;
3221
3222 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003223 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 cc = *name_end;
3225 *name_end = NUL;
3226
3227 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003228 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003230 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003231 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003232 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003233 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003234 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003235 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003236 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 else if (lp->ll_range)
3238 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003240 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003241 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003242
3243 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3244 {
3245 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003246 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003247 return FAIL;
3248 ll_li = li;
3249 ++ll_n1;
3250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251
3252 /* Delete a range of List items. */
3253 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3254 {
3255 li = lp->ll_li->li_next;
3256 listitem_remove(lp->ll_list, lp->ll_li);
3257 lp->ll_li = li;
3258 ++lp->ll_n1;
3259 }
3260 }
3261 else
3262 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003263 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 /* unlet a List item. */
3265 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003266 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003267 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003268 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003269 }
3270
3271 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003272}
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274/*
3275 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 hashtab_T *ht;
3282 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003283 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003284 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003285 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 ht = find_var_ht(name, &varname);
3288 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003290 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003291 if (d == NULL)
3292 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 if (ht == &globvarht)
3294 d = &globvardict;
3295 else if (ht == &compat_hashtab)
3296 d = &vimvardict;
3297 else
3298 {
3299 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3300 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3301 }
3302 if (d == NULL)
3303 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003304 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 return FAIL;
3306 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003307 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003309 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003310 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003311 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003312 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003313 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003314 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003315 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003316 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003317 return FAIL;
3318
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003319 delete_var(ht, hi);
3320 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003323 if (forceit)
3324 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003325 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return FAIL;
3327}
3328
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003329/*
3330 * Lock or unlock variable indicated by "lp".
3331 * "deep" is the levels to go (-1 for unlimited);
3332 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3333 */
3334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335do_lock_var(
3336 lval_T *lp,
3337 char_u *name_end,
3338 int deep,
3339 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003340{
3341 int ret = OK;
3342 int cc;
3343 dictitem_T *di;
3344
3345 if (deep == 0) /* nothing to do */
3346 return OK;
3347
3348 if (lp->ll_tv == NULL)
3349 {
3350 cc = *name_end;
3351 *name_end = NUL;
3352
3353 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003354 di = find_var(lp->ll_name, NULL, TRUE);
3355 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003356 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003357 else if ((di->di_flags & DI_FLAGS_FIX)
3358 && di->di_tv.v_type != VAR_DICT
3359 && di->di_tv.v_type != VAR_LIST)
3360 /* For historic reasons this error is not given for a list or dict.
3361 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003362 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003363 else
3364 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003365 if (lock)
3366 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003368 di->di_flags &= ~DI_FLAGS_LOCK;
3369 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 }
3371 *name_end = cc;
3372 }
3373 else if (lp->ll_range)
3374 {
3375 listitem_T *li = lp->ll_li;
3376
3377 /* (un)lock a range of List items. */
3378 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3379 {
3380 item_lock(&li->li_tv, deep, lock);
3381 li = li->li_next;
3382 ++lp->ll_n1;
3383 }
3384 }
3385 else if (lp->ll_list != NULL)
3386 /* (un)lock a List item. */
3387 item_lock(&lp->ll_li->li_tv, deep, lock);
3388 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003389 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003390 item_lock(&lp->ll_di->di_tv, deep, lock);
3391
3392 return ret;
3393}
3394
3395/*
3396 * Lock or unlock an item. "deep" is nr of levels to go.
3397 */
3398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003400{
3401 static int recurse = 0;
3402 list_T *l;
3403 listitem_T *li;
3404 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003405 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003406 hashitem_T *hi;
3407 int todo;
3408
3409 if (recurse >= DICT_MAXNEST)
3410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003411 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003412 return;
3413 }
3414 if (deep == 0)
3415 return;
3416 ++recurse;
3417
3418 /* lock/unlock the item itself */
3419 if (lock)
3420 tv->v_lock |= VAR_LOCKED;
3421 else
3422 tv->v_lock &= ~VAR_LOCKED;
3423
3424 switch (tv->v_type)
3425 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003426 case VAR_UNKNOWN:
3427 case VAR_NUMBER:
3428 case VAR_STRING:
3429 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003430 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003431 case VAR_FLOAT:
3432 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003433 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003434 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003435 break;
3436
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003437 case VAR_BLOB:
3438 if ((b = tv->vval.v_blob) != NULL)
3439 {
3440 if (lock)
3441 b->bv_lock |= VAR_LOCKED;
3442 else
3443 b->bv_lock &= ~VAR_LOCKED;
3444 }
3445 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003446 case VAR_LIST:
3447 if ((l = tv->vval.v_list) != NULL)
3448 {
3449 if (lock)
3450 l->lv_lock |= VAR_LOCKED;
3451 else
3452 l->lv_lock &= ~VAR_LOCKED;
3453 if (deep < 0 || deep > 1)
3454 /* recursive: lock/unlock the items the List contains */
3455 for (li = l->lv_first; li != NULL; li = li->li_next)
3456 item_lock(&li->li_tv, deep - 1, lock);
3457 }
3458 break;
3459 case VAR_DICT:
3460 if ((d = tv->vval.v_dict) != NULL)
3461 {
3462 if (lock)
3463 d->dv_lock |= VAR_LOCKED;
3464 else
3465 d->dv_lock &= ~VAR_LOCKED;
3466 if (deep < 0 || deep > 1)
3467 {
3468 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003469 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003470 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3471 {
3472 if (!HASHITEM_EMPTY(hi))
3473 {
3474 --todo;
3475 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3476 }
3477 }
3478 }
3479 }
3480 }
3481 --recurse;
3482}
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3485/*
3486 * Delete all "menutrans_" variables.
3487 */
3488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003489del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
Bram Moolenaar33570922005-01-25 22:26:29 +00003491 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003492 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar33570922005-01-25 22:26:29 +00003494 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003496 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003497 {
3498 if (!HASHITEM_EMPTY(hi))
3499 {
3500 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3502 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003503 }
3504 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506}
3507#endif
3508
3509#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3510
3511/*
3512 * Local string buffer for the next two functions to store a variable name
3513 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3514 * get_user_var_name().
3515 */
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517static char_u *varnamebuf = NULL;
3518static int varnamebuflen = 0;
3519
3520/*
3521 * Function to concatenate a prefix and a variable name.
3522 */
3523 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003524cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 int len;
3527
3528 len = (int)STRLEN(name) + 3;
3529 if (len > varnamebuflen)
3530 {
3531 vim_free(varnamebuf);
3532 len += 10; /* some additional space */
3533 varnamebuf = alloc(len);
3534 if (varnamebuf == NULL)
3535 {
3536 varnamebuflen = 0;
3537 return NULL;
3538 }
3539 varnamebuflen = len;
3540 }
3541 *varnamebuf = prefix;
3542 varnamebuf[1] = ':';
3543 STRCPY(varnamebuf + 2, name);
3544 return varnamebuf;
3545}
3546
3547/*
3548 * Function given to ExpandGeneric() to obtain the list of user defined
3549 * (global/buffer/window/built-in) variable names.
3550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003552get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003554 static long_u gdone;
3555 static long_u bdone;
3556 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003557 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003558 static int vidx;
3559 static hashitem_T *hi;
3560 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003563 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003564 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003565 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003566 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003567
3568 /* Global variables */
3569 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003571 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003573 else
3574 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003575 while (HASHITEM_EMPTY(hi))
3576 ++hi;
3577 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3578 return cat_prefix_varname('g', hi->hi_key);
3579 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003581
3582 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003583 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003584 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003586 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003587 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003588 else
3589 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003590 while (HASHITEM_EMPTY(hi))
3591 ++hi;
3592 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003594
3595 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003596 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003599 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003601 else
3602 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003603 while (HASHITEM_EMPTY(hi))
3604 ++hi;
3605 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003607
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003608 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003609 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003610 if (tdone < ht->ht_used)
3611 {
3612 if (tdone++ == 0)
3613 hi = ht->ht_array;
3614 else
3615 ++hi;
3616 while (HASHITEM_EMPTY(hi))
3617 ++hi;
3618 return cat_prefix_varname('t', hi->hi_key);
3619 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003620
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 /* v: variables */
3622 if (vidx < VV_LEN)
3623 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaard23a8232018-02-10 18:45:26 +01003625 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 varnamebuflen = 0;
3627 return NULL;
3628}
3629
3630#endif /* FEAT_CMDL_COMPL */
3631
3632/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003633 * Return TRUE if "pat" matches "text".
3634 * Does not use 'cpo' and always uses 'magic'.
3635 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003636 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003637pattern_match(char_u *pat, char_u *text, int ic)
3638{
3639 int matches = FALSE;
3640 char_u *save_cpo;
3641 regmatch_T regmatch;
3642
3643 /* avoid 'l' flag in 'cpoptions' */
3644 save_cpo = p_cpo;
3645 p_cpo = (char_u *)"";
3646 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3647 if (regmatch.regprog != NULL)
3648 {
3649 regmatch.rm_ic = ic;
3650 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3651 vim_regfree(regmatch.regprog);
3652 }
3653 p_cpo = save_cpo;
3654 return matches;
3655}
3656
3657/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003658 * Handle a name followed by "(". Both for just "name(arg)" and for
3659 * "expr->name(arg)".
3660 * Returns OK or FAIL.
3661 */
3662 static int
3663eval_func(
3664 char_u **arg, // points to "(", will be advanced
3665 char_u *name,
3666 int name_len,
3667 typval_T *rettv,
3668 int evaluate,
3669 typval_T *basetv) // "expr" for "expr->name(arg)"
3670{
3671 char_u *s = name;
3672 int len = name_len;
3673 partial_T *partial;
3674 int ret = OK;
3675
3676 if (!evaluate)
3677 check_vars(s, len);
3678
3679 /* If "s" is the name of a variable of type VAR_FUNC
3680 * use its contents. */
3681 s = deref_func_name(s, &len, &partial, !evaluate);
3682
3683 /* Need to make a copy, in case evaluating the arguments makes
3684 * the name invalid. */
3685 s = vim_strsave(s);
3686 if (s == NULL)
3687 ret = FAIL;
3688 else
3689 {
3690 funcexe_T funcexe;
3691
3692 // Invoke the function.
3693 vim_memset(&funcexe, 0, sizeof(funcexe));
3694 funcexe.firstline = curwin->w_cursor.lnum;
3695 funcexe.lastline = curwin->w_cursor.lnum;
3696 funcexe.doesrange = &len;
3697 funcexe.evaluate = evaluate;
3698 funcexe.partial = partial;
3699 funcexe.basetv = basetv;
3700 ret = get_func_tv(s, len, rettv, arg, &funcexe);
3701 }
3702 vim_free(s);
3703
3704 /* If evaluate is FALSE rettv->v_type was not set in
3705 * get_func_tv, but it's needed in handle_subscript() to parse
3706 * what follows. So set it here. */
3707 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3708 {
3709 rettv->vval.v_string = NULL;
3710 rettv->v_type = VAR_FUNC;
3711 }
3712
3713 /* Stop the expression evaluation when immediately
3714 * aborting on error, or when an interrupt occurred or
3715 * an exception was thrown but not caught. */
3716 if (evaluate && aborting())
3717 {
3718 if (ret == OK)
3719 clear_tv(rettv);
3720 ret = FAIL;
3721 }
3722 return ret;
3723}
3724
3725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3729 */
3730
3731/*
3732 * Handle zero level expression.
3733 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003735 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Return OK or FAIL.
3737 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739eval0(
3740 char_u *arg,
3741 typval_T *rettv,
3742 char_u **nextcmd,
3743 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 int ret;
3746 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003747 int did_emsg_before = did_emsg;
3748 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (ret == FAIL || !ends_excmd(*p))
3753 {
3754 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 /*
3757 * Report the invalid expression unless the expression evaluation has
3758 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003759 * exception, or we already gave a more specific error.
3760 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003762 if (!aborting() && did_emsg == did_emsg_before
3763 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003764 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 ret = FAIL;
3766 }
3767 if (nextcmd != NULL)
3768 *nextcmd = check_nextcmd(p);
3769
3770 return ret;
3771}
3772
3773/*
3774 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003775 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 *
3777 * "arg" must point to the first non-white of the expression.
3778 * "arg" is advanced to the next non-white after the recognized expression.
3779 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003780 * Note: "rettv.v_lock" is not set.
3781 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * Return OK or FAIL.
3783 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003785eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 /*
3791 * Get the first variable.
3792 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 if ((*arg)[0] == '?')
3797 {
3798 result = FALSE;
3799 if (evaluate)
3800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 int error = FALSE;
3802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003803 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003806 if (error)
3807 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809
3810 /*
3811 * Get the second variable.
3812 */
3813 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return FAIL;
3816
3817 /*
3818 * Check for the ":".
3819 */
3820 if ((*arg)[0] != ':')
3821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003822 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826 }
3827
3828 /*
3829 * Get the third variable.
3830 */
3831 *arg = skipwhite(*arg + 1);
3832 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3833 {
3834 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837 }
3838 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
3842 return OK;
3843}
3844
3845/*
3846 * Handle first level expression:
3847 * expr2 || expr2 || expr2 logical OR
3848 *
3849 * "arg" must point to the first non-white of the expression.
3850 * "arg" is advanced to the next non-white after the recognized expression.
3851 *
3852 * Return OK or FAIL.
3853 */
3854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 long result;
3859 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 /*
3863 * Get the first variable.
3864 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return FAIL;
3867
3868 /*
3869 * Repeat until there is no following "||".
3870 */
3871 first = TRUE;
3872 result = FALSE;
3873 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3874 {
3875 if (evaluate && first)
3876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003877 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 if (error)
3881 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 first = FALSE;
3883 }
3884
3885 /*
3886 * Get the second variable.
3887 */
3888 *arg = skipwhite(*arg + 2);
3889 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3890 return FAIL;
3891
3892 /*
3893 * Compute the result.
3894 */
3895 if (evaluate && !result)
3896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (error)
3901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 if (evaluate)
3904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 rettv->v_type = VAR_NUMBER;
3906 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909
3910 return OK;
3911}
3912
3913/*
3914 * Handle second level expression:
3915 * expr3 && expr3 && expr3 logical AND
3916 *
3917 * "arg" must point to the first non-white of the expression.
3918 * "arg" is advanced to the next non-white after the recognized expression.
3919 *
3920 * Return OK or FAIL.
3921 */
3922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 long result;
3927 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 /*
3931 * Get the first variable.
3932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 return FAIL;
3935
3936 /*
3937 * Repeat until there is no following "&&".
3938 */
3939 first = TRUE;
3940 result = TRUE;
3941 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3942 {
3943 if (evaluate && first)
3944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003948 if (error)
3949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 first = FALSE;
3951 }
3952
3953 /*
3954 * Get the second variable.
3955 */
3956 *arg = skipwhite(*arg + 2);
3957 if (eval4(arg, &var2, evaluate && result) == FAIL)
3958 return FAIL;
3959
3960 /*
3961 * Compute the result.
3962 */
3963 if (evaluate && result)
3964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003965 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003968 if (error)
3969 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 if (evaluate)
3972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 rettv->v_type = VAR_NUMBER;
3974 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 }
3977
3978 return OK;
3979}
3980
3981/*
3982 * Handle third level expression:
3983 * var1 == var2
3984 * var1 =~ var2
3985 * var1 != var2
3986 * var1 !~ var2
3987 * var1 > var2
3988 * var1 >= var2
3989 * var1 < var2
3990 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003991 * var1 is var2
3992 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 *
3994 * "arg" must point to the first non-white of the expression.
3995 * "arg" is advanced to the next non-white after the recognized expression.
3996 *
3997 * Return OK or FAIL.
3998 */
3999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004000eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *p;
4004 int i;
4005 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004006 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 /*
4011 * Get the first variable.
4012 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return FAIL;
4015
4016 p = *arg;
4017 switch (p[0])
4018 {
4019 case '=': if (p[1] == '=')
4020 type = TYPE_EQUAL;
4021 else if (p[1] == '~')
4022 type = TYPE_MATCH;
4023 break;
4024 case '!': if (p[1] == '=')
4025 type = TYPE_NEQUAL;
4026 else if (p[1] == '~')
4027 type = TYPE_NOMATCH;
4028 break;
4029 case '>': if (p[1] != '=')
4030 {
4031 type = TYPE_GREATER;
4032 len = 1;
4033 }
4034 else
4035 type = TYPE_GEQUAL;
4036 break;
4037 case '<': if (p[1] != '=')
4038 {
4039 type = TYPE_SMALLER;
4040 len = 1;
4041 }
4042 else
4043 type = TYPE_SEQUAL;
4044 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004045 case 'i': if (p[1] == 's')
4046 {
4047 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4048 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004049 i = p[len];
4050 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 {
4052 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4053 type_is = TRUE;
4054 }
4055 }
4056 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058
4059 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 */
4062 if (type != TYPE_UNKNOWN)
4063 {
4064 /* extra question mark appended: ignore case */
4065 if (p[len] == '?')
4066 {
4067 ic = TRUE;
4068 ++len;
4069 }
4070 /* extra '#' appended: match case */
4071 else if (p[len] == '#')
4072 {
4073 ic = FALSE;
4074 ++len;
4075 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 else
4078 ic = p_ic;
4079
4080 /*
4081 * Get the second variable.
4082 */
4083 *arg = skipwhite(p + len);
4084 if (eval5(arg, &var2, evaluate) == FAIL)
4085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004089 if (evaluate)
4090 {
4091 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4092
4093 clear_tv(&var2);
4094 return ret;
4095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 return OK;
4099}
4100
4101/*
4102 * Handle fourth level expression:
4103 * + number addition
4104 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004105 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004106 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 *
4108 * "arg" must point to the first non-white of the expression.
4109 * "arg" is advanced to the next non-white after the recognized expression.
4110 *
4111 * Return OK or FAIL.
4112 */
4113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
Bram Moolenaar33570922005-01-25 22:26:29 +00004116 typval_T var2;
4117 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004119 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004120#ifdef FEAT_FLOAT
4121 float_T f1 = 0, f2 = 0;
4122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 char_u *s1, *s2;
4124 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4125 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004126 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
4128 /*
4129 * Get the first variable.
4130 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004131 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return FAIL;
4133
4134 /*
4135 * Repeat computing, until no '+', '-' or '.' is following.
4136 */
4137 for (;;)
4138 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004139 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004141 concat = op == '.'
4142 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4143 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 break;
4145
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004146 if ((op != '+' || (rettv->v_type != VAR_LIST
4147 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004148#ifdef FEAT_FLOAT
4149 && (op == '.' || rettv->v_type != VAR_FLOAT)
4150#endif
4151 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 {
4153 /* For "list + ...", an illegal use of the first operand as
4154 * a number cannot be determined before evaluating the 2nd
4155 * operand: if this is also a list, all is ok.
4156 * For "something . ...", "something - ..." or "non-list + ...",
4157 * we know that the first operand needs to be a string or number
4158 * without evaluating the 2nd operand. So check before to avoid
4159 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004160 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 {
4162 clear_tv(rettv);
4163 return FAIL;
4164 }
4165 }
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /*
4168 * Get the second variable.
4169 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004170 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4171 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004173 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 return FAIL;
4177 }
4178
4179 if (evaluate)
4180 {
4181 /*
4182 * Compute the result.
4183 */
4184 if (op == '.')
4185 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004186 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4187 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (s2 == NULL) /* type error ? */
4189 {
4190 clear_tv(rettv);
4191 clear_tv(&var2);
4192 return FAIL;
4193 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004194 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
4196 rettv->v_type = VAR_STRING;
4197 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004199 else if (op == '+' && rettv->v_type == VAR_BLOB
4200 && var2.v_type == VAR_BLOB)
4201 {
4202 blob_T *b1 = rettv->vval.v_blob;
4203 blob_T *b2 = var2.vval.v_blob;
4204 blob_T *b = blob_alloc();
4205 int i;
4206
4207 if (b != NULL)
4208 {
4209 for (i = 0; i < blob_len(b1); i++)
4210 ga_append(&b->bv_ga, blob_get(b1, i));
4211 for (i = 0; i < blob_len(b2); i++)
4212 ga_append(&b->bv_ga, blob_get(b2, i));
4213
4214 clear_tv(rettv);
4215 rettv_blob_set(rettv, b);
4216 }
4217 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004218 else if (op == '+' && rettv->v_type == VAR_LIST
4219 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 {
4221 /* concatenate Lists */
4222 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4223 &var3) == FAIL)
4224 {
4225 clear_tv(rettv);
4226 clear_tv(&var2);
4227 return FAIL;
4228 }
4229 clear_tv(rettv);
4230 *rettv = var3;
4231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 else
4233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
4235
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004236#ifdef FEAT_FLOAT
4237 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004239 f1 = rettv->vval.v_float;
4240 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004242 else
4243#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004245 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004246 if (error)
4247 {
4248 /* This can only happen for "list + non-list". For
4249 * "non-list + ..." or "something - ...", we returned
4250 * before evaluating the 2nd operand. */
4251 clear_tv(rettv);
4252 return FAIL;
4253 }
4254#ifdef FEAT_FLOAT
4255 if (var2.v_type == VAR_FLOAT)
4256 f1 = n1;
4257#endif
4258 }
4259#ifdef FEAT_FLOAT
4260 if (var2.v_type == VAR_FLOAT)
4261 {
4262 f2 = var2.vval.v_float;
4263 n2 = 0;
4264 }
4265 else
4266#endif
4267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004268 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004269 if (error)
4270 {
4271 clear_tv(rettv);
4272 clear_tv(&var2);
4273 return FAIL;
4274 }
4275#ifdef FEAT_FLOAT
4276 if (rettv->v_type == VAR_FLOAT)
4277 f2 = n2;
4278#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004281
4282#ifdef FEAT_FLOAT
4283 /* If there is a float on either side the result is a float. */
4284 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4285 {
4286 if (op == '+')
4287 f1 = f1 + f2;
4288 else
4289 f1 = f1 - f2;
4290 rettv->v_type = VAR_FLOAT;
4291 rettv->vval.v_float = f1;
4292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004294#endif
4295 {
4296 if (op == '+')
4297 n1 = n1 + n2;
4298 else
4299 n1 = n1 - n2;
4300 rettv->v_type = VAR_NUMBER;
4301 rettv->vval.v_number = n1;
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 }
4307 return OK;
4308}
4309
4310/*
4311 * Handle fifth level expression:
4312 * * number multiplication
4313 * / number division
4314 * % number modulo
4315 *
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4318 *
4319 * Return OK or FAIL.
4320 */
4321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004322eval6(
4323 char_u **arg,
4324 typval_T *rettv,
4325 int evaluate,
4326 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
Bram Moolenaar33570922005-01-25 22:26:29 +00004328 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004330 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331#ifdef FEAT_FLOAT
4332 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004333 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 /*
4338 * Get the first variable.
4339 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004340 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 return FAIL;
4342
4343 /*
4344 * Repeat computing, until no '*', '/' or '%' is following.
4345 */
4346 for (;;)
4347 {
4348 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004349 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
4351
4352 if (evaluate)
4353 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004354#ifdef FEAT_FLOAT
4355 if (rettv->v_type == VAR_FLOAT)
4356 {
4357 f1 = rettv->vval.v_float;
4358 use_float = TRUE;
4359 n1 = 0;
4360 }
4361 else
4362#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004363 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 if (error)
4366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 else
4369 n1 = 0;
4370
4371 /*
4372 * Get the second variable.
4373 */
4374 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004375 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 return FAIL;
4377
4378 if (evaluate)
4379 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004380#ifdef FEAT_FLOAT
4381 if (var2.v_type == VAR_FLOAT)
4382 {
4383 if (!use_float)
4384 {
4385 f1 = n1;
4386 use_float = TRUE;
4387 }
4388 f2 = var2.vval.v_float;
4389 n2 = 0;
4390 }
4391 else
4392#endif
4393 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004394 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004395 clear_tv(&var2);
4396 if (error)
4397 return FAIL;
4398#ifdef FEAT_FLOAT
4399 if (use_float)
4400 f2 = n2;
4401#endif
4402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 /*
4405 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004406 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408#ifdef FEAT_FLOAT
4409 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004411 if (op == '*')
4412 f1 = f1 * f2;
4413 else if (op == '/')
4414 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004415# ifdef VMS
4416 /* VMS crashes on divide by zero, work around it */
4417 if (f2 == 0.0)
4418 {
4419 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004420 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004421 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004422 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004423 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004424 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004425 }
4426 else
4427 f1 = f1 / f2;
4428# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429 /* We rely on the floating point library to handle divide
4430 * by zero to result in "inf" and not a crash. */
4431 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004432# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004436 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 return FAIL;
4438 }
4439 rettv->v_type = VAR_FLOAT;
4440 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 if (op == '*')
4446 n1 = n1 * n2;
4447 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004448 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004450 n1 = num_modulus(n1, n2);
4451
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 rettv->v_type = VAR_NUMBER;
4453 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 }
4457
4458 return OK;
4459}
4460
4461/*
4462 * Handle sixth level expression:
4463 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004464 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004465 * "string" string constant
4466 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 * &option-name option value
4468 * @r register contents
4469 * identifier variable value
4470 * function() function call
4471 * $VAR environment variable
4472 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004473 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004474 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004475 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 *
4477 * Also handle:
4478 * ! in front logical NOT
4479 * - in front unary minus
4480 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 * trailing [] subscript in String or List
4482 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004483 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 *
4485 * "arg" must point to the first non-white of the expression.
4486 * "arg" is advanced to the next non-white after the recognized expression.
4487 *
4488 * Return OK or FAIL.
4489 */
4490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004491eval7(
4492 char_u **arg,
4493 typval_T *rettv,
4494 int evaluate,
4495 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004497 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 int len;
4499 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 char_u *start_leader, *end_leader;
4501 int ret = OK;
4502 char_u *alias;
4503
4504 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004511 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 */
4513 start_leader = *arg;
4514 while (**arg == '!' || **arg == '-' || **arg == '+')
4515 *arg = skipwhite(*arg + 1);
4516 end_leader = *arg;
4517
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004518 if (**arg == '.' && (!isdigit(*(*arg + 1))
4519#ifdef FEAT_FLOAT
4520 || current_sctx.sc_version < 2
4521#endif
4522 ))
4523 {
4524 semsg(_(e_invexpr2), *arg);
4525 ++*arg;
4526 return FAIL;
4527 }
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 switch (**arg)
4530 {
4531 /*
4532 * Number constant.
4533 */
4534 case '0':
4535 case '1':
4536 case '2':
4537 case '3':
4538 case '4':
4539 case '5':
4540 case '6':
4541 case '7':
4542 case '8':
4543 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004544 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004545 {
4546#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004547 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004548 int get_float = FALSE;
4549
4550 /* We accept a float when the format matches
4551 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004552 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004553 * With script version 2 and later the leading digit can be
4554 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004555 * Don't look for a float after the "." operator, so that
4556 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004557 if (**arg == '.')
4558 p = *arg;
4559 else
4560 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004561 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004563 get_float = TRUE;
4564 p = skipdigits(p + 2);
4565 if (*p == 'e' || *p == 'E')
4566 {
4567 ++p;
4568 if (*p == '-' || *p == '+')
4569 ++p;
4570 if (!vim_isdigit(*p))
4571 get_float = FALSE;
4572 else
4573 p = skipdigits(p + 1);
4574 }
4575 if (ASCII_ISALPHA(*p) || *p == '.')
4576 get_float = FALSE;
4577 }
4578 if (get_float)
4579 {
4580 float_T f;
4581
4582 *arg += string2float(*arg, &f);
4583 if (evaluate)
4584 {
4585 rettv->v_type = VAR_FLOAT;
4586 rettv->vval.v_float = f;
4587 }
4588 }
4589 else
4590#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004591 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004592 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004593 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004594 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004595
4596 // Blob constant: 0z0123456789abcdef
4597 if (evaluate)
4598 blob = blob_alloc();
4599 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4600 {
4601 if (!vim_isxdigit(bp[1]))
4602 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004603 if (blob != NULL)
4604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004605 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004606 ga_clear(&blob->bv_ga);
4607 VIM_CLEAR(blob);
4608 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004609 ret = FAIL;
4610 break;
4611 }
4612 if (blob != NULL)
4613 ga_append(&blob->bv_ga,
4614 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004615 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4616 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004617 }
4618 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004619 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004620 *arg = bp;
4621 }
4622 else
4623 {
4624 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004625 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4626 if (len == 0)
4627 {
4628 semsg(_(e_invexpr2), *arg);
4629 ret = FAIL;
4630 break;
4631 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 *arg += len;
4633 if (evaluate)
4634 {
4635 rettv->v_type = VAR_NUMBER;
4636 rettv->vval.v_number = n;
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /*
4643 * String constant: "string".
4644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 break;
4647
4648 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 break;
4653
4654 /*
4655 * List: [expr, expr]
4656 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 break;
4659
4660 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004661 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004662 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004663 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004664 {
4665 ++*arg;
4666 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4667 }
4668 else
4669 ret = NOTDONE;
4670 break;
4671
4672 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004673 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004674 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004676 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4677 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004678 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 break;
4680
4681 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004682 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 break;
4686
4687 /*
4688 * Environment variable: $VAR.
4689 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 break;
4692
4693 /*
4694 * Register contents: @r.
4695 */
4696 case '@': ++*arg;
4697 if (evaluate)
4698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004700 rettv->vval.v_string = get_reg_contents(**arg,
4701 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 if (**arg != NUL)
4704 ++*arg;
4705 break;
4706
4707 /*
4708 * nested expression: (expression).
4709 */
4710 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004711 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (**arg == ')')
4713 ++*arg;
4714 else if (ret == OK)
4715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004717 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 ret = FAIL;
4719 }
4720 break;
4721
Bram Moolenaar8c711452005-01-14 21:53:12 +00004722 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 break;
4724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725
4726 if (ret == NOTDONE)
4727 {
4728 /*
4729 * Must be a variable or function name.
4730 * Can also be a curly-braces kind of name: {expr}.
4731 */
4732 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 if (alias != NULL)
4735 s = alias;
4736
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004737 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 ret = FAIL;
4739 else
4740 {
4741 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004742 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004744 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004745 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004746 {
4747 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004748 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004751 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 *arg = skipwhite(*arg);
4755
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004756 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004757 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 if (ret == OK)
4759 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 /*
4762 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4763 */
4764 if (ret == OK && evaluate && end_leader > start_leader)
4765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004766 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004767 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768#ifdef FEAT_FLOAT
4769 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771 if (rettv->v_type == VAR_FLOAT)
4772 f = rettv->vval.v_float;
4773 else
4774#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004775 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 clear_tv(rettv);
4779 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 else
4782 {
4783 while (end_leader > start_leader)
4784 {
4785 --end_leader;
4786 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 {
4788#ifdef FEAT_FLOAT
4789 if (rettv->v_type == VAR_FLOAT)
4790 f = !f;
4791 else
4792#endif
4793 val = !val;
4794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 {
4797#ifdef FEAT_FLOAT
4798 if (rettv->v_type == VAR_FLOAT)
4799 f = -f;
4800 else
4801#endif
4802 val = -val;
4803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (rettv->v_type == VAR_FLOAT)
4807 {
4808 clear_tv(rettv);
4809 rettv->vval.v_float = f;
4810 }
4811 else
4812#endif
4813 {
4814 clear_tv(rettv);
4815 rettv->v_type = VAR_NUMBER;
4816 rettv->vval.v_number = val;
4817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820
4821 return ret;
4822}
4823
4824/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004825 * Evaluate "->method()".
4826 * "*arg" points to the '-'.
4827 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4828 */
4829 static int
4830eval_method(
4831 char_u **arg,
4832 typval_T *rettv,
4833 int evaluate,
4834 int verbose) /* give error messages */
4835{
4836 char_u *name;
4837 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004838 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004839 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004840 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004841
4842 // Skip over the ->.
4843 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004844 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004845
Bram Moolenaarac92e252019-08-03 21:58:38 +02004846 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004847 len = get_name_len(arg, &alias, evaluate, TRUE);
4848 if (alias != NULL)
4849 name = alias;
4850
4851 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004852 {
4853 if (verbose)
4854 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004855 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004856 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004857 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004858 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004859 if (**arg != '(')
4860 {
4861 if (verbose)
4862 semsg(_(e_missingparen), name);
4863 ret = FAIL;
4864 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004865 else if (VIM_ISWHITE((*arg)[-1]))
4866 {
4867 if (verbose)
4868 semsg(_("E274: No white space allowed before parenthesis"));
4869 ret = FAIL;
4870 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004871 else
4872 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004873 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004874
4875 /* Clear the funcref afterwards, so that deleting it while
4876 * evaluating the arguments is possible (see test55). */
4877 if (evaluate)
4878 clear_tv(&base);
4879
Bram Moolenaarac92e252019-08-03 21:58:38 +02004880 return ret;
4881}
4882
4883/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004884 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4885 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4887 */
4888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004889eval_index(
4890 char_u **arg,
4891 typval_T *rettv,
4892 int evaluate,
4893 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894{
4895 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004896 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004897 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004899 long len = -1;
4900 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004902 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004903
Bram Moolenaara03f2332016-02-06 18:09:59 +01004904 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004905 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004906 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004907 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004908 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004909 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004910 return FAIL;
4911 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004912#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004913 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004914 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004915 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004916#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004917 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004918 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004919 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004920 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004921 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004922 return FAIL;
4923 case VAR_UNKNOWN:
4924 if (evaluate)
4925 return FAIL;
4926 /* FALLTHROUGH */
4927
4928 case VAR_STRING:
4929 case VAR_NUMBER:
4930 case VAR_LIST:
4931 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004932 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004933 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004934 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004936 init_tv(&var1);
4937 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 /*
4941 * dict.name
4942 */
4943 key = *arg + 1;
4944 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4945 ;
4946 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 }
4950 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 /*
4953 * something[idx]
4954 *
4955 * Get the (first) variable from inside the [].
4956 */
4957 *arg = skipwhite(*arg + 1);
4958 if (**arg == ':')
4959 empty1 = TRUE;
4960 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4961 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004963 {
4964 /* not a number or string */
4965 clear_tv(&var1);
4966 return FAIL;
4967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968
4969 /*
4970 * Get the second variable from inside the [:].
4971 */
4972 if (**arg == ':')
4973 {
4974 range = TRUE;
4975 *arg = skipwhite(*arg + 1);
4976 if (**arg == ']')
4977 empty2 = TRUE;
4978 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004980 if (!empty1)
4981 clear_tv(&var1);
4982 return FAIL;
4983 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004984 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004985 {
4986 /* not a number or string */
4987 if (!empty1)
4988 clear_tv(&var1);
4989 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 return FAIL;
4991 }
4992 }
4993
4994 /* Check for the ']'. */
4995 if (**arg != ']')
4996 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004997 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004998 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 clear_tv(&var1);
5000 if (range)
5001 clear_tv(&var2);
5002 return FAIL;
5003 }
5004 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 }
5006
5007 if (evaluate)
5008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 n1 = 0;
5010 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014 }
5015 if (range)
5016 {
5017 if (empty2)
5018 n2 = -1;
5019 else
5020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005021 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005023 }
5024 }
5025
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005026 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005027 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005028 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005029 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005030 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005031 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005032 case VAR_SPECIAL:
5033 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005034 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005035 break; /* not evaluating, skipping over subscript */
5036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 case VAR_NUMBER:
5038 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005039 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005040 len = (long)STRLEN(s);
5041 if (range)
5042 {
5043 /* The resulting variable is a substring. If the indexes
5044 * are out of range the result is empty. */
5045 if (n1 < 0)
5046 {
5047 n1 = len + n1;
5048 if (n1 < 0)
5049 n1 = 0;
5050 }
5051 if (n2 < 0)
5052 n2 = len + n2;
5053 else if (n2 >= len)
5054 n2 = len;
5055 if (n1 >= len || n2 < 0 || n1 > n2)
5056 s = NULL;
5057 else
5058 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5059 }
5060 else
5061 {
5062 /* The resulting variable is a string of a single
5063 * character. If the index is too big or negative the
5064 * result is empty. */
5065 if (n1 >= len || n1 < 0)
5066 s = NULL;
5067 else
5068 s = vim_strnsave(s + n1, 1);
5069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 clear_tv(rettv);
5071 rettv->v_type = VAR_STRING;
5072 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 break;
5074
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005075 case VAR_BLOB:
5076 len = blob_len(rettv->vval.v_blob);
5077 if (range)
5078 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005079 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005080 // are out of range the result is empty.
5081 if (n1 < 0)
5082 {
5083 n1 = len + n1;
5084 if (n1 < 0)
5085 n1 = 0;
5086 }
5087 if (n2 < 0)
5088 n2 = len + n2;
5089 else if (n2 >= len)
5090 n2 = len - 1;
5091 if (n1 >= len || n2 < 0 || n1 > n2)
5092 {
5093 clear_tv(rettv);
5094 rettv->v_type = VAR_BLOB;
5095 rettv->vval.v_blob = NULL;
5096 }
5097 else
5098 {
5099 blob_T *blob = blob_alloc();
5100
5101 if (blob != NULL)
5102 {
5103 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5104 {
5105 blob_free(blob);
5106 return FAIL;
5107 }
5108 blob->bv_ga.ga_len = n2 - n1 + 1;
5109 for (i = n1; i <= n2; i++)
5110 blob_set(blob, i - n1,
5111 blob_get(rettv->vval.v_blob, i));
5112
5113 clear_tv(rettv);
5114 rettv_blob_set(rettv, blob);
5115 }
5116 }
5117 }
5118 else
5119 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005120 // The resulting variable is a byte value.
5121 // If the index is too big or negative that is an error.
5122 if (n1 < 0)
5123 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005124 if (n1 < len && n1 >= 0)
5125 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005126 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005127
5128 clear_tv(rettv);
5129 rettv->v_type = VAR_NUMBER;
5130 rettv->vval.v_number = v;
5131 }
5132 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005134 }
5135 break;
5136
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005137 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 if (n1 < 0)
5140 n1 = len + n1;
5141 if (!empty1 && (n1 < 0 || n1 >= len))
5142 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005143 /* For a range we allow invalid values and return an empty
5144 * list. A list index out of range is an error. */
5145 if (!range)
5146 {
5147 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005148 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005149 return FAIL;
5150 }
5151 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152 }
5153 if (range)
5154 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005155 list_T *l;
5156 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157
5158 if (n2 < 0)
5159 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005160 else if (n2 >= len)
5161 n2 = len - 1;
5162 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005163 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 l = list_alloc();
5165 if (l == NULL)
5166 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 n1 <= n2; ++n1)
5169 {
5170 if (list_append_tv(l, &item->li_tv) == FAIL)
5171 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 return FAIL;
5174 }
5175 item = item->li_next;
5176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005178 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 }
5180 else
5181 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005182 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 clear_tv(rettv);
5184 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 }
5186 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187
5188 case VAR_DICT:
5189 if (range)
5190 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005192 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 if (len == -1)
5194 clear_tv(&var1);
5195 return FAIL;
5196 }
5197 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005198 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199
5200 if (len == -1)
5201 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005202 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005203 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 clear_tv(&var1);
5206 return FAIL;
5207 }
5208 }
5209
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005210 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005213 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 if (len == -1)
5215 clear_tv(&var1);
5216 if (item == NULL)
5217 return FAIL;
5218
5219 copy_tv(&item->di_tv, &var1);
5220 clear_tv(rettv);
5221 *rettv = var1;
5222 }
5223 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 }
5225 }
5226
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 return OK;
5228}
5229
5230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 * Get an option value.
5232 * "arg" points to the '&' or '+' before the option name.
5233 * "arg" is advanced to character after the option name.
5234 * Return OK or FAIL.
5235 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005237get_option_tv(
5238 char_u **arg,
5239 typval_T *rettv, /* when NULL, only check if option exists */
5240 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242 char_u *option_end;
5243 long numval;
5244 char_u *stringval;
5245 int opt_type;
5246 int c;
5247 int working = (**arg == '+'); /* has("+option") */
5248 int ret = OK;
5249 int opt_flags;
5250
5251 /*
5252 * Isolate the option name and find its value.
5253 */
5254 option_end = find_option_end(arg, &opt_flags);
5255 if (option_end == NULL)
5256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005257 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005258 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return FAIL;
5260 }
5261
5262 if (!evaluate)
5263 {
5264 *arg = option_end;
5265 return OK;
5266 }
5267
5268 c = *option_end;
5269 *option_end = NUL;
5270 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005271 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 if (opt_type == -3) /* invalid name */
5274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005275 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005276 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 ret = FAIL;
5278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 if (opt_type == -2) /* hidden string option */
5282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005283 rettv->v_type = VAR_STRING;
5284 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 else if (opt_type == -1) /* hidden number option */
5287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005288 rettv->v_type = VAR_NUMBER;
5289 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 else if (opt_type == 1) /* number option */
5292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005293 rettv->v_type = VAR_NUMBER;
5294 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 else /* string option */
5297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 rettv->v_type = VAR_STRING;
5299 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301 }
5302 else if (working && (opt_type == -2 || opt_type == -1))
5303 ret = FAIL;
5304
5305 *option_end = c; /* put back for error messages */
5306 *arg = option_end;
5307
5308 return ret;
5309}
5310
5311/*
5312 * Allocate a variable for a string constant.
5313 * Return OK or FAIL.
5314 */
5315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005316get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 char_u *p;
5319 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 int extra = 0;
5321
5322 /*
5323 * Find the end of the string, skipping backslashed characters.
5324 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005325 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
5327 if (*p == '\\' && p[1] != NUL)
5328 {
5329 ++p;
5330 /* A "\<x>" form occupies at least 4 characters, and produces up
5331 * to 6 characters: reserve space for 2 extra */
5332 if (*p == '<')
5333 extra += 2;
5334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 if (*p != '"')
5338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005339 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 return FAIL;
5341 }
5342
5343 /* If only parsing, set *arg and return here */
5344 if (!evaluate)
5345 {
5346 *arg = p + 1;
5347 return OK;
5348 }
5349
5350 /*
5351 * Copy the string into allocated memory, handling backslashed
5352 * characters.
5353 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005354 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (name == NULL)
5356 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 rettv->v_type = VAR_STRING;
5358 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 if (*p == '\\')
5363 {
5364 switch (*++p)
5365 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 case 'b': *name++ = BS; ++p; break;
5367 case 'e': *name++ = ESC; ++p; break;
5368 case 'f': *name++ = FF; ++p; break;
5369 case 'n': *name++ = NL; ++p; break;
5370 case 'r': *name++ = CAR; ++p; break;
5371 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 case 'X': /* hex: "\x1", "\x12" */
5374 case 'x':
5375 case 'u': /* Unicode: "\u0023" */
5376 case 'U':
5377 if (vim_isxdigit(p[1]))
5378 {
5379 int n, nr;
5380 int c = toupper(*p);
5381
5382 if (c == 'X')
5383 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005384 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005386 else
5387 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 nr = 0;
5389 while (--n >= 0 && vim_isxdigit(p[1]))
5390 {
5391 ++p;
5392 nr = (nr << 4) + hex2nr(*p);
5393 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 /* For "\u" store the number according to
5396 * 'encoding'. */
5397 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 break;
5403
5404 /* octal: "\1", "\12", "\123" */
5405 case '0':
5406 case '1':
5407 case '2':
5408 case '3':
5409 case '4':
5410 case '5':
5411 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 case '7': *name = *p++ - '0';
5413 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 *name = (*name << 3) + *p++ - '0';
5416 if (*p >= '0' && *p <= '7')
5417 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 break;
5421
5422 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005423 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (extra != 0)
5425 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 break;
5428 }
5429 /* FALLTHROUGH */
5430
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 break;
5433 }
5434 }
5435 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005440 if (*p != NUL) /* just in case */
5441 ++p;
5442 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 return OK;
5445}
5446
5447/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 * Return OK or FAIL.
5450 */
5451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005452get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005455 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005456 int reduce = 0;
5457
5458 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005460 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005461 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005466 break;
5467 ++reduce;
5468 ++p;
5469 }
5470 }
5471
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005474 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005475 return FAIL;
5476 }
5477
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005479 if (!evaluate)
5480 {
5481 *arg = p + 1;
5482 return OK;
5483 }
5484
5485 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005487 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005488 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005489 if (str == NULL)
5490 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 rettv->v_type = VAR_STRING;
5492 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005493
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005499 break;
5500 ++p;
5501 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005505 *arg = p + 1;
5506
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005507 return OK;
5508}
5509
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005510/*
5511 * Return the function name of the partial.
5512 */
5513 char_u *
5514partial_name(partial_T *pt)
5515{
5516 if (pt->pt_name != NULL)
5517 return pt->pt_name;
5518 return pt->pt_func->uf_name;
5519}
5520
Bram Moolenaarddecc252016-04-06 22:59:37 +02005521 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005522partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005523{
5524 int i;
5525
5526 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005527 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005528 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005529 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005530 if (pt->pt_name != NULL)
5531 {
5532 func_unref(pt->pt_name);
5533 vim_free(pt->pt_name);
5534 }
5535 else
5536 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005537 vim_free(pt);
5538}
5539
5540/*
5541 * Unreference a closure: decrement the reference count and free it when it
5542 * becomes zero.
5543 */
5544 void
5545partial_unref(partial_T *pt)
5546{
5547 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005548 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005549}
5550
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005551static int tv_equal_recurse_limit;
5552
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005553 static int
5554func_equal(
5555 typval_T *tv1,
5556 typval_T *tv2,
5557 int ic) /* ignore case */
5558{
5559 char_u *s1, *s2;
5560 dict_T *d1, *d2;
5561 int a1, a2;
5562 int i;
5563
5564 /* empty and NULL function name considered the same */
5565 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005566 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005567 if (s1 != NULL && *s1 == NUL)
5568 s1 = NULL;
5569 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005570 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005571 if (s2 != NULL && *s2 == NUL)
5572 s2 = NULL;
5573 if (s1 == NULL || s2 == NULL)
5574 {
5575 if (s1 != s2)
5576 return FALSE;
5577 }
5578 else if (STRCMP(s1, s2) != 0)
5579 return FALSE;
5580
5581 /* empty dict and NULL dict is different */
5582 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5583 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5584 if (d1 == NULL || d2 == NULL)
5585 {
5586 if (d1 != d2)
5587 return FALSE;
5588 }
5589 else if (!dict_equal(d1, d2, ic, TRUE))
5590 return FALSE;
5591
5592 /* empty list and no list considered the same */
5593 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5594 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5595 if (a1 != a2)
5596 return FALSE;
5597 for (i = 0; i < a1; ++i)
5598 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5599 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5600 return FALSE;
5601
5602 return TRUE;
5603}
5604
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005605/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005606 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005607 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005608 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005609 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005610 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005611tv_equal(
5612 typval_T *tv1,
5613 typval_T *tv2,
5614 int ic, /* ignore case */
5615 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005616{
5617 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005618 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005619 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005620 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005621
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005622 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005623 * recursiveness to a limit. We guess they are equal then.
5624 * A fixed limit has the problem of still taking an awful long time.
5625 * Reduce the limit every time running into it. That should work fine for
5626 * deeply linked structures that are not recursively linked and catch
5627 * recursiveness quickly. */
5628 if (!recursive)
5629 tv_equal_recurse_limit = 1000;
5630 if (recursive_cnt >= tv_equal_recurse_limit)
5631 {
5632 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005633 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005634 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005635
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005636 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5637 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005638 if ((tv1->v_type == VAR_FUNC
5639 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5640 && (tv2->v_type == VAR_FUNC
5641 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5642 {
5643 ++recursive_cnt;
5644 r = func_equal(tv1, tv2, ic);
5645 --recursive_cnt;
5646 return r;
5647 }
5648
5649 if (tv1->v_type != tv2->v_type)
5650 return FALSE;
5651
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005652 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005653 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005654 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005655 ++recursive_cnt;
5656 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5657 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005658 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005659
5660 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005661 ++recursive_cnt;
5662 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5663 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005664 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005665
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005666 case VAR_BLOB:
5667 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5668
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005669 case VAR_NUMBER:
5670 return tv1->vval.v_number == tv2->vval.v_number;
5671
5672 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005673 s1 = tv_get_string_buf(tv1, buf1);
5674 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005675 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005676
5677 case VAR_SPECIAL:
5678 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005679
5680 case VAR_FLOAT:
5681#ifdef FEAT_FLOAT
5682 return tv1->vval.v_float == tv2->vval.v_float;
5683#endif
5684 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005685#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005686 return tv1->vval.v_job == tv2->vval.v_job;
5687#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005688 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005689#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005690 return tv1->vval.v_channel == tv2->vval.v_channel;
5691#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005692 case VAR_FUNC:
5693 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005694 case VAR_UNKNOWN:
5695 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005697
Bram Moolenaara03f2332016-02-06 18:09:59 +01005698 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5699 * does not equal anything, not even itself. */
5700 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701}
5702
5703/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005704 * Return the next (unique) copy ID.
5705 * Used for serializing nested structures.
5706 */
5707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005709{
5710 current_copyID += COPYID_INC;
5711 return current_copyID;
5712}
5713
5714/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005715 * Garbage collection for lists and dictionaries.
5716 *
5717 * We use reference counts to be able to free most items right away when they
5718 * are no longer used. But for composite items it's possible that it becomes
5719 * unused while the reference count is > 0: When there is a recursive
5720 * reference. Example:
5721 * :let l = [1, 2, 3]
5722 * :let d = {9: l}
5723 * :let l[1] = d
5724 *
5725 * Since this is quite unusual we handle this with garbage collection: every
5726 * once in a while find out which lists and dicts are not referenced from any
5727 * variable.
5728 *
5729 * Here is a good reference text about garbage collection (refers to Python
5730 * but it applies to all reference-counting mechanisms):
5731 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005732 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005733
5734/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005735 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005736 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005737 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005738 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005739 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005740garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005741{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005742 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005743 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005744 buf_T *buf;
5745 win_T *wp;
5746 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005747 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005748 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005749
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005750 if (!testing)
5751 {
5752 /* Only do this once. */
5753 want_garbage_collect = FALSE;
5754 may_garbage_collect = FALSE;
5755 garbage_collect_at_exit = FALSE;
5756 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005757
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005758 /* We advance by two because we add one for items referenced through
5759 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005760 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005761
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005762 /*
5763 * 1. Go through all accessible variables and mark all lists and dicts
5764 * with copyID.
5765 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005766
5767 /* Don't free variables in the previous_funccal list unless they are only
5768 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005769 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005770 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005771
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005772 /* script-local variables */
5773 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005774 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005775
5776 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005777 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005778 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5779 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005780
5781 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005782 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005783 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5784 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005785 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005786 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5787 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005788#ifdef FEAT_TEXT_PROP
5789 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5790 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5791 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005792 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005793 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005794 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5795 NULL, NULL);
5796#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005797
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005798 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005799 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005800 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5801 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005802 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005803 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005804
5805 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005806 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005807
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005808 /* named functions (matters for closures) */
5809 abort = abort || set_ref_in_functions(copyID);
5810
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005811 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005812 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005813
Bram Moolenaard812df62008-11-09 12:46:09 +00005814 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005815 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005816
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005817 // callbacks in buffers
5818 abort = abort || set_ref_in_buffers(copyID);
5819
Bram Moolenaar1dced572012-04-05 16:54:08 +02005820#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005821 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005822#endif
5823
Bram Moolenaardb913952012-06-29 12:54:53 +02005824#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005825 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005826#endif
5827
5828#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005829 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005830#endif
5831
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005832#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005833 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005834 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005835#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005836#ifdef FEAT_NETBEANS_INTG
5837 abort = abort || set_ref_in_nb_channel(copyID);
5838#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005839
Bram Moolenaare3188e22016-05-31 21:13:04 +02005840#ifdef FEAT_TIMERS
5841 abort = abort || set_ref_in_timer(copyID);
5842#endif
5843
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005844#ifdef FEAT_QUICKFIX
5845 abort = abort || set_ref_in_quickfix(copyID);
5846#endif
5847
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005848#ifdef FEAT_TERMINAL
5849 abort = abort || set_ref_in_term(copyID);
5850#endif
5851
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005852#ifdef FEAT_TEXT_PROP
5853 abort = abort || set_ref_in_popups(copyID);
5854#endif
5855
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005857 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005858 /*
5859 * 2. Free lists and dictionaries that are not referenced.
5860 */
5861 did_free = free_unref_items(copyID);
5862
5863 /*
5864 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005865 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005866 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005867 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005868 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005869 else if (p_verbose > 0)
5870 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005871 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005872 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005873
5874 return did_free;
5875}
5876
5877/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005878 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005879 */
5880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005881free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005882{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005883 int did_free = FALSE;
5884
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005885 /* Let all "free" functions know that we are here. This means no
5886 * dictionaries, lists, channels or jobs are to be freed, because we will
5887 * do that here. */
5888 in_free_unref_items = TRUE;
5889
5890 /*
5891 * PASS 1: free the contents of the items. We don't free the items
5892 * themselves yet, so that it is possible to decrement refcount counters
5893 */
5894
Bram Moolenaarcd524592016-07-17 14:57:05 +02005895 /* Go through the list of dicts and free items without the copyID. */
5896 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005897
Bram Moolenaarda861d62016-07-17 15:46:27 +02005898 /* Go through the list of lists and free items without the copyID. */
5899 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005900
5901#ifdef FEAT_JOB_CHANNEL
5902 /* Go through the list of jobs and free items without the copyID. This
5903 * must happen before doing channels, because jobs refer to channels, but
5904 * the reference from the channel to the job isn't tracked. */
5905 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5906
5907 /* Go through the list of channels and free items without the copyID. */
5908 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5909#endif
5910
5911 /*
5912 * PASS 2: free the items themselves.
5913 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005914 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005915 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005916
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005917#ifdef FEAT_JOB_CHANNEL
5918 /* Go through the list of jobs and free items without the copyID. This
5919 * must happen before doing channels, because jobs refer to channels, but
5920 * the reference from the channel to the job isn't tracked. */
5921 free_unused_jobs(copyID, COPYID_MASK);
5922
5923 /* Go through the list of channels and free items without the copyID. */
5924 free_unused_channels(copyID, COPYID_MASK);
5925#endif
5926
5927 in_free_unref_items = FALSE;
5928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 return did_free;
5930}
5931
5932/*
5933 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005934 * "list_stack" is used to add lists to be marked. Can be NULL.
5935 *
5936 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005937 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005939set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005940{
5941 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005942 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005944 hashtab_T *cur_ht;
5945 ht_stack_T *ht_stack = NULL;
5946 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005947
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005948 cur_ht = ht;
5949 for (;;)
5950 {
5951 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005952 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005953 /* Mark each item in the hashtab. If the item contains a hashtab
5954 * it is added to ht_stack, if it contains a list it is added to
5955 * list_stack. */
5956 todo = (int)cur_ht->ht_used;
5957 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5958 if (!HASHITEM_EMPTY(hi))
5959 {
5960 --todo;
5961 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5962 &ht_stack, list_stack);
5963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005965
5966 if (ht_stack == NULL)
5967 break;
5968
5969 /* take an item from the stack */
5970 cur_ht = ht_stack->ht;
5971 tempitem = ht_stack;
5972 ht_stack = ht_stack->prev;
5973 free(tempitem);
5974 }
5975
5976 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005977}
5978
5979/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005980 * Mark a dict and its items with "copyID".
5981 * Returns TRUE if setting references failed somehow.
5982 */
5983 int
5984set_ref_in_dict(dict_T *d, int copyID)
5985{
5986 if (d != NULL && d->dv_copyID != copyID)
5987 {
5988 d->dv_copyID = copyID;
5989 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5990 }
5991 return FALSE;
5992}
5993
5994/*
5995 * Mark a list and its items with "copyID".
5996 * Returns TRUE if setting references failed somehow.
5997 */
5998 int
5999set_ref_in_list(list_T *ll, int copyID)
6000{
6001 if (ll != NULL && ll->lv_copyID != copyID)
6002 {
6003 ll->lv_copyID = copyID;
6004 return set_ref_in_list_items(ll, copyID, NULL);
6005 }
6006 return FALSE;
6007}
6008
6009/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006011 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6012 *
6013 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006014 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006015 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006016set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006018 listitem_T *li;
6019 int abort = FALSE;
6020 list_T *cur_l;
6021 list_stack_T *list_stack = NULL;
6022 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006023
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006024 cur_l = l;
6025 for (;;)
6026 {
6027 if (!abort)
6028 /* Mark each item in the list. If the item contains a hashtab
6029 * it is added to ht_stack, if it contains a list it is added to
6030 * list_stack. */
6031 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6032 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6033 ht_stack, &list_stack);
6034 if (list_stack == NULL)
6035 break;
6036
6037 /* take an item from the stack */
6038 cur_l = list_stack->list;
6039 tempitem = list_stack;
6040 list_stack = list_stack->prev;
6041 free(tempitem);
6042 }
6043
6044 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006045}
6046
6047/*
6048 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006049 * "list_stack" is used to add lists to be marked. Can be NULL.
6050 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6051 *
6052 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006053 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006055set_ref_in_item(
6056 typval_T *tv,
6057 int copyID,
6058 ht_stack_T **ht_stack,
6059 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006060{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006061 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006062
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006063 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006064 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006065 dict_T *dd = tv->vval.v_dict;
6066
Bram Moolenaara03f2332016-02-06 18:09:59 +01006067 if (dd != NULL && dd->dv_copyID != copyID)
6068 {
6069 /* Didn't see this dict yet. */
6070 dd->dv_copyID = copyID;
6071 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006072 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006073 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6074 }
6075 else
6076 {
6077 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
6078 if (newitem == NULL)
6079 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006080 else
6081 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006082 newitem->ht = &dd->dv_hashtab;
6083 newitem->prev = *ht_stack;
6084 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006085 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006086 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006087 }
6088 }
6089 else if (tv->v_type == VAR_LIST)
6090 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006091 list_T *ll = tv->vval.v_list;
6092
Bram Moolenaara03f2332016-02-06 18:09:59 +01006093 if (ll != NULL && ll->lv_copyID != copyID)
6094 {
6095 /* Didn't see this list yet. */
6096 ll->lv_copyID = copyID;
6097 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006098 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006099 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006100 }
6101 else
6102 {
6103 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006104 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006105 if (newitem == NULL)
6106 abort = TRUE;
6107 else
6108 {
6109 newitem->list = ll;
6110 newitem->prev = *list_stack;
6111 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006112 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006113 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006114 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006116 else if (tv->v_type == VAR_FUNC)
6117 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006118 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006119 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006120 else if (tv->v_type == VAR_PARTIAL)
6121 {
6122 partial_T *pt = tv->vval.v_partial;
6123 int i;
6124
6125 /* A partial does not have a copyID, because it cannot contain itself.
6126 */
6127 if (pt != NULL)
6128 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006129 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006130
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006131 if (pt->pt_dict != NULL)
6132 {
6133 typval_T dtv;
6134
6135 dtv.v_type = VAR_DICT;
6136 dtv.vval.v_dict = pt->pt_dict;
6137 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6138 }
6139
6140 for (i = 0; i < pt->pt_argc; ++i)
6141 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6142 ht_stack, list_stack);
6143 }
6144 }
6145#ifdef FEAT_JOB_CHANNEL
6146 else if (tv->v_type == VAR_JOB)
6147 {
6148 job_T *job = tv->vval.v_job;
6149 typval_T dtv;
6150
6151 if (job != NULL && job->jv_copyID != copyID)
6152 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006153 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006154 if (job->jv_channel != NULL)
6155 {
6156 dtv.v_type = VAR_CHANNEL;
6157 dtv.vval.v_channel = job->jv_channel;
6158 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6159 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006160 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006161 {
6162 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006163 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006164 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6165 }
6166 }
6167 }
6168 else if (tv->v_type == VAR_CHANNEL)
6169 {
6170 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006171 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006172 typval_T dtv;
6173 jsonq_T *jq;
6174 cbq_T *cq;
6175
6176 if (ch != NULL && ch->ch_copyID != copyID)
6177 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006178 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006179 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006180 {
6181 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6182 jq = jq->jq_next)
6183 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6184 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6185 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006186 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006187 {
6188 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006189 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006190 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6191 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006192 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006193 {
6194 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006195 dtv.vval.v_partial =
6196 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006197 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6198 }
6199 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006200 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006201 {
6202 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006203 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006204 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6205 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006206 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006207 {
6208 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006209 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006210 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6211 }
6212 }
6213 }
6214#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006215 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006216}
6217
Bram Moolenaar17a13432016-01-24 14:22:10 +01006218 static char *
6219get_var_special_name(int nr)
6220{
6221 switch (nr)
6222 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006223 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006224 case VVAL_TRUE: return "v:true";
6225 case VVAL_NONE: return "v:none";
6226 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006227 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006228 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006229 return "42";
6230}
6231
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233 * Return a string with the string representation of a variable.
6234 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006236 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006237 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6238 * stings as "string()", otherwise does not put quotes around strings, as
6239 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006240 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6241 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006242 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006244 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006245echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006246 typval_T *tv,
6247 char_u **tofree,
6248 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006249 int copyID,
6250 int echo_style,
6251 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006252 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006254 static int recurse = 0;
6255 char_u *r = NULL;
6256
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006258 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006259 if (!did_echo_string_emsg)
6260 {
6261 /* Only give this message once for a recursive call to avoid
6262 * flooding the user with errors. And stop iterating over lists
6263 * and dicts. */
6264 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006265 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006266 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006267 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006268 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 }
6270 ++recurse;
6271
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 switch (tv->v_type)
6273 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006274 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006275 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006276 {
6277 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006278 r = tv->vval.v_string;
6279 if (r == NULL)
6280 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006281 }
6282 else
6283 {
6284 *tofree = string_quote(tv->vval.v_string, FALSE);
6285 r = *tofree;
6286 }
6287 break;
6288
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006290 if (echo_style)
6291 {
6292 *tofree = NULL;
6293 r = tv->vval.v_string;
6294 }
6295 else
6296 {
6297 *tofree = string_quote(tv->vval.v_string, TRUE);
6298 r = *tofree;
6299 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006301
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006302 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006303 {
6304 partial_T *pt = tv->vval.v_partial;
6305 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006306 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006307 garray_T ga;
6308 int i;
6309 char_u *tf;
6310
6311 ga_init2(&ga, 1, 100);
6312 ga_concat(&ga, (char_u *)"function(");
6313 if (fname != NULL)
6314 {
6315 ga_concat(&ga, fname);
6316 vim_free(fname);
6317 }
6318 if (pt != NULL && pt->pt_argc > 0)
6319 {
6320 ga_concat(&ga, (char_u *)", [");
6321 for (i = 0; i < pt->pt_argc; ++i)
6322 {
6323 if (i > 0)
6324 ga_concat(&ga, (char_u *)", ");
6325 ga_concat(&ga,
6326 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6327 vim_free(tf);
6328 }
6329 ga_concat(&ga, (char_u *)"]");
6330 }
6331 if (pt != NULL && pt->pt_dict != NULL)
6332 {
6333 typval_T dtv;
6334
6335 ga_concat(&ga, (char_u *)", ");
6336 dtv.v_type = VAR_DICT;
6337 dtv.vval.v_dict = pt->pt_dict;
6338 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6339 vim_free(tf);
6340 }
6341 ga_concat(&ga, (char_u *)")");
6342
6343 *tofree = ga.ga_data;
6344 r = *tofree;
6345 break;
6346 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006347
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006348 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006349 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006350 break;
6351
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006353 if (tv->vval.v_list == NULL)
6354 {
6355 *tofree = NULL;
6356 r = NULL;
6357 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006358 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6359 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006360 {
6361 *tofree = NULL;
6362 r = (char_u *)"[...]";
6363 }
6364 else
6365 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006366 int old_copyID = tv->vval.v_list->lv_copyID;
6367
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006368 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006369 *tofree = list2string(tv, copyID, restore_copyID);
6370 if (restore_copyID)
6371 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006372 r = *tofree;
6373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006374 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006375
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006377 if (tv->vval.v_dict == NULL)
6378 {
6379 *tofree = NULL;
6380 r = NULL;
6381 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006382 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6383 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006384 {
6385 *tofree = NULL;
6386 r = (char_u *)"{...}";
6387 }
6388 else
6389 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006390 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006391 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006392 *tofree = dict2string(tv, copyID, restore_copyID);
6393 if (restore_copyID)
6394 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006395 r = *tofree;
6396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006399 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006400 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006401 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006402 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006403 break;
6404
Bram Moolenaar835dc632016-02-07 14:27:38 +01006405 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006406 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006407 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006408 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006409 if (composite_val)
6410 {
6411 *tofree = string_quote(r, FALSE);
6412 r = *tofree;
6413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006415
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006416 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006417#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006418 *tofree = NULL;
6419 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6420 r = numbuf;
6421 break;
6422#endif
6423
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006424 case VAR_SPECIAL:
6425 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006426 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006427 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006429
Bram Moolenaar8502c702014-06-17 12:51:16 +02006430 if (--recurse == 0)
6431 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006433}
6434
6435/*
6436 * Return a string with the string representation of a variable.
6437 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6438 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006439 * Does not put quotes around strings, as ":echo" displays values.
6440 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6441 * May return NULL.
6442 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006443 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006444echo_string(
6445 typval_T *tv,
6446 char_u **tofree,
6447 char_u *numbuf,
6448 int copyID)
6449{
6450 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6451}
6452
6453/*
6454 * Return a string with the string representation of a variable.
6455 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6456 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006457 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006458 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006459 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006461tv2string(
6462 typval_T *tv,
6463 char_u **tofree,
6464 char_u *numbuf,
6465 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006466{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006467 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468}
6469
6470/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 * Return string "str" in ' quotes, doubling ' characters.
6472 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006474 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006477{
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006479 char_u *p, *r, *s;
6480
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 len = (function ? 13 : 3);
6482 if (str != NULL)
6483 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006484 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006485 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 if (*p == '\'')
6487 ++len;
6488 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006489 s = r = alloc(len);
6490 if (r != NULL)
6491 {
6492 if (function)
6493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006495 r += 10;
6496 }
6497 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 if (str != NULL)
6500 for (p = str; *p != NUL; )
6501 {
6502 if (*p == '\'')
6503 *r++ = '\'';
6504 MB_COPY_CHAR(p, r);
6505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006507 if (function)
6508 *r++ = ')';
6509 *r++ = NUL;
6510 }
6511 return s;
6512}
6513
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006514#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006515/*
6516 * Convert the string "text" to a floating point number.
6517 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6518 * this always uses a decimal point.
6519 * Returns the length of the text that was consumed.
6520 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006522string2float(
6523 char_u *text,
6524 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006525{
6526 char *s = (char *)text;
6527 float_T f;
6528
Bram Moolenaar62473612017-01-08 19:25:40 +01006529 /* MS-Windows does not deal with "inf" and "nan" properly. */
6530 if (STRNICMP(text, "inf", 3) == 0)
6531 {
6532 *value = INFINITY;
6533 return 3;
6534 }
6535 if (STRNICMP(text, "-inf", 3) == 0)
6536 {
6537 *value = -INFINITY;
6538 return 4;
6539 }
6540 if (STRNICMP(text, "nan", 3) == 0)
6541 {
6542 *value = NAN;
6543 return 3;
6544 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006545 f = strtod(s, &s);
6546 *value = f;
6547 return (int)((char_u *)s - text);
6548}
6549#endif
6550
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 * Get the value of an environment variable.
6553 * "arg" is pointing to the '$'. It is advanced to after the name.
6554 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006555 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 */
6557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006558get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 char_u *string = NULL;
6561 int len;
6562 int cc;
6563 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006564 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566 ++*arg;
6567 name = *arg;
6568 len = get_env_len(arg);
6569 if (evaluate)
6570 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006571 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006572 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006573
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006574 cc = name[len];
6575 name[len] = NUL;
6576 /* first try vim_getenv(), fast for normal environment vars */
6577 string = vim_getenv(name, &mustfree);
6578 if (string != NULL && *string != NUL)
6579 {
6580 if (!mustfree)
6581 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006583 else
6584 {
6585 if (mustfree)
6586 vim_free(string);
6587
6588 /* next try expanding things like $VIM and ${HOME} */
6589 string = expand_env_save(name - 1);
6590 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006591 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006592 }
6593 name[len] = cc;
6594
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006595 rettv->v_type = VAR_STRING;
6596 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 }
6598
6599 return OK;
6600}
6601
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006602
6603
6604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006606 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609var2fpos(
6610 typval_T *varp,
6611 int dollar_lnum, /* TRUE when $ is last line */
6612 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006614 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006616 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
Bram Moolenaara5525202006-03-02 22:52:09 +00006618 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006619 if (varp->v_type == VAR_LIST)
6620 {
6621 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006622 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006623 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006624 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006625
6626 l = varp->vval.v_list;
6627 if (l == NULL)
6628 return NULL;
6629
6630 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006631 pos.lnum = list_find_nr(l, 0L, &error);
6632 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006633 return NULL; /* invalid line number */
6634
6635 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006636 pos.col = list_find_nr(l, 1L, &error);
6637 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006638 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006639 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006640
6641 /* We accept "$" for the column number: last column. */
6642 li = list_find(l, 1L);
6643 if (li != NULL && li->li_tv.v_type == VAR_STRING
6644 && li->li_tv.vval.v_string != NULL
6645 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6646 pos.col = len + 1;
6647
Bram Moolenaara5525202006-03-02 22:52:09 +00006648 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006649 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006650 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006651 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006652
Bram Moolenaara5525202006-03-02 22:52:09 +00006653 /* Get the virtual offset. Defaults to zero. */
6654 pos.coladd = list_find_nr(l, 2L, &error);
6655 if (error)
6656 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006657
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006658 return &pos;
6659 }
6660
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006661 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006662 if (name == NULL)
6663 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006664 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006666 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6667 {
6668 if (VIsual_active)
6669 return &VIsual;
6670 return &curwin->w_cursor;
6671 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006672 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006674 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6676 return NULL;
6677 return pp;
6678 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006679
Bram Moolenaara5525202006-03-02 22:52:09 +00006680 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006681
Bram Moolenaar477933c2007-07-17 14:32:23 +00006682 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006683 {
6684 pos.col = 0;
6685 if (name[1] == '0') /* "w0": first visible line */
6686 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006687 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006688 /* In silent Ex mode topline is zero, but that's not a valid line
6689 * number; use one instead. */
6690 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006691 return &pos;
6692 }
6693 else if (name[1] == '$') /* "w$": last visible line */
6694 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006695 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006696 /* In silent Ex mode botline is zero, return zero then. */
6697 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006698 return &pos;
6699 }
6700 }
6701 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006703 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
6705 pos.lnum = curbuf->b_ml.ml_line_count;
6706 pos.col = 0;
6707 }
6708 else
6709 {
6710 pos.lnum = curwin->w_cursor.lnum;
6711 pos.col = (colnr_T)STRLEN(ml_get_curline());
6712 }
6713 return &pos;
6714 }
6715 return NULL;
6716}
6717
6718/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006719 * Convert list in "arg" into a position and optional file number.
6720 * When "fnump" is NULL there is no file number, only 3 items.
6721 * Note that the column is passed on as-is, the caller may want to decrement
6722 * it to use 1 for the first column.
6723 * Return FAIL when conversion is not possible, doesn't check the position for
6724 * validity.
6725 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006727list2fpos(
6728 typval_T *arg,
6729 pos_T *posp,
6730 int *fnump,
6731 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006732{
6733 list_T *l = arg->vval.v_list;
6734 long i = 0;
6735 long n;
6736
Bram Moolenaar493c1782014-05-28 14:34:46 +02006737 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6738 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006739 if (arg->v_type != VAR_LIST
6740 || l == NULL
6741 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006742 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006743 return FAIL;
6744
6745 if (fnump != NULL)
6746 {
6747 n = list_find_nr(l, i++, NULL); /* fnum */
6748 if (n < 0)
6749 return FAIL;
6750 if (n == 0)
6751 n = curbuf->b_fnum; /* current buffer */
6752 *fnump = n;
6753 }
6754
6755 n = list_find_nr(l, i++, NULL); /* lnum */
6756 if (n < 0)
6757 return FAIL;
6758 posp->lnum = n;
6759
6760 n = list_find_nr(l, i++, NULL); /* col */
6761 if (n < 0)
6762 return FAIL;
6763 posp->col = n;
6764
Bram Moolenaar493c1782014-05-28 14:34:46 +02006765 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006766 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006767 posp->coladd = 0;
6768 else
6769 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006770
Bram Moolenaar493c1782014-05-28 14:34:46 +02006771 if (curswantp != NULL)
6772 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6773
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006774 return OK;
6775}
6776
6777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 * Get the length of an environment variable name.
6779 * Advance "arg" to the first character after the name.
6780 * Return 0 for error.
6781 */
6782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 char_u *p;
6786 int len;
6787
6788 for (p = *arg; vim_isIDc(*p); ++p)
6789 ;
6790 if (p == *arg) /* no name found */
6791 return 0;
6792
6793 len = (int)(p - *arg);
6794 *arg = p;
6795 return len;
6796}
6797
6798/*
6799 * Get the length of the name of a function or internal variable.
6800 * "arg" is advanced to the first non-white character after the name.
6801 * Return 0 if something is wrong.
6802 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 char_u *p;
6807 int len;
6808
6809 /* Find the end of the name. */
6810 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006811 {
6812 if (*p == ':')
6813 {
6814 /* "s:" is start of "s:var", but "n:" is not and can be used in
6815 * slice "[n:]". Also "xx:" is not a namespace. */
6816 len = (int)(p - *arg);
6817 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6818 || len > 1)
6819 break;
6820 }
6821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (p == *arg) /* no name found */
6823 return 0;
6824
6825 len = (int)(p - *arg);
6826 *arg = skipwhite(p);
6827
6828 return len;
6829}
6830
6831/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006832 * Get the length of the name of a variable or function.
6833 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006835 * Return -1 if curly braces expansion failed.
6836 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 * If the name contains 'magic' {}'s, expand them and return the
6838 * expanded name in an allocated string via 'alias' - caller must free.
6839 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841get_name_len(
6842 char_u **arg,
6843 char_u **alias,
6844 int evaluate,
6845 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 char_u *p;
6849 char_u *expr_start;
6850 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
6852 *alias = NULL; /* default to no alias */
6853
6854 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6855 && (*arg)[2] == (int)KE_SNR)
6856 {
6857 /* hard coded <SNR>, already translated */
6858 *arg += 3;
6859 return get_id_len(arg) + 3;
6860 }
6861 len = eval_fname_script(*arg);
6862 if (len > 0)
6863 {
6864 /* literal "<SID>", "s:" or "<SNR>" */
6865 *arg += len;
6866 }
6867
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006871 p = find_name_end(*arg, &expr_start, &expr_end,
6872 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 if (expr_start != NULL)
6874 {
6875 char_u *temp_string;
6876
6877 if (!evaluate)
6878 {
6879 len += (int)(p - *arg);
6880 *arg = skipwhite(p);
6881 return len;
6882 }
6883
6884 /*
6885 * Include any <SID> etc in the expanded string:
6886 * Thus the -len here.
6887 */
6888 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6889 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006890 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 *alias = temp_string;
6892 *arg = skipwhite(p);
6893 return (int)STRLEN(temp_string);
6894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006897 // Only give an error when there is something, otherwise it will be
6898 // reported at a higher level.
6899 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006900 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
6902 return len;
6903}
6904
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006905/*
6906 * Find the end of a variable or function name, taking care of magic braces.
6907 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6908 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006909 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 * Return a pointer to just after the name. Equal to "arg" if there is no
6911 * valid name.
6912 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006913 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006914find_name_end(
6915 char_u *arg,
6916 char_u **expr_start,
6917 char_u **expr_end,
6918 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920 int mb_nest = 0;
6921 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006923 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006927 *expr_start = NULL;
6928 *expr_end = NULL;
6929 }
6930
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006931 /* Quick check for valid starting character. */
6932 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6933 return arg;
6934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 for (p = arg; *p != NUL
6936 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006938 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006939 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006940 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006942 if (*p == '\'')
6943 {
6944 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006945 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006946 ;
6947 if (*p == NUL)
6948 break;
6949 }
6950 else if (*p == '"')
6951 {
6952 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006953 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006954 if (*p == '\\' && p[1] != NUL)
6955 ++p;
6956 if (*p == NUL)
6957 break;
6958 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006959 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6960 {
6961 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006962 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006963 len = (int)(p - arg);
6964 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006965 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006966 break;
6967 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006968
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006969 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 if (*p == '[')
6972 ++br_nest;
6973 else if (*p == ']')
6974 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006976
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006977 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979 if (*p == '{')
6980 {
6981 mb_nest++;
6982 if (expr_start != NULL && *expr_start == NULL)
6983 *expr_start = p;
6984 }
6985 else if (*p == '}')
6986 {
6987 mb_nest--;
6988 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6989 *expr_end = p;
6990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 }
6993
6994 return p;
6995}
6996
6997/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006998 * Expands out the 'magic' {}'s in a variable/function name.
6999 * Note that this can call itself recursively, to deal with
7000 * constructs like foo{bar}{baz}{bam}
7001 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7002 * "in_start" ^
7003 * "expr_start" ^
7004 * "expr_end" ^
7005 * "in_end" ^
7006 *
7007 * Returns a new allocated string, which the caller must free.
7008 * Returns NULL for failure.
7009 */
7010 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007011make_expanded_name(
7012 char_u *in_start,
7013 char_u *expr_start,
7014 char_u *expr_end,
7015 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007016{
7017 char_u c1;
7018 char_u *retval = NULL;
7019 char_u *temp_result;
7020 char_u *nextcmd = NULL;
7021
7022 if (expr_end == NULL || in_end == NULL)
7023 return NULL;
7024 *expr_start = NUL;
7025 *expr_end = NUL;
7026 c1 = *in_end;
7027 *in_end = NUL;
7028
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007029 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007030 if (temp_result != NULL && nextcmd == NULL)
7031 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007032 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7033 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007034 if (retval != NULL)
7035 {
7036 STRCPY(retval, in_start);
7037 STRCAT(retval, temp_result);
7038 STRCAT(retval, expr_end + 1);
7039 }
7040 }
7041 vim_free(temp_result);
7042
7043 *in_end = c1; /* put char back for error messages */
7044 *expr_start = '{';
7045 *expr_end = '}';
7046
7047 if (retval != NULL)
7048 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007049 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007050 if (expr_start != NULL)
7051 {
7052 /* Further expansion! */
7053 temp_result = make_expanded_name(retval, expr_start,
7054 expr_end, temp_result);
7055 vim_free(retval);
7056 retval = temp_result;
7057 }
7058 }
7059
7060 return retval;
7061}
7062
7063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007068eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007070 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
7071}
7072
7073/*
7074 * Return TRUE if character "c" can be used as the first character in a
7075 * variable or function name (excluding '{' and '}').
7076 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007079{
7080 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081}
7082
7083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 * Set number v: variable to "val".
7085 */
7086 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007087set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090}
7091
7092/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007093 * Get typval_T v: variable value.
7094 */
7095 typval_T *
7096get_vim_var_tv(int idx)
7097{
7098 return &vimvars[idx].vv_tv;
7099}
7100
7101/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007102 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007104 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108}
7109
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007110/*
7111 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007112 * If the String variable has never been set, return an empty string.
7113 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007114 */
7115 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007116get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007117{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007118 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007119}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007122 * Get List v: variable value. Caller must take care of reference count when
7123 * needed.
7124 */
7125 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007126get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007127{
7128 return vimvars[idx].vv_list;
7129}
7130
7131/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007132 * Get Dict v: variable value. Caller must take care of reference count when
7133 * needed.
7134 */
7135 dict_T *
7136get_vim_var_dict(int idx)
7137{
7138 return vimvars[idx].vv_dict;
7139}
7140
7141/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007142 * Set v:char to character "c".
7143 */
7144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007145set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007146{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007147 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007148
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007149 if (has_mbyte)
7150 buf[(*mb_char2bytes)(c, buf)] = NUL;
7151 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007152 {
7153 buf[0] = c;
7154 buf[1] = NUL;
7155 }
7156 set_vim_var_string(VV_CHAR, buf, -1);
7157}
7158
7159/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007160 * Set v:count to "count" and v:count1 to "count1".
7161 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 */
7163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164set_vcount(
7165 long count,
7166 long count1,
7167 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007169 if (set_prevcount)
7170 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 vimvars[VV_COUNT].vv_nr = count;
7172 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
7175/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007176 * Save variables that might be changed as a side effect. Used when executing
7177 * a timer callback.
7178 */
7179 void
7180save_vimvars(vimvars_save_T *vvsave)
7181{
7182 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7183 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7184 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7185}
7186
7187/*
7188 * Restore variables saved by save_vimvars().
7189 */
7190 void
7191restore_vimvars(vimvars_save_T *vvsave)
7192{
7193 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7194 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7195 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7196}
7197
7198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 * Set string v: variable to a copy of "val".
7200 */
7201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202set_vim_var_string(
7203 int idx,
7204 char_u *val,
7205 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
Bram Moolenaara542c682016-01-31 16:28:04 +01007207 clear_tv(&vimvars[idx].vv_di.di_tv);
7208 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215}
7216
7217/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007218 * Set List v: variable to "val".
7219 */
7220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007222{
Bram Moolenaara542c682016-01-31 16:28:04 +01007223 clear_tv(&vimvars[idx].vv_di.di_tv);
7224 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007225 vimvars[idx].vv_list = val;
7226 if (val != NULL)
7227 ++val->lv_refcount;
7228}
7229
7230/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007231 * Set Dictionary v: variable to "val".
7232 */
7233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007235{
Bram Moolenaara542c682016-01-31 16:28:04 +01007236 clear_tv(&vimvars[idx].vv_di.di_tv);
7237 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007238 vimvars[idx].vv_dict = val;
7239 if (val != NULL)
7240 {
7241 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007242 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007243 }
7244}
7245
7246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 * Set v:register if needed.
7248 */
7249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 char_u regname;
7253
7254 if (c == 0 || c == ' ')
7255 regname = '"';
7256 else
7257 regname = c;
7258 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007259 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 set_vim_var_string(VV_REG, &regname, 1);
7261}
7262
7263/*
7264 * Get or set v:exception. If "oldval" == NULL, return the current value.
7265 * Otherwise, restore the value to "oldval" and return NULL.
7266 * Must always be called in pairs to save and restore v:exception! Does not
7267 * take care of memory allocations.
7268 */
7269 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 return NULL;
7277}
7278
7279/*
7280 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7281 * Otherwise, restore the value to "oldval" and return NULL.
7282 * Must always be called in pairs to save and restore v:throwpoint! Does not
7283 * take care of memory allocations.
7284 */
7285 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007286v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 return NULL;
7293}
7294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295/*
7296 * Set v:cmdarg.
7297 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7298 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7299 * Must always be called in pairs!
7300 */
7301 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007302set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 char_u *oldval;
7305 char_u *newval;
7306 unsigned len;
7307
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007309 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007311 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007313 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 }
7315
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007316 if (eap->force_bin == FORCE_BIN)
7317 len = 6;
7318 else if (eap->force_bin == FORCE_NOBIN)
7319 len = 8;
7320 else
7321 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007322
7323 if (eap->read_edit)
7324 len += 7;
7325
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007326 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007327 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007328 if (eap->force_enc != 0)
7329 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007330 if (eap->bad_char != 0)
7331 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007332
7333 newval = alloc(len + 1);
7334 if (newval == NULL)
7335 return NULL;
7336
7337 if (eap->force_bin == FORCE_BIN)
7338 sprintf((char *)newval, " ++bin");
7339 else if (eap->force_bin == FORCE_NOBIN)
7340 sprintf((char *)newval, " ++nobin");
7341 else
7342 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007343
7344 if (eap->read_edit)
7345 STRCAT(newval, " ++edit");
7346
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007347 if (eap->force_ff != 0)
7348 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007349 eap->force_ff == 'u' ? "unix"
7350 : eap->force_ff == 'd' ? "dos"
7351 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007352 if (eap->force_enc != 0)
7353 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7354 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007355 if (eap->bad_char == BAD_KEEP)
7356 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7357 else if (eap->bad_char == BAD_DROP)
7358 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7359 else if (eap->bad_char != 0)
7360 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007362 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365/*
7366 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007367 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007370get_var_tv(
7371 char_u *name,
7372 int len, /* length of "name" */
7373 typval_T *rettv, /* NULL when only checking existence */
7374 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7375 int verbose, /* may give error message */
7376 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 /* truncate the name, so that we can use strcmp() */
7384 cc = name[len];
7385 name[len] = NUL;
7386
7387 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 * Check for user-defined variables.
7389 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007390 v = find_var(name, NULL, no_autoload);
7391 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007393 tv = &v->di_tv;
7394 if (dip != NULL)
7395 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007400 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007401 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 ret = FAIL;
7403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007404 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 name[len] = cc;
7408
7409 return ret;
7410}
7411
7412/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007413 * Check if variable "name[len]" is a local variable or an argument.
7414 * If so, "*eval_lavars_used" is set to TRUE.
7415 */
7416 static void
7417check_vars(char_u *name, int len)
7418{
7419 int cc;
7420 char_u *varname;
7421 hashtab_T *ht;
7422
7423 if (eval_lavars_used == NULL)
7424 return;
7425
7426 /* truncate the name, so that we can use strcmp() */
7427 cc = name[len];
7428 name[len] = NUL;
7429
7430 ht = find_var_ht(name, &varname);
7431 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7432 {
7433 if (find_var(name, NULL, TRUE) != NULL)
7434 *eval_lavars_used = TRUE;
7435 }
7436
7437 name[len] = cc;
7438}
7439
7440/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007441 * Handle:
7442 * - expr[expr], expr[expr:expr] subscript
7443 * - ".name" lookup
7444 * - function call with Funcref variable: func(expr)
7445 * - method call: var->method()
7446 *
7447 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007448 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450handle_subscript(
7451 char_u **arg,
7452 typval_T *rettv,
7453 int evaluate, /* do more than finding the end */
7454 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007455{
7456 int ret = OK;
7457 dict_T *selfdict = NULL;
7458 char_u *s;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007459 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007460
Bram Moolenaar61343f02019-07-20 21:11:13 +02007461 // "." is ".name" lookup when we found a dict or when evaluating and
7462 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007463 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02007464 && (((**arg == '['
7465 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02007466 || (!evaluate
7467 && (*arg)[1] != '.'
7468 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007469 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007470 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007471 && !VIM_ISWHITE(*(*arg - 1)))
7472 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007473 {
7474 if (**arg == '(')
7475 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007476 partial_T *pt = NULL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02007477 funcexe_T funcexe;
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007478
Bram Moolenaard9fba312005-06-26 22:34:35 +00007479 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007480 if (evaluate)
7481 {
7482 functv = *rettv;
7483 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007484
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007485 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007486 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007487 {
7488 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007489 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007490 }
7491 else
7492 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007493 }
7494 else
7495 s = (char_u *)"";
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02007496
Bram Moolenaarac92e252019-08-03 21:58:38 +02007497 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02007498 funcexe.firstline = curwin->w_cursor.lnum;
7499 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02007500 funcexe.evaluate = evaluate;
7501 funcexe.partial = pt;
7502 funcexe.selfdict = selfdict;
7503 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007504
7505 /* Clear the funcref afterwards, so that deleting it while
7506 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007507 if (evaluate)
7508 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007509
7510 /* Stop the expression evaluation when immediately aborting on
7511 * error, or when an interrupt occurred or an exception was thrown
7512 * but not caught. */
7513 if (aborting())
7514 {
7515 if (ret == OK)
7516 clear_tv(rettv);
7517 ret = FAIL;
7518 }
7519 dict_unref(selfdict);
7520 selfdict = NULL;
7521 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007522 else if (**arg == '-')
7523 {
7524 if (eval_method(arg, rettv, evaluate, verbose) == FAIL)
7525 {
7526 clear_tv(rettv);
7527 ret = FAIL;
7528 }
7529 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007530 else /* **arg == '[' || **arg == '.' */
7531 {
7532 dict_unref(selfdict);
7533 if (rettv->v_type == VAR_DICT)
7534 {
7535 selfdict = rettv->vval.v_dict;
7536 if (selfdict != NULL)
7537 ++selfdict->dv_refcount;
7538 }
7539 else
7540 selfdict = NULL;
7541 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7542 {
7543 clear_tv(rettv);
7544 ret = FAIL;
7545 }
7546 }
7547 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007548
Bram Moolenaar1d429612016-05-24 15:44:17 +02007549 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7550 * Don't do this when "Func" is already a partial that was bound
7551 * explicitly (pt_auto is FALSE). */
7552 if (selfdict != NULL
7553 && (rettv->v_type == VAR_FUNC
7554 || (rettv->v_type == VAR_PARTIAL
7555 && (rettv->vval.v_partial->pt_auto
7556 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007557 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007558
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007559 dict_unref(selfdict);
7560 return ret;
7561}
7562
7563/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007564 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 * value).
7566 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007567 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007570 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571}
7572
7573/*
7574 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 * The string "s" must have been allocated, it is consumed.
7576 * Return NULL for out of memory, the variable otherwise.
7577 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007579alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583 rettv = alloc_tv();
7584 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586 rettv->v_type = VAR_STRING;
7587 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 }
7589 else
7590 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592}
7593
7594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007598free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
7600 if (varp != NULL)
7601 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 switch (varp->v_type)
7603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007605 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007606 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007607 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608 vim_free(varp->vval.v_string);
7609 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007610 case VAR_PARTIAL:
7611 partial_unref(varp->vval.v_partial);
7612 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007613 case VAR_BLOB:
7614 blob_unref(varp->vval.v_blob);
7615 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 case VAR_LIST:
7617 list_unref(varp->vval.v_list);
7618 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007619 case VAR_DICT:
7620 dict_unref(varp->vval.v_dict);
7621 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007622 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007623#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007624 job_unref(varp->vval.v_job);
7625 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007626#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007627 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007628#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007629 channel_unref(varp->vval.v_channel);
7630 break;
7631#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007632 case VAR_NUMBER:
7633 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007634 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007635 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007636 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 vim_free(varp);
7639 }
7640}
7641
7642/*
7643 * Free the memory for a variable value and set the value to NULL or 0.
7644 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007646clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 if (varp != NULL)
7649 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007653 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007654 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007655 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007656 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007657 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007658 case VAR_PARTIAL:
7659 partial_unref(varp->vval.v_partial);
7660 varp->vval.v_partial = NULL;
7661 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007662 case VAR_BLOB:
7663 blob_unref(varp->vval.v_blob);
7664 varp->vval.v_blob = NULL;
7665 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007666 case VAR_LIST:
7667 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 case VAR_DICT:
7671 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007674 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007675 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676 varp->vval.v_number = 0;
7677 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007678 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007679#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007680 varp->vval.v_float = 0.0;
7681 break;
7682#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007683 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007684#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007685 job_unref(varp->vval.v_job);
7686 varp->vval.v_job = NULL;
7687#endif
7688 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007689 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007690#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007691 channel_unref(varp->vval.v_channel);
7692 varp->vval.v_channel = NULL;
7693#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 case VAR_UNKNOWN:
7695 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007697 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699}
7700
7701/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702 * Set the value of a variable to NULL without freeing items.
7703 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706{
7707 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007709}
7710
7711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 * Get the number value of a variable.
7713 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007714 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007715 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007716 * caller of incompatible types: it sets *denote to TRUE if "denote"
7717 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007719 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007720tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007722 int error = FALSE;
7723
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007724 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007725}
7726
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007727 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007728tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007729{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007730 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007732 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007734 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007735 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007736 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007737#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007738 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007739 break;
7740#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007741 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007742 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007743 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007744 break;
7745 case VAR_STRING:
7746 if (varp->vval.v_string != NULL)
7747 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007748 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007750 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007751 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007752 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007753 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007754 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007755 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007756 case VAR_SPECIAL:
7757 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7758 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007761 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007762 break;
7763#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007764 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007765#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007766 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007767 break;
7768#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007769 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007770 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007771 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007772 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007773 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007776 if (denote == NULL) /* useful for values that must be unsigned */
7777 n = -1;
7778 else
7779 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007783#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007785tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007786{
7787 switch (varp->v_type)
7788 {
7789 case VAR_NUMBER:
7790 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007791 case VAR_FLOAT:
7792 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007793 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007794 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007795 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007796 break;
7797 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007798 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007799 break;
7800 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007801 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007802 break;
7803 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007804 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007805 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007806 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007807 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007808 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007809 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007810# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007811 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812 break;
7813# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007814 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007815# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007816 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007817 break;
7818# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007819 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007820 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007821 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007822 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007823 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007824 break;
7825 }
7826 return 0;
7827}
7828#endif
7829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 * Get the string value of a variable.
7832 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007833 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7834 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 * If the String variable has never been set, return an empty string.
7836 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007837 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007840 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007841tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 static char_u mybuf[NUMBUFLEN];
7844
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007845 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007848 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007849tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007851 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852
7853 return res != NULL ? res : (char_u *)"";
7854}
7855
Bram Moolenaar7d647822014-04-05 21:28:56 +02007856/*
7857 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7858 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007859 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007860tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007861{
7862 static char_u mybuf[NUMBUFLEN];
7863
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007864 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007865}
7866
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007867 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007868tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007872 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007873 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007874 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 return buf;
7876 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007877 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007878 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 break;
7880 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007881 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007882 break;
7883 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007884 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007887#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889 break;
7890#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 case VAR_STRING:
7892 if (varp->vval.v_string != NULL)
7893 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007894 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007895 case VAR_SPECIAL:
7896 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7897 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007898 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007899 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007900 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007901 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007902#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007903 {
7904 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007905 char *status;
7906
7907 if (job == NULL)
7908 return (char_u *)"no process";
7909 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007910 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007911 : "run";
7912# ifdef UNIX
7913 vim_snprintf((char *)buf, NUMBUFLEN,
7914 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007915# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007916 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007917 "process %ld %s",
7918 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007919 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007920# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007921 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007922 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7923# endif
7924 return buf;
7925 }
7926#endif
7927 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007928 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007929#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007930 {
7931 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007932 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007933
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007934 if (channel == NULL)
7935 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7936 else
7937 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007938 "channel %d %s", channel->ch_id, status);
7939 return buf;
7940 }
7941#endif
7942 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007943 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007944 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007947 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948}
7949
7950/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007951 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7952 * string() on Dict, List, etc.
7953 */
7954 char_u *
7955tv_stringify(typval_T *varp, char_u *buf)
7956{
7957 if (varp->v_type == VAR_LIST
7958 || varp->v_type == VAR_DICT
7959 || varp->v_type == VAR_FUNC
7960 || varp->v_type == VAR_PARTIAL
7961 || varp->v_type == VAR_FLOAT)
7962 {
7963 typval_T tmp;
7964
7965 f_string(varp, &tmp);
7966 tv_get_string_buf(&tmp, buf);
7967 clear_tv(varp);
7968 *varp = tmp;
7969 return tmp.vval.v_string;
7970 }
7971 return tv_get_string_buf(varp, buf);
7972}
7973
7974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 * Find variable "name" in the list of variables.
7976 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007977 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007978 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007981 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007982find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007986 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987
Bram Moolenaara7043832005-01-21 11:56:39 +00007988 ht = find_var_ht(name, &varname);
7989 if (htp != NULL)
7990 *htp = ht;
7991 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007993 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7994 if (ret != NULL)
7995 return ret;
7996
7997 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007998 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999}
8000
8001/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02008002 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00008003 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008006find_var_in_ht(
8007 hashtab_T *ht,
8008 int htname,
8009 char_u *varname,
8010 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00008011{
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 hashitem_T *hi;
8013
8014 if (*varname == NUL)
8015 {
8016 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02008017 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008019 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 case 'g': return &globvars_var;
8021 case 'v': return &vimvars_var;
8022 case 'b': return &curbuf->b_bufvar;
8023 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008024 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008025 case 'l': return get_funccal_local_var();
8026 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00008027 }
8028 return NULL;
8029 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008030
8031 hi = hash_find(ht, varname);
8032 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008033 {
8034 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008035 * worked find the variable again. Don't auto-load a script if it was
8036 * loaded already, otherwise it would be loaded every time when
8037 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008038 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008039 {
8040 /* Note: script_autoload() may make "hi" invalid. It must either
8041 * be obtained again or not used. */
8042 if (!script_autoload(varname, FALSE) || aborting())
8043 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008044 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008045 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008046 if (HASHITEM_EMPTY(hi))
8047 return NULL;
8048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008050}
8051
8052/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02008054 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00008055 * Set "varname" to the start of name without ':'.
8056 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008057 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008058find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008060 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008061 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008062
Bram Moolenaar73627d02015-08-11 15:46:09 +02008063 if (name[0] == NUL)
8064 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (name[1] != ':')
8066 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008067 /* The name must not start with a colon or #. */
8068 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 return NULL;
8070 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00008071
Bram Moolenaard2e716e2019-04-20 14:39:52 +02008072 // "version" is "v:version" in all scopes if scriptversion < 3.
8073 // Same for a few other variables marked with VV_COMPAT.
8074 if (current_sctx.sc_version < 3)
8075 {
8076 hi = hash_find(&compat_hashtab, name);
8077 if (!HASHITEM_EMPTY(hi))
8078 return &compat_hashtab;
8079 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00008080
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008081 ht = get_funccal_local_ht();
8082 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008084 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
8086 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008087 if (*name == 'g') /* global variable */
8088 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02008089 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008090 if (vim_strchr(name + 2, ':') != NULL
8091 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008092 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008094 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008096 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008097 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008098 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 if (*name == 'v') /* v: variable */
8100 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008101 if (*name == 'a') /* a: function argument */
8102 return get_funccal_args_ht();
8103 if (*name == 'l') /* l: local function variable */
8104 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008106 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
8107 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 return NULL;
8109}
8110
8111/*
8112 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008113 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 * Returns NULL when it doesn't exist.
8115 */
8116 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008117get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118{
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008121 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (v == NULL)
8123 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008124 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125}
8126
8127/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 * sourcing this script and when executing functions defined in the script.
8130 */
8131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008132new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
Bram Moolenaara7043832005-01-21 11:56:39 +00008134 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 hashtab_T *ht;
8136 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008137
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8139 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008140 /* Re-allocating ga_data means that an ht_array pointing to
8141 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008143 for (i = 1; i <= ga_scripts.ga_len; ++i)
8144 {
8145 ht = &SCRIPT_VARS(i);
8146 if (ht->ht_mask == HT_INIT_SIZE - 1)
8147 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008148 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008150 }
8151
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 while (ga_scripts.ga_len < id)
8153 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008154 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008155 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008156 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 }
8159 }
8160}
8161
8162/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8164 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 */
8166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008167init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008170 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008171 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008172 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008173 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 dict_var->di_tv.vval.v_dict = dict;
8175 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008176 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8178 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179}
8180
8181/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008182 * Unreference a dictionary initialized by init_var_dict().
8183 */
8184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008185unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008186{
8187 /* Now the dict needs to be freed if no one else is using it, go back to
8188 * normal reference counting. */
8189 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8190 dict_unref(dict);
8191}
8192
8193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 * Frees all allocated variables and the value they contain.
8196 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 */
8198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008199vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008200{
8201 vars_clear_ext(ht, TRUE);
8202}
8203
8204/*
8205 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8206 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008208vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209{
Bram Moolenaara7043832005-01-21 11:56:39 +00008210 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 hashitem_T *hi;
8212 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
Bram Moolenaar33570922005-01-25 22:26:29 +00008214 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008215 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008216 for (hi = ht->ht_array; todo > 0; ++hi)
8217 {
8218 if (!HASHITEM_EMPTY(hi))
8219 {
8220 --todo;
8221
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008223 * ht_array might change then. hash_clear() takes care of it
8224 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 v = HI2DI(hi);
8226 if (free_val)
8227 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008228 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008230 }
8231 }
8232 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008233 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234}
8235
Bram Moolenaara7043832005-01-21 11:56:39 +00008236/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008237 * Delete a variable from hashtab "ht" at item "hi".
8238 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008241delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242{
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008244
8245 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008246 clear_tv(&di->di_tv);
8247 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248}
8249
8250/*
8251 * List the value of one internal variable.
8252 */
8253 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008254list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008256 char_u *tofree;
8257 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008258 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008260 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008262 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008263 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264}
8265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008267list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008268 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008269 char_u *name,
8270 int type,
8271 char_u *string,
8272 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
Bram Moolenaar31859182007-08-14 20:41:13 +00008274 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8275 msg_start();
8276 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008278 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 msg_putchar(' ');
8280 msg_advance(22);
8281 if (type == VAR_NUMBER)
8282 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008283 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 msg_putchar('*');
8285 else if (type == VAR_LIST)
8286 {
8287 msg_putchar('[');
8288 if (*string == '[')
8289 ++string;
8290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008291 else if (type == VAR_DICT)
8292 {
8293 msg_putchar('{');
8294 if (*string == '{')
8295 ++string;
8296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 else
8298 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008301
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008302 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008303 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008304 if (*first)
8305 {
8306 msg_clr_eos();
8307 *first = FALSE;
8308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309}
8310
8311/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 * If the variable already exists, the value is updated.
8314 * Otherwise the variable is created.
8315 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008317set_var(
8318 char_u *name,
8319 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008320 int copy) // make copy of value in "tv"
8321{
8322 set_var_const(name, tv, copy, FALSE);
8323}
8324
8325/*
8326 * Set variable "name" to value in "tv".
8327 * If the variable already exists and "is_const" is FALSE the value is updated.
8328 * Otherwise the variable is created.
8329 */
8330 static void
8331set_var_const(
8332 char_u *name,
8333 typval_T *tv,
8334 int copy, // make copy of value in "tv"
8335 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008341 ht = find_var_ht(name, &varname);
8342 if (ht == NULL || *varname == NUL)
8343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008344 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008345 return;
8346 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008347 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008348
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008349 /* Search in parent scope which is possible to reference from lambda */
8350 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008351 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008352
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008353 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8354 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008355 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008356
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008359 if (is_const)
8360 {
8361 emsg(_(e_cannot_mod));
8362 return;
8363 }
8364
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008366 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008367 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008369
8370 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008371 * Handle setting internal v: variables separately where needed to
8372 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 */
8374 if (ht == &vimvarht)
8375 {
8376 if (v->di_tv.v_type == VAR_STRING)
8377 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008378 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008380 {
8381 char_u *val = tv_get_string(tv);
8382
8383 // Careful: when assigning to v:errmsg and tv_get_string()
8384 // causes an error message the variable will alrady be set.
8385 if (v->di_tv.vval.v_string == NULL)
8386 v->di_tv.vval.v_string = vim_strsave(val);
8387 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 else
8389 {
8390 /* Take over the string to avoid an extra alloc/free. */
8391 v->di_tv.vval.v_string = tv->vval.v_string;
8392 tv->vval.v_string = NULL;
8393 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008394 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008395 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008396 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008397 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008398 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008399 if (STRCMP(varname, "searchforward") == 0)
8400 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008401#ifdef FEAT_SEARCH_EXTRA
8402 else if (STRCMP(varname, "hlsearch") == 0)
8403 {
8404 no_hlsearch = !v->di_tv.vval.v_number;
8405 redraw_all_later(SOME_VALID);
8406 }
8407#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008408 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008409 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008410 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008412 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008413 return;
8414 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 }
8416
8417 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419 else /* add a new variable */
8420 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008421 // Can't add "v:" or "a:" variable.
8422 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008424 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008425 return;
8426 }
8427
Bram Moolenaar31b81602019-02-10 22:14:27 +01008428 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008429 if (!valid_varname(varname))
8430 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008431
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008432 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008433 if (v == NULL)
8434 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008435 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008438 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 return;
8440 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008441 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008442 if (is_const)
8443 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008445
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008446 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008447 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008448 else
8449 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008451 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008452 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008453 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008454
8455 if (is_const)
8456 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008459/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008460 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008461 * Also give an error message.
8462 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008464var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008465{
8466 if (flags & DI_FLAGS_RO)
8467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008468 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008469 return TRUE;
8470 }
8471 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008473 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 return TRUE;
8475 }
8476 return FALSE;
8477}
8478
8479/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008480 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8481 * Also give an error message.
8482 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008484var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008485{
8486 if (flags & DI_FLAGS_FIX)
8487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008488 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008489 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008490 return TRUE;
8491 }
8492 return FALSE;
8493}
8494
8495/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008496 * Check if a funcref is assigned to a valid variable name.
8497 * Return TRUE and give an error if not.
8498 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008500var_check_func_name(
8501 char_u *name, /* points to start of variable name */
8502 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008503{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008504 /* Allow for w: b: s: and t:. */
8505 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008506 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8507 ? name[2] : name[0]))
8508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008509 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008510 name);
8511 return TRUE;
8512 }
8513 /* Don't allow hiding a function. When "v" is not NULL we might be
8514 * assigning another function to the same var, the type is checked
8515 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008516 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008518 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008519 name);
8520 return TRUE;
8521 }
8522 return FALSE;
8523}
8524
8525/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008526 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008527 * Also give an error message, using "name" or _("name") when use_gettext is
8528 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008529 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008530 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008531var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008532{
8533 if (lock & VAR_LOCKED)
8534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008535 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008536 name == NULL ? (char_u *)_("Unknown")
8537 : use_gettext ? (char_u *)_(name)
8538 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008539 return TRUE;
8540 }
8541 if (lock & VAR_FIXED)
8542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008543 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008544 name == NULL ? (char_u *)_("Unknown")
8545 : use_gettext ? (char_u *)_(name)
8546 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008547 return TRUE;
8548 }
8549 return FALSE;
8550}
8551
8552/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008553 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8554 * Also give an error message, using "name" or _("name") when use_gettext is
8555 * TRUE.
8556 */
8557 static int
8558tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8559{
8560 int lock = 0;
8561
8562 switch (tv->v_type)
8563 {
8564 case VAR_BLOB:
8565 if (tv->vval.v_blob != NULL)
8566 lock = tv->vval.v_blob->bv_lock;
8567 break;
8568 case VAR_LIST:
8569 if (tv->vval.v_list != NULL)
8570 lock = tv->vval.v_list->lv_lock;
8571 break;
8572 case VAR_DICT:
8573 if (tv->vval.v_dict != NULL)
8574 lock = tv->vval.v_dict->dv_lock;
8575 break;
8576 default:
8577 break;
8578 }
8579 return var_check_lock(tv->v_lock, name, use_gettext)
8580 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8581}
8582
8583/*
8584 * Check if a variable name is valid.
8585 * Return FALSE and give an error if not.
8586 */
8587 int
8588valid_varname(char_u *varname)
8589{
8590 char_u *p;
8591
8592 for (p = varname; *p != NUL; ++p)
8593 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8594 && *p != AUTOLOAD_CHAR)
8595 {
8596 semsg(_(e_illvar), varname);
8597 return FALSE;
8598 }
8599 return TRUE;
8600}
8601
8602/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008604 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008605 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008606 * It is OK for "from" and "to" to point to the same item. This is used to
8607 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008610copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008612 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008613 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 switch (from->v_type)
8615 {
8616 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008617 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008618 to->vval.v_number = from->vval.v_number;
8619 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008620 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008621#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008622 to->vval.v_float = from->vval.v_float;
8623 break;
8624#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008625 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008626#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008627 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008628 if (to->vval.v_job != NULL)
8629 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008630 break;
8631#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008632 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008634 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008635 if (to->vval.v_channel != NULL)
8636 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008637 break;
8638#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008639 case VAR_STRING:
8640 case VAR_FUNC:
8641 if (from->vval.v_string == NULL)
8642 to->vval.v_string = NULL;
8643 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008644 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008645 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008646 if (from->v_type == VAR_FUNC)
8647 func_ref(to->vval.v_string);
8648 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008649 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008650 case VAR_PARTIAL:
8651 if (from->vval.v_partial == NULL)
8652 to->vval.v_partial = NULL;
8653 else
8654 {
8655 to->vval.v_partial = from->vval.v_partial;
8656 ++to->vval.v_partial->pt_refcount;
8657 }
8658 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008659 case VAR_BLOB:
8660 if (from->vval.v_blob == NULL)
8661 to->vval.v_blob = NULL;
8662 else
8663 {
8664 to->vval.v_blob = from->vval.v_blob;
8665 ++to->vval.v_blob->bv_refcount;
8666 }
8667 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 case VAR_LIST:
8669 if (from->vval.v_list == NULL)
8670 to->vval.v_list = NULL;
8671 else
8672 {
8673 to->vval.v_list = from->vval.v_list;
8674 ++to->vval.v_list->lv_refcount;
8675 }
8676 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008677 case VAR_DICT:
8678 if (from->vval.v_dict == NULL)
8679 to->vval.v_dict = NULL;
8680 else
8681 {
8682 to->vval.v_dict = from->vval.v_dict;
8683 ++to->vval.v_dict->dv_refcount;
8684 }
8685 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008686 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008687 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008688 break;
8689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008693 * Make a copy of an item.
8694 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008695 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8696 * reference to an already copied list/dict can be used.
8697 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008698 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008700item_copy(
8701 typval_T *from,
8702 typval_T *to,
8703 int deep,
8704 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008705{
8706 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008707 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008708
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008711 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008712 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 }
8714 ++recurse;
8715
8716 switch (from->v_type)
8717 {
8718 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 case VAR_STRING:
8721 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008722 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008723 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008724 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008725 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008726 copy_tv(from, to);
8727 break;
8728 case VAR_LIST:
8729 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008730 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008731 if (from->vval.v_list == NULL)
8732 to->vval.v_list = NULL;
8733 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8734 {
8735 /* use the copy made earlier */
8736 to->vval.v_list = from->vval.v_list->lv_copylist;
8737 ++to->vval.v_list->lv_refcount;
8738 }
8739 else
8740 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8741 if (to->vval.v_list == NULL)
8742 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008743 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008744 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008745 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008746 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 case VAR_DICT:
8748 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008749 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008750 if (from->vval.v_dict == NULL)
8751 to->vval.v_dict = NULL;
8752 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8753 {
8754 /* use the copy made earlier */
8755 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8756 ++to->vval.v_dict->dv_refcount;
8757 }
8758 else
8759 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8760 if (to->vval.v_dict == NULL)
8761 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008762 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008763 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008764 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008765 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 }
8767 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008768 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008769}
8770
8771/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 * This function is used by f_input() and f_inputdialog() functions. The third
8773 * argument to f_input() specifies the type of completion to use at the
8774 * prompt. The third argument to f_inputdialog() specifies the value to return
8775 * when the user cancels the prompt.
8776 */
8777 void
8778get_user_input(
8779 typval_T *argvars,
8780 typval_T *rettv,
8781 int inputdialog,
8782 int secret)
8783{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008784 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 char_u *p = NULL;
8786 int c;
8787 char_u buf[NUMBUFLEN];
8788 int cmd_silent_save = cmd_silent;
8789 char_u *defstr = (char_u *)"";
8790 int xp_type = EXPAND_NOTHING;
8791 char_u *xp_arg = NULL;
8792
8793 rettv->v_type = VAR_STRING;
8794 rettv->vval.v_string = NULL;
8795
8796#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008797 /* While starting up, there is no place to enter text. When running tests
8798 * with --not-a-term we assume feedkeys() will be used. */
8799 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 return;
8801#endif
8802
8803 cmd_silent = FALSE; /* Want to see the prompt. */
8804 if (prompt != NULL)
8805 {
8806 /* Only the part of the message after the last NL is considered as
8807 * prompt for the command line */
8808 p = vim_strrchr(prompt, '\n');
8809 if (p == NULL)
8810 p = prompt;
8811 else
8812 {
8813 ++p;
8814 c = *p;
8815 *p = NUL;
8816 msg_start();
8817 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008818 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 msg_didout = FALSE;
8820 msg_starthere();
8821 *p = c;
8822 }
8823 cmdline_row = msg_row;
8824
8825 if (argvars[1].v_type != VAR_UNKNOWN)
8826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008827 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 if (defstr != NULL)
8829 stuffReadbuffSpec(defstr);
8830
8831 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8832 {
8833 char_u *xp_name;
8834 int xp_namelen;
8835 long argt;
8836
8837 /* input() with a third argument: completion */
8838 rettv->vval.v_string = NULL;
8839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008840 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 if (xp_name == NULL)
8842 return;
8843
8844 xp_namelen = (int)STRLEN(xp_name);
8845
8846 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8847 &xp_arg) == FAIL)
8848 return;
8849 }
8850 }
8851
8852 if (defstr != NULL)
8853 {
8854 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 ex_normal_busy = 0;
8857 rettv->vval.v_string =
8858 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8859 xp_type, xp_arg);
8860 ex_normal_busy = save_ex_normal_busy;
8861 }
8862 if (inputdialog && rettv->vval.v_string == NULL
8863 && argvars[1].v_type != VAR_UNKNOWN
8864 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008865 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866 &argvars[2], buf));
8867
8868 vim_free(xp_arg);
8869
8870 /* since the user typed this, no need to wait for return */
8871 need_wait_return = FALSE;
8872 msg_didout = FALSE;
8873 }
8874 cmd_silent = cmd_silent_save;
8875}
8876
8877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 * ":echo expr1 ..." print each argument separated with a space, add a
8879 * newline at the end.
8880 * ":echon expr1 ..." print each argument plain.
8881 */
8882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008883ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008887 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 char_u *p;
8889 int needclr = TRUE;
8890 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008891 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008892 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008893 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894
8895 if (eap->skip)
8896 ++emsg_skip;
8897 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899 /* If eval1() causes an error message the text from the command may
8900 * still need to be cleared. E.g., "echo 22,44". */
8901 need_clr_eos = needclr;
8902
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 {
8906 /*
8907 * Report the invalid expression unless the expression evaluation
8908 * has been cancelled due to an aborting error, an interrupt, or an
8909 * exception.
8910 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008911 if (!aborting() && did_emsg == did_emsg_before
8912 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008913 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008914 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 break;
8916 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008917 need_clr_eos = FALSE;
8918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 if (!eap->skip)
8920 {
8921 if (atstart)
8922 {
8923 atstart = FALSE;
8924 /* Call msg_start() after eval1(), evaluating the expression
8925 * may cause a message to appear. */
8926 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008927 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008928 /* Mark the saved text as finishing the line, so that what
8929 * follows is displayed on a new line when scrolling back
8930 * at the more prompt. */
8931 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008936 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008937 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008938 if (p != NULL)
8939 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008941 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008943 if (*p != TAB && needclr)
8944 {
8945 /* remove any text still there from the command */
8946 msg_clr_eos();
8947 needclr = FALSE;
8948 }
8949 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008952 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008953 if (has_mbyte)
8954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008955 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008956
8957 (void)msg_outtrans_len_attr(p, i, echo_attr);
8958 p += i - 1;
8959 }
8960 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008961 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 arg = skipwhite(arg);
8968 }
8969 eap->nextcmd = check_nextcmd(arg);
8970
8971 if (eap->skip)
8972 --emsg_skip;
8973 else
8974 {
8975 /* remove text that may still be there from the command */
8976 if (needclr)
8977 msg_clr_eos();
8978 if (eap->cmdidx == CMD_echo)
8979 msg_end();
8980 }
8981}
8982
8983/*
8984 * ":echohl {name}".
8985 */
8986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008987ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008989 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
8992/*
8993 * ":execute expr1 ..." execute the result of an expression.
8994 * ":echomsg expr1 ..." Print a message
8995 * ":echoerr expr1 ..." Print an error
8996 * Each gets spaces around each argument and a newline at the end for
8997 * echo commands
8998 */
8999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009000ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 int ret = OK;
9005 char_u *p;
9006 garray_T ga;
9007 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01009008 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
9010 ga_init2(&ga, 1, 80);
9011
9012 if (eap->skip)
9013 ++emsg_skip;
9014 while (*arg != NUL && *arg != '|' && *arg != '\n')
9015 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01009016 ret = eval1_emsg(&arg, &rettv, !eap->skip);
9017 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020 if (!eap->skip)
9021 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009022 char_u buf[NUMBUFLEN];
9023
9024 if (eap->cmdidx == CMD_execute)
9025 p = tv_get_string_buf(&rettv, buf);
9026 else
9027 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 len = (int)STRLEN(p);
9029 if (ga_grow(&ga, len + 2) == FAIL)
9030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 ret = FAIL;
9033 break;
9034 }
9035 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 ga.ga_len += len;
9039 }
9040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 arg = skipwhite(arg);
9043 }
9044
9045 if (ret != FAIL && ga.ga_data != NULL)
9046 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009047 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
9048 {
9049 /* Mark the already saved text as finishing the line, so that what
9050 * follows is displayed on a new line when scrolling back at the
9051 * more prompt. */
9052 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009053 }
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009056 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009057 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009058 out_flush();
9059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 else if (eap->cmdidx == CMD_echoerr)
9061 {
9062 /* We don't want to abort following commands, restore did_emsg. */
9063 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009064 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (!force_abort)
9066 did_emsg = save_did_emsg;
9067 }
9068 else if (eap->cmdidx == CMD_execute)
9069 do_cmdline((char_u *)ga.ga_data,
9070 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9071 }
9072
9073 ga_clear(&ga);
9074
9075 if (eap->skip)
9076 --emsg_skip;
9077
9078 eap->nextcmd = check_nextcmd(arg);
9079}
9080
9081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 * Find window specified by "vp" in tabpage "tp".
9083 */
9084 win_T *
9085find_win_by_nr(
9086 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009087 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009090 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 if (nr < 0)
9093 return NULL;
9094 if (nr == 0)
9095 return curwin;
9096
Bram Moolenaar29323592016-07-24 22:04:11 +02009097 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
9098 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 if (nr >= LOWEST_WIN_ID)
9100 {
9101 if (wp->w_id == nr)
9102 return wp;
9103 }
9104 else if (--nr <= 0)
9105 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02009106 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009108 {
9109#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009110 // check tab-local popup windows
9111 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009112 if (wp->w_id == nr)
9113 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009114 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009115 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9116 if (wp->w_id == nr)
9117 return wp;
9118#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009120 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122}
9123
9124/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009125 * Find a window: When using a Window ID in any tab page, when using a number
9126 * in the current tab page.
9127 */
9128 win_T *
9129find_win_by_nr_or_id(typval_T *vp)
9130{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009131 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009132
9133 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009134 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009135 return find_win_by_nr(vp, NULL);
9136}
9137
9138/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009140 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141 */
9142 win_T *
9143find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009144 typval_T *wvp, // VAR_UNKNOWN for current window
9145 typval_T *tvp, // VAR_UNKNOWN for current tab page
9146 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147{
9148 win_T *wp = NULL;
9149 tabpage_T *tp = NULL;
9150 long n;
9151
9152 if (wvp->v_type != VAR_UNKNOWN)
9153 {
9154 if (tvp->v_type != VAR_UNKNOWN)
9155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009156 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 if (n >= 0)
9158 tp = find_tabpage(n);
9159 }
9160 else
9161 tp = curtab;
9162
9163 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009164 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009166 if (wp == NULL && wvp->v_type == VAR_NUMBER
9167 && wvp->vval.v_number != -1)
9168 // A window with the specified number is not found
9169 tp = NULL;
9170 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 }
9172 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009173 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009175 tp = curtab;
9176 }
9177
9178 if (ptp != NULL)
9179 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180
9181 return wp;
9182}
9183
9184/*
9185 * getwinvar() and gettabwinvar()
9186 */
9187 void
9188getwinvar(
9189 typval_T *argvars,
9190 typval_T *rettv,
9191 int off) /* 1 for gettabwinvar() */
9192{
9193 win_T *win;
9194 char_u *varname;
9195 dictitem_T *v;
9196 tabpage_T *tp = NULL;
9197 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 win_T *oldcurwin;
9199 tabpage_T *oldtabpage;
9200 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009203 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 else
9205 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009207 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208 ++emsg_off;
9209
9210 rettv->v_type = VAR_STRING;
9211 rettv->vval.v_string = NULL;
9212
9213 if (win != NULL && varname != NULL)
9214 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215 /* Set curwin to be our win, temporarily. Also set the tabpage,
9216 * otherwise the window is not valid. Only do this when needed,
9217 * autocommands get blocked. */
9218 need_switch_win = !(tp == curtab && win == curwin);
9219 if (!need_switch_win
9220 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009222 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009223 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009224 if (varname[1] == NUL)
9225 {
9226 /* get all window-local options in a dict */
9227 dict_T *opts = get_winbuf_options(FALSE);
9228
9229 if (opts != NULL)
9230 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009231 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009232 done = TRUE;
9233 }
9234 }
9235 else if (get_option_tv(&varname, rettv, 1) == OK)
9236 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237 done = TRUE;
9238 }
9239 else
9240 {
9241 /* Look up the variable. */
9242 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9243 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9244 varname, FALSE);
9245 if (v != NULL)
9246 {
9247 copy_tv(&v->di_tv, rettv);
9248 done = TRUE;
9249 }
9250 }
9251 }
9252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009253 if (need_switch_win)
9254 /* restore previous notion of curwin */
9255 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256 }
9257
9258 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9259 /* use the default return value */
9260 copy_tv(&argvars[off + 2], rettv);
9261
9262 --emsg_off;
9263}
9264
9265/*
9266 * "setwinvar()" and "settabwinvar()" functions
9267 */
9268 void
9269setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9270{
9271 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 win_T *save_curwin;
9273 tabpage_T *save_curtab;
9274 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 char_u *varname, *winvarname;
9276 typval_T *varp;
9277 char_u nbuf[NUMBUFLEN];
9278 tabpage_T *tp = NULL;
9279
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009280 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281 return;
9282
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009284 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285 else
9286 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009288 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 varp = &argvars[off + 2];
9290
9291 if (win != NULL && varname != NULL && varp != NULL)
9292 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 need_switch_win = !(tp == curtab && win == curwin);
9294 if (!need_switch_win
9295 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009296 {
9297 if (*varname == '&')
9298 {
9299 long numval;
9300 char_u *strval;
9301 int error = FALSE;
9302
9303 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009304 numval = (long)tv_get_number_chk(varp, &error);
9305 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 if (!error && strval != NULL)
9307 set_option_value(varname, numval, strval, OPT_LOCAL);
9308 }
9309 else
9310 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009311 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312 if (winvarname != NULL)
9313 {
9314 STRCPY(winvarname, "w:");
9315 STRCPY(winvarname + 2, varname);
9316 set_var(winvarname, varp, TRUE);
9317 vim_free(winvarname);
9318 }
9319 }
9320 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321 if (need_switch_win)
9322 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 }
9324}
9325
9326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9328 * "arg" points to the "&" or '+' when called, to "option" when returning.
9329 * Returns NULL when no option name found. Otherwise pointer to the char
9330 * after the option name.
9331 */
9332 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009333find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
9335 char_u *p = *arg;
9336
9337 ++p;
9338 if (*p == 'g' && p[1] == ':')
9339 {
9340 *opt_flags = OPT_GLOBAL;
9341 p += 2;
9342 }
9343 else if (*p == 'l' && p[1] == ':')
9344 {
9345 *opt_flags = OPT_LOCAL;
9346 p += 2;
9347 }
9348 else
9349 *opt_flags = 0;
9350
9351 if (!ASCII_ISALPHA(*p))
9352 return NULL;
9353 *arg = p;
9354
9355 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9356 p += 4; /* termcap option */
9357 else
9358 while (ASCII_ISALPHA(*p))
9359 ++p;
9360 return p;
9361}
9362
9363/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009364 * Return the autoload script name for a function or variable name.
9365 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009366 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009368 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009369autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009370{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009371 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009372 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009373
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009374 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009375 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009376 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009377 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009378 STRCPY(scriptname, "autoload/");
9379 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009380 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9381 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009382 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009383 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009384 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009385}
9386
9387/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009388 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009389 * Return TRUE if a package was loaded.
9390 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392script_autoload(
9393 char_u *name,
9394 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009395{
9396 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009397 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009398 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009399 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009400
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009401 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009402 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009403 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009404 return FALSE;
9405
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009406 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009407
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009408 /* Find the name in the list of previously loaded package names. Skip
9409 * "autoload/", it's always the same. */
9410 for (i = 0; i < ga_loaded.ga_len; ++i)
9411 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9412 break;
9413 if (!reload && i < ga_loaded.ga_len)
9414 ret = FALSE; /* was loaded already */
9415 else
9416 {
9417 /* Remember the name if it wasn't loaded already. */
9418 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9419 {
9420 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9421 tofree = NULL;
9422 }
9423
9424 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009425 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009426 ret = TRUE;
9427 }
9428
9429 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009430 return ret;
9431}
9432
Bram Moolenaar661b1822005-07-28 22:36:45 +00009433/*
9434 * Display script name where an item was last set.
9435 * Should only be invoked when 'verbose' is non-zero.
9436 */
9437 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009438last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009439{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009440 char_u *p;
9441
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009442 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009443 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009444 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009445 if (p != NULL)
9446 {
9447 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009448 msg_puts(_("\n\tLast set from "));
9449 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009450 if (script_ctx.sc_lnum > 0)
9451 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009452 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009453 msg_outnum((long)script_ctx.sc_lnum);
9454 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009455 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009456 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009457 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009458 }
9459}
9460
Bram Moolenaar61be3762019-03-19 23:04:17 +01009461/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009462 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9463 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009464 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009466reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009467{
9468 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9469 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009470 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9471 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009472 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009473 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009474}
9475
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477 * Add an assert error to v:errors.
9478 */
9479 void
9480assert_error(garray_T *gap)
9481{
9482 struct vimvar *vp = &vimvars[VV_ERRORS];
9483
9484 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9485 /* Make sure v:errors is a list. */
9486 set_vim_var_list(VV_ERRORS, list_alloc());
9487 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9488}
Bram Moolenaar31988702018-02-13 12:57:42 +01009489/*
9490 * Compare "typ1" and "typ2". Put the result in "typ1".
9491 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009492 int
9493typval_compare(
9494 typval_T *typ1, /* first operand */
9495 typval_T *typ2, /* second operand */
9496 exptype_T type, /* operator */
9497 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009498 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009499{
9500 int i;
9501 varnumber_T n1, n2;
9502 char_u *s1, *s2;
9503 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9504
Bram Moolenaar31988702018-02-13 12:57:42 +01009505 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009506 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009507 /* For "is" a different type always means FALSE, for "notis"
9508 * it means TRUE. */
9509 n1 = (type == TYPE_NEQUAL);
9510 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009511 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9512 {
9513 if (type_is)
9514 {
9515 n1 = (typ1->v_type == typ2->v_type
9516 && typ1->vval.v_blob == typ2->vval.v_blob);
9517 if (type == TYPE_NEQUAL)
9518 n1 = !n1;
9519 }
9520 else if (typ1->v_type != typ2->v_type
9521 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9522 {
9523 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009524 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009525 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009526 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009527 clear_tv(typ1);
9528 return FAIL;
9529 }
9530 else
9531 {
9532 // Compare two Blobs for being equal or unequal.
9533 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9534 if (type == TYPE_NEQUAL)
9535 n1 = !n1;
9536 }
9537 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009538 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9539 {
9540 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009541 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009542 n1 = (typ1->v_type == typ2->v_type
9543 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009544 if (type == TYPE_NEQUAL)
9545 n1 = !n1;
9546 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009547 else if (typ1->v_type != typ2->v_type
9548 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009549 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009550 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009551 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009552 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009553 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009554 clear_tv(typ1);
9555 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009556 }
9557 else
9558 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009559 /* Compare two Lists for being equal or unequal. */
9560 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9561 ic, FALSE);
9562 if (type == TYPE_NEQUAL)
9563 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009564 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009565 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009566
9567 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9568 {
9569 if (type_is)
9570 {
9571 n1 = (typ1->v_type == typ2->v_type
9572 && typ1->vval.v_dict == typ2->vval.v_dict);
9573 if (type == TYPE_NEQUAL)
9574 n1 = !n1;
9575 }
9576 else if (typ1->v_type != typ2->v_type
9577 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9578 {
9579 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009580 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009581 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009582 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009583 clear_tv(typ1);
9584 return FAIL;
9585 }
9586 else
9587 {
9588 /* Compare two Dictionaries for being equal or unequal. */
9589 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9590 ic, FALSE);
9591 if (type == TYPE_NEQUAL)
9592 n1 = !n1;
9593 }
9594 }
9595
9596 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9597 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9598 {
9599 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009601 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009602 clear_tv(typ1);
9603 return FAIL;
9604 }
9605 if ((typ1->v_type == VAR_PARTIAL
9606 && typ1->vval.v_partial == NULL)
9607 || (typ2->v_type == VAR_PARTIAL
9608 && typ2->vval.v_partial == NULL))
9609 /* when a partial is NULL assume not equal */
9610 n1 = FALSE;
9611 else if (type_is)
9612 {
9613 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9614 /* strings are considered the same if their value is
9615 * the same */
9616 n1 = tv_equal(typ1, typ2, ic, FALSE);
9617 else if (typ1->v_type == VAR_PARTIAL
9618 && typ2->v_type == VAR_PARTIAL)
9619 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9620 else
9621 n1 = FALSE;
9622 }
9623 else
9624 n1 = tv_equal(typ1, typ2, ic, FALSE);
9625 if (type == TYPE_NEQUAL)
9626 n1 = !n1;
9627 }
9628
9629#ifdef FEAT_FLOAT
9630 /*
9631 * If one of the two variables is a float, compare as a float.
9632 * When using "=~" or "!~", always compare as string.
9633 */
9634 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9635 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9636 {
9637 float_T f1, f2;
9638
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009639 f1 = tv_get_float(typ1);
9640 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009641 n1 = FALSE;
9642 switch (type)
9643 {
9644 case TYPE_EQUAL: n1 = (f1 == f2); break;
9645 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9646 case TYPE_GREATER: n1 = (f1 > f2); break;
9647 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9648 case TYPE_SMALLER: n1 = (f1 < f2); break;
9649 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9650 case TYPE_UNKNOWN:
9651 case TYPE_MATCH:
9652 case TYPE_NOMATCH: break; /* avoid gcc warning */
9653 }
9654 }
9655#endif
9656
9657 /*
9658 * If one of the two variables is a number, compare as a number.
9659 * When using "=~" or "!~", always compare as string.
9660 */
9661 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9662 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009664 n1 = tv_get_number(typ1);
9665 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009666 switch (type)
9667 {
9668 case TYPE_EQUAL: n1 = (n1 == n2); break;
9669 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9670 case TYPE_GREATER: n1 = (n1 > n2); break;
9671 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9672 case TYPE_SMALLER: n1 = (n1 < n2); break;
9673 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9674 case TYPE_UNKNOWN:
9675 case TYPE_MATCH:
9676 case TYPE_NOMATCH: break; /* avoid gcc warning */
9677 }
9678 }
9679 else
9680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009681 s1 = tv_get_string_buf(typ1, buf1);
9682 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009683 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9684 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9685 else
9686 i = 0;
9687 n1 = FALSE;
9688 switch (type)
9689 {
9690 case TYPE_EQUAL: n1 = (i == 0); break;
9691 case TYPE_NEQUAL: n1 = (i != 0); break;
9692 case TYPE_GREATER: n1 = (i > 0); break;
9693 case TYPE_GEQUAL: n1 = (i >= 0); break;
9694 case TYPE_SMALLER: n1 = (i < 0); break;
9695 case TYPE_SEQUAL: n1 = (i <= 0); break;
9696
9697 case TYPE_MATCH:
9698 case TYPE_NOMATCH:
9699 n1 = pattern_match(s2, s1, ic);
9700 if (type == TYPE_NOMATCH)
9701 n1 = !n1;
9702 break;
9703
9704 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9705 }
9706 }
9707 clear_tv(typ1);
9708 typ1->v_type = VAR_NUMBER;
9709 typ1->vval.v_number = n1;
9710
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009711 return OK;
9712}
9713
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009714 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009715typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009716{
9717 char_u *tofree;
9718 char_u numbuf[NUMBUFLEN];
9719 char_u *ret = NULL;
9720
9721 if (arg == NULL)
9722 return vim_strsave((char_u *)"(does not exist)");
9723 ret = tv2string(arg, &tofree, numbuf, 0);
9724 /* Make a copy if we have a value but it's not in allocated memory. */
9725 if (ret != NULL && tofree == NULL)
9726 ret = vim_strsave(ret);
9727 return ret;
9728}
9729
9730 int
9731var_exists(char_u *var)
9732{
9733 char_u *name;
9734 char_u *tofree;
9735 typval_T tv;
9736 int len = 0;
9737 int n = FALSE;
9738
9739 /* get_name_len() takes care of expanding curly braces */
9740 name = var;
9741 len = get_name_len(&var, &tofree, TRUE, FALSE);
9742 if (len > 0)
9743 {
9744 if (tofree != NULL)
9745 name = tofree;
9746 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9747 if (n)
9748 {
9749 /* handle d.key, l[idx], f(expr) */
9750 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9751 if (n)
9752 clear_tv(&tv);
9753 }
9754 }
9755 if (*var != NUL)
9756 n = FALSE;
9757
9758 vim_free(tofree);
9759 return n;
9760}
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762#endif /* FEAT_EVAL */
9763
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009765#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766
Bram Moolenaar4f974752019-02-17 17:44:42 +01009767#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768/*
9769 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
9772/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009773 * Get the short path (8.3) for the filename in "fnamep".
9774 * Only works for a valid file name.
9775 * When the path gets longer "fnamep" is changed and the allocated buffer
9776 * is put in "bufp".
9777 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9778 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 */
9780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009781get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009783 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 char_u *newbuf;
9785
9786 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009787 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (l > len - 1)
9789 {
9790 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009791 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 newbuf = vim_strnsave(*fnamep, l);
9793 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009794 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795
9796 vim_free(*bufp);
9797 *fnamep = *bufp = newbuf;
9798
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009799 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009800 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 }
9802
9803 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009804 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
9807/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009808 * Get the short path (8.3) for the filename in "fname". The converted
9809 * path is returned in "bufp".
9810 *
9811 * Some of the directories specified in "fname" may not exist. This function
9812 * will shorten the existing directories at the beginning of the path and then
9813 * append the remaining non-existing path.
9814 *
9815 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009816 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009817 * bufp - Pointer to an allocated buffer for the filename.
9818 * fnamelen - Length of the filename pointed to by fname
9819 *
9820 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 */
9822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009823shortpath_for_invalid_fname(
9824 char_u **fname,
9825 char_u **bufp,
9826 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009828 char_u *short_fname, *save_fname, *pbuf_unused;
9829 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009831 int old_len, len;
9832 int new_len, sfx_len;
9833 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834
9835 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009836 old_len = *fnamelen;
9837 save_fname = vim_strnsave(*fname, old_len);
9838 pbuf_unused = NULL;
9839 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009841 endp = save_fname + old_len - 1; /* Find the end of the copy */
9842 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009844 /*
9845 * Try shortening the supplied path till it succeeds by removing one
9846 * directory at a time from the tail of the path.
9847 */
9848 len = 0;
9849 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009851 /* go back one path-separator */
9852 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9853 --endp;
9854 if (endp <= save_fname)
9855 break; /* processed the complete path */
9856
9857 /*
9858 * Replace the path separator with a NUL and try to shorten the
9859 * resulting path.
9860 */
9861 ch = *endp;
9862 *endp = 0;
9863 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009864 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009865 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9866 {
9867 retval = FAIL;
9868 goto theend;
9869 }
9870 *endp = ch; /* preserve the string */
9871
9872 if (len > 0)
9873 break; /* successfully shortened the path */
9874
9875 /* failed to shorten the path. Skip the path separator */
9876 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 }
9878
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009879 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009881 /*
9882 * Succeeded in shortening the path. Now concatenate the shortened
9883 * path with the remaining path at the tail.
9884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009886 /* Compute the length of the new path. */
9887 sfx_len = (int)(save_endp - endp) + 1;
9888 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009890 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009892 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009894 /* There is not enough space in the currently allocated string,
9895 * copy it to a buffer big enough. */
9896 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009898 {
9899 retval = FAIL;
9900 goto theend;
9901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 }
9903 else
9904 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009905 /* Transfer short_fname to the main buffer (it's big enough),
9906 * unless get_short_pathname() did its work in-place. */
9907 *fname = *bufp = save_fname;
9908 if (short_fname != save_fname)
9909 vim_strncpy(save_fname, short_fname, len);
9910 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009912
9913 /* concat the not-shortened part of the path */
9914 vim_strncpy(*fname + len, endp, sfx_len);
9915 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009917
9918theend:
9919 vim_free(pbuf_unused);
9920 vim_free(save_fname);
9921
9922 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923}
9924
9925/*
9926 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009927 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 */
9929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009930shortpath_for_partial(
9931 char_u **fnamep,
9932 char_u **bufp,
9933 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
9935 int sepcount, len, tflen;
9936 char_u *p;
9937 char_u *pbuf, *tfname;
9938 int hasTilde;
9939
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009940 /* Count up the path separators from the RHS.. so we know which part
9941 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009943 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 if (vim_ispathsep(*p))
9945 ++sepcount;
9946
9947 /* Need full path first (use expand_env() to remove a "~/") */
9948 hasTilde = (**fnamep == '~');
9949 if (hasTilde)
9950 pbuf = tfname = expand_env_save(*fnamep);
9951 else
9952 pbuf = tfname = FullName_save(*fnamep, FALSE);
9953
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009954 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009956 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
9959 if (len == 0)
9960 {
9961 /* Don't have a valid filename, so shorten the rest of the
9962 * path if we can. This CAN give us invalid 8.3 filenames, but
9963 * there's not a lot of point in guessing what it might be.
9964 */
9965 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009966 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 }
9969
9970 /* Count the paths backward to find the beginning of the desired string. */
9971 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009972 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009973 if (has_mbyte)
9974 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (vim_ispathsep(*p))
9976 {
9977 if (sepcount == 0 || (hasTilde && sepcount == 1))
9978 break;
9979 else
9980 sepcount --;
9981 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 if (hasTilde)
9984 {
9985 --p;
9986 if (p >= tfname)
9987 *p = '~';
9988 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 }
9991 else
9992 ++p;
9993
9994 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9995 vim_free(*bufp);
9996 *fnamelen = (int)STRLEN(p);
9997 *bufp = pbuf;
9998 *fnamep = p;
9999
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010000 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010002#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
10004/*
10005 * Adjust a filename, according to a string of modifiers.
10006 * *fnamep must be NUL terminated when called. When returning, the length is
10007 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010008 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 * When there is an error, *fnamep is set to NULL.
10010 */
10011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010012modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010013 char_u *src, // string with modifiers
10014 int tilde_file, // "~" is a file name, not $HOME
10015 int *usedlen, // characters after src that are used
10016 char_u **fnamep, // file name so far
10017 char_u **bufp, // buffer for allocated file name or NULL
10018 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020 int valid = 0;
10021 char_u *tail;
10022 char_u *s, *p, *pbuf;
10023 char_u dirname[MAXPATHL];
10024 int c;
10025 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010026#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010027 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 int has_shortname = 0;
10029#endif
10030
10031repeat:
10032 /* ":p" - full path/file_name */
10033 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10034 {
10035 has_fullname = 1;
10036
10037 valid |= VALID_PATH;
10038 *usedlen += 2;
10039
10040 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10041 if ((*fnamep)[0] == '~'
10042#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10043 && ((*fnamep)[1] == '/'
10044# ifdef BACKSLASH_IN_FILENAME
10045 || (*fnamep)[1] == '\\'
10046# endif
10047 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010049 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 )
10051 {
10052 *fnamep = expand_env_save(*fnamep);
10053 vim_free(*bufp); /* free any allocated file name */
10054 *bufp = *fnamep;
10055 if (*fnamep == NULL)
10056 return -1;
10057 }
10058
10059 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010060 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 {
10062 if (vim_ispathsep(*p)
10063 && p[1] == '.'
10064 && (p[2] == NUL
10065 || vim_ispathsep(p[2])
10066 || (p[2] == '.'
10067 && (p[3] == NUL || vim_ispathsep(p[3])))))
10068 break;
10069 }
10070
10071 /* FullName_save() is slow, don't use it when not needed. */
10072 if (*p != NUL || !vim_isAbsName(*fnamep))
10073 {
10074 *fnamep = FullName_save(*fnamep, *p != NUL);
10075 vim_free(*bufp); /* free any allocated file name */
10076 *bufp = *fnamep;
10077 if (*fnamep == NULL)
10078 return -1;
10079 }
10080
Bram Moolenaar4f974752019-02-17 17:44:42 +010010081#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010082# if _WIN32_WINNT >= 0x0500
10083 if (vim_strchr(*fnamep, '~') != NULL)
10084 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010085 // Expand 8.3 filename to full path. Needed to make sure the same
10086 // file does not have two different names.
10087 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10088 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10089 WCHAR buf[_MAX_PATH];
10090
10091 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010092 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010093 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010094 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010095 char_u *p = utf16_to_enc(buf, NULL);
10096
10097 if (p != NULL)
10098 {
10099 vim_free(*bufp); // free any allocated file name
10100 *bufp = *fnamep = p;
10101 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010102 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010103 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010104 }
10105 }
10106# endif
10107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 /* Append a path separator to a directory. */
10109 if (mch_isdir(*fnamep))
10110 {
10111 /* Make room for one or two extra characters. */
10112 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10113 vim_free(*bufp); /* free any allocated file name */
10114 *bufp = *fnamep;
10115 if (*fnamep == NULL)
10116 return -1;
10117 add_pathsep(*fnamep);
10118 }
10119 }
10120
10121 /* ":." - path relative to the current directory */
10122 /* ":~" - path relative to the home directory */
10123 /* ":8" - shortname path - postponed till after */
10124 while (src[*usedlen] == ':'
10125 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10126 {
10127 *usedlen += 2;
10128 if (c == '8')
10129 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010130#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 has_shortname = 1; /* Postpone this. */
10132#endif
10133 continue;
10134 }
10135 pbuf = NULL;
10136 /* Need full path first (use expand_env() to remove a "~/") */
10137 if (!has_fullname)
10138 {
10139 if (c == '.' && **fnamep == '~')
10140 p = pbuf = expand_env_save(*fnamep);
10141 else
10142 p = pbuf = FullName_save(*fnamep, FALSE);
10143 }
10144 else
10145 p = *fnamep;
10146
10147 has_fullname = 0;
10148
10149 if (p != NULL)
10150 {
10151 if (c == '.')
10152 {
10153 mch_dirname(dirname, MAXPATHL);
10154 s = shorten_fname(p, dirname);
10155 if (s != NULL)
10156 {
10157 *fnamep = s;
10158 if (pbuf != NULL)
10159 {
10160 vim_free(*bufp); /* free any allocated file name */
10161 *bufp = pbuf;
10162 pbuf = NULL;
10163 }
10164 }
10165 }
10166 else
10167 {
10168 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10169 /* Only replace it when it starts with '~' */
10170 if (*dirname == '~')
10171 {
10172 s = vim_strsave(dirname);
10173 if (s != NULL)
10174 {
10175 *fnamep = s;
10176 vim_free(*bufp);
10177 *bufp = s;
10178 }
10179 }
10180 }
10181 vim_free(pbuf);
10182 }
10183 }
10184
10185 tail = gettail(*fnamep);
10186 *fnamelen = (int)STRLEN(*fnamep);
10187
10188 /* ":h" - head, remove "/file_name", can be repeated */
10189 /* Don't remove the first "/" or "c:\" */
10190 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10191 {
10192 valid |= VALID_HEAD;
10193 *usedlen += 2;
10194 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010195 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010196 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 *fnamelen = (int)(tail - *fnamep);
10198#ifdef VMS
10199 if (*fnamelen > 0)
10200 *fnamelen += 1; /* the path separator is part of the path */
10201#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010202 if (*fnamelen == 0)
10203 {
10204 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10205 p = vim_strsave((char_u *)".");
10206 if (p == NULL)
10207 return -1;
10208 vim_free(*bufp);
10209 *bufp = *fnamep = tail = p;
10210 *fnamelen = 1;
10211 }
10212 else
10213 {
10214 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010215 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 }
10218
10219 /* ":8" - shortname */
10220 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10221 {
10222 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010223#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 has_shortname = 1;
10225#endif
10226 }
10227
Bram Moolenaar4f974752019-02-17 17:44:42 +010010228#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010229 /*
10230 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 */
10232 if (has_shortname)
10233 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010234 /* Copy the string if it is shortened by :h and when it wasn't copied
10235 * yet, because we are going to change it in place. Avoids changing
10236 * the buffer name for "%:8". */
10237 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 {
10239 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010240 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 return -1;
10242 vim_free(*bufp);
10243 *bufp = *fnamep = p;
10244 }
10245
10246 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010247 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 if (!has_fullname && !vim_isAbsName(*fnamep))
10249 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010250 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 return -1;
10252 }
10253 else
10254 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010255 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256
Bram Moolenaardc935552011-08-17 15:23:23 +020010257 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010259 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 return -1;
10261
10262 if (l == 0)
10263 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010264 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010266 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 return -1;
10268 }
10269 *fnamelen = l;
10270 }
10271 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010272#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273
10274 /* ":t" - tail, just the basename */
10275 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10276 {
10277 *usedlen += 2;
10278 *fnamelen -= (int)(tail - *fnamep);
10279 *fnamep = tail;
10280 }
10281
10282 /* ":e" - extension, can be repeated */
10283 /* ":r" - root, without extension, can be repeated */
10284 while (src[*usedlen] == ':'
10285 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10286 {
10287 /* find a '.' in the tail:
10288 * - for second :e: before the current fname
10289 * - otherwise: The last '.'
10290 */
10291 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10292 s = *fnamep - 2;
10293 else
10294 s = *fnamep + *fnamelen - 1;
10295 for ( ; s > tail; --s)
10296 if (s[0] == '.')
10297 break;
10298 if (src[*usedlen + 1] == 'e') /* :e */
10299 {
10300 if (s > tail)
10301 {
10302 *fnamelen += (int)(*fnamep - (s + 1));
10303 *fnamep = s + 1;
10304#ifdef VMS
10305 /* cut version from the extension */
10306 s = *fnamep + *fnamelen - 1;
10307 for ( ; s > *fnamep; --s)
10308 if (s[0] == ';')
10309 break;
10310 if (s > *fnamep)
10311 *fnamelen = s - *fnamep;
10312#endif
10313 }
10314 else if (*fnamep <= tail)
10315 *fnamelen = 0;
10316 }
10317 else /* :r */
10318 {
10319 if (s > tail) /* remove one extension */
10320 *fnamelen = (int)(s - *fnamep);
10321 }
10322 *usedlen += 2;
10323 }
10324
10325 /* ":s?pat?foo?" - substitute */
10326 /* ":gs?pat?foo?" - global substitute */
10327 if (src[*usedlen] == ':'
10328 && (src[*usedlen + 1] == 's'
10329 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10330 {
10331 char_u *str;
10332 char_u *pat;
10333 char_u *sub;
10334 int sep;
10335 char_u *flags;
10336 int didit = FALSE;
10337
10338 flags = (char_u *)"";
10339 s = src + *usedlen + 2;
10340 if (src[*usedlen + 1] == 'g')
10341 {
10342 flags = (char_u *)"g";
10343 ++s;
10344 }
10345
10346 sep = *s++;
10347 if (sep)
10348 {
10349 /* find end of pattern */
10350 p = vim_strchr(s, sep);
10351 if (p != NULL)
10352 {
10353 pat = vim_strnsave(s, (int)(p - s));
10354 if (pat != NULL)
10355 {
10356 s = p + 1;
10357 /* find end of substitution */
10358 p = vim_strchr(s, sep);
10359 if (p != NULL)
10360 {
10361 sub = vim_strnsave(s, (int)(p - s));
10362 str = vim_strnsave(*fnamep, *fnamelen);
10363 if (sub != NULL && str != NULL)
10364 {
10365 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010366 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 if (s != NULL)
10368 {
10369 *fnamep = s;
10370 *fnamelen = (int)STRLEN(s);
10371 vim_free(*bufp);
10372 *bufp = s;
10373 didit = TRUE;
10374 }
10375 }
10376 vim_free(sub);
10377 vim_free(str);
10378 }
10379 vim_free(pat);
10380 }
10381 }
10382 /* after using ":s", repeat all the modifiers */
10383 if (didit)
10384 goto repeat;
10385 }
10386 }
10387
Bram Moolenaar26df0922014-02-23 23:39:13 +010010388 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10389 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010390 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010391 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010392 if (c != NUL)
10393 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010394 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010395 if (c != NUL)
10396 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010397 if (p == NULL)
10398 return -1;
10399 vim_free(*bufp);
10400 *bufp = *fnamep = p;
10401 *fnamelen = (int)STRLEN(p);
10402 *usedlen += 2;
10403 }
10404
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 return valid;
10406}
10407
10408/*
10409 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010410 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 * "flags" can be "g" to do a global substitute.
10412 * Returns an allocated string, NULL for error.
10413 */
10414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010415do_string_sub(
10416 char_u *str,
10417 char_u *pat,
10418 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010419 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010420 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421{
10422 int sublen;
10423 regmatch_T regmatch;
10424 int i;
10425 int do_all;
10426 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010427 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 garray_T ga;
10429 char_u *ret;
10430 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010431 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432
10433 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10434 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010435 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436
10437 ga_init2(&ga, 1, 200);
10438
10439 do_all = (flags[0] == 'g');
10440
10441 regmatch.rm_ic = p_ic;
10442 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10443 if (regmatch.regprog != NULL)
10444 {
10445 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010446 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10448 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010449 /* Skip empty match except for first match. */
10450 if (regmatch.startp[0] == regmatch.endp[0])
10451 {
10452 if (zero_width == regmatch.startp[0])
10453 {
10454 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010455 i = MB_PTR2LEN(tail);
10456 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10457 (size_t)i);
10458 ga.ga_len += i;
10459 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010460 continue;
10461 }
10462 zero_width = regmatch.startp[0];
10463 }
10464
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 /*
10466 * Get some space for a temporary buffer to do the substitution
10467 * into. It will contain:
10468 * - The text up to where the match is.
10469 * - The substituted text.
10470 * - The text after the match.
10471 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010472 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010473 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10475 {
10476 ga_clear(&ga);
10477 break;
10478 }
10479
10480 /* copy the text up to where the match is */
10481 i = (int)(regmatch.startp[0] - tail);
10482 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10483 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010484 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 + ga.ga_len + i, TRUE, TRUE, FALSE);
10486 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010487 tail = regmatch.endp[0];
10488 if (*tail == NUL)
10489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 if (!do_all)
10491 break;
10492 }
10493
10494 if (ga.ga_data != NULL)
10495 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10496
Bram Moolenaar473de612013-06-08 18:19:48 +020010497 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 }
10499
10500 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10501 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010502 if (p_cpo == empty_option)
10503 p_cpo = save_cpo;
10504 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010505 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010506 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507
10508 return ret;
10509}
10510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010511 static int
10512filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10513{
10514 typval_T rettv;
10515 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010517
10518 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10519 argv[0] = vimvars[VV_KEY].vv_tv;
10520 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010521 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10522 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523 if (map)
10524 {
10525 /* map(): replace the list item value */
10526 clear_tv(tv);
10527 rettv.v_lock = 0;
10528 *tv = rettv;
10529 }
10530 else
10531 {
10532 int error = FALSE;
10533
10534 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010535 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536 clear_tv(&rettv);
10537 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010538 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010539 if (error)
10540 goto theend;
10541 }
10542 retval = OK;
10543theend:
10544 clear_tv(&vimvars[VV_VAL].vv_tv);
10545 return retval;
10546}
10547
10548
10549/*
10550 * Implementation of map() and filter().
10551 */
10552 void
10553filter_map(typval_T *argvars, typval_T *rettv, int map)
10554{
10555 typval_T *expr;
10556 listitem_T *li, *nli;
10557 list_T *l = NULL;
10558 dictitem_T *di;
10559 hashtab_T *ht;
10560 hashitem_T *hi;
10561 dict_T *d = NULL;
10562 typval_T save_val;
10563 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010564 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 int rem;
10566 int todo;
10567 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10568 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10569 : N_("filter() argument"));
10570 int save_did_emsg;
10571 int idx = 0;
10572
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010573 if (argvars[0].v_type == VAR_BLOB)
10574 {
10575 if ((b = argvars[0].vval.v_blob) == NULL)
10576 return;
10577 }
10578 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010579 {
10580 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010581 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 return;
10583 }
10584 else if (argvars[0].v_type == VAR_DICT)
10585 {
10586 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010587 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 return;
10589 }
10590 else
10591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010592 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 return;
10594 }
10595
10596 expr = &argvars[1];
10597 /* On type errors, the preceding call has already displayed an error
10598 * message. Avoid a misleading error message for an empty string that
10599 * was not passed as argument. */
10600 if (expr->v_type != VAR_UNKNOWN)
10601 {
10602 prepare_vimvar(VV_VAL, &save_val);
10603
10604 /* We reset "did_emsg" to be able to detect whether an error
10605 * occurred during evaluation of the expression. */
10606 save_did_emsg = did_emsg;
10607 did_emsg = FALSE;
10608
10609 prepare_vimvar(VV_KEY, &save_key);
10610 if (argvars[0].v_type == VAR_DICT)
10611 {
10612 vimvars[VV_KEY].vv_type = VAR_STRING;
10613
10614 ht = &d->dv_hashtab;
10615 hash_lock(ht);
10616 todo = (int)ht->ht_used;
10617 for (hi = ht->ht_array; todo > 0; ++hi)
10618 {
10619 if (!HASHITEM_EMPTY(hi))
10620 {
10621 int r;
10622
10623 --todo;
10624 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010625 if (map && (var_check_lock(di->di_tv.v_lock,
10626 arg_errmsg, TRUE)
10627 || var_check_ro(di->di_flags,
10628 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 break;
10630 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10631 r = filter_map_one(&di->di_tv, expr, map, &rem);
10632 clear_tv(&vimvars[VV_KEY].vv_tv);
10633 if (r == FAIL || did_emsg)
10634 break;
10635 if (!map && rem)
10636 {
10637 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10638 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10639 break;
10640 dictitem_remove(d, di);
10641 }
10642 }
10643 }
10644 hash_unlock(ht);
10645 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010646 else if (argvars[0].v_type == VAR_BLOB)
10647 {
10648 int i;
10649 typval_T tv;
10650
10651 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10652 for (i = 0; i < b->bv_ga.ga_len; i++)
10653 {
10654 tv.v_type = VAR_NUMBER;
10655 tv.vval.v_number = blob_get(b, i);
10656 vimvars[VV_KEY].vv_nr = idx;
10657 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10658 break;
10659 if (tv.v_type != VAR_NUMBER)
10660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010661 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010662 return;
10663 }
10664 tv.v_type = VAR_NUMBER;
10665 blob_set(b, i, tv.vval.v_number);
10666 if (!map && rem)
10667 {
10668 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10669
10670 mch_memmove(p + idx, p + i + 1,
10671 (size_t)b->bv_ga.ga_len - i - 1);
10672 --b->bv_ga.ga_len;
10673 --i;
10674 }
10675 }
10676 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 else
10678 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010679 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10681
10682 for (li = l->lv_first; li != NULL; li = nli)
10683 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010684 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010685 break;
10686 nli = li->li_next;
10687 vimvars[VV_KEY].vv_nr = idx;
10688 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10689 || did_emsg)
10690 break;
10691 if (!map && rem)
10692 listitem_remove(l, li);
10693 ++idx;
10694 }
10695 }
10696
10697 restore_vimvar(VV_KEY, &save_key);
10698 restore_vimvar(VV_VAL, &save_val);
10699
10700 did_emsg |= save_did_emsg;
10701 }
10702
10703 copy_tv(&argvars[0], rettv);
10704}
10705
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */