blob: 085e7d7e9a9984c178f8c38279f74e4bfa4d84fb [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/*
5844 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005845 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5846 *
5847 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005848 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005850set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005851{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005852 listitem_T *li;
5853 int abort = FALSE;
5854 list_T *cur_l;
5855 list_stack_T *list_stack = NULL;
5856 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005857
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005858 cur_l = l;
5859 for (;;)
5860 {
5861 if (!abort)
5862 /* Mark each item in the list. If the item contains a hashtab
5863 * it is added to ht_stack, if it contains a list it is added to
5864 * list_stack. */
5865 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5866 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5867 ht_stack, &list_stack);
5868 if (list_stack == NULL)
5869 break;
5870
5871 /* take an item from the stack */
5872 cur_l = list_stack->list;
5873 tempitem = list_stack;
5874 list_stack = list_stack->prev;
5875 free(tempitem);
5876 }
5877
5878 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005879}
5880
5881/*
5882 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005883 * "list_stack" is used to add lists to be marked. Can be NULL.
5884 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5885 *
5886 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005887 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005889set_ref_in_item(
5890 typval_T *tv,
5891 int copyID,
5892 ht_stack_T **ht_stack,
5893 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005895 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005896
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005897 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005898 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005899 dict_T *dd = tv->vval.v_dict;
5900
Bram Moolenaara03f2332016-02-06 18:09:59 +01005901 if (dd != NULL && dd->dv_copyID != copyID)
5902 {
5903 /* Didn't see this dict yet. */
5904 dd->dv_copyID = copyID;
5905 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005906 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005907 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5908 }
5909 else
5910 {
5911 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5912 if (newitem == NULL)
5913 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005914 else
5915 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005916 newitem->ht = &dd->dv_hashtab;
5917 newitem->prev = *ht_stack;
5918 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005919 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005920 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005921 }
5922 }
5923 else if (tv->v_type == VAR_LIST)
5924 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005925 list_T *ll = tv->vval.v_list;
5926
Bram Moolenaara03f2332016-02-06 18:09:59 +01005927 if (ll != NULL && ll->lv_copyID != copyID)
5928 {
5929 /* Didn't see this list yet. */
5930 ll->lv_copyID = copyID;
5931 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005933 abort = set_ref_in_list(ll, copyID, ht_stack);
5934 }
5935 else
5936 {
5937 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005938 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005939 if (newitem == NULL)
5940 abort = TRUE;
5941 else
5942 {
5943 newitem->list = ll;
5944 newitem->prev = *list_stack;
5945 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005946 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005947 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005948 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005949 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005950 else if (tv->v_type == VAR_FUNC)
5951 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005952 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005953 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005954 else if (tv->v_type == VAR_PARTIAL)
5955 {
5956 partial_T *pt = tv->vval.v_partial;
5957 int i;
5958
5959 /* A partial does not have a copyID, because it cannot contain itself.
5960 */
5961 if (pt != NULL)
5962 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005963 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005964
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 if (pt->pt_dict != NULL)
5966 {
5967 typval_T dtv;
5968
5969 dtv.v_type = VAR_DICT;
5970 dtv.vval.v_dict = pt->pt_dict;
5971 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5972 }
5973
5974 for (i = 0; i < pt->pt_argc; ++i)
5975 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5976 ht_stack, list_stack);
5977 }
5978 }
5979#ifdef FEAT_JOB_CHANNEL
5980 else if (tv->v_type == VAR_JOB)
5981 {
5982 job_T *job = tv->vval.v_job;
5983 typval_T dtv;
5984
5985 if (job != NULL && job->jv_copyID != copyID)
5986 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005987 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005988 if (job->jv_channel != NULL)
5989 {
5990 dtv.v_type = VAR_CHANNEL;
5991 dtv.vval.v_channel = job->jv_channel;
5992 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5993 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005994 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005995 {
5996 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005997 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005998 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5999 }
6000 }
6001 }
6002 else if (tv->v_type == VAR_CHANNEL)
6003 {
6004 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006005 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006006 typval_T dtv;
6007 jsonq_T *jq;
6008 cbq_T *cq;
6009
6010 if (ch != NULL && ch->ch_copyID != copyID)
6011 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006012 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006013 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006014 {
6015 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6016 jq = jq->jq_next)
6017 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6018 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6019 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006020 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006021 {
6022 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006023 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006024 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6025 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006026 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006027 {
6028 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006029 dtv.vval.v_partial =
6030 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006031 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6032 }
6033 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006034 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006035 {
6036 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006037 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006038 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6039 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006040 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006041 {
6042 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006043 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006044 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6045 }
6046 }
6047 }
6048#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006049 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006050}
6051
Bram Moolenaar17a13432016-01-24 14:22:10 +01006052 static char *
6053get_var_special_name(int nr)
6054{
6055 switch (nr)
6056 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006057 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006058 case VVAL_TRUE: return "v:true";
6059 case VVAL_NONE: return "v:none";
6060 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006061 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006062 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006063 return "42";
6064}
6065
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 * Return a string with the string representation of a variable.
6068 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006070 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006071 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6072 * stings as "string()", otherwise does not put quotes around strings, as
6073 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006074 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6075 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006076 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006078 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006079echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080 typval_T *tv,
6081 char_u **tofree,
6082 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006083 int copyID,
6084 int echo_style,
6085 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006086 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088 static int recurse = 0;
6089 char_u *r = NULL;
6090
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006093 if (!did_echo_string_emsg)
6094 {
6095 /* Only give this message once for a recursive call to avoid
6096 * flooding the user with errors. And stop iterating over lists
6097 * and dicts. */
6098 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006099 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006102 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103 }
6104 ++recurse;
6105
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 switch (tv->v_type)
6107 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006108 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006109 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006110 {
6111 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006112 r = tv->vval.v_string;
6113 if (r == NULL)
6114 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006115 }
6116 else
6117 {
6118 *tofree = string_quote(tv->vval.v_string, FALSE);
6119 r = *tofree;
6120 }
6121 break;
6122
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006124 if (echo_style)
6125 {
6126 *tofree = NULL;
6127 r = tv->vval.v_string;
6128 }
6129 else
6130 {
6131 *tofree = string_quote(tv->vval.v_string, TRUE);
6132 r = *tofree;
6133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006134 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006135
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006136 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006137 {
6138 partial_T *pt = tv->vval.v_partial;
6139 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006140 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006141 garray_T ga;
6142 int i;
6143 char_u *tf;
6144
6145 ga_init2(&ga, 1, 100);
6146 ga_concat(&ga, (char_u *)"function(");
6147 if (fname != NULL)
6148 {
6149 ga_concat(&ga, fname);
6150 vim_free(fname);
6151 }
6152 if (pt != NULL && pt->pt_argc > 0)
6153 {
6154 ga_concat(&ga, (char_u *)", [");
6155 for (i = 0; i < pt->pt_argc; ++i)
6156 {
6157 if (i > 0)
6158 ga_concat(&ga, (char_u *)", ");
6159 ga_concat(&ga,
6160 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6161 vim_free(tf);
6162 }
6163 ga_concat(&ga, (char_u *)"]");
6164 }
6165 if (pt != NULL && pt->pt_dict != NULL)
6166 {
6167 typval_T dtv;
6168
6169 ga_concat(&ga, (char_u *)", ");
6170 dtv.v_type = VAR_DICT;
6171 dtv.vval.v_dict = pt->pt_dict;
6172 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6173 vim_free(tf);
6174 }
6175 ga_concat(&ga, (char_u *)")");
6176
6177 *tofree = ga.ga_data;
6178 r = *tofree;
6179 break;
6180 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006181
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006182 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006183 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006184 break;
6185
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006186 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006187 if (tv->vval.v_list == NULL)
6188 {
6189 *tofree = NULL;
6190 r = NULL;
6191 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006192 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6193 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006194 {
6195 *tofree = NULL;
6196 r = (char_u *)"[...]";
6197 }
6198 else
6199 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006200 int old_copyID = tv->vval.v_list->lv_copyID;
6201
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006202 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006203 *tofree = list2string(tv, copyID, restore_copyID);
6204 if (restore_copyID)
6205 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006206 r = *tofree;
6207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006209
Bram Moolenaar8c711452005-01-14 21:53:12 +00006210 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006211 if (tv->vval.v_dict == NULL)
6212 {
6213 *tofree = NULL;
6214 r = NULL;
6215 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006216 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6217 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218 {
6219 *tofree = NULL;
6220 r = (char_u *)"{...}";
6221 }
6222 else
6223 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006224 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006225 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006226 *tofree = dict2string(tv, copyID, restore_copyID);
6227 if (restore_copyID)
6228 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006229 r = *tofree;
6230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006232
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006233 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006234 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006235 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006236 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006237 break;
6238
Bram Moolenaar835dc632016-02-07 14:27:38 +01006239 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006240 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006241 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006242 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006243 if (composite_val)
6244 {
6245 *tofree = string_quote(r, FALSE);
6246 r = *tofree;
6247 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006249
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006250 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006251#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006252 *tofree = NULL;
6253 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6254 r = numbuf;
6255 break;
6256#endif
6257
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006258 case VAR_SPECIAL:
6259 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006260 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006261 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006263
Bram Moolenaar8502c702014-06-17 12:51:16 +02006264 if (--recurse == 0)
6265 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006266 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006267}
6268
6269/*
6270 * Return a string with the string representation of a variable.
6271 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6272 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006273 * Does not put quotes around strings, as ":echo" displays values.
6274 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6275 * May return NULL.
6276 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006277 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006278echo_string(
6279 typval_T *tv,
6280 char_u **tofree,
6281 char_u *numbuf,
6282 int copyID)
6283{
6284 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6285}
6286
6287/*
6288 * Return a string with the string representation of a variable.
6289 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6290 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006291 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006292 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006293 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006295tv2string(
6296 typval_T *tv,
6297 char_u **tofree,
6298 char_u *numbuf,
6299 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006300{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006301 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302}
6303
6304/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 * Return string "str" in ' quotes, doubling ' characters.
6306 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006307 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006308 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006309 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006311{
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006313 char_u *p, *r, *s;
6314
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 len = (function ? 13 : 3);
6316 if (str != NULL)
6317 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006318 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006319 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 if (*p == '\'')
6321 ++len;
6322 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006323 s = r = alloc(len);
6324 if (r != NULL)
6325 {
6326 if (function)
6327 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006329 r += 10;
6330 }
6331 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006332 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 if (str != NULL)
6334 for (p = str; *p != NUL; )
6335 {
6336 if (*p == '\'')
6337 *r++ = '\'';
6338 MB_COPY_CHAR(p, r);
6339 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006341 if (function)
6342 *r++ = ')';
6343 *r++ = NUL;
6344 }
6345 return s;
6346}
6347
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006348#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006349/*
6350 * Convert the string "text" to a floating point number.
6351 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6352 * this always uses a decimal point.
6353 * Returns the length of the text that was consumed.
6354 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356string2float(
6357 char_u *text,
6358 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006359{
6360 char *s = (char *)text;
6361 float_T f;
6362
Bram Moolenaar62473612017-01-08 19:25:40 +01006363 /* MS-Windows does not deal with "inf" and "nan" properly. */
6364 if (STRNICMP(text, "inf", 3) == 0)
6365 {
6366 *value = INFINITY;
6367 return 3;
6368 }
6369 if (STRNICMP(text, "-inf", 3) == 0)
6370 {
6371 *value = -INFINITY;
6372 return 4;
6373 }
6374 if (STRNICMP(text, "nan", 3) == 0)
6375 {
6376 *value = NAN;
6377 return 3;
6378 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006379 f = strtod(s, &s);
6380 *value = f;
6381 return (int)((char_u *)s - text);
6382}
6383#endif
6384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Get the value of an environment variable.
6387 * "arg" is pointing to the '$'. It is advanced to after the name.
6388 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006389 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 */
6391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006392get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 char_u *string = NULL;
6395 int len;
6396 int cc;
6397 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006398 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 ++*arg;
6401 name = *arg;
6402 len = get_env_len(arg);
6403 if (evaluate)
6404 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006405 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006406 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006407
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006408 cc = name[len];
6409 name[len] = NUL;
6410 /* first try vim_getenv(), fast for normal environment vars */
6411 string = vim_getenv(name, &mustfree);
6412 if (string != NULL && *string != NUL)
6413 {
6414 if (!mustfree)
6415 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006417 else
6418 {
6419 if (mustfree)
6420 vim_free(string);
6421
6422 /* next try expanding things like $VIM and ${HOME} */
6423 string = expand_env_save(name - 1);
6424 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006425 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006426 }
6427 name[len] = cc;
6428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006429 rettv->v_type = VAR_STRING;
6430 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
6432
6433 return OK;
6434}
6435
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006436
6437
6438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006440 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006443var2fpos(
6444 typval_T *varp,
6445 int dollar_lnum, /* TRUE when $ is last line */
6446 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006448 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006450 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
Bram Moolenaara5525202006-03-02 22:52:09 +00006452 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006453 if (varp->v_type == VAR_LIST)
6454 {
6455 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006456 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006457 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006458 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006459
6460 l = varp->vval.v_list;
6461 if (l == NULL)
6462 return NULL;
6463
6464 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006465 pos.lnum = list_find_nr(l, 0L, &error);
6466 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006467 return NULL; /* invalid line number */
6468
6469 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006470 pos.col = list_find_nr(l, 1L, &error);
6471 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006472 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006473 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006474
6475 /* We accept "$" for the column number: last column. */
6476 li = list_find(l, 1L);
6477 if (li != NULL && li->li_tv.v_type == VAR_STRING
6478 && li->li_tv.vval.v_string != NULL
6479 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6480 pos.col = len + 1;
6481
Bram Moolenaara5525202006-03-02 22:52:09 +00006482 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006483 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006484 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006485 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006486
Bram Moolenaara5525202006-03-02 22:52:09 +00006487 /* Get the virtual offset. Defaults to zero. */
6488 pos.coladd = list_find_nr(l, 2L, &error);
6489 if (error)
6490 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006491
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006492 return &pos;
6493 }
6494
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006495 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006496 if (name == NULL)
6497 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006498 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006500 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6501 {
6502 if (VIsual_active)
6503 return &VIsual;
6504 return &curwin->w_cursor;
6505 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006506 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006508 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6510 return NULL;
6511 return pp;
6512 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006513
Bram Moolenaara5525202006-03-02 22:52:09 +00006514 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006515
Bram Moolenaar477933c2007-07-17 14:32:23 +00006516 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006517 {
6518 pos.col = 0;
6519 if (name[1] == '0') /* "w0": first visible line */
6520 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006521 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006522 /* In silent Ex mode topline is zero, but that's not a valid line
6523 * number; use one instead. */
6524 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006525 return &pos;
6526 }
6527 else if (name[1] == '$') /* "w$": last visible line */
6528 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006529 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006530 /* In silent Ex mode botline is zero, return zero then. */
6531 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006532 return &pos;
6533 }
6534 }
6535 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006537 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
6539 pos.lnum = curbuf->b_ml.ml_line_count;
6540 pos.col = 0;
6541 }
6542 else
6543 {
6544 pos.lnum = curwin->w_cursor.lnum;
6545 pos.col = (colnr_T)STRLEN(ml_get_curline());
6546 }
6547 return &pos;
6548 }
6549 return NULL;
6550}
6551
6552/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006553 * Convert list in "arg" into a position and optional file number.
6554 * When "fnump" is NULL there is no file number, only 3 items.
6555 * Note that the column is passed on as-is, the caller may want to decrement
6556 * it to use 1 for the first column.
6557 * Return FAIL when conversion is not possible, doesn't check the position for
6558 * validity.
6559 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006561list2fpos(
6562 typval_T *arg,
6563 pos_T *posp,
6564 int *fnump,
6565 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006566{
6567 list_T *l = arg->vval.v_list;
6568 long i = 0;
6569 long n;
6570
Bram Moolenaar493c1782014-05-28 14:34:46 +02006571 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6572 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006573 if (arg->v_type != VAR_LIST
6574 || l == NULL
6575 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006576 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006577 return FAIL;
6578
6579 if (fnump != NULL)
6580 {
6581 n = list_find_nr(l, i++, NULL); /* fnum */
6582 if (n < 0)
6583 return FAIL;
6584 if (n == 0)
6585 n = curbuf->b_fnum; /* current buffer */
6586 *fnump = n;
6587 }
6588
6589 n = list_find_nr(l, i++, NULL); /* lnum */
6590 if (n < 0)
6591 return FAIL;
6592 posp->lnum = n;
6593
6594 n = list_find_nr(l, i++, NULL); /* col */
6595 if (n < 0)
6596 return FAIL;
6597 posp->col = n;
6598
Bram Moolenaar493c1782014-05-28 14:34:46 +02006599 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006600 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006601 posp->coladd = 0;
6602 else
6603 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006604
Bram Moolenaar493c1782014-05-28 14:34:46 +02006605 if (curswantp != NULL)
6606 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6607
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006608 return OK;
6609}
6610
6611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 * Get the length of an environment variable name.
6613 * Advance "arg" to the first character after the name.
6614 * Return 0 for error.
6615 */
6616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 char_u *p;
6620 int len;
6621
6622 for (p = *arg; vim_isIDc(*p); ++p)
6623 ;
6624 if (p == *arg) /* no name found */
6625 return 0;
6626
6627 len = (int)(p - *arg);
6628 *arg = p;
6629 return len;
6630}
6631
6632/*
6633 * Get the length of the name of a function or internal variable.
6634 * "arg" is advanced to the first non-white character after the name.
6635 * Return 0 if something is wrong.
6636 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006638get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 char_u *p;
6641 int len;
6642
6643 /* Find the end of the name. */
6644 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006645 {
6646 if (*p == ':')
6647 {
6648 /* "s:" is start of "s:var", but "n:" is not and can be used in
6649 * slice "[n:]". Also "xx:" is not a namespace. */
6650 len = (int)(p - *arg);
6651 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6652 || len > 1)
6653 break;
6654 }
6655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (p == *arg) /* no name found */
6657 return 0;
6658
6659 len = (int)(p - *arg);
6660 *arg = skipwhite(p);
6661
6662 return len;
6663}
6664
6665/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006666 * Get the length of the name of a variable or function.
6667 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006669 * Return -1 if curly braces expansion failed.
6670 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 * If the name contains 'magic' {}'s, expand them and return the
6672 * expanded name in an allocated string via 'alias' - caller must free.
6673 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006675get_name_len(
6676 char_u **arg,
6677 char_u **alias,
6678 int evaluate,
6679 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 char_u *p;
6683 char_u *expr_start;
6684 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686 *alias = NULL; /* default to no alias */
6687
6688 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6689 && (*arg)[2] == (int)KE_SNR)
6690 {
6691 /* hard coded <SNR>, already translated */
6692 *arg += 3;
6693 return get_id_len(arg) + 3;
6694 }
6695 len = eval_fname_script(*arg);
6696 if (len > 0)
6697 {
6698 /* literal "<SID>", "s:" or "<SNR>" */
6699 *arg += len;
6700 }
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006705 p = find_name_end(*arg, &expr_start, &expr_end,
6706 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (expr_start != NULL)
6708 {
6709 char_u *temp_string;
6710
6711 if (!evaluate)
6712 {
6713 len += (int)(p - *arg);
6714 *arg = skipwhite(p);
6715 return len;
6716 }
6717
6718 /*
6719 * Include any <SID> etc in the expanded string:
6720 * Thus the -len here.
6721 */
6722 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6723 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006724 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 *alias = temp_string;
6726 *arg = skipwhite(p);
6727 return (int)STRLEN(temp_string);
6728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006731 // Only give an error when there is something, otherwise it will be
6732 // reported at a higher level.
6733 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006734 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 return len;
6737}
6738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006739/*
6740 * Find the end of a variable or function name, taking care of magic braces.
6741 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6742 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006743 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744 * Return a pointer to just after the name. Equal to "arg" if there is no
6745 * valid name.
6746 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006747 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006748find_name_end(
6749 char_u *arg,
6750 char_u **expr_start,
6751 char_u **expr_end,
6752 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006754 int mb_nest = 0;
6755 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006757 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006759 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006761 *expr_start = NULL;
6762 *expr_end = NULL;
6763 }
6764
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006765 /* Quick check for valid starting character. */
6766 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6767 return arg;
6768
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006769 for (p = arg; *p != NUL
6770 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006772 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006773 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006774 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006776 if (*p == '\'')
6777 {
6778 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006779 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006780 ;
6781 if (*p == NUL)
6782 break;
6783 }
6784 else if (*p == '"')
6785 {
6786 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006787 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006788 if (*p == '\\' && p[1] != NUL)
6789 ++p;
6790 if (*p == NUL)
6791 break;
6792 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006793 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6794 {
6795 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006796 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006797 len = (int)(p - arg);
6798 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006799 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006800 break;
6801 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006803 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805 if (*p == '[')
6806 ++br_nest;
6807 else if (*p == ']')
6808 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006810
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006811 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006813 if (*p == '{')
6814 {
6815 mb_nest++;
6816 if (expr_start != NULL && *expr_start == NULL)
6817 *expr_start = p;
6818 }
6819 else if (*p == '}')
6820 {
6821 mb_nest--;
6822 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6823 *expr_end = p;
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827
6828 return p;
6829}
6830
6831/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006832 * Expands out the 'magic' {}'s in a variable/function name.
6833 * Note that this can call itself recursively, to deal with
6834 * constructs like foo{bar}{baz}{bam}
6835 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6836 * "in_start" ^
6837 * "expr_start" ^
6838 * "expr_end" ^
6839 * "in_end" ^
6840 *
6841 * Returns a new allocated string, which the caller must free.
6842 * Returns NULL for failure.
6843 */
6844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006845make_expanded_name(
6846 char_u *in_start,
6847 char_u *expr_start,
6848 char_u *expr_end,
6849 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006850{
6851 char_u c1;
6852 char_u *retval = NULL;
6853 char_u *temp_result;
6854 char_u *nextcmd = NULL;
6855
6856 if (expr_end == NULL || in_end == NULL)
6857 return NULL;
6858 *expr_start = NUL;
6859 *expr_end = NUL;
6860 c1 = *in_end;
6861 *in_end = NUL;
6862
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006863 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006864 if (temp_result != NULL && nextcmd == NULL)
6865 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006866 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6867 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868 if (retval != NULL)
6869 {
6870 STRCPY(retval, in_start);
6871 STRCAT(retval, temp_result);
6872 STRCAT(retval, expr_end + 1);
6873 }
6874 }
6875 vim_free(temp_result);
6876
6877 *in_end = c1; /* put char back for error messages */
6878 *expr_start = '{';
6879 *expr_end = '}';
6880
6881 if (retval != NULL)
6882 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006883 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006884 if (expr_start != NULL)
6885 {
6886 /* Further expansion! */
6887 temp_result = make_expanded_name(retval, expr_start,
6888 expr_end, temp_result);
6889 vim_free(retval);
6890 retval = temp_result;
6891 }
6892 }
6893
6894 return retval;
6895}
6896
6897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006901 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006904 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6905}
6906
6907/*
6908 * Return TRUE if character "c" can be used as the first character in a
6909 * variable or function name (excluding '{' and '}').
6910 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006911 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006913{
6914 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915}
6916
6917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 * Set number v: variable to "val".
6919 */
6920 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006921set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925
6926/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006927 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006929 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006930get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006932 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006935/*
6936 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006937 * If the String variable has never been set, return an empty string.
6938 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006939 */
6940 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006942{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006943 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006944}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006947 * Get List v: variable value. Caller must take care of reference count when
6948 * needed.
6949 */
6950 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006951get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006952{
6953 return vimvars[idx].vv_list;
6954}
6955
6956/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006957 * Get Dict v: variable value. Caller must take care of reference count when
6958 * needed.
6959 */
6960 dict_T *
6961get_vim_var_dict(int idx)
6962{
6963 return vimvars[idx].vv_dict;
6964}
6965
6966/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006967 * Set v:char to character "c".
6968 */
6969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006971{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006972 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006973
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006974 if (has_mbyte)
6975 buf[(*mb_char2bytes)(c, buf)] = NUL;
6976 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006977 {
6978 buf[0] = c;
6979 buf[1] = NUL;
6980 }
6981 set_vim_var_string(VV_CHAR, buf, -1);
6982}
6983
6984/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006985 * Set v:count to "count" and v:count1 to "count1".
6986 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 */
6988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989set_vcount(
6990 long count,
6991 long count1,
6992 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006994 if (set_prevcount)
6995 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 vimvars[VV_COUNT].vv_nr = count;
6997 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
7000/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007001 * Save variables that might be changed as a side effect. Used when executing
7002 * a timer callback.
7003 */
7004 void
7005save_vimvars(vimvars_save_T *vvsave)
7006{
7007 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7008 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7009 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7010}
7011
7012/*
7013 * Restore variables saved by save_vimvars().
7014 */
7015 void
7016restore_vimvars(vimvars_save_T *vvsave)
7017{
7018 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7019 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7020 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7021}
7022
7023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 * Set string v: variable to a copy of "val".
7025 */
7026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007027set_vim_var_string(
7028 int idx,
7029 char_u *val,
7030 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
Bram Moolenaara542c682016-01-31 16:28:04 +01007032 clear_tv(&vimvars[idx].vv_di.di_tv);
7033 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040}
7041
7042/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007043 * Set List v: variable to "val".
7044 */
7045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007046set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007047{
Bram Moolenaara542c682016-01-31 16:28:04 +01007048 clear_tv(&vimvars[idx].vv_di.di_tv);
7049 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007050 vimvars[idx].vv_list = val;
7051 if (val != NULL)
7052 ++val->lv_refcount;
7053}
7054
7055/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007056 * Set Dictionary v: variable to "val".
7057 */
7058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007060{
Bram Moolenaara542c682016-01-31 16:28:04 +01007061 clear_tv(&vimvars[idx].vv_di.di_tv);
7062 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007063 vimvars[idx].vv_dict = val;
7064 if (val != NULL)
7065 {
7066 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007067 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007068 }
7069}
7070
7071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 * Set v:register if needed.
7073 */
7074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007075set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 char_u regname;
7078
7079 if (c == 0 || c == ' ')
7080 regname = '"';
7081 else
7082 regname = c;
7083 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 set_vim_var_string(VV_REG, &regname, 1);
7086}
7087
7088/*
7089 * Get or set v:exception. If "oldval" == NULL, return the current value.
7090 * Otherwise, restore the value to "oldval" and return NULL.
7091 * Must always be called in pairs to save and restore v:exception! Does not
7092 * take care of memory allocations.
7093 */
7094 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007095v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 return NULL;
7102}
7103
7104/*
7105 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7106 * Otherwise, restore the value to "oldval" and return NULL.
7107 * Must always be called in pairs to save and restore v:throwpoint! Does not
7108 * take care of memory allocations.
7109 */
7110 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
Bram Moolenaare9a41262005-01-15 22:18:47 +00007116 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 return NULL;
7118}
7119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120/*
7121 * Set v:cmdarg.
7122 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7123 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7124 * Must always be called in pairs!
7125 */
7126 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007127set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 char_u *oldval;
7130 char_u *newval;
7131 unsigned len;
7132
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007134 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007136 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007138 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 }
7140
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007141 if (eap->force_bin == FORCE_BIN)
7142 len = 6;
7143 else if (eap->force_bin == FORCE_NOBIN)
7144 len = 8;
7145 else
7146 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007147
7148 if (eap->read_edit)
7149 len += 7;
7150
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007151 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007152 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007153 if (eap->force_enc != 0)
7154 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007155 if (eap->bad_char != 0)
7156 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007157
7158 newval = alloc(len + 1);
7159 if (newval == NULL)
7160 return NULL;
7161
7162 if (eap->force_bin == FORCE_BIN)
7163 sprintf((char *)newval, " ++bin");
7164 else if (eap->force_bin == FORCE_NOBIN)
7165 sprintf((char *)newval, " ++nobin");
7166 else
7167 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007168
7169 if (eap->read_edit)
7170 STRCAT(newval, " ++edit");
7171
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007172 if (eap->force_ff != 0)
7173 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007174 eap->force_ff == 'u' ? "unix"
7175 : eap->force_ff == 'd' ? "dos"
7176 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007177 if (eap->force_enc != 0)
7178 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7179 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007180 if (eap->bad_char == BAD_KEEP)
7181 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7182 else if (eap->bad_char == BAD_DROP)
7183 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7184 else if (eap->bad_char != 0)
7185 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007187 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190/*
7191 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007192 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007195get_var_tv(
7196 char_u *name,
7197 int len, /* length of "name" */
7198 typval_T *rettv, /* NULL when only checking existence */
7199 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7200 int verbose, /* may give error message */
7201 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
7208 /* truncate the name, so that we can use strcmp() */
7209 cc = name[len];
7210 name[len] = NUL;
7211
7212 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 * Check for user-defined variables.
7214 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007215 v = find_var(name, NULL, no_autoload);
7216 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007218 tv = &v->di_tv;
7219 if (dip != NULL)
7220 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007225 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007226 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 ret = FAIL;
7228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007229 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 name[len] = cc;
7233
7234 return ret;
7235}
7236
7237/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007238 * Check if variable "name[len]" is a local variable or an argument.
7239 * If so, "*eval_lavars_used" is set to TRUE.
7240 */
7241 static void
7242check_vars(char_u *name, int len)
7243{
7244 int cc;
7245 char_u *varname;
7246 hashtab_T *ht;
7247
7248 if (eval_lavars_used == NULL)
7249 return;
7250
7251 /* truncate the name, so that we can use strcmp() */
7252 cc = name[len];
7253 name[len] = NUL;
7254
7255 ht = find_var_ht(name, &varname);
7256 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7257 {
7258 if (find_var(name, NULL, TRUE) != NULL)
7259 *eval_lavars_used = TRUE;
7260 }
7261
7262 name[len] = cc;
7263}
7264
7265/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007266 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7267 * Also handle function call with Funcref variable: func(expr)
7268 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7269 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007271handle_subscript(
7272 char_u **arg,
7273 typval_T *rettv,
7274 int evaluate, /* do more than finding the end */
7275 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007276{
7277 int ret = OK;
7278 dict_T *selfdict = NULL;
7279 char_u *s;
7280 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007281 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007282
7283 while (ret == OK
7284 && (**arg == '['
7285 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007286 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7287 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007288 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007289 {
7290 if (**arg == '(')
7291 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007292 partial_T *pt = NULL;
7293
Bram Moolenaard9fba312005-06-26 22:34:35 +00007294 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007295 if (evaluate)
7296 {
7297 functv = *rettv;
7298 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007299
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007300 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007301 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007302 {
7303 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007304 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007305 }
7306 else
7307 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007308 }
7309 else
7310 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007311 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007312 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007313 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007314
7315 /* Clear the funcref afterwards, so that deleting it while
7316 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007317 if (evaluate)
7318 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007319
7320 /* Stop the expression evaluation when immediately aborting on
7321 * error, or when an interrupt occurred or an exception was thrown
7322 * but not caught. */
7323 if (aborting())
7324 {
7325 if (ret == OK)
7326 clear_tv(rettv);
7327 ret = FAIL;
7328 }
7329 dict_unref(selfdict);
7330 selfdict = NULL;
7331 }
7332 else /* **arg == '[' || **arg == '.' */
7333 {
7334 dict_unref(selfdict);
7335 if (rettv->v_type == VAR_DICT)
7336 {
7337 selfdict = rettv->vval.v_dict;
7338 if (selfdict != NULL)
7339 ++selfdict->dv_refcount;
7340 }
7341 else
7342 selfdict = NULL;
7343 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7344 {
7345 clear_tv(rettv);
7346 ret = FAIL;
7347 }
7348 }
7349 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007350
Bram Moolenaar1d429612016-05-24 15:44:17 +02007351 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7352 * Don't do this when "Func" is already a partial that was bound
7353 * explicitly (pt_auto is FALSE). */
7354 if (selfdict != NULL
7355 && (rettv->v_type == VAR_FUNC
7356 || (rettv->v_type == VAR_PARTIAL
7357 && (rettv->vval.v_partial->pt_auto
7358 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007359 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007360
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007361 dict_unref(selfdict);
7362 return ret;
7363}
7364
7365/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007366 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007367 * value).
7368 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007369 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007370alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007371{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007372 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373}
7374
7375/*
7376 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 * The string "s" must have been allocated, it is consumed.
7378 * Return NULL for out of memory, the variable otherwise.
7379 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007381alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007385 rettv = alloc_tv();
7386 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007388 rettv->v_type = VAR_STRING;
7389 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
7391 else
7392 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007393 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394}
7395
7396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007400free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402 if (varp != NULL)
7403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007404 switch (varp->v_type)
7405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007406 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007407 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007408 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007409 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007410 vim_free(varp->vval.v_string);
7411 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007412 case VAR_PARTIAL:
7413 partial_unref(varp->vval.v_partial);
7414 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007415 case VAR_BLOB:
7416 blob_unref(varp->vval.v_blob);
7417 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007418 case VAR_LIST:
7419 list_unref(varp->vval.v_list);
7420 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007421 case VAR_DICT:
7422 dict_unref(varp->vval.v_dict);
7423 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007424 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007425#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007426 job_unref(varp->vval.v_job);
7427 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007428#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007429 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007430#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007431 channel_unref(varp->vval.v_channel);
7432 break;
7433#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007434 case VAR_NUMBER:
7435 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007436 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007437 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007438 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 vim_free(varp);
7441 }
7442}
7443
7444/*
7445 * Free the memory for a variable value and set the value to NULL or 0.
7446 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007448clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450 if (varp != NULL)
7451 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007455 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007456 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007457 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007458 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007460 case VAR_PARTIAL:
7461 partial_unref(varp->vval.v_partial);
7462 varp->vval.v_partial = NULL;
7463 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007464 case VAR_BLOB:
7465 blob_unref(varp->vval.v_blob);
7466 varp->vval.v_blob = NULL;
7467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007468 case VAR_LIST:
7469 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007470 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 case VAR_DICT:
7473 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007477 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007478 varp->vval.v_number = 0;
7479 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007480 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007481#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007482 varp->vval.v_float = 0.0;
7483 break;
7484#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007485 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007486#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007487 job_unref(varp->vval.v_job);
7488 varp->vval.v_job = NULL;
7489#endif
7490 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007491 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007493 channel_unref(varp->vval.v_channel);
7494 varp->vval.v_channel = NULL;
7495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496 case VAR_UNKNOWN:
7497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007499 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501}
7502
7503/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007504 * Set the value of a variable to NULL without freeing items.
7505 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007507init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508{
7509 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511}
7512
7513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 * Get the number value of a variable.
7515 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007516 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007517 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 * caller of incompatible types: it sets *denote to TRUE if "denote"
7519 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007521 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007522tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007524 int error = FALSE;
7525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007526 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527}
7528
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007529 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007530tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007531{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007532 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007536 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007537 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007539#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007541 break;
7542#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007543 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007544 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007546 break;
7547 case VAR_STRING:
7548 if (varp->vval.v_string != NULL)
7549 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007550 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007551 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007552 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007553 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007554 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007555 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007557 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007558 case VAR_SPECIAL:
7559 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7560 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007561 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007563 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007564 break;
7565#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007566 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007567#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007568 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007569 break;
7570#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007571 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007572 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007573 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007574 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007575 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007578 if (denote == NULL) /* useful for values that must be unsigned */
7579 n = -1;
7580 else
7581 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583}
7584
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007585#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007587tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007588{
7589 switch (varp->v_type)
7590 {
7591 case VAR_NUMBER:
7592 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007593 case VAR_FLOAT:
7594 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007595 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007596 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007598 break;
7599 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007600 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007601 break;
7602 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007603 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007604 break;
7605 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007607 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007608 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007609 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007610 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007611 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007612# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007613 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007614 break;
7615# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007616 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007617# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007618 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007619 break;
7620# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007621 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007622 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007623 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007624 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007625 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007626 break;
7627 }
7628 return 0;
7629}
7630#endif
7631
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 * Get the string value of a variable.
7634 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7636 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 * If the String variable has never been set, return an empty string.
7638 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007639 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007642 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007643tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
7645 static char_u mybuf[NUMBUFLEN];
7646
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007647 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007650 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007651tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007653 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007654
7655 return res != NULL ? res : (char_u *)"";
7656}
7657
Bram Moolenaar7d647822014-04-05 21:28:56 +02007658/*
7659 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7660 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007661 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007662tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007663{
7664 static char_u mybuf[NUMBUFLEN];
7665
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007666 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667}
7668
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007669 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007670tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007675 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007676 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007677 return buf;
7678 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007679 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007681 break;
7682 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 break;
7685 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007686 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007688 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007689#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007690 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007691 break;
7692#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 case VAR_STRING:
7694 if (varp->vval.v_string != NULL)
7695 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007697 case VAR_SPECIAL:
7698 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7699 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007700 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007701 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007702 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007703 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007704#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007705 {
7706 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007707 char *status;
7708
7709 if (job == NULL)
7710 return (char_u *)"no process";
7711 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007712 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007713 : "run";
7714# ifdef UNIX
7715 vim_snprintf((char *)buf, NUMBUFLEN,
7716 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007717# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007718 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007719 "process %ld %s",
7720 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007721 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007722# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007723 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007724 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7725# endif
7726 return buf;
7727 }
7728#endif
7729 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007730 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007731#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007732 {
7733 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007734 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007735
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007736 if (channel == NULL)
7737 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7738 else
7739 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007740 "channel %d %s", channel->ch_id, status);
7741 return buf;
7742 }
7743#endif
7744 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007745 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007746 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750}
7751
7752/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007753 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7754 * string() on Dict, List, etc.
7755 */
7756 char_u *
7757tv_stringify(typval_T *varp, char_u *buf)
7758{
7759 if (varp->v_type == VAR_LIST
7760 || varp->v_type == VAR_DICT
7761 || varp->v_type == VAR_FUNC
7762 || varp->v_type == VAR_PARTIAL
7763 || varp->v_type == VAR_FLOAT)
7764 {
7765 typval_T tmp;
7766
7767 f_string(varp, &tmp);
7768 tv_get_string_buf(&tmp, buf);
7769 clear_tv(varp);
7770 *varp = tmp;
7771 return tmp.vval.v_string;
7772 }
7773 return tv_get_string_buf(varp, buf);
7774}
7775
7776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 * Find variable "name" in the list of variables.
7778 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007780 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007783 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007784find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007788 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
Bram Moolenaara7043832005-01-21 11:56:39 +00007790 ht = find_var_ht(name, &varname);
7791 if (htp != NULL)
7792 *htp = ht;
7793 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007795 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7796 if (ret != NULL)
7797 return ret;
7798
7799 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007800 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801}
7802
7803/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007804 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007805 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007808find_var_in_ht(
7809 hashtab_T *ht,
7810 int htname,
7811 char_u *varname,
7812 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007813{
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 hashitem_T *hi;
7815
7816 if (*varname == NUL)
7817 {
7818 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007819 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007821 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007822 case 'g': return &globvars_var;
7823 case 'v': return &vimvars_var;
7824 case 'b': return &curbuf->b_bufvar;
7825 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007826 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007827 case 'l': return get_funccal_local_var();
7828 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 }
7830 return NULL;
7831 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007832
7833 hi = hash_find(ht, varname);
7834 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007835 {
7836 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007837 * worked find the variable again. Don't auto-load a script if it was
7838 * loaded already, otherwise it would be loaded every time when
7839 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007840 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007841 {
7842 /* Note: script_autoload() may make "hi" invalid. It must either
7843 * be obtained again or not used. */
7844 if (!script_autoload(varname, FALSE) || aborting())
7845 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007846 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007847 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007848 if (HASHITEM_EMPTY(hi))
7849 return NULL;
7850 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007851 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007852}
7853
7854/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007856 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007857 * Set "varname" to the start of name without ':'.
7858 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007859 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007860find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007862 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007863 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007864
Bram Moolenaar73627d02015-08-11 15:46:09 +02007865 if (name[0] == NUL)
7866 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (name[1] != ':')
7868 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007869 /* The name must not start with a colon or #. */
7870 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 return NULL;
7872 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007873
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007874 // "version" is "v:version" in all scopes if scriptversion < 3.
7875 // Same for a few other variables marked with VV_COMPAT.
7876 if (current_sctx.sc_version < 3)
7877 {
7878 hi = hash_find(&compat_hashtab, name);
7879 if (!HASHITEM_EMPTY(hi))
7880 return &compat_hashtab;
7881 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007882
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007883 ht = get_funccal_local_ht();
7884 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007886 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007889 if (*name == 'g') /* global variable */
7890 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007891 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7892 */
7893 if (vim_strchr(name + 2, ':') != NULL
7894 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007895 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007897 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007899 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007900 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007901 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 if (*name == 'v') /* v: variable */
7903 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007904 if (*name == 'a') /* a: function argument */
7905 return get_funccal_args_ht();
7906 if (*name == 'l') /* l: local function variable */
7907 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007909 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7910 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 return NULL;
7912}
7913
7914/*
7915 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007916 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * Returns NULL when it doesn't exist.
7918 */
7919 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007920get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007924 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (v == NULL)
7926 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007927 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928}
7929
7930/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 * sourcing this script and when executing functions defined in the script.
7933 */
7934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007935new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaara7043832005-01-21 11:56:39 +00007937 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 hashtab_T *ht;
7939 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007940
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7942 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007943 /* Re-allocating ga_data means that an ht_array pointing to
7944 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007946 for (i = 1; i <= ga_scripts.ga_len; ++i)
7947 {
7948 ht = &SCRIPT_VARS(i);
7949 if (ht->ht_mask == HT_INIT_SIZE - 1)
7950 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007951 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007953 }
7954
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 while (ga_scripts.ga_len < id)
7956 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007957 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007958 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007959 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962 }
7963}
7964
7965/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7967 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 */
7969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007970init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971{
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007973 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007974 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007975 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007976 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 dict_var->di_tv.vval.v_dict = dict;
7978 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007979 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7981 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
7984/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007985 * Unreference a dictionary initialized by init_var_dict().
7986 */
7987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007988unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007989{
7990 /* Now the dict needs to be freed if no one else is using it, go back to
7991 * normal reference counting. */
7992 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7993 dict_unref(dict);
7994}
7995
7996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 * Frees all allocated variables and the value they contain.
7999 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 */
8001 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008002vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008003{
8004 vars_clear_ext(ht, TRUE);
8005}
8006
8007/*
8008 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008011vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
Bram Moolenaara7043832005-01-21 11:56:39 +00008013 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 hashitem_T *hi;
8015 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008018 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008019 for (hi = ht->ht_array; todo > 0; ++hi)
8020 {
8021 if (!HASHITEM_EMPTY(hi))
8022 {
8023 --todo;
8024
Bram Moolenaar33570922005-01-25 22:26:29 +00008025 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008026 * ht_array might change then. hash_clear() takes care of it
8027 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 v = HI2DI(hi);
8029 if (free_val)
8030 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008031 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008032 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008033 }
8034 }
8035 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008036 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037}
8038
Bram Moolenaara7043832005-01-21 11:56:39 +00008039/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 * Delete a variable from hashtab "ht" at item "hi".
8041 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008044delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008047
8048 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 clear_tv(&di->di_tv);
8050 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051}
8052
8053/*
8054 * List the value of one internal variable.
8055 */
8056 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008057list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008059 char_u *tofree;
8060 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008061 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008062
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008063 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008065 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008066 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008070list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008071 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008072 char_u *name,
8073 int type,
8074 char_u *string,
8075 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
Bram Moolenaar31859182007-08-14 20:41:13 +00008077 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8078 msg_start();
8079 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008081 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 msg_putchar(' ');
8083 msg_advance(22);
8084 if (type == VAR_NUMBER)
8085 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008086 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 msg_putchar('*');
8088 else if (type == VAR_LIST)
8089 {
8090 msg_putchar('[');
8091 if (*string == '[')
8092 ++string;
8093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008094 else if (type == VAR_DICT)
8095 {
8096 msg_putchar('{');
8097 if (*string == '{')
8098 ++string;
8099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 else
8101 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008104
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008105 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008106 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008107 if (*first)
8108 {
8109 msg_clr_eos();
8110 *first = FALSE;
8111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112}
8113
8114/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 * If the variable already exists, the value is updated.
8117 * Otherwise the variable is created.
8118 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008120set_var(
8121 char_u *name,
8122 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008123 int copy) // make copy of value in "tv"
8124{
8125 set_var_const(name, tv, copy, FALSE);
8126}
8127
8128/*
8129 * Set variable "name" to value in "tv".
8130 * If the variable already exists and "is_const" is FALSE the value is updated.
8131 * Otherwise the variable is created.
8132 */
8133 static void
8134set_var_const(
8135 char_u *name,
8136 typval_T *tv,
8137 int copy, // make copy of value in "tv"
8138 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008144 ht = find_var_ht(name, &varname);
8145 if (ht == NULL || *varname == NUL)
8146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008147 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008148 return;
8149 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008150 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008151
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008152 /* Search in parent scope which is possible to reference from lambda */
8153 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008154 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008155
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008156 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8157 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008158 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008159
Bram Moolenaar33570922005-01-25 22:26:29 +00008160 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008162 if (is_const)
8163 {
8164 emsg(_(e_cannot_mod));
8165 return;
8166 }
8167
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008169 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008170 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008172
8173 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008174 * Handle setting internal v: variables separately where needed to
8175 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 */
8177 if (ht == &vimvarht)
8178 {
8179 if (v->di_tv.v_type == VAR_STRING)
8180 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008181 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008183 {
8184 char_u *val = tv_get_string(tv);
8185
8186 // Careful: when assigning to v:errmsg and tv_get_string()
8187 // causes an error message the variable will alrady be set.
8188 if (v->di_tv.vval.v_string == NULL)
8189 v->di_tv.vval.v_string = vim_strsave(val);
8190 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 else
8192 {
8193 /* Take over the string to avoid an extra alloc/free. */
8194 v->di_tv.vval.v_string = tv->vval.v_string;
8195 tv->vval.v_string = NULL;
8196 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008197 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008199 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008200 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008201 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008202 if (STRCMP(varname, "searchforward") == 0)
8203 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008204#ifdef FEAT_SEARCH_EXTRA
8205 else if (STRCMP(varname, "hlsearch") == 0)
8206 {
8207 no_hlsearch = !v->di_tv.vval.v_number;
8208 redraw_all_later(SOME_VALID);
8209 }
8210#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008211 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008212 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008213 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008215 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008216 return;
8217 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 }
8219
8220 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
8222 else /* add a new variable */
8223 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008224 // Can't add "v:" or "a:" variable.
8225 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008227 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008228 return;
8229 }
8230
Bram Moolenaar31b81602019-02-10 22:14:27 +01008231 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008232 if (!valid_varname(varname))
8233 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008234
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008235 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008236 if (v == NULL)
8237 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008241 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 return;
8243 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008244 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008245 if (is_const)
8246 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008248
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008249 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008251 else
8252 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008254 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008256 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008257
8258 if (is_const)
8259 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260}
8261
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008263 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008264 * Also give an error message.
8265 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008267var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008268{
8269 if (flags & DI_FLAGS_RO)
8270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008271 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 return TRUE;
8273 }
8274 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008276 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 return TRUE;
8278 }
8279 return FALSE;
8280}
8281
8282/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008283 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8284 * Also give an error message.
8285 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008287var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008288{
8289 if (flags & DI_FLAGS_FIX)
8290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008291 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008292 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008293 return TRUE;
8294 }
8295 return FALSE;
8296}
8297
8298/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008299 * Check if a funcref is assigned to a valid variable name.
8300 * Return TRUE and give an error if not.
8301 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008303var_check_func_name(
8304 char_u *name, /* points to start of variable name */
8305 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008306{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008307 /* Allow for w: b: s: and t:. */
8308 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008309 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8310 ? name[2] : name[0]))
8311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008312 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008313 name);
8314 return TRUE;
8315 }
8316 /* Don't allow hiding a function. When "v" is not NULL we might be
8317 * assigning another function to the same var, the type is checked
8318 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008319 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008321 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008322 name);
8323 return TRUE;
8324 }
8325 return FALSE;
8326}
8327
8328/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008329 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008330 * Also give an error message, using "name" or _("name") when use_gettext is
8331 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008332 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008333 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008334var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008335{
8336 if (lock & VAR_LOCKED)
8337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008338 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008339 name == NULL ? (char_u *)_("Unknown")
8340 : use_gettext ? (char_u *)_(name)
8341 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008342 return TRUE;
8343 }
8344 if (lock & VAR_FIXED)
8345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008346 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008347 name == NULL ? (char_u *)_("Unknown")
8348 : use_gettext ? (char_u *)_(name)
8349 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008350 return TRUE;
8351 }
8352 return FALSE;
8353}
8354
8355/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008356 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8357 * Also give an error message, using "name" or _("name") when use_gettext is
8358 * TRUE.
8359 */
8360 static int
8361tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8362{
8363 int lock = 0;
8364
8365 switch (tv->v_type)
8366 {
8367 case VAR_BLOB:
8368 if (tv->vval.v_blob != NULL)
8369 lock = tv->vval.v_blob->bv_lock;
8370 break;
8371 case VAR_LIST:
8372 if (tv->vval.v_list != NULL)
8373 lock = tv->vval.v_list->lv_lock;
8374 break;
8375 case VAR_DICT:
8376 if (tv->vval.v_dict != NULL)
8377 lock = tv->vval.v_dict->dv_lock;
8378 break;
8379 default:
8380 break;
8381 }
8382 return var_check_lock(tv->v_lock, name, use_gettext)
8383 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8384}
8385
8386/*
8387 * Check if a variable name is valid.
8388 * Return FALSE and give an error if not.
8389 */
8390 int
8391valid_varname(char_u *varname)
8392{
8393 char_u *p;
8394
8395 for (p = varname; *p != NUL; ++p)
8396 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8397 && *p != AUTOLOAD_CHAR)
8398 {
8399 semsg(_(e_illvar), varname);
8400 return FALSE;
8401 }
8402 return TRUE;
8403}
8404
8405/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008406 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008407 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008408 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008409 * It is OK for "from" and "to" to point to the same item. This is used to
8410 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008411 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008413copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008415 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008416 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008417 switch (from->v_type)
8418 {
8419 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008420 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008421 to->vval.v_number = from->vval.v_number;
8422 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008423 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008424#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008425 to->vval.v_float = from->vval.v_float;
8426 break;
8427#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008428 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008429#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008430 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008431 if (to->vval.v_job != NULL)
8432 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008433 break;
8434#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008435 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008437 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008438 if (to->vval.v_channel != NULL)
8439 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008440 break;
8441#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008442 case VAR_STRING:
8443 case VAR_FUNC:
8444 if (from->vval.v_string == NULL)
8445 to->vval.v_string = NULL;
8446 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008447 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008448 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008449 if (from->v_type == VAR_FUNC)
8450 func_ref(to->vval.v_string);
8451 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008452 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008453 case VAR_PARTIAL:
8454 if (from->vval.v_partial == NULL)
8455 to->vval.v_partial = NULL;
8456 else
8457 {
8458 to->vval.v_partial = from->vval.v_partial;
8459 ++to->vval.v_partial->pt_refcount;
8460 }
8461 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008462 case VAR_BLOB:
8463 if (from->vval.v_blob == NULL)
8464 to->vval.v_blob = NULL;
8465 else
8466 {
8467 to->vval.v_blob = from->vval.v_blob;
8468 ++to->vval.v_blob->bv_refcount;
8469 }
8470 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008471 case VAR_LIST:
8472 if (from->vval.v_list == NULL)
8473 to->vval.v_list = NULL;
8474 else
8475 {
8476 to->vval.v_list = from->vval.v_list;
8477 ++to->vval.v_list->lv_refcount;
8478 }
8479 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008480 case VAR_DICT:
8481 if (from->vval.v_dict == NULL)
8482 to->vval.v_dict = NULL;
8483 else
8484 {
8485 to->vval.v_dict = from->vval.v_dict;
8486 ++to->vval.v_dict->dv_refcount;
8487 }
8488 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008489 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008490 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008491 break;
8492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 * Make a copy of an item.
8497 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008498 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8499 * reference to an already copied list/dict can be used.
8500 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008503item_copy(
8504 typval_T *from,
8505 typval_T *to,
8506 int deep,
8507 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008508{
8509 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008510 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008514 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008515 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 }
8517 ++recurse;
8518
8519 switch (from->v_type)
8520 {
8521 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008522 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 case VAR_STRING:
8524 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008525 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008526 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008527 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008528 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008529 copy_tv(from, to);
8530 break;
8531 case VAR_LIST:
8532 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008533 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008534 if (from->vval.v_list == NULL)
8535 to->vval.v_list = NULL;
8536 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8537 {
8538 /* use the copy made earlier */
8539 to->vval.v_list = from->vval.v_list->lv_copylist;
8540 ++to->vval.v_list->lv_refcount;
8541 }
8542 else
8543 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8544 if (to->vval.v_list == NULL)
8545 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008547 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008548 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008549 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008550 case VAR_DICT:
8551 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008552 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008553 if (from->vval.v_dict == NULL)
8554 to->vval.v_dict = NULL;
8555 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8556 {
8557 /* use the copy made earlier */
8558 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8559 ++to->vval.v_dict->dv_refcount;
8560 }
8561 else
8562 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8563 if (to->vval.v_dict == NULL)
8564 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008566 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008567 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008568 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569 }
8570 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008571 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572}
8573
8574/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 * This function is used by f_input() and f_inputdialog() functions. The third
8576 * argument to f_input() specifies the type of completion to use at the
8577 * prompt. The third argument to f_inputdialog() specifies the value to return
8578 * when the user cancels the prompt.
8579 */
8580 void
8581get_user_input(
8582 typval_T *argvars,
8583 typval_T *rettv,
8584 int inputdialog,
8585 int secret)
8586{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008587 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 char_u *p = NULL;
8589 int c;
8590 char_u buf[NUMBUFLEN];
8591 int cmd_silent_save = cmd_silent;
8592 char_u *defstr = (char_u *)"";
8593 int xp_type = EXPAND_NOTHING;
8594 char_u *xp_arg = NULL;
8595
8596 rettv->v_type = VAR_STRING;
8597 rettv->vval.v_string = NULL;
8598
8599#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008600 /* While starting up, there is no place to enter text. When running tests
8601 * with --not-a-term we assume feedkeys() will be used. */
8602 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 return;
8604#endif
8605
8606 cmd_silent = FALSE; /* Want to see the prompt. */
8607 if (prompt != NULL)
8608 {
8609 /* Only the part of the message after the last NL is considered as
8610 * prompt for the command line */
8611 p = vim_strrchr(prompt, '\n');
8612 if (p == NULL)
8613 p = prompt;
8614 else
8615 {
8616 ++p;
8617 c = *p;
8618 *p = NUL;
8619 msg_start();
8620 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008621 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 msg_didout = FALSE;
8623 msg_starthere();
8624 *p = c;
8625 }
8626 cmdline_row = msg_row;
8627
8628 if (argvars[1].v_type != VAR_UNKNOWN)
8629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008630 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 if (defstr != NULL)
8632 stuffReadbuffSpec(defstr);
8633
8634 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8635 {
8636 char_u *xp_name;
8637 int xp_namelen;
8638 long argt;
8639
8640 /* input() with a third argument: completion */
8641 rettv->vval.v_string = NULL;
8642
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008643 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 if (xp_name == NULL)
8645 return;
8646
8647 xp_namelen = (int)STRLEN(xp_name);
8648
8649 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8650 &xp_arg) == FAIL)
8651 return;
8652 }
8653 }
8654
8655 if (defstr != NULL)
8656 {
8657 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008658
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 ex_normal_busy = 0;
8660 rettv->vval.v_string =
8661 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8662 xp_type, xp_arg);
8663 ex_normal_busy = save_ex_normal_busy;
8664 }
8665 if (inputdialog && rettv->vval.v_string == NULL
8666 && argvars[1].v_type != VAR_UNKNOWN
8667 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008668 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 &argvars[2], buf));
8670
8671 vim_free(xp_arg);
8672
8673 /* since the user typed this, no need to wait for return */
8674 need_wait_return = FALSE;
8675 msg_didout = FALSE;
8676 }
8677 cmd_silent = cmd_silent_save;
8678}
8679
8680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 * ":echo expr1 ..." print each argument separated with a space, add a
8682 * newline at the end.
8683 * ":echon expr1 ..." print each argument plain.
8684 */
8685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008686ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008690 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 char_u *p;
8692 int needclr = TRUE;
8693 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008694 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008695 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008696 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697
8698 if (eap->skip)
8699 ++emsg_skip;
8700 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8701 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008702 /* If eval1() causes an error message the text from the command may
8703 * still need to be cleared. E.g., "echo 22,44". */
8704 need_clr_eos = needclr;
8705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 /*
8710 * Report the invalid expression unless the expression evaluation
8711 * has been cancelled due to an aborting error, an interrupt, or an
8712 * exception.
8713 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008714 if (!aborting() && did_emsg == did_emsg_before
8715 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008716 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008717 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 break;
8719 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008720 need_clr_eos = FALSE;
8721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 if (!eap->skip)
8723 {
8724 if (atstart)
8725 {
8726 atstart = FALSE;
8727 /* Call msg_start() after eval1(), evaluating the expression
8728 * may cause a message to appear. */
8729 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008730 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008731 /* Mark the saved text as finishing the line, so that what
8732 * follows is displayed on a new line when scrolling back
8733 * at the more prompt. */
8734 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 }
8738 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008739 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008740 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008741 if (p != NULL)
8742 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008744 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008746 if (*p != TAB && needclr)
8747 {
8748 /* remove any text still there from the command */
8749 msg_clr_eos();
8750 needclr = FALSE;
8751 }
8752 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 }
8754 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008755 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008756 if (has_mbyte)
8757 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008758 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008759
8760 (void)msg_outtrans_len_attr(p, i, echo_attr);
8761 p += i - 1;
8762 }
8763 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008764 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008767 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 arg = skipwhite(arg);
8771 }
8772 eap->nextcmd = check_nextcmd(arg);
8773
8774 if (eap->skip)
8775 --emsg_skip;
8776 else
8777 {
8778 /* remove text that may still be there from the command */
8779 if (needclr)
8780 msg_clr_eos();
8781 if (eap->cmdidx == CMD_echo)
8782 msg_end();
8783 }
8784}
8785
8786/*
8787 * ":echohl {name}".
8788 */
8789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008790ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008792 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795/*
8796 * ":execute expr1 ..." execute the result of an expression.
8797 * ":echomsg expr1 ..." Print a message
8798 * ":echoerr expr1 ..." Print an error
8799 * Each gets spaces around each argument and a newline at the end for
8800 * echo commands
8801 */
8802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008803ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 int ret = OK;
8808 char_u *p;
8809 garray_T ga;
8810 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008811 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 ga_init2(&ga, 1, 80);
8814
8815 if (eap->skip)
8816 ++emsg_skip;
8817 while (*arg != NUL && *arg != '|' && *arg != '\n')
8818 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008819 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8820 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
8823 if (!eap->skip)
8824 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008825 char_u buf[NUMBUFLEN];
8826
8827 if (eap->cmdidx == CMD_execute)
8828 p = tv_get_string_buf(&rettv, buf);
8829 else
8830 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 len = (int)STRLEN(p);
8832 if (ga_grow(&ga, len + 2) == FAIL)
8833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 ret = FAIL;
8836 break;
8837 }
8838 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 ga.ga_len += len;
8842 }
8843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 arg = skipwhite(arg);
8846 }
8847
8848 if (ret != FAIL && ga.ga_data != NULL)
8849 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008850 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8851 {
8852 /* Mark the already saved text as finishing the line, so that what
8853 * follows is displayed on a new line when scrolling back at the
8854 * more prompt. */
8855 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008856 }
8857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008859 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008860 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008861 out_flush();
8862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else if (eap->cmdidx == CMD_echoerr)
8864 {
8865 /* We don't want to abort following commands, restore did_emsg. */
8866 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008867 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 if (!force_abort)
8869 did_emsg = save_did_emsg;
8870 }
8871 else if (eap->cmdidx == CMD_execute)
8872 do_cmdline((char_u *)ga.ga_data,
8873 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8874 }
8875
8876 ga_clear(&ga);
8877
8878 if (eap->skip)
8879 --emsg_skip;
8880
8881 eap->nextcmd = check_nextcmd(arg);
8882}
8883
8884/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 * Find window specified by "vp" in tabpage "tp".
8886 */
8887 win_T *
8888find_win_by_nr(
8889 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008890 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008893 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 if (nr < 0)
8896 return NULL;
8897 if (nr == 0)
8898 return curwin;
8899
Bram Moolenaar29323592016-07-24 22:04:11 +02008900 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8901 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 if (nr >= LOWEST_WIN_ID)
8903 {
8904 if (wp->w_id == nr)
8905 return wp;
8906 }
8907 else if (--nr <= 0)
8908 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008909 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008911 {
8912#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008913 // check tab-local popup windows
8914 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008915 if (wp->w_id == nr)
8916 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008917 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008918 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8919 if (wp->w_id == nr)
8920 return wp;
8921#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008923 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925}
8926
8927/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008928 * Find a window: When using a Window ID in any tab page, when using a number
8929 * in the current tab page.
8930 */
8931 win_T *
8932find_win_by_nr_or_id(typval_T *vp)
8933{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008934 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008935
8936 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008937 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008938 return find_win_by_nr(vp, NULL);
8939}
8940
8941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008943 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 */
8945 win_T *
8946find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008947 typval_T *wvp, // VAR_UNKNOWN for current window
8948 typval_T *tvp, // VAR_UNKNOWN for current tab page
8949 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950{
8951 win_T *wp = NULL;
8952 tabpage_T *tp = NULL;
8953 long n;
8954
8955 if (wvp->v_type != VAR_UNKNOWN)
8956 {
8957 if (tvp->v_type != VAR_UNKNOWN)
8958 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008959 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960 if (n >= 0)
8961 tp = find_tabpage(n);
8962 }
8963 else
8964 tp = curtab;
8965
8966 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008967 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008969 if (wp == NULL && wvp->v_type == VAR_NUMBER
8970 && wvp->vval.v_number != -1)
8971 // A window with the specified number is not found
8972 tp = NULL;
8973 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 }
8975 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008976 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008978 tp = curtab;
8979 }
8980
8981 if (ptp != NULL)
8982 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983
8984 return wp;
8985}
8986
8987/*
8988 * getwinvar() and gettabwinvar()
8989 */
8990 void
8991getwinvar(
8992 typval_T *argvars,
8993 typval_T *rettv,
8994 int off) /* 1 for gettabwinvar() */
8995{
8996 win_T *win;
8997 char_u *varname;
8998 dictitem_T *v;
8999 tabpage_T *tp = NULL;
9000 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 win_T *oldcurwin;
9002 tabpage_T *oldtabpage;
9003 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009006 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 else
9008 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009010 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 ++emsg_off;
9012
9013 rettv->v_type = VAR_STRING;
9014 rettv->vval.v_string = NULL;
9015
9016 if (win != NULL && varname != NULL)
9017 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 /* Set curwin to be our win, temporarily. Also set the tabpage,
9019 * otherwise the window is not valid. Only do this when needed,
9020 * autocommands get blocked. */
9021 need_switch_win = !(tp == curtab && win == curwin);
9022 if (!need_switch_win
9023 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009025 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009027 if (varname[1] == NUL)
9028 {
9029 /* get all window-local options in a dict */
9030 dict_T *opts = get_winbuf_options(FALSE);
9031
9032 if (opts != NULL)
9033 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009034 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009035 done = TRUE;
9036 }
9037 }
9038 else if (get_option_tv(&varname, rettv, 1) == OK)
9039 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009040 done = TRUE;
9041 }
9042 else
9043 {
9044 /* Look up the variable. */
9045 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9046 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9047 varname, FALSE);
9048 if (v != NULL)
9049 {
9050 copy_tv(&v->di_tv, rettv);
9051 done = TRUE;
9052 }
9053 }
9054 }
9055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 if (need_switch_win)
9057 /* restore previous notion of curwin */
9058 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 }
9060
9061 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9062 /* use the default return value */
9063 copy_tv(&argvars[off + 2], rettv);
9064
9065 --emsg_off;
9066}
9067
9068/*
9069 * "setwinvar()" and "settabwinvar()" functions
9070 */
9071 void
9072setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9073{
9074 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 win_T *save_curwin;
9076 tabpage_T *save_curtab;
9077 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078 char_u *varname, *winvarname;
9079 typval_T *varp;
9080 char_u nbuf[NUMBUFLEN];
9081 tabpage_T *tp = NULL;
9082
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009083 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 return;
9085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009087 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 else
9089 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009091 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 varp = &argvars[off + 2];
9093
9094 if (win != NULL && varname != NULL && varp != NULL)
9095 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 need_switch_win = !(tp == curtab && win == curwin);
9097 if (!need_switch_win
9098 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 {
9100 if (*varname == '&')
9101 {
9102 long numval;
9103 char_u *strval;
9104 int error = FALSE;
9105
9106 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009107 numval = (long)tv_get_number_chk(varp, &error);
9108 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 if (!error && strval != NULL)
9110 set_option_value(varname, numval, strval, OPT_LOCAL);
9111 }
9112 else
9113 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009114 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 if (winvarname != NULL)
9116 {
9117 STRCPY(winvarname, "w:");
9118 STRCPY(winvarname + 2, varname);
9119 set_var(winvarname, varp, TRUE);
9120 vim_free(winvarname);
9121 }
9122 }
9123 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124 if (need_switch_win)
9125 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 }
9127}
9128
9129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9131 * "arg" points to the "&" or '+' when called, to "option" when returning.
9132 * Returns NULL when no option name found. Otherwise pointer to the char
9133 * after the option name.
9134 */
9135 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009136find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 char_u *p = *arg;
9139
9140 ++p;
9141 if (*p == 'g' && p[1] == ':')
9142 {
9143 *opt_flags = OPT_GLOBAL;
9144 p += 2;
9145 }
9146 else if (*p == 'l' && p[1] == ':')
9147 {
9148 *opt_flags = OPT_LOCAL;
9149 p += 2;
9150 }
9151 else
9152 *opt_flags = 0;
9153
9154 if (!ASCII_ISALPHA(*p))
9155 return NULL;
9156 *arg = p;
9157
9158 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9159 p += 4; /* termcap option */
9160 else
9161 while (ASCII_ISALPHA(*p))
9162 ++p;
9163 return p;
9164}
9165
9166/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009167 * Return the autoload script name for a function or variable name.
9168 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009170 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009171autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009172{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009173 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009174 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009175
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009176 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009177 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009178 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009179 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009180 STRCPY(scriptname, "autoload/");
9181 STRCAT(scriptname, name);
9182 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9183 STRCAT(scriptname, ".vim");
9184 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9185 *p = '/';
9186 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009187}
9188
9189/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009190 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009191 * Return TRUE if a package was loaded.
9192 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009194script_autoload(
9195 char_u *name,
9196 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009197{
9198 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009199 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009200 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009201 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009202
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009203 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009204 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009205 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009206 return FALSE;
9207
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009208 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009209
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009210 /* Find the name in the list of previously loaded package names. Skip
9211 * "autoload/", it's always the same. */
9212 for (i = 0; i < ga_loaded.ga_len; ++i)
9213 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9214 break;
9215 if (!reload && i < ga_loaded.ga_len)
9216 ret = FALSE; /* was loaded already */
9217 else
9218 {
9219 /* Remember the name if it wasn't loaded already. */
9220 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9221 {
9222 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9223 tofree = NULL;
9224 }
9225
9226 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009227 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009228 ret = TRUE;
9229 }
9230
9231 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009232 return ret;
9233}
9234
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9236typedef enum
9237{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009238 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9239 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9240 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241} var_flavour_T;
9242
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009244var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245{
9246 char_u *p = varname;
9247
9248 if (ASCII_ISUPPER(*p))
9249 {
9250 while (*(++p))
9251 if (ASCII_ISLOWER(*p))
9252 return VAR_FLAVOUR_SESSION;
9253 return VAR_FLAVOUR_VIMINFO;
9254 }
9255 else
9256 return VAR_FLAVOUR_DEFAULT;
9257}
9258#endif
9259
9260#if defined(FEAT_VIMINFO) || defined(PROTO)
9261/*
9262 * Restore global vars that start with a capital from the viminfo file
9263 */
9264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009265read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266{
9267 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009268 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009269 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009270 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
9272 if (!writing && (find_viminfo_parameter('!') != NULL))
9273 {
9274 tab = vim_strchr(virp->vir_line + 1, '\t');
9275 if (tab != NULL)
9276 {
9277 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009278 switch (*tab)
9279 {
9280 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009281#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009282 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009283#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009284 case 'D': type = VAR_DICT; break;
9285 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009286 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009287 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289
9290 tab = vim_strchr(tab, '\t');
9291 if (tab != NULL)
9292 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009293 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009294 if (type == VAR_STRING || type == VAR_DICT
9295 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009296 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009298#ifdef FEAT_FLOAT
9299 else if (type == VAR_FLOAT)
9300 (void)string2float(tab + 1, &tv.vval.v_float);
9301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009303 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009304 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009305 {
9306 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9307
9308 if (etv == NULL)
9309 /* Failed to parse back the dict or list, use it as a
9310 * string. */
9311 tv.v_type = VAR_STRING;
9312 else
9313 {
9314 vim_free(tv.vval.v_string);
9315 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009316 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009317 }
9318 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009319 else if (type == VAR_BLOB)
9320 {
9321 blob_T *blob = string2blob(tv.vval.v_string);
9322
9323 if (blob == NULL)
9324 // Failed to parse back the blob, use it as a string.
9325 tv.v_type = VAR_STRING;
9326 else
9327 {
9328 vim_free(tv.vval.v_string);
9329 tv.v_type = VAR_BLOB;
9330 tv.vval.v_blob = blob;
9331 }
9332 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009333
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009334 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009335 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009336 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009337 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009338
9339 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009340 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009341 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9342 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 }
9345 }
9346 }
9347
9348 return viminfo_readline(virp);
9349}
9350
9351/*
9352 * Write global vars that start with a capital to the viminfo file
9353 */
9354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
Bram Moolenaar33570922005-01-25 22:26:29 +00009357 hashitem_T *hi;
9358 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009359 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009360 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009361 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
9365 if (find_viminfo_parameter('!') == NULL)
9366 return;
9367
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009368 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009370 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009373 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009375 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 this_var = HI2DI(hi);
9377 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009379 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009380 {
9381 case VAR_STRING: s = "STR"; break;
9382 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009383 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009384 case VAR_DICT: s = "DIC"; break;
9385 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009386 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009387 case VAR_SPECIAL: s = "XPL"; break;
9388
9389 case VAR_UNKNOWN:
9390 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009391 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009392 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009393 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009394 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009395 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009397 if (this_var->di_tv.v_type == VAR_SPECIAL)
9398 {
9399 sprintf((char *)numbuf, "%ld",
9400 (long)this_var->di_tv.vval.v_number);
9401 p = numbuf;
9402 tofree = NULL;
9403 }
9404 else
9405 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406 if (p != NULL)
9407 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009408 vim_free(tofree);
9409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 }
9412}
9413#endif
9414
9415#if defined(FEAT_SESSION) || defined(PROTO)
9416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009417store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 hashitem_T *hi;
9420 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009421 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 char_u *p, *t;
9423
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009424 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009427 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009429 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 this_var = HI2DI(hi);
9431 if ((this_var->di_tv.v_type == VAR_NUMBER
9432 || this_var->di_tv.v_type == VAR_STRING)
9433 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009434 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009435 /* Escape special characters with a backslash. Turn a LF and
9436 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009437 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009438 (char_u *)"\\\"\n\r");
9439 if (p == NULL) /* out of memory */
9440 break;
9441 for (t = p; *t != NUL; ++t)
9442 if (*t == '\n')
9443 *t = 'n';
9444 else if (*t == '\r')
9445 *t = 'r';
9446 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 this_var->di_key,
9448 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9449 : ' ',
9450 p,
9451 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9452 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009453 || put_eol(fd) == FAIL)
9454 {
9455 vim_free(p);
9456 return FAIL;
9457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 vim_free(p);
9459 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009460#ifdef FEAT_FLOAT
9461 else if (this_var->di_tv.v_type == VAR_FLOAT
9462 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9463 {
9464 float_T f = this_var->di_tv.vval.v_float;
9465 int sign = ' ';
9466
9467 if (f < 0)
9468 {
9469 f = -f;
9470 sign = '-';
9471 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009472 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473 this_var->di_key, sign, f) < 0)
9474 || put_eol(fd) == FAIL)
9475 return FAIL;
9476 }
9477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 }
9479 }
9480 return OK;
9481}
9482#endif
9483
Bram Moolenaar661b1822005-07-28 22:36:45 +00009484/*
9485 * Display script name where an item was last set.
9486 * Should only be invoked when 'verbose' is non-zero.
9487 */
9488 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009489last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009490{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009491 char_u *p;
9492
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009493 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009494 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009495 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009496 if (p != NULL)
9497 {
9498 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009499 msg_puts(_("\n\tLast set from "));
9500 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009501 if (script_ctx.sc_lnum > 0)
9502 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009503 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009504 msg_outnum((long)script_ctx.sc_lnum);
9505 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009506 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009507 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009508 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009509 }
9510}
9511
Bram Moolenaar61be3762019-03-19 23:04:17 +01009512/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009513 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9514 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009515 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009517reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009518{
9519 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9520 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009521 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9522 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009523 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009524 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009525}
9526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527/*
9528 * Prepare "gap" for an assert error and add the sourcing position.
9529 */
9530 void
9531prepare_assert_error(garray_T *gap)
9532{
9533 char buf[NUMBUFLEN];
9534
9535 ga_init2(gap, 1, 100);
9536 if (sourcing_name != NULL)
9537 {
9538 ga_concat(gap, sourcing_name);
9539 if (sourcing_lnum > 0)
9540 ga_concat(gap, (char_u *)" ");
9541 }
9542 if (sourcing_lnum > 0)
9543 {
9544 sprintf(buf, "line %ld", (long)sourcing_lnum);
9545 ga_concat(gap, (char_u *)buf);
9546 }
9547 if (sourcing_name != NULL || sourcing_lnum > 0)
9548 ga_concat(gap, (char_u *)": ");
9549}
9550
9551/*
9552 * Add an assert error to v:errors.
9553 */
9554 void
9555assert_error(garray_T *gap)
9556{
9557 struct vimvar *vp = &vimvars[VV_ERRORS];
9558
9559 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9560 /* Make sure v:errors is a list. */
9561 set_vim_var_list(VV_ERRORS, list_alloc());
9562 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9563}
9564
Bram Moolenaar65a54642018-04-28 16:56:53 +02009565 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566assert_equal_common(typval_T *argvars, assert_type_T atype)
9567{
9568 garray_T ga;
9569
9570 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9571 != (atype == ASSERT_EQUAL))
9572 {
9573 prepare_assert_error(&ga);
9574 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9575 atype);
9576 assert_error(&ga);
9577 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009578 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009579 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009580 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581}
9582
Bram Moolenaar65a54642018-04-28 16:56:53 +02009583 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009584assert_equalfile(typval_T *argvars)
9585{
9586 char_u buf1[NUMBUFLEN];
9587 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009588 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9589 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009590 garray_T ga;
9591 FILE *fd1;
9592 FILE *fd2;
9593
9594 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009595 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009596
9597 IObuff[0] = NUL;
9598 fd1 = mch_fopen((char *)fname1, READBIN);
9599 if (fd1 == NULL)
9600 {
9601 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9602 }
9603 else
9604 {
9605 fd2 = mch_fopen((char *)fname2, READBIN);
9606 if (fd2 == NULL)
9607 {
9608 fclose(fd1);
9609 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9610 }
9611 else
9612 {
9613 int c1, c2;
9614 long count = 0;
9615
9616 for (;;)
9617 {
9618 c1 = fgetc(fd1);
9619 c2 = fgetc(fd2);
9620 if (c1 == EOF)
9621 {
9622 if (c2 != EOF)
9623 STRCPY(IObuff, "first file is shorter");
9624 break;
9625 }
9626 else if (c2 == EOF)
9627 {
9628 STRCPY(IObuff, "second file is shorter");
9629 break;
9630 }
9631 else if (c1 != c2)
9632 {
9633 vim_snprintf((char *)IObuff, IOSIZE,
9634 "difference at byte %ld", count);
9635 break;
9636 }
9637 ++count;
9638 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009639 fclose(fd1);
9640 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009641 }
9642 }
9643 if (IObuff[0] != NUL)
9644 {
9645 prepare_assert_error(&ga);
9646 ga_concat(&ga, IObuff);
9647 assert_error(&ga);
9648 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009649 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009650 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009651 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009652}
9653
Bram Moolenaar65a54642018-04-28 16:56:53 +02009654 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655assert_match_common(typval_T *argvars, assert_type_T atype)
9656{
9657 garray_T ga;
9658 char_u buf1[NUMBUFLEN];
9659 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009660 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9661 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009662
9663 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009664 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9666 {
9667 prepare_assert_error(&ga);
9668 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9669 atype);
9670 assert_error(&ga);
9671 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009672 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009673 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009674 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009675}
9676
Bram Moolenaar65a54642018-04-28 16:56:53 +02009677 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009678assert_inrange(typval_T *argvars)
9679{
9680 garray_T ga;
9681 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009682 char_u *tofree;
9683 char msg[200];
9684 char_u numbuf[NUMBUFLEN];
9685
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009686#ifdef FEAT_FLOAT
9687 if (argvars[0].v_type == VAR_FLOAT
9688 || argvars[1].v_type == VAR_FLOAT
9689 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009690 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009691 float_T flower = tv_get_float(&argvars[0]);
9692 float_T fupper = tv_get_float(&argvars[1]);
9693 float_T factual = tv_get_float(&argvars[2]);
9694
9695 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009696 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009697 prepare_assert_error(&ga);
9698 if (argvars[3].v_type != VAR_UNKNOWN)
9699 {
9700 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9701 vim_free(tofree);
9702 }
9703 else
9704 {
9705 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9706 flower, fupper, factual);
9707 ga_concat(&ga, (char_u *)msg);
9708 }
9709 assert_error(&ga);
9710 ga_clear(&ga);
9711 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009712 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009713 }
9714 else
9715#endif
9716 {
9717 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9718 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9719 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9720
9721 if (error)
9722 return 0;
9723 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009724 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009725 prepare_assert_error(&ga);
9726 if (argvars[3].v_type != VAR_UNKNOWN)
9727 {
9728 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9729 vim_free(tofree);
9730 }
9731 else
9732 {
9733 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009734 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009735 ga_concat(&ga, (char_u *)msg);
9736 }
9737 assert_error(&ga);
9738 ga_clear(&ga);
9739 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009740 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009741 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009742 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009743}
9744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745/*
9746 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009747 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009748 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009749 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750assert_bool(typval_T *argvars, int isTrue)
9751{
9752 int error = FALSE;
9753 garray_T ga;
9754
9755 if (argvars[0].v_type == VAR_SPECIAL
9756 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009757 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009759 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760 || error)
9761 {
9762 prepare_assert_error(&ga);
9763 fill_assert_error(&ga, &argvars[1],
9764 (char_u *)(isTrue ? "True" : "False"),
9765 NULL, &argvars[0], ASSERT_OTHER);
9766 assert_error(&ga);
9767 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009768 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009770 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771}
9772
Bram Moolenaar65a54642018-04-28 16:56:53 +02009773 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009774assert_report(typval_T *argvars)
9775{
9776 garray_T ga;
9777
9778 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009779 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009780 assert_error(&ga);
9781 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009782 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009783}
9784
Bram Moolenaar65a54642018-04-28 16:56:53 +02009785 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786assert_exception(typval_T *argvars)
9787{
9788 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009789 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790
9791 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9792 {
9793 prepare_assert_error(&ga);
9794 ga_concat(&ga, (char_u *)"v:exception is not set");
9795 assert_error(&ga);
9796 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009797 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 }
9799 else if (error != NULL
9800 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9801 {
9802 prepare_assert_error(&ga);
9803 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9804 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9805 assert_error(&ga);
9806 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009807 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009809 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810}
9811
Bram Moolenaar65a54642018-04-28 16:56:53 +02009812 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009813assert_beeps(typval_T *argvars)
9814{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009815 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009816 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009817 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009818
9819 called_vim_beep = FALSE;
9820 suppress_errthrow = TRUE;
9821 emsg_silent = FALSE;
9822 do_cmdline_cmd(cmd);
9823 if (!called_vim_beep)
9824 {
9825 prepare_assert_error(&ga);
9826 ga_concat(&ga, (char_u *)"command did not beep: ");
9827 ga_concat(&ga, cmd);
9828 assert_error(&ga);
9829 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009830 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009831 }
9832
9833 suppress_errthrow = FALSE;
9834 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009835 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009836}
9837
Bram Moolenaar25190db2019-05-04 15:05:28 +02009838 static void
9839assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9840{
9841 char_u *tofree;
9842 char_u numbuf[NUMBUFLEN];
9843
9844 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9845 {
9846 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9847 vim_free(tofree);
9848 }
9849 else
9850 ga_concat(gap, cmd);
9851}
9852
Bram Moolenaar65a54642018-04-28 16:56:53 +02009853 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009854assert_fails(typval_T *argvars)
9855{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009856 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009857 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009858 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859
9860 called_emsg = FALSE;
9861 suppress_errthrow = TRUE;
9862 emsg_silent = TRUE;
9863 do_cmdline_cmd(cmd);
9864 if (!called_emsg)
9865 {
9866 prepare_assert_error(&ga);
9867 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009868 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 assert_error(&ga);
9870 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009871 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 }
9873 else if (argvars[1].v_type != VAR_UNKNOWN)
9874 {
9875 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009876 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009877
9878 if (error == NULL
9879 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9880 {
9881 prepare_assert_error(&ga);
9882 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9883 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009884 ga_concat(&ga, (char_u *)": ");
9885 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009886 assert_error(&ga);
9887 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009888 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 }
9890 }
9891
9892 called_emsg = FALSE;
9893 suppress_errthrow = FALSE;
9894 emsg_silent = FALSE;
9895 emsg_on_display = FALSE;
9896 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009897 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898}
9899
9900/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009901 * Append "p[clen]" to "gap", escaping unprintable characters.
9902 * Changes NL to \n, CR to \r, etc.
9903 */
9904 static void
9905ga_concat_esc(garray_T *gap, char_u *p, int clen)
9906{
9907 char_u buf[NUMBUFLEN];
9908
9909 if (clen > 1)
9910 {
9911 mch_memmove(buf, p, clen);
9912 buf[clen] = NUL;
9913 ga_concat(gap, buf);
9914 }
9915 else switch (*p)
9916 {
9917 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9918 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9919 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9920 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9921 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9922 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9923 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9924 default:
9925 if (*p < ' ')
9926 {
9927 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9928 ga_concat(gap, buf);
9929 }
9930 else
9931 ga_append(gap, *p);
9932 break;
9933 }
9934}
9935
9936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 * Append "str" to "gap", escaping unprintable characters.
9938 * Changes NL to \n, CR to \r, etc.
9939 */
9940 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009941ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009942{
9943 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009944 char_u *s;
9945 int c;
9946 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009947 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009948 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949
9950 if (str == NULL)
9951 {
9952 ga_concat(gap, (char_u *)"NULL");
9953 return;
9954 }
9955
9956 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009957 {
9958 same_len = 1;
9959 s = p;
9960 c = mb_ptr2char_adv(&s);
9961 clen = s - p;
9962 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009964 ++same_len;
9965 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009967 if (same_len > 20)
9968 {
9969 ga_concat(gap, (char_u *)"\\[");
9970 ga_concat_esc(gap, p, clen);
9971 ga_concat(gap, (char_u *)" occurs ");
9972 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9973 ga_concat(gap, buf);
9974 ga_concat(gap, (char_u *)" times]");
9975 p = s - 1;
9976 }
9977 else
9978 ga_concat_esc(gap, p, clen);
9979 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980}
9981
9982/*
9983 * Fill "gap" with information about an assert error.
9984 */
9985 void
9986fill_assert_error(
9987 garray_T *gap,
9988 typval_T *opt_msg_tv,
9989 char_u *exp_str,
9990 typval_T *exp_tv,
9991 typval_T *got_tv,
9992 assert_type_T atype)
9993{
9994 char_u numbuf[NUMBUFLEN];
9995 char_u *tofree;
9996
9997 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9998 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009999 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
10000 vim_free(tofree);
10001 ga_concat(gap, (char_u *)": ");
10002 }
10003
10004 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
10005 ga_concat(gap, (char_u *)"Pattern ");
10006 else if (atype == ASSERT_NOTEQUAL)
10007 ga_concat(gap, (char_u *)"Expected not equal to ");
10008 else
10009 ga_concat(gap, (char_u *)"Expected ");
10010 if (exp_str == NULL)
10011 {
Bram Moolenaar86576712019-01-25 20:48:33 +010010012 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010013 vim_free(tofree);
10014 }
10015 else
Bram Moolenaar86576712019-01-25 20:48:33 +010010016 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010017 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010018 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010019 if (atype == ASSERT_MATCH)
10020 ga_concat(gap, (char_u *)" does not match ");
10021 else if (atype == ASSERT_NOTMATCH)
10022 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010023 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010024 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +010010025 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010026 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 }
10028}
10029
Bram Moolenaar31988702018-02-13 12:57:42 +010010030/*
10031 * Compare "typ1" and "typ2". Put the result in "typ1".
10032 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010033 int
10034typval_compare(
10035 typval_T *typ1, /* first operand */
10036 typval_T *typ2, /* second operand */
10037 exptype_T type, /* operator */
10038 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +010010039 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010040{
10041 int i;
10042 varnumber_T n1, n2;
10043 char_u *s1, *s2;
10044 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
10045
Bram Moolenaar31988702018-02-13 12:57:42 +010010046 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010047 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010048 /* For "is" a different type always means FALSE, for "notis"
10049 * it means TRUE. */
10050 n1 = (type == TYPE_NEQUAL);
10051 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010052 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
10053 {
10054 if (type_is)
10055 {
10056 n1 = (typ1->v_type == typ2->v_type
10057 && typ1->vval.v_blob == typ2->vval.v_blob);
10058 if (type == TYPE_NEQUAL)
10059 n1 = !n1;
10060 }
10061 else if (typ1->v_type != typ2->v_type
10062 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10063 {
10064 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010065 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010066 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010067 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010068 clear_tv(typ1);
10069 return FAIL;
10070 }
10071 else
10072 {
10073 // Compare two Blobs for being equal or unequal.
10074 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
10075 if (type == TYPE_NEQUAL)
10076 n1 = !n1;
10077 }
10078 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010079 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
10080 {
10081 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010082 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010083 n1 = (typ1->v_type == typ2->v_type
10084 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010085 if (type == TYPE_NEQUAL)
10086 n1 = !n1;
10087 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010088 else if (typ1->v_type != typ2->v_type
10089 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010090 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010091 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010092 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010093 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010094 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010095 clear_tv(typ1);
10096 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010097 }
10098 else
10099 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010100 /* Compare two Lists for being equal or unequal. */
10101 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10102 ic, FALSE);
10103 if (type == TYPE_NEQUAL)
10104 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010105 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010106 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010107
10108 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10109 {
10110 if (type_is)
10111 {
10112 n1 = (typ1->v_type == typ2->v_type
10113 && typ1->vval.v_dict == typ2->vval.v_dict);
10114 if (type == TYPE_NEQUAL)
10115 n1 = !n1;
10116 }
10117 else if (typ1->v_type != typ2->v_type
10118 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10119 {
10120 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010121 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010122 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010123 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010124 clear_tv(typ1);
10125 return FAIL;
10126 }
10127 else
10128 {
10129 /* Compare two Dictionaries for being equal or unequal. */
10130 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10131 ic, FALSE);
10132 if (type == TYPE_NEQUAL)
10133 n1 = !n1;
10134 }
10135 }
10136
10137 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10138 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10139 {
10140 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010142 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010143 clear_tv(typ1);
10144 return FAIL;
10145 }
10146 if ((typ1->v_type == VAR_PARTIAL
10147 && typ1->vval.v_partial == NULL)
10148 || (typ2->v_type == VAR_PARTIAL
10149 && typ2->vval.v_partial == NULL))
10150 /* when a partial is NULL assume not equal */
10151 n1 = FALSE;
10152 else if (type_is)
10153 {
10154 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10155 /* strings are considered the same if their value is
10156 * the same */
10157 n1 = tv_equal(typ1, typ2, ic, FALSE);
10158 else if (typ1->v_type == VAR_PARTIAL
10159 && typ2->v_type == VAR_PARTIAL)
10160 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10161 else
10162 n1 = FALSE;
10163 }
10164 else
10165 n1 = tv_equal(typ1, typ2, ic, FALSE);
10166 if (type == TYPE_NEQUAL)
10167 n1 = !n1;
10168 }
10169
10170#ifdef FEAT_FLOAT
10171 /*
10172 * If one of the two variables is a float, compare as a float.
10173 * When using "=~" or "!~", always compare as string.
10174 */
10175 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10176 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10177 {
10178 float_T f1, f2;
10179
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010180 f1 = tv_get_float(typ1);
10181 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010182 n1 = FALSE;
10183 switch (type)
10184 {
10185 case TYPE_EQUAL: n1 = (f1 == f2); break;
10186 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10187 case TYPE_GREATER: n1 = (f1 > f2); break;
10188 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10189 case TYPE_SMALLER: n1 = (f1 < f2); break;
10190 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10191 case TYPE_UNKNOWN:
10192 case TYPE_MATCH:
10193 case TYPE_NOMATCH: break; /* avoid gcc warning */
10194 }
10195 }
10196#endif
10197
10198 /*
10199 * If one of the two variables is a number, compare as a number.
10200 * When using "=~" or "!~", always compare as string.
10201 */
10202 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10203 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010205 n1 = tv_get_number(typ1);
10206 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010207 switch (type)
10208 {
10209 case TYPE_EQUAL: n1 = (n1 == n2); break;
10210 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10211 case TYPE_GREATER: n1 = (n1 > n2); break;
10212 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10213 case TYPE_SMALLER: n1 = (n1 < n2); break;
10214 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10215 case TYPE_UNKNOWN:
10216 case TYPE_MATCH:
10217 case TYPE_NOMATCH: break; /* avoid gcc warning */
10218 }
10219 }
10220 else
10221 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010222 s1 = tv_get_string_buf(typ1, buf1);
10223 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010224 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10225 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10226 else
10227 i = 0;
10228 n1 = FALSE;
10229 switch (type)
10230 {
10231 case TYPE_EQUAL: n1 = (i == 0); break;
10232 case TYPE_NEQUAL: n1 = (i != 0); break;
10233 case TYPE_GREATER: n1 = (i > 0); break;
10234 case TYPE_GEQUAL: n1 = (i >= 0); break;
10235 case TYPE_SMALLER: n1 = (i < 0); break;
10236 case TYPE_SEQUAL: n1 = (i <= 0); break;
10237
10238 case TYPE_MATCH:
10239 case TYPE_NOMATCH:
10240 n1 = pattern_match(s2, s1, ic);
10241 if (type == TYPE_NOMATCH)
10242 n1 = !n1;
10243 break;
10244
10245 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10246 }
10247 }
10248 clear_tv(typ1);
10249 typ1->v_type = VAR_NUMBER;
10250 typ1->vval.v_number = n1;
10251
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010252 return OK;
10253}
10254
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010255 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010256typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010257{
10258 char_u *tofree;
10259 char_u numbuf[NUMBUFLEN];
10260 char_u *ret = NULL;
10261
10262 if (arg == NULL)
10263 return vim_strsave((char_u *)"(does not exist)");
10264 ret = tv2string(arg, &tofree, numbuf, 0);
10265 /* Make a copy if we have a value but it's not in allocated memory. */
10266 if (ret != NULL && tofree == NULL)
10267 ret = vim_strsave(ret);
10268 return ret;
10269}
10270
10271 int
10272var_exists(char_u *var)
10273{
10274 char_u *name;
10275 char_u *tofree;
10276 typval_T tv;
10277 int len = 0;
10278 int n = FALSE;
10279
10280 /* get_name_len() takes care of expanding curly braces */
10281 name = var;
10282 len = get_name_len(&var, &tofree, TRUE, FALSE);
10283 if (len > 0)
10284 {
10285 if (tofree != NULL)
10286 name = tofree;
10287 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10288 if (n)
10289 {
10290 /* handle d.key, l[idx], f(expr) */
10291 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10292 if (n)
10293 clear_tv(&tv);
10294 }
10295 }
10296 if (*var != NUL)
10297 n = FALSE;
10298
10299 vim_free(tofree);
10300 return n;
10301}
10302
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303#endif /* FEAT_EVAL */
10304
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010306#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307
Bram Moolenaar4f974752019-02-17 17:44:42 +010010308#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309/*
10310 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312
10313/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010314 * Get the short path (8.3) for the filename in "fnamep".
10315 * Only works for a valid file name.
10316 * When the path gets longer "fnamep" is changed and the allocated buffer
10317 * is put in "bufp".
10318 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10319 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 */
10321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010322get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010324 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 char_u *newbuf;
10326
10327 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010328 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (l > len - 1)
10330 {
10331 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010332 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 newbuf = vim_strnsave(*fnamep, l);
10334 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336
10337 vim_free(*bufp);
10338 *fnamep = *bufp = newbuf;
10339
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010340 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010341 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 }
10343
10344 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010345 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346}
10347
10348/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010349 * Get the short path (8.3) for the filename in "fname". The converted
10350 * path is returned in "bufp".
10351 *
10352 * Some of the directories specified in "fname" may not exist. This function
10353 * will shorten the existing directories at the beginning of the path and then
10354 * append the remaining non-existing path.
10355 *
10356 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010357 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010358 * bufp - Pointer to an allocated buffer for the filename.
10359 * fnamelen - Length of the filename pointed to by fname
10360 *
10361 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 */
10363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010364shortpath_for_invalid_fname(
10365 char_u **fname,
10366 char_u **bufp,
10367 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010369 char_u *short_fname, *save_fname, *pbuf_unused;
10370 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010372 int old_len, len;
10373 int new_len, sfx_len;
10374 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375
10376 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010377 old_len = *fnamelen;
10378 save_fname = vim_strnsave(*fname, old_len);
10379 pbuf_unused = NULL;
10380 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010382 endp = save_fname + old_len - 1; /* Find the end of the copy */
10383 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010385 /*
10386 * Try shortening the supplied path till it succeeds by removing one
10387 * directory at a time from the tail of the path.
10388 */
10389 len = 0;
10390 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010392 /* go back one path-separator */
10393 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10394 --endp;
10395 if (endp <= save_fname)
10396 break; /* processed the complete path */
10397
10398 /*
10399 * Replace the path separator with a NUL and try to shorten the
10400 * resulting path.
10401 */
10402 ch = *endp;
10403 *endp = 0;
10404 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010405 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010406 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10407 {
10408 retval = FAIL;
10409 goto theend;
10410 }
10411 *endp = ch; /* preserve the string */
10412
10413 if (len > 0)
10414 break; /* successfully shortened the path */
10415
10416 /* failed to shorten the path. Skip the path separator */
10417 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 }
10419
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010420 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010422 /*
10423 * Succeeded in shortening the path. Now concatenate the shortened
10424 * path with the remaining path at the tail.
10425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010427 /* Compute the length of the new path. */
10428 sfx_len = (int)(save_endp - endp) + 1;
10429 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010431 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010433 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010435 /* There is not enough space in the currently allocated string,
10436 * copy it to a buffer big enough. */
10437 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010439 {
10440 retval = FAIL;
10441 goto theend;
10442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 }
10444 else
10445 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010446 /* Transfer short_fname to the main buffer (it's big enough),
10447 * unless get_short_pathname() did its work in-place. */
10448 *fname = *bufp = save_fname;
10449 if (short_fname != save_fname)
10450 vim_strncpy(save_fname, short_fname, len);
10451 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010453
10454 /* concat the not-shortened part of the path */
10455 vim_strncpy(*fname + len, endp, sfx_len);
10456 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010458
10459theend:
10460 vim_free(pbuf_unused);
10461 vim_free(save_fname);
10462
10463 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
10467 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010468 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 */
10470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010471shortpath_for_partial(
10472 char_u **fnamep,
10473 char_u **bufp,
10474 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 int sepcount, len, tflen;
10477 char_u *p;
10478 char_u *pbuf, *tfname;
10479 int hasTilde;
10480
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010481 /* Count up the path separators from the RHS.. so we know which part
10482 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010484 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 if (vim_ispathsep(*p))
10486 ++sepcount;
10487
10488 /* Need full path first (use expand_env() to remove a "~/") */
10489 hasTilde = (**fnamep == '~');
10490 if (hasTilde)
10491 pbuf = tfname = expand_env_save(*fnamep);
10492 else
10493 pbuf = tfname = FullName_save(*fnamep, FALSE);
10494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010495 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010497 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
10500 if (len == 0)
10501 {
10502 /* Don't have a valid filename, so shorten the rest of the
10503 * path if we can. This CAN give us invalid 8.3 filenames, but
10504 * there's not a lot of point in guessing what it might be.
10505 */
10506 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010507 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10508 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
10510
10511 /* Count the paths backward to find the beginning of the desired string. */
10512 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010513 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010514 if (has_mbyte)
10515 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (vim_ispathsep(*p))
10517 {
10518 if (sepcount == 0 || (hasTilde && sepcount == 1))
10519 break;
10520 else
10521 sepcount --;
10522 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (hasTilde)
10525 {
10526 --p;
10527 if (p >= tfname)
10528 *p = '~';
10529 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010530 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 }
10532 else
10533 ++p;
10534
10535 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10536 vim_free(*bufp);
10537 *fnamelen = (int)STRLEN(p);
10538 *bufp = pbuf;
10539 *fnamep = p;
10540
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010541 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010543#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544
10545/*
10546 * Adjust a filename, according to a string of modifiers.
10547 * *fnamep must be NUL terminated when called. When returning, the length is
10548 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010549 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 * When there is an error, *fnamep is set to NULL.
10551 */
10552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010553modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010554 char_u *src, // string with modifiers
10555 int tilde_file, // "~" is a file name, not $HOME
10556 int *usedlen, // characters after src that are used
10557 char_u **fnamep, // file name so far
10558 char_u **bufp, // buffer for allocated file name or NULL
10559 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
10561 int valid = 0;
10562 char_u *tail;
10563 char_u *s, *p, *pbuf;
10564 char_u dirname[MAXPATHL];
10565 int c;
10566 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010567#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010568 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 int has_shortname = 0;
10570#endif
10571
10572repeat:
10573 /* ":p" - full path/file_name */
10574 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10575 {
10576 has_fullname = 1;
10577
10578 valid |= VALID_PATH;
10579 *usedlen += 2;
10580
10581 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10582 if ((*fnamep)[0] == '~'
10583#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10584 && ((*fnamep)[1] == '/'
10585# ifdef BACKSLASH_IN_FILENAME
10586 || (*fnamep)[1] == '\\'
10587# endif
10588 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010590 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 )
10592 {
10593 *fnamep = expand_env_save(*fnamep);
10594 vim_free(*bufp); /* free any allocated file name */
10595 *bufp = *fnamep;
10596 if (*fnamep == NULL)
10597 return -1;
10598 }
10599
10600 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010601 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 {
10603 if (vim_ispathsep(*p)
10604 && p[1] == '.'
10605 && (p[2] == NUL
10606 || vim_ispathsep(p[2])
10607 || (p[2] == '.'
10608 && (p[3] == NUL || vim_ispathsep(p[3])))))
10609 break;
10610 }
10611
10612 /* FullName_save() is slow, don't use it when not needed. */
10613 if (*p != NUL || !vim_isAbsName(*fnamep))
10614 {
10615 *fnamep = FullName_save(*fnamep, *p != NUL);
10616 vim_free(*bufp); /* free any allocated file name */
10617 *bufp = *fnamep;
10618 if (*fnamep == NULL)
10619 return -1;
10620 }
10621
Bram Moolenaar4f974752019-02-17 17:44:42 +010010622#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010623# if _WIN32_WINNT >= 0x0500
10624 if (vim_strchr(*fnamep, '~') != NULL)
10625 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010626 // Expand 8.3 filename to full path. Needed to make sure the same
10627 // file does not have two different names.
10628 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10629 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10630 WCHAR buf[_MAX_PATH];
10631
10632 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010633 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010634 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010635 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010636 char_u *p = utf16_to_enc(buf, NULL);
10637
10638 if (p != NULL)
10639 {
10640 vim_free(*bufp); // free any allocated file name
10641 *bufp = *fnamep = p;
10642 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010643 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010644 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010645 }
10646 }
10647# endif
10648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 /* Append a path separator to a directory. */
10650 if (mch_isdir(*fnamep))
10651 {
10652 /* Make room for one or two extra characters. */
10653 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10654 vim_free(*bufp); /* free any allocated file name */
10655 *bufp = *fnamep;
10656 if (*fnamep == NULL)
10657 return -1;
10658 add_pathsep(*fnamep);
10659 }
10660 }
10661
10662 /* ":." - path relative to the current directory */
10663 /* ":~" - path relative to the home directory */
10664 /* ":8" - shortname path - postponed till after */
10665 while (src[*usedlen] == ':'
10666 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10667 {
10668 *usedlen += 2;
10669 if (c == '8')
10670 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010671#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 has_shortname = 1; /* Postpone this. */
10673#endif
10674 continue;
10675 }
10676 pbuf = NULL;
10677 /* Need full path first (use expand_env() to remove a "~/") */
10678 if (!has_fullname)
10679 {
10680 if (c == '.' && **fnamep == '~')
10681 p = pbuf = expand_env_save(*fnamep);
10682 else
10683 p = pbuf = FullName_save(*fnamep, FALSE);
10684 }
10685 else
10686 p = *fnamep;
10687
10688 has_fullname = 0;
10689
10690 if (p != NULL)
10691 {
10692 if (c == '.')
10693 {
10694 mch_dirname(dirname, MAXPATHL);
10695 s = shorten_fname(p, dirname);
10696 if (s != NULL)
10697 {
10698 *fnamep = s;
10699 if (pbuf != NULL)
10700 {
10701 vim_free(*bufp); /* free any allocated file name */
10702 *bufp = pbuf;
10703 pbuf = NULL;
10704 }
10705 }
10706 }
10707 else
10708 {
10709 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10710 /* Only replace it when it starts with '~' */
10711 if (*dirname == '~')
10712 {
10713 s = vim_strsave(dirname);
10714 if (s != NULL)
10715 {
10716 *fnamep = s;
10717 vim_free(*bufp);
10718 *bufp = s;
10719 }
10720 }
10721 }
10722 vim_free(pbuf);
10723 }
10724 }
10725
10726 tail = gettail(*fnamep);
10727 *fnamelen = (int)STRLEN(*fnamep);
10728
10729 /* ":h" - head, remove "/file_name", can be repeated */
10730 /* Don't remove the first "/" or "c:\" */
10731 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10732 {
10733 valid |= VALID_HEAD;
10734 *usedlen += 2;
10735 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010736 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010737 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 *fnamelen = (int)(tail - *fnamep);
10739#ifdef VMS
10740 if (*fnamelen > 0)
10741 *fnamelen += 1; /* the path separator is part of the path */
10742#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010743 if (*fnamelen == 0)
10744 {
10745 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10746 p = vim_strsave((char_u *)".");
10747 if (p == NULL)
10748 return -1;
10749 vim_free(*bufp);
10750 *bufp = *fnamep = tail = p;
10751 *fnamelen = 1;
10752 }
10753 else
10754 {
10755 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010756 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 }
10759
10760 /* ":8" - shortname */
10761 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10762 {
10763 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010764#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 has_shortname = 1;
10766#endif
10767 }
10768
Bram Moolenaar4f974752019-02-17 17:44:42 +010010769#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010770 /*
10771 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 */
10773 if (has_shortname)
10774 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010775 /* Copy the string if it is shortened by :h and when it wasn't copied
10776 * yet, because we are going to change it in place. Avoids changing
10777 * the buffer name for "%:8". */
10778 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 {
10780 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010781 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 return -1;
10783 vim_free(*bufp);
10784 *bufp = *fnamep = p;
10785 }
10786
10787 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010788 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 if (!has_fullname && !vim_isAbsName(*fnamep))
10790 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010791 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 return -1;
10793 }
10794 else
10795 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010796 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797
Bram Moolenaardc935552011-08-17 15:23:23 +020010798 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010800 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 return -1;
10802
10803 if (l == 0)
10804 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010805 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010807 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 return -1;
10809 }
10810 *fnamelen = l;
10811 }
10812 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010813#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
10815 /* ":t" - tail, just the basename */
10816 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10817 {
10818 *usedlen += 2;
10819 *fnamelen -= (int)(tail - *fnamep);
10820 *fnamep = tail;
10821 }
10822
10823 /* ":e" - extension, can be repeated */
10824 /* ":r" - root, without extension, can be repeated */
10825 while (src[*usedlen] == ':'
10826 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10827 {
10828 /* find a '.' in the tail:
10829 * - for second :e: before the current fname
10830 * - otherwise: The last '.'
10831 */
10832 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10833 s = *fnamep - 2;
10834 else
10835 s = *fnamep + *fnamelen - 1;
10836 for ( ; s > tail; --s)
10837 if (s[0] == '.')
10838 break;
10839 if (src[*usedlen + 1] == 'e') /* :e */
10840 {
10841 if (s > tail)
10842 {
10843 *fnamelen += (int)(*fnamep - (s + 1));
10844 *fnamep = s + 1;
10845#ifdef VMS
10846 /* cut version from the extension */
10847 s = *fnamep + *fnamelen - 1;
10848 for ( ; s > *fnamep; --s)
10849 if (s[0] == ';')
10850 break;
10851 if (s > *fnamep)
10852 *fnamelen = s - *fnamep;
10853#endif
10854 }
10855 else if (*fnamep <= tail)
10856 *fnamelen = 0;
10857 }
10858 else /* :r */
10859 {
10860 if (s > tail) /* remove one extension */
10861 *fnamelen = (int)(s - *fnamep);
10862 }
10863 *usedlen += 2;
10864 }
10865
10866 /* ":s?pat?foo?" - substitute */
10867 /* ":gs?pat?foo?" - global substitute */
10868 if (src[*usedlen] == ':'
10869 && (src[*usedlen + 1] == 's'
10870 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10871 {
10872 char_u *str;
10873 char_u *pat;
10874 char_u *sub;
10875 int sep;
10876 char_u *flags;
10877 int didit = FALSE;
10878
10879 flags = (char_u *)"";
10880 s = src + *usedlen + 2;
10881 if (src[*usedlen + 1] == 'g')
10882 {
10883 flags = (char_u *)"g";
10884 ++s;
10885 }
10886
10887 sep = *s++;
10888 if (sep)
10889 {
10890 /* find end of pattern */
10891 p = vim_strchr(s, sep);
10892 if (p != NULL)
10893 {
10894 pat = vim_strnsave(s, (int)(p - s));
10895 if (pat != NULL)
10896 {
10897 s = p + 1;
10898 /* find end of substitution */
10899 p = vim_strchr(s, sep);
10900 if (p != NULL)
10901 {
10902 sub = vim_strnsave(s, (int)(p - s));
10903 str = vim_strnsave(*fnamep, *fnamelen);
10904 if (sub != NULL && str != NULL)
10905 {
10906 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010907 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 if (s != NULL)
10909 {
10910 *fnamep = s;
10911 *fnamelen = (int)STRLEN(s);
10912 vim_free(*bufp);
10913 *bufp = s;
10914 didit = TRUE;
10915 }
10916 }
10917 vim_free(sub);
10918 vim_free(str);
10919 }
10920 vim_free(pat);
10921 }
10922 }
10923 /* after using ":s", repeat all the modifiers */
10924 if (didit)
10925 goto repeat;
10926 }
10927 }
10928
Bram Moolenaar26df0922014-02-23 23:39:13 +010010929 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10930 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010931 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010932 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010933 if (c != NUL)
10934 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010935 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010936 if (c != NUL)
10937 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010938 if (p == NULL)
10939 return -1;
10940 vim_free(*bufp);
10941 *bufp = *fnamep = p;
10942 *fnamelen = (int)STRLEN(p);
10943 *usedlen += 2;
10944 }
10945
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 return valid;
10947}
10948
10949/*
10950 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010951 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 * "flags" can be "g" to do a global substitute.
10953 * Returns an allocated string, NULL for error.
10954 */
10955 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010956do_string_sub(
10957 char_u *str,
10958 char_u *pat,
10959 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010960 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010961 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962{
10963 int sublen;
10964 regmatch_T regmatch;
10965 int i;
10966 int do_all;
10967 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010968 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 garray_T ga;
10970 char_u *ret;
10971 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010972 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973
10974 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10975 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010976 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
10978 ga_init2(&ga, 1, 200);
10979
10980 do_all = (flags[0] == 'g');
10981
10982 regmatch.rm_ic = p_ic;
10983 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10984 if (regmatch.regprog != NULL)
10985 {
10986 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010987 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10989 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010990 /* Skip empty match except for first match. */
10991 if (regmatch.startp[0] == regmatch.endp[0])
10992 {
10993 if (zero_width == regmatch.startp[0])
10994 {
10995 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010996 i = MB_PTR2LEN(tail);
10997 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10998 (size_t)i);
10999 ga.ga_len += i;
11000 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010011001 continue;
11002 }
11003 zero_width = regmatch.startp[0];
11004 }
11005
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 /*
11007 * Get some space for a temporary buffer to do the substitution
11008 * into. It will contain:
11009 * - The text up to where the match is.
11010 * - The substituted text.
11011 * - The text after the match.
11012 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011013 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010011014 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11016 {
11017 ga_clear(&ga);
11018 break;
11019 }
11020
11021 /* copy the text up to where the match is */
11022 i = (int)(regmatch.startp[0] - tail);
11023 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11024 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011025 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 + ga.ga_len + i, TRUE, TRUE, FALSE);
11027 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020011028 tail = regmatch.endp[0];
11029 if (*tail == NUL)
11030 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 if (!do_all)
11032 break;
11033 }
11034
11035 if (ga.ga_data != NULL)
11036 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11037
Bram Moolenaar473de612013-06-08 18:19:48 +020011038 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 }
11040
11041 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11042 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011043 if (p_cpo == empty_option)
11044 p_cpo = save_cpo;
11045 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011046 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011047 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048
11049 return ret;
11050}
11051
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 static int
11053filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
11054{
11055 typval_T rettv;
11056 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058
11059 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
11060 argv[0] = vimvars[VV_KEY].vv_tv;
11061 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010011062 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
11063 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 if (map)
11065 {
11066 /* map(): replace the list item value */
11067 clear_tv(tv);
11068 rettv.v_lock = 0;
11069 *tv = rettv;
11070 }
11071 else
11072 {
11073 int error = FALSE;
11074
11075 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011076 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011077 clear_tv(&rettv);
11078 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011079 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011080 if (error)
11081 goto theend;
11082 }
11083 retval = OK;
11084theend:
11085 clear_tv(&vimvars[VV_VAL].vv_tv);
11086 return retval;
11087}
11088
11089
11090/*
11091 * Implementation of map() and filter().
11092 */
11093 void
11094filter_map(typval_T *argvars, typval_T *rettv, int map)
11095{
11096 typval_T *expr;
11097 listitem_T *li, *nli;
11098 list_T *l = NULL;
11099 dictitem_T *di;
11100 hashtab_T *ht;
11101 hashitem_T *hi;
11102 dict_T *d = NULL;
11103 typval_T save_val;
11104 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011105 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106 int rem;
11107 int todo;
11108 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11109 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11110 : N_("filter() argument"));
11111 int save_did_emsg;
11112 int idx = 0;
11113
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011114 if (argvars[0].v_type == VAR_BLOB)
11115 {
11116 if ((b = argvars[0].vval.v_blob) == NULL)
11117 return;
11118 }
11119 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011120 {
11121 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011122 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123 return;
11124 }
11125 else if (argvars[0].v_type == VAR_DICT)
11126 {
11127 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011128 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 return;
11130 }
11131 else
11132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011133 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 return;
11135 }
11136
11137 expr = &argvars[1];
11138 /* On type errors, the preceding call has already displayed an error
11139 * message. Avoid a misleading error message for an empty string that
11140 * was not passed as argument. */
11141 if (expr->v_type != VAR_UNKNOWN)
11142 {
11143 prepare_vimvar(VV_VAL, &save_val);
11144
11145 /* We reset "did_emsg" to be able to detect whether an error
11146 * occurred during evaluation of the expression. */
11147 save_did_emsg = did_emsg;
11148 did_emsg = FALSE;
11149
11150 prepare_vimvar(VV_KEY, &save_key);
11151 if (argvars[0].v_type == VAR_DICT)
11152 {
11153 vimvars[VV_KEY].vv_type = VAR_STRING;
11154
11155 ht = &d->dv_hashtab;
11156 hash_lock(ht);
11157 todo = (int)ht->ht_used;
11158 for (hi = ht->ht_array; todo > 0; ++hi)
11159 {
11160 if (!HASHITEM_EMPTY(hi))
11161 {
11162 int r;
11163
11164 --todo;
11165 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011166 if (map && (var_check_lock(di->di_tv.v_lock,
11167 arg_errmsg, TRUE)
11168 || var_check_ro(di->di_flags,
11169 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011170 break;
11171 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11172 r = filter_map_one(&di->di_tv, expr, map, &rem);
11173 clear_tv(&vimvars[VV_KEY].vv_tv);
11174 if (r == FAIL || did_emsg)
11175 break;
11176 if (!map && rem)
11177 {
11178 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11179 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11180 break;
11181 dictitem_remove(d, di);
11182 }
11183 }
11184 }
11185 hash_unlock(ht);
11186 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011187 else if (argvars[0].v_type == VAR_BLOB)
11188 {
11189 int i;
11190 typval_T tv;
11191
11192 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11193 for (i = 0; i < b->bv_ga.ga_len; i++)
11194 {
11195 tv.v_type = VAR_NUMBER;
11196 tv.vval.v_number = blob_get(b, i);
11197 vimvars[VV_KEY].vv_nr = idx;
11198 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11199 break;
11200 if (tv.v_type != VAR_NUMBER)
11201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011202 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011203 return;
11204 }
11205 tv.v_type = VAR_NUMBER;
11206 blob_set(b, i, tv.vval.v_number);
11207 if (!map && rem)
11208 {
11209 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11210
11211 mch_memmove(p + idx, p + i + 1,
11212 (size_t)b->bv_ga.ga_len - i - 1);
11213 --b->bv_ga.ga_len;
11214 --i;
11215 }
11216 }
11217 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011218 else
11219 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011220 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011221 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11222
11223 for (li = l->lv_first; li != NULL; li = nli)
11224 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011225 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011226 break;
11227 nli = li->li_next;
11228 vimvars[VV_KEY].vv_nr = idx;
11229 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11230 || did_emsg)
11231 break;
11232 if (!map && rem)
11233 listitem_remove(l, li);
11234 ++idx;
11235 }
11236 }
11237
11238 restore_vimvar(VV_KEY, &save_key);
11239 restore_vimvar(VV_VAL, &save_val);
11240
11241 did_emsg |= save_did_emsg;
11242 }
11243
11244 copy_tv(&argvars[0], rettv);
11245}
11246
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */