blob: 3cfa4b7a554675d576147eba16670280ac71af18 [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 Moolenaar33570922005-01-25 22:26:29 +000039#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000042 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them.
44 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000045static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000046
47/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000048 * When recursively copying lists and dicts we need to remember which ones we
49 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000050 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000051 */
52static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020053
Bram Moolenaard9fba312005-06-26 22:34:35 +000054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000055 * Array to hold the hashtab with variables local to each sourced script.
56 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000058typedef struct
59{
60 dictitem_T sv_var;
61 dict_T sv_dict;
62} scriptvar_T;
63
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020064static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
65#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
66#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68static int echo_attr = 0; /* attributes used for ":echo" */
69
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000071static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000074 * Info used by a ":for" loop.
75 */
Bram Moolenaar33570922005-01-25 22:26:29 +000076typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000077{
78 int fi_semicolon; /* TRUE if ending in '; var]' */
79 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000080 listwatch_T fi_lw; /* keep an eye on the item used. */
81 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 int fi_bi; /* index of blob */
83 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000084} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085
Bram Moolenaara7043832005-01-21 11:56:39 +000086
87/*
Bram Moolenaar33570922005-01-25 22:26:29 +000088 * Array to hold the value of v: variables.
89 * The value is in a dictitem, so that it can also be used in the v: scope.
90 * The reason to use this table anyway is for very quick access to the
91 * variables with the VV_ defines.
92 */
Bram Moolenaar33570922005-01-25 22:26:29 +000093
94/* values for vv_flags: */
95#define VV_COMPAT 1 /* compatible, also used without "v:" */
96#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000097#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000098
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010099#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000100
101static struct vimvar
102{
103 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100104 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000105 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
106} vimvars[VV_LEN] =
107{
108 /*
109 * The order here must match the VV_ defines in vim.h!
110 * Initializing a union does not work, leave tv.vval empty to get zero's.
111 */
112 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
113 {VV_NAME("count1", VAR_NUMBER), VV_RO},
114 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
115 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
116 {VV_NAME("warningmsg", VAR_STRING), 0},
117 {VV_NAME("statusmsg", VAR_STRING), 0},
118 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
119 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
120 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
121 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
122 {VV_NAME("termresponse", VAR_STRING), VV_RO},
123 {VV_NAME("fname", VAR_STRING), VV_RO},
124 {VV_NAME("lang", VAR_STRING), VV_RO},
125 {VV_NAME("lc_time", VAR_STRING), VV_RO},
126 {VV_NAME("ctype", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
128 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
129 {VV_NAME("fname_in", VAR_STRING), VV_RO},
130 {VV_NAME("fname_out", VAR_STRING), VV_RO},
131 {VV_NAME("fname_new", VAR_STRING), VV_RO},
132 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
133 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
134 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
136 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
137 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
138 {VV_NAME("progname", VAR_STRING), VV_RO},
139 {VV_NAME("servername", VAR_STRING), VV_RO},
140 {VV_NAME("dying", VAR_NUMBER), VV_RO},
141 {VV_NAME("exception", VAR_STRING), VV_RO},
142 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
143 {VV_NAME("register", VAR_STRING), VV_RO},
144 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
145 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000146 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
147 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000148 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000149 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
150 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200153 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000154 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
156 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000157 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000158 {VV_NAME("swapname", VAR_STRING), VV_RO},
159 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000160 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200161 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000162 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200163 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000164 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
165 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000166 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100168 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000169 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200171 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200172 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200173 {VV_NAME("option_new", VAR_STRING), VV_RO},
174 {VV_NAME("option_old", VAR_STRING), VV_RO},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200175 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
176 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
177 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200178 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100179 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100180 {VV_NAME("false", VAR_SPECIAL), VV_RO},
181 {VV_NAME("true", VAR_SPECIAL), VV_RO},
182 {VV_NAME("null", VAR_SPECIAL), VV_RO},
183 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100184 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200185 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200186 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
195 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100196 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200197 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
198 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200199 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200200 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
201 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
202 {VV_NAME("event", VAR_DICT), VV_RO},
203 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000204};
205
206/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000207#define vv_type vv_di.di_tv.v_type
208#define vv_nr vv_di.di_tv.vval.v_number
209#define vv_float vv_di.di_tv.vval.v_float
210#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000211#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200212#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100213#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000214#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000215
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200216static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define vimvarht vimvardict.dv_hashtab
218
Bram Moolenaar9937a052019-06-15 15:45:06 +0200219static void ex_let_const(exarg_T *eap, int is_const);
220static 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 +0100221static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
222static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void list_glob_vars(int *first);
224static void list_buf_vars(int *first);
225static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void list_vim_vars(int *first);
228static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200230static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
231static 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 +0100232static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
234static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
235static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
236static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int eval2(char_u **arg, typval_T *rettv, int evaluate);
239static int eval3(char_u **arg, typval_T *rettv, int evaluate);
240static int eval4(char_u **arg, typval_T *rettv, int evaluate);
241static int eval5(char_u **arg, typval_T *rettv, int evaluate);
242static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
243static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000244
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
246static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static 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 +0200251static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100252static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100253static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100254static void list_one_var(dictitem_T *v, char *prefix, int *first);
255static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200256static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100257static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100258static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260/* for VIM_VERSION_ defines */
261#include "version.h"
262
Bram Moolenaare21c1582019-03-02 11:57:09 +0100263/*
264 * Return "n1" divided by "n2", taking care of dividing by zero.
265 */
266 static varnumber_T
267num_divide(varnumber_T n1, varnumber_T n2)
268{
269 varnumber_T result;
270
271 if (n2 == 0) // give an error message?
272 {
273 if (n1 == 0)
274 result = VARNUM_MIN; // similar to NaN
275 else if (n1 < 0)
276 result = -VARNUM_MAX;
277 else
278 result = VARNUM_MAX;
279 }
280 else
281 result = n1 / n2;
282
283 return result;
284}
285
286/*
287 * Return "n1" modulus "n2", taking care of dividing by zero.
288 */
289 static varnumber_T
290num_modulus(varnumber_T n1, varnumber_T n2)
291{
292 // Give an error when n2 is 0?
293 return (n2 == 0) ? 0 : (n1 % n2);
294}
295
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100296
297#if defined(EBCDIC) || defined(PROTO)
298/*
299 * Compare struct fst by function name.
300 */
301 static int
302compare_func_name(const void *s1, const void *s2)
303{
304 struct fst *p1 = (struct fst *)s1;
305 struct fst *p2 = (struct fst *)s2;
306
307 return STRCMP(p1->f_name, p2->f_name);
308}
309
310/*
311 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100312 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100313 * On machines using EBCDIC we have to sort it.
314 */
315 static void
316sortFunctions(void)
317{
318 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
319
320 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
321}
322#endif
323
324
Bram Moolenaar33570922005-01-25 22:26:29 +0000325/*
326 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000327 */
328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000330{
Bram Moolenaar33570922005-01-25 22:26:29 +0000331 int i;
332 struct vimvar *p;
333
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200334 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
335 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200336 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000337 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000339
340 for (i = 0; i < VV_LEN; ++i)
341 {
342 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200343 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200346 getout(1);
347 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 STRCPY(p->vv_di.di_key, p->vv_name);
349 if (p->vv_flags & VV_RO)
350 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
351 else if (p->vv_flags & VV_RO_SBX)
352 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
353 else
354 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000355
356 /* add to v: scope dict, unless the value is not always available */
357 if (p->vv_type != VAR_UNKNOWN)
358 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000359 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000360 /* add to compat scope dict */
361 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100363 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200364 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100365
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100368 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100369 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100370 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371
372 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
373 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
374 set_vim_var_nr(VV_NONE, VVAL_NONE);
375 set_vim_var_nr(VV_NULL, VVAL_NULL);
376
Bram Moolenaarf562e722016-07-19 17:25:25 +0200377 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
378 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
379 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
380 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
381 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
382 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
383 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
384 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
385 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
386 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100387 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200388
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200389 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200390
391#ifdef EBCDIC
392 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100393 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200394 */
395 sortFunctions();
396#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000397}
398
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000399#if defined(EXITFREE) || defined(PROTO)
400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100401eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000402{
403 int i;
404 struct vimvar *p;
405
406 for (i = 0; i < VV_LEN; ++i)
407 {
408 p = &vimvars[i];
409 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100410 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000411 else if (p->vv_di.di_tv.v_type == VAR_LIST)
412 {
413 list_unref(p->vv_list);
414 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000415 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000416 }
417 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000418 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000419 hash_clear(&compat_hashtab);
420
Bram Moolenaard9fba312005-06-26 22:34:35 +0000421 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100422# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200423 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100424# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000425
426 /* global variables */
427 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000428
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000429 /* autoloaded script names */
430 ga_clear_strings(&ga_loaded);
431
Bram Moolenaarcca74132013-09-25 21:00:28 +0200432 /* Script-local variables. First clear all the variables and in a second
433 * loop free the scriptvar_T, because a variable in one script might hold
434 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200436 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200437 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200438 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200439 ga_clear(&ga_scripts);
440
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200441 // unreferenced lists and dicts
442 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200443
444 // functions not garbage collected
445 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000446}
447#endif
448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 * Set an internal variable to a string value. Creates the variable if it does
452 * not already exist.
453 */
454 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100455set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000457 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000458 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
460 val = vim_strsave(value);
461 if (val != NULL)
462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000463 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000464 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000466 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000467 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 }
469 }
470}
471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200473#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000474static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000475static char_u *redir_endp = NULL;
476static char_u *redir_varname = NULL;
477
478/*
479 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100480 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000481 * Returns OK if successfully completed the setup. FAIL otherwise.
482 */
483 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100484var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000485{
486 int save_emsg;
487 int err;
488 typval_T tv;
489
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000490 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000491 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100493 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 return FAIL;
495 }
496
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 redir_varname = vim_strsave(name);
499 if (redir_varname == NULL)
500 return FAIL;
501
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200502 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000503 if (redir_lval == NULL)
504 {
505 var_redir_stop();
506 return FAIL;
507 }
508
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000509 /* The output is stored in growarray "redir_ga" until redirection ends. */
510 ga_init2(&redir_ga, (int)sizeof(char), 500);
511
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100513 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000514 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
516 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200517 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 if (redir_endp != NULL && *redir_endp != NUL)
519 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100520 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100522 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000523 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 var_redir_stop();
525 return FAIL;
526 }
527
528 /* check if we can write to the variable: set it to or append an empty
529 * string */
530 save_emsg = did_emsg;
531 did_emsg = FALSE;
532 tv.v_type = VAR_STRING;
533 tv.vval.v_string = (char_u *)"";
534 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200535 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200537 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200538 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000540 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 if (err)
542 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000543 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544 var_redir_stop();
545 return FAIL;
546 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000547
548 return OK;
549}
550
551/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000552 * Append "value[value_len]" to the variable set by var_redir_start().
553 * The actual appending is postponed until redirection ends, because the value
554 * appended may in fact be the string we write to, changing it may cause freed
555 * memory to be used:
556 * :redir => foo
557 * :let foo
558 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000559 */
560 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100561var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000563 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564
565 if (redir_lval == NULL)
566 return;
567
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000569 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000571 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000573 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574 {
575 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000576 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000577 }
578 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580}
581
582/*
583 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000584 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585 */
586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000588{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000589 typval_T tv;
590
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200591 if (EVALCMD_BUSY)
592 {
593 redir_lval = NULL;
594 return;
595 }
596
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000597 if (redir_lval != NULL)
598 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000599 /* If there was no error: assign the text to the variable. */
600 if (redir_endp != NULL)
601 {
602 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
603 tv.v_type = VAR_STRING;
604 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200605 /* Call get_lval() again, if it's inside a Dict or List it may
606 * have changed. */
607 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100608 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200609 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200610 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
611 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200612 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000613 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000614
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000615 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100616 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000617
Bram Moolenaard23a8232018-02-10 18:45:26 +0100618 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000619 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100624eval_charconvert(
625 char_u *enc_from,
626 char_u *enc_to,
627 char_u *fname_from,
628 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 int err = FALSE;
631
632 set_vim_var_string(VV_CC_FROM, enc_from, -1);
633 set_vim_var_string(VV_CC_TO, enc_to, -1);
634 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
635 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
636 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
637 err = TRUE;
638 set_vim_var_string(VV_CC_FROM, NULL, -1);
639 set_vim_var_string(VV_CC_TO, NULL, -1);
640 set_vim_var_string(VV_FNAME_IN, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642
643 if (err)
644 return FAIL;
645 return OK;
646}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100650eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err = FALSE;
653
654 set_vim_var_string(VV_FNAME_IN, fname, -1);
655 set_vim_var_string(VV_CMDARG, args, -1);
656 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
657 err = TRUE;
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_CMDARG, NULL, -1);
660
661 if (err)
662 {
663 mch_remove(fname);
664 return FAIL;
665 }
666 return OK;
667}
668# endif
669
670# if defined(FEAT_DIFF) || defined(PROTO)
671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100672eval_diff(
673 char_u *origfile,
674 char_u *newfile,
675 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 int err = FALSE;
678
679 set_vim_var_string(VV_FNAME_IN, origfile, -1);
680 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
681 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
682 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
683 set_vim_var_string(VV_FNAME_IN, NULL, -1);
684 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
685 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
686}
687
688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100689eval_patch(
690 char_u *origfile,
691 char_u *difffile,
692 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
694 int err;
695
696 set_vim_var_string(VV_FNAME_IN, origfile, -1);
697 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
698 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
699 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
700 set_vim_var_string(VV_FNAME_IN, NULL, -1);
701 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
702 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
703}
704# endif
705
706/*
707 * Top level evaluation function, returning a boolean.
708 * Sets "error" to TRUE if there was an error.
709 * Return TRUE or FALSE.
710 */
711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712eval_to_bool(
713 char_u *arg,
714 int *error,
715 char_u **nextcmd,
716 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
Bram Moolenaar33570922005-01-25 22:26:29 +0000718 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200719 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721 if (skip)
722 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 else
726 {
727 *error = FALSE;
728 if (!skip)
729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100730 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 }
734 if (skip)
735 --emsg_skip;
736
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200737 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100740/*
741 * Call eval1() and give an error message if not done at a lower level.
742 */
743 static int
744eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
745{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100746 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100747 int ret;
748 int did_emsg_before = did_emsg;
749 int called_emsg_before = called_emsg;
750
751 ret = eval1(arg, rettv, evaluate);
752 if (ret == FAIL)
753 {
754 // Report the invalid expression unless the expression evaluation has
755 // been cancelled due to an aborting error, an interrupt, or an
756 // exception, or we already gave a more specific error.
757 // Also check called_emsg for when using assert_fails().
758 if (!aborting() && did_emsg == did_emsg_before
759 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100760 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100761 }
762 return ret;
763}
764
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200765 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100766eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
767{
768 char_u *s;
769 int dummy;
770 char_u buf[NUMBUFLEN];
771
772 if (expr->v_type == VAR_FUNC)
773 {
774 s = expr->vval.v_string;
775 if (s == NULL || *s == NUL)
776 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200777 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100778 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
779 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 Moolenaar6ed88192019-05-11 18:37:44 +0200788 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100789 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
790 return FAIL;
791 }
792 else
793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100794 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100795 if (s == NULL)
796 return FAIL;
797 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100798 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100799 return FAIL;
800 if (*s != NUL) /* check for trailing chars after expr */
801 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200802 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100804 return FAIL;
805 }
806 }
807 return OK;
808}
809
810/*
811 * Like eval_to_bool() but using a typval_T instead of a string.
812 * Works for string, funcref and partial.
813 */
814 int
815eval_expr_to_bool(typval_T *expr, int *error)
816{
817 typval_T rettv;
818 int res;
819
820 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
821 {
822 *error = TRUE;
823 return FALSE;
824 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100825 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100826 clear_tv(&rettv);
827 return res;
828}
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830/*
831 * Top level evaluation function, returning a string. If "skip" is TRUE,
832 * only parsing to "nextcmd" is done, without reporting errors. Return
833 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
834 */
835 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836eval_to_string_skip(
837 char_u *arg,
838 char_u **nextcmd,
839 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 char_u *retval;
843
844 if (skip)
845 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 retval = NULL;
848 else
849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100850 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 if (skip)
854 --emsg_skip;
855
856 return retval;
857}
858
859/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000860 * Skip over an expression at "*pp".
861 * Return FAIL for an error, OK otherwise.
862 */
863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100864skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867
868 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000870}
871
872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000874 * When "convert" is TRUE convert a List into a sequence of lines and convert
875 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Return pointer to allocated memory, or NULL for failure.
877 */
878 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879eval_to_string(
880 char_u *arg,
881 char_u **nextcmd,
882 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000886 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000887#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000888 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = NULL;
893 else
894 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000895 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000896 {
897 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000898 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200899 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200900 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200901 if (tv.vval.v_list->lv_len > 0)
902 ga_append(&ga, NL);
903 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000904 ga_append(&ga, NUL);
905 retval = (char_u *)ga.ga_data;
906 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000907#ifdef FEAT_FLOAT
908 else if (convert && tv.v_type == VAR_FLOAT)
909 {
910 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
911 retval = vim_strsave(numbuf);
912 }
913#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000914 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100915 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 return retval;
920}
921
922/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000923 * Call eval_to_string() without using current local variables and using
924 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
926 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100927eval_to_string_safe(
928 char_u *arg,
929 char_u **nextcmd,
930 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200933 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200935 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000936 if (use_sandbox)
937 ++sandbox;
938 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000939 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000940 if (use_sandbox)
941 --sandbox;
942 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200943 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 return retval;
945}
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947/*
948 * Top level evaluation function, returning a number.
949 * Evaluates "expr" silently.
950 * Returns -1 for an error.
951 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200952 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100953eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200956 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000957 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
959 ++emsg_off;
960
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000961 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 retval = -1;
963 else
964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100965 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 --emsg_off;
969
970 return retval;
971}
972
Bram Moolenaara40058a2005-07-11 22:42:07 +0000973/*
974 * Prepare v: variable "idx" to be used.
975 * Save the current typeval in "save_tv".
976 * When not used yet add the variable to the v: hashtable.
977 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100979prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000980{
981 *save_tv = vimvars[idx].vv_tv;
982 if (vimvars[idx].vv_type == VAR_UNKNOWN)
983 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
984}
985
986/*
987 * Restore v: variable "idx" to typeval "save_tv".
988 * When no longer defined, remove the variable from the v: hashtable.
989 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100991restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000992{
993 hashitem_T *hi;
994
Bram Moolenaara40058a2005-07-11 22:42:07 +0000995 vimvars[idx].vv_tv = *save_tv;
996 if (vimvars[idx].vv_type == VAR_UNKNOWN)
997 {
998 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
999 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001000 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001001 else
1002 hash_remove(&vimvarht, hi);
1003 }
1004}
1005
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001006#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001007/*
1008 * Evaluate an expression to a list with suggestions.
1009 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001010 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001011 */
1012 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001014{
1015 typval_T save_val;
1016 typval_T rettv;
1017 list_T *list = NULL;
1018 char_u *p = skipwhite(expr);
1019
1020 /* Set "v:val" to the bad word. */
1021 prepare_vimvar(VV_VAL, &save_val);
1022 vimvars[VV_VAL].vv_type = VAR_STRING;
1023 vimvars[VV_VAL].vv_str = badword;
1024 if (p_verbose == 0)
1025 ++emsg_off;
1026
1027 if (eval1(&p, &rettv, TRUE) == OK)
1028 {
1029 if (rettv.v_type != VAR_LIST)
1030 clear_tv(&rettv);
1031 else
1032 list = rettv.vval.v_list;
1033 }
1034
1035 if (p_verbose == 0)
1036 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001037 restore_vimvar(VV_VAL, &save_val);
1038
1039 return list;
1040}
1041
1042/*
1043 * "list" is supposed to contain two items: a word and a number. Return the
1044 * word in "pp" and the number as the return value.
1045 * Return -1 if anything isn't right.
1046 * Used to get the good word and score from the eval_spell_expr() result.
1047 */
1048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001050{
1051 listitem_T *li;
1052
1053 li = list->lv_first;
1054 if (li == NULL)
1055 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001056 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001057
1058 li = li->li_next;
1059 if (li == NULL)
1060 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001062}
1063#endif
1064
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001065/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001066 * Top level evaluation function.
1067 * Returns an allocated typval_T with the result.
1068 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069 */
1070 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001071eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001072{
1073 typval_T *tv;
1074
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001075 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001076 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001077 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001078
1079 return tv;
1080}
1081
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001084 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001085 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1086 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001087 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090call_vim_function(
1091 char_u *func,
1092 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001093 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001094 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001097 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001100 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001102 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001103 if (ret == FAIL)
1104 clear_tv(rettv);
1105
1106 return ret;
1107}
1108
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001109/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001110 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001111 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001112 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1113 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001115 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001116call_func_retnr(
1117 char_u *func,
1118 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001119 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120{
1121 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001122 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001123
Bram Moolenaarded27a12018-08-01 19:06:03 +02001124 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001125 return -1;
1126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001127 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001128 clear_tv(&rettv);
1129 return retval;
1130}
1131
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001132#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001133 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1134
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001135# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001136/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001137 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001138 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001139 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1140 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141 */
1142 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001143call_func_retstr(
1144 char_u *func,
1145 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001146 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147{
1148 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001149 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001150
Bram Moolenaarded27a12018-08-01 19:06:03 +02001151 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152 return NULL;
1153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001154 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return retval;
1157}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001158# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001159
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001160/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001161 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001162 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1163 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001164 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165 */
1166 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001167call_func_retlist(
1168 char_u *func,
1169 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001170 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001171{
1172 typval_T rettv;
1173
Bram Moolenaarded27a12018-08-01 19:06:03 +02001174 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001175 return NULL;
1176
1177 if (rettv.v_type != VAR_LIST)
1178 {
1179 clear_tv(&rettv);
1180 return NULL;
1181 }
1182
1183 return rettv.vval.v_list;
1184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
Bram Moolenaar05159a02005-02-26 23:04:13 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#ifdef FEAT_FOLDING
1189/*
1190 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1191 * it in "*cp". Doesn't give error messages.
1192 */
1193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar33570922005-01-25 22:26:29 +00001196 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001197 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001199 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1200 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001203 if (use_sandbox)
1204 ++sandbox;
1205 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = 0;
1209 else
1210 {
1211 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 if (tv.v_type == VAR_NUMBER)
1213 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001214 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 retval = 0;
1216 else
1217 {
1218 /* If the result is a string, check if there is a non-digit before
1219 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (!VIM_ISDIGIT(*s) && *s != '-')
1222 *cp = *s++;
1223 retval = atol((char *)s);
1224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 if (use_sandbox)
1229 --sandbox;
1230 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001232 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233}
1234#endif
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001237 * Get a list of lines from a HERE document. The here document is a list of
1238 * lines surrounded by a marker.
1239 * cmd << {marker}
1240 * {line1}
1241 * {line2}
1242 * ....
1243 * {marker}
1244 *
1245 * The {marker} is a string. If the optional 'trim' word is supplied before the
1246 * marker, then the leading indentation before the lines (matching the
1247 * indentation in the 'cmd' line) is stripped.
1248 * Returns a List with {lines} or NULL.
1249 */
1250 static list_T *
1251heredoc_get(exarg_T *eap, char_u *cmd)
1252{
1253 char_u *theline;
1254 char_u *marker;
1255 list_T *l;
1256 char_u *p;
1257 int indent_len = 0;
1258
1259 if (eap->getline == NULL)
1260 {
1261 emsg(_("E991: cannot use =<< here"));
1262 return NULL;
1263 }
1264
1265 // Check for the optional 'trim' word before the marker
1266 cmd = skipwhite(cmd);
1267 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1268 {
1269 cmd = skipwhite(cmd + 4);
1270
1271 // Trim the indentation from all the lines in the here document
1272 // The amount of indentation trimmed is the same as the indentation of
1273 // the :let command line.
1274 p = *eap->cmdlinep;
1275 while (VIM_ISWHITE(*p))
1276 {
1277 p++;
1278 indent_len++;
1279 }
1280 }
1281
1282 // The marker is the next word. Default marker is "."
1283 if (*cmd != NUL && *cmd != '"')
1284 {
1285 marker = skipwhite(cmd);
1286 p = skiptowhite(marker);
1287 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1288 {
1289 emsg(_(e_trailing));
1290 return NULL;
1291 }
1292 *p = NUL;
1293 }
1294 else
1295 marker = (char_u *)".";
1296
1297 l = list_alloc();
1298 if (l == NULL)
1299 return NULL;
1300
1301 for (;;)
1302 {
1303 int i = 0;
1304
1305 theline = eap->getline(NUL, eap->cookie, 0);
1306 if (theline != NULL && indent_len > 0)
1307 {
1308 // trim the indent matching the first line
1309 if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0)
1310 i = indent_len;
1311 }
1312
1313 if (theline == NULL)
1314 {
1315 semsg(_("E990: Missing end marker '%s'"), marker);
1316 break;
1317 }
1318 if (STRCMP(marker, theline + i) == 0)
1319 {
1320 vim_free(theline);
1321 break;
1322 }
1323
1324 if (list_append_string(l, theline + i, -1) == FAIL)
1325 break;
1326 vim_free(theline);
1327 }
1328
1329 return l;
1330}
1331
1332/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 * ":let" list all variable values
1334 * ":let var1 var2" list variable values
1335 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336 * ":let var += expr" assignment command.
1337 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001338 * ":let var *= expr" assignment command.
1339 * ":let var /= expr" assignment command.
1340 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001341 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001342 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 */
1345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001346ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001348 ex_let_const(eap, FALSE);
1349}
1350
1351/*
1352 * ":const" list all variable values
1353 * ":const var1 var2" list variable values
1354 * ":const var = expr" assignment command.
1355 * ":const [var1, var2] = expr" unpack list.
1356 */
1357 void
1358ex_const(exarg_T *eap)
1359{
1360 ex_let_const(eap, TRUE);
1361}
1362
1363 static void
1364ex_let_const(exarg_T *eap, int is_const)
1365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 int var_count = 0;
1371 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001373 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001374 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001375 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
Bram Moolenaardb552d602006-03-23 22:59:57 +00001377 argend = skip_var_list(arg, &var_count, &semicolon);
1378 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001379 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001380 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001381 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001382 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001383 concat = expr[0] == '.'
1384 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1385 || (expr[1] == '.' && expr[2] == '='));
1386 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1387 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001389 /*
1390 * ":let" without "=": list variables
1391 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001392 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001393 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001394 else if (expr[0] == '.')
1395 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001396 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001398 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001402 list_glob_vars(&first);
1403 list_buf_vars(&first);
1404 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001405 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001406 list_script_vars(&first);
1407 list_func_vars(&first);
1408 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 eap->nextcmd = check_nextcmd(arg);
1411 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001412 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1413 {
1414 list_T *l;
1415
1416 // HERE document
1417 l = heredoc_get(eap, expr + 3);
1418 if (l != NULL)
1419 {
1420 rettv_list_set(&rettv, l);
1421 op[0] = '=';
1422 op[1] = NUL;
1423 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001424 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001425 clear_tv(&rettv);
1426 }
1427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 else
1429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 op[0] = '=';
1431 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001432 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001434 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001435 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001436 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001437 if (expr[0] == '.' && expr[1] == '.') // ..=
1438 ++expr;
1439 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001440 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001441 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001442 else
1443 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (eap->skip)
1446 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001447 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (eap->skip)
1449 {
1450 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 --emsg_skip;
1453 }
1454 else if (i != FAIL)
1455 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001456 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001457 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460 }
1461}
1462
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001463/*
1464 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1465 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 * When "nextchars" is not NULL it points to a string with characters that
1467 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1468 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001469 * Returns OK or FAIL;
1470 */
1471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472ex_let_vars(
1473 char_u *arg_start,
1474 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001475 int copy, // copy values from "tv", don't move
1476 int semicolon, // from skip_var_list()
1477 int var_count, // from skip_var_list()
1478 int is_const, // lock variables for const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001479 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001480{
1481 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001482 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001484 listitem_T *item;
1485 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001486
1487 if (*arg != '[')
1488 {
1489 /*
1490 * ":let var = expr" or ":for var in list"
1491 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001492 if (ex_let_one(arg, tv, copy, is_const, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001493 return FAIL;
1494 return OK;
1495 }
1496
1497 /*
1498 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1499 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001500 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001502 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503 return FAIL;
1504 }
1505
1506 i = list_len(l);
1507 if (semicolon == 0 && var_count < i)
1508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001509 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001510 return FAIL;
1511 }
1512 if (var_count - semicolon > i)
1513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001514 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515 return FAIL;
1516 }
1517
1518 item = l->lv_first;
1519 while (*arg != ']')
1520 {
1521 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001522 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
1523 (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001524 item = item->li_next;
1525 if (arg == NULL)
1526 return FAIL;
1527
1528 arg = skipwhite(arg);
1529 if (*arg == ';')
1530 {
1531 /* Put the rest of the list (may be empty) in the var after ';'.
1532 * Create a new list for this. */
1533 l = list_alloc();
1534 if (l == NULL)
1535 return FAIL;
1536 while (item != NULL)
1537 {
1538 list_append_tv(l, &item->li_tv);
1539 item = item->li_next;
1540 }
1541
1542 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001543 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001544 ltv.vval.v_list = l;
1545 l->lv_refcount = 1;
1546
Bram Moolenaar9937a052019-06-15 15:45:06 +02001547 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
1548 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549 clear_tv(&ltv);
1550 if (arg == NULL)
1551 return FAIL;
1552 break;
1553 }
1554 else if (*arg != ',' && *arg != ']')
1555 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001556 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 return FAIL;
1558 }
1559 }
1560
1561 return OK;
1562}
1563
1564/*
1565 * Skip over assignable variable "var" or list of variables "[var, var]".
1566 * Used for ":let varvar = expr" and ":for varvar in expr".
1567 * For "[var, var]" increment "*var_count" for each variable.
1568 * for "[var, var; var]" set "semicolon".
1569 * Return NULL for an error.
1570 */
1571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572skip_var_list(
1573 char_u *arg,
1574 int *var_count,
1575 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576{
1577 char_u *p, *s;
1578
1579 if (*arg == '[')
1580 {
1581 /* "[var, var]": find the matching ']'. */
1582 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 {
1585 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1586 s = skip_var_one(p);
1587 if (s == p)
1588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001589 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001590 return NULL;
1591 }
1592 ++*var_count;
1593
1594 p = skipwhite(s);
1595 if (*p == ']')
1596 break;
1597 else if (*p == ';')
1598 {
1599 if (*semicolon == 1)
1600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001601 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602 return NULL;
1603 }
1604 *semicolon = 1;
1605 }
1606 else if (*p != ',')
1607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001608 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609 return NULL;
1610 }
1611 }
1612 return p + 1;
1613 }
1614 else
1615 return skip_var_one(arg);
1616}
1617
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001618/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001619 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001620 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001621 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001624{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001625 if (*arg == '@' && arg[1] != NUL)
1626 return arg + 2;
1627 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1628 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629}
1630
Bram Moolenaara7043832005-01-21 11:56:39 +00001631/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 * List variables for hashtab "ht" with prefix "prefix".
1633 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001634 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636list_hashtable_vars(
1637 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001638 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639 int empty,
1640 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001641{
Bram Moolenaar33570922005-01-25 22:26:29 +00001642 hashitem_T *hi;
1643 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001644 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001645 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001646
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001647 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001648 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1649 {
1650 if (!HASHITEM_EMPTY(hi))
1651 {
1652 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001653 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001654
1655 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001656 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1657 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001658 if (message_filtered(buf))
1659 continue;
1660
Bram Moolenaar33570922005-01-25 22:26:29 +00001661 if (empty || di->di_tv.v_type != VAR_STRING
1662 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001663 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001664 }
1665 }
1666}
1667
1668/*
1669 * List global variables.
1670 */
1671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001673{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001674 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001675}
1676
1677/*
1678 * List buffer variables.
1679 */
1680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001681list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001682{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001683 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001684}
1685
1686/*
1687 * List window variables.
1688 */
1689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001690list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001691{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001692 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001693}
1694
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001695/*
1696 * List tab page variables.
1697 */
1698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001700{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001701 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001702}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001703
Bram Moolenaara7043832005-01-21 11:56:39 +00001704/*
1705 * List Vim variables.
1706 */
1707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001710 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711}
1712
1713/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001714 * List script-local variables, if there is a script.
1715 */
1716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001717list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001718{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001719 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1720 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001721 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001722}
1723
1724/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725 * List variables in "arg".
1726 */
1727 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729{
1730 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001731 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001733 char_u *name_start;
1734 char_u *arg_subsc;
1735 char_u *tofree;
1736 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001737
1738 while (!ends_excmd(*arg) && !got_int)
1739 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001740 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001742 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001743 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001744 {
1745 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001746 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001747 break;
1748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001749 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001752 /* get_name_len() takes care of expanding curly braces */
1753 name_start = name = arg;
1754 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1755 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001756 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001757 /* This is mainly to keep test 49 working: when expanding
1758 * curly braces fails overrule the exception error message. */
1759 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001761 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001762 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001763 break;
1764 }
1765 error = TRUE;
1766 }
1767 else
1768 {
1769 if (tofree != NULL)
1770 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001771 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 else
1774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001775 /* handle d.key, l[idx], f(expr) */
1776 arg_subsc = arg;
1777 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001778 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001781 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001782 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001783 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001784 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001785 case 'g': list_glob_vars(first); break;
1786 case 'b': list_buf_vars(first); break;
1787 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001788 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001789 case 'v': list_vim_vars(first); break;
1790 case 's': list_script_vars(first); break;
1791 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001792 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001793 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001794 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001795 }
1796 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001797 {
1798 char_u numbuf[NUMBUFLEN];
1799 char_u *tf;
1800 int c;
1801 char_u *s;
1802
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001804 c = *arg;
1805 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001806 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001807 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001808 tv.v_type,
1809 s == NULL ? (char_u *)"" : s,
1810 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001811 *arg = c;
1812 vim_free(tf);
1813 }
1814 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 }
1817 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818
1819 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001821
1822 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 }
1824
1825 return arg;
1826}
1827
1828/*
1829 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1830 * Returns a pointer to the char just after the var name.
1831 * Returns NULL if there is an error.
1832 */
1833 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001835 char_u *arg, // points to variable name
1836 typval_T *tv, // value to assign to variable
1837 int copy, // copy value from "tv"
1838 int is_const, // lock variable for const
1839 char_u *endchars, // valid chars after variable name or NULL
1840 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841{
1842 int c1;
1843 char_u *name;
1844 char_u *p;
1845 char_u *arg_end = NULL;
1846 int len;
1847 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001848 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849
1850 /*
1851 * ":let $VAR = expr": Set environment variable.
1852 */
1853 if (*arg == '$')
1854 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001855 if (is_const)
1856 {
1857 emsg(_("E996: Cannot lock an environment variable"));
1858 return NULL;
1859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* Find the end of the name. */
1861 ++arg;
1862 name = arg;
1863 len = get_env_len(&arg);
1864 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001865 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 else
1867 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001868 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001869 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001872 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001873 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 {
1875 c1 = name[len];
1876 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001877 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001878 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 {
1880 int mustfree = FALSE;
1881 char_u *s = vim_getenv(name, &mustfree);
1882
1883 if (s != NULL)
1884 {
1885 p = tofree = concat_str(s, p);
1886 if (mustfree)
1887 vim_free(s);
1888 }
1889 }
1890 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001893 if (STRICMP(name, "HOME") == 0)
1894 init_homedir();
1895 else if (didset_vim && STRICMP(name, "VIM") == 0)
1896 didset_vim = FALSE;
1897 else if (didset_vimruntime
1898 && STRICMP(name, "VIMRUNTIME") == 0)
1899 didset_vimruntime = FALSE;
1900 arg_end = arg;
1901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 }
1905 }
1906 }
1907
1908 /*
1909 * ":let &option = expr": Set option value.
1910 * ":let &l:option = expr": Set local option value.
1911 * ":let &g:option = expr": Set global option value.
1912 */
1913 else if (*arg == '&')
1914 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001915 if (is_const)
1916 {
1917 emsg(_("E996: Cannot lock an option"));
1918 return NULL;
1919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* Find the end of the name. */
1921 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 if (p == NULL || (endchars != NULL
1923 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001924 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 else
1926 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 long n;
1928 int opt_type;
1929 long numval;
1930 char_u *stringval = NULL;
1931 char_u *s;
1932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 c1 = *p;
1934 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001936 n = (long)tv_get_number(tv);
1937 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001938 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 {
1940 opt_type = get_option_value(arg, &numval,
1941 &stringval, opt_flags);
1942 if ((opt_type == 1 && *op == '.')
1943 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001945 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001946 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001947 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 else
1949 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001950 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001952 switch (*op)
1953 {
1954 case '+': n = numval + n; break;
1955 case '-': n = numval - n; break;
1956 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001957 case '/': n = (long)num_divide(numval, n); break;
1958 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001959 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001961 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 {
1963 s = concat_str(stringval, s);
1964 vim_free(stringval);
1965 stringval = s;
1966 }
1967 }
1968 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969 if (s != NULL)
1970 {
1971 set_option_value(arg, n, s, opt_flags);
1972 arg_end = p;
1973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 }
1977 }
1978
1979 /*
1980 * ":let @r = expr": Set register contents.
1981 */
1982 else if (*arg == '@')
1983 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001984 if (is_const)
1985 {
1986 emsg(_("E996: Cannot lock a register"));
1987 return NULL;
1988 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001990 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 else
1996 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001997 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 char_u *s;
1999
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002000 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002003 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 if (s != NULL)
2005 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002006 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002007 vim_free(s);
2008 }
2009 }
2010 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002011 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002012 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002013 arg_end = arg + 1;
2014 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002015 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 }
2017 }
2018
2019 /*
2020 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002023 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002025 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002027 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002031 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 else
2033 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002034 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 arg_end = p;
2036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 }
2040
2041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043
2044 return arg_end;
2045}
2046
2047/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 * Get an lval: variable, Dict item or List item that can be assigned a value
2049 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2050 * "name.key", "name.key[expr]" etc.
2051 * Indexing only works if "name" is an existing List or Dictionary.
2052 * "name" points to the start of the name.
2053 * If "rettv" is not NULL it points to the value to be assigned.
2054 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2055 * wrong; must end in space or cmd separator.
2056 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002057 * flags:
2058 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002059 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002060 * GLV_NO_AUTOLOAD: do not use script autoloading
2061 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002063 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002066 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002067get_lval(
2068 char_u *name,
2069 typval_T *rettv,
2070 lval_T *lp,
2071 int unlet,
2072 int skip,
2073 int flags, /* GLV_ values */
2074 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 char_u *expr_start, *expr_end;
2078 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 dictitem_T *v;
2080 typval_T var1;
2081 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002087 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091
2092 if (skip)
2093 {
2094 /* When skipping just find the end of the name. */
2095 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002096 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 }
2098
2099 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002100 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002101 if (expr_start != NULL)
2102 {
2103 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002104 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 && *p != '[' && *p != '.')
2106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002107 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 return NULL;
2109 }
2110
2111 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2112 if (lp->ll_exp_name == NULL)
2113 {
2114 /* Report an invalid expression in braces, unless the
2115 * expression evaluation has been cancelled due to an
2116 * aborting error, an interrupt, or an exception. */
2117 if (!aborting() && !quiet)
2118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002119 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 return NULL;
2122 }
2123 }
2124 lp->ll_name = lp->ll_exp_name;
2125 }
2126 else
2127 lp->ll_name = name;
2128
2129 /* Without [idx] or .key we are done. */
2130 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2131 return p;
2132
2133 cc = *p;
2134 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002135 /* Only pass &ht when we would write to the variable, it prevents autoload
2136 * as well. */
2137 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2138 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002140 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 if (v == NULL)
2143 return NULL;
2144
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 /*
2146 * Loop until no more [idx] or .key is following.
2147 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002148 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002149 var1.v_type = VAR_UNKNOWN;
2150 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2154 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002155 && lp->ll_tv->vval.v_dict != NULL)
2156 && !(lp->ll_tv->v_type == VAR_BLOB
2157 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002169
Bram Moolenaar8c711452005-01-14 21:53:12 +00002170 len = -1;
2171 if (*p == '.')
2172 {
2173 key = p + 1;
2174 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2175 ;
2176 if (len == 0)
2177 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002179 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002181 }
2182 p = key + len;
2183 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002184 else
2185 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002187 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002188 if (*p == ':')
2189 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002190 else
2191 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 empty1 = FALSE;
2193 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002195 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 {
2197 /* not a number or string */
2198 clear_tv(&var1);
2199 return NULL;
2200 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 }
2202
2203 /* Optionally get the second index [ :expr]. */
2204 if (*p == ':')
2205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002210 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002212 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002213 if (rettv != NULL
2214 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002215 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002216 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002217 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002220 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002221 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 }
2224 p = skipwhite(p + 1);
2225 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 else
2228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002230 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2231 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002232 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002234 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002235 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002236 {
2237 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002238 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002239 clear_tv(&var2);
2240 return NULL;
2241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002247
Bram Moolenaar8c711452005-01-14 21:53:12 +00002248 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002252 clear_tv(&var1);
2253 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002255 }
2256
2257 /* Skip to past ']'. */
2258 ++p;
2259 }
2260
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002262 {
2263 if (len == -1)
2264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002266 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002267 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002269 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 }
2272 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 lp->ll_list = NULL;
2274 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002275 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002276
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002277 /* When assigning to a scope dictionary check that a function and
2278 * variable name is valid (only variable name unless it is l: or
2279 * g: dictionary). Disallow overwriting a builtin function. */
2280 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002281 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002282 int prevval;
2283 int wrong;
2284
2285 if (len != -1)
2286 {
2287 prevval = key[len];
2288 key[len] = NUL;
2289 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002290 else
2291 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002292 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2293 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002294 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002295 || !valid_varname(key);
2296 if (len != -1)
2297 key[len] = prevval;
2298 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002299 return NULL;
2300 }
2301
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002303 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002304 // Can't add "v:" or "a:" variable.
2305 if (lp->ll_dict == &vimvardict
2306 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002308 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002309 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002310 return NULL;
2311 }
2312
Bram Moolenaar31b81602019-02-10 22:14:27 +01002313 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002317 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002318 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002320 }
2321 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002323 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002325 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 p = NULL;
2328 break;
2329 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002330 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002331 else if ((flags & GLV_READ_ONLY) == 0
2332 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002333 {
2334 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002335 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002336 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002337
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002338 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002340 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002341 else if (lp->ll_tv->v_type == VAR_BLOB)
2342 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002343 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2344
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002345 /*
2346 * Get the number and item for the only or first index of the List.
2347 */
2348 if (empty1)
2349 lp->ll_n1 = 0;
2350 else
2351 // is number or string
2352 lp->ll_n1 = (long)tv_get_number(&var1);
2353 clear_tv(&var1);
2354
2355 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002356 || lp->ll_n1 > bloblen
2357 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002358 {
2359 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002360 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002361 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002362 return NULL;
2363 }
2364 if (lp->ll_range && !lp->ll_empty2)
2365 {
2366 lp->ll_n2 = (long)tv_get_number(&var2);
2367 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002368 if (lp->ll_n2 < 0
2369 || lp->ll_n2 >= bloblen
2370 || lp->ll_n2 < lp->ll_n1)
2371 {
2372 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002373 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002374 return NULL;
2375 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002376 }
2377 lp->ll_blob = lp->ll_tv->vval.v_blob;
2378 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002379 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 else
2382 {
2383 /*
2384 * Get the number and item for the only or first index of the List.
2385 */
2386 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002389 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002390 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002391 clear_tv(&var1);
2392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 lp->ll_list = lp->ll_tv->vval.v_list;
2395 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2396 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002398 if (lp->ll_n1 < 0)
2399 {
2400 lp->ll_n1 = 0;
2401 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2402 }
2403 }
2404 if (lp->ll_li == NULL)
2405 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002406 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002407 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 }
2411
2412 /*
2413 * May need to find the item or absolute index for the second
2414 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 * When no index given: "lp->ll_empty2" is TRUE.
2416 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002420 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002421 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002427 {
2428 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002429 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002431 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
2434
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2436 if (lp->ll_n1 < 0)
2437 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2438 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002439 {
2440 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002441 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002443 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002444 }
2445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
2449
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002450 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 return p;
2452}
2453
2454/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002455 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002457 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002458clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459{
2460 vim_free(lp->ll_exp_name);
2461 vim_free(lp->ll_newkey);
2462}
2463
2464/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002465 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002467 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2468 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 */
2470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002471set_var_lval(
2472 lval_T *lp,
2473 char_u *endp,
2474 typval_T *rettv,
2475 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002476 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002477 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478{
2479 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002480 listitem_T *ri;
2481 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482
2483 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002485 cc = *endp;
2486 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002487 if (lp->ll_blob != NULL)
2488 {
2489 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002490
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002491 if (op != NULL && *op != '=')
2492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002493 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002494 return;
2495 }
2496
2497 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2498 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002499 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002500
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002501 if (lp->ll_empty2)
2502 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2503
2504 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002506 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002507 return;
2508 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002509 if (lp->ll_empty2)
2510 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002511
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002512 ir = 0;
2513 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2514 blob_set(lp->ll_blob, il,
2515 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002516 }
2517 else
2518 {
2519 val = (int)tv_get_number_chk(rettv, &error);
2520 if (!error)
2521 {
2522 garray_T *gap = &lp->ll_blob->bv_ga;
2523
2524 // Allow for appending a byte. Setting a byte beyond
2525 // the end is an error otherwise.
2526 if (lp->ll_n1 < gap->ga_len
2527 || (lp->ll_n1 == gap->ga_len
2528 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2529 {
2530 blob_set(lp->ll_blob, lp->ll_n1, val);
2531 if (lp->ll_n1 == gap->ga_len)
2532 ++gap->ga_len;
2533 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002534 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002535 }
2536 }
2537 }
2538 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002540 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541
Bram Moolenaar9937a052019-06-15 15:45:06 +02002542 if (is_const)
2543 {
2544 emsg(_(e_cannot_mod));
2545 *endp = cc;
2546 return;
2547 }
2548
Bram Moolenaarff697e62019-02-12 22:28:33 +01002549 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002550 di = NULL;
2551 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2552 &tv, &di, TRUE, FALSE) == OK)
2553 {
2554 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002555 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2556 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002557 && tv_op(&tv, rettv, op) == OK)
2558 set_var(lp->ll_name, &tv, FALSE);
2559 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002562 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002563 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002564 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002566 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002567 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002568 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002569 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 else if (lp->ll_range)
2571 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002572 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002573 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002574
Bram Moolenaar9937a052019-06-15 15:45:06 +02002575 if (is_const)
2576 {
2577 emsg(_("E996: Cannot lock a range"));
2578 return;
2579 }
2580
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002581 /*
2582 * Check whether any of the list items is locked
2583 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002584 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002585 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002586 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002587 return;
2588 ri = ri->li_next;
2589 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2590 break;
2591 ll_li = ll_li->li_next;
2592 ++ll_n1;
2593 }
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /*
2596 * Assign the List values to the list items.
2597 */
2598 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002600 if (op != NULL && *op != '=')
2601 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2602 else
2603 {
2604 clear_tv(&lp->ll_li->li_tv);
2605 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2606 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 ri = ri->li_next;
2608 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2609 break;
2610 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002613 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 ri = NULL;
2616 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002618 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_li = lp->ll_li->li_next;
2620 ++lp->ll_n1;
2621 }
2622 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002623 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 else if (lp->ll_empty2
2625 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002627 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 }
2629 else
2630 {
2631 /*
2632 * Assign to a List or Dictionary item.
2633 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002634 if (is_const)
2635 {
2636 emsg(_("E996: Cannot lock a list or dict"));
2637 return;
2638 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (lp->ll_newkey != NULL)
2640 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 if (op != NULL && *op != '=')
2642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002643 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 return;
2645 }
2646
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002648 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (di == NULL)
2650 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002651 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2652 {
2653 vim_free(di);
2654 return;
2655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 else if (op != NULL && *op != '=')
2659 {
2660 tv_op(lp->ll_tv, rettv, op);
2661 return;
2662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 /*
2667 * Assign the value to the variable or list item.
2668 */
2669 if (copy)
2670 copy_tv(rettv, lp->ll_tv);
2671 else
2672 {
2673 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002674 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 }
2677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678}
2679
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002680/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002681 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2682 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 * Returns OK or FAIL.
2684 */
2685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002686tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002688 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 char_u numbuf[NUMBUFLEN];
2690 char_u *s;
2691
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002692 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2693 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2694 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002695 {
2696 switch (tv1->v_type)
2697 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002698 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 case VAR_DICT:
2700 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002701 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002702 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002703 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002704 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 break;
2706
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002707 case VAR_BLOB:
2708 if (*op != '+' || tv2->v_type != VAR_BLOB)
2709 break;
2710 // BLOB += BLOB
2711 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2712 {
2713 blob_T *b1 = tv1->vval.v_blob;
2714 blob_T *b2 = tv2->vval.v_blob;
2715 int i, len = blob_len(b2);
2716 for (i = 0; i < len; i++)
2717 ga_append(&b1->bv_ga, blob_get(b2, i));
2718 }
2719 return OK;
2720
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002721 case VAR_LIST:
2722 if (*op != '+' || tv2->v_type != VAR_LIST)
2723 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002724 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2726 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2727 return OK;
2728
2729 case VAR_NUMBER:
2730 case VAR_STRING:
2731 if (tv2->v_type == VAR_LIST)
2732 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002733 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002735 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002736 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002737#ifdef FEAT_FLOAT
2738 if (tv2->v_type == VAR_FLOAT)
2739 {
2740 float_T f = n;
2741
Bram Moolenaarff697e62019-02-12 22:28:33 +01002742 if (*op == '%')
2743 break;
2744 switch (*op)
2745 {
2746 case '+': f += tv2->vval.v_float; break;
2747 case '-': f -= tv2->vval.v_float; break;
2748 case '*': f *= tv2->vval.v_float; break;
2749 case '/': f /= tv2->vval.v_float; break;
2750 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002751 clear_tv(tv1);
2752 tv1->v_type = VAR_FLOAT;
2753 tv1->vval.v_float = f;
2754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756#endif
2757 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002758 switch (*op)
2759 {
2760 case '+': n += tv_get_number(tv2); break;
2761 case '-': n -= tv_get_number(tv2); break;
2762 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002763 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2764 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002765 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002766 clear_tv(tv1);
2767 tv1->v_type = VAR_NUMBER;
2768 tv1->vval.v_number = n;
2769 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 }
2771 else
2772 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002773 if (tv2->v_type == VAR_FLOAT)
2774 break;
2775
Bram Moolenaarff697e62019-02-12 22:28:33 +01002776 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 s = tv_get_string(tv1);
2778 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 clear_tv(tv1);
2780 tv1->v_type = VAR_STRING;
2781 tv1->vval.v_string = s;
2782 }
2783 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002784
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002785 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002786#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002787 {
2788 float_T f;
2789
Bram Moolenaarff697e62019-02-12 22:28:33 +01002790 if (*op == '%' || *op == '.'
2791 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002792 && tv2->v_type != VAR_NUMBER
2793 && tv2->v_type != VAR_STRING))
2794 break;
2795 if (tv2->v_type == VAR_FLOAT)
2796 f = tv2->vval.v_float;
2797 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002798 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002799 switch (*op)
2800 {
2801 case '+': tv1->vval.v_float += f; break;
2802 case '-': tv1->vval.v_float -= f; break;
2803 case '*': tv1->vval.v_float *= f; break;
2804 case '/': tv1->vval.v_float /= f; break;
2805 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002806 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002807#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002808 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 }
2810 }
2811
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002812 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 return FAIL;
2814}
2815
2816/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002817 * Evaluate the expression used in a ":for var in expr" command.
2818 * "arg" points to "var".
2819 * Set "*errp" to TRUE for an error, FALSE otherwise;
2820 * Return a pointer that holds the info. Null when there is an error.
2821 */
2822 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002823eval_for_line(
2824 char_u *arg,
2825 int *errp,
2826 char_u **nextcmdp,
2827 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828{
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002831 typval_T tv;
2832 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833
2834 *errp = TRUE; /* default: there is an error */
2835
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002836 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002837 if (fi == NULL)
2838 return NULL;
2839
2840 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2841 if (expr == NULL)
2842 return fi;
2843
2844 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002845 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002847 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 return fi;
2849 }
2850
2851 if (skip)
2852 ++emsg_skip;
2853 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2854 {
2855 *errp = FALSE;
2856 if (!skip)
2857 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002858 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002859 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002860 l = tv.vval.v_list;
2861 if (l == NULL)
2862 {
2863 // a null list is like an empty list: do nothing
2864 clear_tv(&tv);
2865 }
2866 else
2867 {
2868 // No need to increment the refcount, it's already set for
2869 // the list being used in "tv".
2870 fi->fi_list = l;
2871 list_add_watch(l, &fi->fi_lw);
2872 fi->fi_lw.lw_item = l->lv_first;
2873 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002874 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002875 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002876 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002877 fi->fi_bi = 0;
2878 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002879 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002880 typval_T btv;
2881
2882 // Make a copy, so that the iteration still works when the
2883 // blob is changed.
2884 blob_copy(&tv, &btv);
2885 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002886 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002887 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 else
2890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002891 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002892 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893 }
2894 }
2895 }
2896 if (skip)
2897 --emsg_skip;
2898
2899 return fi;
2900}
2901
2902/*
2903 * Use the first item in a ":for" list. Advance to the next.
2904 * Assign the values to the variable (list). "arg" points to the first one.
2905 * Return TRUE when a valid item was found, FALSE when at end of list or
2906 * something wrong.
2907 */
2908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002909next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002911 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002915 if (fi->fi_blob != NULL)
2916 {
2917 typval_T tv;
2918
2919 if (fi->fi_bi >= blob_len(fi->fi_blob))
2920 return FALSE;
2921 tv.v_type = VAR_NUMBER;
2922 tv.v_lock = VAR_FIXED;
2923 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2924 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002925 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2926 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002927 }
2928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929 item = fi->fi_lw.lw_item;
2930 if (item == NULL)
2931 result = FALSE;
2932 else
2933 {
2934 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002935 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2936 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937 }
2938 return result;
2939}
2940
2941/*
2942 * Free the structure used to store info used by ":for".
2943 */
2944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946{
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002949 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002950 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002951 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002952 list_unref(fi->fi_list);
2953 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002954 if (fi != NULL && fi->fi_blob != NULL)
2955 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956 vim_free(fi);
2957}
2958
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2960
2961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002962set_context_for_expression(
2963 expand_T *xp,
2964 char_u *arg,
2965 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 int got_eq = FALSE;
2968 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971 if (cmdidx == CMD_let)
2972 {
2973 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002974 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 {
2976 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002977 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978 {
2979 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002980 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002981 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982 break;
2983 }
2984 return;
2985 }
2986 }
2987 else
2988 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2989 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 while ((xp->xp_pattern = vim_strpbrk(arg,
2991 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2992 {
2993 c = *xp->xp_pattern;
2994 if (c == '&')
2995 {
2996 c = xp->xp_pattern[1];
2997 if (c == '&')
2998 {
2999 ++xp->xp_pattern;
3000 xp->xp_context = cmdidx != CMD_let || got_eq
3001 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3002 }
3003 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003006 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3007 xp->xp_pattern += 2;
3008
3009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011 else if (c == '$')
3012 {
3013 /* environment variable */
3014 xp->xp_context = EXPAND_ENV_VARS;
3015 }
3016 else if (c == '=')
3017 {
3018 got_eq = TRUE;
3019 xp->xp_context = EXPAND_EXPRESSION;
3020 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003021 else if (c == '#'
3022 && xp->xp_context == EXPAND_EXPRESSION)
3023 {
3024 /* Autoload function/variable contains '#'. */
3025 break;
3026 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003027 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 && xp->xp_context == EXPAND_FUNCTIONS
3029 && vim_strchr(xp->xp_pattern, '(') == NULL)
3030 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003031 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 break;
3033 }
3034 else if (cmdidx != CMD_let || got_eq)
3035 {
3036 if (c == '"') /* string */
3037 {
3038 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3039 if (c == '\\' && xp->xp_pattern[1] != NUL)
3040 ++xp->xp_pattern;
3041 xp->xp_context = EXPAND_NOTHING;
3042 }
3043 else if (c == '\'') /* literal string */
3044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003045 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3047 /* skip */ ;
3048 xp->xp_context = EXPAND_NOTHING;
3049 }
3050 else if (c == '|')
3051 {
3052 if (xp->xp_pattern[1] == '|')
3053 {
3054 ++xp->xp_pattern;
3055 xp->xp_context = EXPAND_EXPRESSION;
3056 }
3057 else
3058 xp->xp_context = EXPAND_COMMANDS;
3059 }
3060 else
3061 xp->xp_context = EXPAND_EXPRESSION;
3062 }
3063 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 /* Doesn't look like something valid, expand as an expression
3065 * anyway. */
3066 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 arg = xp->xp_pattern;
3068 if (*arg != NUL)
3069 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3070 /* skip */ ;
3071 }
3072 xp->xp_pattern = arg;
3073}
3074
3075#endif /* FEAT_CMDL_COMPL */
3076
3077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * ":unlet[!] var1 ... " command.
3079 */
3080 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003083 ex_unletlock(eap, eap->arg, 0);
3084}
3085
3086/*
3087 * ":lockvar" and ":unlockvar" commands
3088 */
3089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003090ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003091{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003093 int deep = 2;
3094
3095 if (eap->forceit)
3096 deep = -1;
3097 else if (vim_isdigit(*arg))
3098 {
3099 deep = getdigits(&arg);
3100 arg = skipwhite(arg);
3101 }
3102
3103 ex_unletlock(eap, arg, deep);
3104}
3105
3106/*
3107 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3108 */
3109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003110ex_unletlock(
3111 exarg_T *eap,
3112 char_u *argstart,
3113 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114{
3115 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003118 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
3120 do
3121 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003122 if (*arg == '$')
3123 {
3124 char_u *name = ++arg;
3125
3126 if (get_env_len(&arg) == 0)
3127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003129 return;
3130 }
3131 vim_unsetenv(name);
3132 arg = skipwhite(arg);
3133 continue;
3134 }
3135
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003136 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003137 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003138 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003139 if (lv.ll_name == NULL)
3140 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003141 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003142 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003144 if (name_end != NULL)
3145 {
3146 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003147 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003148 }
3149 if (!(eap->skip || error))
3150 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 break;
3152 }
3153
3154 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003155 {
3156 if (eap->cmdidx == CMD_unlet)
3157 {
3158 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3159 error = TRUE;
3160 }
3161 else
3162 {
3163 if (do_lock_var(&lv, name_end, deep,
3164 eap->cmdidx == CMD_lockvar) == FAIL)
3165 error = TRUE;
3166 }
3167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 if (!eap->skip)
3170 clear_lval(&lv);
3171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 arg = skipwhite(name_end);
3173 } while (!ends_excmd(*arg));
3174
3175 eap->nextcmd = check_nextcmd(arg);
3176}
3177
Bram Moolenaar8c711452005-01-14 21:53:12 +00003178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179do_unlet_var(
3180 lval_T *lp,
3181 char_u *name_end,
3182 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003183{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003184 int ret = OK;
3185 int cc;
3186
3187 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003189 cc = *name_end;
3190 *name_end = NUL;
3191
3192 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003193 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003194 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003195 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003196 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003197 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003198 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003199 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003200 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003201 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003202 else if (lp->ll_range)
3203 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003205 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003206 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003207
3208 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3209 {
3210 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003211 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003212 return FAIL;
3213 ll_li = li;
3214 ++ll_n1;
3215 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216
3217 /* Delete a range of List items. */
3218 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3219 {
3220 li = lp->ll_li->li_next;
3221 listitem_remove(lp->ll_list, lp->ll_li);
3222 lp->ll_li = li;
3223 ++lp->ll_n1;
3224 }
3225 }
3226 else
3227 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003229 /* unlet a List item. */
3230 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003233 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 }
3235
3236 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003237}
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239/*
3240 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003241 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
3243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 hashtab_T *ht;
3247 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003248 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003249 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003250 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 ht = find_var_ht(name, &varname);
3253 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003256 if (d == NULL)
3257 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 if (ht == &globvarht)
3259 d = &globvardict;
3260 else if (ht == &compat_hashtab)
3261 d = &vimvardict;
3262 else
3263 {
3264 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3265 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3266 }
3267 if (d == NULL)
3268 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003269 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003270 return FAIL;
3271 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003272 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003274 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003275 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003276 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003277 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003278 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003279 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003280 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003281 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003282 return FAIL;
3283
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 delete_var(ht, hi);
3285 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288 if (forceit)
3289 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003290 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292}
3293
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003294/*
3295 * Lock or unlock variable indicated by "lp".
3296 * "deep" is the levels to go (-1 for unlimited);
3297 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3298 */
3299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003300do_lock_var(
3301 lval_T *lp,
3302 char_u *name_end,
3303 int deep,
3304 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003305{
3306 int ret = OK;
3307 int cc;
3308 dictitem_T *di;
3309
3310 if (deep == 0) /* nothing to do */
3311 return OK;
3312
3313 if (lp->ll_tv == NULL)
3314 {
3315 cc = *name_end;
3316 *name_end = NUL;
3317
3318 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003319 di = find_var(lp->ll_name, NULL, TRUE);
3320 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003322 else if ((di->di_flags & DI_FLAGS_FIX)
3323 && di->di_tv.v_type != VAR_DICT
3324 && di->di_tv.v_type != VAR_LIST)
3325 /* For historic reasons this error is not given for a list or dict.
3326 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003327 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003328 else
3329 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003330 if (lock)
3331 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003332 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003333 di->di_flags &= ~DI_FLAGS_LOCK;
3334 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003335 }
3336 *name_end = cc;
3337 }
3338 else if (lp->ll_range)
3339 {
3340 listitem_T *li = lp->ll_li;
3341
3342 /* (un)lock a range of List items. */
3343 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3344 {
3345 item_lock(&li->li_tv, deep, lock);
3346 li = li->li_next;
3347 ++lp->ll_n1;
3348 }
3349 }
3350 else if (lp->ll_list != NULL)
3351 /* (un)lock a List item. */
3352 item_lock(&lp->ll_li->li_tv, deep, lock);
3353 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003354 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 item_lock(&lp->ll_di->di_tv, deep, lock);
3356
3357 return ret;
3358}
3359
3360/*
3361 * Lock or unlock an item. "deep" is nr of levels to go.
3362 */
3363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365{
3366 static int recurse = 0;
3367 list_T *l;
3368 listitem_T *li;
3369 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003370 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 hashitem_T *hi;
3372 int todo;
3373
3374 if (recurse >= DICT_MAXNEST)
3375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003376 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003377 return;
3378 }
3379 if (deep == 0)
3380 return;
3381 ++recurse;
3382
3383 /* lock/unlock the item itself */
3384 if (lock)
3385 tv->v_lock |= VAR_LOCKED;
3386 else
3387 tv->v_lock &= ~VAR_LOCKED;
3388
3389 switch (tv->v_type)
3390 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003391 case VAR_UNKNOWN:
3392 case VAR_NUMBER:
3393 case VAR_STRING:
3394 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003395 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003396 case VAR_FLOAT:
3397 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003398 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003399 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003400 break;
3401
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003402 case VAR_BLOB:
3403 if ((b = tv->vval.v_blob) != NULL)
3404 {
3405 if (lock)
3406 b->bv_lock |= VAR_LOCKED;
3407 else
3408 b->bv_lock &= ~VAR_LOCKED;
3409 }
3410 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003411 case VAR_LIST:
3412 if ((l = tv->vval.v_list) != NULL)
3413 {
3414 if (lock)
3415 l->lv_lock |= VAR_LOCKED;
3416 else
3417 l->lv_lock &= ~VAR_LOCKED;
3418 if (deep < 0 || deep > 1)
3419 /* recursive: lock/unlock the items the List contains */
3420 for (li = l->lv_first; li != NULL; li = li->li_next)
3421 item_lock(&li->li_tv, deep - 1, lock);
3422 }
3423 break;
3424 case VAR_DICT:
3425 if ((d = tv->vval.v_dict) != NULL)
3426 {
3427 if (lock)
3428 d->dv_lock |= VAR_LOCKED;
3429 else
3430 d->dv_lock &= ~VAR_LOCKED;
3431 if (deep < 0 || deep > 1)
3432 {
3433 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003435 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3436 {
3437 if (!HASHITEM_EMPTY(hi))
3438 {
3439 --todo;
3440 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3441 }
3442 }
3443 }
3444 }
3445 }
3446 --recurse;
3447}
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3450/*
3451 * Delete all "menutrans_" variables.
3452 */
3453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003457 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003462 {
3463 if (!HASHITEM_EMPTY(hi))
3464 {
3465 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003466 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3467 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003468 }
3469 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471}
3472#endif
3473
3474#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3475
3476/*
3477 * Local string buffer for the next two functions to store a variable name
3478 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3479 * get_user_var_name().
3480 */
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482static char_u *varnamebuf = NULL;
3483static int varnamebuflen = 0;
3484
3485/*
3486 * Function to concatenate a prefix and a variable name.
3487 */
3488 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003489cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 int len;
3492
3493 len = (int)STRLEN(name) + 3;
3494 if (len > varnamebuflen)
3495 {
3496 vim_free(varnamebuf);
3497 len += 10; /* some additional space */
3498 varnamebuf = alloc(len);
3499 if (varnamebuf == NULL)
3500 {
3501 varnamebuflen = 0;
3502 return NULL;
3503 }
3504 varnamebuflen = len;
3505 }
3506 *varnamebuf = prefix;
3507 varnamebuf[1] = ':';
3508 STRCPY(varnamebuf + 2, name);
3509 return varnamebuf;
3510}
3511
3512/*
3513 * Function given to ExpandGeneric() to obtain the list of user defined
3514 * (global/buffer/window/built-in) variable names.
3515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003517get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003519 static long_u gdone;
3520 static long_u bdone;
3521 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003522 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003523 static int vidx;
3524 static hashitem_T *hi;
3525 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
3527 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003528 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003529 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003530 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003531 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003532
3533 /* Global variables */
3534 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003537 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003538 else
3539 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 while (HASHITEM_EMPTY(hi))
3541 ++hi;
3542 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3543 return cat_prefix_varname('g', hi->hi_key);
3544 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003546
3547 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003548 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003551 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003553 else
3554 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003555 while (HASHITEM_EMPTY(hi))
3556 ++hi;
3557 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003559
3560 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003561 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003564 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003565 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003566 else
3567 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003568 while (HASHITEM_EMPTY(hi))
3569 ++hi;
3570 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003572
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003573 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003574 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003575 if (tdone < ht->ht_used)
3576 {
3577 if (tdone++ == 0)
3578 hi = ht->ht_array;
3579 else
3580 ++hi;
3581 while (HASHITEM_EMPTY(hi))
3582 ++hi;
3583 return cat_prefix_varname('t', hi->hi_key);
3584 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003585
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 /* v: variables */
3587 if (vidx < VV_LEN)
3588 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaard23a8232018-02-10 18:45:26 +01003590 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 varnamebuflen = 0;
3592 return NULL;
3593}
3594
3595#endif /* FEAT_CMDL_COMPL */
3596
3597/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003598 * Return TRUE if "pat" matches "text".
3599 * Does not use 'cpo' and always uses 'magic'.
3600 */
3601 static int
3602pattern_match(char_u *pat, char_u *text, int ic)
3603{
3604 int matches = FALSE;
3605 char_u *save_cpo;
3606 regmatch_T regmatch;
3607
3608 /* avoid 'l' flag in 'cpoptions' */
3609 save_cpo = p_cpo;
3610 p_cpo = (char_u *)"";
3611 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3612 if (regmatch.regprog != NULL)
3613 {
3614 regmatch.rm_ic = ic;
3615 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3616 vim_regfree(regmatch.regprog);
3617 }
3618 p_cpo = save_cpo;
3619 return matches;
3620}
3621
3622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003624 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3626 */
3627
3628/*
3629 * Handle zero level expression.
3630 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003631 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003632 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 * Return OK or FAIL.
3634 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636eval0(
3637 char_u *arg,
3638 typval_T *rettv,
3639 char_u **nextcmd,
3640 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 int ret;
3643 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003644 int did_emsg_before = did_emsg;
3645 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003648 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (ret == FAIL || !ends_excmd(*p))
3650 {
3651 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003652 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 /*
3654 * Report the invalid expression unless the expression evaluation has
3655 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003656 * exception, or we already gave a more specific error.
3657 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003659 if (!aborting() && did_emsg == did_emsg_before
3660 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 ret = FAIL;
3663 }
3664 if (nextcmd != NULL)
3665 *nextcmd = check_nextcmd(p);
3666
3667 return ret;
3668}
3669
3670/*
3671 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003672 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 *
3674 * "arg" must point to the first non-white of the expression.
3675 * "arg" is advanced to the next non-white after the recognized expression.
3676 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003677 * Note: "rettv.v_lock" is not set.
3678 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * Return OK or FAIL.
3680 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003681 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003682eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687 /*
3688 * Get the first variable.
3689 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return FAIL;
3692
3693 if ((*arg)[0] == '?')
3694 {
3695 result = FALSE;
3696 if (evaluate)
3697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003698 int error = FALSE;
3699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003700 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003702 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003703 if (error)
3704 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706
3707 /*
3708 * Get the second variable.
3709 */
3710 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 return FAIL;
3713
3714 /*
3715 * Check for the ":".
3716 */
3717 if ((*arg)[0] != ':')
3718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003719 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return FAIL;
3723 }
3724
3725 /*
3726 * Get the third variable.
3727 */
3728 *arg = skipwhite(*arg + 1);
3729 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3730 {
3731 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 return FAIL;
3734 }
3735 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 return OK;
3740}
3741
3742/*
3743 * Handle first level expression:
3744 * expr2 || expr2 || expr2 logical OR
3745 *
3746 * "arg" must point to the first non-white of the expression.
3747 * "arg" is advanced to the next non-white after the recognized expression.
3748 *
3749 * Return OK or FAIL.
3750 */
3751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003752eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 long result;
3756 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003757 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 /*
3760 * Get the first variable.
3761 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return FAIL;
3764
3765 /*
3766 * Repeat until there is no following "||".
3767 */
3768 first = TRUE;
3769 result = FALSE;
3770 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3771 {
3772 if (evaluate && first)
3773 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003774 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003776 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 if (error)
3778 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 first = FALSE;
3780 }
3781
3782 /*
3783 * Get the second variable.
3784 */
3785 *arg = skipwhite(*arg + 2);
3786 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3787 return FAIL;
3788
3789 /*
3790 * Compute the result.
3791 */
3792 if (evaluate && !result)
3793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003794 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 if (error)
3798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 if (evaluate)
3801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 rettv->v_type = VAR_NUMBER;
3803 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 }
3806
3807 return OK;
3808}
3809
3810/*
3811 * Handle second level expression:
3812 * expr3 && expr3 && expr3 logical AND
3813 *
3814 * "arg" must point to the first non-white of the expression.
3815 * "arg" is advanced to the next non-white after the recognized expression.
3816 *
3817 * Return OK or FAIL.
3818 */
3819 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003820eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
Bram Moolenaar33570922005-01-25 22:26:29 +00003822 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 long result;
3824 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003825 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 /*
3828 * Get the first variable.
3829 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return FAIL;
3832
3833 /*
3834 * Repeat until there is no following "&&".
3835 */
3836 first = TRUE;
3837 result = TRUE;
3838 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3839 {
3840 if (evaluate && first)
3841 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003842 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003844 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003845 if (error)
3846 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 first = FALSE;
3848 }
3849
3850 /*
3851 * Get the second variable.
3852 */
3853 *arg = skipwhite(*arg + 2);
3854 if (eval4(arg, &var2, evaluate && result) == FAIL)
3855 return FAIL;
3856
3857 /*
3858 * Compute the result.
3859 */
3860 if (evaluate && result)
3861 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003862 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 if (error)
3866 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868 if (evaluate)
3869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003870 rettv->v_type = VAR_NUMBER;
3871 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873 }
3874
3875 return OK;
3876}
3877
3878/*
3879 * Handle third level expression:
3880 * var1 == var2
3881 * var1 =~ var2
3882 * var1 != var2
3883 * var1 !~ var2
3884 * var1 > var2
3885 * var1 >= var2
3886 * var1 < var2
3887 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003888 * var1 is var2
3889 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 *
3891 * "arg" must point to the first non-white of the expression.
3892 * "arg" is advanced to the next non-white after the recognized expression.
3893 *
3894 * Return OK or FAIL.
3895 */
3896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003897eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
Bram Moolenaar33570922005-01-25 22:26:29 +00003899 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 char_u *p;
3901 int i;
3902 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003903 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 /*
3908 * Get the first variable.
3909 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return FAIL;
3912
3913 p = *arg;
3914 switch (p[0])
3915 {
3916 case '=': if (p[1] == '=')
3917 type = TYPE_EQUAL;
3918 else if (p[1] == '~')
3919 type = TYPE_MATCH;
3920 break;
3921 case '!': if (p[1] == '=')
3922 type = TYPE_NEQUAL;
3923 else if (p[1] == '~')
3924 type = TYPE_NOMATCH;
3925 break;
3926 case '>': if (p[1] != '=')
3927 {
3928 type = TYPE_GREATER;
3929 len = 1;
3930 }
3931 else
3932 type = TYPE_GEQUAL;
3933 break;
3934 case '<': if (p[1] != '=')
3935 {
3936 type = TYPE_SMALLER;
3937 len = 1;
3938 }
3939 else
3940 type = TYPE_SEQUAL;
3941 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003942 case 'i': if (p[1] == 's')
3943 {
3944 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3945 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003946 i = p[len];
3947 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003948 {
3949 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3950 type_is = TRUE;
3951 }
3952 }
3953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955
3956 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003957 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 */
3959 if (type != TYPE_UNKNOWN)
3960 {
3961 /* extra question mark appended: ignore case */
3962 if (p[len] == '?')
3963 {
3964 ic = TRUE;
3965 ++len;
3966 }
3967 /* extra '#' appended: match case */
3968 else if (p[len] == '#')
3969 {
3970 ic = FALSE;
3971 ++len;
3972 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003973 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
3975 ic = p_ic;
3976
3977 /*
3978 * Get the second variable.
3979 */
3980 *arg = skipwhite(p + len);
3981 if (eval5(arg, &var2, evaluate) == FAIL)
3982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003986 if (evaluate)
3987 {
3988 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3989
3990 clear_tv(&var2);
3991 return ret;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994
3995 return OK;
3996}
3997
3998/*
3999 * Handle fourth level expression:
4000 * + number addition
4001 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004002 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004003 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 *
4005 * "arg" must point to the first non-white of the expression.
4006 * "arg" is advanced to the next non-white after the recognized expression.
4007 *
4008 * Return OK or FAIL.
4009 */
4010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004011eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 typval_T var2;
4014 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004016 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004017#ifdef FEAT_FLOAT
4018 float_T f1 = 0, f2 = 0;
4019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 char_u *s1, *s2;
4021 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4022 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004023 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 /*
4026 * Get the first variable.
4027 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004028 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030
4031 /*
4032 * Repeat computing, until no '+', '-' or '.' is following.
4033 */
4034 for (;;)
4035 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004036 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004038 concat = op == '.'
4039 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4040 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 break;
4042
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004043 if ((op != '+' || (rettv->v_type != VAR_LIST
4044 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045#ifdef FEAT_FLOAT
4046 && (op == '.' || rettv->v_type != VAR_FLOAT)
4047#endif
4048 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004049 {
4050 /* For "list + ...", an illegal use of the first operand as
4051 * a number cannot be determined before evaluating the 2nd
4052 * operand: if this is also a list, all is ok.
4053 * For "something . ...", "something - ..." or "non-list + ...",
4054 * we know that the first operand needs to be a string or number
4055 * without evaluating the 2nd operand. So check before to avoid
4056 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004057 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004058 {
4059 clear_tv(rettv);
4060 return FAIL;
4061 }
4062 }
4063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 /*
4065 * Get the second variable.
4066 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004067 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4068 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004070 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return FAIL;
4074 }
4075
4076 if (evaluate)
4077 {
4078 /*
4079 * Compute the result.
4080 */
4081 if (op == '.')
4082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004083 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4084 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 if (s2 == NULL) /* type error ? */
4086 {
4087 clear_tv(rettv);
4088 clear_tv(&var2);
4089 return FAIL;
4090 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004091 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
4093 rettv->v_type = VAR_STRING;
4094 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004096 else if (op == '+' && rettv->v_type == VAR_BLOB
4097 && var2.v_type == VAR_BLOB)
4098 {
4099 blob_T *b1 = rettv->vval.v_blob;
4100 blob_T *b2 = var2.vval.v_blob;
4101 blob_T *b = blob_alloc();
4102 int i;
4103
4104 if (b != NULL)
4105 {
4106 for (i = 0; i < blob_len(b1); i++)
4107 ga_append(&b->bv_ga, blob_get(b1, i));
4108 for (i = 0; i < blob_len(b2); i++)
4109 ga_append(&b->bv_ga, blob_get(b2, i));
4110
4111 clear_tv(rettv);
4112 rettv_blob_set(rettv, b);
4113 }
4114 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004115 else if (op == '+' && rettv->v_type == VAR_LIST
4116 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004117 {
4118 /* concatenate Lists */
4119 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4120 &var3) == FAIL)
4121 {
4122 clear_tv(rettv);
4123 clear_tv(&var2);
4124 return FAIL;
4125 }
4126 clear_tv(rettv);
4127 *rettv = var3;
4128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else
4130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131 int error = FALSE;
4132
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004133#ifdef FEAT_FLOAT
4134 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004136 f1 = rettv->vval.v_float;
4137 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004139 else
4140#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004142 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004143 if (error)
4144 {
4145 /* This can only happen for "list + non-list". For
4146 * "non-list + ..." or "something - ...", we returned
4147 * before evaluating the 2nd operand. */
4148 clear_tv(rettv);
4149 return FAIL;
4150 }
4151#ifdef FEAT_FLOAT
4152 if (var2.v_type == VAR_FLOAT)
4153 f1 = n1;
4154#endif
4155 }
4156#ifdef FEAT_FLOAT
4157 if (var2.v_type == VAR_FLOAT)
4158 {
4159 f2 = var2.vval.v_float;
4160 n2 = 0;
4161 }
4162 else
4163#endif
4164 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004165 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 if (error)
4167 {
4168 clear_tv(rettv);
4169 clear_tv(&var2);
4170 return FAIL;
4171 }
4172#ifdef FEAT_FLOAT
4173 if (rettv->v_type == VAR_FLOAT)
4174 f2 = n2;
4175#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004178
4179#ifdef FEAT_FLOAT
4180 /* If there is a float on either side the result is a float. */
4181 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4182 {
4183 if (op == '+')
4184 f1 = f1 + f2;
4185 else
4186 f1 = f1 - f2;
4187 rettv->v_type = VAR_FLOAT;
4188 rettv->vval.v_float = f1;
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004191#endif
4192 {
4193 if (op == '+')
4194 n1 = n1 + n2;
4195 else
4196 n1 = n1 - n2;
4197 rettv->v_type = VAR_NUMBER;
4198 rettv->vval.v_number = n1;
4199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 }
4204 return OK;
4205}
4206
4207/*
4208 * Handle fifth level expression:
4209 * * number multiplication
4210 * / number division
4211 * % number modulo
4212 *
4213 * "arg" must point to the first non-white of the expression.
4214 * "arg" is advanced to the next non-white after the recognized expression.
4215 *
4216 * Return OK or FAIL.
4217 */
4218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004219eval6(
4220 char_u **arg,
4221 typval_T *rettv,
4222 int evaluate,
4223 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
Bram Moolenaar33570922005-01-25 22:26:29 +00004225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004227 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004228#ifdef FEAT_FLOAT
4229 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004230 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004231#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004237 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat computing, until no '*', '/' or '%' is following.
4242 */
4243 for (;;)
4244 {
4245 op = **arg;
4246 if (op != '*' && op != '/' && op != '%')
4247 break;
4248
4249 if (evaluate)
4250 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004251#ifdef FEAT_FLOAT
4252 if (rettv->v_type == VAR_FLOAT)
4253 {
4254 f1 = rettv->vval.v_float;
4255 use_float = TRUE;
4256 n1 = 0;
4257 }
4258 else
4259#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004260 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 else
4266 n1 = 0;
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004272 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return FAIL;
4274
4275 if (evaluate)
4276 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004277#ifdef FEAT_FLOAT
4278 if (var2.v_type == VAR_FLOAT)
4279 {
4280 if (!use_float)
4281 {
4282 f1 = n1;
4283 use_float = TRUE;
4284 }
4285 f2 = var2.vval.v_float;
4286 n2 = 0;
4287 }
4288 else
4289#endif
4290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004291 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004292 clear_tv(&var2);
4293 if (error)
4294 return FAIL;
4295#ifdef FEAT_FLOAT
4296 if (use_float)
4297 f2 = n2;
4298#endif
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
4301 /*
4302 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004303 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004305#ifdef FEAT_FLOAT
4306 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308 if (op == '*')
4309 f1 = f1 * f2;
4310 else if (op == '/')
4311 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004312# ifdef VMS
4313 /* VMS crashes on divide by zero, work around it */
4314 if (f2 == 0.0)
4315 {
4316 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004317 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004318 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004319 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004320 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004321 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004322 }
4323 else
4324 f1 = f1 / f2;
4325# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004326 /* We rely on the floating point library to handle divide
4327 * by zero to result in "inf" and not a crash. */
4328 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004329# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004333 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334 return FAIL;
4335 }
4336 rettv->v_type = VAR_FLOAT;
4337 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004342 if (op == '*')
4343 n1 = n1 * n2;
4344 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004345 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004347 n1 = num_modulus(n1, n2);
4348
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354
4355 return OK;
4356}
4357
4358/*
4359 * Handle sixth level expression:
4360 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004361 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004362 * "string" string constant
4363 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 * &option-name option value
4365 * @r register contents
4366 * identifier variable value
4367 * function() function call
4368 * $VAR environment variable
4369 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004370 * [expr, expr] List
4371 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 *
4373 * Also handle:
4374 * ! in front logical NOT
4375 * - in front unary minus
4376 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 * trailing [] subscript in String or List
4378 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 *
4380 * "arg" must point to the first non-white of the expression.
4381 * "arg" is advanced to the next non-white after the recognized expression.
4382 *
4383 * Return OK or FAIL.
4384 */
4385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004386eval7(
4387 char_u **arg,
4388 typval_T *rettv,
4389 int evaluate,
4390 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004392 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 int len;
4394 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u *start_leader, *end_leader;
4396 int ret = OK;
4397 char_u *alias;
4398
4399 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004406 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
4408 start_leader = *arg;
4409 while (**arg == '!' || **arg == '-' || **arg == '+')
4410 *arg = skipwhite(*arg + 1);
4411 end_leader = *arg;
4412
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004413 if (**arg == '.' && (!isdigit(*(*arg + 1))
4414#ifdef FEAT_FLOAT
4415 || current_sctx.sc_version < 2
4416#endif
4417 ))
4418 {
4419 semsg(_(e_invexpr2), *arg);
4420 ++*arg;
4421 return FAIL;
4422 }
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 switch (**arg)
4425 {
4426 /*
4427 * Number constant.
4428 */
4429 case '0':
4430 case '1':
4431 case '2':
4432 case '3':
4433 case '4':
4434 case '5':
4435 case '6':
4436 case '7':
4437 case '8':
4438 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004439 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004440 {
4441#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004442 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 int get_float = FALSE;
4444
4445 /* We accept a float when the format matches
4446 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004447 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004448 * With script version 2 and later the leading digit can be
4449 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004450 * Don't look for a float after the "." operator, so that
4451 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004452 if (**arg == '.')
4453 p = *arg;
4454 else
4455 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004456 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 get_float = TRUE;
4459 p = skipdigits(p + 2);
4460 if (*p == 'e' || *p == 'E')
4461 {
4462 ++p;
4463 if (*p == '-' || *p == '+')
4464 ++p;
4465 if (!vim_isdigit(*p))
4466 get_float = FALSE;
4467 else
4468 p = skipdigits(p + 1);
4469 }
4470 if (ASCII_ISALPHA(*p) || *p == '.')
4471 get_float = FALSE;
4472 }
4473 if (get_float)
4474 {
4475 float_T f;
4476
4477 *arg += string2float(*arg, &f);
4478 if (evaluate)
4479 {
4480 rettv->v_type = VAR_FLOAT;
4481 rettv->vval.v_float = f;
4482 }
4483 }
4484 else
4485#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004486 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004487 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004488 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004489 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004490
4491 // Blob constant: 0z0123456789abcdef
4492 if (evaluate)
4493 blob = blob_alloc();
4494 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4495 {
4496 if (!vim_isxdigit(bp[1]))
4497 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004498 if (blob != NULL)
4499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004500 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004501 ga_clear(&blob->bv_ga);
4502 VIM_CLEAR(blob);
4503 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004504 ret = FAIL;
4505 break;
4506 }
4507 if (blob != NULL)
4508 ga_append(&blob->bv_ga,
4509 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004510 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4511 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004512 }
4513 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004514 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004515 *arg = bp;
4516 }
4517 else
4518 {
4519 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004520 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4521 if (len == 0)
4522 {
4523 semsg(_(e_invexpr2), *arg);
4524 ret = FAIL;
4525 break;
4526 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004527 *arg += len;
4528 if (evaluate)
4529 {
4530 rettv->v_type = VAR_NUMBER;
4531 rettv->vval.v_number = n;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 /*
4538 * String constant: "string".
4539 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 break;
4542
4543 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004546 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004547 break;
4548
4549 /*
4550 * List: [expr, expr]
4551 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004552 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 break;
4554
4555 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004556 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 * Dictionary: {key: val, key: val}
4558 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004559 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4560 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004561 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 break;
4563
4564 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004565 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004567 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 break;
4569
4570 /*
4571 * Environment variable: $VAR.
4572 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 break;
4575
4576 /*
4577 * Register contents: @r.
4578 */
4579 case '@': ++*arg;
4580 if (evaluate)
4581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004583 rettv->vval.v_string = get_reg_contents(**arg,
4584 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 if (**arg != NUL)
4587 ++*arg;
4588 break;
4589
4590 /*
4591 * nested expression: (expression).
4592 */
4593 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (**arg == ')')
4596 ++*arg;
4597 else if (ret == OK)
4598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004599 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 ret = FAIL;
4602 }
4603 break;
4604
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 break;
4607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608
4609 if (ret == NOTDONE)
4610 {
4611 /*
4612 * Must be a variable or function name.
4613 * Can also be a curly-braces kind of name: {expr}.
4614 */
4615 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004616 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 if (alias != NULL)
4618 s = alias;
4619
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004620 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 ret = FAIL;
4622 else
4623 {
4624 if (**arg == '(') /* recursive! */
4625 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004626 partial_T *partial;
4627
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004628 if (!evaluate)
4629 check_vars(s, len);
4630
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 /* If "s" is the name of a variable of type VAR_FUNC
4632 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004633 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004634
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004635 /* Need to make a copy, in case evaluating the arguments makes
4636 * the name invalid. */
4637 s = vim_strsave(s);
4638 if (s == NULL)
4639 ret = FAIL;
4640 else
4641 /* Invoke the function. */
4642 ret = get_func_tv(s, len, rettv, arg,
4643 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4644 &len, evaluate, partial, NULL);
4645 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004646
4647 /* If evaluate is FALSE rettv->v_type was not set in
4648 * get_func_tv, but it's needed in handle_subscript() to parse
4649 * what follows. So set it here. */
4650 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4651 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004652 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004653 rettv->v_type = VAR_FUNC;
4654 }
4655
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 /* Stop the expression evaluation when immediately
4657 * aborting on error, or when an interrupt occurred or
4658 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004659 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 {
4661 if (ret == OK)
4662 clear_tv(rettv);
4663 ret = FAIL;
4664 }
4665 }
4666 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004667 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004668 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004669 {
4670 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004671 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004674 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 }
4676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 *arg = skipwhite(*arg);
4678
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004679 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4680 * expr(expr). */
4681 if (ret == OK)
4682 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 /*
4685 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4686 */
4687 if (ret == OK && evaluate && end_leader > start_leader)
4688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004690 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#ifdef FEAT_FLOAT
4692 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004693
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694 if (rettv->v_type == VAR_FLOAT)
4695 f = rettv->vval.v_float;
4696 else
4697#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004698 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 clear_tv(rettv);
4702 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 else
4705 {
4706 while (end_leader > start_leader)
4707 {
4708 --end_leader;
4709 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 {
4711#ifdef FEAT_FLOAT
4712 if (rettv->v_type == VAR_FLOAT)
4713 f = !f;
4714 else
4715#endif
4716 val = !val;
4717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 {
4720#ifdef FEAT_FLOAT
4721 if (rettv->v_type == VAR_FLOAT)
4722 f = -f;
4723 else
4724#endif
4725 val = -val;
4726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728#ifdef FEAT_FLOAT
4729 if (rettv->v_type == VAR_FLOAT)
4730 {
4731 clear_tv(rettv);
4732 rettv->vval.v_float = f;
4733 }
4734 else
4735#endif
4736 {
4737 clear_tv(rettv);
4738 rettv->v_type = VAR_NUMBER;
4739 rettv->vval.v_number = val;
4740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743
4744 return ret;
4745}
4746
4747/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004748 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4749 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4751 */
4752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004753eval_index(
4754 char_u **arg,
4755 typval_T *rettv,
4756 int evaluate,
4757 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758{
4759 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004761 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 long len = -1;
4764 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767
Bram Moolenaara03f2332016-02-06 18:09:59 +01004768 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004770 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004771 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004772 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004773 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004774 return FAIL;
4775 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004776#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004778 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004779 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004780#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004781 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004782 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004783 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004784 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004785 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004786 return FAIL;
4787 case VAR_UNKNOWN:
4788 if (evaluate)
4789 return FAIL;
4790 /* FALLTHROUGH */
4791
4792 case VAR_STRING:
4793 case VAR_NUMBER:
4794 case VAR_LIST:
4795 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004796 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004797 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004798 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004800 init_tv(&var1);
4801 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 /*
4805 * dict.name
4806 */
4807 key = *arg + 1;
4808 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4809 ;
4810 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 }
4814 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 /*
4817 * something[idx]
4818 *
4819 * Get the (first) variable from inside the [].
4820 */
4821 *arg = skipwhite(*arg + 1);
4822 if (**arg == ':')
4823 empty1 = TRUE;
4824 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4825 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004826 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 {
4828 /* not a number or string */
4829 clear_tv(&var1);
4830 return FAIL;
4831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004832
4833 /*
4834 * Get the second variable from inside the [:].
4835 */
4836 if (**arg == ':')
4837 {
4838 range = TRUE;
4839 *arg = skipwhite(*arg + 1);
4840 if (**arg == ']')
4841 empty2 = TRUE;
4842 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 if (!empty1)
4845 clear_tv(&var1);
4846 return FAIL;
4847 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004848 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004849 {
4850 /* not a number or string */
4851 if (!empty1)
4852 clear_tv(&var1);
4853 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004854 return FAIL;
4855 }
4856 }
4857
4858 /* Check for the ']'. */
4859 if (**arg != ']')
4860 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004861 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004862 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004863 clear_tv(&var1);
4864 if (range)
4865 clear_tv(&var2);
4866 return FAIL;
4867 }
4868 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004869 }
4870
4871 if (evaluate)
4872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004873 n1 = 0;
4874 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004876 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004878 }
4879 if (range)
4880 {
4881 if (empty2)
4882 n2 = -1;
4883 else
4884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004885 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 }
4888 }
4889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004891 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004892 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004893 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004894 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004895 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004896 case VAR_SPECIAL:
4897 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004898 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004899 break; /* not evaluating, skipping over subscript */
4900
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 case VAR_NUMBER:
4902 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004903 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004904 len = (long)STRLEN(s);
4905 if (range)
4906 {
4907 /* The resulting variable is a substring. If the indexes
4908 * are out of range the result is empty. */
4909 if (n1 < 0)
4910 {
4911 n1 = len + n1;
4912 if (n1 < 0)
4913 n1 = 0;
4914 }
4915 if (n2 < 0)
4916 n2 = len + n2;
4917 else if (n2 >= len)
4918 n2 = len;
4919 if (n1 >= len || n2 < 0 || n1 > n2)
4920 s = NULL;
4921 else
4922 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4923 }
4924 else
4925 {
4926 /* The resulting variable is a string of a single
4927 * character. If the index is too big or negative the
4928 * result is empty. */
4929 if (n1 >= len || n1 < 0)
4930 s = NULL;
4931 else
4932 s = vim_strnsave(s + n1, 1);
4933 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 clear_tv(rettv);
4935 rettv->v_type = VAR_STRING;
4936 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 break;
4938
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004939 case VAR_BLOB:
4940 len = blob_len(rettv->vval.v_blob);
4941 if (range)
4942 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004943 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004944 // are out of range the result is empty.
4945 if (n1 < 0)
4946 {
4947 n1 = len + n1;
4948 if (n1 < 0)
4949 n1 = 0;
4950 }
4951 if (n2 < 0)
4952 n2 = len + n2;
4953 else if (n2 >= len)
4954 n2 = len - 1;
4955 if (n1 >= len || n2 < 0 || n1 > n2)
4956 {
4957 clear_tv(rettv);
4958 rettv->v_type = VAR_BLOB;
4959 rettv->vval.v_blob = NULL;
4960 }
4961 else
4962 {
4963 blob_T *blob = blob_alloc();
4964
4965 if (blob != NULL)
4966 {
4967 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4968 {
4969 blob_free(blob);
4970 return FAIL;
4971 }
4972 blob->bv_ga.ga_len = n2 - n1 + 1;
4973 for (i = n1; i <= n2; i++)
4974 blob_set(blob, i - n1,
4975 blob_get(rettv->vval.v_blob, i));
4976
4977 clear_tv(rettv);
4978 rettv_blob_set(rettv, blob);
4979 }
4980 }
4981 }
4982 else
4983 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004984 // The resulting variable is a byte value.
4985 // If the index is too big or negative that is an error.
4986 if (n1 < 0)
4987 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004988 if (n1 < len && n1 >= 0)
4989 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004990 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004991
4992 clear_tv(rettv);
4993 rettv->v_type = VAR_NUMBER;
4994 rettv->vval.v_number = v;
4995 }
4996 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004997 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004998 }
4999 break;
5000
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003 if (n1 < 0)
5004 n1 = len + n1;
5005 if (!empty1 && (n1 < 0 || n1 >= len))
5006 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005007 /* For a range we allow invalid values and return an empty
5008 * list. A list index out of range is an error. */
5009 if (!range)
5010 {
5011 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005012 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005013 return FAIL;
5014 }
5015 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005016 }
5017 if (range)
5018 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005019 list_T *l;
5020 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005021
5022 if (n2 < 0)
5023 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005024 else if (n2 >= len)
5025 n2 = len - 1;
5026 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005027 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028 l = list_alloc();
5029 if (l == NULL)
5030 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005032 n1 <= n2; ++n1)
5033 {
5034 if (list_append_tv(l, &item->li_tv) == FAIL)
5035 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005036 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 return FAIL;
5038 }
5039 item = item->li_next;
5040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005042 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005043 }
5044 else
5045 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005046 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 clear_tv(rettv);
5048 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005049 }
5050 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051
5052 case VAR_DICT:
5053 if (range)
5054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005055 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005056 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 if (len == -1)
5058 clear_tv(&var1);
5059 return FAIL;
5060 }
5061 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005062 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063
5064 if (len == -1)
5065 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005066 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005067 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 clear_tv(&var1);
5070 return FAIL;
5071 }
5072 }
5073
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005074 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005077 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 if (len == -1)
5079 clear_tv(&var1);
5080 if (item == NULL)
5081 return FAIL;
5082
5083 copy_tv(&item->di_tv, &var1);
5084 clear_tv(rettv);
5085 *rettv = var1;
5086 }
5087 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 }
5089 }
5090
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 return OK;
5092}
5093
5094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * Get an option value.
5096 * "arg" points to the '&' or '+' before the option name.
5097 * "arg" is advanced to character after the option name.
5098 * Return OK or FAIL.
5099 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005101get_option_tv(
5102 char_u **arg,
5103 typval_T *rettv, /* when NULL, only check if option exists */
5104 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
5106 char_u *option_end;
5107 long numval;
5108 char_u *stringval;
5109 int opt_type;
5110 int c;
5111 int working = (**arg == '+'); /* has("+option") */
5112 int ret = OK;
5113 int opt_flags;
5114
5115 /*
5116 * Isolate the option name and find its value.
5117 */
5118 option_end = find_option_end(arg, &opt_flags);
5119 if (option_end == NULL)
5120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005122 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 return FAIL;
5124 }
5125
5126 if (!evaluate)
5127 {
5128 *arg = option_end;
5129 return OK;
5130 }
5131
5132 c = *option_end;
5133 *option_end = NUL;
5134 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
5137 if (opt_type == -3) /* invalid name */
5138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005140 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ret = FAIL;
5142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 if (opt_type == -2) /* hidden string option */
5146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 rettv->v_type = VAR_STRING;
5148 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 else if (opt_type == -1) /* hidden number option */
5151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 rettv->v_type = VAR_NUMBER;
5153 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 else if (opt_type == 1) /* number option */
5156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 rettv->v_type = VAR_NUMBER;
5158 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 else /* string option */
5161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 rettv->v_type = VAR_STRING;
5163 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 }
5166 else if (working && (opt_type == -2 || opt_type == -1))
5167 ret = FAIL;
5168
5169 *option_end = c; /* put back for error messages */
5170 *arg = option_end;
5171
5172 return ret;
5173}
5174
5175/*
5176 * Allocate a variable for a string constant.
5177 * Return OK or FAIL.
5178 */
5179 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005180get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
5182 char_u *p;
5183 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 int extra = 0;
5185
5186 /*
5187 * Find the end of the string, skipping backslashed characters.
5188 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005189 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 if (*p == '\\' && p[1] != NUL)
5192 {
5193 ++p;
5194 /* A "\<x>" form occupies at least 4 characters, and produces up
5195 * to 6 characters: reserve space for 2 extra */
5196 if (*p == '<')
5197 extra += 2;
5198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200
5201 if (*p != '"')
5202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005203 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 return FAIL;
5205 }
5206
5207 /* If only parsing, set *arg and return here */
5208 if (!evaluate)
5209 {
5210 *arg = p + 1;
5211 return OK;
5212 }
5213
5214 /*
5215 * Copy the string into allocated memory, handling backslashed
5216 * characters.
5217 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005218 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (name == NULL)
5220 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 rettv->v_type = VAR_STRING;
5222 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
5226 if (*p == '\\')
5227 {
5228 switch (*++p)
5229 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 case 'b': *name++ = BS; ++p; break;
5231 case 'e': *name++ = ESC; ++p; break;
5232 case 'f': *name++ = FF; ++p; break;
5233 case 'n': *name++ = NL; ++p; break;
5234 case 'r': *name++ = CAR; ++p; break;
5235 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 case 'X': /* hex: "\x1", "\x12" */
5238 case 'x':
5239 case 'u': /* Unicode: "\u0023" */
5240 case 'U':
5241 if (vim_isxdigit(p[1]))
5242 {
5243 int n, nr;
5244 int c = toupper(*p);
5245
5246 if (c == 'X')
5247 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005248 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005250 else
5251 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 nr = 0;
5253 while (--n >= 0 && vim_isxdigit(p[1]))
5254 {
5255 ++p;
5256 nr = (nr << 4) + hex2nr(*p);
5257 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 /* For "\u" store the number according to
5260 * 'encoding'. */
5261 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 break;
5267
5268 /* octal: "\1", "\12", "\123" */
5269 case '0':
5270 case '1':
5271 case '2':
5272 case '3':
5273 case '4':
5274 case '5':
5275 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 case '7': *name = *p++ - '0';
5277 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 *name = (*name << 3) + *p++ - '0';
5280 if (*p >= '0' && *p <= '7')
5281 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 break;
5285
5286 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005287 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (extra != 0)
5289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 break;
5292 }
5293 /* FALLTHROUGH */
5294
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 break;
5297 }
5298 }
5299 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005304 if (*p != NUL) /* just in case */
5305 ++p;
5306 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 return OK;
5309}
5310
5311/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 * Return OK or FAIL.
5314 */
5315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005316get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005319 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005320 int reduce = 0;
5321
5322 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005324 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005325 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005326 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005328 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005330 break;
5331 ++reduce;
5332 ++p;
5333 }
5334 }
5335
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005338 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005339 return FAIL;
5340 }
5341
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005343 if (!evaluate)
5344 {
5345 *arg = p + 1;
5346 return OK;
5347 }
5348
5349 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005351 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005352 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005353 if (str == NULL)
5354 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 rettv->v_type = VAR_STRING;
5356 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005357
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005363 break;
5364 ++p;
5365 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005369 *arg = p + 1;
5370
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005371 return OK;
5372}
5373
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005374/*
5375 * Return the function name of the partial.
5376 */
5377 char_u *
5378partial_name(partial_T *pt)
5379{
5380 if (pt->pt_name != NULL)
5381 return pt->pt_name;
5382 return pt->pt_func->uf_name;
5383}
5384
Bram Moolenaarddecc252016-04-06 22:59:37 +02005385 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005386partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005387{
5388 int i;
5389
5390 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005391 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005392 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005393 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005394 if (pt->pt_name != NULL)
5395 {
5396 func_unref(pt->pt_name);
5397 vim_free(pt->pt_name);
5398 }
5399 else
5400 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005401 vim_free(pt);
5402}
5403
5404/*
5405 * Unreference a closure: decrement the reference count and free it when it
5406 * becomes zero.
5407 */
5408 void
5409partial_unref(partial_T *pt)
5410{
5411 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005412 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005413}
5414
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005415static int tv_equal_recurse_limit;
5416
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005417 static int
5418func_equal(
5419 typval_T *tv1,
5420 typval_T *tv2,
5421 int ic) /* ignore case */
5422{
5423 char_u *s1, *s2;
5424 dict_T *d1, *d2;
5425 int a1, a2;
5426 int i;
5427
5428 /* empty and NULL function name considered the same */
5429 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005430 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005431 if (s1 != NULL && *s1 == NUL)
5432 s1 = NULL;
5433 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005434 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005435 if (s2 != NULL && *s2 == NUL)
5436 s2 = NULL;
5437 if (s1 == NULL || s2 == NULL)
5438 {
5439 if (s1 != s2)
5440 return FALSE;
5441 }
5442 else if (STRCMP(s1, s2) != 0)
5443 return FALSE;
5444
5445 /* empty dict and NULL dict is different */
5446 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5447 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5448 if (d1 == NULL || d2 == NULL)
5449 {
5450 if (d1 != d2)
5451 return FALSE;
5452 }
5453 else if (!dict_equal(d1, d2, ic, TRUE))
5454 return FALSE;
5455
5456 /* empty list and no list considered the same */
5457 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5458 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5459 if (a1 != a2)
5460 return FALSE;
5461 for (i = 0; i < a1; ++i)
5462 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5463 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5464 return FALSE;
5465
5466 return TRUE;
5467}
5468
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005469/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005470 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005471 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005472 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005473 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005474 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005475tv_equal(
5476 typval_T *tv1,
5477 typval_T *tv2,
5478 int ic, /* ignore case */
5479 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005480{
5481 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005482 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005483 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005484 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005486 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005487 * recursiveness to a limit. We guess they are equal then.
5488 * A fixed limit has the problem of still taking an awful long time.
5489 * Reduce the limit every time running into it. That should work fine for
5490 * deeply linked structures that are not recursively linked and catch
5491 * recursiveness quickly. */
5492 if (!recursive)
5493 tv_equal_recurse_limit = 1000;
5494 if (recursive_cnt >= tv_equal_recurse_limit)
5495 {
5496 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005497 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005498 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005499
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005500 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5501 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005502 if ((tv1->v_type == VAR_FUNC
5503 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5504 && (tv2->v_type == VAR_FUNC
5505 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5506 {
5507 ++recursive_cnt;
5508 r = func_equal(tv1, tv2, ic);
5509 --recursive_cnt;
5510 return r;
5511 }
5512
5513 if (tv1->v_type != tv2->v_type)
5514 return FALSE;
5515
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005516 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005517 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005518 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005519 ++recursive_cnt;
5520 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5521 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005522 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005523
5524 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005525 ++recursive_cnt;
5526 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5527 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005528 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005529
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005530 case VAR_BLOB:
5531 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5532
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005533 case VAR_NUMBER:
5534 return tv1->vval.v_number == tv2->vval.v_number;
5535
5536 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005537 s1 = tv_get_string_buf(tv1, buf1);
5538 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005539 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005540
5541 case VAR_SPECIAL:
5542 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005543
5544 case VAR_FLOAT:
5545#ifdef FEAT_FLOAT
5546 return tv1->vval.v_float == tv2->vval.v_float;
5547#endif
5548 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005550 return tv1->vval.v_job == tv2->vval.v_job;
5551#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005552 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005553#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005554 return tv1->vval.v_channel == tv2->vval.v_channel;
5555#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005556 case VAR_FUNC:
5557 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005558 case VAR_UNKNOWN:
5559 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005560 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005561
Bram Moolenaara03f2332016-02-06 18:09:59 +01005562 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5563 * does not equal anything, not even itself. */
5564 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565}
5566
5567/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005568 * Return the next (unique) copy ID.
5569 * Used for serializing nested structures.
5570 */
5571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005572get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005573{
5574 current_copyID += COPYID_INC;
5575 return current_copyID;
5576}
5577
5578/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005579 * Garbage collection for lists and dictionaries.
5580 *
5581 * We use reference counts to be able to free most items right away when they
5582 * are no longer used. But for composite items it's possible that it becomes
5583 * unused while the reference count is > 0: When there is a recursive
5584 * reference. Example:
5585 * :let l = [1, 2, 3]
5586 * :let d = {9: l}
5587 * :let l[1] = d
5588 *
5589 * Since this is quite unusual we handle this with garbage collection: every
5590 * once in a while find out which lists and dicts are not referenced from any
5591 * variable.
5592 *
5593 * Here is a good reference text about garbage collection (refers to Python
5594 * but it applies to all reference-counting mechanisms):
5595 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005596 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005597
5598/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005599 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005600 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005601 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005602 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005603 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005604garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005605{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005606 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005607 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005608 buf_T *buf;
5609 win_T *wp;
5610 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005611 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005612 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005613
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005614 if (!testing)
5615 {
5616 /* Only do this once. */
5617 want_garbage_collect = FALSE;
5618 may_garbage_collect = FALSE;
5619 garbage_collect_at_exit = FALSE;
5620 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005621
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005622 /* We advance by two because we add one for items referenced through
5623 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005624 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005625
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005626 /*
5627 * 1. Go through all accessible variables and mark all lists and dicts
5628 * with copyID.
5629 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005630
5631 /* Don't free variables in the previous_funccal list unless they are only
5632 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005633 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005635
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005636 /* script-local variables */
5637 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005638 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005639
5640 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005641 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005642 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5643 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005644
5645 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005646 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005647 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5648 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005649 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005650 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5651 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005652#ifdef FEAT_TEXT_PROP
5653 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5654 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5655 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005656 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005657 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005658 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5659 NULL, NULL);
5660#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005661
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005662 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005663 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005664 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5665 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005666 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005667 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005668
5669 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005671
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005672 /* named functions (matters for closures) */
5673 abort = abort || set_ref_in_functions(copyID);
5674
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005675 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005677
Bram Moolenaard812df62008-11-09 12:46:09 +00005678 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005679 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005680
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005681 // callbacks in buffers
5682 abort = abort || set_ref_in_buffers(copyID);
5683
Bram Moolenaar1dced572012-04-05 16:54:08 +02005684#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005685 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005686#endif
5687
Bram Moolenaardb913952012-06-29 12:54:53 +02005688#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005689 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005690#endif
5691
5692#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005693 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005694#endif
5695
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005697 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005698 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005699#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005700#ifdef FEAT_NETBEANS_INTG
5701 abort = abort || set_ref_in_nb_channel(copyID);
5702#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005703
Bram Moolenaare3188e22016-05-31 21:13:04 +02005704#ifdef FEAT_TIMERS
5705 abort = abort || set_ref_in_timer(copyID);
5706#endif
5707
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005708#ifdef FEAT_QUICKFIX
5709 abort = abort || set_ref_in_quickfix(copyID);
5710#endif
5711
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005712#ifdef FEAT_TERMINAL
5713 abort = abort || set_ref_in_term(copyID);
5714#endif
5715
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005716#ifdef FEAT_TEXT_PROP
5717 abort = abort || set_ref_in_popups(copyID);
5718#endif
5719
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005720 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005721 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 /*
5723 * 2. Free lists and dictionaries that are not referenced.
5724 */
5725 did_free = free_unref_items(copyID);
5726
5727 /*
5728 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005729 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005730 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005731 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005732 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005733 else if (p_verbose > 0)
5734 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005735 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005737
5738 return did_free;
5739}
5740
5741/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005742 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005743 */
5744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005745free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005746{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005747 int did_free = FALSE;
5748
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005749 /* Let all "free" functions know that we are here. This means no
5750 * dictionaries, lists, channels or jobs are to be freed, because we will
5751 * do that here. */
5752 in_free_unref_items = TRUE;
5753
5754 /*
5755 * PASS 1: free the contents of the items. We don't free the items
5756 * themselves yet, so that it is possible to decrement refcount counters
5757 */
5758
Bram Moolenaarcd524592016-07-17 14:57:05 +02005759 /* Go through the list of dicts and free items without the copyID. */
5760 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005761
Bram Moolenaarda861d62016-07-17 15:46:27 +02005762 /* Go through the list of lists and free items without the copyID. */
5763 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005764
5765#ifdef FEAT_JOB_CHANNEL
5766 /* Go through the list of jobs and free items without the copyID. This
5767 * must happen before doing channels, because jobs refer to channels, but
5768 * the reference from the channel to the job isn't tracked. */
5769 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5770
5771 /* Go through the list of channels and free items without the copyID. */
5772 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5773#endif
5774
5775 /*
5776 * PASS 2: free the items themselves.
5777 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005778 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005779 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005780
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005781#ifdef FEAT_JOB_CHANNEL
5782 /* Go through the list of jobs and free items without the copyID. This
5783 * must happen before doing channels, because jobs refer to channels, but
5784 * the reference from the channel to the job isn't tracked. */
5785 free_unused_jobs(copyID, COPYID_MASK);
5786
5787 /* Go through the list of channels and free items without the copyID. */
5788 free_unused_channels(copyID, COPYID_MASK);
5789#endif
5790
5791 in_free_unref_items = FALSE;
5792
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005793 return did_free;
5794}
5795
5796/*
5797 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005798 * "list_stack" is used to add lists to be marked. Can be NULL.
5799 *
5800 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005801 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005803set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005804{
5805 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005806 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005807 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005808 hashtab_T *cur_ht;
5809 ht_stack_T *ht_stack = NULL;
5810 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005811
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005812 cur_ht = ht;
5813 for (;;)
5814 {
5815 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005816 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005817 /* Mark each item in the hashtab. If the item contains a hashtab
5818 * it is added to ht_stack, if it contains a list it is added to
5819 * list_stack. */
5820 todo = (int)cur_ht->ht_used;
5821 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5822 if (!HASHITEM_EMPTY(hi))
5823 {
5824 --todo;
5825 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5826 &ht_stack, list_stack);
5827 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005828 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005829
5830 if (ht_stack == NULL)
5831 break;
5832
5833 /* take an item from the stack */
5834 cur_ht = ht_stack->ht;
5835 tempitem = ht_stack;
5836 ht_stack = ht_stack->prev;
5837 free(tempitem);
5838 }
5839
5840 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005841}
5842
5843/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005844 * Mark a dict and its items with "copyID".
5845 * Returns TRUE if setting references failed somehow.
5846 */
5847 int
5848set_ref_in_dict(dict_T *d, int copyID)
5849{
5850 if (d != NULL && d->dv_copyID != copyID)
5851 {
5852 d->dv_copyID = copyID;
5853 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5854 }
5855 return FALSE;
5856}
5857
5858/*
5859 * Mark a list and its items with "copyID".
5860 * Returns TRUE if setting references failed somehow.
5861 */
5862 int
5863set_ref_in_list(list_T *ll, int copyID)
5864{
5865 if (ll != NULL && ll->lv_copyID != copyID)
5866 {
5867 ll->lv_copyID = copyID;
5868 return set_ref_in_list_items(ll, copyID, NULL);
5869 }
5870 return FALSE;
5871}
5872
5873/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005874 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5876 *
5877 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005878 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005879 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005880set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005882 listitem_T *li;
5883 int abort = FALSE;
5884 list_T *cur_l;
5885 list_stack_T *list_stack = NULL;
5886 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005887
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005888 cur_l = l;
5889 for (;;)
5890 {
5891 if (!abort)
5892 /* Mark each item in the list. If the item contains a hashtab
5893 * it is added to ht_stack, if it contains a list it is added to
5894 * list_stack. */
5895 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5896 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5897 ht_stack, &list_stack);
5898 if (list_stack == NULL)
5899 break;
5900
5901 /* take an item from the stack */
5902 cur_l = list_stack->list;
5903 tempitem = list_stack;
5904 list_stack = list_stack->prev;
5905 free(tempitem);
5906 }
5907
5908 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005909}
5910
5911/*
5912 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005913 * "list_stack" is used to add lists to be marked. Can be NULL.
5914 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5915 *
5916 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005918 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919set_ref_in_item(
5920 typval_T *tv,
5921 int copyID,
5922 ht_stack_T **ht_stack,
5923 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005925 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005926
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005927 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005928 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005929 dict_T *dd = tv->vval.v_dict;
5930
Bram Moolenaara03f2332016-02-06 18:09:59 +01005931 if (dd != NULL && dd->dv_copyID != copyID)
5932 {
5933 /* Didn't see this dict yet. */
5934 dd->dv_copyID = copyID;
5935 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005936 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005937 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5938 }
5939 else
5940 {
5941 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5942 if (newitem == NULL)
5943 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005944 else
5945 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005946 newitem->ht = &dd->dv_hashtab;
5947 newitem->prev = *ht_stack;
5948 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005949 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005950 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005951 }
5952 }
5953 else if (tv->v_type == VAR_LIST)
5954 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005955 list_T *ll = tv->vval.v_list;
5956
Bram Moolenaara03f2332016-02-06 18:09:59 +01005957 if (ll != NULL && ll->lv_copyID != copyID)
5958 {
5959 /* Didn't see this list yet. */
5960 ll->lv_copyID = copyID;
5961 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005962 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005963 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005964 }
5965 else
5966 {
5967 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005968 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005969 if (newitem == NULL)
5970 abort = TRUE;
5971 else
5972 {
5973 newitem->list = ll;
5974 newitem->prev = *list_stack;
5975 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005976 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005977 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005978 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005979 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005980 else if (tv->v_type == VAR_FUNC)
5981 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005982 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005983 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005984 else if (tv->v_type == VAR_PARTIAL)
5985 {
5986 partial_T *pt = tv->vval.v_partial;
5987 int i;
5988
5989 /* A partial does not have a copyID, because it cannot contain itself.
5990 */
5991 if (pt != NULL)
5992 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005993 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005994
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005995 if (pt->pt_dict != NULL)
5996 {
5997 typval_T dtv;
5998
5999 dtv.v_type = VAR_DICT;
6000 dtv.vval.v_dict = pt->pt_dict;
6001 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6002 }
6003
6004 for (i = 0; i < pt->pt_argc; ++i)
6005 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6006 ht_stack, list_stack);
6007 }
6008 }
6009#ifdef FEAT_JOB_CHANNEL
6010 else if (tv->v_type == VAR_JOB)
6011 {
6012 job_T *job = tv->vval.v_job;
6013 typval_T dtv;
6014
6015 if (job != NULL && job->jv_copyID != copyID)
6016 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006017 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006018 if (job->jv_channel != NULL)
6019 {
6020 dtv.v_type = VAR_CHANNEL;
6021 dtv.vval.v_channel = job->jv_channel;
6022 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6023 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006024 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006025 {
6026 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006027 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006028 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6029 }
6030 }
6031 }
6032 else if (tv->v_type == VAR_CHANNEL)
6033 {
6034 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006035 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006036 typval_T dtv;
6037 jsonq_T *jq;
6038 cbq_T *cq;
6039
6040 if (ch != NULL && ch->ch_copyID != copyID)
6041 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006042 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006043 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006044 {
6045 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6046 jq = jq->jq_next)
6047 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6048 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6049 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006050 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006051 {
6052 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006053 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006054 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6055 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006056 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006057 {
6058 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006059 dtv.vval.v_partial =
6060 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006061 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6062 }
6063 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006064 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006065 {
6066 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006067 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006068 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6069 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006070 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006071 {
6072 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006073 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006074 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6075 }
6076 }
6077 }
6078#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006079 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006080}
6081
Bram Moolenaar17a13432016-01-24 14:22:10 +01006082 static char *
6083get_var_special_name(int nr)
6084{
6085 switch (nr)
6086 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006087 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006088 case VVAL_TRUE: return "v:true";
6089 case VVAL_NONE: return "v:none";
6090 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006091 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006092 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006093 return "42";
6094}
6095
Bram Moolenaar8c711452005-01-14 21:53:12 +00006096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 * Return a string with the string representation of a variable.
6098 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006100 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006101 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6102 * stings as "string()", otherwise does not put quotes around strings, as
6103 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006104 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6105 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006106 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006108 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006109echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006110 typval_T *tv,
6111 char_u **tofree,
6112 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006113 int copyID,
6114 int echo_style,
6115 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006116 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118 static int recurse = 0;
6119 char_u *r = NULL;
6120
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006123 if (!did_echo_string_emsg)
6124 {
6125 /* Only give this message once for a recursive call to avoid
6126 * flooding the user with errors. And stop iterating over lists
6127 * and dicts. */
6128 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006129 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006130 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006131 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006132 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006133 }
6134 ++recurse;
6135
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 switch (tv->v_type)
6137 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006138 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006139 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006140 {
6141 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006142 r = tv->vval.v_string;
6143 if (r == NULL)
6144 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006145 }
6146 else
6147 {
6148 *tofree = string_quote(tv->vval.v_string, FALSE);
6149 r = *tofree;
6150 }
6151 break;
6152
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006154 if (echo_style)
6155 {
6156 *tofree = NULL;
6157 r = tv->vval.v_string;
6158 }
6159 else
6160 {
6161 *tofree = string_quote(tv->vval.v_string, TRUE);
6162 r = *tofree;
6163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006164 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006165
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006166 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006167 {
6168 partial_T *pt = tv->vval.v_partial;
6169 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006170 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006171 garray_T ga;
6172 int i;
6173 char_u *tf;
6174
6175 ga_init2(&ga, 1, 100);
6176 ga_concat(&ga, (char_u *)"function(");
6177 if (fname != NULL)
6178 {
6179 ga_concat(&ga, fname);
6180 vim_free(fname);
6181 }
6182 if (pt != NULL && pt->pt_argc > 0)
6183 {
6184 ga_concat(&ga, (char_u *)", [");
6185 for (i = 0; i < pt->pt_argc; ++i)
6186 {
6187 if (i > 0)
6188 ga_concat(&ga, (char_u *)", ");
6189 ga_concat(&ga,
6190 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6191 vim_free(tf);
6192 }
6193 ga_concat(&ga, (char_u *)"]");
6194 }
6195 if (pt != NULL && pt->pt_dict != NULL)
6196 {
6197 typval_T dtv;
6198
6199 ga_concat(&ga, (char_u *)", ");
6200 dtv.v_type = VAR_DICT;
6201 dtv.vval.v_dict = pt->pt_dict;
6202 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6203 vim_free(tf);
6204 }
6205 ga_concat(&ga, (char_u *)")");
6206
6207 *tofree = ga.ga_data;
6208 r = *tofree;
6209 break;
6210 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006211
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006212 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006213 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006214 break;
6215
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006217 if (tv->vval.v_list == NULL)
6218 {
6219 *tofree = NULL;
6220 r = NULL;
6221 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006222 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6223 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006224 {
6225 *tofree = NULL;
6226 r = (char_u *)"[...]";
6227 }
6228 else
6229 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006230 int old_copyID = tv->vval.v_list->lv_copyID;
6231
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006232 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006233 *tofree = list2string(tv, copyID, restore_copyID);
6234 if (restore_copyID)
6235 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006236 r = *tofree;
6237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006239
Bram Moolenaar8c711452005-01-14 21:53:12 +00006240 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006241 if (tv->vval.v_dict == NULL)
6242 {
6243 *tofree = NULL;
6244 r = NULL;
6245 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006246 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6247 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006248 {
6249 *tofree = NULL;
6250 r = (char_u *)"{...}";
6251 }
6252 else
6253 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006254 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006255 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006256 *tofree = dict2string(tv, copyID, restore_copyID);
6257 if (restore_copyID)
6258 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006259 r = *tofree;
6260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006262
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006263 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006264 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006265 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006266 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006267 break;
6268
Bram Moolenaar835dc632016-02-07 14:27:38 +01006269 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006270 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006271 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006272 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006273 if (composite_val)
6274 {
6275 *tofree = string_quote(r, FALSE);
6276 r = *tofree;
6277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006279
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006280 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006281#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006282 *tofree = NULL;
6283 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6284 r = numbuf;
6285 break;
6286#endif
6287
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006288 case VAR_SPECIAL:
6289 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006290 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006291 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006292 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006293
Bram Moolenaar8502c702014-06-17 12:51:16 +02006294 if (--recurse == 0)
6295 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006297}
6298
6299/*
6300 * Return a string with the string representation of a variable.
6301 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6302 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006303 * Does not put quotes around strings, as ":echo" displays values.
6304 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6305 * May return NULL.
6306 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006308echo_string(
6309 typval_T *tv,
6310 char_u **tofree,
6311 char_u *numbuf,
6312 int copyID)
6313{
6314 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6315}
6316
6317/*
6318 * Return a string with the string representation of a variable.
6319 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6320 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006321 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006322 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006323 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325tv2string(
6326 typval_T *tv,
6327 char_u **tofree,
6328 char_u *numbuf,
6329 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006330{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006331 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332}
6333
6334/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 * Return string "str" in ' quotes, doubling ' characters.
6336 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006338 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006340string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006341{
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006343 char_u *p, *r, *s;
6344
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 len = (function ? 13 : 3);
6346 if (str != NULL)
6347 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006348 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006349 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 if (*p == '\'')
6351 ++len;
6352 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006353 s = r = alloc(len);
6354 if (r != NULL)
6355 {
6356 if (function)
6357 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006359 r += 10;
6360 }
6361 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006362 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 if (str != NULL)
6364 for (p = str; *p != NUL; )
6365 {
6366 if (*p == '\'')
6367 *r++ = '\'';
6368 MB_COPY_CHAR(p, r);
6369 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006371 if (function)
6372 *r++ = ')';
6373 *r++ = NUL;
6374 }
6375 return s;
6376}
6377
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006378#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006379/*
6380 * Convert the string "text" to a floating point number.
6381 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6382 * this always uses a decimal point.
6383 * Returns the length of the text that was consumed.
6384 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006386string2float(
6387 char_u *text,
6388 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006389{
6390 char *s = (char *)text;
6391 float_T f;
6392
Bram Moolenaar62473612017-01-08 19:25:40 +01006393 /* MS-Windows does not deal with "inf" and "nan" properly. */
6394 if (STRNICMP(text, "inf", 3) == 0)
6395 {
6396 *value = INFINITY;
6397 return 3;
6398 }
6399 if (STRNICMP(text, "-inf", 3) == 0)
6400 {
6401 *value = -INFINITY;
6402 return 4;
6403 }
6404 if (STRNICMP(text, "nan", 3) == 0)
6405 {
6406 *value = NAN;
6407 return 3;
6408 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006409 f = strtod(s, &s);
6410 *value = f;
6411 return (int)((char_u *)s - text);
6412}
6413#endif
6414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 * Get the value of an environment variable.
6417 * "arg" is pointing to the '$'. It is advanced to after the name.
6418 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006419 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 */
6421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006422get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
6424 char_u *string = NULL;
6425 int len;
6426 int cc;
6427 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
6430 ++*arg;
6431 name = *arg;
6432 len = get_env_len(arg);
6433 if (evaluate)
6434 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006435 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006436 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006438 cc = name[len];
6439 name[len] = NUL;
6440 /* first try vim_getenv(), fast for normal environment vars */
6441 string = vim_getenv(name, &mustfree);
6442 if (string != NULL && *string != NUL)
6443 {
6444 if (!mustfree)
6445 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006447 else
6448 {
6449 if (mustfree)
6450 vim_free(string);
6451
6452 /* next try expanding things like $VIM and ${HOME} */
6453 string = expand_env_save(name - 1);
6454 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006455 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006456 }
6457 name[len] = cc;
6458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006459 rettv->v_type = VAR_STRING;
6460 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462
6463 return OK;
6464}
6465
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006466
6467
6468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006470 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473var2fpos(
6474 typval_T *varp,
6475 int dollar_lnum, /* TRUE when $ is last line */
6476 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006478 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006480 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaara5525202006-03-02 22:52:09 +00006482 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006483 if (varp->v_type == VAR_LIST)
6484 {
6485 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006486 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006487 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006488 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006489
6490 l = varp->vval.v_list;
6491 if (l == NULL)
6492 return NULL;
6493
6494 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006495 pos.lnum = list_find_nr(l, 0L, &error);
6496 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006497 return NULL; /* invalid line number */
6498
6499 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006500 pos.col = list_find_nr(l, 1L, &error);
6501 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006502 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006503 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006504
6505 /* We accept "$" for the column number: last column. */
6506 li = list_find(l, 1L);
6507 if (li != NULL && li->li_tv.v_type == VAR_STRING
6508 && li->li_tv.vval.v_string != NULL
6509 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6510 pos.col = len + 1;
6511
Bram Moolenaara5525202006-03-02 22:52:09 +00006512 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006513 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006514 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006515 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006516
Bram Moolenaara5525202006-03-02 22:52:09 +00006517 /* Get the virtual offset. Defaults to zero. */
6518 pos.coladd = list_find_nr(l, 2L, &error);
6519 if (error)
6520 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006521
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006522 return &pos;
6523 }
6524
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006525 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006526 if (name == NULL)
6527 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006528 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006530 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6531 {
6532 if (VIsual_active)
6533 return &VIsual;
6534 return &curwin->w_cursor;
6535 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006536 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006538 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6540 return NULL;
6541 return pp;
6542 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006543
Bram Moolenaara5525202006-03-02 22:52:09 +00006544 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006545
Bram Moolenaar477933c2007-07-17 14:32:23 +00006546 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006547 {
6548 pos.col = 0;
6549 if (name[1] == '0') /* "w0": first visible line */
6550 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006551 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006552 /* In silent Ex mode topline is zero, but that's not a valid line
6553 * number; use one instead. */
6554 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006555 return &pos;
6556 }
6557 else if (name[1] == '$') /* "w$": last visible line */
6558 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006559 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006560 /* In silent Ex mode botline is zero, return zero then. */
6561 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006562 return &pos;
6563 }
6564 }
6565 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006567 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
6569 pos.lnum = curbuf->b_ml.ml_line_count;
6570 pos.col = 0;
6571 }
6572 else
6573 {
6574 pos.lnum = curwin->w_cursor.lnum;
6575 pos.col = (colnr_T)STRLEN(ml_get_curline());
6576 }
6577 return &pos;
6578 }
6579 return NULL;
6580}
6581
6582/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006583 * Convert list in "arg" into a position and optional file number.
6584 * When "fnump" is NULL there is no file number, only 3 items.
6585 * Note that the column is passed on as-is, the caller may want to decrement
6586 * it to use 1 for the first column.
6587 * Return FAIL when conversion is not possible, doesn't check the position for
6588 * validity.
6589 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006591list2fpos(
6592 typval_T *arg,
6593 pos_T *posp,
6594 int *fnump,
6595 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006596{
6597 list_T *l = arg->vval.v_list;
6598 long i = 0;
6599 long n;
6600
Bram Moolenaar493c1782014-05-28 14:34:46 +02006601 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6602 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006603 if (arg->v_type != VAR_LIST
6604 || l == NULL
6605 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006606 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006607 return FAIL;
6608
6609 if (fnump != NULL)
6610 {
6611 n = list_find_nr(l, i++, NULL); /* fnum */
6612 if (n < 0)
6613 return FAIL;
6614 if (n == 0)
6615 n = curbuf->b_fnum; /* current buffer */
6616 *fnump = n;
6617 }
6618
6619 n = list_find_nr(l, i++, NULL); /* lnum */
6620 if (n < 0)
6621 return FAIL;
6622 posp->lnum = n;
6623
6624 n = list_find_nr(l, i++, NULL); /* col */
6625 if (n < 0)
6626 return FAIL;
6627 posp->col = n;
6628
Bram Moolenaar493c1782014-05-28 14:34:46 +02006629 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006630 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006631 posp->coladd = 0;
6632 else
6633 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006634
Bram Moolenaar493c1782014-05-28 14:34:46 +02006635 if (curswantp != NULL)
6636 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6637
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006638 return OK;
6639}
6640
6641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 * Get the length of an environment variable name.
6643 * Advance "arg" to the first character after the name.
6644 * Return 0 for error.
6645 */
6646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 char_u *p;
6650 int len;
6651
6652 for (p = *arg; vim_isIDc(*p); ++p)
6653 ;
6654 if (p == *arg) /* no name found */
6655 return 0;
6656
6657 len = (int)(p - *arg);
6658 *arg = p;
6659 return len;
6660}
6661
6662/*
6663 * Get the length of the name of a function or internal variable.
6664 * "arg" is advanced to the first non-white character after the name.
6665 * Return 0 if something is wrong.
6666 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 char_u *p;
6671 int len;
6672
6673 /* Find the end of the name. */
6674 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006675 {
6676 if (*p == ':')
6677 {
6678 /* "s:" is start of "s:var", but "n:" is not and can be used in
6679 * slice "[n:]". Also "xx:" is not a namespace. */
6680 len = (int)(p - *arg);
6681 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6682 || len > 1)
6683 break;
6684 }
6685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if (p == *arg) /* no name found */
6687 return 0;
6688
6689 len = (int)(p - *arg);
6690 *arg = skipwhite(p);
6691
6692 return len;
6693}
6694
6695/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006696 * Get the length of the name of a variable or function.
6697 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006699 * Return -1 if curly braces expansion failed.
6700 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 * If the name contains 'magic' {}'s, expand them and return the
6702 * expanded name in an allocated string via 'alias' - caller must free.
6703 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705get_name_len(
6706 char_u **arg,
6707 char_u **alias,
6708 int evaluate,
6709 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 char_u *p;
6713 char_u *expr_start;
6714 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 *alias = NULL; /* default to no alias */
6717
6718 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6719 && (*arg)[2] == (int)KE_SNR)
6720 {
6721 /* hard coded <SNR>, already translated */
6722 *arg += 3;
6723 return get_id_len(arg) + 3;
6724 }
6725 len = eval_fname_script(*arg);
6726 if (len > 0)
6727 {
6728 /* literal "<SID>", "s:" or "<SNR>" */
6729 *arg += len;
6730 }
6731
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006733 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006735 p = find_name_end(*arg, &expr_start, &expr_end,
6736 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (expr_start != NULL)
6738 {
6739 char_u *temp_string;
6740
6741 if (!evaluate)
6742 {
6743 len += (int)(p - *arg);
6744 *arg = skipwhite(p);
6745 return len;
6746 }
6747
6748 /*
6749 * Include any <SID> etc in the expanded string:
6750 * Thus the -len here.
6751 */
6752 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6753 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006754 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 *alias = temp_string;
6756 *arg = skipwhite(p);
6757 return (int)STRLEN(temp_string);
6758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006761 // Only give an error when there is something, otherwise it will be
6762 // reported at a higher level.
6763 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006764 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766 return len;
6767}
6768
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006769/*
6770 * Find the end of a variable or function name, taking care of magic braces.
6771 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6772 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006773 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 * Return a pointer to just after the name. Equal to "arg" if there is no
6775 * valid name.
6776 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006777 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006778find_name_end(
6779 char_u *arg,
6780 char_u **expr_start,
6781 char_u **expr_end,
6782 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006784 int mb_nest = 0;
6785 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006787 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006789 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791 *expr_start = NULL;
6792 *expr_end = NULL;
6793 }
6794
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006795 /* Quick check for valid starting character. */
6796 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6797 return arg;
6798
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006799 for (p = arg; *p != NUL
6800 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006801 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006802 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006804 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006806 if (*p == '\'')
6807 {
6808 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006809 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006810 ;
6811 if (*p == NUL)
6812 break;
6813 }
6814 else if (*p == '"')
6815 {
6816 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006817 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006818 if (*p == '\\' && p[1] != NUL)
6819 ++p;
6820 if (*p == NUL)
6821 break;
6822 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006823 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6824 {
6825 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006826 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006827 len = (int)(p - arg);
6828 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006829 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006830 break;
6831 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 if (*p == '[')
6836 ++br_nest;
6837 else if (*p == ']')
6838 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843 if (*p == '{')
6844 {
6845 mb_nest++;
6846 if (expr_start != NULL && *expr_start == NULL)
6847 *expr_start = p;
6848 }
6849 else if (*p == '}')
6850 {
6851 mb_nest--;
6852 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6853 *expr_end = p;
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 }
6857
6858 return p;
6859}
6860
6861/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006862 * Expands out the 'magic' {}'s in a variable/function name.
6863 * Note that this can call itself recursively, to deal with
6864 * constructs like foo{bar}{baz}{bam}
6865 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6866 * "in_start" ^
6867 * "expr_start" ^
6868 * "expr_end" ^
6869 * "in_end" ^
6870 *
6871 * Returns a new allocated string, which the caller must free.
6872 * Returns NULL for failure.
6873 */
6874 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006875make_expanded_name(
6876 char_u *in_start,
6877 char_u *expr_start,
6878 char_u *expr_end,
6879 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006880{
6881 char_u c1;
6882 char_u *retval = NULL;
6883 char_u *temp_result;
6884 char_u *nextcmd = NULL;
6885
6886 if (expr_end == NULL || in_end == NULL)
6887 return NULL;
6888 *expr_start = NUL;
6889 *expr_end = NUL;
6890 c1 = *in_end;
6891 *in_end = NUL;
6892
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006893 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006894 if (temp_result != NULL && nextcmd == NULL)
6895 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006896 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6897 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006898 if (retval != NULL)
6899 {
6900 STRCPY(retval, in_start);
6901 STRCAT(retval, temp_result);
6902 STRCAT(retval, expr_end + 1);
6903 }
6904 }
6905 vim_free(temp_result);
6906
6907 *in_end = c1; /* put char back for error messages */
6908 *expr_start = '{';
6909 *expr_end = '}';
6910
6911 if (retval != NULL)
6912 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006913 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006914 if (expr_start != NULL)
6915 {
6916 /* Further expansion! */
6917 temp_result = make_expanded_name(retval, expr_start,
6918 expr_end, temp_result);
6919 vim_free(retval);
6920 retval = temp_result;
6921 }
6922 }
6923
6924 return retval;
6925}
6926
6927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006931 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006932eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006934 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6935}
6936
6937/*
6938 * Return TRUE if character "c" can be used as the first character in a
6939 * variable or function name (excluding '{' and '}').
6940 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006942eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006943{
6944 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945}
6946
6947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 * Set number v: variable to "val".
6949 */
6950 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006951set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954}
6955
6956/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006957 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006959 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963}
6964
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006965/*
6966 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006967 * If the String variable has never been set, return an empty string.
6968 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006969 */
6970 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006972{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006973 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006974}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006977 * Get List v: variable value. Caller must take care of reference count when
6978 * needed.
6979 */
6980 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006981get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006982{
6983 return vimvars[idx].vv_list;
6984}
6985
6986/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006987 * Get Dict v: variable value. Caller must take care of reference count when
6988 * needed.
6989 */
6990 dict_T *
6991get_vim_var_dict(int idx)
6992{
6993 return vimvars[idx].vv_dict;
6994}
6995
6996/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006997 * Set v:char to character "c".
6998 */
6999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007000set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007001{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007002 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007003
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007004 if (has_mbyte)
7005 buf[(*mb_char2bytes)(c, buf)] = NUL;
7006 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007007 {
7008 buf[0] = c;
7009 buf[1] = NUL;
7010 }
7011 set_vim_var_string(VV_CHAR, buf, -1);
7012}
7013
7014/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007015 * Set v:count to "count" and v:count1 to "count1".
7016 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 */
7018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007019set_vcount(
7020 long count,
7021 long count1,
7022 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007024 if (set_prevcount)
7025 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007026 vimvars[VV_COUNT].vv_nr = count;
7027 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028}
7029
7030/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007031 * Save variables that might be changed as a side effect. Used when executing
7032 * a timer callback.
7033 */
7034 void
7035save_vimvars(vimvars_save_T *vvsave)
7036{
7037 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7038 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7039 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7040}
7041
7042/*
7043 * Restore variables saved by save_vimvars().
7044 */
7045 void
7046restore_vimvars(vimvars_save_T *vvsave)
7047{
7048 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7049 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7050 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7051}
7052
7053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 * Set string v: variable to a copy of "val".
7055 */
7056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007057set_vim_var_string(
7058 int idx,
7059 char_u *val,
7060 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
Bram Moolenaara542c682016-01-31 16:28:04 +01007062 clear_tv(&vimvars[idx].vv_di.di_tv);
7063 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007065 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007067 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070}
7071
7072/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007073 * Set List v: variable to "val".
7074 */
7075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007076set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007077{
Bram Moolenaara542c682016-01-31 16:28:04 +01007078 clear_tv(&vimvars[idx].vv_di.di_tv);
7079 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007080 vimvars[idx].vv_list = val;
7081 if (val != NULL)
7082 ++val->lv_refcount;
7083}
7084
7085/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007086 * Set Dictionary v: variable to "val".
7087 */
7088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007090{
Bram Moolenaara542c682016-01-31 16:28:04 +01007091 clear_tv(&vimvars[idx].vv_di.di_tv);
7092 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007093 vimvars[idx].vv_dict = val;
7094 if (val != NULL)
7095 {
7096 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007097 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007098 }
7099}
7100
7101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 * Set v:register if needed.
7103 */
7104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
7107 char_u regname;
7108
7109 if (c == 0 || c == ' ')
7110 regname = '"';
7111 else
7112 regname = c;
7113 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 set_vim_var_string(VV_REG, &regname, 1);
7116}
7117
7118/*
7119 * Get or set v:exception. If "oldval" == NULL, return the current value.
7120 * Otherwise, restore the value to "oldval" and return NULL.
7121 * Must always be called in pairs to save and restore v:exception! Does not
7122 * take care of memory allocations.
7123 */
7124 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129
Bram Moolenaare9a41262005-01-15 22:18:47 +00007130 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 return NULL;
7132}
7133
7134/*
7135 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7136 * Otherwise, restore the value to "oldval" and return NULL.
7137 * Must always be called in pairs to save and restore v:throwpoint! Does not
7138 * take care of memory allocations.
7139 */
7140 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 return NULL;
7148}
7149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150/*
7151 * Set v:cmdarg.
7152 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7153 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7154 * Must always be called in pairs!
7155 */
7156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 char_u *oldval;
7160 char_u *newval;
7161 unsigned len;
7162
Bram Moolenaare9a41262005-01-15 22:18:47 +00007163 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007164 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007166 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007168 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 }
7170
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007171 if (eap->force_bin == FORCE_BIN)
7172 len = 6;
7173 else if (eap->force_bin == FORCE_NOBIN)
7174 len = 8;
7175 else
7176 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007177
7178 if (eap->read_edit)
7179 len += 7;
7180
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007181 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007182 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007183 if (eap->force_enc != 0)
7184 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007185 if (eap->bad_char != 0)
7186 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007187
7188 newval = alloc(len + 1);
7189 if (newval == NULL)
7190 return NULL;
7191
7192 if (eap->force_bin == FORCE_BIN)
7193 sprintf((char *)newval, " ++bin");
7194 else if (eap->force_bin == FORCE_NOBIN)
7195 sprintf((char *)newval, " ++nobin");
7196 else
7197 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007198
7199 if (eap->read_edit)
7200 STRCAT(newval, " ++edit");
7201
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007202 if (eap->force_ff != 0)
7203 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007204 eap->force_ff == 'u' ? "unix"
7205 : eap->force_ff == 'd' ? "dos"
7206 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007207 if (eap->force_enc != 0)
7208 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7209 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007210 if (eap->bad_char == BAD_KEEP)
7211 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7212 else if (eap->bad_char == BAD_DROP)
7213 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7214 else if (eap->bad_char != 0)
7215 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007216 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007217 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220/*
7221 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007222 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225get_var_tv(
7226 char_u *name,
7227 int len, /* length of "name" */
7228 typval_T *rettv, /* NULL when only checking existence */
7229 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7230 int verbose, /* may give error message */
7231 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238 /* truncate the name, so that we can use strcmp() */
7239 cc = name[len];
7240 name[len] = NUL;
7241
7242 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 * Check for user-defined variables.
7244 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007245 v = find_var(name, NULL, no_autoload);
7246 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007248 tv = &v->di_tv;
7249 if (dip != NULL)
7250 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
7252
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007255 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007256 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 ret = FAIL;
7258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007259 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262 name[len] = cc;
7263
7264 return ret;
7265}
7266
7267/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007268 * Check if variable "name[len]" is a local variable or an argument.
7269 * If so, "*eval_lavars_used" is set to TRUE.
7270 */
7271 static void
7272check_vars(char_u *name, int len)
7273{
7274 int cc;
7275 char_u *varname;
7276 hashtab_T *ht;
7277
7278 if (eval_lavars_used == NULL)
7279 return;
7280
7281 /* truncate the name, so that we can use strcmp() */
7282 cc = name[len];
7283 name[len] = NUL;
7284
7285 ht = find_var_ht(name, &varname);
7286 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7287 {
7288 if (find_var(name, NULL, TRUE) != NULL)
7289 *eval_lavars_used = TRUE;
7290 }
7291
7292 name[len] = cc;
7293}
7294
7295/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007296 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7297 * Also handle function call with Funcref variable: func(expr)
7298 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7299 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007301handle_subscript(
7302 char_u **arg,
7303 typval_T *rettv,
7304 int evaluate, /* do more than finding the end */
7305 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007306{
7307 int ret = OK;
7308 dict_T *selfdict = NULL;
7309 char_u *s;
7310 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007311 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007312
7313 while (ret == OK
7314 && (**arg == '['
7315 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007316 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7317 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007318 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007319 {
7320 if (**arg == '(')
7321 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007322 partial_T *pt = NULL;
7323
Bram Moolenaard9fba312005-06-26 22:34:35 +00007324 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007325 if (evaluate)
7326 {
7327 functv = *rettv;
7328 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007329
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007330 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007331 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007332 {
7333 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007334 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007335 }
7336 else
7337 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007338 }
7339 else
7340 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007341 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007342 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007343 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007344
7345 /* Clear the funcref afterwards, so that deleting it while
7346 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007347 if (evaluate)
7348 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007349
7350 /* Stop the expression evaluation when immediately aborting on
7351 * error, or when an interrupt occurred or an exception was thrown
7352 * but not caught. */
7353 if (aborting())
7354 {
7355 if (ret == OK)
7356 clear_tv(rettv);
7357 ret = FAIL;
7358 }
7359 dict_unref(selfdict);
7360 selfdict = NULL;
7361 }
7362 else /* **arg == '[' || **arg == '.' */
7363 {
7364 dict_unref(selfdict);
7365 if (rettv->v_type == VAR_DICT)
7366 {
7367 selfdict = rettv->vval.v_dict;
7368 if (selfdict != NULL)
7369 ++selfdict->dv_refcount;
7370 }
7371 else
7372 selfdict = NULL;
7373 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7374 {
7375 clear_tv(rettv);
7376 ret = FAIL;
7377 }
7378 }
7379 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007380
Bram Moolenaar1d429612016-05-24 15:44:17 +02007381 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7382 * Don't do this when "Func" is already a partial that was bound
7383 * explicitly (pt_auto is FALSE). */
7384 if (selfdict != NULL
7385 && (rettv->v_type == VAR_FUNC
7386 || (rettv->v_type == VAR_PARTIAL
7387 && (rettv->vval.v_partial->pt_auto
7388 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007389 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007390
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007391 dict_unref(selfdict);
7392 return ret;
7393}
7394
7395/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007396 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 * value).
7398 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007399 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007400alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007402 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007403}
7404
7405/*
7406 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * The string "s" must have been allocated, it is consumed.
7408 * Return NULL for out of memory, the variable otherwise.
7409 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 rettv = alloc_tv();
7416 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418 rettv->v_type = VAR_STRING;
7419 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 else
7422 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007427 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007430free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 if (varp != NULL)
7433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007434 switch (varp->v_type)
7435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007436 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007437 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007438 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007439 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007440 vim_free(varp->vval.v_string);
7441 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007442 case VAR_PARTIAL:
7443 partial_unref(varp->vval.v_partial);
7444 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007445 case VAR_BLOB:
7446 blob_unref(varp->vval.v_blob);
7447 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007448 case VAR_LIST:
7449 list_unref(varp->vval.v_list);
7450 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007451 case VAR_DICT:
7452 dict_unref(varp->vval.v_dict);
7453 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007454 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007455#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007456 job_unref(varp->vval.v_job);
7457 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007458#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007459 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007461 channel_unref(varp->vval.v_channel);
7462 break;
7463#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007464 case VAR_NUMBER:
7465 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007466 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007467 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007468 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 vim_free(varp);
7471 }
7472}
7473
7474/*
7475 * Free the memory for a variable value and set the value to NULL or 0.
7476 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
7480 if (varp != NULL)
7481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007485 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007486 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007487 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007488 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007489 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007490 case VAR_PARTIAL:
7491 partial_unref(varp->vval.v_partial);
7492 varp->vval.v_partial = NULL;
7493 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007494 case VAR_BLOB:
7495 blob_unref(varp->vval.v_blob);
7496 varp->vval.v_blob = NULL;
7497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007498 case VAR_LIST:
7499 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 case VAR_DICT:
7503 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007507 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508 varp->vval.v_number = 0;
7509 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007510 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007511#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007512 varp->vval.v_float = 0.0;
7513 break;
7514#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007515 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007517 job_unref(varp->vval.v_job);
7518 varp->vval.v_job = NULL;
7519#endif
7520 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007521 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007522#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007523 channel_unref(varp->vval.v_channel);
7524 varp->vval.v_channel = NULL;
7525#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526 case VAR_UNKNOWN:
7527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007529 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 }
7531}
7532
7533/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 * Set the value of a variable to NULL without freeing items.
7535 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007537init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538{
7539 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541}
7542
7543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 * Get the number value of a variable.
7545 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007546 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007547 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007548 * caller of incompatible types: it sets *denote to TRUE if "denote"
7549 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007551 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007552tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007554 int error = FALSE;
7555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007556 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007557}
7558
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007559 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007560tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007561{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007562 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007564 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007567 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007568 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007569#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007570 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571 break;
7572#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007574 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007575 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007576 break;
7577 case VAR_STRING:
7578 if (varp->vval.v_string != NULL)
7579 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007580 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007581 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007582 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007583 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007584 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007585 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007586 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007587 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007588 case VAR_SPECIAL:
7589 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7590 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007591 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007592#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007594 break;
7595#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007596 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007597#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007599 break;
7600#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007601 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007602 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007603 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007604 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007605 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007608 if (denote == NULL) /* useful for values that must be unsigned */
7609 n = -1;
7610 else
7611 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613}
7614
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007615#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007617tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007618{
7619 switch (varp->v_type)
7620 {
7621 case VAR_NUMBER:
7622 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007623 case VAR_FLOAT:
7624 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007625 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007626 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007627 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007628 break;
7629 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007630 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007631 break;
7632 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007634 break;
7635 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007637 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007638 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007640 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007641 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007642# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007644 break;
7645# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007646 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007647# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007648 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007649 break;
7650# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007651 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007652 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007653 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007654 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007655 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007656 break;
7657 }
7658 return 0;
7659}
7660#endif
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * Get the string value of a variable.
7664 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007665 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7666 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 * If the String variable has never been set, return an empty string.
7668 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007669 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007670 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007672 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007673tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 static char_u mybuf[NUMBUFLEN];
7676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007677 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678}
7679
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007680 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007681tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007683 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684
7685 return res != NULL ? res : (char_u *)"";
7686}
7687
Bram Moolenaar7d647822014-04-05 21:28:56 +02007688/*
7689 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7690 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007691 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007692tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007693{
7694 static char_u mybuf[NUMBUFLEN];
7695
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007696 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007697}
7698
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007699 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007701{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007702 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007705 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007706 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007707 return buf;
7708 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007709 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007710 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007711 break;
7712 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007713 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 break;
7715 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007716 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007718 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007719#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007720 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007721 break;
7722#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723 case VAR_STRING:
7724 if (varp->vval.v_string != NULL)
7725 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007726 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007727 case VAR_SPECIAL:
7728 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7729 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007730 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007731 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007732 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007733 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007734#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007735 {
7736 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007737 char *status;
7738
7739 if (job == NULL)
7740 return (char_u *)"no process";
7741 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007742 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007743 : "run";
7744# ifdef UNIX
7745 vim_snprintf((char *)buf, NUMBUFLEN,
7746 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007747# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007748 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007749 "process %ld %s",
7750 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007751 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007752# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007753 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007754 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7755# endif
7756 return buf;
7757 }
7758#endif
7759 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007760 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007761#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007762 {
7763 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007764 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007765
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007766 if (channel == NULL)
7767 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7768 else
7769 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007770 "channel %d %s", channel->ch_id, status);
7771 return buf;
7772 }
7773#endif
7774 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007775 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007776 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780}
7781
7782/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007783 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7784 * string() on Dict, List, etc.
7785 */
7786 char_u *
7787tv_stringify(typval_T *varp, char_u *buf)
7788{
7789 if (varp->v_type == VAR_LIST
7790 || varp->v_type == VAR_DICT
7791 || varp->v_type == VAR_FUNC
7792 || varp->v_type == VAR_PARTIAL
7793 || varp->v_type == VAR_FLOAT)
7794 {
7795 typval_T tmp;
7796
7797 f_string(varp, &tmp);
7798 tv_get_string_buf(&tmp, buf);
7799 clear_tv(varp);
7800 *varp = tmp;
7801 return tmp.vval.v_string;
7802 }
7803 return tv_get_string_buf(varp, buf);
7804}
7805
7806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 * Find variable "name" in the list of variables.
7808 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007810 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007811 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007813 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007818 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaara7043832005-01-21 11:56:39 +00007820 ht = find_var_ht(name, &varname);
7821 if (htp != NULL)
7822 *htp = ht;
7823 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007825 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7826 if (ret != NULL)
7827 return ret;
7828
7829 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007830 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831}
7832
7833/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007834 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007835 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838find_var_in_ht(
7839 hashtab_T *ht,
7840 int htname,
7841 char_u *varname,
7842 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007843{
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 hashitem_T *hi;
7845
7846 if (*varname == NUL)
7847 {
7848 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007849 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007851 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 case 'g': return &globvars_var;
7853 case 'v': return &vimvars_var;
7854 case 'b': return &curbuf->b_bufvar;
7855 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007856 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007857 case 'l': return get_funccal_local_var();
7858 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 }
7860 return NULL;
7861 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007862
7863 hi = hash_find(ht, varname);
7864 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007865 {
7866 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007867 * worked find the variable again. Don't auto-load a script if it was
7868 * loaded already, otherwise it would be loaded every time when
7869 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007870 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007871 {
7872 /* Note: script_autoload() may make "hi" invalid. It must either
7873 * be obtained again or not used. */
7874 if (!script_autoload(varname, FALSE) || aborting())
7875 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007876 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007877 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007878 if (HASHITEM_EMPTY(hi))
7879 return NULL;
7880 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007882}
7883
7884/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007886 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007887 * Set "varname" to the start of name without ':'.
7888 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007889 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007890find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007892 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007893 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007894
Bram Moolenaar73627d02015-08-11 15:46:09 +02007895 if (name[0] == NUL)
7896 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 if (name[1] != ':')
7898 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007899 /* The name must not start with a colon or #. */
7900 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 return NULL;
7902 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007903
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007904 // "version" is "v:version" in all scopes if scriptversion < 3.
7905 // Same for a few other variables marked with VV_COMPAT.
7906 if (current_sctx.sc_version < 3)
7907 {
7908 hi = hash_find(&compat_hashtab, name);
7909 if (!HASHITEM_EMPTY(hi))
7910 return &compat_hashtab;
7911 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007912
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007913 ht = get_funccal_local_ht();
7914 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007916 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007919 if (*name == 'g') /* global variable */
7920 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007921 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7922 */
7923 if (vim_strchr(name + 2, ':') != NULL
7924 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007925 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007927 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007929 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007930 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007931 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007932 if (*name == 'v') /* v: variable */
7933 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007934 if (*name == 'a') /* a: function argument */
7935 return get_funccal_args_ht();
7936 if (*name == 'l') /* l: local function variable */
7937 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007939 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7940 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 return NULL;
7942}
7943
7944/*
7945 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007946 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 * Returns NULL when it doesn't exist.
7948 */
7949 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007950get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007954 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (v == NULL)
7956 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007957 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958}
7959
7960/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 * sourcing this script and when executing functions defined in the script.
7963 */
7964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007965new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
Bram Moolenaara7043832005-01-21 11:56:39 +00007967 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 hashtab_T *ht;
7969 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7972 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007973 /* Re-allocating ga_data means that an ht_array pointing to
7974 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007976 for (i = 1; i <= ga_scripts.ga_len; ++i)
7977 {
7978 ht = &SCRIPT_VARS(i);
7979 if (ht->ht_mask == HT_INIT_SIZE - 1)
7980 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007981 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007983 }
7984
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 while (ga_scripts.ga_len < id)
7986 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007987 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007988 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007989 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 }
7992 }
7993}
7994
7995/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7997 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 */
7999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008000init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
Bram Moolenaar33570922005-01-25 22:26:29 +00008002 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008003 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008004 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008005 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008006 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 dict_var->di_tv.vval.v_dict = dict;
8008 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008009 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8011 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012}
8013
8014/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008015 * Unreference a dictionary initialized by init_var_dict().
8016 */
8017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008018unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008019{
8020 /* Now the dict needs to be freed if no one else is using it, go back to
8021 * normal reference counting. */
8022 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8023 dict_unref(dict);
8024}
8025
8026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 * Frees all allocated variables and the value they contain.
8029 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 */
8031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008032vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008033{
8034 vars_clear_ext(ht, TRUE);
8035}
8036
8037/*
8038 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8039 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008041vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
Bram Moolenaara7043832005-01-21 11:56:39 +00008043 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 hashitem_T *hi;
8045 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008048 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008049 for (hi = ht->ht_array; todo > 0; ++hi)
8050 {
8051 if (!HASHITEM_EMPTY(hi))
8052 {
8053 --todo;
8054
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008056 * ht_array might change then. hash_clear() takes care of it
8057 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 v = HI2DI(hi);
8059 if (free_val)
8060 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008061 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008063 }
8064 }
8065 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008066 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
Bram Moolenaara7043832005-01-21 11:56:39 +00008069/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008070 * Delete a variable from hashtab "ht" at item "hi".
8071 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008074delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008077
8078 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008079 clear_tv(&di->di_tv);
8080 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081}
8082
8083/*
8084 * List the value of one internal variable.
8085 */
8086 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008087list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008089 char_u *tofree;
8090 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008091 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008093 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008095 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008096 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097}
8098
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008100list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008101 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008102 char_u *name,
8103 int type,
8104 char_u *string,
8105 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
Bram Moolenaar31859182007-08-14 20:41:13 +00008107 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8108 msg_start();
8109 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008111 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 msg_putchar(' ');
8113 msg_advance(22);
8114 if (type == VAR_NUMBER)
8115 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008116 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008117 msg_putchar('*');
8118 else if (type == VAR_LIST)
8119 {
8120 msg_putchar('[');
8121 if (*string == '[')
8122 ++string;
8123 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008124 else if (type == VAR_DICT)
8125 {
8126 msg_putchar('{');
8127 if (*string == '{')
8128 ++string;
8129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 else
8131 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008132
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008135 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008136 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008137 if (*first)
8138 {
8139 msg_clr_eos();
8140 *first = FALSE;
8141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142}
8143
8144/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 * If the variable already exists, the value is updated.
8147 * Otherwise the variable is created.
8148 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008150set_var(
8151 char_u *name,
8152 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008153 int copy) // make copy of value in "tv"
8154{
8155 set_var_const(name, tv, copy, FALSE);
8156}
8157
8158/*
8159 * Set variable "name" to value in "tv".
8160 * If the variable already exists and "is_const" is FALSE the value is updated.
8161 * Otherwise the variable is created.
8162 */
8163 static void
8164set_var_const(
8165 char_u *name,
8166 typval_T *tv,
8167 int copy, // make copy of value in "tv"
8168 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008174 ht = find_var_ht(name, &varname);
8175 if (ht == NULL || *varname == NUL)
8176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008177 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008178 return;
8179 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008180 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008181
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008182 /* Search in parent scope which is possible to reference from lambda */
8183 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008184 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008185
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008186 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8187 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008188 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008189
Bram Moolenaar33570922005-01-25 22:26:29 +00008190 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008192 if (is_const)
8193 {
8194 emsg(_(e_cannot_mod));
8195 return;
8196 }
8197
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008199 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008200 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008201 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008202
8203 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008204 * Handle setting internal v: variables separately where needed to
8205 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 */
8207 if (ht == &vimvarht)
8208 {
8209 if (v->di_tv.v_type == VAR_STRING)
8210 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008211 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008213 {
8214 char_u *val = tv_get_string(tv);
8215
8216 // Careful: when assigning to v:errmsg and tv_get_string()
8217 // causes an error message the variable will alrady be set.
8218 if (v->di_tv.vval.v_string == NULL)
8219 v->di_tv.vval.v_string = vim_strsave(val);
8220 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 else
8222 {
8223 /* Take over the string to avoid an extra alloc/free. */
8224 v->di_tv.vval.v_string = tv->vval.v_string;
8225 tv->vval.v_string = NULL;
8226 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008227 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008229 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008230 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008231 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232 if (STRCMP(varname, "searchforward") == 0)
8233 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008234#ifdef FEAT_SEARCH_EXTRA
8235 else if (STRCMP(varname, "hlsearch") == 0)
8236 {
8237 no_hlsearch = !v->di_tv.vval.v_number;
8238 redraw_all_later(SOME_VALID);
8239 }
8240#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008241 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008243 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008245 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008246 return;
8247 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008248 }
8249
8250 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 }
8252 else /* add a new variable */
8253 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008254 // Can't add "v:" or "a:" variable.
8255 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008257 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008258 return;
8259 }
8260
Bram Moolenaar31b81602019-02-10 22:14:27 +01008261 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008262 if (!valid_varname(varname))
8263 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008264
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008265 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008266 if (v == NULL)
8267 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008271 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 return;
8273 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008274 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008275 if (is_const)
8276 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008278
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008281 else
8282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008284 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008286 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008287
8288 if (is_const)
8289 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290}
8291
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008293 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 * Also give an error message.
8295 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008296 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008297var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298{
8299 if (flags & DI_FLAGS_RO)
8300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008301 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 return TRUE;
8303 }
8304 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008306 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 return TRUE;
8308 }
8309 return FALSE;
8310}
8311
8312/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008313 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8314 * Also give an error message.
8315 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008317var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008318{
8319 if (flags & DI_FLAGS_FIX)
8320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008321 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008322 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008323 return TRUE;
8324 }
8325 return FALSE;
8326}
8327
8328/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008329 * Check if a funcref is assigned to a valid variable name.
8330 * Return TRUE and give an error if not.
8331 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008333var_check_func_name(
8334 char_u *name, /* points to start of variable name */
8335 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008336{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008337 /* Allow for w: b: s: and t:. */
8338 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008339 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8340 ? name[2] : name[0]))
8341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008342 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008343 name);
8344 return TRUE;
8345 }
8346 /* Don't allow hiding a function. When "v" is not NULL we might be
8347 * assigning another function to the same var, the type is checked
8348 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008349 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008351 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008352 name);
8353 return TRUE;
8354 }
8355 return FALSE;
8356}
8357
8358/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008359 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008360 * Also give an error message, using "name" or _("name") when use_gettext is
8361 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008362 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008363 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008364var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008365{
8366 if (lock & VAR_LOCKED)
8367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008368 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008369 name == NULL ? (char_u *)_("Unknown")
8370 : use_gettext ? (char_u *)_(name)
8371 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008372 return TRUE;
8373 }
8374 if (lock & VAR_FIXED)
8375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008376 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008377 name == NULL ? (char_u *)_("Unknown")
8378 : use_gettext ? (char_u *)_(name)
8379 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008380 return TRUE;
8381 }
8382 return FALSE;
8383}
8384
8385/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008386 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8387 * Also give an error message, using "name" or _("name") when use_gettext is
8388 * TRUE.
8389 */
8390 static int
8391tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8392{
8393 int lock = 0;
8394
8395 switch (tv->v_type)
8396 {
8397 case VAR_BLOB:
8398 if (tv->vval.v_blob != NULL)
8399 lock = tv->vval.v_blob->bv_lock;
8400 break;
8401 case VAR_LIST:
8402 if (tv->vval.v_list != NULL)
8403 lock = tv->vval.v_list->lv_lock;
8404 break;
8405 case VAR_DICT:
8406 if (tv->vval.v_dict != NULL)
8407 lock = tv->vval.v_dict->dv_lock;
8408 break;
8409 default:
8410 break;
8411 }
8412 return var_check_lock(tv->v_lock, name, use_gettext)
8413 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8414}
8415
8416/*
8417 * Check if a variable name is valid.
8418 * Return FALSE and give an error if not.
8419 */
8420 int
8421valid_varname(char_u *varname)
8422{
8423 char_u *p;
8424
8425 for (p = varname; *p != NUL; ++p)
8426 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8427 && *p != AUTOLOAD_CHAR)
8428 {
8429 semsg(_(e_illvar), varname);
8430 return FALSE;
8431 }
8432 return TRUE;
8433}
8434
8435/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008438 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008439 * It is OK for "from" and "to" to point to the same item. This is used to
8440 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008443copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008445 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008446 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 switch (from->v_type)
8448 {
8449 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008450 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008451 to->vval.v_number = from->vval.v_number;
8452 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008453 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008454#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455 to->vval.v_float = from->vval.v_float;
8456 break;
8457#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008458 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008459#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008460 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008461 if (to->vval.v_job != NULL)
8462 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008463 break;
8464#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008465 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008466#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008467 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008468 if (to->vval.v_channel != NULL)
8469 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008470 break;
8471#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 case VAR_STRING:
8473 case VAR_FUNC:
8474 if (from->vval.v_string == NULL)
8475 to->vval.v_string = NULL;
8476 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008477 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008478 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008479 if (from->v_type == VAR_FUNC)
8480 func_ref(to->vval.v_string);
8481 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008483 case VAR_PARTIAL:
8484 if (from->vval.v_partial == NULL)
8485 to->vval.v_partial = NULL;
8486 else
8487 {
8488 to->vval.v_partial = from->vval.v_partial;
8489 ++to->vval.v_partial->pt_refcount;
8490 }
8491 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008492 case VAR_BLOB:
8493 if (from->vval.v_blob == NULL)
8494 to->vval.v_blob = NULL;
8495 else
8496 {
8497 to->vval.v_blob = from->vval.v_blob;
8498 ++to->vval.v_blob->bv_refcount;
8499 }
8500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 case VAR_LIST:
8502 if (from->vval.v_list == NULL)
8503 to->vval.v_list = NULL;
8504 else
8505 {
8506 to->vval.v_list = from->vval.v_list;
8507 ++to->vval.v_list->lv_refcount;
8508 }
8509 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008510 case VAR_DICT:
8511 if (from->vval.v_dict == NULL)
8512 to->vval.v_dict = NULL;
8513 else
8514 {
8515 to->vval.v_dict = from->vval.v_dict;
8516 ++to->vval.v_dict->dv_refcount;
8517 }
8518 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008519 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008520 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 break;
8522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524
8525/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526 * Make a copy of an item.
8527 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008528 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8529 * reference to an already copied list/dict can be used.
8530 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008531 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008533item_copy(
8534 typval_T *from,
8535 typval_T *to,
8536 int deep,
8537 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538{
8539 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008540 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008541
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008544 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008545 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 }
8547 ++recurse;
8548
8549 switch (from->v_type)
8550 {
8551 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008553 case VAR_STRING:
8554 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008555 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008556 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008557 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008558 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 copy_tv(from, to);
8560 break;
8561 case VAR_LIST:
8562 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008563 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008564 if (from->vval.v_list == NULL)
8565 to->vval.v_list = NULL;
8566 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8567 {
8568 /* use the copy made earlier */
8569 to->vval.v_list = from->vval.v_list->lv_copylist;
8570 ++to->vval.v_list->lv_refcount;
8571 }
8572 else
8573 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8574 if (to->vval.v_list == NULL)
8575 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008576 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008577 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008578 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008580 case VAR_DICT:
8581 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008582 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008583 if (from->vval.v_dict == NULL)
8584 to->vval.v_dict = NULL;
8585 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8586 {
8587 /* use the copy made earlier */
8588 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8589 ++to->vval.v_dict->dv_refcount;
8590 }
8591 else
8592 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8593 if (to->vval.v_dict == NULL)
8594 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008596 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008597 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008598 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 }
8600 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008601 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602}
8603
8604/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605 * This function is used by f_input() and f_inputdialog() functions. The third
8606 * argument to f_input() specifies the type of completion to use at the
8607 * prompt. The third argument to f_inputdialog() specifies the value to return
8608 * when the user cancels the prompt.
8609 */
8610 void
8611get_user_input(
8612 typval_T *argvars,
8613 typval_T *rettv,
8614 int inputdialog,
8615 int secret)
8616{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008617 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 char_u *p = NULL;
8619 int c;
8620 char_u buf[NUMBUFLEN];
8621 int cmd_silent_save = cmd_silent;
8622 char_u *defstr = (char_u *)"";
8623 int xp_type = EXPAND_NOTHING;
8624 char_u *xp_arg = NULL;
8625
8626 rettv->v_type = VAR_STRING;
8627 rettv->vval.v_string = NULL;
8628
8629#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008630 /* While starting up, there is no place to enter text. When running tests
8631 * with --not-a-term we assume feedkeys() will be used. */
8632 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633 return;
8634#endif
8635
8636 cmd_silent = FALSE; /* Want to see the prompt. */
8637 if (prompt != NULL)
8638 {
8639 /* Only the part of the message after the last NL is considered as
8640 * prompt for the command line */
8641 p = vim_strrchr(prompt, '\n');
8642 if (p == NULL)
8643 p = prompt;
8644 else
8645 {
8646 ++p;
8647 c = *p;
8648 *p = NUL;
8649 msg_start();
8650 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008651 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 msg_didout = FALSE;
8653 msg_starthere();
8654 *p = c;
8655 }
8656 cmdline_row = msg_row;
8657
8658 if (argvars[1].v_type != VAR_UNKNOWN)
8659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008660 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 if (defstr != NULL)
8662 stuffReadbuffSpec(defstr);
8663
8664 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8665 {
8666 char_u *xp_name;
8667 int xp_namelen;
8668 long argt;
8669
8670 /* input() with a third argument: completion */
8671 rettv->vval.v_string = NULL;
8672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008673 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 if (xp_name == NULL)
8675 return;
8676
8677 xp_namelen = (int)STRLEN(xp_name);
8678
8679 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8680 &xp_arg) == FAIL)
8681 return;
8682 }
8683 }
8684
8685 if (defstr != NULL)
8686 {
8687 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689 ex_normal_busy = 0;
8690 rettv->vval.v_string =
8691 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8692 xp_type, xp_arg);
8693 ex_normal_busy = save_ex_normal_busy;
8694 }
8695 if (inputdialog && rettv->vval.v_string == NULL
8696 && argvars[1].v_type != VAR_UNKNOWN
8697 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 &argvars[2], buf));
8700
8701 vim_free(xp_arg);
8702
8703 /* since the user typed this, no need to wait for return */
8704 need_wait_return = FALSE;
8705 msg_didout = FALSE;
8706 }
8707 cmd_silent = cmd_silent_save;
8708}
8709
8710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 * ":echo expr1 ..." print each argument separated with a space, add a
8712 * newline at the end.
8713 * ":echon expr1 ..." print each argument plain.
8714 */
8715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008716ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008719 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008720 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 char_u *p;
8722 int needclr = TRUE;
8723 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008724 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008725 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008726 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 if (eap->skip)
8729 ++emsg_skip;
8730 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8731 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008732 /* If eval1() causes an error message the text from the command may
8733 * still need to be cleared. E.g., "echo 22,44". */
8734 need_clr_eos = needclr;
8735
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
8739 /*
8740 * Report the invalid expression unless the expression evaluation
8741 * has been cancelled due to an aborting error, an interrupt, or an
8742 * exception.
8743 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008744 if (!aborting() && did_emsg == did_emsg_before
8745 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008746 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008747 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 break;
8749 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008750 need_clr_eos = FALSE;
8751
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (!eap->skip)
8753 {
8754 if (atstart)
8755 {
8756 atstart = FALSE;
8757 /* Call msg_start() after eval1(), evaluating the expression
8758 * may cause a message to appear. */
8759 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008760 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008761 /* Mark the saved text as finishing the line, so that what
8762 * follows is displayed on a new line when scrolling back
8763 * at the more prompt. */
8764 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 }
8768 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008769 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008770 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008771 if (p != NULL)
8772 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008774 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008776 if (*p != TAB && needclr)
8777 {
8778 /* remove any text still there from the command */
8779 msg_clr_eos();
8780 needclr = FALSE;
8781 }
8782 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 }
8784 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008786 if (has_mbyte)
8787 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008788 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008789
8790 (void)msg_outtrans_len_attr(p, i, echo_attr);
8791 p += i - 1;
8792 }
8793 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008794 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008797 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 arg = skipwhite(arg);
8801 }
8802 eap->nextcmd = check_nextcmd(arg);
8803
8804 if (eap->skip)
8805 --emsg_skip;
8806 else
8807 {
8808 /* remove text that may still be there from the command */
8809 if (needclr)
8810 msg_clr_eos();
8811 if (eap->cmdidx == CMD_echo)
8812 msg_end();
8813 }
8814}
8815
8816/*
8817 * ":echohl {name}".
8818 */
8819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008820ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008822 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
8825/*
8826 * ":execute expr1 ..." execute the result of an expression.
8827 * ":echomsg expr1 ..." Print a message
8828 * ":echoerr expr1 ..." Print an error
8829 * Each gets spaces around each argument and a newline at the end for
8830 * echo commands
8831 */
8832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008833ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 int ret = OK;
8838 char_u *p;
8839 garray_T ga;
8840 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008841 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
8843 ga_init2(&ga, 1, 80);
8844
8845 if (eap->skip)
8846 ++emsg_skip;
8847 while (*arg != NUL && *arg != '|' && *arg != '\n')
8848 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008849 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8850 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 if (!eap->skip)
8854 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008855 char_u buf[NUMBUFLEN];
8856
8857 if (eap->cmdidx == CMD_execute)
8858 p = tv_get_string_buf(&rettv, buf);
8859 else
8860 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 len = (int)STRLEN(p);
8862 if (ga_grow(&ga, len + 2) == FAIL)
8863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 ret = FAIL;
8866 break;
8867 }
8868 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 ga.ga_len += len;
8872 }
8873
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 arg = skipwhite(arg);
8876 }
8877
8878 if (ret != FAIL && ga.ga_data != NULL)
8879 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008880 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8881 {
8882 /* Mark the already saved text as finishing the line, so that what
8883 * follows is displayed on a new line when scrolling back at the
8884 * more prompt. */
8885 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008886 }
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008889 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008890 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008891 out_flush();
8892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 else if (eap->cmdidx == CMD_echoerr)
8894 {
8895 /* We don't want to abort following commands, restore did_emsg. */
8896 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008897 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 if (!force_abort)
8899 did_emsg = save_did_emsg;
8900 }
8901 else if (eap->cmdidx == CMD_execute)
8902 do_cmdline((char_u *)ga.ga_data,
8903 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8904 }
8905
8906 ga_clear(&ga);
8907
8908 if (eap->skip)
8909 --emsg_skip;
8910
8911 eap->nextcmd = check_nextcmd(arg);
8912}
8913
8914/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 * Find window specified by "vp" in tabpage "tp".
8916 */
8917 win_T *
8918find_win_by_nr(
8919 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008920 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 if (nr < 0)
8926 return NULL;
8927 if (nr == 0)
8928 return curwin;
8929
Bram Moolenaar29323592016-07-24 22:04:11 +02008930 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8931 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008932 if (nr >= LOWEST_WIN_ID)
8933 {
8934 if (wp->w_id == nr)
8935 return wp;
8936 }
8937 else if (--nr <= 0)
8938 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008941 {
8942#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008943 // check tab-local popup windows
8944 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008945 if (wp->w_id == nr)
8946 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008947 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008948 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8949 if (wp->w_id == nr)
8950 return wp;
8951#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008953 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955}
8956
8957/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008958 * Find a window: When using a Window ID in any tab page, when using a number
8959 * in the current tab page.
8960 */
8961 win_T *
8962find_win_by_nr_or_id(typval_T *vp)
8963{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008964 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008965
8966 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008967 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008968 return find_win_by_nr(vp, NULL);
8969}
8970
8971/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008973 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 */
8975 win_T *
8976find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008977 typval_T *wvp, // VAR_UNKNOWN for current window
8978 typval_T *tvp, // VAR_UNKNOWN for current tab page
8979 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980{
8981 win_T *wp = NULL;
8982 tabpage_T *tp = NULL;
8983 long n;
8984
8985 if (wvp->v_type != VAR_UNKNOWN)
8986 {
8987 if (tvp->v_type != VAR_UNKNOWN)
8988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008989 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 if (n >= 0)
8991 tp = find_tabpage(n);
8992 }
8993 else
8994 tp = curtab;
8995
8996 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008997 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008999 if (wp == NULL && wvp->v_type == VAR_NUMBER
9000 && wvp->vval.v_number != -1)
9001 // A window with the specified number is not found
9002 tp = NULL;
9003 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 }
9005 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009006 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009008 tp = curtab;
9009 }
9010
9011 if (ptp != NULL)
9012 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013
9014 return wp;
9015}
9016
9017/*
9018 * getwinvar() and gettabwinvar()
9019 */
9020 void
9021getwinvar(
9022 typval_T *argvars,
9023 typval_T *rettv,
9024 int off) /* 1 for gettabwinvar() */
9025{
9026 win_T *win;
9027 char_u *varname;
9028 dictitem_T *v;
9029 tabpage_T *tp = NULL;
9030 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 win_T *oldcurwin;
9032 tabpage_T *oldtabpage;
9033 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009035 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009036 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 else
9038 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009040 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 ++emsg_off;
9042
9043 rettv->v_type = VAR_STRING;
9044 rettv->vval.v_string = NULL;
9045
9046 if (win != NULL && varname != NULL)
9047 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048 /* Set curwin to be our win, temporarily. Also set the tabpage,
9049 * otherwise the window is not valid. Only do this when needed,
9050 * autocommands get blocked. */
9051 need_switch_win = !(tp == curtab && win == curwin);
9052 if (!need_switch_win
9053 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009055 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009057 if (varname[1] == NUL)
9058 {
9059 /* get all window-local options in a dict */
9060 dict_T *opts = get_winbuf_options(FALSE);
9061
9062 if (opts != NULL)
9063 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009064 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009065 done = TRUE;
9066 }
9067 }
9068 else if (get_option_tv(&varname, rettv, 1) == OK)
9069 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 done = TRUE;
9071 }
9072 else
9073 {
9074 /* Look up the variable. */
9075 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9076 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9077 varname, FALSE);
9078 if (v != NULL)
9079 {
9080 copy_tv(&v->di_tv, rettv);
9081 done = TRUE;
9082 }
9083 }
9084 }
9085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 if (need_switch_win)
9087 /* restore previous notion of curwin */
9088 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 }
9090
9091 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9092 /* use the default return value */
9093 copy_tv(&argvars[off + 2], rettv);
9094
9095 --emsg_off;
9096}
9097
9098/*
9099 * "setwinvar()" and "settabwinvar()" functions
9100 */
9101 void
9102setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9103{
9104 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 win_T *save_curwin;
9106 tabpage_T *save_curtab;
9107 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108 char_u *varname, *winvarname;
9109 typval_T *varp;
9110 char_u nbuf[NUMBUFLEN];
9111 tabpage_T *tp = NULL;
9112
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009113 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 return;
9115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009117 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 else
9119 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009120 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009121 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122 varp = &argvars[off + 2];
9123
9124 if (win != NULL && varname != NULL && varp != NULL)
9125 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 need_switch_win = !(tp == curtab && win == curwin);
9127 if (!need_switch_win
9128 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009129 {
9130 if (*varname == '&')
9131 {
9132 long numval;
9133 char_u *strval;
9134 int error = FALSE;
9135
9136 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009137 numval = (long)tv_get_number_chk(varp, &error);
9138 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 if (!error && strval != NULL)
9140 set_option_value(varname, numval, strval, OPT_LOCAL);
9141 }
9142 else
9143 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009144 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 if (winvarname != NULL)
9146 {
9147 STRCPY(winvarname, "w:");
9148 STRCPY(winvarname + 2, varname);
9149 set_var(winvarname, varp, TRUE);
9150 vim_free(winvarname);
9151 }
9152 }
9153 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 if (need_switch_win)
9155 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156 }
9157}
9158
9159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9161 * "arg" points to the "&" or '+' when called, to "option" when returning.
9162 * Returns NULL when no option name found. Otherwise pointer to the char
9163 * after the option name.
9164 */
9165 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009166find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 char_u *p = *arg;
9169
9170 ++p;
9171 if (*p == 'g' && p[1] == ':')
9172 {
9173 *opt_flags = OPT_GLOBAL;
9174 p += 2;
9175 }
9176 else if (*p == 'l' && p[1] == ':')
9177 {
9178 *opt_flags = OPT_LOCAL;
9179 p += 2;
9180 }
9181 else
9182 *opt_flags = 0;
9183
9184 if (!ASCII_ISALPHA(*p))
9185 return NULL;
9186 *arg = p;
9187
9188 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9189 p += 4; /* termcap option */
9190 else
9191 while (ASCII_ISALPHA(*p))
9192 ++p;
9193 return p;
9194}
9195
9196/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009197 * Return the autoload script name for a function or variable name.
9198 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009200 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009201autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009202{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009203 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009204 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009205
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009206 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009207 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009208 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009209 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009210 STRCPY(scriptname, "autoload/");
9211 STRCAT(scriptname, name);
9212 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9213 STRCAT(scriptname, ".vim");
9214 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9215 *p = '/';
9216 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009217}
9218
9219/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009220 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009221 * Return TRUE if a package was loaded.
9222 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224script_autoload(
9225 char_u *name,
9226 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009227{
9228 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009229 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009230 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009231 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009232
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009233 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009234 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009235 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009236 return FALSE;
9237
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009238 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009239
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009240 /* Find the name in the list of previously loaded package names. Skip
9241 * "autoload/", it's always the same. */
9242 for (i = 0; i < ga_loaded.ga_len; ++i)
9243 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9244 break;
9245 if (!reload && i < ga_loaded.ga_len)
9246 ret = FALSE; /* was loaded already */
9247 else
9248 {
9249 /* Remember the name if it wasn't loaded already. */
9250 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9251 {
9252 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9253 tofree = NULL;
9254 }
9255
9256 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009257 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009258 ret = TRUE;
9259 }
9260
9261 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009262 return ret;
9263}
9264
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9266typedef enum
9267{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009268 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9269 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9270 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271} var_flavour_T;
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009274var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 char_u *p = varname;
9277
9278 if (ASCII_ISUPPER(*p))
9279 {
9280 while (*(++p))
9281 if (ASCII_ISLOWER(*p))
9282 return VAR_FLAVOUR_SESSION;
9283 return VAR_FLAVOUR_VIMINFO;
9284 }
9285 else
9286 return VAR_FLAVOUR_DEFAULT;
9287}
9288#endif
9289
9290#if defined(FEAT_VIMINFO) || defined(PROTO)
9291/*
9292 * Restore global vars that start with a capital from the viminfo file
9293 */
9294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009295read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009298 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009299 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009300 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
9302 if (!writing && (find_viminfo_parameter('!') != NULL))
9303 {
9304 tab = vim_strchr(virp->vir_line + 1, '\t');
9305 if (tab != NULL)
9306 {
9307 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009308 switch (*tab)
9309 {
9310 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009311#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009312 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009313#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009314 case 'D': type = VAR_DICT; break;
9315 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009316 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009317 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320 tab = vim_strchr(tab, '\t');
9321 if (tab != NULL)
9322 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009323 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009324 if (type == VAR_STRING || type == VAR_DICT
9325 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009326 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009328#ifdef FEAT_FLOAT
9329 else if (type == VAR_FLOAT)
9330 (void)string2float(tab + 1, &tv.vval.v_float);
9331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009333 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009334 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009335 {
9336 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9337
9338 if (etv == NULL)
9339 /* Failed to parse back the dict or list, use it as a
9340 * string. */
9341 tv.v_type = VAR_STRING;
9342 else
9343 {
9344 vim_free(tv.vval.v_string);
9345 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009346 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009347 }
9348 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009349 else if (type == VAR_BLOB)
9350 {
9351 blob_T *blob = string2blob(tv.vval.v_string);
9352
9353 if (blob == NULL)
9354 // Failed to parse back the blob, use it as a string.
9355 tv.v_type = VAR_STRING;
9356 else
9357 {
9358 vim_free(tv.vval.v_string);
9359 tv.v_type = VAR_BLOB;
9360 tv.vval.v_blob = blob;
9361 }
9362 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009363
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009364 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009365 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009366 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009367 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009368
9369 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009370 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009371 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9372 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 }
9375 }
9376 }
9377
9378 return viminfo_readline(virp);
9379}
9380
9381/*
9382 * Write global vars that start with a capital to the viminfo file
9383 */
9384 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009385write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 hashitem_T *hi;
9388 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009389 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009390 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009391 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009393 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394
9395 if (find_viminfo_parameter('!') == NULL)
9396 return;
9397
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009398 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009399
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009400 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009401 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009403 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009405 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 this_var = HI2DI(hi);
9407 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009410 {
9411 case VAR_STRING: s = "STR"; break;
9412 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009413 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009414 case VAR_DICT: s = "DIC"; break;
9415 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009416 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009417 case VAR_SPECIAL: s = "XPL"; break;
9418
9419 case VAR_UNKNOWN:
9420 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009421 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009422 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009423 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009424 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009425 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009426 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009427 if (this_var->di_tv.v_type == VAR_SPECIAL)
9428 {
9429 sprintf((char *)numbuf, "%ld",
9430 (long)this_var->di_tv.vval.v_number);
9431 p = numbuf;
9432 tofree = NULL;
9433 }
9434 else
9435 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009436 if (p != NULL)
9437 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009438 vim_free(tofree);
9439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 }
9441 }
9442}
9443#endif
9444
9445#if defined(FEAT_SESSION) || defined(PROTO)
9446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009447store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 hashitem_T *hi;
9450 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009451 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 char_u *p, *t;
9453
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009454 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009455 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009457 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009459 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 this_var = HI2DI(hi);
9461 if ((this_var->di_tv.v_type == VAR_NUMBER
9462 || this_var->di_tv.v_type == VAR_STRING)
9463 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009464 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009465 /* Escape special characters with a backslash. Turn a LF and
9466 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009467 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009468 (char_u *)"\\\"\n\r");
9469 if (p == NULL) /* out of memory */
9470 break;
9471 for (t = p; *t != NUL; ++t)
9472 if (*t == '\n')
9473 *t = 'n';
9474 else if (*t == '\r')
9475 *t = 'r';
9476 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 this_var->di_key,
9478 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9479 : ' ',
9480 p,
9481 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9482 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009483 || put_eol(fd) == FAIL)
9484 {
9485 vim_free(p);
9486 return FAIL;
9487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 vim_free(p);
9489 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009490#ifdef FEAT_FLOAT
9491 else if (this_var->di_tv.v_type == VAR_FLOAT
9492 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9493 {
9494 float_T f = this_var->di_tv.vval.v_float;
9495 int sign = ' ';
9496
9497 if (f < 0)
9498 {
9499 f = -f;
9500 sign = '-';
9501 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009502 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009503 this_var->di_key, sign, f) < 0)
9504 || put_eol(fd) == FAIL)
9505 return FAIL;
9506 }
9507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 }
9509 }
9510 return OK;
9511}
9512#endif
9513
Bram Moolenaar661b1822005-07-28 22:36:45 +00009514/*
9515 * Display script name where an item was last set.
9516 * Should only be invoked when 'verbose' is non-zero.
9517 */
9518 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009519last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009520{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009521 char_u *p;
9522
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009523 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009524 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009525 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009526 if (p != NULL)
9527 {
9528 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009529 msg_puts(_("\n\tLast set from "));
9530 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009531 if (script_ctx.sc_lnum > 0)
9532 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009533 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009534 msg_outnum((long)script_ctx.sc_lnum);
9535 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009536 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009537 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009538 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009539 }
9540}
9541
Bram Moolenaar61be3762019-03-19 23:04:17 +01009542/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009543 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9544 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009545 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009547reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009548{
9549 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9550 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009551 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9552 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009553 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009554 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009555}
9556
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557/*
9558 * Prepare "gap" for an assert error and add the sourcing position.
9559 */
9560 void
9561prepare_assert_error(garray_T *gap)
9562{
9563 char buf[NUMBUFLEN];
9564
9565 ga_init2(gap, 1, 100);
9566 if (sourcing_name != NULL)
9567 {
9568 ga_concat(gap, sourcing_name);
9569 if (sourcing_lnum > 0)
9570 ga_concat(gap, (char_u *)" ");
9571 }
9572 if (sourcing_lnum > 0)
9573 {
9574 sprintf(buf, "line %ld", (long)sourcing_lnum);
9575 ga_concat(gap, (char_u *)buf);
9576 }
9577 if (sourcing_name != NULL || sourcing_lnum > 0)
9578 ga_concat(gap, (char_u *)": ");
9579}
9580
9581/*
9582 * Add an assert error to v:errors.
9583 */
9584 void
9585assert_error(garray_T *gap)
9586{
9587 struct vimvar *vp = &vimvars[VV_ERRORS];
9588
9589 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9590 /* Make sure v:errors is a list. */
9591 set_vim_var_list(VV_ERRORS, list_alloc());
9592 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9593}
9594
Bram Moolenaar65a54642018-04-28 16:56:53 +02009595 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596assert_equal_common(typval_T *argvars, assert_type_T atype)
9597{
9598 garray_T ga;
9599
9600 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9601 != (atype == ASSERT_EQUAL))
9602 {
9603 prepare_assert_error(&ga);
9604 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9605 atype);
9606 assert_error(&ga);
9607 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009608 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009610 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009611}
9612
Bram Moolenaar65a54642018-04-28 16:56:53 +02009613 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009614assert_equalfile(typval_T *argvars)
9615{
9616 char_u buf1[NUMBUFLEN];
9617 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009618 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9619 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009620 garray_T ga;
9621 FILE *fd1;
9622 FILE *fd2;
9623
9624 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009625 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009626
9627 IObuff[0] = NUL;
9628 fd1 = mch_fopen((char *)fname1, READBIN);
9629 if (fd1 == NULL)
9630 {
9631 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9632 }
9633 else
9634 {
9635 fd2 = mch_fopen((char *)fname2, READBIN);
9636 if (fd2 == NULL)
9637 {
9638 fclose(fd1);
9639 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9640 }
9641 else
9642 {
9643 int c1, c2;
9644 long count = 0;
9645
9646 for (;;)
9647 {
9648 c1 = fgetc(fd1);
9649 c2 = fgetc(fd2);
9650 if (c1 == EOF)
9651 {
9652 if (c2 != EOF)
9653 STRCPY(IObuff, "first file is shorter");
9654 break;
9655 }
9656 else if (c2 == EOF)
9657 {
9658 STRCPY(IObuff, "second file is shorter");
9659 break;
9660 }
9661 else if (c1 != c2)
9662 {
9663 vim_snprintf((char *)IObuff, IOSIZE,
9664 "difference at byte %ld", count);
9665 break;
9666 }
9667 ++count;
9668 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009669 fclose(fd1);
9670 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009671 }
9672 }
9673 if (IObuff[0] != NUL)
9674 {
9675 prepare_assert_error(&ga);
9676 ga_concat(&ga, IObuff);
9677 assert_error(&ga);
9678 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009679 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009680 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009681 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009682}
9683
Bram Moolenaar65a54642018-04-28 16:56:53 +02009684 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685assert_match_common(typval_T *argvars, assert_type_T atype)
9686{
9687 garray_T ga;
9688 char_u buf1[NUMBUFLEN];
9689 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009690 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9691 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692
9693 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009694 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9696 {
9697 prepare_assert_error(&ga);
9698 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9699 atype);
9700 assert_error(&ga);
9701 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009702 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009704 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705}
9706
Bram Moolenaar65a54642018-04-28 16:56:53 +02009707 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009708assert_inrange(typval_T *argvars)
9709{
9710 garray_T ga;
9711 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009712 char_u *tofree;
9713 char msg[200];
9714 char_u numbuf[NUMBUFLEN];
9715
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009716#ifdef FEAT_FLOAT
9717 if (argvars[0].v_type == VAR_FLOAT
9718 || argvars[1].v_type == VAR_FLOAT
9719 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009720 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009721 float_T flower = tv_get_float(&argvars[0]);
9722 float_T fupper = tv_get_float(&argvars[1]);
9723 float_T factual = tv_get_float(&argvars[2]);
9724
9725 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009726 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009727 prepare_assert_error(&ga);
9728 if (argvars[3].v_type != VAR_UNKNOWN)
9729 {
9730 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9731 vim_free(tofree);
9732 }
9733 else
9734 {
9735 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9736 flower, fupper, factual);
9737 ga_concat(&ga, (char_u *)msg);
9738 }
9739 assert_error(&ga);
9740 ga_clear(&ga);
9741 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009742 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009743 }
9744 else
9745#endif
9746 {
9747 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9748 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9749 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9750
9751 if (error)
9752 return 0;
9753 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009754 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009755 prepare_assert_error(&ga);
9756 if (argvars[3].v_type != VAR_UNKNOWN)
9757 {
9758 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9759 vim_free(tofree);
9760 }
9761 else
9762 {
9763 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009764 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009765 ga_concat(&ga, (char_u *)msg);
9766 }
9767 assert_error(&ga);
9768 ga_clear(&ga);
9769 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009770 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009771 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009772 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009773}
9774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775/*
9776 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009777 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009779 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780assert_bool(typval_T *argvars, int isTrue)
9781{
9782 int error = FALSE;
9783 garray_T ga;
9784
9785 if (argvars[0].v_type == VAR_SPECIAL
9786 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009787 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009789 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 || error)
9791 {
9792 prepare_assert_error(&ga);
9793 fill_assert_error(&ga, &argvars[1],
9794 (char_u *)(isTrue ? "True" : "False"),
9795 NULL, &argvars[0], ASSERT_OTHER);
9796 assert_error(&ga);
9797 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009798 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009800 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801}
9802
Bram Moolenaar65a54642018-04-28 16:56:53 +02009803 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009804assert_report(typval_T *argvars)
9805{
9806 garray_T ga;
9807
9808 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009809 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009810 assert_error(&ga);
9811 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009812 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009813}
9814
Bram Moolenaar65a54642018-04-28 16:56:53 +02009815 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816assert_exception(typval_T *argvars)
9817{
9818 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009819 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820
9821 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9822 {
9823 prepare_assert_error(&ga);
9824 ga_concat(&ga, (char_u *)"v:exception is not set");
9825 assert_error(&ga);
9826 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009827 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 }
9829 else if (error != NULL
9830 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9831 {
9832 prepare_assert_error(&ga);
9833 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9834 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9835 assert_error(&ga);
9836 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009837 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009838 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009839 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840}
9841
Bram Moolenaar65a54642018-04-28 16:56:53 +02009842 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009843assert_beeps(typval_T *argvars)
9844{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009845 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009846 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009847 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009848
9849 called_vim_beep = FALSE;
9850 suppress_errthrow = TRUE;
9851 emsg_silent = FALSE;
9852 do_cmdline_cmd(cmd);
9853 if (!called_vim_beep)
9854 {
9855 prepare_assert_error(&ga);
9856 ga_concat(&ga, (char_u *)"command did not beep: ");
9857 ga_concat(&ga, cmd);
9858 assert_error(&ga);
9859 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009860 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009861 }
9862
9863 suppress_errthrow = FALSE;
9864 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009865 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009866}
9867
Bram Moolenaar25190db2019-05-04 15:05:28 +02009868 static void
9869assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9870{
9871 char_u *tofree;
9872 char_u numbuf[NUMBUFLEN];
9873
9874 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9875 {
9876 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9877 vim_free(tofree);
9878 }
9879 else
9880 ga_concat(gap, cmd);
9881}
9882
Bram Moolenaar65a54642018-04-28 16:56:53 +02009883 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884assert_fails(typval_T *argvars)
9885{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009886 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009888 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889
9890 called_emsg = FALSE;
9891 suppress_errthrow = TRUE;
9892 emsg_silent = TRUE;
9893 do_cmdline_cmd(cmd);
9894 if (!called_emsg)
9895 {
9896 prepare_assert_error(&ga);
9897 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009898 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009899 assert_error(&ga);
9900 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009901 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 }
9903 else if (argvars[1].v_type != VAR_UNKNOWN)
9904 {
9905 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009906 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907
9908 if (error == NULL
9909 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9910 {
9911 prepare_assert_error(&ga);
9912 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9913 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009914 ga_concat(&ga, (char_u *)": ");
9915 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 assert_error(&ga);
9917 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009918 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919 }
9920 }
9921
9922 called_emsg = FALSE;
9923 suppress_errthrow = FALSE;
9924 emsg_silent = FALSE;
9925 emsg_on_display = FALSE;
9926 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009927 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928}
9929
9930/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009931 * Append "p[clen]" to "gap", escaping unprintable characters.
9932 * Changes NL to \n, CR to \r, etc.
9933 */
9934 static void
9935ga_concat_esc(garray_T *gap, char_u *p, int clen)
9936{
9937 char_u buf[NUMBUFLEN];
9938
9939 if (clen > 1)
9940 {
9941 mch_memmove(buf, p, clen);
9942 buf[clen] = NUL;
9943 ga_concat(gap, buf);
9944 }
9945 else switch (*p)
9946 {
9947 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9948 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9949 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9950 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9951 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9952 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9953 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9954 default:
9955 if (*p < ' ')
9956 {
9957 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9958 ga_concat(gap, buf);
9959 }
9960 else
9961 ga_append(gap, *p);
9962 break;
9963 }
9964}
9965
9966/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009967 * Append "str" to "gap", escaping unprintable characters.
9968 * Changes NL to \n, CR to \r, etc.
9969 */
9970 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009971ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972{
9973 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009974 char_u *s;
9975 int c;
9976 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009977 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009978 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009979
9980 if (str == NULL)
9981 {
9982 ga_concat(gap, (char_u *)"NULL");
9983 return;
9984 }
9985
9986 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009987 {
9988 same_len = 1;
9989 s = p;
9990 c = mb_ptr2char_adv(&s);
9991 clen = s - p;
9992 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009993 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009994 ++same_len;
9995 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009997 if (same_len > 20)
9998 {
9999 ga_concat(gap, (char_u *)"\\[");
10000 ga_concat_esc(gap, p, clen);
10001 ga_concat(gap, (char_u *)" occurs ");
10002 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
10003 ga_concat(gap, buf);
10004 ga_concat(gap, (char_u *)" times]");
10005 p = s - 1;
10006 }
10007 else
10008 ga_concat_esc(gap, p, clen);
10009 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010}
10011
10012/*
10013 * Fill "gap" with information about an assert error.
10014 */
10015 void
10016fill_assert_error(
10017 garray_T *gap,
10018 typval_T *opt_msg_tv,
10019 char_u *exp_str,
10020 typval_T *exp_tv,
10021 typval_T *got_tv,
10022 assert_type_T atype)
10023{
10024 char_u numbuf[NUMBUFLEN];
10025 char_u *tofree;
10026
10027 if (opt_msg_tv->v_type != VAR_UNKNOWN)
10028 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010029 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
10030 vim_free(tofree);
10031 ga_concat(gap, (char_u *)": ");
10032 }
10033
10034 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
10035 ga_concat(gap, (char_u *)"Pattern ");
10036 else if (atype == ASSERT_NOTEQUAL)
10037 ga_concat(gap, (char_u *)"Expected not equal to ");
10038 else
10039 ga_concat(gap, (char_u *)"Expected ");
10040 if (exp_str == NULL)
10041 {
Bram Moolenaar86576712019-01-25 20:48:33 +010010042 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 vim_free(tofree);
10044 }
10045 else
Bram Moolenaar86576712019-01-25 20:48:33 +010010046 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010047 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010048 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010049 if (atype == ASSERT_MATCH)
10050 ga_concat(gap, (char_u *)" does not match ");
10051 else if (atype == ASSERT_NOTMATCH)
10052 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010054 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +010010055 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010056 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010057 }
10058}
10059
Bram Moolenaar31988702018-02-13 12:57:42 +010010060/*
10061 * Compare "typ1" and "typ2". Put the result in "typ1".
10062 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010063 int
10064typval_compare(
10065 typval_T *typ1, /* first operand */
10066 typval_T *typ2, /* second operand */
10067 exptype_T type, /* operator */
10068 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +010010069 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010070{
10071 int i;
10072 varnumber_T n1, n2;
10073 char_u *s1, *s2;
10074 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
10075
Bram Moolenaar31988702018-02-13 12:57:42 +010010076 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010077 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010078 /* For "is" a different type always means FALSE, for "notis"
10079 * it means TRUE. */
10080 n1 = (type == TYPE_NEQUAL);
10081 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010082 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
10083 {
10084 if (type_is)
10085 {
10086 n1 = (typ1->v_type == typ2->v_type
10087 && typ1->vval.v_blob == typ2->vval.v_blob);
10088 if (type == TYPE_NEQUAL)
10089 n1 = !n1;
10090 }
10091 else if (typ1->v_type != typ2->v_type
10092 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10093 {
10094 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010095 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010096 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010097 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010098 clear_tv(typ1);
10099 return FAIL;
10100 }
10101 else
10102 {
10103 // Compare two Blobs for being equal or unequal.
10104 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
10105 if (type == TYPE_NEQUAL)
10106 n1 = !n1;
10107 }
10108 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010109 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
10110 {
10111 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010112 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010113 n1 = (typ1->v_type == typ2->v_type
10114 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010115 if (type == TYPE_NEQUAL)
10116 n1 = !n1;
10117 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010118 else if (typ1->v_type != typ2->v_type
10119 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010120 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010121 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010122 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010123 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010124 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010125 clear_tv(typ1);
10126 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010127 }
10128 else
10129 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010130 /* Compare two Lists for being equal or unequal. */
10131 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10132 ic, FALSE);
10133 if (type == TYPE_NEQUAL)
10134 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010135 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010136 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010137
10138 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10139 {
10140 if (type_is)
10141 {
10142 n1 = (typ1->v_type == typ2->v_type
10143 && typ1->vval.v_dict == typ2->vval.v_dict);
10144 if (type == TYPE_NEQUAL)
10145 n1 = !n1;
10146 }
10147 else if (typ1->v_type != typ2->v_type
10148 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10149 {
10150 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010151 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010152 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010153 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010154 clear_tv(typ1);
10155 return FAIL;
10156 }
10157 else
10158 {
10159 /* Compare two Dictionaries for being equal or unequal. */
10160 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10161 ic, FALSE);
10162 if (type == TYPE_NEQUAL)
10163 n1 = !n1;
10164 }
10165 }
10166
10167 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10168 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10169 {
10170 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010172 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010173 clear_tv(typ1);
10174 return FAIL;
10175 }
10176 if ((typ1->v_type == VAR_PARTIAL
10177 && typ1->vval.v_partial == NULL)
10178 || (typ2->v_type == VAR_PARTIAL
10179 && typ2->vval.v_partial == NULL))
10180 /* when a partial is NULL assume not equal */
10181 n1 = FALSE;
10182 else if (type_is)
10183 {
10184 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10185 /* strings are considered the same if their value is
10186 * the same */
10187 n1 = tv_equal(typ1, typ2, ic, FALSE);
10188 else if (typ1->v_type == VAR_PARTIAL
10189 && typ2->v_type == VAR_PARTIAL)
10190 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10191 else
10192 n1 = FALSE;
10193 }
10194 else
10195 n1 = tv_equal(typ1, typ2, ic, FALSE);
10196 if (type == TYPE_NEQUAL)
10197 n1 = !n1;
10198 }
10199
10200#ifdef FEAT_FLOAT
10201 /*
10202 * If one of the two variables is a float, compare as a float.
10203 * When using "=~" or "!~", always compare as string.
10204 */
10205 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10206 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10207 {
10208 float_T f1, f2;
10209
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010210 f1 = tv_get_float(typ1);
10211 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010212 n1 = FALSE;
10213 switch (type)
10214 {
10215 case TYPE_EQUAL: n1 = (f1 == f2); break;
10216 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10217 case TYPE_GREATER: n1 = (f1 > f2); break;
10218 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10219 case TYPE_SMALLER: n1 = (f1 < f2); break;
10220 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10221 case TYPE_UNKNOWN:
10222 case TYPE_MATCH:
10223 case TYPE_NOMATCH: break; /* avoid gcc warning */
10224 }
10225 }
10226#endif
10227
10228 /*
10229 * If one of the two variables is a number, compare as a number.
10230 * When using "=~" or "!~", always compare as string.
10231 */
10232 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10233 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010235 n1 = tv_get_number(typ1);
10236 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010237 switch (type)
10238 {
10239 case TYPE_EQUAL: n1 = (n1 == n2); break;
10240 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10241 case TYPE_GREATER: n1 = (n1 > n2); break;
10242 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10243 case TYPE_SMALLER: n1 = (n1 < n2); break;
10244 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10245 case TYPE_UNKNOWN:
10246 case TYPE_MATCH:
10247 case TYPE_NOMATCH: break; /* avoid gcc warning */
10248 }
10249 }
10250 else
10251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010252 s1 = tv_get_string_buf(typ1, buf1);
10253 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010254 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10255 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10256 else
10257 i = 0;
10258 n1 = FALSE;
10259 switch (type)
10260 {
10261 case TYPE_EQUAL: n1 = (i == 0); break;
10262 case TYPE_NEQUAL: n1 = (i != 0); break;
10263 case TYPE_GREATER: n1 = (i > 0); break;
10264 case TYPE_GEQUAL: n1 = (i >= 0); break;
10265 case TYPE_SMALLER: n1 = (i < 0); break;
10266 case TYPE_SEQUAL: n1 = (i <= 0); break;
10267
10268 case TYPE_MATCH:
10269 case TYPE_NOMATCH:
10270 n1 = pattern_match(s2, s1, ic);
10271 if (type == TYPE_NOMATCH)
10272 n1 = !n1;
10273 break;
10274
10275 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10276 }
10277 }
10278 clear_tv(typ1);
10279 typ1->v_type = VAR_NUMBER;
10280 typ1->vval.v_number = n1;
10281
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010282 return OK;
10283}
10284
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010285 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010286typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010287{
10288 char_u *tofree;
10289 char_u numbuf[NUMBUFLEN];
10290 char_u *ret = NULL;
10291
10292 if (arg == NULL)
10293 return vim_strsave((char_u *)"(does not exist)");
10294 ret = tv2string(arg, &tofree, numbuf, 0);
10295 /* Make a copy if we have a value but it's not in allocated memory. */
10296 if (ret != NULL && tofree == NULL)
10297 ret = vim_strsave(ret);
10298 return ret;
10299}
10300
10301 int
10302var_exists(char_u *var)
10303{
10304 char_u *name;
10305 char_u *tofree;
10306 typval_T tv;
10307 int len = 0;
10308 int n = FALSE;
10309
10310 /* get_name_len() takes care of expanding curly braces */
10311 name = var;
10312 len = get_name_len(&var, &tofree, TRUE, FALSE);
10313 if (len > 0)
10314 {
10315 if (tofree != NULL)
10316 name = tofree;
10317 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10318 if (n)
10319 {
10320 /* handle d.key, l[idx], f(expr) */
10321 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10322 if (n)
10323 clear_tv(&tv);
10324 }
10325 }
10326 if (*var != NUL)
10327 n = FALSE;
10328
10329 vim_free(tofree);
10330 return n;
10331}
10332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333#endif /* FEAT_EVAL */
10334
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010336#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337
Bram Moolenaar4f974752019-02-17 17:44:42 +010010338#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339/*
10340 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342
10343/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010344 * Get the short path (8.3) for the filename in "fnamep".
10345 * Only works for a valid file name.
10346 * When the path gets longer "fnamep" is changed and the allocated buffer
10347 * is put in "bufp".
10348 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10349 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 */
10351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010354 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 char_u *newbuf;
10356
10357 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010358 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 if (l > len - 1)
10360 {
10361 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010362 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 newbuf = vim_strnsave(*fnamep, l);
10364 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010365 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366
10367 vim_free(*bufp);
10368 *fnamep = *bufp = newbuf;
10369
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010370 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010371 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373
10374 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010375 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
10377
10378/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010379 * Get the short path (8.3) for the filename in "fname". The converted
10380 * path is returned in "bufp".
10381 *
10382 * Some of the directories specified in "fname" may not exist. This function
10383 * will shorten the existing directories at the beginning of the path and then
10384 * append the remaining non-existing path.
10385 *
10386 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010387 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010388 * bufp - Pointer to an allocated buffer for the filename.
10389 * fnamelen - Length of the filename pointed to by fname
10390 *
10391 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 */
10393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010394shortpath_for_invalid_fname(
10395 char_u **fname,
10396 char_u **bufp,
10397 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010399 char_u *short_fname, *save_fname, *pbuf_unused;
10400 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010402 int old_len, len;
10403 int new_len, sfx_len;
10404 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405
10406 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010407 old_len = *fnamelen;
10408 save_fname = vim_strnsave(*fname, old_len);
10409 pbuf_unused = NULL;
10410 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010412 endp = save_fname + old_len - 1; /* Find the end of the copy */
10413 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010415 /*
10416 * Try shortening the supplied path till it succeeds by removing one
10417 * directory at a time from the tail of the path.
10418 */
10419 len = 0;
10420 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010422 /* go back one path-separator */
10423 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10424 --endp;
10425 if (endp <= save_fname)
10426 break; /* processed the complete path */
10427
10428 /*
10429 * Replace the path separator with a NUL and try to shorten the
10430 * resulting path.
10431 */
10432 ch = *endp;
10433 *endp = 0;
10434 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010435 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010436 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10437 {
10438 retval = FAIL;
10439 goto theend;
10440 }
10441 *endp = ch; /* preserve the string */
10442
10443 if (len > 0)
10444 break; /* successfully shortened the path */
10445
10446 /* failed to shorten the path. Skip the path separator */
10447 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 }
10449
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010450 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010452 /*
10453 * Succeeded in shortening the path. Now concatenate the shortened
10454 * path with the remaining path at the tail.
10455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010457 /* Compute the length of the new path. */
10458 sfx_len = (int)(save_endp - endp) + 1;
10459 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010461 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010463 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010465 /* There is not enough space in the currently allocated string,
10466 * copy it to a buffer big enough. */
10467 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010469 {
10470 retval = FAIL;
10471 goto theend;
10472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 }
10474 else
10475 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010476 /* Transfer short_fname to the main buffer (it's big enough),
10477 * unless get_short_pathname() did its work in-place. */
10478 *fname = *bufp = save_fname;
10479 if (short_fname != save_fname)
10480 vim_strncpy(save_fname, short_fname, len);
10481 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010483
10484 /* concat the not-shortened part of the path */
10485 vim_strncpy(*fname + len, endp, sfx_len);
10486 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010488
10489theend:
10490 vim_free(pbuf_unused);
10491 vim_free(save_fname);
10492
10493 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494}
10495
10496/*
10497 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010498 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 */
10500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501shortpath_for_partial(
10502 char_u **fnamep,
10503 char_u **bufp,
10504 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506 int sepcount, len, tflen;
10507 char_u *p;
10508 char_u *pbuf, *tfname;
10509 int hasTilde;
10510
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010511 /* Count up the path separators from the RHS.. so we know which part
10512 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010514 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 if (vim_ispathsep(*p))
10516 ++sepcount;
10517
10518 /* Need full path first (use expand_env() to remove a "~/") */
10519 hasTilde = (**fnamep == '~');
10520 if (hasTilde)
10521 pbuf = tfname = expand_env_save(*fnamep);
10522 else
10523 pbuf = tfname = FullName_save(*fnamep, FALSE);
10524
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010525 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010527 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10528 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529
10530 if (len == 0)
10531 {
10532 /* Don't have a valid filename, so shorten the rest of the
10533 * path if we can. This CAN give us invalid 8.3 filenames, but
10534 * there's not a lot of point in guessing what it might be.
10535 */
10536 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010537 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10538 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540
10541 /* Count the paths backward to find the beginning of the desired string. */
10542 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010543 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010544 if (has_mbyte)
10545 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (vim_ispathsep(*p))
10547 {
10548 if (sepcount == 0 || (hasTilde && sepcount == 1))
10549 break;
10550 else
10551 sepcount --;
10552 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 if (hasTilde)
10555 {
10556 --p;
10557 if (p >= tfname)
10558 *p = '~';
10559 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010560 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 }
10562 else
10563 ++p;
10564
10565 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10566 vim_free(*bufp);
10567 *fnamelen = (int)STRLEN(p);
10568 *bufp = pbuf;
10569 *fnamep = p;
10570
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010571 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010573#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
10575/*
10576 * Adjust a filename, according to a string of modifiers.
10577 * *fnamep must be NUL terminated when called. When returning, the length is
10578 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010579 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 * When there is an error, *fnamep is set to NULL.
10581 */
10582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010583modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010584 char_u *src, // string with modifiers
10585 int tilde_file, // "~" is a file name, not $HOME
10586 int *usedlen, // characters after src that are used
10587 char_u **fnamep, // file name so far
10588 char_u **bufp, // buffer for allocated file name or NULL
10589 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590{
10591 int valid = 0;
10592 char_u *tail;
10593 char_u *s, *p, *pbuf;
10594 char_u dirname[MAXPATHL];
10595 int c;
10596 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010597#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010598 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 int has_shortname = 0;
10600#endif
10601
10602repeat:
10603 /* ":p" - full path/file_name */
10604 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10605 {
10606 has_fullname = 1;
10607
10608 valid |= VALID_PATH;
10609 *usedlen += 2;
10610
10611 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10612 if ((*fnamep)[0] == '~'
10613#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10614 && ((*fnamep)[1] == '/'
10615# ifdef BACKSLASH_IN_FILENAME
10616 || (*fnamep)[1] == '\\'
10617# endif
10618 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010620 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 )
10622 {
10623 *fnamep = expand_env_save(*fnamep);
10624 vim_free(*bufp); /* free any allocated file name */
10625 *bufp = *fnamep;
10626 if (*fnamep == NULL)
10627 return -1;
10628 }
10629
10630 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010631 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 {
10633 if (vim_ispathsep(*p)
10634 && p[1] == '.'
10635 && (p[2] == NUL
10636 || vim_ispathsep(p[2])
10637 || (p[2] == '.'
10638 && (p[3] == NUL || vim_ispathsep(p[3])))))
10639 break;
10640 }
10641
10642 /* FullName_save() is slow, don't use it when not needed. */
10643 if (*p != NUL || !vim_isAbsName(*fnamep))
10644 {
10645 *fnamep = FullName_save(*fnamep, *p != NUL);
10646 vim_free(*bufp); /* free any allocated file name */
10647 *bufp = *fnamep;
10648 if (*fnamep == NULL)
10649 return -1;
10650 }
10651
Bram Moolenaar4f974752019-02-17 17:44:42 +010010652#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010653# if _WIN32_WINNT >= 0x0500
10654 if (vim_strchr(*fnamep, '~') != NULL)
10655 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010656 // Expand 8.3 filename to full path. Needed to make sure the same
10657 // file does not have two different names.
10658 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10659 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10660 WCHAR buf[_MAX_PATH];
10661
10662 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010663 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010664 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010665 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010666 char_u *p = utf16_to_enc(buf, NULL);
10667
10668 if (p != NULL)
10669 {
10670 vim_free(*bufp); // free any allocated file name
10671 *bufp = *fnamep = p;
10672 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010673 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010674 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010675 }
10676 }
10677# endif
10678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 /* Append a path separator to a directory. */
10680 if (mch_isdir(*fnamep))
10681 {
10682 /* Make room for one or two extra characters. */
10683 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10684 vim_free(*bufp); /* free any allocated file name */
10685 *bufp = *fnamep;
10686 if (*fnamep == NULL)
10687 return -1;
10688 add_pathsep(*fnamep);
10689 }
10690 }
10691
10692 /* ":." - path relative to the current directory */
10693 /* ":~" - path relative to the home directory */
10694 /* ":8" - shortname path - postponed till after */
10695 while (src[*usedlen] == ':'
10696 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10697 {
10698 *usedlen += 2;
10699 if (c == '8')
10700 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010701#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 has_shortname = 1; /* Postpone this. */
10703#endif
10704 continue;
10705 }
10706 pbuf = NULL;
10707 /* Need full path first (use expand_env() to remove a "~/") */
10708 if (!has_fullname)
10709 {
10710 if (c == '.' && **fnamep == '~')
10711 p = pbuf = expand_env_save(*fnamep);
10712 else
10713 p = pbuf = FullName_save(*fnamep, FALSE);
10714 }
10715 else
10716 p = *fnamep;
10717
10718 has_fullname = 0;
10719
10720 if (p != NULL)
10721 {
10722 if (c == '.')
10723 {
10724 mch_dirname(dirname, MAXPATHL);
10725 s = shorten_fname(p, dirname);
10726 if (s != NULL)
10727 {
10728 *fnamep = s;
10729 if (pbuf != NULL)
10730 {
10731 vim_free(*bufp); /* free any allocated file name */
10732 *bufp = pbuf;
10733 pbuf = NULL;
10734 }
10735 }
10736 }
10737 else
10738 {
10739 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10740 /* Only replace it when it starts with '~' */
10741 if (*dirname == '~')
10742 {
10743 s = vim_strsave(dirname);
10744 if (s != NULL)
10745 {
10746 *fnamep = s;
10747 vim_free(*bufp);
10748 *bufp = s;
10749 }
10750 }
10751 }
10752 vim_free(pbuf);
10753 }
10754 }
10755
10756 tail = gettail(*fnamep);
10757 *fnamelen = (int)STRLEN(*fnamep);
10758
10759 /* ":h" - head, remove "/file_name", can be repeated */
10760 /* Don't remove the first "/" or "c:\" */
10761 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10762 {
10763 valid |= VALID_HEAD;
10764 *usedlen += 2;
10765 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010766 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010767 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 *fnamelen = (int)(tail - *fnamep);
10769#ifdef VMS
10770 if (*fnamelen > 0)
10771 *fnamelen += 1; /* the path separator is part of the path */
10772#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010773 if (*fnamelen == 0)
10774 {
10775 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10776 p = vim_strsave((char_u *)".");
10777 if (p == NULL)
10778 return -1;
10779 vim_free(*bufp);
10780 *bufp = *fnamep = tail = p;
10781 *fnamelen = 1;
10782 }
10783 else
10784 {
10785 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010786 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 }
10789
10790 /* ":8" - shortname */
10791 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10792 {
10793 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010794#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 has_shortname = 1;
10796#endif
10797 }
10798
Bram Moolenaar4f974752019-02-17 17:44:42 +010010799#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010800 /*
10801 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 */
10803 if (has_shortname)
10804 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010805 /* Copy the string if it is shortened by :h and when it wasn't copied
10806 * yet, because we are going to change it in place. Avoids changing
10807 * the buffer name for "%:8". */
10808 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 {
10810 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010811 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 return -1;
10813 vim_free(*bufp);
10814 *bufp = *fnamep = p;
10815 }
10816
10817 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010818 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 if (!has_fullname && !vim_isAbsName(*fnamep))
10820 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010821 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 return -1;
10823 }
10824 else
10825 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010826 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827
Bram Moolenaardc935552011-08-17 15:23:23 +020010828 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010830 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 return -1;
10832
10833 if (l == 0)
10834 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010835 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010837 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 return -1;
10839 }
10840 *fnamelen = l;
10841 }
10842 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010843#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
10845 /* ":t" - tail, just the basename */
10846 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10847 {
10848 *usedlen += 2;
10849 *fnamelen -= (int)(tail - *fnamep);
10850 *fnamep = tail;
10851 }
10852
10853 /* ":e" - extension, can be repeated */
10854 /* ":r" - root, without extension, can be repeated */
10855 while (src[*usedlen] == ':'
10856 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10857 {
10858 /* find a '.' in the tail:
10859 * - for second :e: before the current fname
10860 * - otherwise: The last '.'
10861 */
10862 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10863 s = *fnamep - 2;
10864 else
10865 s = *fnamep + *fnamelen - 1;
10866 for ( ; s > tail; --s)
10867 if (s[0] == '.')
10868 break;
10869 if (src[*usedlen + 1] == 'e') /* :e */
10870 {
10871 if (s > tail)
10872 {
10873 *fnamelen += (int)(*fnamep - (s + 1));
10874 *fnamep = s + 1;
10875#ifdef VMS
10876 /* cut version from the extension */
10877 s = *fnamep + *fnamelen - 1;
10878 for ( ; s > *fnamep; --s)
10879 if (s[0] == ';')
10880 break;
10881 if (s > *fnamep)
10882 *fnamelen = s - *fnamep;
10883#endif
10884 }
10885 else if (*fnamep <= tail)
10886 *fnamelen = 0;
10887 }
10888 else /* :r */
10889 {
10890 if (s > tail) /* remove one extension */
10891 *fnamelen = (int)(s - *fnamep);
10892 }
10893 *usedlen += 2;
10894 }
10895
10896 /* ":s?pat?foo?" - substitute */
10897 /* ":gs?pat?foo?" - global substitute */
10898 if (src[*usedlen] == ':'
10899 && (src[*usedlen + 1] == 's'
10900 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10901 {
10902 char_u *str;
10903 char_u *pat;
10904 char_u *sub;
10905 int sep;
10906 char_u *flags;
10907 int didit = FALSE;
10908
10909 flags = (char_u *)"";
10910 s = src + *usedlen + 2;
10911 if (src[*usedlen + 1] == 'g')
10912 {
10913 flags = (char_u *)"g";
10914 ++s;
10915 }
10916
10917 sep = *s++;
10918 if (sep)
10919 {
10920 /* find end of pattern */
10921 p = vim_strchr(s, sep);
10922 if (p != NULL)
10923 {
10924 pat = vim_strnsave(s, (int)(p - s));
10925 if (pat != NULL)
10926 {
10927 s = p + 1;
10928 /* find end of substitution */
10929 p = vim_strchr(s, sep);
10930 if (p != NULL)
10931 {
10932 sub = vim_strnsave(s, (int)(p - s));
10933 str = vim_strnsave(*fnamep, *fnamelen);
10934 if (sub != NULL && str != NULL)
10935 {
10936 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010937 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 if (s != NULL)
10939 {
10940 *fnamep = s;
10941 *fnamelen = (int)STRLEN(s);
10942 vim_free(*bufp);
10943 *bufp = s;
10944 didit = TRUE;
10945 }
10946 }
10947 vim_free(sub);
10948 vim_free(str);
10949 }
10950 vim_free(pat);
10951 }
10952 }
10953 /* after using ":s", repeat all the modifiers */
10954 if (didit)
10955 goto repeat;
10956 }
10957 }
10958
Bram Moolenaar26df0922014-02-23 23:39:13 +010010959 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10960 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010961 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010962 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010963 if (c != NUL)
10964 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010965 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010966 if (c != NUL)
10967 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010968 if (p == NULL)
10969 return -1;
10970 vim_free(*bufp);
10971 *bufp = *fnamep = p;
10972 *fnamelen = (int)STRLEN(p);
10973 *usedlen += 2;
10974 }
10975
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return valid;
10977}
10978
10979/*
10980 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010981 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 * "flags" can be "g" to do a global substitute.
10983 * Returns an allocated string, NULL for error.
10984 */
10985 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010986do_string_sub(
10987 char_u *str,
10988 char_u *pat,
10989 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010990 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010991 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
10993 int sublen;
10994 regmatch_T regmatch;
10995 int i;
10996 int do_all;
10997 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010998 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 garray_T ga;
11000 char_u *ret;
11001 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010011002 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003
11004 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
11005 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011006 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007
11008 ga_init2(&ga, 1, 200);
11009
11010 do_all = (flags[0] == 'g');
11011
11012 regmatch.rm_ic = p_ic;
11013 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11014 if (regmatch.regprog != NULL)
11015 {
11016 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010011017 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
11019 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010011020 /* Skip empty match except for first match. */
11021 if (regmatch.startp[0] == regmatch.endp[0])
11022 {
11023 if (zero_width == regmatch.startp[0])
11024 {
11025 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020011026 i = MB_PTR2LEN(tail);
11027 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
11028 (size_t)i);
11029 ga.ga_len += i;
11030 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010011031 continue;
11032 }
11033 zero_width = regmatch.startp[0];
11034 }
11035
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 /*
11037 * Get some space for a temporary buffer to do the substitution
11038 * into. It will contain:
11039 * - The text up to where the match is.
11040 * - The substituted text.
11041 * - The text after the match.
11042 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011043 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010011044 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11046 {
11047 ga_clear(&ga);
11048 break;
11049 }
11050
11051 /* copy the text up to where the match is */
11052 i = (int)(regmatch.startp[0] - tail);
11053 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11054 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011055 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 + ga.ga_len + i, TRUE, TRUE, FALSE);
11057 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020011058 tail = regmatch.endp[0];
11059 if (*tail == NUL)
11060 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 if (!do_all)
11062 break;
11063 }
11064
11065 if (ga.ga_data != NULL)
11066 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11067
Bram Moolenaar473de612013-06-08 18:19:48 +020011068 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 }
11070
11071 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11072 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011073 if (p_cpo == empty_option)
11074 p_cpo = save_cpo;
11075 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011076 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011077 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
11079 return ret;
11080}
11081
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011082 static int
11083filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
11084{
11085 typval_T rettv;
11086 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011087 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011088
11089 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
11090 argv[0] = vimvars[VV_KEY].vv_tv;
11091 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010011092 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
11093 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011094 if (map)
11095 {
11096 /* map(): replace the list item value */
11097 clear_tv(tv);
11098 rettv.v_lock = 0;
11099 *tv = rettv;
11100 }
11101 else
11102 {
11103 int error = FALSE;
11104
11105 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011106 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011107 clear_tv(&rettv);
11108 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011109 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011110 if (error)
11111 goto theend;
11112 }
11113 retval = OK;
11114theend:
11115 clear_tv(&vimvars[VV_VAL].vv_tv);
11116 return retval;
11117}
11118
11119
11120/*
11121 * Implementation of map() and filter().
11122 */
11123 void
11124filter_map(typval_T *argvars, typval_T *rettv, int map)
11125{
11126 typval_T *expr;
11127 listitem_T *li, *nli;
11128 list_T *l = NULL;
11129 dictitem_T *di;
11130 hashtab_T *ht;
11131 hashitem_T *hi;
11132 dict_T *d = NULL;
11133 typval_T save_val;
11134 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011135 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011136 int rem;
11137 int todo;
11138 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11139 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11140 : N_("filter() argument"));
11141 int save_did_emsg;
11142 int idx = 0;
11143
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011144 if (argvars[0].v_type == VAR_BLOB)
11145 {
11146 if ((b = argvars[0].vval.v_blob) == NULL)
11147 return;
11148 }
11149 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 {
11151 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011152 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 return;
11154 }
11155 else if (argvars[0].v_type == VAR_DICT)
11156 {
11157 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011158 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011159 return;
11160 }
11161 else
11162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011163 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011164 return;
11165 }
11166
11167 expr = &argvars[1];
11168 /* On type errors, the preceding call has already displayed an error
11169 * message. Avoid a misleading error message for an empty string that
11170 * was not passed as argument. */
11171 if (expr->v_type != VAR_UNKNOWN)
11172 {
11173 prepare_vimvar(VV_VAL, &save_val);
11174
11175 /* We reset "did_emsg" to be able to detect whether an error
11176 * occurred during evaluation of the expression. */
11177 save_did_emsg = did_emsg;
11178 did_emsg = FALSE;
11179
11180 prepare_vimvar(VV_KEY, &save_key);
11181 if (argvars[0].v_type == VAR_DICT)
11182 {
11183 vimvars[VV_KEY].vv_type = VAR_STRING;
11184
11185 ht = &d->dv_hashtab;
11186 hash_lock(ht);
11187 todo = (int)ht->ht_used;
11188 for (hi = ht->ht_array; todo > 0; ++hi)
11189 {
11190 if (!HASHITEM_EMPTY(hi))
11191 {
11192 int r;
11193
11194 --todo;
11195 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011196 if (map && (var_check_lock(di->di_tv.v_lock,
11197 arg_errmsg, TRUE)
11198 || var_check_ro(di->di_flags,
11199 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011200 break;
11201 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11202 r = filter_map_one(&di->di_tv, expr, map, &rem);
11203 clear_tv(&vimvars[VV_KEY].vv_tv);
11204 if (r == FAIL || did_emsg)
11205 break;
11206 if (!map && rem)
11207 {
11208 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11209 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11210 break;
11211 dictitem_remove(d, di);
11212 }
11213 }
11214 }
11215 hash_unlock(ht);
11216 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011217 else if (argvars[0].v_type == VAR_BLOB)
11218 {
11219 int i;
11220 typval_T tv;
11221
11222 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11223 for (i = 0; i < b->bv_ga.ga_len; i++)
11224 {
11225 tv.v_type = VAR_NUMBER;
11226 tv.vval.v_number = blob_get(b, i);
11227 vimvars[VV_KEY].vv_nr = idx;
11228 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11229 break;
11230 if (tv.v_type != VAR_NUMBER)
11231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011232 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011233 return;
11234 }
11235 tv.v_type = VAR_NUMBER;
11236 blob_set(b, i, tv.vval.v_number);
11237 if (!map && rem)
11238 {
11239 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11240
11241 mch_memmove(p + idx, p + i + 1,
11242 (size_t)b->bv_ga.ga_len - i - 1);
11243 --b->bv_ga.ga_len;
11244 --i;
11245 }
11246 }
11247 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011248 else
11249 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011250 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011251 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11252
11253 for (li = l->lv_first; li != NULL; li = nli)
11254 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011255 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011256 break;
11257 nli = li->li_next;
11258 vimvars[VV_KEY].vv_nr = idx;
11259 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11260 || did_emsg)
11261 break;
11262 if (!map && rem)
11263 listitem_remove(l, li);
11264 ++idx;
11265 }
11266 }
11267
11268 restore_vimvar(VV_KEY, &save_key);
11269 restore_vimvar(VV_VAL, &save_val);
11270
11271 did_emsg |= save_did_emsg;
11272 }
11273
11274 copy_tv(&argvars[0], rettv);
11275}
11276
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */