blob: 43866e657e737cc8337d9326dcc8ca1a505cc5f3 [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 Moolenaar1dced572012-04-05 16:54:08 +02005681#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005682 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005683#endif
5684
Bram Moolenaardb913952012-06-29 12:54:53 +02005685#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005686 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005687#endif
5688
5689#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005690 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005691#endif
5692
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005693#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005694 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005695 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005696#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005697#ifdef FEAT_NETBEANS_INTG
5698 abort = abort || set_ref_in_nb_channel(copyID);
5699#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005700
Bram Moolenaare3188e22016-05-31 21:13:04 +02005701#ifdef FEAT_TIMERS
5702 abort = abort || set_ref_in_timer(copyID);
5703#endif
5704
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005705#ifdef FEAT_QUICKFIX
5706 abort = abort || set_ref_in_quickfix(copyID);
5707#endif
5708
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005709#ifdef FEAT_TERMINAL
5710 abort = abort || set_ref_in_term(copyID);
5711#endif
5712
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005713 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005714 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005715 /*
5716 * 2. Free lists and dictionaries that are not referenced.
5717 */
5718 did_free = free_unref_items(copyID);
5719
5720 /*
5721 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005722 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005724 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005725 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005726 else if (p_verbose > 0)
5727 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005728 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005730
5731 return did_free;
5732}
5733
5734/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005735 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005736 */
5737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005739{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005740 int did_free = FALSE;
5741
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005742 /* Let all "free" functions know that we are here. This means no
5743 * dictionaries, lists, channels or jobs are to be freed, because we will
5744 * do that here. */
5745 in_free_unref_items = TRUE;
5746
5747 /*
5748 * PASS 1: free the contents of the items. We don't free the items
5749 * themselves yet, so that it is possible to decrement refcount counters
5750 */
5751
Bram Moolenaarcd524592016-07-17 14:57:05 +02005752 /* Go through the list of dicts and free items without the copyID. */
5753 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005754
Bram Moolenaarda861d62016-07-17 15:46:27 +02005755 /* Go through the list of lists and free items without the copyID. */
5756 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005757
5758#ifdef FEAT_JOB_CHANNEL
5759 /* Go through the list of jobs and free items without the copyID. This
5760 * must happen before doing channels, because jobs refer to channels, but
5761 * the reference from the channel to the job isn't tracked. */
5762 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5763
5764 /* Go through the list of channels and free items without the copyID. */
5765 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5766#endif
5767
5768 /*
5769 * PASS 2: free the items themselves.
5770 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005771 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005772 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005774#ifdef FEAT_JOB_CHANNEL
5775 /* Go through the list of jobs and free items without the copyID. This
5776 * must happen before doing channels, because jobs refer to channels, but
5777 * the reference from the channel to the job isn't tracked. */
5778 free_unused_jobs(copyID, COPYID_MASK);
5779
5780 /* Go through the list of channels and free items without the copyID. */
5781 free_unused_channels(copyID, COPYID_MASK);
5782#endif
5783
5784 in_free_unref_items = FALSE;
5785
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005786 return did_free;
5787}
5788
5789/*
5790 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005791 * "list_stack" is used to add lists to be marked. Can be NULL.
5792 *
5793 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005794 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005796set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005797{
5798 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005799 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005800 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005801 hashtab_T *cur_ht;
5802 ht_stack_T *ht_stack = NULL;
5803 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005804
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005805 cur_ht = ht;
5806 for (;;)
5807 {
5808 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005809 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005810 /* Mark each item in the hashtab. If the item contains a hashtab
5811 * it is added to ht_stack, if it contains a list it is added to
5812 * list_stack. */
5813 todo = (int)cur_ht->ht_used;
5814 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5815 if (!HASHITEM_EMPTY(hi))
5816 {
5817 --todo;
5818 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5819 &ht_stack, list_stack);
5820 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005821 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005822
5823 if (ht_stack == NULL)
5824 break;
5825
5826 /* take an item from the stack */
5827 cur_ht = ht_stack->ht;
5828 tempitem = ht_stack;
5829 ht_stack = ht_stack->prev;
5830 free(tempitem);
5831 }
5832
5833 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005834}
5835
5836/*
5837 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005838 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5839 *
5840 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005841 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005843set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005844{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005845 listitem_T *li;
5846 int abort = FALSE;
5847 list_T *cur_l;
5848 list_stack_T *list_stack = NULL;
5849 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005850
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005851 cur_l = l;
5852 for (;;)
5853 {
5854 if (!abort)
5855 /* Mark each item in the list. If the item contains a hashtab
5856 * it is added to ht_stack, if it contains a list it is added to
5857 * list_stack. */
5858 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5859 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5860 ht_stack, &list_stack);
5861 if (list_stack == NULL)
5862 break;
5863
5864 /* take an item from the stack */
5865 cur_l = list_stack->list;
5866 tempitem = list_stack;
5867 list_stack = list_stack->prev;
5868 free(tempitem);
5869 }
5870
5871 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005872}
5873
5874/*
5875 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005876 * "list_stack" is used to add lists to be marked. Can be NULL.
5877 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5878 *
5879 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005880 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005881 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005882set_ref_in_item(
5883 typval_T *tv,
5884 int copyID,
5885 ht_stack_T **ht_stack,
5886 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005887{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005888 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005889
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005890 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005891 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005892 dict_T *dd = tv->vval.v_dict;
5893
Bram Moolenaara03f2332016-02-06 18:09:59 +01005894 if (dd != NULL && dd->dv_copyID != copyID)
5895 {
5896 /* Didn't see this dict yet. */
5897 dd->dv_copyID = copyID;
5898 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005899 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005900 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5901 }
5902 else
5903 {
5904 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5905 if (newitem == NULL)
5906 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005907 else
5908 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005909 newitem->ht = &dd->dv_hashtab;
5910 newitem->prev = *ht_stack;
5911 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005912 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005913 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005914 }
5915 }
5916 else if (tv->v_type == VAR_LIST)
5917 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005918 list_T *ll = tv->vval.v_list;
5919
Bram Moolenaara03f2332016-02-06 18:09:59 +01005920 if (ll != NULL && ll->lv_copyID != copyID)
5921 {
5922 /* Didn't see this list yet. */
5923 ll->lv_copyID = copyID;
5924 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005925 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005926 abort = set_ref_in_list(ll, copyID, ht_stack);
5927 }
5928 else
5929 {
5930 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005931 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005932 if (newitem == NULL)
5933 abort = TRUE;
5934 else
5935 {
5936 newitem->list = ll;
5937 newitem->prev = *list_stack;
5938 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005939 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005940 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005941 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005943 else if (tv->v_type == VAR_FUNC)
5944 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005945 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005946 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005947 else if (tv->v_type == VAR_PARTIAL)
5948 {
5949 partial_T *pt = tv->vval.v_partial;
5950 int i;
5951
5952 /* A partial does not have a copyID, because it cannot contain itself.
5953 */
5954 if (pt != NULL)
5955 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005956 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005957
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958 if (pt->pt_dict != NULL)
5959 {
5960 typval_T dtv;
5961
5962 dtv.v_type = VAR_DICT;
5963 dtv.vval.v_dict = pt->pt_dict;
5964 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5965 }
5966
5967 for (i = 0; i < pt->pt_argc; ++i)
5968 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5969 ht_stack, list_stack);
5970 }
5971 }
5972#ifdef FEAT_JOB_CHANNEL
5973 else if (tv->v_type == VAR_JOB)
5974 {
5975 job_T *job = tv->vval.v_job;
5976 typval_T dtv;
5977
5978 if (job != NULL && job->jv_copyID != copyID)
5979 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005980 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005981 if (job->jv_channel != NULL)
5982 {
5983 dtv.v_type = VAR_CHANNEL;
5984 dtv.vval.v_channel = job->jv_channel;
5985 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5986 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005987 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005988 {
5989 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005990 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005991 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5992 }
5993 }
5994 }
5995 else if (tv->v_type == VAR_CHANNEL)
5996 {
5997 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005998 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005999 typval_T dtv;
6000 jsonq_T *jq;
6001 cbq_T *cq;
6002
6003 if (ch != NULL && ch->ch_copyID != copyID)
6004 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006005 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006006 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006007 {
6008 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6009 jq = jq->jq_next)
6010 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6011 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6012 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006013 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006014 {
6015 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006016 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006017 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6018 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006019 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006020 {
6021 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006022 dtv.vval.v_partial =
6023 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006024 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6025 }
6026 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006027 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006028 {
6029 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006030 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006031 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6032 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006033 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006034 {
6035 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006036 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006037 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6038 }
6039 }
6040 }
6041#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006042 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043}
6044
Bram Moolenaar17a13432016-01-24 14:22:10 +01006045 static char *
6046get_var_special_name(int nr)
6047{
6048 switch (nr)
6049 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006050 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006051 case VVAL_TRUE: return "v:true";
6052 case VVAL_NONE: return "v:none";
6053 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006054 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006055 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006056 return "42";
6057}
6058
Bram Moolenaar8c711452005-01-14 21:53:12 +00006059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 * Return a string with the string representation of a variable.
6061 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006063 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006064 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6065 * stings as "string()", otherwise does not put quotes around strings, as
6066 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006067 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6068 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006069 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006071 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006072echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073 typval_T *tv,
6074 char_u **tofree,
6075 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006076 int copyID,
6077 int echo_style,
6078 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006079 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006081 static int recurse = 0;
6082 char_u *r = NULL;
6083
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006085 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006086 if (!did_echo_string_emsg)
6087 {
6088 /* Only give this message once for a recursive call to avoid
6089 * flooding the user with errors. And stop iterating over lists
6090 * and dicts. */
6091 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006092 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006093 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006094 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006095 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006096 }
6097 ++recurse;
6098
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099 switch (tv->v_type)
6100 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006101 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006102 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006103 {
6104 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006105 r = tv->vval.v_string;
6106 if (r == NULL)
6107 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006108 }
6109 else
6110 {
6111 *tofree = string_quote(tv->vval.v_string, FALSE);
6112 r = *tofree;
6113 }
6114 break;
6115
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006117 if (echo_style)
6118 {
6119 *tofree = NULL;
6120 r = tv->vval.v_string;
6121 }
6122 else
6123 {
6124 *tofree = string_quote(tv->vval.v_string, TRUE);
6125 r = *tofree;
6126 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006127 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006128
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006129 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006130 {
6131 partial_T *pt = tv->vval.v_partial;
6132 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006133 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006134 garray_T ga;
6135 int i;
6136 char_u *tf;
6137
6138 ga_init2(&ga, 1, 100);
6139 ga_concat(&ga, (char_u *)"function(");
6140 if (fname != NULL)
6141 {
6142 ga_concat(&ga, fname);
6143 vim_free(fname);
6144 }
6145 if (pt != NULL && pt->pt_argc > 0)
6146 {
6147 ga_concat(&ga, (char_u *)", [");
6148 for (i = 0; i < pt->pt_argc; ++i)
6149 {
6150 if (i > 0)
6151 ga_concat(&ga, (char_u *)", ");
6152 ga_concat(&ga,
6153 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6154 vim_free(tf);
6155 }
6156 ga_concat(&ga, (char_u *)"]");
6157 }
6158 if (pt != NULL && pt->pt_dict != NULL)
6159 {
6160 typval_T dtv;
6161
6162 ga_concat(&ga, (char_u *)", ");
6163 dtv.v_type = VAR_DICT;
6164 dtv.vval.v_dict = pt->pt_dict;
6165 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6166 vim_free(tf);
6167 }
6168 ga_concat(&ga, (char_u *)")");
6169
6170 *tofree = ga.ga_data;
6171 r = *tofree;
6172 break;
6173 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006174
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006175 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006176 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006177 break;
6178
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006180 if (tv->vval.v_list == NULL)
6181 {
6182 *tofree = NULL;
6183 r = NULL;
6184 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006185 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6186 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006187 {
6188 *tofree = NULL;
6189 r = (char_u *)"[...]";
6190 }
6191 else
6192 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006193 int old_copyID = tv->vval.v_list->lv_copyID;
6194
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006195 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006196 *tofree = list2string(tv, copyID, restore_copyID);
6197 if (restore_copyID)
6198 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006199 r = *tofree;
6200 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006201 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006202
Bram Moolenaar8c711452005-01-14 21:53:12 +00006203 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006204 if (tv->vval.v_dict == NULL)
6205 {
6206 *tofree = NULL;
6207 r = NULL;
6208 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006209 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6210 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006211 {
6212 *tofree = NULL;
6213 r = (char_u *)"{...}";
6214 }
6215 else
6216 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006217 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006219 *tofree = dict2string(tv, copyID, restore_copyID);
6220 if (restore_copyID)
6221 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006222 r = *tofree;
6223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006224 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006226 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006227 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006228 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006229 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006230 break;
6231
Bram Moolenaar835dc632016-02-07 14:27:38 +01006232 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006233 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006235 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006236 if (composite_val)
6237 {
6238 *tofree = string_quote(r, FALSE);
6239 r = *tofree;
6240 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006242
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006243 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006244#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006245 *tofree = NULL;
6246 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6247 r = numbuf;
6248 break;
6249#endif
6250
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006251 case VAR_SPECIAL:
6252 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006253 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006254 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006255 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006256
Bram Moolenaar8502c702014-06-17 12:51:16 +02006257 if (--recurse == 0)
6258 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006259 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006260}
6261
6262/*
6263 * Return a string with the string representation of a variable.
6264 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6265 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006266 * Does not put quotes around strings, as ":echo" displays values.
6267 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6268 * May return NULL.
6269 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006270 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006271echo_string(
6272 typval_T *tv,
6273 char_u **tofree,
6274 char_u *numbuf,
6275 int copyID)
6276{
6277 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6278}
6279
6280/*
6281 * Return a string with the string representation of a variable.
6282 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6283 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006284 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006285 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006286 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288tv2string(
6289 typval_T *tv,
6290 char_u **tofree,
6291 char_u *numbuf,
6292 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006293{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006294 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295}
6296
6297/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 * Return string "str" in ' quotes, doubling ' characters.
6299 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006301 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006302 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006303string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006304{
Bram Moolenaar33570922005-01-25 22:26:29 +00006305 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006306 char_u *p, *r, *s;
6307
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 len = (function ? 13 : 3);
6309 if (str != NULL)
6310 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006311 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006312 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 if (*p == '\'')
6314 ++len;
6315 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006316 s = r = alloc(len);
6317 if (r != NULL)
6318 {
6319 if (function)
6320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006322 r += 10;
6323 }
6324 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006325 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 if (str != NULL)
6327 for (p = str; *p != NUL; )
6328 {
6329 if (*p == '\'')
6330 *r++ = '\'';
6331 MB_COPY_CHAR(p, r);
6332 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006333 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006334 if (function)
6335 *r++ = ')';
6336 *r++ = NUL;
6337 }
6338 return s;
6339}
6340
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006341#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006342/*
6343 * Convert the string "text" to a floating point number.
6344 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6345 * this always uses a decimal point.
6346 * Returns the length of the text that was consumed.
6347 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006348 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349string2float(
6350 char_u *text,
6351 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006352{
6353 char *s = (char *)text;
6354 float_T f;
6355
Bram Moolenaar62473612017-01-08 19:25:40 +01006356 /* MS-Windows does not deal with "inf" and "nan" properly. */
6357 if (STRNICMP(text, "inf", 3) == 0)
6358 {
6359 *value = INFINITY;
6360 return 3;
6361 }
6362 if (STRNICMP(text, "-inf", 3) == 0)
6363 {
6364 *value = -INFINITY;
6365 return 4;
6366 }
6367 if (STRNICMP(text, "nan", 3) == 0)
6368 {
6369 *value = NAN;
6370 return 3;
6371 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006372 f = strtod(s, &s);
6373 *value = f;
6374 return (int)((char_u *)s - text);
6375}
6376#endif
6377
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 * Get the value of an environment variable.
6380 * "arg" is pointing to the '$'. It is advanced to after the name.
6381 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006382 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 */
6384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006385get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 char_u *string = NULL;
6388 int len;
6389 int cc;
6390 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006391 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393 ++*arg;
6394 name = *arg;
6395 len = get_env_len(arg);
6396 if (evaluate)
6397 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006398 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006399 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006400
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006401 cc = name[len];
6402 name[len] = NUL;
6403 /* first try vim_getenv(), fast for normal environment vars */
6404 string = vim_getenv(name, &mustfree);
6405 if (string != NULL && *string != NUL)
6406 {
6407 if (!mustfree)
6408 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006410 else
6411 {
6412 if (mustfree)
6413 vim_free(string);
6414
6415 /* next try expanding things like $VIM and ${HOME} */
6416 string = expand_env_save(name - 1);
6417 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006418 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006419 }
6420 name[len] = cc;
6421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006422 rettv->v_type = VAR_STRING;
6423 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425
6426 return OK;
6427}
6428
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006429
6430
6431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006433 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436var2fpos(
6437 typval_T *varp,
6438 int dollar_lnum, /* TRUE when $ is last line */
6439 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006441 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006443 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
Bram Moolenaara5525202006-03-02 22:52:09 +00006445 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006446 if (varp->v_type == VAR_LIST)
6447 {
6448 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006449 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006450 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006451 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006452
6453 l = varp->vval.v_list;
6454 if (l == NULL)
6455 return NULL;
6456
6457 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006458 pos.lnum = list_find_nr(l, 0L, &error);
6459 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006460 return NULL; /* invalid line number */
6461
6462 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006463 pos.col = list_find_nr(l, 1L, &error);
6464 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006465 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006466 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006467
6468 /* We accept "$" for the column number: last column. */
6469 li = list_find(l, 1L);
6470 if (li != NULL && li->li_tv.v_type == VAR_STRING
6471 && li->li_tv.vval.v_string != NULL
6472 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6473 pos.col = len + 1;
6474
Bram Moolenaara5525202006-03-02 22:52:09 +00006475 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006476 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006477 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006478 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006479
Bram Moolenaara5525202006-03-02 22:52:09 +00006480 /* Get the virtual offset. Defaults to zero. */
6481 pos.coladd = list_find_nr(l, 2L, &error);
6482 if (error)
6483 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006484
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006485 return &pos;
6486 }
6487
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006488 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006489 if (name == NULL)
6490 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006491 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006493 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6494 {
6495 if (VIsual_active)
6496 return &VIsual;
6497 return &curwin->w_cursor;
6498 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006499 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006501 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6503 return NULL;
6504 return pp;
6505 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006506
Bram Moolenaara5525202006-03-02 22:52:09 +00006507 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006508
Bram Moolenaar477933c2007-07-17 14:32:23 +00006509 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006510 {
6511 pos.col = 0;
6512 if (name[1] == '0') /* "w0": first visible line */
6513 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006514 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006515 /* In silent Ex mode topline is zero, but that's not a valid line
6516 * number; use one instead. */
6517 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006518 return &pos;
6519 }
6520 else if (name[1] == '$') /* "w$": last visible line */
6521 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006522 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006523 /* In silent Ex mode botline is zero, return zero then. */
6524 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006525 return &pos;
6526 }
6527 }
6528 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006530 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
6532 pos.lnum = curbuf->b_ml.ml_line_count;
6533 pos.col = 0;
6534 }
6535 else
6536 {
6537 pos.lnum = curwin->w_cursor.lnum;
6538 pos.col = (colnr_T)STRLEN(ml_get_curline());
6539 }
6540 return &pos;
6541 }
6542 return NULL;
6543}
6544
6545/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006546 * Convert list in "arg" into a position and optional file number.
6547 * When "fnump" is NULL there is no file number, only 3 items.
6548 * Note that the column is passed on as-is, the caller may want to decrement
6549 * it to use 1 for the first column.
6550 * Return FAIL when conversion is not possible, doesn't check the position for
6551 * validity.
6552 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554list2fpos(
6555 typval_T *arg,
6556 pos_T *posp,
6557 int *fnump,
6558 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006559{
6560 list_T *l = arg->vval.v_list;
6561 long i = 0;
6562 long n;
6563
Bram Moolenaar493c1782014-05-28 14:34:46 +02006564 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6565 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006566 if (arg->v_type != VAR_LIST
6567 || l == NULL
6568 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006569 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006570 return FAIL;
6571
6572 if (fnump != NULL)
6573 {
6574 n = list_find_nr(l, i++, NULL); /* fnum */
6575 if (n < 0)
6576 return FAIL;
6577 if (n == 0)
6578 n = curbuf->b_fnum; /* current buffer */
6579 *fnump = n;
6580 }
6581
6582 n = list_find_nr(l, i++, NULL); /* lnum */
6583 if (n < 0)
6584 return FAIL;
6585 posp->lnum = n;
6586
6587 n = list_find_nr(l, i++, NULL); /* col */
6588 if (n < 0)
6589 return FAIL;
6590 posp->col = n;
6591
Bram Moolenaar493c1782014-05-28 14:34:46 +02006592 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006593 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006594 posp->coladd = 0;
6595 else
6596 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006597
Bram Moolenaar493c1782014-05-28 14:34:46 +02006598 if (curswantp != NULL)
6599 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6600
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006601 return OK;
6602}
6603
6604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 * Get the length of an environment variable name.
6606 * Advance "arg" to the first character after the name.
6607 * Return 0 for error.
6608 */
6609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 char_u *p;
6613 int len;
6614
6615 for (p = *arg; vim_isIDc(*p); ++p)
6616 ;
6617 if (p == *arg) /* no name found */
6618 return 0;
6619
6620 len = (int)(p - *arg);
6621 *arg = p;
6622 return len;
6623}
6624
6625/*
6626 * Get the length of the name of a function or internal variable.
6627 * "arg" is advanced to the first non-white character after the name.
6628 * Return 0 if something is wrong.
6629 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 char_u *p;
6634 int len;
6635
6636 /* Find the end of the name. */
6637 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006638 {
6639 if (*p == ':')
6640 {
6641 /* "s:" is start of "s:var", but "n:" is not and can be used in
6642 * slice "[n:]". Also "xx:" is not a namespace. */
6643 len = (int)(p - *arg);
6644 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6645 || len > 1)
6646 break;
6647 }
6648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 if (p == *arg) /* no name found */
6650 return 0;
6651
6652 len = (int)(p - *arg);
6653 *arg = skipwhite(p);
6654
6655 return len;
6656}
6657
6658/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006659 * Get the length of the name of a variable or function.
6660 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006662 * Return -1 if curly braces expansion failed.
6663 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 * If the name contains 'magic' {}'s, expand them and return the
6665 * expanded name in an allocated string via 'alias' - caller must free.
6666 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668get_name_len(
6669 char_u **arg,
6670 char_u **alias,
6671 int evaluate,
6672 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 char_u *p;
6676 char_u *expr_start;
6677 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 *alias = NULL; /* default to no alias */
6680
6681 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6682 && (*arg)[2] == (int)KE_SNR)
6683 {
6684 /* hard coded <SNR>, already translated */
6685 *arg += 3;
6686 return get_id_len(arg) + 3;
6687 }
6688 len = eval_fname_script(*arg);
6689 if (len > 0)
6690 {
6691 /* literal "<SID>", "s:" or "<SNR>" */
6692 *arg += len;
6693 }
6694
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006696 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006698 p = find_name_end(*arg, &expr_start, &expr_end,
6699 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (expr_start != NULL)
6701 {
6702 char_u *temp_string;
6703
6704 if (!evaluate)
6705 {
6706 len += (int)(p - *arg);
6707 *arg = skipwhite(p);
6708 return len;
6709 }
6710
6711 /*
6712 * Include any <SID> etc in the expanded string:
6713 * Thus the -len here.
6714 */
6715 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6716 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006717 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 *alias = temp_string;
6719 *arg = skipwhite(p);
6720 return (int)STRLEN(temp_string);
6721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
6723 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006724 // Only give an error when there is something, otherwise it will be
6725 // reported at a higher level.
6726 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006727 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
6729 return len;
6730}
6731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732/*
6733 * Find the end of a variable or function name, taking care of magic braces.
6734 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6735 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006736 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 * Return a pointer to just after the name. Equal to "arg" if there is no
6738 * valid name.
6739 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006740 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741find_name_end(
6742 char_u *arg,
6743 char_u **expr_start,
6744 char_u **expr_end,
6745 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006747 int mb_nest = 0;
6748 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006750 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006752 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006754 *expr_start = NULL;
6755 *expr_end = NULL;
6756 }
6757
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006758 /* Quick check for valid starting character. */
6759 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6760 return arg;
6761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006762 for (p = arg; *p != NUL
6763 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006764 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006765 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006766 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006767 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006768 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006769 if (*p == '\'')
6770 {
6771 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006772 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006773 ;
6774 if (*p == NUL)
6775 break;
6776 }
6777 else if (*p == '"')
6778 {
6779 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006780 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006781 if (*p == '\\' && p[1] != NUL)
6782 ++p;
6783 if (*p == NUL)
6784 break;
6785 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006786 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6787 {
6788 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006789 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006790 len = (int)(p - arg);
6791 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006792 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006793 break;
6794 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006795
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006796 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006798 if (*p == '[')
6799 ++br_nest;
6800 else if (*p == ']')
6801 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006803
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006804 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 if (*p == '{')
6807 {
6808 mb_nest++;
6809 if (expr_start != NULL && *expr_start == NULL)
6810 *expr_start = p;
6811 }
6812 else if (*p == '}')
6813 {
6814 mb_nest--;
6815 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6816 *expr_end = p;
6817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820
6821 return p;
6822}
6823
6824/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006825 * Expands out the 'magic' {}'s in a variable/function name.
6826 * Note that this can call itself recursively, to deal with
6827 * constructs like foo{bar}{baz}{bam}
6828 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6829 * "in_start" ^
6830 * "expr_start" ^
6831 * "expr_end" ^
6832 * "in_end" ^
6833 *
6834 * Returns a new allocated string, which the caller must free.
6835 * Returns NULL for failure.
6836 */
6837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838make_expanded_name(
6839 char_u *in_start,
6840 char_u *expr_start,
6841 char_u *expr_end,
6842 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006843{
6844 char_u c1;
6845 char_u *retval = NULL;
6846 char_u *temp_result;
6847 char_u *nextcmd = NULL;
6848
6849 if (expr_end == NULL || in_end == NULL)
6850 return NULL;
6851 *expr_start = NUL;
6852 *expr_end = NUL;
6853 c1 = *in_end;
6854 *in_end = NUL;
6855
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006856 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006857 if (temp_result != NULL && nextcmd == NULL)
6858 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006859 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6860 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006861 if (retval != NULL)
6862 {
6863 STRCPY(retval, in_start);
6864 STRCAT(retval, temp_result);
6865 STRCAT(retval, expr_end + 1);
6866 }
6867 }
6868 vim_free(temp_result);
6869
6870 *in_end = c1; /* put char back for error messages */
6871 *expr_start = '{';
6872 *expr_end = '}';
6873
6874 if (retval != NULL)
6875 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006876 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006877 if (expr_start != NULL)
6878 {
6879 /* Further expansion! */
6880 temp_result = make_expanded_name(retval, expr_start,
6881 expr_end, temp_result);
6882 vim_free(retval);
6883 retval = temp_result;
6884 }
6885 }
6886
6887 return retval;
6888}
6889
6890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006895eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006897 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6898}
6899
6900/*
6901 * Return TRUE if character "c" can be used as the first character in a
6902 * variable or function name (excluding '{' and '}').
6903 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006904 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006905eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006906{
6907 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908}
6909
6910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 * Set number v: variable to "val".
6912 */
6913 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006914set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917}
6918
6919/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006920 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006922 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006923get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006925 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
6927
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006928/*
6929 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006930 * If the String variable has never been set, return an empty string.
6931 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006932 */
6933 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006934get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006935{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006936 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006937}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006940 * Get List v: variable value. Caller must take care of reference count when
6941 * needed.
6942 */
6943 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006944get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006945{
6946 return vimvars[idx].vv_list;
6947}
6948
6949/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006950 * Get Dict v: variable value. Caller must take care of reference count when
6951 * needed.
6952 */
6953 dict_T *
6954get_vim_var_dict(int idx)
6955{
6956 return vimvars[idx].vv_dict;
6957}
6958
6959/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006960 * Set v:char to character "c".
6961 */
6962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006964{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006965 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006966
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006967 if (has_mbyte)
6968 buf[(*mb_char2bytes)(c, buf)] = NUL;
6969 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006970 {
6971 buf[0] = c;
6972 buf[1] = NUL;
6973 }
6974 set_vim_var_string(VV_CHAR, buf, -1);
6975}
6976
6977/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006978 * Set v:count to "count" and v:count1 to "count1".
6979 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 */
6981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982set_vcount(
6983 long count,
6984 long count1,
6985 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006987 if (set_prevcount)
6988 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 vimvars[VV_COUNT].vv_nr = count;
6990 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991}
6992
6993/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006994 * Save variables that might be changed as a side effect. Used when executing
6995 * a timer callback.
6996 */
6997 void
6998save_vimvars(vimvars_save_T *vvsave)
6999{
7000 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7001 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7002 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7003}
7004
7005/*
7006 * Restore variables saved by save_vimvars().
7007 */
7008 void
7009restore_vimvars(vimvars_save_T *vvsave)
7010{
7011 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7012 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7013 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7014}
7015
7016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 * Set string v: variable to a copy of "val".
7018 */
7019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007020set_vim_var_string(
7021 int idx,
7022 char_u *val,
7023 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
Bram Moolenaara542c682016-01-31 16:28:04 +01007025 clear_tv(&vimvars[idx].vv_di.di_tv);
7026 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007032 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033}
7034
7035/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007036 * Set List v: variable to "val".
7037 */
7038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007040{
Bram Moolenaara542c682016-01-31 16:28:04 +01007041 clear_tv(&vimvars[idx].vv_di.di_tv);
7042 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007043 vimvars[idx].vv_list = val;
7044 if (val != NULL)
7045 ++val->lv_refcount;
7046}
7047
7048/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007049 * Set Dictionary v: variable to "val".
7050 */
7051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007052set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007053{
Bram Moolenaara542c682016-01-31 16:28:04 +01007054 clear_tv(&vimvars[idx].vv_di.di_tv);
7055 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007056 vimvars[idx].vv_dict = val;
7057 if (val != NULL)
7058 {
7059 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007060 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007061 }
7062}
7063
7064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 * Set v:register if needed.
7066 */
7067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007068set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
7070 char_u regname;
7071
7072 if (c == 0 || c == ' ')
7073 regname = '"';
7074 else
7075 regname = c;
7076 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007077 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 set_vim_var_string(VV_REG, &regname, 1);
7079}
7080
7081/*
7082 * Get or set v:exception. If "oldval" == NULL, return the current value.
7083 * Otherwise, restore the value to "oldval" and return NULL.
7084 * Must always be called in pairs to save and restore v:exception! Does not
7085 * take care of memory allocations.
7086 */
7087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007088v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089{
7090 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 return NULL;
7095}
7096
7097/*
7098 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7099 * Otherwise, restore the value to "oldval" and return NULL.
7100 * Must always be called in pairs to save and restore v:throwpoint! Does not
7101 * take care of memory allocations.
7102 */
7103 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
Bram Moolenaare9a41262005-01-15 22:18:47 +00007109 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 return NULL;
7111}
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113/*
7114 * Set v:cmdarg.
7115 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7116 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7117 * Must always be called in pairs!
7118 */
7119 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007120set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 char_u *oldval;
7123 char_u *newval;
7124 unsigned len;
7125
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007127 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007129 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007130 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007131 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
7133
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007134 if (eap->force_bin == FORCE_BIN)
7135 len = 6;
7136 else if (eap->force_bin == FORCE_NOBIN)
7137 len = 8;
7138 else
7139 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007140
7141 if (eap->read_edit)
7142 len += 7;
7143
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007144 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007145 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007146 if (eap->force_enc != 0)
7147 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007148 if (eap->bad_char != 0)
7149 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007150
7151 newval = alloc(len + 1);
7152 if (newval == NULL)
7153 return NULL;
7154
7155 if (eap->force_bin == FORCE_BIN)
7156 sprintf((char *)newval, " ++bin");
7157 else if (eap->force_bin == FORCE_NOBIN)
7158 sprintf((char *)newval, " ++nobin");
7159 else
7160 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007161
7162 if (eap->read_edit)
7163 STRCAT(newval, " ++edit");
7164
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007165 if (eap->force_ff != 0)
7166 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007167 eap->force_ff == 'u' ? "unix"
7168 : eap->force_ff == 'd' ? "dos"
7169 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007170 if (eap->force_enc != 0)
7171 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7172 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007173 if (eap->bad_char == BAD_KEEP)
7174 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7175 else if (eap->bad_char == BAD_DROP)
7176 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7177 else if (eap->bad_char != 0)
7178 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007180 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
7183/*
7184 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007185 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007188get_var_tv(
7189 char_u *name,
7190 int len, /* length of "name" */
7191 typval_T *rettv, /* NULL when only checking existence */
7192 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7193 int verbose, /* may give error message */
7194 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
7201 /* truncate the name, so that we can use strcmp() */
7202 cc = name[len];
7203 name[len] = NUL;
7204
7205 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 * Check for user-defined variables.
7207 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007208 v = find_var(name, NULL, no_autoload);
7209 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007211 tv = &v->di_tv;
7212 if (dip != NULL)
7213 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215
Bram Moolenaare9a41262005-01-15 22:18:47 +00007216 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007218 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007219 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 ret = FAIL;
7221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007222 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
7225 name[len] = cc;
7226
7227 return ret;
7228}
7229
7230/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007231 * Check if variable "name[len]" is a local variable or an argument.
7232 * If so, "*eval_lavars_used" is set to TRUE.
7233 */
7234 static void
7235check_vars(char_u *name, int len)
7236{
7237 int cc;
7238 char_u *varname;
7239 hashtab_T *ht;
7240
7241 if (eval_lavars_used == NULL)
7242 return;
7243
7244 /* truncate the name, so that we can use strcmp() */
7245 cc = name[len];
7246 name[len] = NUL;
7247
7248 ht = find_var_ht(name, &varname);
7249 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7250 {
7251 if (find_var(name, NULL, TRUE) != NULL)
7252 *eval_lavars_used = TRUE;
7253 }
7254
7255 name[len] = cc;
7256}
7257
7258/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007259 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7260 * Also handle function call with Funcref variable: func(expr)
7261 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7262 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007264handle_subscript(
7265 char_u **arg,
7266 typval_T *rettv,
7267 int evaluate, /* do more than finding the end */
7268 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007269{
7270 int ret = OK;
7271 dict_T *selfdict = NULL;
7272 char_u *s;
7273 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007274 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007275
7276 while (ret == OK
7277 && (**arg == '['
7278 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007279 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7280 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007281 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007282 {
7283 if (**arg == '(')
7284 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007285 partial_T *pt = NULL;
7286
Bram Moolenaard9fba312005-06-26 22:34:35 +00007287 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007288 if (evaluate)
7289 {
7290 functv = *rettv;
7291 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007292
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007293 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007294 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007295 {
7296 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007297 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007298 }
7299 else
7300 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007301 }
7302 else
7303 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007304 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007305 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007306 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007307
7308 /* Clear the funcref afterwards, so that deleting it while
7309 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007310 if (evaluate)
7311 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007312
7313 /* Stop the expression evaluation when immediately aborting on
7314 * error, or when an interrupt occurred or an exception was thrown
7315 * but not caught. */
7316 if (aborting())
7317 {
7318 if (ret == OK)
7319 clear_tv(rettv);
7320 ret = FAIL;
7321 }
7322 dict_unref(selfdict);
7323 selfdict = NULL;
7324 }
7325 else /* **arg == '[' || **arg == '.' */
7326 {
7327 dict_unref(selfdict);
7328 if (rettv->v_type == VAR_DICT)
7329 {
7330 selfdict = rettv->vval.v_dict;
7331 if (selfdict != NULL)
7332 ++selfdict->dv_refcount;
7333 }
7334 else
7335 selfdict = NULL;
7336 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7337 {
7338 clear_tv(rettv);
7339 ret = FAIL;
7340 }
7341 }
7342 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007343
Bram Moolenaar1d429612016-05-24 15:44:17 +02007344 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7345 * Don't do this when "Func" is already a partial that was bound
7346 * explicitly (pt_auto is FALSE). */
7347 if (selfdict != NULL
7348 && (rettv->v_type == VAR_FUNC
7349 || (rettv->v_type == VAR_PARTIAL
7350 && (rettv->vval.v_partial->pt_auto
7351 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007352 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007353
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007354 dict_unref(selfdict);
7355 return ret;
7356}
7357
7358/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007359 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 * value).
7361 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007362 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007363alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007364{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007365 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366}
7367
7368/*
7369 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 * The string "s" must have been allocated, it is consumed.
7371 * Return NULL for out of memory, the variable otherwise.
7372 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007374alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 rettv = alloc_tv();
7379 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007381 rettv->v_type = VAR_STRING;
7382 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
7384 else
7385 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007386 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387}
7388
7389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007390 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007393free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 if (varp != NULL)
7396 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 switch (varp->v_type)
7398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007400 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007401 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007402 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007403 vim_free(varp->vval.v_string);
7404 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007405 case VAR_PARTIAL:
7406 partial_unref(varp->vval.v_partial);
7407 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007408 case VAR_BLOB:
7409 blob_unref(varp->vval.v_blob);
7410 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007411 case VAR_LIST:
7412 list_unref(varp->vval.v_list);
7413 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007414 case VAR_DICT:
7415 dict_unref(varp->vval.v_dict);
7416 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007417 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007418#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007419 job_unref(varp->vval.v_job);
7420 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007421#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007422 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007423#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007424 channel_unref(varp->vval.v_channel);
7425 break;
7426#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007427 case VAR_NUMBER:
7428 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007429 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007430 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007431 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 vim_free(varp);
7434 }
7435}
7436
7437/*
7438 * Free the memory for a variable value and set the value to NULL or 0.
7439 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
7443 if (varp != NULL)
7444 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007445 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007448 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007449 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007450 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007451 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007453 case VAR_PARTIAL:
7454 partial_unref(varp->vval.v_partial);
7455 varp->vval.v_partial = NULL;
7456 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007457 case VAR_BLOB:
7458 blob_unref(varp->vval.v_blob);
7459 varp->vval.v_blob = NULL;
7460 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 case VAR_LIST:
7462 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007463 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 case VAR_DICT:
7466 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007470 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 varp->vval.v_number = 0;
7472 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007473 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007474#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007475 varp->vval.v_float = 0.0;
7476 break;
7477#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007478 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007479#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007480 job_unref(varp->vval.v_job);
7481 varp->vval.v_job = NULL;
7482#endif
7483 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007484 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007485#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007486 channel_unref(varp->vval.v_channel);
7487 varp->vval.v_channel = NULL;
7488#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007489 case VAR_UNKNOWN:
7490 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007492 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494}
7495
7496/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 * Set the value of a variable to NULL without freeing items.
7498 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007500init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501{
7502 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007504}
7505
7506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 * Get the number value of a variable.
7508 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007509 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007510 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007511 * caller of incompatible types: it sets *denote to TRUE if "denote"
7512 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007514 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007515tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007517 int error = FALSE;
7518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007519 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007520}
7521
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007522 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007523tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007524{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007525 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007527 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007529 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007530 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007531 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007532#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007533 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007534 break;
7535#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007537 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007538 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007539 break;
7540 case VAR_STRING:
7541 if (varp->vval.v_string != NULL)
7542 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007543 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007544 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007545 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007547 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007548 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007549 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007550 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007551 case VAR_SPECIAL:
7552 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7553 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007554 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007555#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007557 break;
7558#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007559 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007560#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007561 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007562 break;
7563#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007564 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007565 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007566 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007567 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007568 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007571 if (denote == NULL) /* useful for values that must be unsigned */
7572 n = -1;
7573 else
7574 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576}
7577
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007578#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007580tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007581{
7582 switch (varp->v_type)
7583 {
7584 case VAR_NUMBER:
7585 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007586 case VAR_FLOAT:
7587 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007588 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007589 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007591 break;
7592 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007594 break;
7595 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007597 break;
7598 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007600 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007601 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007602 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007603 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007604 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007605# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007607 break;
7608# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007609 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007610# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007611 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007612 break;
7613# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007614 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007616 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007617 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007618 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007619 break;
7620 }
7621 return 0;
7622}
7623#endif
7624
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 * Get the string value of a variable.
7627 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007628 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7629 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 * If the String variable has never been set, return an empty string.
7631 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007632 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007633 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007635 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007636tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637{
7638 static char_u mybuf[NUMBUFLEN];
7639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007640 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641}
7642
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007643 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007644tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007646 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647
7648 return res != NULL ? res : (char_u *)"";
7649}
7650
Bram Moolenaar7d647822014-04-05 21:28:56 +02007651/*
7652 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7653 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007654 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007655tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007656{
7657 static char_u mybuf[NUMBUFLEN];
7658
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007659 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007660}
7661
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007662 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007663tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007664{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007667 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007668 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007669 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 return buf;
7671 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007672 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 break;
7675 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007676 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 break;
7678 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007679 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007681 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007682#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007684 break;
7685#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 case VAR_STRING:
7687 if (varp->vval.v_string != NULL)
7688 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007689 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007690 case VAR_SPECIAL:
7691 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7692 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007693 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007694 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007695 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007696 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007697#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007698 {
7699 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007700 char *status;
7701
7702 if (job == NULL)
7703 return (char_u *)"no process";
7704 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007705 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007706 : "run";
7707# ifdef UNIX
7708 vim_snprintf((char *)buf, NUMBUFLEN,
7709 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007710# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007711 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007712 "process %ld %s",
7713 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007714 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007715# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007716 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007717 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7718# endif
7719 return buf;
7720 }
7721#endif
7722 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007723 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007724#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007725 {
7726 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007727 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007728
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007729 if (channel == NULL)
7730 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7731 else
7732 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007733 "channel %d %s", channel->ch_id, status);
7734 return buf;
7735 }
7736#endif
7737 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007738 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007739 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007746 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7747 * string() on Dict, List, etc.
7748 */
7749 char_u *
7750tv_stringify(typval_T *varp, char_u *buf)
7751{
7752 if (varp->v_type == VAR_LIST
7753 || varp->v_type == VAR_DICT
7754 || varp->v_type == VAR_FUNC
7755 || varp->v_type == VAR_PARTIAL
7756 || varp->v_type == VAR_FLOAT)
7757 {
7758 typval_T tmp;
7759
7760 f_string(varp, &tmp);
7761 tv_get_string_buf(&tmp, buf);
7762 clear_tv(varp);
7763 *varp = tmp;
7764 return tmp.vval.v_string;
7765 }
7766 return tv_get_string_buf(varp, buf);
7767}
7768
7769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 * Find variable "name" in the list of variables.
7771 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007772 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007773 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007776 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007781 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782
Bram Moolenaara7043832005-01-21 11:56:39 +00007783 ht = find_var_ht(name, &varname);
7784 if (htp != NULL)
7785 *htp = ht;
7786 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007788 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7789 if (ret != NULL)
7790 return ret;
7791
7792 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007793 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794}
7795
7796/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007797 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007798 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801find_var_in_ht(
7802 hashtab_T *ht,
7803 int htname,
7804 char_u *varname,
7805 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007806{
Bram Moolenaar33570922005-01-25 22:26:29 +00007807 hashitem_T *hi;
7808
7809 if (*varname == NUL)
7810 {
7811 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007812 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007813 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007814 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 case 'g': return &globvars_var;
7816 case 'v': return &vimvars_var;
7817 case 'b': return &curbuf->b_bufvar;
7818 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007819 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007820 case 'l': return get_funccal_local_var();
7821 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007822 }
7823 return NULL;
7824 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007825
7826 hi = hash_find(ht, varname);
7827 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007828 {
7829 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007830 * worked find the variable again. Don't auto-load a script if it was
7831 * loaded already, otherwise it would be loaded every time when
7832 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007833 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007834 {
7835 /* Note: script_autoload() may make "hi" invalid. It must either
7836 * be obtained again or not used. */
7837 if (!script_autoload(varname, FALSE) || aborting())
7838 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007839 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007840 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007841 if (HASHITEM_EMPTY(hi))
7842 return NULL;
7843 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007845}
7846
7847/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007848 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007849 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007850 * Set "varname" to the start of name without ':'.
7851 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007852 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007855 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007856 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007857
Bram Moolenaar73627d02015-08-11 15:46:09 +02007858 if (name[0] == NUL)
7859 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 if (name[1] != ':')
7861 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007862 /* The name must not start with a colon or #. */
7863 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 return NULL;
7865 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007866
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007867 // "version" is "v:version" in all scopes if scriptversion < 3.
7868 // Same for a few other variables marked with VV_COMPAT.
7869 if (current_sctx.sc_version < 3)
7870 {
7871 hi = hash_find(&compat_hashtab, name);
7872 if (!HASHITEM_EMPTY(hi))
7873 return &compat_hashtab;
7874 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007875
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007876 ht = get_funccal_local_ht();
7877 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007879 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007882 if (*name == 'g') /* global variable */
7883 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007884 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7885 */
7886 if (vim_strchr(name + 2, ':') != NULL
7887 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007888 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007890 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007892 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007893 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007894 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 if (*name == 'v') /* v: variable */
7896 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007897 if (*name == 'a') /* a: function argument */
7898 return get_funccal_args_ht();
7899 if (*name == 'l') /* l: local function variable */
7900 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007902 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7903 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 return NULL;
7905}
7906
7907/*
7908 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007909 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 * Returns NULL when it doesn't exist.
7911 */
7912 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007913get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007917 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if (v == NULL)
7919 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007920 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
7923/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 * sourcing this script and when executing functions defined in the script.
7926 */
7927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007928new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
Bram Moolenaara7043832005-01-21 11:56:39 +00007930 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 hashtab_T *ht;
7932 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007933
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7935 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007936 /* Re-allocating ga_data means that an ht_array pointing to
7937 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007939 for (i = 1; i <= ga_scripts.ga_len; ++i)
7940 {
7941 ht = &SCRIPT_VARS(i);
7942 if (ht->ht_mask == HT_INIT_SIZE - 1)
7943 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007944 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007946 }
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 while (ga_scripts.ga_len < id)
7949 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007950 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007951 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007952 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
7955 }
7956}
7957
7958/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7960 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 */
7962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007963init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007966 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007967 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007968 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007969 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 dict_var->di_tv.vval.v_dict = dict;
7971 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007972 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7974 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975}
7976
7977/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007978 * Unreference a dictionary initialized by init_var_dict().
7979 */
7980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007981unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007982{
7983 /* Now the dict needs to be freed if no one else is using it, go back to
7984 * normal reference counting. */
7985 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7986 dict_unref(dict);
7987}
7988
7989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007991 * Frees all allocated variables and the value they contain.
7992 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 */
7994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007995vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007996{
7997 vars_clear_ext(ht, TRUE);
7998}
7999
8000/*
8001 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8002 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
Bram Moolenaara7043832005-01-21 11:56:39 +00008006 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 hashitem_T *hi;
8008 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008011 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008012 for (hi = ht->ht_array; todo > 0; ++hi)
8013 {
8014 if (!HASHITEM_EMPTY(hi))
8015 {
8016 --todo;
8017
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008019 * ht_array might change then. hash_clear() takes care of it
8020 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008021 v = HI2DI(hi);
8022 if (free_val)
8023 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008024 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008025 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008026 }
8027 }
8028 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008029 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
Bram Moolenaara7043832005-01-21 11:56:39 +00008032/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 * Delete a variable from hashtab "ht" at item "hi".
8034 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008037delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008040
8041 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008042 clear_tv(&di->di_tv);
8043 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045
8046/*
8047 * List the value of one internal variable.
8048 */
8049 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008050list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052 char_u *tofree;
8053 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008054 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008055
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008056 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008058 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008059 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008063list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008064 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008065 char_u *name,
8066 int type,
8067 char_u *string,
8068 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
Bram Moolenaar31859182007-08-14 20:41:13 +00008070 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8071 msg_start();
8072 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008074 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 msg_putchar(' ');
8076 msg_advance(22);
8077 if (type == VAR_NUMBER)
8078 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008079 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008080 msg_putchar('*');
8081 else if (type == VAR_LIST)
8082 {
8083 msg_putchar('[');
8084 if (*string == '[')
8085 ++string;
8086 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008087 else if (type == VAR_DICT)
8088 {
8089 msg_putchar('{');
8090 if (*string == '{')
8091 ++string;
8092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 else
8094 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008098 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008099 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008100 if (*first)
8101 {
8102 msg_clr_eos();
8103 *first = FALSE;
8104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105}
8106
8107/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008108 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 * If the variable already exists, the value is updated.
8110 * Otherwise the variable is created.
8111 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008113set_var(
8114 char_u *name,
8115 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008116 int copy) // make copy of value in "tv"
8117{
8118 set_var_const(name, tv, copy, FALSE);
8119}
8120
8121/*
8122 * Set variable "name" to value in "tv".
8123 * If the variable already exists and "is_const" is FALSE the value is updated.
8124 * Otherwise the variable is created.
8125 */
8126 static void
8127set_var_const(
8128 char_u *name,
8129 typval_T *tv,
8130 int copy, // make copy of value in "tv"
8131 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008137 ht = find_var_ht(name, &varname);
8138 if (ht == NULL || *varname == NUL)
8139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008140 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008141 return;
8142 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008143 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008144
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008145 /* Search in parent scope which is possible to reference from lambda */
8146 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008147 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008148
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008149 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8150 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008151 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152
Bram Moolenaar33570922005-01-25 22:26:29 +00008153 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008155 if (is_const)
8156 {
8157 emsg(_(e_cannot_mod));
8158 return;
8159 }
8160
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008162 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008163 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008165
8166 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008167 * Handle setting internal v: variables separately where needed to
8168 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 */
8170 if (ht == &vimvarht)
8171 {
8172 if (v->di_tv.v_type == VAR_STRING)
8173 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008174 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008175 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008176 {
8177 char_u *val = tv_get_string(tv);
8178
8179 // Careful: when assigning to v:errmsg and tv_get_string()
8180 // causes an error message the variable will alrady be set.
8181 if (v->di_tv.vval.v_string == NULL)
8182 v->di_tv.vval.v_string = vim_strsave(val);
8183 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 else
8185 {
8186 /* Take over the string to avoid an extra alloc/free. */
8187 v->di_tv.vval.v_string = tv->vval.v_string;
8188 tv->vval.v_string = NULL;
8189 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008190 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008192 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008194 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008195 if (STRCMP(varname, "searchforward") == 0)
8196 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008197#ifdef FEAT_SEARCH_EXTRA
8198 else if (STRCMP(varname, "hlsearch") == 0)
8199 {
8200 no_hlsearch = !v->di_tv.vval.v_number;
8201 redraw_all_later(SOME_VALID);
8202 }
8203#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008204 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008205 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008206 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008208 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008209 return;
8210 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 }
8212
8213 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 }
8215 else /* add a new variable */
8216 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008217 // Can't add "v:" or "a:" variable.
8218 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008220 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008221 return;
8222 }
8223
Bram Moolenaar31b81602019-02-10 22:14:27 +01008224 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008225 if (!valid_varname(varname))
8226 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008227
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008228 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008229 if (v == NULL)
8230 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008232 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008234 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 return;
8236 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008237 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008238 if (is_const)
8239 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008241
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008244 else
8245 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008246 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008247 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008249 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008250
8251 if (is_const)
8252 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008255/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008256 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 * Also give an error message.
8258 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008260var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008261{
8262 if (flags & DI_FLAGS_RO)
8263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008264 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 return TRUE;
8266 }
8267 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008269 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 return TRUE;
8271 }
8272 return FALSE;
8273}
8274
8275/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008276 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8277 * Also give an error message.
8278 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008280var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008281{
8282 if (flags & DI_FLAGS_FIX)
8283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008284 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008285 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008286 return TRUE;
8287 }
8288 return FALSE;
8289}
8290
8291/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008292 * Check if a funcref is assigned to a valid variable name.
8293 * Return TRUE and give an error if not.
8294 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008296var_check_func_name(
8297 char_u *name, /* points to start of variable name */
8298 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008299{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008300 /* Allow for w: b: s: and t:. */
8301 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008302 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8303 ? name[2] : name[0]))
8304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008305 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008306 name);
8307 return TRUE;
8308 }
8309 /* Don't allow hiding a function. When "v" is not NULL we might be
8310 * assigning another function to the same var, the type is checked
8311 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008312 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008314 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008315 name);
8316 return TRUE;
8317 }
8318 return FALSE;
8319}
8320
8321/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008322 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008323 * Also give an error message, using "name" or _("name") when use_gettext is
8324 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008325 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008326 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008327var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008328{
8329 if (lock & VAR_LOCKED)
8330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008331 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008332 name == NULL ? (char_u *)_("Unknown")
8333 : use_gettext ? (char_u *)_(name)
8334 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008335 return TRUE;
8336 }
8337 if (lock & VAR_FIXED)
8338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008339 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008340 name == NULL ? (char_u *)_("Unknown")
8341 : use_gettext ? (char_u *)_(name)
8342 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008343 return TRUE;
8344 }
8345 return FALSE;
8346}
8347
8348/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008349 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8350 * Also give an error message, using "name" or _("name") when use_gettext is
8351 * TRUE.
8352 */
8353 static int
8354tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8355{
8356 int lock = 0;
8357
8358 switch (tv->v_type)
8359 {
8360 case VAR_BLOB:
8361 if (tv->vval.v_blob != NULL)
8362 lock = tv->vval.v_blob->bv_lock;
8363 break;
8364 case VAR_LIST:
8365 if (tv->vval.v_list != NULL)
8366 lock = tv->vval.v_list->lv_lock;
8367 break;
8368 case VAR_DICT:
8369 if (tv->vval.v_dict != NULL)
8370 lock = tv->vval.v_dict->dv_lock;
8371 break;
8372 default:
8373 break;
8374 }
8375 return var_check_lock(tv->v_lock, name, use_gettext)
8376 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8377}
8378
8379/*
8380 * Check if a variable name is valid.
8381 * Return FALSE and give an error if not.
8382 */
8383 int
8384valid_varname(char_u *varname)
8385{
8386 char_u *p;
8387
8388 for (p = varname; *p != NUL; ++p)
8389 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8390 && *p != AUTOLOAD_CHAR)
8391 {
8392 semsg(_(e_illvar), varname);
8393 return FALSE;
8394 }
8395 return TRUE;
8396}
8397
8398/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008399 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008401 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008402 * It is OK for "from" and "to" to point to the same item. This is used to
8403 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008404 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008406copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008408 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008409 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008410 switch (from->v_type)
8411 {
8412 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008413 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008414 to->vval.v_number = from->vval.v_number;
8415 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008416 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008417#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008418 to->vval.v_float = from->vval.v_float;
8419 break;
8420#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008421 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008422#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008423 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008424 if (to->vval.v_job != NULL)
8425 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008426 break;
8427#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008428 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008429#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008430 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008431 if (to->vval.v_channel != NULL)
8432 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008433 break;
8434#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008435 case VAR_STRING:
8436 case VAR_FUNC:
8437 if (from->vval.v_string == NULL)
8438 to->vval.v_string = NULL;
8439 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008441 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008442 if (from->v_type == VAR_FUNC)
8443 func_ref(to->vval.v_string);
8444 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008445 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008446 case VAR_PARTIAL:
8447 if (from->vval.v_partial == NULL)
8448 to->vval.v_partial = NULL;
8449 else
8450 {
8451 to->vval.v_partial = from->vval.v_partial;
8452 ++to->vval.v_partial->pt_refcount;
8453 }
8454 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008455 case VAR_BLOB:
8456 if (from->vval.v_blob == NULL)
8457 to->vval.v_blob = NULL;
8458 else
8459 {
8460 to->vval.v_blob = from->vval.v_blob;
8461 ++to->vval.v_blob->bv_refcount;
8462 }
8463 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008464 case VAR_LIST:
8465 if (from->vval.v_list == NULL)
8466 to->vval.v_list = NULL;
8467 else
8468 {
8469 to->vval.v_list = from->vval.v_list;
8470 ++to->vval.v_list->lv_refcount;
8471 }
8472 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008473 case VAR_DICT:
8474 if (from->vval.v_dict == NULL)
8475 to->vval.v_dict = NULL;
8476 else
8477 {
8478 to->vval.v_dict = from->vval.v_dict;
8479 ++to->vval.v_dict->dv_refcount;
8480 }
8481 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008482 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008483 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 break;
8485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486}
8487
8488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 * Make a copy of an item.
8490 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008491 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8492 * reference to an already copied list/dict can be used.
8493 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008496item_copy(
8497 typval_T *from,
8498 typval_T *to,
8499 int deep,
8500 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501{
8502 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008503 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008504
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008507 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008508 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008509 }
8510 ++recurse;
8511
8512 switch (from->v_type)
8513 {
8514 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008515 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 case VAR_STRING:
8517 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008518 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008519 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008520 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008521 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 copy_tv(from, to);
8523 break;
8524 case VAR_LIST:
8525 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008526 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008527 if (from->vval.v_list == NULL)
8528 to->vval.v_list = NULL;
8529 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8530 {
8531 /* use the copy made earlier */
8532 to->vval.v_list = from->vval.v_list->lv_copylist;
8533 ++to->vval.v_list->lv_refcount;
8534 }
8535 else
8536 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8537 if (to->vval.v_list == NULL)
8538 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008540 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008541 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008542 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 case VAR_DICT:
8544 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008545 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008546 if (from->vval.v_dict == NULL)
8547 to->vval.v_dict = NULL;
8548 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8549 {
8550 /* use the copy made earlier */
8551 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8552 ++to->vval.v_dict->dv_refcount;
8553 }
8554 else
8555 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8556 if (to->vval.v_dict == NULL)
8557 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008558 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008559 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008560 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008561 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008562 }
8563 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008564 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565}
8566
8567/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 * This function is used by f_input() and f_inputdialog() functions. The third
8569 * argument to f_input() specifies the type of completion to use at the
8570 * prompt. The third argument to f_inputdialog() specifies the value to return
8571 * when the user cancels the prompt.
8572 */
8573 void
8574get_user_input(
8575 typval_T *argvars,
8576 typval_T *rettv,
8577 int inputdialog,
8578 int secret)
8579{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 char_u *p = NULL;
8582 int c;
8583 char_u buf[NUMBUFLEN];
8584 int cmd_silent_save = cmd_silent;
8585 char_u *defstr = (char_u *)"";
8586 int xp_type = EXPAND_NOTHING;
8587 char_u *xp_arg = NULL;
8588
8589 rettv->v_type = VAR_STRING;
8590 rettv->vval.v_string = NULL;
8591
8592#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008593 /* While starting up, there is no place to enter text. When running tests
8594 * with --not-a-term we assume feedkeys() will be used. */
8595 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 return;
8597#endif
8598
8599 cmd_silent = FALSE; /* Want to see the prompt. */
8600 if (prompt != NULL)
8601 {
8602 /* Only the part of the message after the last NL is considered as
8603 * prompt for the command line */
8604 p = vim_strrchr(prompt, '\n');
8605 if (p == NULL)
8606 p = prompt;
8607 else
8608 {
8609 ++p;
8610 c = *p;
8611 *p = NUL;
8612 msg_start();
8613 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008614 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008615 msg_didout = FALSE;
8616 msg_starthere();
8617 *p = c;
8618 }
8619 cmdline_row = msg_row;
8620
8621 if (argvars[1].v_type != VAR_UNKNOWN)
8622 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008623 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 if (defstr != NULL)
8625 stuffReadbuffSpec(defstr);
8626
8627 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8628 {
8629 char_u *xp_name;
8630 int xp_namelen;
8631 long argt;
8632
8633 /* input() with a third argument: completion */
8634 rettv->vval.v_string = NULL;
8635
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008636 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 if (xp_name == NULL)
8638 return;
8639
8640 xp_namelen = (int)STRLEN(xp_name);
8641
8642 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8643 &xp_arg) == FAIL)
8644 return;
8645 }
8646 }
8647
8648 if (defstr != NULL)
8649 {
8650 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 ex_normal_busy = 0;
8653 rettv->vval.v_string =
8654 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8655 xp_type, xp_arg);
8656 ex_normal_busy = save_ex_normal_busy;
8657 }
8658 if (inputdialog && rettv->vval.v_string == NULL
8659 && argvars[1].v_type != VAR_UNKNOWN
8660 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 &argvars[2], buf));
8663
8664 vim_free(xp_arg);
8665
8666 /* since the user typed this, no need to wait for return */
8667 need_wait_return = FALSE;
8668 msg_didout = FALSE;
8669 }
8670 cmd_silent = cmd_silent_save;
8671}
8672
8673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 * ":echo expr1 ..." print each argument separated with a space, add a
8675 * newline at the end.
8676 * ":echon expr1 ..." print each argument plain.
8677 */
8678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008679ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 char_u *p;
8685 int needclr = TRUE;
8686 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008687 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008688 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008689 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690
8691 if (eap->skip)
8692 ++emsg_skip;
8693 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8694 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695 /* If eval1() causes an error message the text from the command may
8696 * still need to be cleared. E.g., "echo 22,44". */
8697 need_clr_eos = needclr;
8698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
8702 /*
8703 * Report the invalid expression unless the expression evaluation
8704 * has been cancelled due to an aborting error, an interrupt, or an
8705 * exception.
8706 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008707 if (!aborting() && did_emsg == did_emsg_before
8708 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008709 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 break;
8712 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008713 need_clr_eos = FALSE;
8714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 if (!eap->skip)
8716 {
8717 if (atstart)
8718 {
8719 atstart = FALSE;
8720 /* Call msg_start() after eval1(), evaluating the expression
8721 * may cause a message to appear. */
8722 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008723 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008724 /* Mark the saved text as finishing the line, so that what
8725 * follows is displayed on a new line when scrolling back
8726 * at the more prompt. */
8727 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 }
8731 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008732 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008733 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008734 if (p != NULL)
8735 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008737 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008739 if (*p != TAB && needclr)
8740 {
8741 /* remove any text still there from the command */
8742 msg_clr_eos();
8743 needclr = FALSE;
8744 }
8745 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 }
8747 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008748 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008749 if (has_mbyte)
8750 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008751 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008752
8753 (void)msg_outtrans_len_attr(p, i, echo_attr);
8754 p += i - 1;
8755 }
8756 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008757 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008760 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 arg = skipwhite(arg);
8764 }
8765 eap->nextcmd = check_nextcmd(arg);
8766
8767 if (eap->skip)
8768 --emsg_skip;
8769 else
8770 {
8771 /* remove text that may still be there from the command */
8772 if (needclr)
8773 msg_clr_eos();
8774 if (eap->cmdidx == CMD_echo)
8775 msg_end();
8776 }
8777}
8778
8779/*
8780 * ":echohl {name}".
8781 */
8782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008783ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008785 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786}
8787
8788/*
8789 * ":execute expr1 ..." execute the result of an expression.
8790 * ":echomsg expr1 ..." Print a message
8791 * ":echoerr expr1 ..." Print an error
8792 * Each gets spaces around each argument and a newline at the end for
8793 * echo commands
8794 */
8795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008796ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
8798 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008799 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 int ret = OK;
8801 char_u *p;
8802 garray_T ga;
8803 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008804 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
8806 ga_init2(&ga, 1, 80);
8807
8808 if (eap->skip)
8809 ++emsg_skip;
8810 while (*arg != NUL && *arg != '|' && *arg != '\n')
8811 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008812 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8813 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
8816 if (!eap->skip)
8817 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008818 char_u buf[NUMBUFLEN];
8819
8820 if (eap->cmdidx == CMD_execute)
8821 p = tv_get_string_buf(&rettv, buf);
8822 else
8823 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 len = (int)STRLEN(p);
8825 if (ga_grow(&ga, len + 2) == FAIL)
8826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 ret = FAIL;
8829 break;
8830 }
8831 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 ga.ga_len += len;
8835 }
8836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 arg = skipwhite(arg);
8839 }
8840
8841 if (ret != FAIL && ga.ga_data != NULL)
8842 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008843 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8844 {
8845 /* Mark the already saved text as finishing the line, so that what
8846 * follows is displayed on a new line when scrolling back at the
8847 * more prompt. */
8848 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008849 }
8850
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008852 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008853 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008854 out_flush();
8855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 else if (eap->cmdidx == CMD_echoerr)
8857 {
8858 /* We don't want to abort following commands, restore did_emsg. */
8859 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008860 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (!force_abort)
8862 did_emsg = save_did_emsg;
8863 }
8864 else if (eap->cmdidx == CMD_execute)
8865 do_cmdline((char_u *)ga.ga_data,
8866 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8867 }
8868
8869 ga_clear(&ga);
8870
8871 if (eap->skip)
8872 --emsg_skip;
8873
8874 eap->nextcmd = check_nextcmd(arg);
8875}
8876
8877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 * Find window specified by "vp" in tabpage "tp".
8879 */
8880 win_T *
8881find_win_by_nr(
8882 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008883 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008886 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 if (nr < 0)
8889 return NULL;
8890 if (nr == 0)
8891 return curwin;
8892
Bram Moolenaar29323592016-07-24 22:04:11 +02008893 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8894 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 if (nr >= LOWEST_WIN_ID)
8896 {
8897 if (wp->w_id == nr)
8898 return wp;
8899 }
8900 else if (--nr <= 0)
8901 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008902 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008904 {
8905#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008906 // check tab-local popup windows
8907 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008908 if (wp->w_id == nr)
8909 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008910 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008911 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8912 if (wp->w_id == nr)
8913 return wp;
8914#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918}
8919
8920/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008921 * Find a window: When using a Window ID in any tab page, when using a number
8922 * in the current tab page.
8923 */
8924 win_T *
8925find_win_by_nr_or_id(typval_T *vp)
8926{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008927 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008928
8929 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008930 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008931 return find_win_by_nr(vp, NULL);
8932}
8933
8934/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008936 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 */
8938 win_T *
8939find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008940 typval_T *wvp, // VAR_UNKNOWN for current window
8941 typval_T *tvp, // VAR_UNKNOWN for current tab page
8942 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943{
8944 win_T *wp = NULL;
8945 tabpage_T *tp = NULL;
8946 long n;
8947
8948 if (wvp->v_type != VAR_UNKNOWN)
8949 {
8950 if (tvp->v_type != VAR_UNKNOWN)
8951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008952 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008953 if (n >= 0)
8954 tp = find_tabpage(n);
8955 }
8956 else
8957 tp = curtab;
8958
8959 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008960 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008962 if (wp == NULL && wvp->v_type == VAR_NUMBER
8963 && wvp->vval.v_number != -1)
8964 // A window with the specified number is not found
8965 tp = NULL;
8966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 }
8968 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008969 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008971 tp = curtab;
8972 }
8973
8974 if (ptp != NULL)
8975 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976
8977 return wp;
8978}
8979
8980/*
8981 * getwinvar() and gettabwinvar()
8982 */
8983 void
8984getwinvar(
8985 typval_T *argvars,
8986 typval_T *rettv,
8987 int off) /* 1 for gettabwinvar() */
8988{
8989 win_T *win;
8990 char_u *varname;
8991 dictitem_T *v;
8992 tabpage_T *tp = NULL;
8993 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994 win_T *oldcurwin;
8995 tabpage_T *oldtabpage;
8996 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008999 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000 else
9001 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009003 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 ++emsg_off;
9005
9006 rettv->v_type = VAR_STRING;
9007 rettv->vval.v_string = NULL;
9008
9009 if (win != NULL && varname != NULL)
9010 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 /* Set curwin to be our win, temporarily. Also set the tabpage,
9012 * otherwise the window is not valid. Only do this when needed,
9013 * autocommands get blocked. */
9014 need_switch_win = !(tp == curtab && win == curwin);
9015 if (!need_switch_win
9016 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009018 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009020 if (varname[1] == NUL)
9021 {
9022 /* get all window-local options in a dict */
9023 dict_T *opts = get_winbuf_options(FALSE);
9024
9025 if (opts != NULL)
9026 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009027 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009028 done = TRUE;
9029 }
9030 }
9031 else if (get_option_tv(&varname, rettv, 1) == OK)
9032 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 done = TRUE;
9034 }
9035 else
9036 {
9037 /* Look up the variable. */
9038 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9039 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9040 varname, FALSE);
9041 if (v != NULL)
9042 {
9043 copy_tv(&v->di_tv, rettv);
9044 done = TRUE;
9045 }
9046 }
9047 }
9048
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 if (need_switch_win)
9050 /* restore previous notion of curwin */
9051 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009052 }
9053
9054 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9055 /* use the default return value */
9056 copy_tv(&argvars[off + 2], rettv);
9057
9058 --emsg_off;
9059}
9060
9061/*
9062 * "setwinvar()" and "settabwinvar()" functions
9063 */
9064 void
9065setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9066{
9067 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 win_T *save_curwin;
9069 tabpage_T *save_curtab;
9070 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 char_u *varname, *winvarname;
9072 typval_T *varp;
9073 char_u nbuf[NUMBUFLEN];
9074 tabpage_T *tp = NULL;
9075
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009076 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 return;
9078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009080 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 else
9082 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009084 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009085 varp = &argvars[off + 2];
9086
9087 if (win != NULL && varname != NULL && varp != NULL)
9088 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 need_switch_win = !(tp == curtab && win == curwin);
9090 if (!need_switch_win
9091 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 {
9093 if (*varname == '&')
9094 {
9095 long numval;
9096 char_u *strval;
9097 int error = FALSE;
9098
9099 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009100 numval = (long)tv_get_number_chk(varp, &error);
9101 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 if (!error && strval != NULL)
9103 set_option_value(varname, numval, strval, OPT_LOCAL);
9104 }
9105 else
9106 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009107 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108 if (winvarname != NULL)
9109 {
9110 STRCPY(winvarname, "w:");
9111 STRCPY(winvarname + 2, varname);
9112 set_var(winvarname, varp, TRUE);
9113 vim_free(winvarname);
9114 }
9115 }
9116 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 if (need_switch_win)
9118 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 }
9120}
9121
9122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9124 * "arg" points to the "&" or '+' when called, to "option" when returning.
9125 * Returns NULL when no option name found. Otherwise pointer to the char
9126 * after the option name.
9127 */
9128 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009129find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
9131 char_u *p = *arg;
9132
9133 ++p;
9134 if (*p == 'g' && p[1] == ':')
9135 {
9136 *opt_flags = OPT_GLOBAL;
9137 p += 2;
9138 }
9139 else if (*p == 'l' && p[1] == ':')
9140 {
9141 *opt_flags = OPT_LOCAL;
9142 p += 2;
9143 }
9144 else
9145 *opt_flags = 0;
9146
9147 if (!ASCII_ISALPHA(*p))
9148 return NULL;
9149 *arg = p;
9150
9151 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9152 p += 4; /* termcap option */
9153 else
9154 while (ASCII_ISALPHA(*p))
9155 ++p;
9156 return p;
9157}
9158
9159/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009160 * Return the autoload script name for a function or variable name.
9161 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009163 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009164autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009165{
Bram Moolenaara1544c02013-05-30 12:35:52 +02009166 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009167 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009168
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009169 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009170 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009171 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009172 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009173 STRCPY(scriptname, "autoload/");
9174 STRCAT(scriptname, name);
9175 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
9176 STRCAT(scriptname, ".vim");
9177 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
9178 *p = '/';
9179 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009180}
9181
9182/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009183 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009184 * Return TRUE if a package was loaded.
9185 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187script_autoload(
9188 char_u *name,
9189 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009190{
9191 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009192 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009193 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009194 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009195
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009196 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009197 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009198 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009199 return FALSE;
9200
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009201 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009202
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009203 /* Find the name in the list of previously loaded package names. Skip
9204 * "autoload/", it's always the same. */
9205 for (i = 0; i < ga_loaded.ga_len; ++i)
9206 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9207 break;
9208 if (!reload && i < ga_loaded.ga_len)
9209 ret = FALSE; /* was loaded already */
9210 else
9211 {
9212 /* Remember the name if it wasn't loaded already. */
9213 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9214 {
9215 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9216 tofree = NULL;
9217 }
9218
9219 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009220 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009221 ret = TRUE;
9222 }
9223
9224 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009225 return ret;
9226}
9227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9229typedef enum
9230{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009231 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9232 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9233 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234} var_flavour_T;
9235
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009237var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 char_u *p = varname;
9240
9241 if (ASCII_ISUPPER(*p))
9242 {
9243 while (*(++p))
9244 if (ASCII_ISLOWER(*p))
9245 return VAR_FLAVOUR_SESSION;
9246 return VAR_FLAVOUR_VIMINFO;
9247 }
9248 else
9249 return VAR_FLAVOUR_DEFAULT;
9250}
9251#endif
9252
9253#if defined(FEAT_VIMINFO) || defined(PROTO)
9254/*
9255 * Restore global vars that start with a capital from the viminfo file
9256 */
9257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009258read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259{
9260 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009261 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009263 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264
9265 if (!writing && (find_viminfo_parameter('!') != NULL))
9266 {
9267 tab = vim_strchr(virp->vir_line + 1, '\t');
9268 if (tab != NULL)
9269 {
9270 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009271 switch (*tab)
9272 {
9273 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009275 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009276#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009277 case 'D': type = VAR_DICT; break;
9278 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009279 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009280 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282
9283 tab = vim_strchr(tab, '\t');
9284 if (tab != NULL)
9285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009286 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009287 if (type == VAR_STRING || type == VAR_DICT
9288 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009289 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009291#ifdef FEAT_FLOAT
9292 else if (type == VAR_FLOAT)
9293 (void)string2float(tab + 1, &tv.vval.v_float);
9294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009296 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009297 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009298 {
9299 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9300
9301 if (etv == NULL)
9302 /* Failed to parse back the dict or list, use it as a
9303 * string. */
9304 tv.v_type = VAR_STRING;
9305 else
9306 {
9307 vim_free(tv.vval.v_string);
9308 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009309 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009310 }
9311 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009312 else if (type == VAR_BLOB)
9313 {
9314 blob_T *blob = string2blob(tv.vval.v_string);
9315
9316 if (blob == NULL)
9317 // Failed to parse back the blob, use it as a string.
9318 tv.v_type = VAR_STRING;
9319 else
9320 {
9321 vim_free(tv.vval.v_string);
9322 tv.v_type = VAR_BLOB;
9323 tv.vval.v_blob = blob;
9324 }
9325 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009326
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009327 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009328 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009329 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009330 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009331
9332 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009333 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009334 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9335 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009336 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 }
9338 }
9339 }
9340
9341 return viminfo_readline(virp);
9342}
9343
9344/*
9345 * Write global vars that start with a capital to the viminfo file
9346 */
9347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009348write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
Bram Moolenaar33570922005-01-25 22:26:29 +00009350 hashitem_T *hi;
9351 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009352 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009353 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009354 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009356 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357
9358 if (find_viminfo_parameter('!') == NULL)
9359 return;
9360
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009361 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009362
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009363 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009364 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009368 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 this_var = HI2DI(hi);
9370 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009373 {
9374 case VAR_STRING: s = "STR"; break;
9375 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009376 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009377 case VAR_DICT: s = "DIC"; break;
9378 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009379 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009380 case VAR_SPECIAL: s = "XPL"; break;
9381
9382 case VAR_UNKNOWN:
9383 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009384 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009385 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009386 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009387 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009388 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009389 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009390 if (this_var->di_tv.v_type == VAR_SPECIAL)
9391 {
9392 sprintf((char *)numbuf, "%ld",
9393 (long)this_var->di_tv.vval.v_number);
9394 p = numbuf;
9395 tofree = NULL;
9396 }
9397 else
9398 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009399 if (p != NULL)
9400 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009401 vim_free(tofree);
9402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
9404 }
9405}
9406#endif
9407
9408#if defined(FEAT_SESSION) || defined(PROTO)
9409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009410store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
Bram Moolenaar33570922005-01-25 22:26:29 +00009412 hashitem_T *hi;
9413 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009414 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 char_u *p, *t;
9416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009417 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009418 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009420 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009422 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 this_var = HI2DI(hi);
9424 if ((this_var->di_tv.v_type == VAR_NUMBER
9425 || this_var->di_tv.v_type == VAR_STRING)
9426 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009427 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009428 /* Escape special characters with a backslash. Turn a LF and
9429 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009430 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009431 (char_u *)"\\\"\n\r");
9432 if (p == NULL) /* out of memory */
9433 break;
9434 for (t = p; *t != NUL; ++t)
9435 if (*t == '\n')
9436 *t = 'n';
9437 else if (*t == '\r')
9438 *t = 'r';
9439 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 this_var->di_key,
9441 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9442 : ' ',
9443 p,
9444 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9445 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009446 || put_eol(fd) == FAIL)
9447 {
9448 vim_free(p);
9449 return FAIL;
9450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 vim_free(p);
9452 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009453#ifdef FEAT_FLOAT
9454 else if (this_var->di_tv.v_type == VAR_FLOAT
9455 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9456 {
9457 float_T f = this_var->di_tv.vval.v_float;
9458 int sign = ' ';
9459
9460 if (f < 0)
9461 {
9462 f = -f;
9463 sign = '-';
9464 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009465 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009466 this_var->di_key, sign, f) < 0)
9467 || put_eol(fd) == FAIL)
9468 return FAIL;
9469 }
9470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 }
9472 }
9473 return OK;
9474}
9475#endif
9476
Bram Moolenaar661b1822005-07-28 22:36:45 +00009477/*
9478 * Display script name where an item was last set.
9479 * Should only be invoked when 'verbose' is non-zero.
9480 */
9481 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009482last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009483{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009484 char_u *p;
9485
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009486 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009487 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009488 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009489 if (p != NULL)
9490 {
9491 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009492 msg_puts(_("\n\tLast set from "));
9493 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009494 if (script_ctx.sc_lnum > 0)
9495 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009496 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009497 msg_outnum((long)script_ctx.sc_lnum);
9498 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009499 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009500 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009501 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009502 }
9503}
9504
Bram Moolenaar61be3762019-03-19 23:04:17 +01009505/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009506 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9507 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009508 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009509 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009511{
9512 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9513 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009514 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9515 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009516 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009517 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009518}
9519
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009520/*
9521 * Prepare "gap" for an assert error and add the sourcing position.
9522 */
9523 void
9524prepare_assert_error(garray_T *gap)
9525{
9526 char buf[NUMBUFLEN];
9527
9528 ga_init2(gap, 1, 100);
9529 if (sourcing_name != NULL)
9530 {
9531 ga_concat(gap, sourcing_name);
9532 if (sourcing_lnum > 0)
9533 ga_concat(gap, (char_u *)" ");
9534 }
9535 if (sourcing_lnum > 0)
9536 {
9537 sprintf(buf, "line %ld", (long)sourcing_lnum);
9538 ga_concat(gap, (char_u *)buf);
9539 }
9540 if (sourcing_name != NULL || sourcing_lnum > 0)
9541 ga_concat(gap, (char_u *)": ");
9542}
9543
9544/*
9545 * Add an assert error to v:errors.
9546 */
9547 void
9548assert_error(garray_T *gap)
9549{
9550 struct vimvar *vp = &vimvars[VV_ERRORS];
9551
9552 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9553 /* Make sure v:errors is a list. */
9554 set_vim_var_list(VV_ERRORS, list_alloc());
9555 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9556}
9557
Bram Moolenaar65a54642018-04-28 16:56:53 +02009558 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559assert_equal_common(typval_T *argvars, assert_type_T atype)
9560{
9561 garray_T ga;
9562
9563 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9564 != (atype == ASSERT_EQUAL))
9565 {
9566 prepare_assert_error(&ga);
9567 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9568 atype);
9569 assert_error(&ga);
9570 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009571 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009573 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574}
9575
Bram Moolenaar65a54642018-04-28 16:56:53 +02009576 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009577assert_equalfile(typval_T *argvars)
9578{
9579 char_u buf1[NUMBUFLEN];
9580 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009581 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9582 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009583 garray_T ga;
9584 FILE *fd1;
9585 FILE *fd2;
9586
9587 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009588 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009589
9590 IObuff[0] = NUL;
9591 fd1 = mch_fopen((char *)fname1, READBIN);
9592 if (fd1 == NULL)
9593 {
9594 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9595 }
9596 else
9597 {
9598 fd2 = mch_fopen((char *)fname2, READBIN);
9599 if (fd2 == NULL)
9600 {
9601 fclose(fd1);
9602 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9603 }
9604 else
9605 {
9606 int c1, c2;
9607 long count = 0;
9608
9609 for (;;)
9610 {
9611 c1 = fgetc(fd1);
9612 c2 = fgetc(fd2);
9613 if (c1 == EOF)
9614 {
9615 if (c2 != EOF)
9616 STRCPY(IObuff, "first file is shorter");
9617 break;
9618 }
9619 else if (c2 == EOF)
9620 {
9621 STRCPY(IObuff, "second file is shorter");
9622 break;
9623 }
9624 else if (c1 != c2)
9625 {
9626 vim_snprintf((char *)IObuff, IOSIZE,
9627 "difference at byte %ld", count);
9628 break;
9629 }
9630 ++count;
9631 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009632 fclose(fd1);
9633 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009634 }
9635 }
9636 if (IObuff[0] != NUL)
9637 {
9638 prepare_assert_error(&ga);
9639 ga_concat(&ga, IObuff);
9640 assert_error(&ga);
9641 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009642 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009643 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009644 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009645}
9646
Bram Moolenaar65a54642018-04-28 16:56:53 +02009647 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648assert_match_common(typval_T *argvars, assert_type_T atype)
9649{
9650 garray_T ga;
9651 char_u buf1[NUMBUFLEN];
9652 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009653 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9654 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655
9656 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009657 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9659 {
9660 prepare_assert_error(&ga);
9661 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9662 atype);
9663 assert_error(&ga);
9664 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009665 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009666 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009667 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668}
9669
Bram Moolenaar65a54642018-04-28 16:56:53 +02009670 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009671assert_inrange(typval_T *argvars)
9672{
9673 garray_T ga;
9674 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009675 char_u *tofree;
9676 char msg[200];
9677 char_u numbuf[NUMBUFLEN];
9678
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009679#ifdef FEAT_FLOAT
9680 if (argvars[0].v_type == VAR_FLOAT
9681 || argvars[1].v_type == VAR_FLOAT
9682 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009683 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009684 float_T flower = tv_get_float(&argvars[0]);
9685 float_T fupper = tv_get_float(&argvars[1]);
9686 float_T factual = tv_get_float(&argvars[2]);
9687
9688 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009689 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009690 prepare_assert_error(&ga);
9691 if (argvars[3].v_type != VAR_UNKNOWN)
9692 {
9693 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9694 vim_free(tofree);
9695 }
9696 else
9697 {
9698 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9699 flower, fupper, factual);
9700 ga_concat(&ga, (char_u *)msg);
9701 }
9702 assert_error(&ga);
9703 ga_clear(&ga);
9704 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009705 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009706 }
9707 else
9708#endif
9709 {
9710 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9711 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9712 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9713
9714 if (error)
9715 return 0;
9716 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009717 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009718 prepare_assert_error(&ga);
9719 if (argvars[3].v_type != VAR_UNKNOWN)
9720 {
9721 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9722 vim_free(tofree);
9723 }
9724 else
9725 {
9726 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009727 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009728 ga_concat(&ga, (char_u *)msg);
9729 }
9730 assert_error(&ga);
9731 ga_clear(&ga);
9732 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009733 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009734 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009735 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009736}
9737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009738/*
9739 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009740 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009742 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009743assert_bool(typval_T *argvars, int isTrue)
9744{
9745 int error = FALSE;
9746 garray_T ga;
9747
9748 if (argvars[0].v_type == VAR_SPECIAL
9749 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009750 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009752 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009753 || error)
9754 {
9755 prepare_assert_error(&ga);
9756 fill_assert_error(&ga, &argvars[1],
9757 (char_u *)(isTrue ? "True" : "False"),
9758 NULL, &argvars[0], ASSERT_OTHER);
9759 assert_error(&ga);
9760 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009761 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009763 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764}
9765
Bram Moolenaar65a54642018-04-28 16:56:53 +02009766 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009767assert_report(typval_T *argvars)
9768{
9769 garray_T ga;
9770
9771 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009772 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009773 assert_error(&ga);
9774 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009775 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009776}
9777
Bram Moolenaar65a54642018-04-28 16:56:53 +02009778 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779assert_exception(typval_T *argvars)
9780{
9781 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009782 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783
9784 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9785 {
9786 prepare_assert_error(&ga);
9787 ga_concat(&ga, (char_u *)"v:exception is not set");
9788 assert_error(&ga);
9789 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009790 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 }
9792 else if (error != NULL
9793 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9794 {
9795 prepare_assert_error(&ga);
9796 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9797 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9798 assert_error(&ga);
9799 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009800 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009802 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803}
9804
Bram Moolenaar65a54642018-04-28 16:56:53 +02009805 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009806assert_beeps(typval_T *argvars)
9807{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009808 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009809 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009810 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009811
9812 called_vim_beep = FALSE;
9813 suppress_errthrow = TRUE;
9814 emsg_silent = FALSE;
9815 do_cmdline_cmd(cmd);
9816 if (!called_vim_beep)
9817 {
9818 prepare_assert_error(&ga);
9819 ga_concat(&ga, (char_u *)"command did not beep: ");
9820 ga_concat(&ga, cmd);
9821 assert_error(&ga);
9822 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009823 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009824 }
9825
9826 suppress_errthrow = FALSE;
9827 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009828 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009829}
9830
Bram Moolenaar25190db2019-05-04 15:05:28 +02009831 static void
9832assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9833{
9834 char_u *tofree;
9835 char_u numbuf[NUMBUFLEN];
9836
9837 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9838 {
9839 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9840 vim_free(tofree);
9841 }
9842 else
9843 ga_concat(gap, cmd);
9844}
9845
Bram Moolenaar65a54642018-04-28 16:56:53 +02009846 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847assert_fails(typval_T *argvars)
9848{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009849 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009851 int ret = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852
9853 called_emsg = FALSE;
9854 suppress_errthrow = TRUE;
9855 emsg_silent = TRUE;
9856 do_cmdline_cmd(cmd);
9857 if (!called_emsg)
9858 {
9859 prepare_assert_error(&ga);
9860 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009861 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862 assert_error(&ga);
9863 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009864 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009865 }
9866 else if (argvars[1].v_type != VAR_UNKNOWN)
9867 {
9868 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009869 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870
9871 if (error == NULL
9872 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9873 {
9874 prepare_assert_error(&ga);
9875 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9876 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009877 ga_concat(&ga, (char_u *)": ");
9878 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879 assert_error(&ga);
9880 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009881 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 }
9883 }
9884
9885 called_emsg = FALSE;
9886 suppress_errthrow = FALSE;
9887 emsg_silent = FALSE;
9888 emsg_on_display = FALSE;
9889 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009890 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009891}
9892
9893/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009894 * Append "p[clen]" to "gap", escaping unprintable characters.
9895 * Changes NL to \n, CR to \r, etc.
9896 */
9897 static void
9898ga_concat_esc(garray_T *gap, char_u *p, int clen)
9899{
9900 char_u buf[NUMBUFLEN];
9901
9902 if (clen > 1)
9903 {
9904 mch_memmove(buf, p, clen);
9905 buf[clen] = NUL;
9906 ga_concat(gap, buf);
9907 }
9908 else switch (*p)
9909 {
9910 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9911 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9912 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9913 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9914 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9915 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9916 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9917 default:
9918 if (*p < ' ')
9919 {
9920 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9921 ga_concat(gap, buf);
9922 }
9923 else
9924 ga_append(gap, *p);
9925 break;
9926 }
9927}
9928
9929/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 * Append "str" to "gap", escaping unprintable characters.
9931 * Changes NL to \n, CR to \r, etc.
9932 */
9933 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009934ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935{
9936 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009937 char_u *s;
9938 int c;
9939 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009941 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009942
9943 if (str == NULL)
9944 {
9945 ga_concat(gap, (char_u *)"NULL");
9946 return;
9947 }
9948
9949 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009950 {
9951 same_len = 1;
9952 s = p;
9953 c = mb_ptr2char_adv(&s);
9954 clen = s - p;
9955 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009957 ++same_len;
9958 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009960 if (same_len > 20)
9961 {
9962 ga_concat(gap, (char_u *)"\\[");
9963 ga_concat_esc(gap, p, clen);
9964 ga_concat(gap, (char_u *)" occurs ");
9965 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9966 ga_concat(gap, buf);
9967 ga_concat(gap, (char_u *)" times]");
9968 p = s - 1;
9969 }
9970 else
9971 ga_concat_esc(gap, p, clen);
9972 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973}
9974
9975/*
9976 * Fill "gap" with information about an assert error.
9977 */
9978 void
9979fill_assert_error(
9980 garray_T *gap,
9981 typval_T *opt_msg_tv,
9982 char_u *exp_str,
9983 typval_T *exp_tv,
9984 typval_T *got_tv,
9985 assert_type_T atype)
9986{
9987 char_u numbuf[NUMBUFLEN];
9988 char_u *tofree;
9989
9990 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9991 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009992 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9993 vim_free(tofree);
9994 ga_concat(gap, (char_u *)": ");
9995 }
9996
9997 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9998 ga_concat(gap, (char_u *)"Pattern ");
9999 else if (atype == ASSERT_NOTEQUAL)
10000 ga_concat(gap, (char_u *)"Expected not equal to ");
10001 else
10002 ga_concat(gap, (char_u *)"Expected ");
10003 if (exp_str == NULL)
10004 {
Bram Moolenaar86576712019-01-25 20:48:33 +010010005 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 vim_free(tofree);
10007 }
10008 else
Bram Moolenaar86576712019-01-25 20:48:33 +010010009 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010010 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010012 if (atype == ASSERT_MATCH)
10013 ga_concat(gap, (char_u *)" does not match ");
10014 else if (atype == ASSERT_NOTMATCH)
10015 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010016 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010017 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +010010018 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010019 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020 }
10021}
10022
Bram Moolenaar31988702018-02-13 12:57:42 +010010023/*
10024 * Compare "typ1" and "typ2". Put the result in "typ1".
10025 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010026 int
10027typval_compare(
10028 typval_T *typ1, /* first operand */
10029 typval_T *typ2, /* second operand */
10030 exptype_T type, /* operator */
10031 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +010010032 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010033{
10034 int i;
10035 varnumber_T n1, n2;
10036 char_u *s1, *s2;
10037 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
10038
Bram Moolenaar31988702018-02-13 12:57:42 +010010039 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010040 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010041 /* For "is" a different type always means FALSE, for "notis"
10042 * it means TRUE. */
10043 n1 = (type == TYPE_NEQUAL);
10044 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010045 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
10046 {
10047 if (type_is)
10048 {
10049 n1 = (typ1->v_type == typ2->v_type
10050 && typ1->vval.v_blob == typ2->vval.v_blob);
10051 if (type == TYPE_NEQUAL)
10052 n1 = !n1;
10053 }
10054 else if (typ1->v_type != typ2->v_type
10055 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10056 {
10057 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010058 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010059 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010060 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010061 clear_tv(typ1);
10062 return FAIL;
10063 }
10064 else
10065 {
10066 // Compare two Blobs for being equal or unequal.
10067 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
10068 if (type == TYPE_NEQUAL)
10069 n1 = !n1;
10070 }
10071 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010072 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
10073 {
10074 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010075 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010076 n1 = (typ1->v_type == typ2->v_type
10077 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010078 if (type == TYPE_NEQUAL)
10079 n1 = !n1;
10080 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010081 else if (typ1->v_type != typ2->v_type
10082 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010083 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010084 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010085 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010086 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010087 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010088 clear_tv(typ1);
10089 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010090 }
10091 else
10092 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010093 /* Compare two Lists for being equal or unequal. */
10094 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10095 ic, FALSE);
10096 if (type == TYPE_NEQUAL)
10097 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010098 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010099 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010100
10101 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10102 {
10103 if (type_is)
10104 {
10105 n1 = (typ1->v_type == typ2->v_type
10106 && typ1->vval.v_dict == typ2->vval.v_dict);
10107 if (type == TYPE_NEQUAL)
10108 n1 = !n1;
10109 }
10110 else if (typ1->v_type != typ2->v_type
10111 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10112 {
10113 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010114 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010115 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010116 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010117 clear_tv(typ1);
10118 return FAIL;
10119 }
10120 else
10121 {
10122 /* Compare two Dictionaries for being equal or unequal. */
10123 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10124 ic, FALSE);
10125 if (type == TYPE_NEQUAL)
10126 n1 = !n1;
10127 }
10128 }
10129
10130 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10131 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10132 {
10133 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010135 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010136 clear_tv(typ1);
10137 return FAIL;
10138 }
10139 if ((typ1->v_type == VAR_PARTIAL
10140 && typ1->vval.v_partial == NULL)
10141 || (typ2->v_type == VAR_PARTIAL
10142 && typ2->vval.v_partial == NULL))
10143 /* when a partial is NULL assume not equal */
10144 n1 = FALSE;
10145 else if (type_is)
10146 {
10147 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10148 /* strings are considered the same if their value is
10149 * the same */
10150 n1 = tv_equal(typ1, typ2, ic, FALSE);
10151 else if (typ1->v_type == VAR_PARTIAL
10152 && typ2->v_type == VAR_PARTIAL)
10153 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10154 else
10155 n1 = FALSE;
10156 }
10157 else
10158 n1 = tv_equal(typ1, typ2, ic, FALSE);
10159 if (type == TYPE_NEQUAL)
10160 n1 = !n1;
10161 }
10162
10163#ifdef FEAT_FLOAT
10164 /*
10165 * If one of the two variables is a float, compare as a float.
10166 * When using "=~" or "!~", always compare as string.
10167 */
10168 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10169 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10170 {
10171 float_T f1, f2;
10172
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010173 f1 = tv_get_float(typ1);
10174 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010175 n1 = FALSE;
10176 switch (type)
10177 {
10178 case TYPE_EQUAL: n1 = (f1 == f2); break;
10179 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10180 case TYPE_GREATER: n1 = (f1 > f2); break;
10181 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10182 case TYPE_SMALLER: n1 = (f1 < f2); break;
10183 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10184 case TYPE_UNKNOWN:
10185 case TYPE_MATCH:
10186 case TYPE_NOMATCH: break; /* avoid gcc warning */
10187 }
10188 }
10189#endif
10190
10191 /*
10192 * If one of the two variables is a number, compare as a number.
10193 * When using "=~" or "!~", always compare as string.
10194 */
10195 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10196 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10197 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010198 n1 = tv_get_number(typ1);
10199 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010200 switch (type)
10201 {
10202 case TYPE_EQUAL: n1 = (n1 == n2); break;
10203 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10204 case TYPE_GREATER: n1 = (n1 > n2); break;
10205 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10206 case TYPE_SMALLER: n1 = (n1 < n2); break;
10207 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10208 case TYPE_UNKNOWN:
10209 case TYPE_MATCH:
10210 case TYPE_NOMATCH: break; /* avoid gcc warning */
10211 }
10212 }
10213 else
10214 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010215 s1 = tv_get_string_buf(typ1, buf1);
10216 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010217 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10218 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10219 else
10220 i = 0;
10221 n1 = FALSE;
10222 switch (type)
10223 {
10224 case TYPE_EQUAL: n1 = (i == 0); break;
10225 case TYPE_NEQUAL: n1 = (i != 0); break;
10226 case TYPE_GREATER: n1 = (i > 0); break;
10227 case TYPE_GEQUAL: n1 = (i >= 0); break;
10228 case TYPE_SMALLER: n1 = (i < 0); break;
10229 case TYPE_SEQUAL: n1 = (i <= 0); break;
10230
10231 case TYPE_MATCH:
10232 case TYPE_NOMATCH:
10233 n1 = pattern_match(s2, s1, ic);
10234 if (type == TYPE_NOMATCH)
10235 n1 = !n1;
10236 break;
10237
10238 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10239 }
10240 }
10241 clear_tv(typ1);
10242 typ1->v_type = VAR_NUMBER;
10243 typ1->vval.v_number = n1;
10244
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010245 return OK;
10246}
10247
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010248 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010249typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010250{
10251 char_u *tofree;
10252 char_u numbuf[NUMBUFLEN];
10253 char_u *ret = NULL;
10254
10255 if (arg == NULL)
10256 return vim_strsave((char_u *)"(does not exist)");
10257 ret = tv2string(arg, &tofree, numbuf, 0);
10258 /* Make a copy if we have a value but it's not in allocated memory. */
10259 if (ret != NULL && tofree == NULL)
10260 ret = vim_strsave(ret);
10261 return ret;
10262}
10263
10264 int
10265var_exists(char_u *var)
10266{
10267 char_u *name;
10268 char_u *tofree;
10269 typval_T tv;
10270 int len = 0;
10271 int n = FALSE;
10272
10273 /* get_name_len() takes care of expanding curly braces */
10274 name = var;
10275 len = get_name_len(&var, &tofree, TRUE, FALSE);
10276 if (len > 0)
10277 {
10278 if (tofree != NULL)
10279 name = tofree;
10280 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10281 if (n)
10282 {
10283 /* handle d.key, l[idx], f(expr) */
10284 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10285 if (n)
10286 clear_tv(&tv);
10287 }
10288 }
10289 if (*var != NUL)
10290 n = FALSE;
10291
10292 vim_free(tofree);
10293 return n;
10294}
10295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296#endif /* FEAT_EVAL */
10297
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010299#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300
Bram Moolenaar4f974752019-02-17 17:44:42 +010010301#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302/*
10303 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305
10306/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010307 * Get the short path (8.3) for the filename in "fnamep".
10308 * Only works for a valid file name.
10309 * When the path gets longer "fnamep" is changed and the allocated buffer
10310 * is put in "bufp".
10311 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10312 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 */
10314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010315get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010317 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 char_u *newbuf;
10319
10320 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010321 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 if (l > len - 1)
10323 {
10324 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010325 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 newbuf = vim_strnsave(*fnamep, l);
10327 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329
10330 vim_free(*bufp);
10331 *fnamep = *bufp = newbuf;
10332
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010333 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010334 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 }
10336
10337 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010338 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339}
10340
10341/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010342 * Get the short path (8.3) for the filename in "fname". The converted
10343 * path is returned in "bufp".
10344 *
10345 * Some of the directories specified in "fname" may not exist. This function
10346 * will shorten the existing directories at the beginning of the path and then
10347 * append the remaining non-existing path.
10348 *
10349 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010350 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010351 * bufp - Pointer to an allocated buffer for the filename.
10352 * fnamelen - Length of the filename pointed to by fname
10353 *
10354 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 */
10356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010357shortpath_for_invalid_fname(
10358 char_u **fname,
10359 char_u **bufp,
10360 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010362 char_u *short_fname, *save_fname, *pbuf_unused;
10363 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010365 int old_len, len;
10366 int new_len, sfx_len;
10367 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368
10369 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010370 old_len = *fnamelen;
10371 save_fname = vim_strnsave(*fname, old_len);
10372 pbuf_unused = NULL;
10373 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010375 endp = save_fname + old_len - 1; /* Find the end of the copy */
10376 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010378 /*
10379 * Try shortening the supplied path till it succeeds by removing one
10380 * directory at a time from the tail of the path.
10381 */
10382 len = 0;
10383 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010385 /* go back one path-separator */
10386 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10387 --endp;
10388 if (endp <= save_fname)
10389 break; /* processed the complete path */
10390
10391 /*
10392 * Replace the path separator with a NUL and try to shorten the
10393 * resulting path.
10394 */
10395 ch = *endp;
10396 *endp = 0;
10397 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010398 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010399 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10400 {
10401 retval = FAIL;
10402 goto theend;
10403 }
10404 *endp = ch; /* preserve the string */
10405
10406 if (len > 0)
10407 break; /* successfully shortened the path */
10408
10409 /* failed to shorten the path. Skip the path separator */
10410 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 }
10412
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010413 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010415 /*
10416 * Succeeded in shortening the path. Now concatenate the shortened
10417 * path with the remaining path at the tail.
10418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010420 /* Compute the length of the new path. */
10421 sfx_len = (int)(save_endp - endp) + 1;
10422 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010424 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010426 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010428 /* There is not enough space in the currently allocated string,
10429 * copy it to a buffer big enough. */
10430 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010432 {
10433 retval = FAIL;
10434 goto theend;
10435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 }
10437 else
10438 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010439 /* Transfer short_fname to the main buffer (it's big enough),
10440 * unless get_short_pathname() did its work in-place. */
10441 *fname = *bufp = save_fname;
10442 if (short_fname != save_fname)
10443 vim_strncpy(save_fname, short_fname, len);
10444 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010446
10447 /* concat the not-shortened part of the path */
10448 vim_strncpy(*fname + len, endp, sfx_len);
10449 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010451
10452theend:
10453 vim_free(pbuf_unused);
10454 vim_free(save_fname);
10455
10456 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457}
10458
10459/*
10460 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010461 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 */
10463 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010464shortpath_for_partial(
10465 char_u **fnamep,
10466 char_u **bufp,
10467 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468{
10469 int sepcount, len, tflen;
10470 char_u *p;
10471 char_u *pbuf, *tfname;
10472 int hasTilde;
10473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010474 /* Count up the path separators from the RHS.. so we know which part
10475 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010477 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 if (vim_ispathsep(*p))
10479 ++sepcount;
10480
10481 /* Need full path first (use expand_env() to remove a "~/") */
10482 hasTilde = (**fnamep == '~');
10483 if (hasTilde)
10484 pbuf = tfname = expand_env_save(*fnamep);
10485 else
10486 pbuf = tfname = FullName_save(*fnamep, FALSE);
10487
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010488 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010490 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492
10493 if (len == 0)
10494 {
10495 /* Don't have a valid filename, so shorten the rest of the
10496 * path if we can. This CAN give us invalid 8.3 filenames, but
10497 * there's not a lot of point in guessing what it might be.
10498 */
10499 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010500 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10501 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 }
10503
10504 /* Count the paths backward to find the beginning of the desired string. */
10505 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010506 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010507 if (has_mbyte)
10508 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 if (vim_ispathsep(*p))
10510 {
10511 if (sepcount == 0 || (hasTilde && sepcount == 1))
10512 break;
10513 else
10514 sepcount --;
10515 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 if (hasTilde)
10518 {
10519 --p;
10520 if (p >= tfname)
10521 *p = '~';
10522 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010523 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 }
10525 else
10526 ++p;
10527
10528 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10529 vim_free(*bufp);
10530 *fnamelen = (int)STRLEN(p);
10531 *bufp = pbuf;
10532 *fnamep = p;
10533
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010534 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010536#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537
10538/*
10539 * Adjust a filename, according to a string of modifiers.
10540 * *fnamep must be NUL terminated when called. When returning, the length is
10541 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010542 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 * When there is an error, *fnamep is set to NULL.
10544 */
10545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010547 char_u *src, // string with modifiers
10548 int tilde_file, // "~" is a file name, not $HOME
10549 int *usedlen, // characters after src that are used
10550 char_u **fnamep, // file name so far
10551 char_u **bufp, // buffer for allocated file name or NULL
10552 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 int valid = 0;
10555 char_u *tail;
10556 char_u *s, *p, *pbuf;
10557 char_u dirname[MAXPATHL];
10558 int c;
10559 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010560#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010561 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 int has_shortname = 0;
10563#endif
10564
10565repeat:
10566 /* ":p" - full path/file_name */
10567 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10568 {
10569 has_fullname = 1;
10570
10571 valid |= VALID_PATH;
10572 *usedlen += 2;
10573
10574 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10575 if ((*fnamep)[0] == '~'
10576#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10577 && ((*fnamep)[1] == '/'
10578# ifdef BACKSLASH_IN_FILENAME
10579 || (*fnamep)[1] == '\\'
10580# endif
10581 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010583 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 )
10585 {
10586 *fnamep = expand_env_save(*fnamep);
10587 vim_free(*bufp); /* free any allocated file name */
10588 *bufp = *fnamep;
10589 if (*fnamep == NULL)
10590 return -1;
10591 }
10592
10593 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010594 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 {
10596 if (vim_ispathsep(*p)
10597 && p[1] == '.'
10598 && (p[2] == NUL
10599 || vim_ispathsep(p[2])
10600 || (p[2] == '.'
10601 && (p[3] == NUL || vim_ispathsep(p[3])))))
10602 break;
10603 }
10604
10605 /* FullName_save() is slow, don't use it when not needed. */
10606 if (*p != NUL || !vim_isAbsName(*fnamep))
10607 {
10608 *fnamep = FullName_save(*fnamep, *p != NUL);
10609 vim_free(*bufp); /* free any allocated file name */
10610 *bufp = *fnamep;
10611 if (*fnamep == NULL)
10612 return -1;
10613 }
10614
Bram Moolenaar4f974752019-02-17 17:44:42 +010010615#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010616# if _WIN32_WINNT >= 0x0500
10617 if (vim_strchr(*fnamep, '~') != NULL)
10618 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010619 // Expand 8.3 filename to full path. Needed to make sure the same
10620 // file does not have two different names.
10621 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10622 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10623 WCHAR buf[_MAX_PATH];
10624
10625 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010626 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010627 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010628 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010629 char_u *p = utf16_to_enc(buf, NULL);
10630
10631 if (p != NULL)
10632 {
10633 vim_free(*bufp); // free any allocated file name
10634 *bufp = *fnamep = p;
10635 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010636 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010637 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010638 }
10639 }
10640# endif
10641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 /* Append a path separator to a directory. */
10643 if (mch_isdir(*fnamep))
10644 {
10645 /* Make room for one or two extra characters. */
10646 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10647 vim_free(*bufp); /* free any allocated file name */
10648 *bufp = *fnamep;
10649 if (*fnamep == NULL)
10650 return -1;
10651 add_pathsep(*fnamep);
10652 }
10653 }
10654
10655 /* ":." - path relative to the current directory */
10656 /* ":~" - path relative to the home directory */
10657 /* ":8" - shortname path - postponed till after */
10658 while (src[*usedlen] == ':'
10659 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10660 {
10661 *usedlen += 2;
10662 if (c == '8')
10663 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010664#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 has_shortname = 1; /* Postpone this. */
10666#endif
10667 continue;
10668 }
10669 pbuf = NULL;
10670 /* Need full path first (use expand_env() to remove a "~/") */
10671 if (!has_fullname)
10672 {
10673 if (c == '.' && **fnamep == '~')
10674 p = pbuf = expand_env_save(*fnamep);
10675 else
10676 p = pbuf = FullName_save(*fnamep, FALSE);
10677 }
10678 else
10679 p = *fnamep;
10680
10681 has_fullname = 0;
10682
10683 if (p != NULL)
10684 {
10685 if (c == '.')
10686 {
10687 mch_dirname(dirname, MAXPATHL);
10688 s = shorten_fname(p, dirname);
10689 if (s != NULL)
10690 {
10691 *fnamep = s;
10692 if (pbuf != NULL)
10693 {
10694 vim_free(*bufp); /* free any allocated file name */
10695 *bufp = pbuf;
10696 pbuf = NULL;
10697 }
10698 }
10699 }
10700 else
10701 {
10702 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10703 /* Only replace it when it starts with '~' */
10704 if (*dirname == '~')
10705 {
10706 s = vim_strsave(dirname);
10707 if (s != NULL)
10708 {
10709 *fnamep = s;
10710 vim_free(*bufp);
10711 *bufp = s;
10712 }
10713 }
10714 }
10715 vim_free(pbuf);
10716 }
10717 }
10718
10719 tail = gettail(*fnamep);
10720 *fnamelen = (int)STRLEN(*fnamep);
10721
10722 /* ":h" - head, remove "/file_name", can be repeated */
10723 /* Don't remove the first "/" or "c:\" */
10724 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10725 {
10726 valid |= VALID_HEAD;
10727 *usedlen += 2;
10728 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010729 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010730 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 *fnamelen = (int)(tail - *fnamep);
10732#ifdef VMS
10733 if (*fnamelen > 0)
10734 *fnamelen += 1; /* the path separator is part of the path */
10735#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010736 if (*fnamelen == 0)
10737 {
10738 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10739 p = vim_strsave((char_u *)".");
10740 if (p == NULL)
10741 return -1;
10742 vim_free(*bufp);
10743 *bufp = *fnamep = tail = p;
10744 *fnamelen = 1;
10745 }
10746 else
10747 {
10748 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010749 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 }
10752
10753 /* ":8" - shortname */
10754 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10755 {
10756 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010757#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 has_shortname = 1;
10759#endif
10760 }
10761
Bram Moolenaar4f974752019-02-17 17:44:42 +010010762#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010763 /*
10764 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 */
10766 if (has_shortname)
10767 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010768 /* Copy the string if it is shortened by :h and when it wasn't copied
10769 * yet, because we are going to change it in place. Avoids changing
10770 * the buffer name for "%:8". */
10771 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 {
10773 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010774 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 return -1;
10776 vim_free(*bufp);
10777 *bufp = *fnamep = p;
10778 }
10779
10780 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010781 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 if (!has_fullname && !vim_isAbsName(*fnamep))
10783 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010784 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 return -1;
10786 }
10787 else
10788 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010789 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790
Bram Moolenaardc935552011-08-17 15:23:23 +020010791 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010793 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 return -1;
10795
10796 if (l == 0)
10797 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010798 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010800 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 return -1;
10802 }
10803 *fnamelen = l;
10804 }
10805 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010806#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807
10808 /* ":t" - tail, just the basename */
10809 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10810 {
10811 *usedlen += 2;
10812 *fnamelen -= (int)(tail - *fnamep);
10813 *fnamep = tail;
10814 }
10815
10816 /* ":e" - extension, can be repeated */
10817 /* ":r" - root, without extension, can be repeated */
10818 while (src[*usedlen] == ':'
10819 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10820 {
10821 /* find a '.' in the tail:
10822 * - for second :e: before the current fname
10823 * - otherwise: The last '.'
10824 */
10825 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10826 s = *fnamep - 2;
10827 else
10828 s = *fnamep + *fnamelen - 1;
10829 for ( ; s > tail; --s)
10830 if (s[0] == '.')
10831 break;
10832 if (src[*usedlen + 1] == 'e') /* :e */
10833 {
10834 if (s > tail)
10835 {
10836 *fnamelen += (int)(*fnamep - (s + 1));
10837 *fnamep = s + 1;
10838#ifdef VMS
10839 /* cut version from the extension */
10840 s = *fnamep + *fnamelen - 1;
10841 for ( ; s > *fnamep; --s)
10842 if (s[0] == ';')
10843 break;
10844 if (s > *fnamep)
10845 *fnamelen = s - *fnamep;
10846#endif
10847 }
10848 else if (*fnamep <= tail)
10849 *fnamelen = 0;
10850 }
10851 else /* :r */
10852 {
10853 if (s > tail) /* remove one extension */
10854 *fnamelen = (int)(s - *fnamep);
10855 }
10856 *usedlen += 2;
10857 }
10858
10859 /* ":s?pat?foo?" - substitute */
10860 /* ":gs?pat?foo?" - global substitute */
10861 if (src[*usedlen] == ':'
10862 && (src[*usedlen + 1] == 's'
10863 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10864 {
10865 char_u *str;
10866 char_u *pat;
10867 char_u *sub;
10868 int sep;
10869 char_u *flags;
10870 int didit = FALSE;
10871
10872 flags = (char_u *)"";
10873 s = src + *usedlen + 2;
10874 if (src[*usedlen + 1] == 'g')
10875 {
10876 flags = (char_u *)"g";
10877 ++s;
10878 }
10879
10880 sep = *s++;
10881 if (sep)
10882 {
10883 /* find end of pattern */
10884 p = vim_strchr(s, sep);
10885 if (p != NULL)
10886 {
10887 pat = vim_strnsave(s, (int)(p - s));
10888 if (pat != NULL)
10889 {
10890 s = p + 1;
10891 /* find end of substitution */
10892 p = vim_strchr(s, sep);
10893 if (p != NULL)
10894 {
10895 sub = vim_strnsave(s, (int)(p - s));
10896 str = vim_strnsave(*fnamep, *fnamelen);
10897 if (sub != NULL && str != NULL)
10898 {
10899 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010900 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 if (s != NULL)
10902 {
10903 *fnamep = s;
10904 *fnamelen = (int)STRLEN(s);
10905 vim_free(*bufp);
10906 *bufp = s;
10907 didit = TRUE;
10908 }
10909 }
10910 vim_free(sub);
10911 vim_free(str);
10912 }
10913 vim_free(pat);
10914 }
10915 }
10916 /* after using ":s", repeat all the modifiers */
10917 if (didit)
10918 goto repeat;
10919 }
10920 }
10921
Bram Moolenaar26df0922014-02-23 23:39:13 +010010922 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10923 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010924 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010925 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010926 if (c != NUL)
10927 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010928 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010929 if (c != NUL)
10930 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010931 if (p == NULL)
10932 return -1;
10933 vim_free(*bufp);
10934 *bufp = *fnamep = p;
10935 *fnamelen = (int)STRLEN(p);
10936 *usedlen += 2;
10937 }
10938
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 return valid;
10940}
10941
10942/*
10943 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010944 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 * "flags" can be "g" to do a global substitute.
10946 * Returns an allocated string, NULL for error.
10947 */
10948 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010949do_string_sub(
10950 char_u *str,
10951 char_u *pat,
10952 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010953 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010954 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955{
10956 int sublen;
10957 regmatch_T regmatch;
10958 int i;
10959 int do_all;
10960 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010961 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 garray_T ga;
10963 char_u *ret;
10964 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010965 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
10967 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10968 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010969 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970
10971 ga_init2(&ga, 1, 200);
10972
10973 do_all = (flags[0] == 'g');
10974
10975 regmatch.rm_ic = p_ic;
10976 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10977 if (regmatch.regprog != NULL)
10978 {
10979 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010980 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10982 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010983 /* Skip empty match except for first match. */
10984 if (regmatch.startp[0] == regmatch.endp[0])
10985 {
10986 if (zero_width == regmatch.startp[0])
10987 {
10988 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010989 i = MB_PTR2LEN(tail);
10990 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10991 (size_t)i);
10992 ga.ga_len += i;
10993 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010994 continue;
10995 }
10996 zero_width = regmatch.startp[0];
10997 }
10998
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 /*
11000 * Get some space for a temporary buffer to do the substitution
11001 * into. It will contain:
11002 * - The text up to where the match is.
11003 * - The substituted text.
11004 * - The text after the match.
11005 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011006 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010011007 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11009 {
11010 ga_clear(&ga);
11011 break;
11012 }
11013
11014 /* copy the text up to where the match is */
11015 i = (int)(regmatch.startp[0] - tail);
11016 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11017 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011018 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 + ga.ga_len + i, TRUE, TRUE, FALSE);
11020 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020011021 tail = regmatch.endp[0];
11022 if (*tail == NUL)
11023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 if (!do_all)
11025 break;
11026 }
11027
11028 if (ga.ga_data != NULL)
11029 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11030
Bram Moolenaar473de612013-06-08 18:19:48 +020011031 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033
11034 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11035 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011036 if (p_cpo == empty_option)
11037 p_cpo = save_cpo;
11038 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011039 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011040 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041
11042 return ret;
11043}
11044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045 static int
11046filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
11047{
11048 typval_T rettv;
11049 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051
11052 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
11053 argv[0] = vimvars[VV_KEY].vv_tv;
11054 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010011055 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
11056 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 if (map)
11058 {
11059 /* map(): replace the list item value */
11060 clear_tv(tv);
11061 rettv.v_lock = 0;
11062 *tv = rettv;
11063 }
11064 else
11065 {
11066 int error = FALSE;
11067
11068 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011069 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070 clear_tv(&rettv);
11071 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011072 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 if (error)
11074 goto theend;
11075 }
11076 retval = OK;
11077theend:
11078 clear_tv(&vimvars[VV_VAL].vv_tv);
11079 return retval;
11080}
11081
11082
11083/*
11084 * Implementation of map() and filter().
11085 */
11086 void
11087filter_map(typval_T *argvars, typval_T *rettv, int map)
11088{
11089 typval_T *expr;
11090 listitem_T *li, *nli;
11091 list_T *l = NULL;
11092 dictitem_T *di;
11093 hashtab_T *ht;
11094 hashitem_T *hi;
11095 dict_T *d = NULL;
11096 typval_T save_val;
11097 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011098 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011099 int rem;
11100 int todo;
11101 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11102 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11103 : N_("filter() argument"));
11104 int save_did_emsg;
11105 int idx = 0;
11106
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011107 if (argvars[0].v_type == VAR_BLOB)
11108 {
11109 if ((b = argvars[0].vval.v_blob) == NULL)
11110 return;
11111 }
11112 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 {
11114 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011115 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 return;
11117 }
11118 else if (argvars[0].v_type == VAR_DICT)
11119 {
11120 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011121 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 return;
11123 }
11124 else
11125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011126 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011127 return;
11128 }
11129
11130 expr = &argvars[1];
11131 /* On type errors, the preceding call has already displayed an error
11132 * message. Avoid a misleading error message for an empty string that
11133 * was not passed as argument. */
11134 if (expr->v_type != VAR_UNKNOWN)
11135 {
11136 prepare_vimvar(VV_VAL, &save_val);
11137
11138 /* We reset "did_emsg" to be able to detect whether an error
11139 * occurred during evaluation of the expression. */
11140 save_did_emsg = did_emsg;
11141 did_emsg = FALSE;
11142
11143 prepare_vimvar(VV_KEY, &save_key);
11144 if (argvars[0].v_type == VAR_DICT)
11145 {
11146 vimvars[VV_KEY].vv_type = VAR_STRING;
11147
11148 ht = &d->dv_hashtab;
11149 hash_lock(ht);
11150 todo = (int)ht->ht_used;
11151 for (hi = ht->ht_array; todo > 0; ++hi)
11152 {
11153 if (!HASHITEM_EMPTY(hi))
11154 {
11155 int r;
11156
11157 --todo;
11158 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011159 if (map && (var_check_lock(di->di_tv.v_lock,
11160 arg_errmsg, TRUE)
11161 || var_check_ro(di->di_flags,
11162 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011163 break;
11164 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11165 r = filter_map_one(&di->di_tv, expr, map, &rem);
11166 clear_tv(&vimvars[VV_KEY].vv_tv);
11167 if (r == FAIL || did_emsg)
11168 break;
11169 if (!map && rem)
11170 {
11171 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11172 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11173 break;
11174 dictitem_remove(d, di);
11175 }
11176 }
11177 }
11178 hash_unlock(ht);
11179 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011180 else if (argvars[0].v_type == VAR_BLOB)
11181 {
11182 int i;
11183 typval_T tv;
11184
11185 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11186 for (i = 0; i < b->bv_ga.ga_len; i++)
11187 {
11188 tv.v_type = VAR_NUMBER;
11189 tv.vval.v_number = blob_get(b, i);
11190 vimvars[VV_KEY].vv_nr = idx;
11191 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11192 break;
11193 if (tv.v_type != VAR_NUMBER)
11194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011195 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011196 return;
11197 }
11198 tv.v_type = VAR_NUMBER;
11199 blob_set(b, i, tv.vval.v_number);
11200 if (!map && rem)
11201 {
11202 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11203
11204 mch_memmove(p + idx, p + i + 1,
11205 (size_t)b->bv_ga.ga_len - i - 1);
11206 --b->bv_ga.ga_len;
11207 --i;
11208 }
11209 }
11210 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211 else
11212 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011213 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011214 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11215
11216 for (li = l->lv_first; li != NULL; li = nli)
11217 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011218 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 break;
11220 nli = li->li_next;
11221 vimvars[VV_KEY].vv_nr = idx;
11222 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11223 || did_emsg)
11224 break;
11225 if (!map && rem)
11226 listitem_remove(l, li);
11227 ++idx;
11228 }
11229 }
11230
11231 restore_vimvar(VV_KEY, &save_key);
11232 restore_vimvar(VV_VAL, &save_val);
11233
11234 did_emsg |= save_did_emsg;
11235 }
11236
11237 copy_tv(&argvars[0], rettv);
11238}
11239
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */