blob: d89093cb33b1e5d0140535b050d0dd2490b1a82f [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;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001257 int marker_indent_len = 0;
1258 int text_indent_len = 0;
1259 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001260
1261 if (eap->getline == NULL)
1262 {
1263 emsg(_("E991: cannot use =<< here"));
1264 return NULL;
1265 }
1266
1267 // Check for the optional 'trim' word before the marker
1268 cmd = skipwhite(cmd);
1269 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1270 {
1271 cmd = skipwhite(cmd + 4);
1272
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001273 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001274 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001275 // the first line after the :let command line. To find the end marker
1276 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001277 p = *eap->cmdlinep;
1278 while (VIM_ISWHITE(*p))
1279 {
1280 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001281 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001282 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001283 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001284 }
1285
1286 // The marker is the next word. Default marker is "."
1287 if (*cmd != NUL && *cmd != '"')
1288 {
1289 marker = skipwhite(cmd);
1290 p = skiptowhite(marker);
1291 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1292 {
1293 emsg(_(e_trailing));
1294 return NULL;
1295 }
1296 *p = NUL;
1297 }
1298 else
1299 marker = (char_u *)".";
1300
1301 l = list_alloc();
1302 if (l == NULL)
1303 return NULL;
1304
1305 for (;;)
1306 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001307 int mi = 0;
1308 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001309
Bram Moolenaare96a2492019-06-25 04:12:16 +02001310 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001311 if (theline == NULL)
1312 {
1313 semsg(_("E990: Missing end marker '%s'"), marker);
1314 break;
1315 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001316
1317 // with "trim": skip the indent matching the :let line to find the
1318 // marker
1319 if (marker_indent_len > 0
1320 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1321 mi = marker_indent_len;
1322 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001323 {
1324 vim_free(theline);
1325 break;
1326 }
1327
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001328 if (text_indent_len == -1 && *theline != NUL)
1329 {
1330 // set the text indent from the first line.
1331 p = theline;
1332 text_indent_len = 0;
1333 while (VIM_ISWHITE(*p))
1334 {
1335 p++;
1336 text_indent_len++;
1337 }
1338 text_indent = vim_strnsave(theline, text_indent_len);
1339 }
1340 // with "trim": skip the indent matching the first line
1341 if (text_indent != NULL)
1342 for (ti = 0; ti < text_indent_len; ++ti)
1343 if (theline[ti] != text_indent[ti])
1344 break;
1345
1346 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001347 break;
1348 vim_free(theline);
1349 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001350 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001351
1352 return l;
1353}
1354
1355/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 * ":let" list all variable values
1357 * ":let var1 var2" list variable values
1358 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 * ":let var += expr" assignment command.
1360 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001361 * ":let var *= expr" assignment command.
1362 * ":let var /= expr" assignment command.
1363 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001365 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
1368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001371 ex_let_const(eap, FALSE);
1372}
1373
1374/*
1375 * ":const" list all variable values
1376 * ":const var1 var2" list variable values
1377 * ":const var = expr" assignment command.
1378 * ":const [var1, var2] = expr" unpack list.
1379 */
1380 void
1381ex_const(exarg_T *eap)
1382{
1383 ex_let_const(eap, TRUE);
1384}
1385
1386 static void
1387ex_let_const(exarg_T *eap, int is_const)
1388{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 int var_count = 0;
1394 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001396 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001397 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001398 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaardb552d602006-03-23 22:59:57 +00001400 argend = skip_var_list(arg, &var_count, &semicolon);
1401 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001403 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001404 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001405 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001406 concat = expr[0] == '.'
1407 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1408 || (expr[1] == '.' && expr[2] == '='));
1409 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1410 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001412 /*
1413 * ":let" without "=": list variables
1414 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001416 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001417 else if (expr[0] == '.')
1418 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001421 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001425 list_glob_vars(&first);
1426 list_buf_vars(&first);
1427 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001428 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001429 list_script_vars(&first);
1430 list_func_vars(&first);
1431 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 eap->nextcmd = check_nextcmd(arg);
1434 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001435 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1436 {
1437 list_T *l;
1438
1439 // HERE document
1440 l = heredoc_get(eap, expr + 3);
1441 if (l != NULL)
1442 {
1443 rettv_list_set(&rettv, l);
1444 op[0] = '=';
1445 op[1] = NUL;
1446 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001447 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001448 clear_tv(&rettv);
1449 }
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 else
1452 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 op[0] = '=';
1454 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001455 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001457 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001458 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001459 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001460 if (expr[0] == '.' && expr[1] == '.') // ..=
1461 ++expr;
1462 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001463 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001465 else
1466 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (eap->skip)
1469 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (eap->skip)
1472 {
1473 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 --emsg_skip;
1476 }
1477 else if (i != FAIL)
1478 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001480 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 }
1484}
1485
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001486/*
1487 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1488 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 * When "nextchars" is not NULL it points to a string with characters that
1490 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1491 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 * Returns OK or FAIL;
1493 */
1494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495ex_let_vars(
1496 char_u *arg_start,
1497 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001498 int copy, // copy values from "tv", don't move
1499 int semicolon, // from skip_var_list()
1500 int var_count, // from skip_var_list()
1501 int is_const, // lock variables for const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503{
1504 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001505 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001507 listitem_T *item;
1508 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509
1510 if (*arg != '[')
1511 {
1512 /*
1513 * ":let var = expr" or ":for var in list"
1514 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001515 if (ex_let_one(arg, tv, copy, is_const, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001516 return FAIL;
1517 return OK;
1518 }
1519
1520 /*
1521 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1522 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001523 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001525 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001526 return FAIL;
1527 }
1528
1529 i = list_len(l);
1530 if (semicolon == 0 && var_count < i)
1531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001532 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533 return FAIL;
1534 }
1535 if (var_count - semicolon > i)
1536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001537 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 return FAIL;
1539 }
1540
1541 item = l->lv_first;
1542 while (*arg != ']')
1543 {
1544 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001545 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
1546 (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 item = item->li_next;
1548 if (arg == NULL)
1549 return FAIL;
1550
1551 arg = skipwhite(arg);
1552 if (*arg == ';')
1553 {
1554 /* Put the rest of the list (may be empty) in the var after ';'.
1555 * Create a new list for this. */
1556 l = list_alloc();
1557 if (l == NULL)
1558 return FAIL;
1559 while (item != NULL)
1560 {
1561 list_append_tv(l, &item->li_tv);
1562 item = item->li_next;
1563 }
1564
1565 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001566 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 ltv.vval.v_list = l;
1568 l->lv_refcount = 1;
1569
Bram Moolenaar9937a052019-06-15 15:45:06 +02001570 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
1571 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001572 clear_tv(&ltv);
1573 if (arg == NULL)
1574 return FAIL;
1575 break;
1576 }
1577 else if (*arg != ',' && *arg != ']')
1578 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001579 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 return FAIL;
1581 }
1582 }
1583
1584 return OK;
1585}
1586
1587/*
1588 * Skip over assignable variable "var" or list of variables "[var, var]".
1589 * Used for ":let varvar = expr" and ":for varvar in expr".
1590 * For "[var, var]" increment "*var_count" for each variable.
1591 * for "[var, var; var]" set "semicolon".
1592 * Return NULL for an error.
1593 */
1594 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595skip_var_list(
1596 char_u *arg,
1597 int *var_count,
1598 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599{
1600 char_u *p, *s;
1601
1602 if (*arg == '[')
1603 {
1604 /* "[var, var]": find the matching ']'. */
1605 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607 {
1608 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1609 s = skip_var_one(p);
1610 if (s == p)
1611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001612 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 return NULL;
1614 }
1615 ++*var_count;
1616
1617 p = skipwhite(s);
1618 if (*p == ']')
1619 break;
1620 else if (*p == ';')
1621 {
1622 if (*semicolon == 1)
1623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001624 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 return NULL;
1626 }
1627 *semicolon = 1;
1628 }
1629 else if (*p != ',')
1630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001631 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 return NULL;
1633 }
1634 }
1635 return p + 1;
1636 }
1637 else
1638 return skip_var_one(arg);
1639}
1640
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001641/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001642 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001643 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001644 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001648 if (*arg == '@' && arg[1] != NUL)
1649 return arg + 2;
1650 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1651 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652}
1653
Bram Moolenaara7043832005-01-21 11:56:39 +00001654/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001655 * List variables for hashtab "ht" with prefix "prefix".
1656 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001657 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659list_hashtable_vars(
1660 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001661 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662 int empty,
1663 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001664{
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 hashitem_T *hi;
1666 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001667 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001668 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001669
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001670 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001671 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1672 {
1673 if (!HASHITEM_EMPTY(hi))
1674 {
1675 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001677
1678 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001679 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1680 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001681 if (message_filtered(buf))
1682 continue;
1683
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 if (empty || di->di_tv.v_type != VAR_STRING
1685 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001686 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001687 }
1688 }
1689}
1690
1691/*
1692 * List global variables.
1693 */
1694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001696{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001697 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001698}
1699
1700/*
1701 * List buffer variables.
1702 */
1703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001705{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001706 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001707}
1708
1709/*
1710 * List window variables.
1711 */
1712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001713list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001714{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001715 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001716}
1717
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001718/*
1719 * List tab page variables.
1720 */
1721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001723{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001724 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001725}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726
Bram Moolenaara7043832005-01-21 11:56:39 +00001727/*
1728 * List Vim variables.
1729 */
1730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001733 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734}
1735
1736/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001737 * List script-local variables, if there is a script.
1738 */
1739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001740list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001741{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001742 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1743 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001744 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001745}
1746
1747/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001748 * List variables in "arg".
1749 */
1750 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752{
1753 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001754 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001756 char_u *name_start;
1757 char_u *arg_subsc;
1758 char_u *tofree;
1759 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760
1761 while (!ends_excmd(*arg) && !got_int)
1762 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001763 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001765 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001766 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001767 {
1768 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001769 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001770 break;
1771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001775 /* get_name_len() takes care of expanding curly braces */
1776 name_start = name = arg;
1777 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1778 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 /* This is mainly to keep test 49 working: when expanding
1781 * curly braces fails overrule the exception error message. */
1782 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001784 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001785 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001786 break;
1787 }
1788 error = TRUE;
1789 }
1790 else
1791 {
1792 if (tofree != NULL)
1793 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001794 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 else
1797 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001798 /* handle d.key, l[idx], f(expr) */
1799 arg_subsc = arg;
1800 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001801 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001804 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001806 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001807 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001808 case 'g': list_glob_vars(first); break;
1809 case 'b': list_buf_vars(first); break;
1810 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001811 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001812 case 'v': list_vim_vars(first); break;
1813 case 's': list_script_vars(first); break;
1814 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001816 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 }
1819 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001820 {
1821 char_u numbuf[NUMBUFLEN];
1822 char_u *tf;
1823 int c;
1824 char_u *s;
1825
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 c = *arg;
1828 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001829 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001831 tv.v_type,
1832 s == NULL ? (char_u *)"" : s,
1833 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001834 *arg = c;
1835 vim_free(tf);
1836 }
1837 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 }
1840 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841
1842 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001844
1845 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 }
1847
1848 return arg;
1849}
1850
1851/*
1852 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1853 * Returns a pointer to the char just after the var name.
1854 * Returns NULL if there is an error.
1855 */
1856 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001858 char_u *arg, // points to variable name
1859 typval_T *tv, // value to assign to variable
1860 int copy, // copy value from "tv"
1861 int is_const, // lock variable for const
1862 char_u *endchars, // valid chars after variable name or NULL
1863 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864{
1865 int c1;
1866 char_u *name;
1867 char_u *p;
1868 char_u *arg_end = NULL;
1869 int len;
1870 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872
1873 /*
1874 * ":let $VAR = expr": Set environment variable.
1875 */
1876 if (*arg == '$')
1877 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001878 if (is_const)
1879 {
1880 emsg(_("E996: Cannot lock an environment variable"));
1881 return NULL;
1882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 /* Find the end of the name. */
1884 ++arg;
1885 name = arg;
1886 len = get_env_len(&arg);
1887 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 else
1890 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001891 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001896 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 {
1898 c1 = name[len];
1899 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001900 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001901 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 {
1903 int mustfree = FALSE;
1904 char_u *s = vim_getenv(name, &mustfree);
1905
1906 if (s != NULL)
1907 {
1908 p = tofree = concat_str(s, p);
1909 if (mustfree)
1910 vim_free(s);
1911 }
1912 }
1913 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 if (STRICMP(name, "HOME") == 0)
1917 init_homedir();
1918 else if (didset_vim && STRICMP(name, "VIM") == 0)
1919 didset_vim = FALSE;
1920 else if (didset_vimruntime
1921 && STRICMP(name, "VIMRUNTIME") == 0)
1922 didset_vimruntime = FALSE;
1923 arg_end = arg;
1924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 }
1928 }
1929 }
1930
1931 /*
1932 * ":let &option = expr": Set option value.
1933 * ":let &l:option = expr": Set local option value.
1934 * ":let &g:option = expr": Set global option value.
1935 */
1936 else if (*arg == '&')
1937 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001938 if (is_const)
1939 {
1940 emsg(_("E996: Cannot lock an option"));
1941 return NULL;
1942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 /* Find the end of the name. */
1944 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 if (p == NULL || (endchars != NULL
1946 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001947 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 else
1949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 long n;
1951 int opt_type;
1952 long numval;
1953 char_u *stringval = NULL;
1954 char_u *s;
1955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 c1 = *p;
1957 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001959 n = (long)tv_get_number(tv);
1960 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001961 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 {
1963 opt_type = get_option_value(arg, &numval,
1964 &stringval, opt_flags);
1965 if ((opt_type == 1 && *op == '.')
1966 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001968 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001969 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 else
1972 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001973 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001975 switch (*op)
1976 {
1977 case '+': n = numval + n; break;
1978 case '-': n = numval - n; break;
1979 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001980 case '/': n = (long)num_divide(numval, n); break;
1981 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001984 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 {
1986 s = concat_str(stringval, s);
1987 vim_free(stringval);
1988 stringval = s;
1989 }
1990 }
1991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 if (s != NULL)
1993 {
1994 set_option_value(arg, n, s, opt_flags);
1995 arg_end = p;
1996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
2000 }
2001
2002 /*
2003 * ":let @r = expr": Set register contents.
2004 */
2005 else if (*arg == '@')
2006 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002007 if (is_const)
2008 {
2009 emsg(_("E996: Cannot lock a register"));
2010 return NULL;
2011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002013 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002014 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002017 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 else
2019 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002020 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 char_u *s;
2022
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002023 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002024 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002026 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 if (s != NULL)
2028 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002029 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 vim_free(s);
2031 }
2032 }
2033 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 arg_end = arg + 1;
2037 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002038 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 }
2040 }
2041
2042 /*
2043 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002044 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002046 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002048 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002050 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002055 else
2056 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002057 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 arg_end = p;
2059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 }
2063
2064 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002065 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066
2067 return arg_end;
2068}
2069
2070/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 * Get an lval: variable, Dict item or List item that can be assigned a value
2072 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2073 * "name.key", "name.key[expr]" etc.
2074 * Indexing only works if "name" is an existing List or Dictionary.
2075 * "name" points to the start of the name.
2076 * If "rettv" is not NULL it points to the value to be assigned.
2077 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2078 * wrong; must end in space or cmd separator.
2079 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002080 * flags:
2081 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002082 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002083 * GLV_NO_AUTOLOAD: do not use script autoloading
2084 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002090get_lval(
2091 char_u *name,
2092 typval_T *rettv,
2093 lval_T *lp,
2094 int unlet,
2095 int skip,
2096 int flags, /* GLV_ values */
2097 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 char_u *expr_start, *expr_end;
2101 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 dictitem_T *v;
2103 typval_T var1;
2104 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002110 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114
2115 if (skip)
2116 {
2117 /* When skipping just find the end of the name. */
2118 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002119 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 }
2121
2122 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002123 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (expr_start != NULL)
2125 {
2126 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002127 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 && *p != '[' && *p != '.')
2129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 return NULL;
2132 }
2133
2134 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2135 if (lp->ll_exp_name == NULL)
2136 {
2137 /* Report an invalid expression in braces, unless the
2138 * expression evaluation has been cancelled due to an
2139 * aborting error, an interrupt, or an exception. */
2140 if (!aborting() && !quiet)
2141 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002142 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002143 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 return NULL;
2145 }
2146 }
2147 lp->ll_name = lp->ll_exp_name;
2148 }
2149 else
2150 lp->ll_name = name;
2151
2152 /* Without [idx] or .key we are done. */
2153 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2154 return p;
2155
2156 cc = *p;
2157 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002158 /* Only pass &ht when we would write to the variable, it prevents autoload
2159 * as well. */
2160 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2161 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 if (v == NULL)
2166 return NULL;
2167
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 /*
2169 * Loop until no more [idx] or .key is following.
2170 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002171 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002172 var1.v_type = VAR_UNKNOWN;
2173 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2177 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002178 && lp->ll_tv->vval.v_dict != NULL)
2179 && !(lp->ll_tv->v_type == VAR_BLOB
2180 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002189 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002192
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 len = -1;
2194 if (*p == '.')
2195 {
2196 key = p + 1;
2197 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2198 ;
2199 if (len == 0)
2200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 }
2205 p = key + len;
2206 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002207 else
2208 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002209 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002210 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 if (*p == ':')
2212 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002213 else
2214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002215 empty1 = FALSE;
2216 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002218 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002219 {
2220 /* not a number or string */
2221 clear_tv(&var1);
2222 return NULL;
2223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 }
2225
2226 /* Optionally get the second index [ :expr]. */
2227 if (*p == ':')
2228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002232 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002233 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002235 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002236 if (rettv != NULL
2237 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002238 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002239 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002240 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002243 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002244 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002246 }
2247 p = skipwhite(p + 1);
2248 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 else
2251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2254 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002255 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002258 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 {
2260 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002261 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 clear_tv(&var2);
2263 return NULL;
2264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002270
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002275 clear_tv(&var1);
2276 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 }
2279
2280 /* Skip to past ']'. */
2281 ++p;
2282 }
2283
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002285 {
2286 if (len == -1)
2287 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002289 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002290 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002291 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002292 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002294 }
2295 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 lp->ll_list = NULL;
2297 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002298 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002299
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002300 /* When assigning to a scope dictionary check that a function and
2301 * variable name is valid (only variable name unless it is l: or
2302 * g: dictionary). Disallow overwriting a builtin function. */
2303 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002304 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002305 int prevval;
2306 int wrong;
2307
2308 if (len != -1)
2309 {
2310 prevval = key[len];
2311 key[len] = NUL;
2312 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002313 else
2314 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002315 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2316 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002317 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002318 || !valid_varname(key);
2319 if (len != -1)
2320 key[len] = prevval;
2321 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002322 return NULL;
2323 }
2324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002327 // Can't add "v:" or "a:" variable.
2328 if (lp->ll_dict == &vimvardict
2329 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002332 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002333 return NULL;
2334 }
2335
Bram Moolenaar31b81602019-02-10 22:14:27 +01002336 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002340 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002341 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 }
2344 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 p = NULL;
2351 break;
2352 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002353 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002354 else if ((flags & GLV_READ_ONLY) == 0
2355 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002356 {
2357 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002358 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002359 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002360
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002361 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002364 else if (lp->ll_tv->v_type == VAR_BLOB)
2365 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002366 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2367
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002368 /*
2369 * Get the number and item for the only or first index of the List.
2370 */
2371 if (empty1)
2372 lp->ll_n1 = 0;
2373 else
2374 // is number or string
2375 lp->ll_n1 = (long)tv_get_number(&var1);
2376 clear_tv(&var1);
2377
2378 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002379 || lp->ll_n1 > bloblen
2380 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002381 {
2382 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002384 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002385 return NULL;
2386 }
2387 if (lp->ll_range && !lp->ll_empty2)
2388 {
2389 lp->ll_n2 = (long)tv_get_number(&var2);
2390 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002391 if (lp->ll_n2 < 0
2392 || lp->ll_n2 >= bloblen
2393 || lp->ll_n2 < lp->ll_n1)
2394 {
2395 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002396 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002397 return NULL;
2398 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002399 }
2400 lp->ll_blob = lp->ll_tv->vval.v_blob;
2401 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002402 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 else
2405 {
2406 /*
2407 * Get the number and item for the only or first index of the List.
2408 */
2409 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002412 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002413 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002414 clear_tv(&var1);
2415
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_list = lp->ll_tv->vval.v_list;
2418 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2419 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002421 if (lp->ll_n1 < 0)
2422 {
2423 lp->ll_n1 = 0;
2424 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2425 }
2426 }
2427 if (lp->ll_li == NULL)
2428 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002429 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002430 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002431 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
2434
2435 /*
2436 * May need to find the item or absolute index for the second
2437 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 * When no index given: "lp->ll_empty2" is TRUE.
2439 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002443 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002444 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002450 {
2451 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002452 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002454 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 }
2457
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2459 if (lp->ll_n1 < 0)
2460 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2461 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002462 {
2463 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002464 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002466 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002467 }
2468
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
2472
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002473 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 return p;
2475}
2476
2477/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002478 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002481clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482{
2483 vim_free(lp->ll_exp_name);
2484 vim_free(lp->ll_newkey);
2485}
2486
2487/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002488 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002490 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2491 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 */
2493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494set_var_lval(
2495 lval_T *lp,
2496 char_u *endp,
2497 typval_T *rettv,
2498 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002499 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002500 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501{
2502 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 listitem_T *ri;
2504 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505
2506 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002508 cc = *endp;
2509 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002510 if (lp->ll_blob != NULL)
2511 {
2512 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002513
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002514 if (op != NULL && *op != '=')
2515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002516 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002517 return;
2518 }
2519
2520 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2521 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002522 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002523
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002524 if (lp->ll_empty2)
2525 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2526
2527 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002529 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002530 return;
2531 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002532 if (lp->ll_empty2)
2533 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002534
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002535 ir = 0;
2536 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2537 blob_set(lp->ll_blob, il,
2538 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002539 }
2540 else
2541 {
2542 val = (int)tv_get_number_chk(rettv, &error);
2543 if (!error)
2544 {
2545 garray_T *gap = &lp->ll_blob->bv_ga;
2546
2547 // Allow for appending a byte. Setting a byte beyond
2548 // the end is an error otherwise.
2549 if (lp->ll_n1 < gap->ga_len
2550 || (lp->ll_n1 == gap->ga_len
2551 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2552 {
2553 blob_set(lp->ll_blob, lp->ll_n1, val);
2554 if (lp->ll_n1 == gap->ga_len)
2555 ++gap->ga_len;
2556 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002557 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002558 }
2559 }
2560 }
2561 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002563 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564
Bram Moolenaar9937a052019-06-15 15:45:06 +02002565 if (is_const)
2566 {
2567 emsg(_(e_cannot_mod));
2568 *endp = cc;
2569 return;
2570 }
2571
Bram Moolenaarff697e62019-02-12 22:28:33 +01002572 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002573 di = NULL;
2574 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2575 &tv, &di, TRUE, FALSE) == OK)
2576 {
2577 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002578 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2579 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002580 && tv_op(&tv, rettv, op) == OK)
2581 set_var(lp->ll_name, &tv, FALSE);
2582 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002585 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002586 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002587 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002589 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002590 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002591 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002592 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 else if (lp->ll_range)
2594 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002595 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002596 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002597
Bram Moolenaar9937a052019-06-15 15:45:06 +02002598 if (is_const)
2599 {
2600 emsg(_("E996: Cannot lock a range"));
2601 return;
2602 }
2603
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002604 /*
2605 * Check whether any of the list items is locked
2606 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002607 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002608 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002609 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002610 return;
2611 ri = ri->li_next;
2612 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2613 break;
2614 ll_li = ll_li->li_next;
2615 ++ll_n1;
2616 }
2617
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /*
2619 * Assign the List values to the list items.
2620 */
2621 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 if (op != NULL && *op != '=')
2624 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2625 else
2626 {
2627 clear_tv(&lp->ll_li->li_tv);
2628 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2629 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 ri = ri->li_next;
2631 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2632 break;
2633 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002636 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 ri = NULL;
2639 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 lp->ll_li = lp->ll_li->li_next;
2643 ++lp->ll_n1;
2644 }
2645 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 else if (lp->ll_empty2
2648 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002650 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 }
2652 else
2653 {
2654 /*
2655 * Assign to a List or Dictionary item.
2656 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002657 if (is_const)
2658 {
2659 emsg(_("E996: Cannot lock a list or dict"));
2660 return;
2661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_newkey != NULL)
2663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 if (op != NULL && *op != '=')
2665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002666 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 return;
2668 }
2669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002671 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (di == NULL)
2673 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002674 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2675 {
2676 vim_free(di);
2677 return;
2678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002681 else if (op != NULL && *op != '=')
2682 {
2683 tv_op(lp->ll_tv, rettv, op);
2684 return;
2685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 /*
2690 * Assign the value to the variable or list item.
2691 */
2692 if (copy)
2693 copy_tv(rettv, lp->ll_tv);
2694 else
2695 {
2696 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002697 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 }
2700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701}
2702
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002703/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002704 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2705 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 * Returns OK or FAIL.
2707 */
2708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002711 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 char_u numbuf[NUMBUFLEN];
2713 char_u *s;
2714
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002715 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2716 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2717 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 {
2719 switch (tv1->v_type)
2720 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002721 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722 case VAR_DICT:
2723 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002724 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002725 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002726 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002727 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 break;
2729
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002730 case VAR_BLOB:
2731 if (*op != '+' || tv2->v_type != VAR_BLOB)
2732 break;
2733 // BLOB += BLOB
2734 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2735 {
2736 blob_T *b1 = tv1->vval.v_blob;
2737 blob_T *b2 = tv2->vval.v_blob;
2738 int i, len = blob_len(b2);
2739 for (i = 0; i < len; i++)
2740 ga_append(&b1->bv_ga, blob_get(b2, i));
2741 }
2742 return OK;
2743
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002744 case VAR_LIST:
2745 if (*op != '+' || tv2->v_type != VAR_LIST)
2746 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002747 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2749 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2750 return OK;
2751
2752 case VAR_NUMBER:
2753 case VAR_STRING:
2754 if (tv2->v_type == VAR_LIST)
2755 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002756 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002757 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002758 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002759 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002760#ifdef FEAT_FLOAT
2761 if (tv2->v_type == VAR_FLOAT)
2762 {
2763 float_T f = n;
2764
Bram Moolenaarff697e62019-02-12 22:28:33 +01002765 if (*op == '%')
2766 break;
2767 switch (*op)
2768 {
2769 case '+': f += tv2->vval.v_float; break;
2770 case '-': f -= tv2->vval.v_float; break;
2771 case '*': f *= tv2->vval.v_float; break;
2772 case '/': f /= tv2->vval.v_float; break;
2773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002774 clear_tv(tv1);
2775 tv1->v_type = VAR_FLOAT;
2776 tv1->vval.v_float = f;
2777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002779#endif
2780 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002781 switch (*op)
2782 {
2783 case '+': n += tv_get_number(tv2); break;
2784 case '-': n -= tv_get_number(tv2); break;
2785 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002786 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2787 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002789 clear_tv(tv1);
2790 tv1->v_type = VAR_NUMBER;
2791 tv1->vval.v_number = n;
2792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 }
2794 else
2795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002796 if (tv2->v_type == VAR_FLOAT)
2797 break;
2798
Bram Moolenaarff697e62019-02-12 22:28:33 +01002799 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002800 s = tv_get_string(tv1);
2801 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 clear_tv(tv1);
2803 tv1->v_type = VAR_STRING;
2804 tv1->vval.v_string = s;
2805 }
2806 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002807
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002809#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002810 {
2811 float_T f;
2812
Bram Moolenaarff697e62019-02-12 22:28:33 +01002813 if (*op == '%' || *op == '.'
2814 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002815 && tv2->v_type != VAR_NUMBER
2816 && tv2->v_type != VAR_STRING))
2817 break;
2818 if (tv2->v_type == VAR_FLOAT)
2819 f = tv2->vval.v_float;
2820 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002822 switch (*op)
2823 {
2824 case '+': tv1->vval.v_float += f; break;
2825 case '-': tv1->vval.v_float -= f; break;
2826 case '*': tv1->vval.v_float *= f; break;
2827 case '/': tv1->vval.v_float /= f; break;
2828 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002829 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002830#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002831 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002832 }
2833 }
2834
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002835 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 return FAIL;
2837}
2838
2839/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840 * Evaluate the expression used in a ":for var in expr" command.
2841 * "arg" points to "var".
2842 * Set "*errp" to TRUE for an error, FALSE otherwise;
2843 * Return a pointer that holds the info. Null when there is an error.
2844 */
2845 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002846eval_for_line(
2847 char_u *arg,
2848 int *errp,
2849 char_u **nextcmdp,
2850 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851{
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002854 typval_T tv;
2855 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856
2857 *errp = TRUE; /* default: there is an error */
2858
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002859 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 if (fi == NULL)
2861 return NULL;
2862
2863 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2864 if (expr == NULL)
2865 return fi;
2866
2867 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002868 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002870 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871 return fi;
2872 }
2873
2874 if (skip)
2875 ++emsg_skip;
2876 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2877 {
2878 *errp = FALSE;
2879 if (!skip)
2880 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002881 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002882 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002883 l = tv.vval.v_list;
2884 if (l == NULL)
2885 {
2886 // a null list is like an empty list: do nothing
2887 clear_tv(&tv);
2888 }
2889 else
2890 {
2891 // No need to increment the refcount, it's already set for
2892 // the list being used in "tv".
2893 fi->fi_list = l;
2894 list_add_watch(l, &fi->fi_lw);
2895 fi->fi_lw.lw_item = l->lv_first;
2896 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002898 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002899 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002900 fi->fi_bi = 0;
2901 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002902 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002903 typval_T btv;
2904
2905 // Make a copy, so that the iteration still works when the
2906 // blob is changed.
2907 blob_copy(&tv, &btv);
2908 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002909 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002910 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002911 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 else
2913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002915 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 }
2917 }
2918 }
2919 if (skip)
2920 --emsg_skip;
2921
2922 return fi;
2923}
2924
2925/*
2926 * Use the first item in a ":for" list. Advance to the next.
2927 * Assign the values to the variable (list). "arg" points to the first one.
2928 * Return TRUE when a valid item was found, FALSE when at end of list or
2929 * something wrong.
2930 */
2931 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002934 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002936 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002938 if (fi->fi_blob != NULL)
2939 {
2940 typval_T tv;
2941
2942 if (fi->fi_bi >= blob_len(fi->fi_blob))
2943 return FALSE;
2944 tv.v_type = VAR_NUMBER;
2945 tv.v_lock = VAR_FIXED;
2946 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2947 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002948 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2949 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002950 }
2951
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952 item = fi->fi_lw.lw_item;
2953 if (item == NULL)
2954 result = FALSE;
2955 else
2956 {
2957 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002958 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2959 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960 }
2961 return result;
2962}
2963
2964/*
2965 * Free the structure used to store info used by ":for".
2966 */
2967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969{
Bram Moolenaar33570922005-01-25 22:26:29 +00002970 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002972 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002973 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002974 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002975 list_unref(fi->fi_list);
2976 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002977 if (fi != NULL && fi->fi_blob != NULL)
2978 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 vim_free(fi);
2980}
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2983
2984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002985set_context_for_expression(
2986 expand_T *xp,
2987 char_u *arg,
2988 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 int got_eq = FALSE;
2991 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 if (cmdidx == CMD_let)
2995 {
2996 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002997 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002998 {
2999 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003000 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 {
3002 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003003 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003004 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 break;
3006 }
3007 return;
3008 }
3009 }
3010 else
3011 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3012 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 while ((xp->xp_pattern = vim_strpbrk(arg,
3014 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3015 {
3016 c = *xp->xp_pattern;
3017 if (c == '&')
3018 {
3019 c = xp->xp_pattern[1];
3020 if (c == '&')
3021 {
3022 ++xp->xp_pattern;
3023 xp->xp_context = cmdidx != CMD_let || got_eq
3024 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3025 }
3026 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003029 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3030 xp->xp_pattern += 2;
3031
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034 else if (c == '$')
3035 {
3036 /* environment variable */
3037 xp->xp_context = EXPAND_ENV_VARS;
3038 }
3039 else if (c == '=')
3040 {
3041 got_eq = TRUE;
3042 xp->xp_context = EXPAND_EXPRESSION;
3043 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003044 else if (c == '#'
3045 && xp->xp_context == EXPAND_EXPRESSION)
3046 {
3047 /* Autoload function/variable contains '#'. */
3048 break;
3049 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003050 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 && xp->xp_context == EXPAND_FUNCTIONS
3052 && vim_strchr(xp->xp_pattern, '(') == NULL)
3053 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003054 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
3056 }
3057 else if (cmdidx != CMD_let || got_eq)
3058 {
3059 if (c == '"') /* string */
3060 {
3061 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3062 if (c == '\\' && xp->xp_pattern[1] != NUL)
3063 ++xp->xp_pattern;
3064 xp->xp_context = EXPAND_NOTHING;
3065 }
3066 else if (c == '\'') /* literal string */
3067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3070 /* skip */ ;
3071 xp->xp_context = EXPAND_NOTHING;
3072 }
3073 else if (c == '|')
3074 {
3075 if (xp->xp_pattern[1] == '|')
3076 {
3077 ++xp->xp_pattern;
3078 xp->xp_context = EXPAND_EXPRESSION;
3079 }
3080 else
3081 xp->xp_context = EXPAND_COMMANDS;
3082 }
3083 else
3084 xp->xp_context = EXPAND_EXPRESSION;
3085 }
3086 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087 /* Doesn't look like something valid, expand as an expression
3088 * anyway. */
3089 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 arg = xp->xp_pattern;
3091 if (*arg != NUL)
3092 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3093 /* skip */ ;
3094 }
3095 xp->xp_pattern = arg;
3096}
3097
3098#endif /* FEAT_CMDL_COMPL */
3099
3100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * ":unlet[!] var1 ... " command.
3102 */
3103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 ex_unletlock(eap, eap->arg, 0);
3107}
3108
3109/*
3110 * ":lockvar" and ":unlockvar" commands
3111 */
3112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116 int deep = 2;
3117
3118 if (eap->forceit)
3119 deep = -1;
3120 else if (vim_isdigit(*arg))
3121 {
3122 deep = getdigits(&arg);
3123 arg = skipwhite(arg);
3124 }
3125
3126 ex_unletlock(eap, arg, deep);
3127}
3128
3129/*
3130 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3131 */
3132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003133ex_unletlock(
3134 exarg_T *eap,
3135 char_u *argstart,
3136 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003137{
3138 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003141 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 do
3144 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003145 if (*arg == '$')
3146 {
3147 char_u *name = ++arg;
3148
3149 if (get_env_len(&arg) == 0)
3150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003151 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003152 return;
3153 }
3154 vim_unsetenv(name);
3155 arg = skipwhite(arg);
3156 continue;
3157 }
3158
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003159 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003160 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003161 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003162 if (lv.ll_name == NULL)
3163 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003164 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 if (name_end != NULL)
3168 {
3169 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003170 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003171 }
3172 if (!(eap->skip || error))
3173 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 break;
3175 }
3176
3177 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003178 {
3179 if (eap->cmdidx == CMD_unlet)
3180 {
3181 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3182 error = TRUE;
3183 }
3184 else
3185 {
3186 if (do_lock_var(&lv, name_end, deep,
3187 eap->cmdidx == CMD_lockvar) == FAIL)
3188 error = TRUE;
3189 }
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003192 if (!eap->skip)
3193 clear_lval(&lv);
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 arg = skipwhite(name_end);
3196 } while (!ends_excmd(*arg));
3197
3198 eap->nextcmd = check_nextcmd(arg);
3199}
3200
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202do_unlet_var(
3203 lval_T *lp,
3204 char_u *name_end,
3205 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003206{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 int ret = OK;
3208 int cc;
3209
3210 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003211 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 cc = *name_end;
3213 *name_end = NUL;
3214
3215 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003216 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003218 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003220 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003221 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003222 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003223 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003224 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 else if (lp->ll_range)
3226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003228 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003229 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003230
3231 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3232 {
3233 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003234 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003235 return FAIL;
3236 ll_li = li;
3237 ++ll_n1;
3238 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239
3240 /* Delete a range of List items. */
3241 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3242 {
3243 li = lp->ll_li->li_next;
3244 listitem_remove(lp->ll_list, lp->ll_li);
3245 lp->ll_li = li;
3246 ++lp->ll_n1;
3247 }
3248 }
3249 else
3250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 /* unlet a List item. */
3253 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003256 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 }
3258
3259 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262/*
3263 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003264 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
3266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 hashtab_T *ht;
3270 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003272 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003273 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 ht = find_var_ht(name, &varname);
3276 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003279 if (d == NULL)
3280 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281 if (ht == &globvarht)
3282 d = &globvardict;
3283 else if (ht == &compat_hashtab)
3284 d = &vimvardict;
3285 else
3286 {
3287 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3288 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3289 }
3290 if (d == NULL)
3291 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003292 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 return FAIL;
3294 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003295 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003297 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003298 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003299 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003300 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003301 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003302 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003303 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003304 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003305 return FAIL;
3306
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003307 delete_var(ht, hi);
3308 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003311 if (forceit)
3312 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return FAIL;
3315}
3316
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003317/*
3318 * Lock or unlock variable indicated by "lp".
3319 * "deep" is the levels to go (-1 for unlimited);
3320 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3321 */
3322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003323do_lock_var(
3324 lval_T *lp,
3325 char_u *name_end,
3326 int deep,
3327 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003328{
3329 int ret = OK;
3330 int cc;
3331 dictitem_T *di;
3332
3333 if (deep == 0) /* nothing to do */
3334 return OK;
3335
3336 if (lp->ll_tv == NULL)
3337 {
3338 cc = *name_end;
3339 *name_end = NUL;
3340
3341 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003342 di = find_var(lp->ll_name, NULL, TRUE);
3343 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003344 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003345 else if ((di->di_flags & DI_FLAGS_FIX)
3346 && di->di_tv.v_type != VAR_DICT
3347 && di->di_tv.v_type != VAR_LIST)
3348 /* For historic reasons this error is not given for a list or dict.
3349 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003350 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003351 else
3352 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003353 if (lock)
3354 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003356 di->di_flags &= ~DI_FLAGS_LOCK;
3357 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358 }
3359 *name_end = cc;
3360 }
3361 else if (lp->ll_range)
3362 {
3363 listitem_T *li = lp->ll_li;
3364
3365 /* (un)lock a range of List items. */
3366 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3367 {
3368 item_lock(&li->li_tv, deep, lock);
3369 li = li->li_next;
3370 ++lp->ll_n1;
3371 }
3372 }
3373 else if (lp->ll_list != NULL)
3374 /* (un)lock a List item. */
3375 item_lock(&lp->ll_li->li_tv, deep, lock);
3376 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003377 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003378 item_lock(&lp->ll_di->di_tv, deep, lock);
3379
3380 return ret;
3381}
3382
3383/*
3384 * Lock or unlock an item. "deep" is nr of levels to go.
3385 */
3386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003387item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003388{
3389 static int recurse = 0;
3390 list_T *l;
3391 listitem_T *li;
3392 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003393 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003394 hashitem_T *hi;
3395 int todo;
3396
3397 if (recurse >= DICT_MAXNEST)
3398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003399 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003400 return;
3401 }
3402 if (deep == 0)
3403 return;
3404 ++recurse;
3405
3406 /* lock/unlock the item itself */
3407 if (lock)
3408 tv->v_lock |= VAR_LOCKED;
3409 else
3410 tv->v_lock &= ~VAR_LOCKED;
3411
3412 switch (tv->v_type)
3413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003414 case VAR_UNKNOWN:
3415 case VAR_NUMBER:
3416 case VAR_STRING:
3417 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003418 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003419 case VAR_FLOAT:
3420 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003421 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003422 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003423 break;
3424
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003425 case VAR_BLOB:
3426 if ((b = tv->vval.v_blob) != NULL)
3427 {
3428 if (lock)
3429 b->bv_lock |= VAR_LOCKED;
3430 else
3431 b->bv_lock &= ~VAR_LOCKED;
3432 }
3433 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003434 case VAR_LIST:
3435 if ((l = tv->vval.v_list) != NULL)
3436 {
3437 if (lock)
3438 l->lv_lock |= VAR_LOCKED;
3439 else
3440 l->lv_lock &= ~VAR_LOCKED;
3441 if (deep < 0 || deep > 1)
3442 /* recursive: lock/unlock the items the List contains */
3443 for (li = l->lv_first; li != NULL; li = li->li_next)
3444 item_lock(&li->li_tv, deep - 1, lock);
3445 }
3446 break;
3447 case VAR_DICT:
3448 if ((d = tv->vval.v_dict) != NULL)
3449 {
3450 if (lock)
3451 d->dv_lock |= VAR_LOCKED;
3452 else
3453 d->dv_lock &= ~VAR_LOCKED;
3454 if (deep < 0 || deep > 1)
3455 {
3456 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003458 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3459 {
3460 if (!HASHITEM_EMPTY(hi))
3461 {
3462 --todo;
3463 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3464 }
3465 }
3466 }
3467 }
3468 }
3469 --recurse;
3470}
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3473/*
3474 * Delete all "menutrans_" variables.
3475 */
3476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003477del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
Bram Moolenaar33570922005-01-25 22:26:29 +00003479 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003480 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003485 {
3486 if (!HASHITEM_EMPTY(hi))
3487 {
3488 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3490 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003491 }
3492 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003493 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494}
3495#endif
3496
3497#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3498
3499/*
3500 * Local string buffer for the next two functions to store a variable name
3501 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3502 * get_user_var_name().
3503 */
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505static char_u *varnamebuf = NULL;
3506static int varnamebuflen = 0;
3507
3508/*
3509 * Function to concatenate a prefix and a variable name.
3510 */
3511 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 int len;
3515
3516 len = (int)STRLEN(name) + 3;
3517 if (len > varnamebuflen)
3518 {
3519 vim_free(varnamebuf);
3520 len += 10; /* some additional space */
3521 varnamebuf = alloc(len);
3522 if (varnamebuf == NULL)
3523 {
3524 varnamebuflen = 0;
3525 return NULL;
3526 }
3527 varnamebuflen = len;
3528 }
3529 *varnamebuf = prefix;
3530 varnamebuf[1] = ':';
3531 STRCPY(varnamebuf + 2, name);
3532 return varnamebuf;
3533}
3534
3535/*
3536 * Function given to ExpandGeneric() to obtain the list of user defined
3537 * (global/buffer/window/built-in) variable names.
3538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003542 static long_u gdone;
3543 static long_u bdone;
3544 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003545 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003546 static int vidx;
3547 static hashitem_T *hi;
3548 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003551 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003552 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003553 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003554 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003555
3556 /* Global variables */
3557 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003559 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003560 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003561 else
3562 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003563 while (HASHITEM_EMPTY(hi))
3564 ++hi;
3565 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3566 return cat_prefix_varname('g', hi->hi_key);
3567 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003569
3570 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003571 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003576 else
3577 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003578 while (HASHITEM_EMPTY(hi))
3579 ++hi;
3580 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003582
3583 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003584 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003587 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003588 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003589 else
3590 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003591 while (HASHITEM_EMPTY(hi))
3592 ++hi;
3593 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003595
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003596 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003597 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003598 if (tdone < ht->ht_used)
3599 {
3600 if (tdone++ == 0)
3601 hi = ht->ht_array;
3602 else
3603 ++hi;
3604 while (HASHITEM_EMPTY(hi))
3605 ++hi;
3606 return cat_prefix_varname('t', hi->hi_key);
3607 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003608
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 /* v: variables */
3610 if (vidx < VV_LEN)
3611 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaard23a8232018-02-10 18:45:26 +01003613 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 varnamebuflen = 0;
3615 return NULL;
3616}
3617
3618#endif /* FEAT_CMDL_COMPL */
3619
3620/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003621 * Return TRUE if "pat" matches "text".
3622 * Does not use 'cpo' and always uses 'magic'.
3623 */
3624 static int
3625pattern_match(char_u *pat, char_u *text, int ic)
3626{
3627 int matches = FALSE;
3628 char_u *save_cpo;
3629 regmatch_T regmatch;
3630
3631 /* avoid 'l' flag in 'cpoptions' */
3632 save_cpo = p_cpo;
3633 p_cpo = (char_u *)"";
3634 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3635 if (regmatch.regprog != NULL)
3636 {
3637 regmatch.rm_ic = ic;
3638 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3639 vim_regfree(regmatch.regprog);
3640 }
3641 p_cpo = save_cpo;
3642 return matches;
3643}
3644
3645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003647 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3649 */
3650
3651/*
3652 * Handle zero level expression.
3653 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003655 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 * Return OK or FAIL.
3657 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003659eval0(
3660 char_u *arg,
3661 typval_T *rettv,
3662 char_u **nextcmd,
3663 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 int ret;
3666 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003667 int did_emsg_before = did_emsg;
3668 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 if (ret == FAIL || !ends_excmd(*p))
3673 {
3674 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 /*
3677 * Report the invalid expression unless the expression evaluation has
3678 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003679 * exception, or we already gave a more specific error.
3680 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003682 if (!aborting() && did_emsg == did_emsg_before
3683 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003684 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 ret = FAIL;
3686 }
3687 if (nextcmd != NULL)
3688 *nextcmd = check_nextcmd(p);
3689
3690 return ret;
3691}
3692
3693/*
3694 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003695 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 *
3697 * "arg" must point to the first non-white of the expression.
3698 * "arg" is advanced to the next non-white after the recognized expression.
3699 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003700 * Note: "rettv.v_lock" is not set.
3701 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 * Return OK or FAIL.
3703 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003708 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 /*
3711 * Get the first variable.
3712 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FAIL;
3715
3716 if ((*arg)[0] == '?')
3717 {
3718 result = FALSE;
3719 if (evaluate)
3720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003721 int error = FALSE;
3722
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003723 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 if (error)
3727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
3730 /*
3731 * Get the second variable.
3732 */
3733 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 return FAIL;
3736
3737 /*
3738 * Check for the ":".
3739 */
3740 if ((*arg)[0] != ':')
3741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003742 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return FAIL;
3746 }
3747
3748 /*
3749 * Get the third variable.
3750 */
3751 *arg = skipwhite(*arg + 1);
3752 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3753 {
3754 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 return FAIL;
3757 }
3758 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003759 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761
3762 return OK;
3763}
3764
3765/*
3766 * Handle first level expression:
3767 * expr2 || expr2 || expr2 logical OR
3768 *
3769 * "arg" must point to the first non-white of the expression.
3770 * "arg" is advanced to the next non-white after the recognized expression.
3771 *
3772 * Return OK or FAIL.
3773 */
3774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003775eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
Bram Moolenaar33570922005-01-25 22:26:29 +00003777 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 long result;
3779 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * Get the first variable.
3784 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 /*
3789 * Repeat until there is no following "||".
3790 */
3791 first = TRUE;
3792 result = FALSE;
3793 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3794 {
3795 if (evaluate && first)
3796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003797 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 if (error)
3801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 first = FALSE;
3803 }
3804
3805 /*
3806 * Get the second variable.
3807 */
3808 *arg = skipwhite(*arg + 2);
3809 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3810 return FAIL;
3811
3812 /*
3813 * Compute the result.
3814 */
3815 if (evaluate && !result)
3816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003817 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003819 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003820 if (error)
3821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 if (evaluate)
3824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825 rettv->v_type = VAR_NUMBER;
3826 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829
3830 return OK;
3831}
3832
3833/*
3834 * Handle second level expression:
3835 * expr3 && expr3 && expr3 logical AND
3836 *
3837 * "arg" must point to the first non-white of the expression.
3838 * "arg" is advanced to the next non-white after the recognized expression.
3839 *
3840 * Return OK or FAIL.
3841 */
3842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 long result;
3847 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
3850 /*
3851 * Get the first variable.
3852 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return FAIL;
3855
3856 /*
3857 * Repeat until there is no following "&&".
3858 */
3859 first = TRUE;
3860 result = TRUE;
3861 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3862 {
3863 if (evaluate && first)
3864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003865 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868 if (error)
3869 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 first = FALSE;
3871 }
3872
3873 /*
3874 * Get the second variable.
3875 */
3876 *arg = skipwhite(*arg + 2);
3877 if (eval4(arg, &var2, evaluate && result) == FAIL)
3878 return FAIL;
3879
3880 /*
3881 * Compute the result.
3882 */
3883 if (evaluate && result)
3884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 if (error)
3889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891 if (evaluate)
3892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893 rettv->v_type = VAR_NUMBER;
3894 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 }
3897
3898 return OK;
3899}
3900
3901/*
3902 * Handle third level expression:
3903 * var1 == var2
3904 * var1 =~ var2
3905 * var1 != var2
3906 * var1 !~ var2
3907 * var1 > var2
3908 * var1 >= var2
3909 * var1 < var2
3910 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003911 * var1 is var2
3912 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 *
3914 * "arg" must point to the first non-white of the expression.
3915 * "arg" is advanced to the next non-white after the recognized expression.
3916 *
3917 * Return OK or FAIL.
3918 */
3919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *p;
3924 int i;
3925 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003926 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 /*
3931 * Get the first variable.
3932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 return FAIL;
3935
3936 p = *arg;
3937 switch (p[0])
3938 {
3939 case '=': if (p[1] == '=')
3940 type = TYPE_EQUAL;
3941 else if (p[1] == '~')
3942 type = TYPE_MATCH;
3943 break;
3944 case '!': if (p[1] == '=')
3945 type = TYPE_NEQUAL;
3946 else if (p[1] == '~')
3947 type = TYPE_NOMATCH;
3948 break;
3949 case '>': if (p[1] != '=')
3950 {
3951 type = TYPE_GREATER;
3952 len = 1;
3953 }
3954 else
3955 type = TYPE_GEQUAL;
3956 break;
3957 case '<': if (p[1] != '=')
3958 {
3959 type = TYPE_SMALLER;
3960 len = 1;
3961 }
3962 else
3963 type = TYPE_SEQUAL;
3964 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003965 case 'i': if (p[1] == 's')
3966 {
3967 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3968 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003969 i = p[len];
3970 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003971 {
3972 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3973 type_is = TRUE;
3974 }
3975 }
3976 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978
3979 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003980 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 if (type != TYPE_UNKNOWN)
3983 {
3984 /* extra question mark appended: ignore case */
3985 if (p[len] == '?')
3986 {
3987 ic = TRUE;
3988 ++len;
3989 }
3990 /* extra '#' appended: match case */
3991 else if (p[len] == '#')
3992 {
3993 ic = FALSE;
3994 ++len;
3995 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003996 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else
3998 ic = p_ic;
3999
4000 /*
4001 * Get the second variable.
4002 */
4003 *arg = skipwhite(p + len);
4004 if (eval5(arg, &var2, evaluate) == FAIL)
4005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FAIL;
4008 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004009 if (evaluate)
4010 {
4011 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4012
4013 clear_tv(&var2);
4014 return ret;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017
4018 return OK;
4019}
4020
4021/*
4022 * Handle fourth level expression:
4023 * + number addition
4024 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004025 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004026 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 *
4028 * "arg" must point to the first non-white of the expression.
4029 * "arg" is advanced to the next non-white after the recognized expression.
4030 *
4031 * Return OK or FAIL.
4032 */
4033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004034eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 typval_T var2;
4037 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004039 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040#ifdef FEAT_FLOAT
4041 float_T f1 = 0, f2 = 0;
4042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 char_u *s1, *s2;
4044 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4045 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004046 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 /*
4049 * Get the first variable.
4050 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004051 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return FAIL;
4053
4054 /*
4055 * Repeat computing, until no '+', '-' or '.' is following.
4056 */
4057 for (;;)
4058 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004059 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004061 concat = op == '.'
4062 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4063 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 break;
4065
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004066 if ((op != '+' || (rettv->v_type != VAR_LIST
4067 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068#ifdef FEAT_FLOAT
4069 && (op == '.' || rettv->v_type != VAR_FLOAT)
4070#endif
4071 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 {
4073 /* For "list + ...", an illegal use of the first operand as
4074 * a number cannot be determined before evaluating the 2nd
4075 * operand: if this is also a list, all is ok.
4076 * For "something . ...", "something - ..." or "non-list + ...",
4077 * we know that the first operand needs to be a string or number
4078 * without evaluating the 2nd operand. So check before to avoid
4079 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004080 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 {
4082 clear_tv(rettv);
4083 return FAIL;
4084 }
4085 }
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 /*
4088 * Get the second variable.
4089 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004090 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4091 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004093 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return FAIL;
4097 }
4098
4099 if (evaluate)
4100 {
4101 /*
4102 * Compute the result.
4103 */
4104 if (op == '.')
4105 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004106 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4107 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (s2 == NULL) /* type error ? */
4109 {
4110 clear_tv(rettv);
4111 clear_tv(&var2);
4112 return FAIL;
4113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004114 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
4116 rettv->v_type = VAR_STRING;
4117 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004119 else if (op == '+' && rettv->v_type == VAR_BLOB
4120 && var2.v_type == VAR_BLOB)
4121 {
4122 blob_T *b1 = rettv->vval.v_blob;
4123 blob_T *b2 = var2.vval.v_blob;
4124 blob_T *b = blob_alloc();
4125 int i;
4126
4127 if (b != NULL)
4128 {
4129 for (i = 0; i < blob_len(b1); i++)
4130 ga_append(&b->bv_ga, blob_get(b1, i));
4131 for (i = 0; i < blob_len(b2); i++)
4132 ga_append(&b->bv_ga, blob_get(b2, i));
4133
4134 clear_tv(rettv);
4135 rettv_blob_set(rettv, b);
4136 }
4137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004138 else if (op == '+' && rettv->v_type == VAR_LIST
4139 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004140 {
4141 /* concatenate Lists */
4142 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4143 &var3) == FAIL)
4144 {
4145 clear_tv(rettv);
4146 clear_tv(&var2);
4147 return FAIL;
4148 }
4149 clear_tv(rettv);
4150 *rettv = var3;
4151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
4153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 int error = FALSE;
4155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156#ifdef FEAT_FLOAT
4157 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004158 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004159 f1 = rettv->vval.v_float;
4160 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004162 else
4163#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004165 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 if (error)
4167 {
4168 /* This can only happen for "list + non-list". For
4169 * "non-list + ..." or "something - ...", we returned
4170 * before evaluating the 2nd operand. */
4171 clear_tv(rettv);
4172 return FAIL;
4173 }
4174#ifdef FEAT_FLOAT
4175 if (var2.v_type == VAR_FLOAT)
4176 f1 = n1;
4177#endif
4178 }
4179#ifdef FEAT_FLOAT
4180 if (var2.v_type == VAR_FLOAT)
4181 {
4182 f2 = var2.vval.v_float;
4183 n2 = 0;
4184 }
4185 else
4186#endif
4187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004188 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004189 if (error)
4190 {
4191 clear_tv(rettv);
4192 clear_tv(&var2);
4193 return FAIL;
4194 }
4195#ifdef FEAT_FLOAT
4196 if (rettv->v_type == VAR_FLOAT)
4197 f2 = n2;
4198#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004201
4202#ifdef FEAT_FLOAT
4203 /* If there is a float on either side the result is a float. */
4204 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4205 {
4206 if (op == '+')
4207 f1 = f1 + f2;
4208 else
4209 f1 = f1 - f2;
4210 rettv->v_type = VAR_FLOAT;
4211 rettv->vval.v_float = f1;
4212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004214#endif
4215 {
4216 if (op == '+')
4217 n1 = n1 + n2;
4218 else
4219 n1 = n1 - n2;
4220 rettv->v_type = VAR_NUMBER;
4221 rettv->vval.v_number = n1;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227 return OK;
4228}
4229
4230/*
4231 * Handle fifth level expression:
4232 * * number multiplication
4233 * / number division
4234 * % number modulo
4235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
4239 * Return OK or FAIL.
4240 */
4241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004242eval6(
4243 char_u **arg,
4244 typval_T *rettv,
4245 int evaluate,
4246 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
Bram Moolenaar33570922005-01-25 22:26:29 +00004248 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004250 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004251#ifdef FEAT_FLOAT
4252 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004253 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004254#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /*
4258 * Get the first variable.
4259 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004260 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 return FAIL;
4262
4263 /*
4264 * Repeat computing, until no '*', '/' or '%' is following.
4265 */
4266 for (;;)
4267 {
4268 op = **arg;
4269 if (op != '*' && op != '/' && op != '%')
4270 break;
4271
4272 if (evaluate)
4273 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004274#ifdef FEAT_FLOAT
4275 if (rettv->v_type == VAR_FLOAT)
4276 {
4277 f1 = rettv->vval.v_float;
4278 use_float = TRUE;
4279 n1 = 0;
4280 }
4281 else
4282#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004283 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 if (error)
4286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 else
4289 n1 = 0;
4290
4291 /*
4292 * Get the second variable.
4293 */
4294 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004295 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return FAIL;
4297
4298 if (evaluate)
4299 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004300#ifdef FEAT_FLOAT
4301 if (var2.v_type == VAR_FLOAT)
4302 {
4303 if (!use_float)
4304 {
4305 f1 = n1;
4306 use_float = TRUE;
4307 }
4308 f2 = var2.vval.v_float;
4309 n2 = 0;
4310 }
4311 else
4312#endif
4313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004314 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004315 clear_tv(&var2);
4316 if (error)
4317 return FAIL;
4318#ifdef FEAT_FLOAT
4319 if (use_float)
4320 f2 = n2;
4321#endif
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004326 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004328#ifdef FEAT_FLOAT
4329 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331 if (op == '*')
4332 f1 = f1 * f2;
4333 else if (op == '/')
4334 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004335# ifdef VMS
4336 /* VMS crashes on divide by zero, work around it */
4337 if (f2 == 0.0)
4338 {
4339 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004340 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004341 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004342 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004343 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004344 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004345 }
4346 else
4347 f1 = f1 / f2;
4348# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004349 /* We rely on the floating point library to handle divide
4350 * by zero to result in "inf" and not a crash. */
4351 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004352# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004356 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 return FAIL;
4358 }
4359 rettv->v_type = VAR_FLOAT;
4360 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 if (op == '*')
4366 n1 = n1 * n2;
4367 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004368 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004370 n1 = num_modulus(n1, n2);
4371
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 rettv->v_type = VAR_NUMBER;
4373 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377
4378 return OK;
4379}
4380
4381/*
4382 * Handle sixth level expression:
4383 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004384 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004385 * "string" string constant
4386 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 * &option-name option value
4388 * @r register contents
4389 * identifier variable value
4390 * function() function call
4391 * $VAR environment variable
4392 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004393 * [expr, expr] List
4394 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 *
4396 * Also handle:
4397 * ! in front logical NOT
4398 * - in front unary minus
4399 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 * trailing [] subscript in String or List
4401 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 *
4403 * "arg" must point to the first non-white of the expression.
4404 * "arg" is advanced to the next non-white after the recognized expression.
4405 *
4406 * Return OK or FAIL.
4407 */
4408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004409eval7(
4410 char_u **arg,
4411 typval_T *rettv,
4412 int evaluate,
4413 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004415 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 int len;
4417 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 char_u *start_leader, *end_leader;
4419 int ret = OK;
4420 char_u *alias;
4421
4422 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004429 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 */
4431 start_leader = *arg;
4432 while (**arg == '!' || **arg == '-' || **arg == '+')
4433 *arg = skipwhite(*arg + 1);
4434 end_leader = *arg;
4435
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004436 if (**arg == '.' && (!isdigit(*(*arg + 1))
4437#ifdef FEAT_FLOAT
4438 || current_sctx.sc_version < 2
4439#endif
4440 ))
4441 {
4442 semsg(_(e_invexpr2), *arg);
4443 ++*arg;
4444 return FAIL;
4445 }
4446
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 switch (**arg)
4448 {
4449 /*
4450 * Number constant.
4451 */
4452 case '0':
4453 case '1':
4454 case '2':
4455 case '3':
4456 case '4':
4457 case '5':
4458 case '6':
4459 case '7':
4460 case '8':
4461 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004462 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463 {
4464#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004465 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004466 int get_float = FALSE;
4467
4468 /* We accept a float when the format matches
4469 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004470 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004471 * With script version 2 and later the leading digit can be
4472 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004473 * Don't look for a float after the "." operator, so that
4474 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004475 if (**arg == '.')
4476 p = *arg;
4477 else
4478 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004479 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004481 get_float = TRUE;
4482 p = skipdigits(p + 2);
4483 if (*p == 'e' || *p == 'E')
4484 {
4485 ++p;
4486 if (*p == '-' || *p == '+')
4487 ++p;
4488 if (!vim_isdigit(*p))
4489 get_float = FALSE;
4490 else
4491 p = skipdigits(p + 1);
4492 }
4493 if (ASCII_ISALPHA(*p) || *p == '.')
4494 get_float = FALSE;
4495 }
4496 if (get_float)
4497 {
4498 float_T f;
4499
4500 *arg += string2float(*arg, &f);
4501 if (evaluate)
4502 {
4503 rettv->v_type = VAR_FLOAT;
4504 rettv->vval.v_float = f;
4505 }
4506 }
4507 else
4508#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004509 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004510 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004511 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004512 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004513
4514 // Blob constant: 0z0123456789abcdef
4515 if (evaluate)
4516 blob = blob_alloc();
4517 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4518 {
4519 if (!vim_isxdigit(bp[1]))
4520 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004521 if (blob != NULL)
4522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004523 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004524 ga_clear(&blob->bv_ga);
4525 VIM_CLEAR(blob);
4526 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004527 ret = FAIL;
4528 break;
4529 }
4530 if (blob != NULL)
4531 ga_append(&blob->bv_ga,
4532 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004533 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4534 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004535 }
4536 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004537 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004538 *arg = bp;
4539 }
4540 else
4541 {
4542 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004543 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4544 if (len == 0)
4545 {
4546 semsg(_(e_invexpr2), *arg);
4547 ret = FAIL;
4548 break;
4549 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004550 *arg += len;
4551 if (evaluate)
4552 {
4553 rettv->v_type = VAR_NUMBER;
4554 rettv->vval.v_number = n;
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /*
4561 * String constant: "string".
4562 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004563 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 break;
4565
4566 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 break;
4571
4572 /*
4573 * List: [expr, expr]
4574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 break;
4577
4578 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004579 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 * Dictionary: {key: val, key: val}
4581 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004582 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4583 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004584 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004585 break;
4586
4587 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004588 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004590 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 break;
4592
4593 /*
4594 * Environment variable: $VAR.
4595 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004596 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 break;
4598
4599 /*
4600 * Register contents: @r.
4601 */
4602 case '@': ++*arg;
4603 if (evaluate)
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004606 rettv->vval.v_string = get_reg_contents(**arg,
4607 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 if (**arg != NUL)
4610 ++*arg;
4611 break;
4612
4613 /*
4614 * nested expression: (expression).
4615 */
4616 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (**arg == ')')
4619 ++*arg;
4620 else if (ret == OK)
4621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004622 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 ret = FAIL;
4625 }
4626 break;
4627
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631
4632 if (ret == NOTDONE)
4633 {
4634 /*
4635 * Must be a variable or function name.
4636 * Can also be a curly-braces kind of name: {expr}.
4637 */
4638 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004639 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 if (alias != NULL)
4641 s = alias;
4642
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004643 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 ret = FAIL;
4645 else
4646 {
4647 if (**arg == '(') /* recursive! */
4648 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004649 partial_T *partial;
4650
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004651 if (!evaluate)
4652 check_vars(s, len);
4653
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 /* If "s" is the name of a variable of type VAR_FUNC
4655 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004656 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004658 /* Need to make a copy, in case evaluating the arguments makes
4659 * the name invalid. */
4660 s = vim_strsave(s);
4661 if (s == NULL)
4662 ret = FAIL;
4663 else
4664 /* Invoke the function. */
4665 ret = get_func_tv(s, len, rettv, arg,
4666 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4667 &len, evaluate, partial, NULL);
4668 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004669
4670 /* If evaluate is FALSE rettv->v_type was not set in
4671 * get_func_tv, but it's needed in handle_subscript() to parse
4672 * what follows. So set it here. */
4673 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4674 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004675 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004676 rettv->v_type = VAR_FUNC;
4677 }
4678
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 /* Stop the expression evaluation when immediately
4680 * aborting on error, or when an interrupt occurred or
4681 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004682 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 {
4684 if (ret == OK)
4685 clear_tv(rettv);
4686 ret = FAIL;
4687 }
4688 }
4689 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004690 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004691 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004692 {
4693 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004694 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004697 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 }
4699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 *arg = skipwhite(*arg);
4701
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004702 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4703 * expr(expr). */
4704 if (ret == OK)
4705 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
4707 /*
4708 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4709 */
4710 if (ret == OK && evaluate && end_leader > start_leader)
4711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004712 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004713 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714#ifdef FEAT_FLOAT
4715 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 if (rettv->v_type == VAR_FLOAT)
4718 f = rettv->vval.v_float;
4719 else
4720#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004721 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 clear_tv(rettv);
4725 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 else
4728 {
4729 while (end_leader > start_leader)
4730 {
4731 --end_leader;
4732 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733 {
4734#ifdef FEAT_FLOAT
4735 if (rettv->v_type == VAR_FLOAT)
4736 f = !f;
4737 else
4738#endif
4739 val = !val;
4740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004742 {
4743#ifdef FEAT_FLOAT
4744 if (rettv->v_type == VAR_FLOAT)
4745 f = -f;
4746 else
4747#endif
4748 val = -val;
4749 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
4753 {
4754 clear_tv(rettv);
4755 rettv->vval.v_float = f;
4756 }
4757 else
4758#endif
4759 {
4760 clear_tv(rettv);
4761 rettv->v_type = VAR_NUMBER;
4762 rettv->vval.v_number = val;
4763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766
4767 return ret;
4768}
4769
4770/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004771 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4772 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4774 */
4775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004776eval_index(
4777 char_u **arg,
4778 typval_T *rettv,
4779 int evaluate,
4780 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781{
4782 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004783 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004784 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 long len = -1;
4787 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790
Bram Moolenaara03f2332016-02-06 18:09:59 +01004791 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004793 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004794 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004795 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004796 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004797 return FAIL;
4798 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004799#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004800 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004801 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004802 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004803#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004804 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004805 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004806 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004807 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004808 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004809 return FAIL;
4810 case VAR_UNKNOWN:
4811 if (evaluate)
4812 return FAIL;
4813 /* FALLTHROUGH */
4814
4815 case VAR_STRING:
4816 case VAR_NUMBER:
4817 case VAR_LIST:
4818 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004819 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004820 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004821 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004823 init_tv(&var1);
4824 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004827 /*
4828 * dict.name
4829 */
4830 key = *arg + 1;
4831 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4832 ;
4833 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836 }
4837 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004839 /*
4840 * something[idx]
4841 *
4842 * Get the (first) variable from inside the [].
4843 */
4844 *arg = skipwhite(*arg + 1);
4845 if (**arg == ':')
4846 empty1 = TRUE;
4847 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4848 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 {
4851 /* not a number or string */
4852 clear_tv(&var1);
4853 return FAIL;
4854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855
4856 /*
4857 * Get the second variable from inside the [:].
4858 */
4859 if (**arg == ':')
4860 {
4861 range = TRUE;
4862 *arg = skipwhite(*arg + 1);
4863 if (**arg == ']')
4864 empty2 = TRUE;
4865 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867 if (!empty1)
4868 clear_tv(&var1);
4869 return FAIL;
4870 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004871 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 {
4873 /* not a number or string */
4874 if (!empty1)
4875 clear_tv(&var1);
4876 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004877 return FAIL;
4878 }
4879 }
4880
4881 /* Check for the ']'. */
4882 if (**arg != ']')
4883 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004884 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004885 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 clear_tv(&var1);
4887 if (range)
4888 clear_tv(&var2);
4889 return FAIL;
4890 }
4891 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 }
4893
4894 if (evaluate)
4895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004896 n1 = 0;
4897 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004899 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 }
4902 if (range)
4903 {
4904 if (empty2)
4905 n2 = -1;
4906 else
4907 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004908 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 }
4911 }
4912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004915 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004916 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004917 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004918 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004919 case VAR_SPECIAL:
4920 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004921 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004922 break; /* not evaluating, skipping over subscript */
4923
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924 case VAR_NUMBER:
4925 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004926 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004927 len = (long)STRLEN(s);
4928 if (range)
4929 {
4930 /* The resulting variable is a substring. If the indexes
4931 * are out of range the result is empty. */
4932 if (n1 < 0)
4933 {
4934 n1 = len + n1;
4935 if (n1 < 0)
4936 n1 = 0;
4937 }
4938 if (n2 < 0)
4939 n2 = len + n2;
4940 else if (n2 >= len)
4941 n2 = len;
4942 if (n1 >= len || n2 < 0 || n1 > n2)
4943 s = NULL;
4944 else
4945 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4946 }
4947 else
4948 {
4949 /* The resulting variable is a string of a single
4950 * character. If the index is too big or negative the
4951 * result is empty. */
4952 if (n1 >= len || n1 < 0)
4953 s = NULL;
4954 else
4955 s = vim_strnsave(s + n1, 1);
4956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 clear_tv(rettv);
4958 rettv->v_type = VAR_STRING;
4959 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 break;
4961
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004962 case VAR_BLOB:
4963 len = blob_len(rettv->vval.v_blob);
4964 if (range)
4965 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004966 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004967 // are out of range the result is empty.
4968 if (n1 < 0)
4969 {
4970 n1 = len + n1;
4971 if (n1 < 0)
4972 n1 = 0;
4973 }
4974 if (n2 < 0)
4975 n2 = len + n2;
4976 else if (n2 >= len)
4977 n2 = len - 1;
4978 if (n1 >= len || n2 < 0 || n1 > n2)
4979 {
4980 clear_tv(rettv);
4981 rettv->v_type = VAR_BLOB;
4982 rettv->vval.v_blob = NULL;
4983 }
4984 else
4985 {
4986 blob_T *blob = blob_alloc();
4987
4988 if (blob != NULL)
4989 {
4990 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4991 {
4992 blob_free(blob);
4993 return FAIL;
4994 }
4995 blob->bv_ga.ga_len = n2 - n1 + 1;
4996 for (i = n1; i <= n2; i++)
4997 blob_set(blob, i - n1,
4998 blob_get(rettv->vval.v_blob, i));
4999
5000 clear_tv(rettv);
5001 rettv_blob_set(rettv, blob);
5002 }
5003 }
5004 }
5005 else
5006 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005007 // The resulting variable is a byte value.
5008 // If the index is too big or negative that is an error.
5009 if (n1 < 0)
5010 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005011 if (n1 < len && n1 >= 0)
5012 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005013 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005014
5015 clear_tv(rettv);
5016 rettv->v_type = VAR_NUMBER;
5017 rettv->vval.v_number = v;
5018 }
5019 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005020 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005021 }
5022 break;
5023
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005024 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005026 if (n1 < 0)
5027 n1 = len + n1;
5028 if (!empty1 && (n1 < 0 || n1 >= len))
5029 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005030 /* For a range we allow invalid values and return an empty
5031 * list. A list index out of range is an error. */
5032 if (!range)
5033 {
5034 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005035 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005036 return FAIL;
5037 }
5038 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005039 }
5040 if (range)
5041 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 list_T *l;
5043 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005044
5045 if (n2 < 0)
5046 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005047 else if (n2 >= len)
5048 n2 = len - 1;
5049 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005050 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005051 l = list_alloc();
5052 if (l == NULL)
5053 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 n1 <= n2; ++n1)
5056 {
5057 if (list_append_tv(l, &item->li_tv) == FAIL)
5058 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005059 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 return FAIL;
5061 }
5062 item = item->li_next;
5063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005065 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 }
5067 else
5068 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005069 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 clear_tv(rettv);
5071 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005072 }
5073 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074
5075 case VAR_DICT:
5076 if (range)
5077 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005078 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005079 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 if (len == -1)
5081 clear_tv(&var1);
5082 return FAIL;
5083 }
5084 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005085 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086
5087 if (len == -1)
5088 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005089 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005090 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 clear_tv(&var1);
5093 return FAIL;
5094 }
5095 }
5096
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005097 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005099 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005100 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (len == -1)
5102 clear_tv(&var1);
5103 if (item == NULL)
5104 return FAIL;
5105
5106 copy_tv(&item->di_tv, &var1);
5107 clear_tv(rettv);
5108 *rettv = var1;
5109 }
5110 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 }
5112 }
5113
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 return OK;
5115}
5116
5117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 * Get an option value.
5119 * "arg" points to the '&' or '+' before the option name.
5120 * "arg" is advanced to character after the option name.
5121 * Return OK or FAIL.
5122 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005124get_option_tv(
5125 char_u **arg,
5126 typval_T *rettv, /* when NULL, only check if option exists */
5127 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128{
5129 char_u *option_end;
5130 long numval;
5131 char_u *stringval;
5132 int opt_type;
5133 int c;
5134 int working = (**arg == '+'); /* has("+option") */
5135 int ret = OK;
5136 int opt_flags;
5137
5138 /*
5139 * Isolate the option name and find its value.
5140 */
5141 option_end = find_option_end(arg, &opt_flags);
5142 if (option_end == NULL)
5143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005145 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 return FAIL;
5147 }
5148
5149 if (!evaluate)
5150 {
5151 *arg = option_end;
5152 return OK;
5153 }
5154
5155 c = *option_end;
5156 *option_end = NUL;
5157 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 if (opt_type == -3) /* invalid name */
5161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005163 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 ret = FAIL;
5165 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
5168 if (opt_type == -2) /* hidden string option */
5169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 rettv->v_type = VAR_STRING;
5171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173 else if (opt_type == -1) /* hidden number option */
5174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 rettv->v_type = VAR_NUMBER;
5176 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 else if (opt_type == 1) /* number option */
5179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 rettv->v_type = VAR_NUMBER;
5181 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183 else /* string option */
5184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 rettv->v_type = VAR_STRING;
5186 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188 }
5189 else if (working && (opt_type == -2 || opt_type == -1))
5190 ret = FAIL;
5191
5192 *option_end = c; /* put back for error messages */
5193 *arg = option_end;
5194
5195 return ret;
5196}
5197
5198/*
5199 * Allocate a variable for a string constant.
5200 * Return OK or FAIL.
5201 */
5202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005203get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
5205 char_u *p;
5206 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 int extra = 0;
5208
5209 /*
5210 * Find the end of the string, skipping backslashed characters.
5211 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005212 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 if (*p == '\\' && p[1] != NUL)
5215 {
5216 ++p;
5217 /* A "\<x>" form occupies at least 4 characters, and produces up
5218 * to 6 characters: reserve space for 2 extra */
5219 if (*p == '<')
5220 extra += 2;
5221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223
5224 if (*p != '"')
5225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005226 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return FAIL;
5228 }
5229
5230 /* If only parsing, set *arg and return here */
5231 if (!evaluate)
5232 {
5233 *arg = p + 1;
5234 return OK;
5235 }
5236
5237 /*
5238 * Copy the string into allocated memory, handling backslashed
5239 * characters.
5240 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005241 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 if (name == NULL)
5243 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 rettv->v_type = VAR_STRING;
5245 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
5249 if (*p == '\\')
5250 {
5251 switch (*++p)
5252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 case 'b': *name++ = BS; ++p; break;
5254 case 'e': *name++ = ESC; ++p; break;
5255 case 'f': *name++ = FF; ++p; break;
5256 case 'n': *name++ = NL; ++p; break;
5257 case 'r': *name++ = CAR; ++p; break;
5258 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 case 'X': /* hex: "\x1", "\x12" */
5261 case 'x':
5262 case 'u': /* Unicode: "\u0023" */
5263 case 'U':
5264 if (vim_isxdigit(p[1]))
5265 {
5266 int n, nr;
5267 int c = toupper(*p);
5268
5269 if (c == 'X')
5270 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005271 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005273 else
5274 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 nr = 0;
5276 while (--n >= 0 && vim_isxdigit(p[1]))
5277 {
5278 ++p;
5279 nr = (nr << 4) + hex2nr(*p);
5280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 /* For "\u" store the number according to
5283 * 'encoding'. */
5284 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 break;
5290
5291 /* octal: "\1", "\12", "\123" */
5292 case '0':
5293 case '1':
5294 case '2':
5295 case '3':
5296 case '4':
5297 case '5':
5298 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 case '7': *name = *p++ - '0';
5300 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 *name = (*name << 3) + *p++ - '0';
5303 if (*p >= '0' && *p <= '7')
5304 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 break;
5308
5309 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005310 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (extra != 0)
5312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 break;
5315 }
5316 /* FALLTHROUGH */
5317
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 break;
5320 }
5321 }
5322 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005327 if (*p != NUL) /* just in case */
5328 ++p;
5329 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 return OK;
5332}
5333
5334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005335 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 * Return OK or FAIL.
5337 */
5338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005339get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
5341 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005342 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005343 int reduce = 0;
5344
5345 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005347 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005348 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005351 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005353 break;
5354 ++reduce;
5355 ++p;
5356 }
5357 }
5358
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005361 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005362 return FAIL;
5363 }
5364
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005366 if (!evaluate)
5367 {
5368 *arg = p + 1;
5369 return OK;
5370 }
5371
5372 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005374 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005375 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005376 if (str == NULL)
5377 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 rettv->v_type = VAR_STRING;
5379 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005380
Bram Moolenaar8c711452005-01-14 21:53:12 +00005381 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005382 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005386 break;
5387 ++p;
5388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005392 *arg = p + 1;
5393
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005394 return OK;
5395}
5396
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005397/*
5398 * Return the function name of the partial.
5399 */
5400 char_u *
5401partial_name(partial_T *pt)
5402{
5403 if (pt->pt_name != NULL)
5404 return pt->pt_name;
5405 return pt->pt_func->uf_name;
5406}
5407
Bram Moolenaarddecc252016-04-06 22:59:37 +02005408 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005409partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005410{
5411 int i;
5412
5413 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005414 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005415 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005416 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005417 if (pt->pt_name != NULL)
5418 {
5419 func_unref(pt->pt_name);
5420 vim_free(pt->pt_name);
5421 }
5422 else
5423 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005424 vim_free(pt);
5425}
5426
5427/*
5428 * Unreference a closure: decrement the reference count and free it when it
5429 * becomes zero.
5430 */
5431 void
5432partial_unref(partial_T *pt)
5433{
5434 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005435 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005436}
5437
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005438static int tv_equal_recurse_limit;
5439
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005440 static int
5441func_equal(
5442 typval_T *tv1,
5443 typval_T *tv2,
5444 int ic) /* ignore case */
5445{
5446 char_u *s1, *s2;
5447 dict_T *d1, *d2;
5448 int a1, a2;
5449 int i;
5450
5451 /* empty and NULL function name considered the same */
5452 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005453 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005454 if (s1 != NULL && *s1 == NUL)
5455 s1 = NULL;
5456 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005457 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005458 if (s2 != NULL && *s2 == NUL)
5459 s2 = NULL;
5460 if (s1 == NULL || s2 == NULL)
5461 {
5462 if (s1 != s2)
5463 return FALSE;
5464 }
5465 else if (STRCMP(s1, s2) != 0)
5466 return FALSE;
5467
5468 /* empty dict and NULL dict is different */
5469 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5470 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5471 if (d1 == NULL || d2 == NULL)
5472 {
5473 if (d1 != d2)
5474 return FALSE;
5475 }
5476 else if (!dict_equal(d1, d2, ic, TRUE))
5477 return FALSE;
5478
5479 /* empty list and no list considered the same */
5480 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5481 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5482 if (a1 != a2)
5483 return FALSE;
5484 for (i = 0; i < a1; ++i)
5485 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5486 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5487 return FALSE;
5488
5489 return TRUE;
5490}
5491
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005494 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005495 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005496 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498tv_equal(
5499 typval_T *tv1,
5500 typval_T *tv2,
5501 int ic, /* ignore case */
5502 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503{
5504 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005505 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005506 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005507 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005509 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005510 * recursiveness to a limit. We guess they are equal then.
5511 * A fixed limit has the problem of still taking an awful long time.
5512 * Reduce the limit every time running into it. That should work fine for
5513 * deeply linked structures that are not recursively linked and catch
5514 * recursiveness quickly. */
5515 if (!recursive)
5516 tv_equal_recurse_limit = 1000;
5517 if (recursive_cnt >= tv_equal_recurse_limit)
5518 {
5519 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005520 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005521 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005522
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005523 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5524 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005525 if ((tv1->v_type == VAR_FUNC
5526 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5527 && (tv2->v_type == VAR_FUNC
5528 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5529 {
5530 ++recursive_cnt;
5531 r = func_equal(tv1, tv2, ic);
5532 --recursive_cnt;
5533 return r;
5534 }
5535
5536 if (tv1->v_type != tv2->v_type)
5537 return FALSE;
5538
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005539 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005540 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005541 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005542 ++recursive_cnt;
5543 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5544 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005545 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005546
5547 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005548 ++recursive_cnt;
5549 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5550 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005551 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005552
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005553 case VAR_BLOB:
5554 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5555
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005556 case VAR_NUMBER:
5557 return tv1->vval.v_number == tv2->vval.v_number;
5558
5559 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005560 s1 = tv_get_string_buf(tv1, buf1);
5561 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005562 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005563
5564 case VAR_SPECIAL:
5565 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005566
5567 case VAR_FLOAT:
5568#ifdef FEAT_FLOAT
5569 return tv1->vval.v_float == tv2->vval.v_float;
5570#endif
5571 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005572#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005573 return tv1->vval.v_job == tv2->vval.v_job;
5574#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005575 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005576#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005577 return tv1->vval.v_channel == tv2->vval.v_channel;
5578#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005579 case VAR_FUNC:
5580 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005581 case VAR_UNKNOWN:
5582 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005583 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005584
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5586 * does not equal anything, not even itself. */
5587 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588}
5589
5590/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005591 * Return the next (unique) copy ID.
5592 * Used for serializing nested structures.
5593 */
5594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005595get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005596{
5597 current_copyID += COPYID_INC;
5598 return current_copyID;
5599}
5600
5601/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005602 * Garbage collection for lists and dictionaries.
5603 *
5604 * We use reference counts to be able to free most items right away when they
5605 * are no longer used. But for composite items it's possible that it becomes
5606 * unused while the reference count is > 0: When there is a recursive
5607 * reference. Example:
5608 * :let l = [1, 2, 3]
5609 * :let d = {9: l}
5610 * :let l[1] = d
5611 *
5612 * Since this is quite unusual we handle this with garbage collection: every
5613 * once in a while find out which lists and dicts are not referenced from any
5614 * variable.
5615 *
5616 * Here is a good reference text about garbage collection (refers to Python
5617 * but it applies to all reference-counting mechanisms):
5618 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005619 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005620
5621/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005622 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005623 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005624 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005625 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005626 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005627garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005628{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005629 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005630 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005631 buf_T *buf;
5632 win_T *wp;
5633 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005634 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005635 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005636
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005637 if (!testing)
5638 {
5639 /* Only do this once. */
5640 want_garbage_collect = FALSE;
5641 may_garbage_collect = FALSE;
5642 garbage_collect_at_exit = FALSE;
5643 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005644
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005645 /* We advance by two because we add one for items referenced through
5646 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005647 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005648
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005649 /*
5650 * 1. Go through all accessible variables and mark all lists and dicts
5651 * with copyID.
5652 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005653
5654 /* Don't free variables in the previous_funccal list unless they are only
5655 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005656 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005658
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005659 /* script-local variables */
5660 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005662
5663 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005664 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005665 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5666 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005667
5668 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005669 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005670 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5671 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005672 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005673 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5674 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005675#ifdef FEAT_TEXT_PROP
5676 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5677 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5678 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005679 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005680 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005681 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5682 NULL, NULL);
5683#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005684
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005685 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005686 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005687 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5688 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005689 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005690 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005691
5692 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005694
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005695 /* named functions (matters for closures) */
5696 abort = abort || set_ref_in_functions(copyID);
5697
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005698 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005700
Bram Moolenaard812df62008-11-09 12:46:09 +00005701 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005702 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005703
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005704 // callbacks in buffers
5705 abort = abort || set_ref_in_buffers(copyID);
5706
Bram Moolenaar1dced572012-04-05 16:54:08 +02005707#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005708 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005709#endif
5710
Bram Moolenaardb913952012-06-29 12:54:53 +02005711#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005712 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005713#endif
5714
5715#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005716 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005717#endif
5718
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005719#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005720 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005721 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005722#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005723#ifdef FEAT_NETBEANS_INTG
5724 abort = abort || set_ref_in_nb_channel(copyID);
5725#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005726
Bram Moolenaare3188e22016-05-31 21:13:04 +02005727#ifdef FEAT_TIMERS
5728 abort = abort || set_ref_in_timer(copyID);
5729#endif
5730
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005731#ifdef FEAT_QUICKFIX
5732 abort = abort || set_ref_in_quickfix(copyID);
5733#endif
5734
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005735#ifdef FEAT_TERMINAL
5736 abort = abort || set_ref_in_term(copyID);
5737#endif
5738
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005739#ifdef FEAT_TEXT_PROP
5740 abort = abort || set_ref_in_popups(copyID);
5741#endif
5742
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005743 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005744 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005745 /*
5746 * 2. Free lists and dictionaries that are not referenced.
5747 */
5748 did_free = free_unref_items(copyID);
5749
5750 /*
5751 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005752 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005753 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005754 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005755 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005756 else if (p_verbose > 0)
5757 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005758 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005759 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005760
5761 return did_free;
5762}
5763
5764/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005765 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005766 */
5767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005768free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005769{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005770 int did_free = FALSE;
5771
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005772 /* Let all "free" functions know that we are here. This means no
5773 * dictionaries, lists, channels or jobs are to be freed, because we will
5774 * do that here. */
5775 in_free_unref_items = TRUE;
5776
5777 /*
5778 * PASS 1: free the contents of the items. We don't free the items
5779 * themselves yet, so that it is possible to decrement refcount counters
5780 */
5781
Bram Moolenaarcd524592016-07-17 14:57:05 +02005782 /* Go through the list of dicts and free items without the copyID. */
5783 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005784
Bram Moolenaarda861d62016-07-17 15:46:27 +02005785 /* Go through the list of lists and free items without the copyID. */
5786 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005787
5788#ifdef FEAT_JOB_CHANNEL
5789 /* Go through the list of jobs and free items without the copyID. This
5790 * must happen before doing channels, because jobs refer to channels, but
5791 * the reference from the channel to the job isn't tracked. */
5792 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5793
5794 /* Go through the list of channels and free items without the copyID. */
5795 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5796#endif
5797
5798 /*
5799 * PASS 2: free the items themselves.
5800 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005801 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005802 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005803
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005804#ifdef FEAT_JOB_CHANNEL
5805 /* Go through the list of jobs and free items without the copyID. This
5806 * must happen before doing channels, because jobs refer to channels, but
5807 * the reference from the channel to the job isn't tracked. */
5808 free_unused_jobs(copyID, COPYID_MASK);
5809
5810 /* Go through the list of channels and free items without the copyID. */
5811 free_unused_channels(copyID, COPYID_MASK);
5812#endif
5813
5814 in_free_unref_items = FALSE;
5815
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005816 return did_free;
5817}
5818
5819/*
5820 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005821 * "list_stack" is used to add lists to be marked. Can be NULL.
5822 *
5823 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005827{
5828 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005829 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005830 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005831 hashtab_T *cur_ht;
5832 ht_stack_T *ht_stack = NULL;
5833 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005834
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005835 cur_ht = ht;
5836 for (;;)
5837 {
5838 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005839 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005840 /* Mark each item in the hashtab. If the item contains a hashtab
5841 * it is added to ht_stack, if it contains a list it is added to
5842 * list_stack. */
5843 todo = (int)cur_ht->ht_used;
5844 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5845 if (!HASHITEM_EMPTY(hi))
5846 {
5847 --todo;
5848 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5849 &ht_stack, list_stack);
5850 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005851 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005852
5853 if (ht_stack == NULL)
5854 break;
5855
5856 /* take an item from the stack */
5857 cur_ht = ht_stack->ht;
5858 tempitem = ht_stack;
5859 ht_stack = ht_stack->prev;
5860 free(tempitem);
5861 }
5862
5863 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005864}
5865
5866/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005867 * Mark a dict and its items with "copyID".
5868 * Returns TRUE if setting references failed somehow.
5869 */
5870 int
5871set_ref_in_dict(dict_T *d, int copyID)
5872{
5873 if (d != NULL && d->dv_copyID != copyID)
5874 {
5875 d->dv_copyID = copyID;
5876 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5877 }
5878 return FALSE;
5879}
5880
5881/*
5882 * Mark a list and its items with "copyID".
5883 * Returns TRUE if setting references failed somehow.
5884 */
5885 int
5886set_ref_in_list(list_T *ll, int copyID)
5887{
5888 if (ll != NULL && ll->lv_copyID != copyID)
5889 {
5890 ll->lv_copyID = copyID;
5891 return set_ref_in_list_items(ll, copyID, NULL);
5892 }
5893 return FALSE;
5894}
5895
5896/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005897 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005898 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5899 *
5900 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005901 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005902 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005903set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005904{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005905 listitem_T *li;
5906 int abort = FALSE;
5907 list_T *cur_l;
5908 list_stack_T *list_stack = NULL;
5909 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005910
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005911 cur_l = l;
5912 for (;;)
5913 {
5914 if (!abort)
5915 /* Mark each item in the list. If the item contains a hashtab
5916 * it is added to ht_stack, if it contains a list it is added to
5917 * list_stack. */
5918 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5919 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5920 ht_stack, &list_stack);
5921 if (list_stack == NULL)
5922 break;
5923
5924 /* take an item from the stack */
5925 cur_l = list_stack->list;
5926 tempitem = list_stack;
5927 list_stack = list_stack->prev;
5928 free(tempitem);
5929 }
5930
5931 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005932}
5933
5934/*
5935 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005936 * "list_stack" is used to add lists to be marked. Can be NULL.
5937 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5938 *
5939 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005940 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942set_ref_in_item(
5943 typval_T *tv,
5944 int copyID,
5945 ht_stack_T **ht_stack,
5946 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005947{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005948 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005949
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005950 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005952 dict_T *dd = tv->vval.v_dict;
5953
Bram Moolenaara03f2332016-02-06 18:09:59 +01005954 if (dd != NULL && dd->dv_copyID != copyID)
5955 {
5956 /* Didn't see this dict yet. */
5957 dd->dv_copyID = copyID;
5958 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005959 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005960 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5961 }
5962 else
5963 {
5964 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5965 if (newitem == NULL)
5966 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005967 else
5968 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005969 newitem->ht = &dd->dv_hashtab;
5970 newitem->prev = *ht_stack;
5971 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005972 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005973 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005974 }
5975 }
5976 else if (tv->v_type == VAR_LIST)
5977 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005978 list_T *ll = tv->vval.v_list;
5979
Bram Moolenaara03f2332016-02-06 18:09:59 +01005980 if (ll != NULL && ll->lv_copyID != copyID)
5981 {
5982 /* Didn't see this list yet. */
5983 ll->lv_copyID = copyID;
5984 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005985 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005986 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005987 }
5988 else
5989 {
5990 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005991 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005992 if (newitem == NULL)
5993 abort = TRUE;
5994 else
5995 {
5996 newitem->list = ll;
5997 newitem->prev = *list_stack;
5998 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005999 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006000 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006001 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006002 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006003 else if (tv->v_type == VAR_FUNC)
6004 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006005 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006006 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006007 else if (tv->v_type == VAR_PARTIAL)
6008 {
6009 partial_T *pt = tv->vval.v_partial;
6010 int i;
6011
6012 /* A partial does not have a copyID, because it cannot contain itself.
6013 */
6014 if (pt != NULL)
6015 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006016 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006017
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006018 if (pt->pt_dict != NULL)
6019 {
6020 typval_T dtv;
6021
6022 dtv.v_type = VAR_DICT;
6023 dtv.vval.v_dict = pt->pt_dict;
6024 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6025 }
6026
6027 for (i = 0; i < pt->pt_argc; ++i)
6028 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6029 ht_stack, list_stack);
6030 }
6031 }
6032#ifdef FEAT_JOB_CHANNEL
6033 else if (tv->v_type == VAR_JOB)
6034 {
6035 job_T *job = tv->vval.v_job;
6036 typval_T dtv;
6037
6038 if (job != NULL && job->jv_copyID != copyID)
6039 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006040 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006041 if (job->jv_channel != NULL)
6042 {
6043 dtv.v_type = VAR_CHANNEL;
6044 dtv.vval.v_channel = job->jv_channel;
6045 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6046 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006047 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006048 {
6049 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006050 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006051 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6052 }
6053 }
6054 }
6055 else if (tv->v_type == VAR_CHANNEL)
6056 {
6057 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006058 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006059 typval_T dtv;
6060 jsonq_T *jq;
6061 cbq_T *cq;
6062
6063 if (ch != NULL && ch->ch_copyID != copyID)
6064 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006065 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006066 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006067 {
6068 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6069 jq = jq->jq_next)
6070 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6071 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6072 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006073 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006074 {
6075 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006076 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006077 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6078 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006079 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 {
6081 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006082 dtv.vval.v_partial =
6083 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006084 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6085 }
6086 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006087 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006088 {
6089 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006090 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006091 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6092 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006093 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006094 {
6095 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006096 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6098 }
6099 }
6100 }
6101#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006102 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006103}
6104
Bram Moolenaar17a13432016-01-24 14:22:10 +01006105 static char *
6106get_var_special_name(int nr)
6107{
6108 switch (nr)
6109 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006110 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006111 case VVAL_TRUE: return "v:true";
6112 case VVAL_NONE: return "v:none";
6113 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006114 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006115 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006116 return "42";
6117}
6118
Bram Moolenaar8c711452005-01-14 21:53:12 +00006119/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 * Return a string with the string representation of a variable.
6121 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006122 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006123 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006124 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6125 * stings as "string()", otherwise does not put quotes around strings, as
6126 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006127 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6128 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006129 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006131 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006132echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006133 typval_T *tv,
6134 char_u **tofree,
6135 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006136 int copyID,
6137 int echo_style,
6138 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006139 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006141 static int recurse = 0;
6142 char_u *r = NULL;
6143
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006145 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006146 if (!did_echo_string_emsg)
6147 {
6148 /* Only give this message once for a recursive call to avoid
6149 * flooding the user with errors. And stop iterating over lists
6150 * and dicts. */
6151 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006152 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006154 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006155 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006156 }
6157 ++recurse;
6158
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 switch (tv->v_type)
6160 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006161 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006162 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006163 {
6164 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006165 r = tv->vval.v_string;
6166 if (r == NULL)
6167 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006168 }
6169 else
6170 {
6171 *tofree = string_quote(tv->vval.v_string, FALSE);
6172 r = *tofree;
6173 }
6174 break;
6175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006177 if (echo_style)
6178 {
6179 *tofree = NULL;
6180 r = tv->vval.v_string;
6181 }
6182 else
6183 {
6184 *tofree = string_quote(tv->vval.v_string, TRUE);
6185 r = *tofree;
6186 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006187 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006188
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006189 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006190 {
6191 partial_T *pt = tv->vval.v_partial;
6192 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006193 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006194 garray_T ga;
6195 int i;
6196 char_u *tf;
6197
6198 ga_init2(&ga, 1, 100);
6199 ga_concat(&ga, (char_u *)"function(");
6200 if (fname != NULL)
6201 {
6202 ga_concat(&ga, fname);
6203 vim_free(fname);
6204 }
6205 if (pt != NULL && pt->pt_argc > 0)
6206 {
6207 ga_concat(&ga, (char_u *)", [");
6208 for (i = 0; i < pt->pt_argc; ++i)
6209 {
6210 if (i > 0)
6211 ga_concat(&ga, (char_u *)", ");
6212 ga_concat(&ga,
6213 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6214 vim_free(tf);
6215 }
6216 ga_concat(&ga, (char_u *)"]");
6217 }
6218 if (pt != NULL && pt->pt_dict != NULL)
6219 {
6220 typval_T dtv;
6221
6222 ga_concat(&ga, (char_u *)", ");
6223 dtv.v_type = VAR_DICT;
6224 dtv.vval.v_dict = pt->pt_dict;
6225 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6226 vim_free(tf);
6227 }
6228 ga_concat(&ga, (char_u *)")");
6229
6230 *tofree = ga.ga_data;
6231 r = *tofree;
6232 break;
6233 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006234
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006235 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006236 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006237 break;
6238
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006240 if (tv->vval.v_list == NULL)
6241 {
6242 *tofree = NULL;
6243 r = NULL;
6244 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006245 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6246 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006247 {
6248 *tofree = NULL;
6249 r = (char_u *)"[...]";
6250 }
6251 else
6252 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006253 int old_copyID = tv->vval.v_list->lv_copyID;
6254
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006255 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006256 *tofree = list2string(tv, copyID, restore_copyID);
6257 if (restore_copyID)
6258 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006259 r = *tofree;
6260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006261 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006262
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006264 if (tv->vval.v_dict == NULL)
6265 {
6266 *tofree = NULL;
6267 r = NULL;
6268 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006269 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6270 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006271 {
6272 *tofree = NULL;
6273 r = (char_u *)"{...}";
6274 }
6275 else
6276 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006277 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006278 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006279 *tofree = dict2string(tv, copyID, restore_copyID);
6280 if (restore_copyID)
6281 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006282 r = *tofree;
6283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006284 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006286 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006287 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006288 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006289 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006290 break;
6291
Bram Moolenaar835dc632016-02-07 14:27:38 +01006292 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006293 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006295 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006296 if (composite_val)
6297 {
6298 *tofree = string_quote(r, FALSE);
6299 r = *tofree;
6300 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006302
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006303 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006304#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006305 *tofree = NULL;
6306 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6307 r = numbuf;
6308 break;
6309#endif
6310
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006311 case VAR_SPECIAL:
6312 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006313 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006314 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006315 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316
Bram Moolenaar8502c702014-06-17 12:51:16 +02006317 if (--recurse == 0)
6318 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006319 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006320}
6321
6322/*
6323 * Return a string with the string representation of a variable.
6324 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6325 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006326 * Does not put quotes around strings, as ":echo" displays values.
6327 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6328 * May return NULL.
6329 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006330 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006331echo_string(
6332 typval_T *tv,
6333 char_u **tofree,
6334 char_u *numbuf,
6335 int copyID)
6336{
6337 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6338}
6339
6340/*
6341 * Return a string with the string representation of a variable.
6342 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6343 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006344 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006345 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006346 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348tv2string(
6349 typval_T *tv,
6350 char_u **tofree,
6351 char_u *numbuf,
6352 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006353{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006354 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355}
6356
6357/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 * Return string "str" in ' quotes, doubling ' characters.
6359 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006360 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006361 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006363string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006364{
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006366 char_u *p, *r, *s;
6367
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 len = (function ? 13 : 3);
6369 if (str != NULL)
6370 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006371 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006372 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 if (*p == '\'')
6374 ++len;
6375 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006376 s = r = alloc(len);
6377 if (r != NULL)
6378 {
6379 if (function)
6380 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006382 r += 10;
6383 }
6384 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006385 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 if (str != NULL)
6387 for (p = str; *p != NUL; )
6388 {
6389 if (*p == '\'')
6390 *r++ = '\'';
6391 MB_COPY_CHAR(p, r);
6392 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006393 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006394 if (function)
6395 *r++ = ')';
6396 *r++ = NUL;
6397 }
6398 return s;
6399}
6400
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006401#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006402/*
6403 * Convert the string "text" to a floating point number.
6404 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6405 * this always uses a decimal point.
6406 * Returns the length of the text that was consumed.
6407 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409string2float(
6410 char_u *text,
6411 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006412{
6413 char *s = (char *)text;
6414 float_T f;
6415
Bram Moolenaar62473612017-01-08 19:25:40 +01006416 /* MS-Windows does not deal with "inf" and "nan" properly. */
6417 if (STRNICMP(text, "inf", 3) == 0)
6418 {
6419 *value = INFINITY;
6420 return 3;
6421 }
6422 if (STRNICMP(text, "-inf", 3) == 0)
6423 {
6424 *value = -INFINITY;
6425 return 4;
6426 }
6427 if (STRNICMP(text, "nan", 3) == 0)
6428 {
6429 *value = NAN;
6430 return 3;
6431 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006432 f = strtod(s, &s);
6433 *value = f;
6434 return (int)((char_u *)s - text);
6435}
6436#endif
6437
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 * Get the value of an environment variable.
6440 * "arg" is pointing to the '$'. It is advanced to after the name.
6441 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006442 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 */
6444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006445get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 char_u *string = NULL;
6448 int len;
6449 int cc;
6450 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006451 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 ++*arg;
6454 name = *arg;
6455 len = get_env_len(arg);
6456 if (evaluate)
6457 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006458 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006459 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006460
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006461 cc = name[len];
6462 name[len] = NUL;
6463 /* first try vim_getenv(), fast for normal environment vars */
6464 string = vim_getenv(name, &mustfree);
6465 if (string != NULL && *string != NUL)
6466 {
6467 if (!mustfree)
6468 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006470 else
6471 {
6472 if (mustfree)
6473 vim_free(string);
6474
6475 /* next try expanding things like $VIM and ${HOME} */
6476 string = expand_env_save(name - 1);
6477 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006478 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006479 }
6480 name[len] = cc;
6481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006482 rettv->v_type = VAR_STRING;
6483 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485
6486 return OK;
6487}
6488
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006489
6490
6491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006493 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496var2fpos(
6497 typval_T *varp,
6498 int dollar_lnum, /* TRUE when $ is last line */
6499 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006501 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006503 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaara5525202006-03-02 22:52:09 +00006505 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006506 if (varp->v_type == VAR_LIST)
6507 {
6508 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006509 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006510 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006511 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006512
6513 l = varp->vval.v_list;
6514 if (l == NULL)
6515 return NULL;
6516
6517 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006518 pos.lnum = list_find_nr(l, 0L, &error);
6519 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006520 return NULL; /* invalid line number */
6521
6522 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006523 pos.col = list_find_nr(l, 1L, &error);
6524 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006525 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006526 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006527
6528 /* We accept "$" for the column number: last column. */
6529 li = list_find(l, 1L);
6530 if (li != NULL && li->li_tv.v_type == VAR_STRING
6531 && li->li_tv.vval.v_string != NULL
6532 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6533 pos.col = len + 1;
6534
Bram Moolenaara5525202006-03-02 22:52:09 +00006535 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006536 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006537 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006538 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006539
Bram Moolenaara5525202006-03-02 22:52:09 +00006540 /* Get the virtual offset. Defaults to zero. */
6541 pos.coladd = list_find_nr(l, 2L, &error);
6542 if (error)
6543 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006544
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006545 return &pos;
6546 }
6547
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006548 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006549 if (name == NULL)
6550 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006551 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006553 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6554 {
6555 if (VIsual_active)
6556 return &VIsual;
6557 return &curwin->w_cursor;
6558 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006559 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006561 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6563 return NULL;
6564 return pp;
6565 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006566
Bram Moolenaara5525202006-03-02 22:52:09 +00006567 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006568
Bram Moolenaar477933c2007-07-17 14:32:23 +00006569 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006570 {
6571 pos.col = 0;
6572 if (name[1] == '0') /* "w0": first visible line */
6573 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006575 /* In silent Ex mode topline is zero, but that's not a valid line
6576 * number; use one instead. */
6577 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006578 return &pos;
6579 }
6580 else if (name[1] == '$') /* "w$": last visible line */
6581 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006582 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006583 /* In silent Ex mode botline is zero, return zero then. */
6584 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006585 return &pos;
6586 }
6587 }
6588 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006590 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {
6592 pos.lnum = curbuf->b_ml.ml_line_count;
6593 pos.col = 0;
6594 }
6595 else
6596 {
6597 pos.lnum = curwin->w_cursor.lnum;
6598 pos.col = (colnr_T)STRLEN(ml_get_curline());
6599 }
6600 return &pos;
6601 }
6602 return NULL;
6603}
6604
6605/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006606 * Convert list in "arg" into a position and optional file number.
6607 * When "fnump" is NULL there is no file number, only 3 items.
6608 * Note that the column is passed on as-is, the caller may want to decrement
6609 * it to use 1 for the first column.
6610 * Return FAIL when conversion is not possible, doesn't check the position for
6611 * validity.
6612 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614list2fpos(
6615 typval_T *arg,
6616 pos_T *posp,
6617 int *fnump,
6618 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006619{
6620 list_T *l = arg->vval.v_list;
6621 long i = 0;
6622 long n;
6623
Bram Moolenaar493c1782014-05-28 14:34:46 +02006624 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6625 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006626 if (arg->v_type != VAR_LIST
6627 || l == NULL
6628 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006629 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006630 return FAIL;
6631
6632 if (fnump != NULL)
6633 {
6634 n = list_find_nr(l, i++, NULL); /* fnum */
6635 if (n < 0)
6636 return FAIL;
6637 if (n == 0)
6638 n = curbuf->b_fnum; /* current buffer */
6639 *fnump = n;
6640 }
6641
6642 n = list_find_nr(l, i++, NULL); /* lnum */
6643 if (n < 0)
6644 return FAIL;
6645 posp->lnum = n;
6646
6647 n = list_find_nr(l, i++, NULL); /* col */
6648 if (n < 0)
6649 return FAIL;
6650 posp->col = n;
6651
Bram Moolenaar493c1782014-05-28 14:34:46 +02006652 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006653 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006654 posp->coladd = 0;
6655 else
6656 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006657
Bram Moolenaar493c1782014-05-28 14:34:46 +02006658 if (curswantp != NULL)
6659 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6660
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006661 return OK;
6662}
6663
6664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 * Get the length of an environment variable name.
6666 * Advance "arg" to the first character after the name.
6667 * Return 0 for error.
6668 */
6669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 char_u *p;
6673 int len;
6674
6675 for (p = *arg; vim_isIDc(*p); ++p)
6676 ;
6677 if (p == *arg) /* no name found */
6678 return 0;
6679
6680 len = (int)(p - *arg);
6681 *arg = p;
6682 return len;
6683}
6684
6685/*
6686 * Get the length of the name of a function or internal variable.
6687 * "arg" is advanced to the first non-white character after the name.
6688 * Return 0 if something is wrong.
6689 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006691get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 char_u *p;
6694 int len;
6695
6696 /* Find the end of the name. */
6697 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006698 {
6699 if (*p == ':')
6700 {
6701 /* "s:" is start of "s:var", but "n:" is not and can be used in
6702 * slice "[n:]". Also "xx:" is not a namespace. */
6703 len = (int)(p - *arg);
6704 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6705 || len > 1)
6706 break;
6707 }
6708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (p == *arg) /* no name found */
6710 return 0;
6711
6712 len = (int)(p - *arg);
6713 *arg = skipwhite(p);
6714
6715 return len;
6716}
6717
6718/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006719 * Get the length of the name of a variable or function.
6720 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006722 * Return -1 if curly braces expansion failed.
6723 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 * If the name contains 'magic' {}'s, expand them and return the
6725 * expanded name in an allocated string via 'alias' - caller must free.
6726 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006728get_name_len(
6729 char_u **arg,
6730 char_u **alias,
6731 int evaluate,
6732 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 char_u *p;
6736 char_u *expr_start;
6737 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
6739 *alias = NULL; /* default to no alias */
6740
6741 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6742 && (*arg)[2] == (int)KE_SNR)
6743 {
6744 /* hard coded <SNR>, already translated */
6745 *arg += 3;
6746 return get_id_len(arg) + 3;
6747 }
6748 len = eval_fname_script(*arg);
6749 if (len > 0)
6750 {
6751 /* literal "<SID>", "s:" or "<SNR>" */
6752 *arg += len;
6753 }
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006756 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006758 p = find_name_end(*arg, &expr_start, &expr_end,
6759 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (expr_start != NULL)
6761 {
6762 char_u *temp_string;
6763
6764 if (!evaluate)
6765 {
6766 len += (int)(p - *arg);
6767 *arg = skipwhite(p);
6768 return len;
6769 }
6770
6771 /*
6772 * Include any <SID> etc in the expanded string:
6773 * Thus the -len here.
6774 */
6775 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6776 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006777 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 *alias = temp_string;
6779 *arg = skipwhite(p);
6780 return (int)STRLEN(temp_string);
6781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006784 // Only give an error when there is something, otherwise it will be
6785 // reported at a higher level.
6786 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006787 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
6789 return len;
6790}
6791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006792/*
6793 * Find the end of a variable or function name, taking care of magic braces.
6794 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6795 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006796 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006797 * Return a pointer to just after the name. Equal to "arg" if there is no
6798 * valid name.
6799 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006801find_name_end(
6802 char_u *arg,
6803 char_u **expr_start,
6804 char_u **expr_end,
6805 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 int mb_nest = 0;
6808 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006810 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 *expr_start = NULL;
6815 *expr_end = NULL;
6816 }
6817
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006818 /* Quick check for valid starting character. */
6819 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6820 return arg;
6821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006822 for (p = arg; *p != NUL
6823 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006825 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006827 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006828 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006829 if (*p == '\'')
6830 {
6831 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006832 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006833 ;
6834 if (*p == NUL)
6835 break;
6836 }
6837 else if (*p == '"')
6838 {
6839 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006840 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006841 if (*p == '\\' && p[1] != NUL)
6842 ++p;
6843 if (*p == NUL)
6844 break;
6845 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006846 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6847 {
6848 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006849 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006850 len = (int)(p - arg);
6851 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006852 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006853 break;
6854 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006856 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858 if (*p == '[')
6859 ++br_nest;
6860 else if (*p == ']')
6861 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006864 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006866 if (*p == '{')
6867 {
6868 mb_nest++;
6869 if (expr_start != NULL && *expr_start == NULL)
6870 *expr_start = p;
6871 }
6872 else if (*p == '}')
6873 {
6874 mb_nest--;
6875 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6876 *expr_end = p;
6877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
6880
6881 return p;
6882}
6883
6884/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006885 * Expands out the 'magic' {}'s in a variable/function name.
6886 * Note that this can call itself recursively, to deal with
6887 * constructs like foo{bar}{baz}{bam}
6888 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6889 * "in_start" ^
6890 * "expr_start" ^
6891 * "expr_end" ^
6892 * "in_end" ^
6893 *
6894 * Returns a new allocated string, which the caller must free.
6895 * Returns NULL for failure.
6896 */
6897 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898make_expanded_name(
6899 char_u *in_start,
6900 char_u *expr_start,
6901 char_u *expr_end,
6902 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006903{
6904 char_u c1;
6905 char_u *retval = NULL;
6906 char_u *temp_result;
6907 char_u *nextcmd = NULL;
6908
6909 if (expr_end == NULL || in_end == NULL)
6910 return NULL;
6911 *expr_start = NUL;
6912 *expr_end = NUL;
6913 c1 = *in_end;
6914 *in_end = NUL;
6915
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006916 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006917 if (temp_result != NULL && nextcmd == NULL)
6918 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006919 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6920 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006921 if (retval != NULL)
6922 {
6923 STRCPY(retval, in_start);
6924 STRCAT(retval, temp_result);
6925 STRCAT(retval, expr_end + 1);
6926 }
6927 }
6928 vim_free(temp_result);
6929
6930 *in_end = c1; /* put char back for error messages */
6931 *expr_start = '{';
6932 *expr_end = '}';
6933
6934 if (retval != NULL)
6935 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006936 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006937 if (expr_start != NULL)
6938 {
6939 /* Further expansion! */
6940 temp_result = make_expanded_name(retval, expr_start,
6941 expr_end, temp_result);
6942 vim_free(retval);
6943 retval = temp_result;
6944 }
6945 }
6946
6947 return retval;
6948}
6949
6950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006955eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006957 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6958}
6959
6960/*
6961 * Return TRUE if character "c" can be used as the first character in a
6962 * variable or function name (excluding '{' and '}').
6963 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006966{
6967 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Set number v: variable to "val".
6972 */
6973 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006974set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977}
6978
6979/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006980 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006982 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006983get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986}
6987
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006988/*
6989 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006990 * If the String variable has never been set, return an empty string.
6991 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006992 */
6993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006994get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006995{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006996 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006997}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007000 * Get List v: variable value. Caller must take care of reference count when
7001 * needed.
7002 */
7003 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007004get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007005{
7006 return vimvars[idx].vv_list;
7007}
7008
7009/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007010 * Get Dict v: variable value. Caller must take care of reference count when
7011 * needed.
7012 */
7013 dict_T *
7014get_vim_var_dict(int idx)
7015{
7016 return vimvars[idx].vv_dict;
7017}
7018
7019/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007020 * Set v:char to character "c".
7021 */
7022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007024{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007025 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007026
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007027 if (has_mbyte)
7028 buf[(*mb_char2bytes)(c, buf)] = NUL;
7029 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007030 {
7031 buf[0] = c;
7032 buf[1] = NUL;
7033 }
7034 set_vim_var_string(VV_CHAR, buf, -1);
7035}
7036
7037/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007038 * Set v:count to "count" and v:count1 to "count1".
7039 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 */
7041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042set_vcount(
7043 long count,
7044 long count1,
7045 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007047 if (set_prevcount)
7048 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007049 vimvars[VV_COUNT].vv_nr = count;
7050 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051}
7052
7053/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007054 * Save variables that might be changed as a side effect. Used when executing
7055 * a timer callback.
7056 */
7057 void
7058save_vimvars(vimvars_save_T *vvsave)
7059{
7060 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7061 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7062 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7063}
7064
7065/*
7066 * Restore variables saved by save_vimvars().
7067 */
7068 void
7069restore_vimvars(vimvars_save_T *vvsave)
7070{
7071 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7072 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7073 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7074}
7075
7076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 * Set string v: variable to a copy of "val".
7078 */
7079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007080set_vim_var_string(
7081 int idx,
7082 char_u *val,
7083 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
Bram Moolenaara542c682016-01-31 16:28:04 +01007085 clear_tv(&vimvars[idx].vv_di.di_tv);
7086 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007088 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093}
7094
7095/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007096 * Set List v: variable to "val".
7097 */
7098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007099set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007100{
Bram Moolenaara542c682016-01-31 16:28:04 +01007101 clear_tv(&vimvars[idx].vv_di.di_tv);
7102 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007103 vimvars[idx].vv_list = val;
7104 if (val != NULL)
7105 ++val->lv_refcount;
7106}
7107
7108/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007109 * Set Dictionary v: variable to "val".
7110 */
7111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007113{
Bram Moolenaara542c682016-01-31 16:28:04 +01007114 clear_tv(&vimvars[idx].vv_di.di_tv);
7115 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007116 vimvars[idx].vv_dict = val;
7117 if (val != NULL)
7118 {
7119 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007120 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007121 }
7122}
7123
7124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 * Set v:register if needed.
7126 */
7127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007128set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 char_u regname;
7131
7132 if (c == 0 || c == ' ')
7133 regname = '"';
7134 else
7135 regname = c;
7136 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 set_vim_var_string(VV_REG, &regname, 1);
7139}
7140
7141/*
7142 * Get or set v:exception. If "oldval" == NULL, return the current value.
7143 * Otherwise, restore the value to "oldval" and return NULL.
7144 * Must always be called in pairs to save and restore v:exception! Does not
7145 * take care of memory allocations.
7146 */
7147 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007148v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 return NULL;
7155}
7156
7157/*
7158 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7159 * Otherwise, restore the value to "oldval" and return NULL.
7160 * Must always be called in pairs to save and restore v:throwpoint! Does not
7161 * take care of memory allocations.
7162 */
7163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007164v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 return NULL;
7171}
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * Set v:cmdarg.
7175 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7176 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7177 * Must always be called in pairs!
7178 */
7179 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 char_u *oldval;
7183 char_u *newval;
7184 unsigned len;
7185
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007187 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007189 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007191 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007194 if (eap->force_bin == FORCE_BIN)
7195 len = 6;
7196 else if (eap->force_bin == FORCE_NOBIN)
7197 len = 8;
7198 else
7199 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007200
7201 if (eap->read_edit)
7202 len += 7;
7203
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007204 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007205 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007206 if (eap->force_enc != 0)
7207 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007208 if (eap->bad_char != 0)
7209 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007210
7211 newval = alloc(len + 1);
7212 if (newval == NULL)
7213 return NULL;
7214
7215 if (eap->force_bin == FORCE_BIN)
7216 sprintf((char *)newval, " ++bin");
7217 else if (eap->force_bin == FORCE_NOBIN)
7218 sprintf((char *)newval, " ++nobin");
7219 else
7220 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007221
7222 if (eap->read_edit)
7223 STRCAT(newval, " ++edit");
7224
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007225 if (eap->force_ff != 0)
7226 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007227 eap->force_ff == 'u' ? "unix"
7228 : eap->force_ff == 'd' ? "dos"
7229 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007230 if (eap->force_enc != 0)
7231 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7232 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007233 if (eap->bad_char == BAD_KEEP)
7234 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7235 else if (eap->bad_char == BAD_DROP)
7236 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7237 else if (eap->bad_char != 0)
7238 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007239 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007240 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243/*
7244 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007245 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248get_var_tv(
7249 char_u *name,
7250 int len, /* length of "name" */
7251 typval_T *rettv, /* NULL when only checking existence */
7252 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7253 int verbose, /* may give error message */
7254 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
7256 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 /* truncate the name, so that we can use strcmp() */
7262 cc = name[len];
7263 name[len] = NUL;
7264
7265 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 * Check for user-defined variables.
7267 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007268 v = find_var(name, NULL, no_autoload);
7269 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007271 tv = &v->di_tv;
7272 if (dip != NULL)
7273 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007278 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007279 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 ret = FAIL;
7281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007282 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284
7285 name[len] = cc;
7286
7287 return ret;
7288}
7289
7290/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007291 * Check if variable "name[len]" is a local variable or an argument.
7292 * If so, "*eval_lavars_used" is set to TRUE.
7293 */
7294 static void
7295check_vars(char_u *name, int len)
7296{
7297 int cc;
7298 char_u *varname;
7299 hashtab_T *ht;
7300
7301 if (eval_lavars_used == NULL)
7302 return;
7303
7304 /* truncate the name, so that we can use strcmp() */
7305 cc = name[len];
7306 name[len] = NUL;
7307
7308 ht = find_var_ht(name, &varname);
7309 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7310 {
7311 if (find_var(name, NULL, TRUE) != NULL)
7312 *eval_lavars_used = TRUE;
7313 }
7314
7315 name[len] = cc;
7316}
7317
7318/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007319 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7320 * Also handle function call with Funcref variable: func(expr)
7321 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7322 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007324handle_subscript(
7325 char_u **arg,
7326 typval_T *rettv,
7327 int evaluate, /* do more than finding the end */
7328 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007329{
7330 int ret = OK;
7331 dict_T *selfdict = NULL;
7332 char_u *s;
7333 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007334 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007335
7336 while (ret == OK
7337 && (**arg == '['
7338 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007339 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7340 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007341 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007342 {
7343 if (**arg == '(')
7344 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007345 partial_T *pt = NULL;
7346
Bram Moolenaard9fba312005-06-26 22:34:35 +00007347 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007348 if (evaluate)
7349 {
7350 functv = *rettv;
7351 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007352
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007353 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007354 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007355 {
7356 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007357 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007358 }
7359 else
7360 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007361 }
7362 else
7363 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007364 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007365 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007366 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007367
7368 /* Clear the funcref afterwards, so that deleting it while
7369 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007370 if (evaluate)
7371 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007372
7373 /* Stop the expression evaluation when immediately aborting on
7374 * error, or when an interrupt occurred or an exception was thrown
7375 * but not caught. */
7376 if (aborting())
7377 {
7378 if (ret == OK)
7379 clear_tv(rettv);
7380 ret = FAIL;
7381 }
7382 dict_unref(selfdict);
7383 selfdict = NULL;
7384 }
7385 else /* **arg == '[' || **arg == '.' */
7386 {
7387 dict_unref(selfdict);
7388 if (rettv->v_type == VAR_DICT)
7389 {
7390 selfdict = rettv->vval.v_dict;
7391 if (selfdict != NULL)
7392 ++selfdict->dv_refcount;
7393 }
7394 else
7395 selfdict = NULL;
7396 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7397 {
7398 clear_tv(rettv);
7399 ret = FAIL;
7400 }
7401 }
7402 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007403
Bram Moolenaar1d429612016-05-24 15:44:17 +02007404 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7405 * Don't do this when "Func" is already a partial that was bound
7406 * explicitly (pt_auto is FALSE). */
7407 if (selfdict != NULL
7408 && (rettv->v_type == VAR_FUNC
7409 || (rettv->v_type == VAR_PARTIAL
7410 && (rettv->vval.v_partial->pt_auto
7411 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007412 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007414 dict_unref(selfdict);
7415 return ret;
7416}
7417
7418/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007419 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420 * value).
7421 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007422 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007424{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007425 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007426}
7427
7428/*
7429 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 * The string "s" must have been allocated, it is consumed.
7431 * Return NULL for out of memory, the variable otherwise.
7432 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007434alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007438 rettv = alloc_tv();
7439 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 rettv->v_type = VAR_STRING;
7442 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 }
7444 else
7445 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447}
7448
7449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007453free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 if (varp != NULL)
7456 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007457 switch (varp->v_type)
7458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007460 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007461 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007462 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007463 vim_free(varp->vval.v_string);
7464 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007465 case VAR_PARTIAL:
7466 partial_unref(varp->vval.v_partial);
7467 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007468 case VAR_BLOB:
7469 blob_unref(varp->vval.v_blob);
7470 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 case VAR_LIST:
7472 list_unref(varp->vval.v_list);
7473 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007474 case VAR_DICT:
7475 dict_unref(varp->vval.v_dict);
7476 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007477 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007479 job_unref(varp->vval.v_job);
7480 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007481#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007482 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007483#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007484 channel_unref(varp->vval.v_channel);
7485 break;
7486#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007487 case VAR_NUMBER:
7488 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007489 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007490 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007491 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 vim_free(varp);
7494 }
7495}
7496
7497/*
7498 * Free the memory for a variable value and set the value to NULL or 0.
7499 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007501clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
7503 if (varp != NULL)
7504 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007505 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007507 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007508 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007509 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007510 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007511 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007512 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007513 case VAR_PARTIAL:
7514 partial_unref(varp->vval.v_partial);
7515 varp->vval.v_partial = NULL;
7516 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007517 case VAR_BLOB:
7518 blob_unref(varp->vval.v_blob);
7519 varp->vval.v_blob = NULL;
7520 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007521 case VAR_LIST:
7522 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 case VAR_DICT:
7526 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007529 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007530 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007531 varp->vval.v_number = 0;
7532 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007533 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007534#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007535 varp->vval.v_float = 0.0;
7536 break;
7537#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007538 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007539#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007540 job_unref(varp->vval.v_job);
7541 varp->vval.v_job = NULL;
7542#endif
7543 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007544 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007546 channel_unref(varp->vval.v_channel);
7547 varp->vval.v_channel = NULL;
7548#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549 case VAR_UNKNOWN:
7550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 }
7554}
7555
7556/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 * Set the value of a variable to NULL without freeing items.
7558 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561{
7562 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564}
7565
7566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 * Get the number value of a variable.
7568 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007569 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007570 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007571 * caller of incompatible types: it sets *denote to TRUE if "denote"
7572 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007574 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007575tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007577 int error = FALSE;
7578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007579 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007580}
7581
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007582 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007584{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007585 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007587 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007590 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007591 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007592#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007594 break;
7595#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007596 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007597 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 break;
7600 case VAR_STRING:
7601 if (varp->vval.v_string != NULL)
7602 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007603 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007604 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007605 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007607 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007609 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007610 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007611 case VAR_SPECIAL:
7612 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7613 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007614 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007615#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007617 break;
7618#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007619 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007620#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007621 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007622 break;
7623#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007624 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007626 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007627 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007628 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007631 if (denote == NULL) /* useful for values that must be unsigned */
7632 n = -1;
7633 else
7634 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636}
7637
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007638#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007640tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007641{
7642 switch (varp->v_type)
7643 {
7644 case VAR_NUMBER:
7645 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007646 case VAR_FLOAT:
7647 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007648 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007649 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007650 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007651 break;
7652 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007654 break;
7655 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007657 break;
7658 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007659 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007660 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007661 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007662 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007663 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007664 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007665# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007666 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007667 break;
7668# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007669 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007670# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007671 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007672 break;
7673# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007674 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007675 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007676 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007677 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007678 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007679 break;
7680 }
7681 return 0;
7682}
7683#endif
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 * Get the string value of a variable.
7687 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007688 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7689 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 * If the String variable has never been set, return an empty string.
7691 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007692 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007693 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007695 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007696tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
7698 static char_u mybuf[NUMBUFLEN];
7699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701}
7702
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007703 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007704tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007706 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007707
7708 return res != NULL ? res : (char_u *)"";
7709}
7710
Bram Moolenaar7d647822014-04-05 21:28:56 +02007711/*
7712 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7713 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007714 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007715tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007716{
7717 static char_u mybuf[NUMBUFLEN];
7718
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007719 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007720}
7721
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007722 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007723tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007724{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007727 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007728 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007729 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730 return buf;
7731 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007732 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007733 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007734 break;
7735 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007736 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 break;
7738 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007739 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007740 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007741 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007742#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007743 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007744 break;
7745#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007746 case VAR_STRING:
7747 if (varp->vval.v_string != NULL)
7748 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007750 case VAR_SPECIAL:
7751 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7752 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007753 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007754 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007755 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007756 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007757#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758 {
7759 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007760 char *status;
7761
7762 if (job == NULL)
7763 return (char_u *)"no process";
7764 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007765 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007766 : "run";
7767# ifdef UNIX
7768 vim_snprintf((char *)buf, NUMBUFLEN,
7769 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007770# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007771 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007772 "process %ld %s",
7773 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007774 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007775# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007776 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007777 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7778# endif
7779 return buf;
7780 }
7781#endif
7782 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007783 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007784#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007785 {
7786 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007787 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007788
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007789 if (channel == NULL)
7790 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7791 else
7792 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007793 "channel %d %s", channel->ch_id, status);
7794 return buf;
7795 }
7796#endif
7797 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007798 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007799 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007802 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804
7805/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007806 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7807 * string() on Dict, List, etc.
7808 */
7809 char_u *
7810tv_stringify(typval_T *varp, char_u *buf)
7811{
7812 if (varp->v_type == VAR_LIST
7813 || varp->v_type == VAR_DICT
7814 || varp->v_type == VAR_FUNC
7815 || varp->v_type == VAR_PARTIAL
7816 || varp->v_type == VAR_FLOAT)
7817 {
7818 typval_T tmp;
7819
7820 f_string(varp, &tmp);
7821 tv_get_string_buf(&tmp, buf);
7822 clear_tv(varp);
7823 *varp = tmp;
7824 return tmp.vval.v_string;
7825 }
7826 return tv_get_string_buf(varp, buf);
7827}
7828
7829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 * Find variable "name" in the list of variables.
7831 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007833 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007836 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007841 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
Bram Moolenaara7043832005-01-21 11:56:39 +00007843 ht = find_var_ht(name, &varname);
7844 if (htp != NULL)
7845 *htp = ht;
7846 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007848 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7849 if (ret != NULL)
7850 return ret;
7851
7852 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007853 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007857 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007858 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007860 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861find_var_in_ht(
7862 hashtab_T *ht,
7863 int htname,
7864 char_u *varname,
7865 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007866{
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 hashitem_T *hi;
7868
7869 if (*varname == NUL)
7870 {
7871 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007872 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007874 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 case 'g': return &globvars_var;
7876 case 'v': return &vimvars_var;
7877 case 'b': return &curbuf->b_bufvar;
7878 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007879 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007880 case 'l': return get_funccal_local_var();
7881 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 }
7883 return NULL;
7884 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007885
7886 hi = hash_find(ht, varname);
7887 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007888 {
7889 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007890 * worked find the variable again. Don't auto-load a script if it was
7891 * loaded already, otherwise it would be loaded every time when
7892 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007893 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007894 {
7895 /* Note: script_autoload() may make "hi" invalid. It must either
7896 * be obtained again or not used. */
7897 if (!script_autoload(varname, FALSE) || aborting())
7898 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007899 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007900 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007901 if (HASHITEM_EMPTY(hi))
7902 return NULL;
7903 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007905}
7906
7907/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007908 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007909 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007910 * Set "varname" to the start of name without ':'.
7911 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007912 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007913find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007915 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007916 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007917
Bram Moolenaar73627d02015-08-11 15:46:09 +02007918 if (name[0] == NUL)
7919 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 if (name[1] != ':')
7921 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007922 /* The name must not start with a colon or #. */
7923 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 return NULL;
7925 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007926
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007927 // "version" is "v:version" in all scopes if scriptversion < 3.
7928 // Same for a few other variables marked with VV_COMPAT.
7929 if (current_sctx.sc_version < 3)
7930 {
7931 hi = hash_find(&compat_hashtab, name);
7932 if (!HASHITEM_EMPTY(hi))
7933 return &compat_hashtab;
7934 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007935
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007936 ht = get_funccal_local_ht();
7937 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007939 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 }
7941 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007942 if (*name == 'g') /* global variable */
7943 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007944 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7945 */
7946 if (vim_strchr(name + 2, ':') != NULL
7947 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007948 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007950 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007952 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007953 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007954 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 if (*name == 'v') /* v: variable */
7956 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007957 if (*name == 'a') /* a: function argument */
7958 return get_funccal_args_ht();
7959 if (*name == 'l') /* l: local function variable */
7960 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007962 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7963 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 return NULL;
7965}
7966
7967/*
7968 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007969 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 * Returns NULL when it doesn't exist.
7971 */
7972 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007973get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007977 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 if (v == NULL)
7979 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007980 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981}
7982
7983/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 * sourcing this script and when executing functions defined in the script.
7986 */
7987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007988new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
Bram Moolenaara7043832005-01-21 11:56:39 +00007990 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007991 hashtab_T *ht;
7992 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007993
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7995 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007996 /* Re-allocating ga_data means that an ht_array pointing to
7997 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007999 for (i = 1; i <= ga_scripts.ga_len; ++i)
8000 {
8001 ht = &SCRIPT_VARS(i);
8002 if (ht->ht_mask == HT_INIT_SIZE - 1)
8003 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008004 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008006 }
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 while (ga_scripts.ga_len < id)
8009 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008010 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008011 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008012 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 }
8015 }
8016}
8017
8018/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8020 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 */
8022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008023init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
Bram Moolenaar33570922005-01-25 22:26:29 +00008025 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008026 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008027 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008028 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008029 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 dict_var->di_tv.vval.v_dict = dict;
8031 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008032 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8034 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035}
8036
8037/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008038 * Unreference a dictionary initialized by init_var_dict().
8039 */
8040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008041unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008042{
8043 /* Now the dict needs to be freed if no one else is using it, go back to
8044 * normal reference counting. */
8045 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8046 dict_unref(dict);
8047}
8048
8049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 * Frees all allocated variables and the value they contain.
8052 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 */
8054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008055vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008056{
8057 vars_clear_ext(ht, TRUE);
8058}
8059
8060/*
8061 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8062 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008064vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
Bram Moolenaara7043832005-01-21 11:56:39 +00008066 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 hashitem_T *hi;
8068 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
Bram Moolenaar33570922005-01-25 22:26:29 +00008070 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008071 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008072 for (hi = ht->ht_array; todo > 0; ++hi)
8073 {
8074 if (!HASHITEM_EMPTY(hi))
8075 {
8076 --todo;
8077
Bram Moolenaar33570922005-01-25 22:26:29 +00008078 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008079 * ht_array might change then. hash_clear() takes care of it
8080 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 v = HI2DI(hi);
8082 if (free_val)
8083 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008084 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008086 }
8087 }
8088 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008089 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090}
8091
Bram Moolenaara7043832005-01-21 11:56:39 +00008092/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008093 * Delete a variable from hashtab "ht" at item "hi".
8094 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008100
8101 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008102 clear_tv(&di->di_tv);
8103 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104}
8105
8106/*
8107 * List the value of one internal variable.
8108 */
8109 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008110list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112 char_u *tofree;
8113 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008115
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008116 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008118 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120}
8121
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008123list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008124 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008125 char_u *name,
8126 int type,
8127 char_u *string,
8128 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
Bram Moolenaar31859182007-08-14 20:41:13 +00008130 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8131 msg_start();
8132 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008134 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 msg_putchar(' ');
8136 msg_advance(22);
8137 if (type == VAR_NUMBER)
8138 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008139 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 msg_putchar('*');
8141 else if (type == VAR_LIST)
8142 {
8143 msg_putchar('[');
8144 if (*string == '[')
8145 ++string;
8146 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008147 else if (type == VAR_DICT)
8148 {
8149 msg_putchar('{');
8150 if (*string == '{')
8151 ++string;
8152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 else
8154 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008155
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008158 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008159 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008160 if (*first)
8161 {
8162 msg_clr_eos();
8163 *first = FALSE;
8164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
8167/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 * If the variable already exists, the value is updated.
8170 * Otherwise the variable is created.
8171 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008173set_var(
8174 char_u *name,
8175 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008176 int copy) // make copy of value in "tv"
8177{
8178 set_var_const(name, tv, copy, FALSE);
8179}
8180
8181/*
8182 * Set variable "name" to value in "tv".
8183 * If the variable already exists and "is_const" is FALSE the value is updated.
8184 * Otherwise the variable is created.
8185 */
8186 static void
8187set_var_const(
8188 char_u *name,
8189 typval_T *tv,
8190 int copy, // make copy of value in "tv"
8191 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008197 ht = find_var_ht(name, &varname);
8198 if (ht == NULL || *varname == NUL)
8199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008200 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008201 return;
8202 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008203 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008204
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008205 /* Search in parent scope which is possible to reference from lambda */
8206 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008207 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008208
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008209 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8210 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008211 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008212
Bram Moolenaar33570922005-01-25 22:26:29 +00008213 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008215 if (is_const)
8216 {
8217 emsg(_(e_cannot_mod));
8218 return;
8219 }
8220
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008222 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008223 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008225
8226 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008227 * Handle setting internal v: variables separately where needed to
8228 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 */
8230 if (ht == &vimvarht)
8231 {
8232 if (v->di_tv.v_type == VAR_STRING)
8233 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008234 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008236 {
8237 char_u *val = tv_get_string(tv);
8238
8239 // Careful: when assigning to v:errmsg and tv_get_string()
8240 // causes an error message the variable will alrady be set.
8241 if (v->di_tv.vval.v_string == NULL)
8242 v->di_tv.vval.v_string = vim_strsave(val);
8243 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008244 else
8245 {
8246 /* Take over the string to avoid an extra alloc/free. */
8247 v->di_tv.vval.v_string = tv->vval.v_string;
8248 tv->vval.v_string = NULL;
8249 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008250 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008251 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008252 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008254 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255 if (STRCMP(varname, "searchforward") == 0)
8256 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008257#ifdef FEAT_SEARCH_EXTRA
8258 else if (STRCMP(varname, "hlsearch") == 0)
8259 {
8260 no_hlsearch = !v->di_tv.vval.v_number;
8261 redraw_all_later(SOME_VALID);
8262 }
8263#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008264 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008266 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008268 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008269 return;
8270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008271 }
8272
8273 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
8275 else /* add a new variable */
8276 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008277 // Can't add "v:" or "a:" variable.
8278 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008280 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008281 return;
8282 }
8283
Bram Moolenaar31b81602019-02-10 22:14:27 +01008284 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008285 if (!valid_varname(varname))
8286 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008287
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008288 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008289 if (v == NULL)
8290 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008294 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 return;
8296 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008297 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008298 if (is_const)
8299 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008301
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008303 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008304 else
8305 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008307 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008309 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008310
8311 if (is_const)
8312 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008316 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 * Also give an error message.
8318 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008320var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008321{
8322 if (flags & DI_FLAGS_RO)
8323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008324 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 return TRUE;
8326 }
8327 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008329 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 return TRUE;
8331 }
8332 return FALSE;
8333}
8334
8335/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008336 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8337 * Also give an error message.
8338 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008340var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008341{
8342 if (flags & DI_FLAGS_FIX)
8343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008344 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008345 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008346 return TRUE;
8347 }
8348 return FALSE;
8349}
8350
8351/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008352 * Check if a funcref is assigned to a valid variable name.
8353 * Return TRUE and give an error if not.
8354 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008356var_check_func_name(
8357 char_u *name, /* points to start of variable name */
8358 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008359{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008360 /* Allow for w: b: s: and t:. */
8361 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008362 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8363 ? name[2] : name[0]))
8364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008365 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008366 name);
8367 return TRUE;
8368 }
8369 /* Don't allow hiding a function. When "v" is not NULL we might be
8370 * assigning another function to the same var, the type is checked
8371 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008372 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008374 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008375 name);
8376 return TRUE;
8377 }
8378 return FALSE;
8379}
8380
8381/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008382 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008383 * Also give an error message, using "name" or _("name") when use_gettext is
8384 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008385 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008386 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008387var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008388{
8389 if (lock & VAR_LOCKED)
8390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008391 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008392 name == NULL ? (char_u *)_("Unknown")
8393 : use_gettext ? (char_u *)_(name)
8394 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008395 return TRUE;
8396 }
8397 if (lock & VAR_FIXED)
8398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008399 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008400 name == NULL ? (char_u *)_("Unknown")
8401 : use_gettext ? (char_u *)_(name)
8402 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008403 return TRUE;
8404 }
8405 return FALSE;
8406}
8407
8408/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008409 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8410 * Also give an error message, using "name" or _("name") when use_gettext is
8411 * TRUE.
8412 */
8413 static int
8414tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8415{
8416 int lock = 0;
8417
8418 switch (tv->v_type)
8419 {
8420 case VAR_BLOB:
8421 if (tv->vval.v_blob != NULL)
8422 lock = tv->vval.v_blob->bv_lock;
8423 break;
8424 case VAR_LIST:
8425 if (tv->vval.v_list != NULL)
8426 lock = tv->vval.v_list->lv_lock;
8427 break;
8428 case VAR_DICT:
8429 if (tv->vval.v_dict != NULL)
8430 lock = tv->vval.v_dict->dv_lock;
8431 break;
8432 default:
8433 break;
8434 }
8435 return var_check_lock(tv->v_lock, name, use_gettext)
8436 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8437}
8438
8439/*
8440 * Check if a variable name is valid.
8441 * Return FALSE and give an error if not.
8442 */
8443 int
8444valid_varname(char_u *varname)
8445{
8446 char_u *p;
8447
8448 for (p = varname; *p != NUL; ++p)
8449 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8450 && *p != AUTOLOAD_CHAR)
8451 {
8452 semsg(_(e_illvar), varname);
8453 return FALSE;
8454 }
8455 return TRUE;
8456}
8457
8458/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008461 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008462 * It is OK for "from" and "to" to point to the same item. This is used to
8463 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008464 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008466copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008468 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008469 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008470 switch (from->v_type)
8471 {
8472 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008473 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008474 to->vval.v_number = from->vval.v_number;
8475 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008476 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008477#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478 to->vval.v_float = from->vval.v_float;
8479 break;
8480#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008481 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008482#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008483 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008484 if (to->vval.v_job != NULL)
8485 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008486 break;
8487#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008488 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008489#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008490 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008491 if (to->vval.v_channel != NULL)
8492 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008493 break;
8494#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008495 case VAR_STRING:
8496 case VAR_FUNC:
8497 if (from->vval.v_string == NULL)
8498 to->vval.v_string = NULL;
8499 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008502 if (from->v_type == VAR_FUNC)
8503 func_ref(to->vval.v_string);
8504 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008506 case VAR_PARTIAL:
8507 if (from->vval.v_partial == NULL)
8508 to->vval.v_partial = NULL;
8509 else
8510 {
8511 to->vval.v_partial = from->vval.v_partial;
8512 ++to->vval.v_partial->pt_refcount;
8513 }
8514 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008515 case VAR_BLOB:
8516 if (from->vval.v_blob == NULL)
8517 to->vval.v_blob = NULL;
8518 else
8519 {
8520 to->vval.v_blob = from->vval.v_blob;
8521 ++to->vval.v_blob->bv_refcount;
8522 }
8523 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 case VAR_LIST:
8525 if (from->vval.v_list == NULL)
8526 to->vval.v_list = NULL;
8527 else
8528 {
8529 to->vval.v_list = from->vval.v_list;
8530 ++to->vval.v_list->lv_refcount;
8531 }
8532 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008533 case VAR_DICT:
8534 if (from->vval.v_dict == NULL)
8535 to->vval.v_dict = NULL;
8536 else
8537 {
8538 to->vval.v_dict = from->vval.v_dict;
8539 ++to->vval.v_dict->dv_refcount;
8540 }
8541 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008542 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008543 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008544 break;
8545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546}
8547
8548/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008549 * Make a copy of an item.
8550 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008551 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8552 * reference to an already copied list/dict can be used.
8553 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008556item_copy(
8557 typval_T *from,
8558 typval_T *to,
8559 int deep,
8560 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561{
8562 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008564
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008567 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008568 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569 }
8570 ++recurse;
8571
8572 switch (from->v_type)
8573 {
8574 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008575 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008576 case VAR_STRING:
8577 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008578 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008579 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008580 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008581 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008582 copy_tv(from, to);
8583 break;
8584 case VAR_LIST:
8585 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008586 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008587 if (from->vval.v_list == NULL)
8588 to->vval.v_list = NULL;
8589 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8590 {
8591 /* use the copy made earlier */
8592 to->vval.v_list = from->vval.v_list->lv_copylist;
8593 ++to->vval.v_list->lv_refcount;
8594 }
8595 else
8596 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8597 if (to->vval.v_list == NULL)
8598 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008600 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008601 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008602 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 case VAR_DICT:
8604 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008605 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008606 if (from->vval.v_dict == NULL)
8607 to->vval.v_dict = NULL;
8608 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8609 {
8610 /* use the copy made earlier */
8611 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8612 ++to->vval.v_dict->dv_refcount;
8613 }
8614 else
8615 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8616 if (to->vval.v_dict == NULL)
8617 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008618 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008619 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008620 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008621 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 }
8623 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008624 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008625}
8626
8627/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 * This function is used by f_input() and f_inputdialog() functions. The third
8629 * argument to f_input() specifies the type of completion to use at the
8630 * prompt. The third argument to f_inputdialog() specifies the value to return
8631 * when the user cancels the prompt.
8632 */
8633 void
8634get_user_input(
8635 typval_T *argvars,
8636 typval_T *rettv,
8637 int inputdialog,
8638 int secret)
8639{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 char_u *p = NULL;
8642 int c;
8643 char_u buf[NUMBUFLEN];
8644 int cmd_silent_save = cmd_silent;
8645 char_u *defstr = (char_u *)"";
8646 int xp_type = EXPAND_NOTHING;
8647 char_u *xp_arg = NULL;
8648
8649 rettv->v_type = VAR_STRING;
8650 rettv->vval.v_string = NULL;
8651
8652#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008653 /* While starting up, there is no place to enter text. When running tests
8654 * with --not-a-term we assume feedkeys() will be used. */
8655 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 return;
8657#endif
8658
8659 cmd_silent = FALSE; /* Want to see the prompt. */
8660 if (prompt != NULL)
8661 {
8662 /* Only the part of the message after the last NL is considered as
8663 * prompt for the command line */
8664 p = vim_strrchr(prompt, '\n');
8665 if (p == NULL)
8666 p = prompt;
8667 else
8668 {
8669 ++p;
8670 c = *p;
8671 *p = NUL;
8672 msg_start();
8673 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008674 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 msg_didout = FALSE;
8676 msg_starthere();
8677 *p = c;
8678 }
8679 cmdline_row = msg_row;
8680
8681 if (argvars[1].v_type != VAR_UNKNOWN)
8682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008683 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684 if (defstr != NULL)
8685 stuffReadbuffSpec(defstr);
8686
8687 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8688 {
8689 char_u *xp_name;
8690 int xp_namelen;
8691 long argt;
8692
8693 /* input() with a third argument: completion */
8694 rettv->vval.v_string = NULL;
8695
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008696 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 if (xp_name == NULL)
8698 return;
8699
8700 xp_namelen = (int)STRLEN(xp_name);
8701
8702 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8703 &xp_arg) == FAIL)
8704 return;
8705 }
8706 }
8707
8708 if (defstr != NULL)
8709 {
8710 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 ex_normal_busy = 0;
8713 rettv->vval.v_string =
8714 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8715 xp_type, xp_arg);
8716 ex_normal_busy = save_ex_normal_busy;
8717 }
8718 if (inputdialog && rettv->vval.v_string == NULL
8719 && argvars[1].v_type != VAR_UNKNOWN
8720 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 &argvars[2], buf));
8723
8724 vim_free(xp_arg);
8725
8726 /* since the user typed this, no need to wait for return */
8727 need_wait_return = FALSE;
8728 msg_didout = FALSE;
8729 }
8730 cmd_silent = cmd_silent_save;
8731}
8732
8733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 * ":echo expr1 ..." print each argument separated with a space, add a
8735 * newline at the end.
8736 * ":echon expr1 ..." print each argument plain.
8737 */
8738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008739ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008742 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008743 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 char_u *p;
8745 int needclr = TRUE;
8746 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008747 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008748 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008749 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
8751 if (eap->skip)
8752 ++emsg_skip;
8753 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008755 /* If eval1() causes an error message the text from the command may
8756 * still need to be cleared. E.g., "echo 22,44". */
8757 need_clr_eos = needclr;
8758
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
8762 /*
8763 * Report the invalid expression unless the expression evaluation
8764 * has been cancelled due to an aborting error, an interrupt, or an
8765 * exception.
8766 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008767 if (!aborting() && did_emsg == did_emsg_before
8768 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008769 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008770 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 break;
8772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008773 need_clr_eos = FALSE;
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (!eap->skip)
8776 {
8777 if (atstart)
8778 {
8779 atstart = FALSE;
8780 /* Call msg_start() after eval1(), evaluating the expression
8781 * may cause a message to appear. */
8782 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008783 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008784 /* Mark the saved text as finishing the line, so that what
8785 * follows is displayed on a new line when scrolling back
8786 * at the more prompt. */
8787 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
8791 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008792 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008793 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008794 if (p != NULL)
8795 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008797 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008799 if (*p != TAB && needclr)
8800 {
8801 /* remove any text still there from the command */
8802 msg_clr_eos();
8803 needclr = FALSE;
8804 }
8805 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
8807 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008808 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008809 if (has_mbyte)
8810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008811 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008812
8813 (void)msg_outtrans_len_attr(p, i, echo_attr);
8814 p += i - 1;
8815 }
8816 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008817 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008820 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 arg = skipwhite(arg);
8824 }
8825 eap->nextcmd = check_nextcmd(arg);
8826
8827 if (eap->skip)
8828 --emsg_skip;
8829 else
8830 {
8831 /* remove text that may still be there from the command */
8832 if (needclr)
8833 msg_clr_eos();
8834 if (eap->cmdidx == CMD_echo)
8835 msg_end();
8836 }
8837}
8838
8839/*
8840 * ":echohl {name}".
8841 */
8842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008843ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008845 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
8848/*
8849 * ":execute expr1 ..." execute the result of an expression.
8850 * ":echomsg expr1 ..." Print a message
8851 * ":echoerr expr1 ..." Print an error
8852 * Each gets spaces around each argument and a newline at the end for
8853 * echo commands
8854 */
8855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
8858 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 int ret = OK;
8861 char_u *p;
8862 garray_T ga;
8863 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008864 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
8866 ga_init2(&ga, 1, 80);
8867
8868 if (eap->skip)
8869 ++emsg_skip;
8870 while (*arg != NUL && *arg != '|' && *arg != '\n')
8871 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008872 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8873 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
8876 if (!eap->skip)
8877 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008878 char_u buf[NUMBUFLEN];
8879
8880 if (eap->cmdidx == CMD_execute)
8881 p = tv_get_string_buf(&rettv, buf);
8882 else
8883 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 len = (int)STRLEN(p);
8885 if (ga_grow(&ga, len + 2) == FAIL)
8886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 ret = FAIL;
8889 break;
8890 }
8891 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 ga.ga_len += len;
8895 }
8896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 arg = skipwhite(arg);
8899 }
8900
8901 if (ret != FAIL && ga.ga_data != NULL)
8902 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008903 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8904 {
8905 /* Mark the already saved text as finishing the line, so that what
8906 * follows is displayed on a new line when scrolling back at the
8907 * more prompt. */
8908 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008909 }
8910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008912 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008913 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008914 out_flush();
8915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else if (eap->cmdidx == CMD_echoerr)
8917 {
8918 /* We don't want to abort following commands, restore did_emsg. */
8919 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008920 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (!force_abort)
8922 did_emsg = save_did_emsg;
8923 }
8924 else if (eap->cmdidx == CMD_execute)
8925 do_cmdline((char_u *)ga.ga_data,
8926 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8927 }
8928
8929 ga_clear(&ga);
8930
8931 if (eap->skip)
8932 --emsg_skip;
8933
8934 eap->nextcmd = check_nextcmd(arg);
8935}
8936
8937/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938 * Find window specified by "vp" in tabpage "tp".
8939 */
8940 win_T *
8941find_win_by_nr(
8942 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008943 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008946 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 if (nr < 0)
8949 return NULL;
8950 if (nr == 0)
8951 return curwin;
8952
Bram Moolenaar29323592016-07-24 22:04:11 +02008953 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8954 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 if (nr >= LOWEST_WIN_ID)
8956 {
8957 if (wp->w_id == nr)
8958 return wp;
8959 }
8960 else if (--nr <= 0)
8961 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008962 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008964 {
8965#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008966 // check tab-local popup windows
8967 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008968 if (wp->w_id == nr)
8969 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008970 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008971 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8972 if (wp->w_id == nr)
8973 return wp;
8974#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008976 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008978}
8979
8980/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008981 * Find a window: When using a Window ID in any tab page, when using a number
8982 * in the current tab page.
8983 */
8984 win_T *
8985find_win_by_nr_or_id(typval_T *vp)
8986{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008987 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008988
8989 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008990 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008991 return find_win_by_nr(vp, NULL);
8992}
8993
8994/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008996 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 */
8998 win_T *
8999find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009000 typval_T *wvp, // VAR_UNKNOWN for current window
9001 typval_T *tvp, // VAR_UNKNOWN for current tab page
9002 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003{
9004 win_T *wp = NULL;
9005 tabpage_T *tp = NULL;
9006 long n;
9007
9008 if (wvp->v_type != VAR_UNKNOWN)
9009 {
9010 if (tvp->v_type != VAR_UNKNOWN)
9011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009012 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 if (n >= 0)
9014 tp = find_tabpage(n);
9015 }
9016 else
9017 tp = curtab;
9018
9019 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009020 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009022 if (wp == NULL && wvp->v_type == VAR_NUMBER
9023 && wvp->vval.v_number != -1)
9024 // A window with the specified number is not found
9025 tp = NULL;
9026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 }
9028 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009029 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009031 tp = curtab;
9032 }
9033
9034 if (ptp != NULL)
9035 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036
9037 return wp;
9038}
9039
9040/*
9041 * getwinvar() and gettabwinvar()
9042 */
9043 void
9044getwinvar(
9045 typval_T *argvars,
9046 typval_T *rettv,
9047 int off) /* 1 for gettabwinvar() */
9048{
9049 win_T *win;
9050 char_u *varname;
9051 dictitem_T *v;
9052 tabpage_T *tp = NULL;
9053 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 win_T *oldcurwin;
9055 tabpage_T *oldtabpage;
9056 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009059 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 else
9061 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009063 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 ++emsg_off;
9065
9066 rettv->v_type = VAR_STRING;
9067 rettv->vval.v_string = NULL;
9068
9069 if (win != NULL && varname != NULL)
9070 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 /* Set curwin to be our win, temporarily. Also set the tabpage,
9072 * otherwise the window is not valid. Only do this when needed,
9073 * autocommands get blocked. */
9074 need_switch_win = !(tp == curtab && win == curwin);
9075 if (!need_switch_win
9076 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009078 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009080 if (varname[1] == NUL)
9081 {
9082 /* get all window-local options in a dict */
9083 dict_T *opts = get_winbuf_options(FALSE);
9084
9085 if (opts != NULL)
9086 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009087 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009088 done = TRUE;
9089 }
9090 }
9091 else if (get_option_tv(&varname, rettv, 1) == OK)
9092 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 done = TRUE;
9094 }
9095 else
9096 {
9097 /* Look up the variable. */
9098 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9099 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9100 varname, FALSE);
9101 if (v != NULL)
9102 {
9103 copy_tv(&v->di_tv, rettv);
9104 done = TRUE;
9105 }
9106 }
9107 }
9108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 if (need_switch_win)
9110 /* restore previous notion of curwin */
9111 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 }
9113
9114 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9115 /* use the default return value */
9116 copy_tv(&argvars[off + 2], rettv);
9117
9118 --emsg_off;
9119}
9120
9121/*
9122 * "setwinvar()" and "settabwinvar()" functions
9123 */
9124 void
9125setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9126{
9127 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 win_T *save_curwin;
9129 tabpage_T *save_curtab;
9130 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131 char_u *varname, *winvarname;
9132 typval_T *varp;
9133 char_u nbuf[NUMBUFLEN];
9134 tabpage_T *tp = NULL;
9135
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009136 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 return;
9138
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009140 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141 else
9142 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009144 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 varp = &argvars[off + 2];
9146
9147 if (win != NULL && varname != NULL && varp != NULL)
9148 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 need_switch_win = !(tp == curtab && win == curwin);
9150 if (!need_switch_win
9151 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152 {
9153 if (*varname == '&')
9154 {
9155 long numval;
9156 char_u *strval;
9157 int error = FALSE;
9158
9159 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009160 numval = (long)tv_get_number_chk(varp, &error);
9161 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 if (!error && strval != NULL)
9163 set_option_value(varname, numval, strval, OPT_LOCAL);
9164 }
9165 else
9166 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009167 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 if (winvarname != NULL)
9169 {
9170 STRCPY(winvarname, "w:");
9171 STRCPY(winvarname + 2, varname);
9172 set_var(winvarname, varp, TRUE);
9173 vim_free(winvarname);
9174 }
9175 }
9176 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 if (need_switch_win)
9178 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179 }
9180}
9181
9182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9184 * "arg" points to the "&" or '+' when called, to "option" when returning.
9185 * Returns NULL when no option name found. Otherwise pointer to the char
9186 * after the option name.
9187 */
9188 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191 char_u *p = *arg;
9192
9193 ++p;
9194 if (*p == 'g' && p[1] == ':')
9195 {
9196 *opt_flags = OPT_GLOBAL;
9197 p += 2;
9198 }
9199 else if (*p == 'l' && p[1] == ':')
9200 {
9201 *opt_flags = OPT_LOCAL;
9202 p += 2;
9203 }
9204 else
9205 *opt_flags = 0;
9206
9207 if (!ASCII_ISALPHA(*p))
9208 return NULL;
9209 *arg = p;
9210
9211 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9212 p += 4; /* termcap option */
9213 else
9214 while (ASCII_ISALPHA(*p))
9215 ++p;
9216 return p;
9217}
9218
9219/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009220 * Return the autoload script name for a function or variable name.
9221 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009222 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009224 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009225autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009226{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009227 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009228 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009229
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009230 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009231 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009232 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009233 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009234 STRCPY(scriptname, "autoload/");
9235 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009236 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9237 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009238 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009239 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009240 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009241}
9242
9243/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009244 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009245 * Return TRUE if a package was loaded.
9246 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009247 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009248script_autoload(
9249 char_u *name,
9250 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009251{
9252 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009253 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009254 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009255 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009256
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009257 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009258 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009259 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009260 return FALSE;
9261
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009262 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009263
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009264 /* Find the name in the list of previously loaded package names. Skip
9265 * "autoload/", it's always the same. */
9266 for (i = 0; i < ga_loaded.ga_len; ++i)
9267 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9268 break;
9269 if (!reload && i < ga_loaded.ga_len)
9270 ret = FALSE; /* was loaded already */
9271 else
9272 {
9273 /* Remember the name if it wasn't loaded already. */
9274 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9275 {
9276 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9277 tofree = NULL;
9278 }
9279
9280 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009281 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009282 ret = TRUE;
9283 }
9284
9285 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009286 return ret;
9287}
9288
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9290typedef enum
9291{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9293 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9294 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295} var_flavour_T;
9296
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009298var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 char_u *p = varname;
9301
9302 if (ASCII_ISUPPER(*p))
9303 {
9304 while (*(++p))
9305 if (ASCII_ISLOWER(*p))
9306 return VAR_FLAVOUR_SESSION;
9307 return VAR_FLAVOUR_VIMINFO;
9308 }
9309 else
9310 return VAR_FLAVOUR_DEFAULT;
9311}
9312#endif
9313
9314#if defined(FEAT_VIMINFO) || defined(PROTO)
9315/*
9316 * Restore global vars that start with a capital from the viminfo file
9317 */
9318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009322 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009323 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009324 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325
9326 if (!writing && (find_viminfo_parameter('!') != NULL))
9327 {
9328 tab = vim_strchr(virp->vir_line + 1, '\t');
9329 if (tab != NULL)
9330 {
9331 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009332 switch (*tab)
9333 {
9334 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009335#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009336 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009337#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009338 case 'D': type = VAR_DICT; break;
9339 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009340 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009341 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
9344 tab = vim_strchr(tab, '\t');
9345 if (tab != NULL)
9346 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009347 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009348 if (type == VAR_STRING || type == VAR_DICT
9349 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009350 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009352#ifdef FEAT_FLOAT
9353 else if (type == VAR_FLOAT)
9354 (void)string2float(tab + 1, &tv.vval.v_float);
9355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009357 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009358 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009359 {
9360 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9361
9362 if (etv == NULL)
9363 /* Failed to parse back the dict or list, use it as a
9364 * string. */
9365 tv.v_type = VAR_STRING;
9366 else
9367 {
9368 vim_free(tv.vval.v_string);
9369 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009370 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009371 }
9372 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009373 else if (type == VAR_BLOB)
9374 {
9375 blob_T *blob = string2blob(tv.vval.v_string);
9376
9377 if (blob == NULL)
9378 // Failed to parse back the blob, use it as a string.
9379 tv.v_type = VAR_STRING;
9380 else
9381 {
9382 vim_free(tv.vval.v_string);
9383 tv.v_type = VAR_BLOB;
9384 tv.vval.v_blob = blob;
9385 }
9386 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009387
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009388 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009389 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009390 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009391 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009392
9393 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009394 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009395 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9396 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009397 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399 }
9400 }
9401
9402 return viminfo_readline(virp);
9403}
9404
9405/*
9406 * Write global vars that start with a capital to the viminfo file
9407 */
9408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009409write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaar33570922005-01-25 22:26:29 +00009411 hashitem_T *hi;
9412 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009413 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009414 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009415 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009417 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
9419 if (find_viminfo_parameter('!') == NULL)
9420 return;
9421
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009422 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009423
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009424 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009425 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009427 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009429 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009430 this_var = HI2DI(hi);
9431 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009433 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009434 {
9435 case VAR_STRING: s = "STR"; break;
9436 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009437 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009438 case VAR_DICT: s = "DIC"; break;
9439 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009440 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009441 case VAR_SPECIAL: s = "XPL"; break;
9442
9443 case VAR_UNKNOWN:
9444 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009445 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009446 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009447 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009448 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009449 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009451 if (this_var->di_tv.v_type == VAR_SPECIAL)
9452 {
9453 sprintf((char *)numbuf, "%ld",
9454 (long)this_var->di_tv.vval.v_number);
9455 p = numbuf;
9456 tofree = NULL;
9457 }
9458 else
9459 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009460 if (p != NULL)
9461 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009462 vim_free(tofree);
9463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465 }
9466}
9467#endif
9468
9469#if defined(FEAT_SESSION) || defined(PROTO)
9470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
Bram Moolenaar33570922005-01-25 22:26:29 +00009473 hashitem_T *hi;
9474 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009475 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 char_u *p, *t;
9477
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009478 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009479 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009481 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009483 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 this_var = HI2DI(hi);
9485 if ((this_var->di_tv.v_type == VAR_NUMBER
9486 || this_var->di_tv.v_type == VAR_STRING)
9487 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009488 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009489 /* Escape special characters with a backslash. Turn a LF and
9490 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009491 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009492 (char_u *)"\\\"\n\r");
9493 if (p == NULL) /* out of memory */
9494 break;
9495 for (t = p; *t != NUL; ++t)
9496 if (*t == '\n')
9497 *t = 'n';
9498 else if (*t == '\r')
9499 *t = 'r';
9500 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009501 this_var->di_key,
9502 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9503 : ' ',
9504 p,
9505 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9506 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009507 || put_eol(fd) == FAIL)
9508 {
9509 vim_free(p);
9510 return FAIL;
9511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 vim_free(p);
9513 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009514#ifdef FEAT_FLOAT
9515 else if (this_var->di_tv.v_type == VAR_FLOAT
9516 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9517 {
9518 float_T f = this_var->di_tv.vval.v_float;
9519 int sign = ' ';
9520
9521 if (f < 0)
9522 {
9523 f = -f;
9524 sign = '-';
9525 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009526 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009527 this_var->di_key, sign, f) < 0)
9528 || put_eol(fd) == FAIL)
9529 return FAIL;
9530 }
9531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 }
9533 }
9534 return OK;
9535}
9536#endif
9537
Bram Moolenaar661b1822005-07-28 22:36:45 +00009538/*
9539 * Display script name where an item was last set.
9540 * Should only be invoked when 'verbose' is non-zero.
9541 */
9542 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009543last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009544{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009545 char_u *p;
9546
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009547 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009548 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009549 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009550 if (p != NULL)
9551 {
9552 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009553 msg_puts(_("\n\tLast set from "));
9554 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009555 if (script_ctx.sc_lnum > 0)
9556 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009557 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009558 msg_outnum((long)script_ctx.sc_lnum);
9559 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009560 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009561 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009562 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009563 }
9564}
9565
Bram Moolenaar61be3762019-03-19 23:04:17 +01009566/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009567 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9568 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009569 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009571reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009572{
9573 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9574 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009575 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9576 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009577 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009578 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009579}
9580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581/*
9582 * Prepare "gap" for an assert error and add the sourcing position.
9583 */
9584 void
9585prepare_assert_error(garray_T *gap)
9586{
9587 char buf[NUMBUFLEN];
9588
9589 ga_init2(gap, 1, 100);
9590 if (sourcing_name != NULL)
9591 {
9592 ga_concat(gap, sourcing_name);
9593 if (sourcing_lnum > 0)
9594 ga_concat(gap, (char_u *)" ");
9595 }
9596 if (sourcing_lnum > 0)
9597 {
9598 sprintf(buf, "line %ld", (long)sourcing_lnum);
9599 ga_concat(gap, (char_u *)buf);
9600 }
9601 if (sourcing_name != NULL || sourcing_lnum > 0)
9602 ga_concat(gap, (char_u *)": ");
9603}
9604
9605/*
9606 * Add an assert error to v:errors.
9607 */
9608 void
9609assert_error(garray_T *gap)
9610{
9611 struct vimvar *vp = &vimvars[VV_ERRORS];
9612
9613 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9614 /* Make sure v:errors is a list. */
9615 set_vim_var_list(VV_ERRORS, list_alloc());
9616 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9617}
9618
Bram Moolenaar65a54642018-04-28 16:56:53 +02009619 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620assert_equal_common(typval_T *argvars, assert_type_T atype)
9621{
9622 garray_T ga;
9623
9624 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9625 != (atype == ASSERT_EQUAL))
9626 {
9627 prepare_assert_error(&ga);
9628 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9629 atype);
9630 assert_error(&ga);
9631 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009632 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009634 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009635}
9636
Bram Moolenaar65a54642018-04-28 16:56:53 +02009637 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009638assert_equalfile(typval_T *argvars)
9639{
9640 char_u buf1[NUMBUFLEN];
9641 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009642 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9643 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009644 garray_T ga;
9645 FILE *fd1;
9646 FILE *fd2;
9647
9648 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009649 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009650
9651 IObuff[0] = NUL;
9652 fd1 = mch_fopen((char *)fname1, READBIN);
9653 if (fd1 == NULL)
9654 {
9655 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9656 }
9657 else
9658 {
9659 fd2 = mch_fopen((char *)fname2, READBIN);
9660 if (fd2 == NULL)
9661 {
9662 fclose(fd1);
9663 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9664 }
9665 else
9666 {
9667 int c1, c2;
9668 long count = 0;
9669
9670 for (;;)
9671 {
9672 c1 = fgetc(fd1);
9673 c2 = fgetc(fd2);
9674 if (c1 == EOF)
9675 {
9676 if (c2 != EOF)
9677 STRCPY(IObuff, "first file is shorter");
9678 break;
9679 }
9680 else if (c2 == EOF)
9681 {
9682 STRCPY(IObuff, "second file is shorter");
9683 break;
9684 }
9685 else if (c1 != c2)
9686 {
9687 vim_snprintf((char *)IObuff, IOSIZE,
9688 "difference at byte %ld", count);
9689 break;
9690 }
9691 ++count;
9692 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009693 fclose(fd1);
9694 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009695 }
9696 }
9697 if (IObuff[0] != NUL)
9698 {
9699 prepare_assert_error(&ga);
9700 ga_concat(&ga, IObuff);
9701 assert_error(&ga);
9702 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009703 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009704 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009705 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009706}
9707
Bram Moolenaar65a54642018-04-28 16:56:53 +02009708 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709assert_match_common(typval_T *argvars, assert_type_T atype)
9710{
9711 garray_T ga;
9712 char_u buf1[NUMBUFLEN];
9713 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009714 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9715 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716
9717 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009718 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9720 {
9721 prepare_assert_error(&ga);
9722 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9723 atype);
9724 assert_error(&ga);
9725 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009726 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009728 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009729}
9730
Bram Moolenaar65a54642018-04-28 16:56:53 +02009731 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009732assert_inrange(typval_T *argvars)
9733{
9734 garray_T ga;
9735 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009736 char_u *tofree;
9737 char msg[200];
9738 char_u numbuf[NUMBUFLEN];
9739
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009740#ifdef FEAT_FLOAT
9741 if (argvars[0].v_type == VAR_FLOAT
9742 || argvars[1].v_type == VAR_FLOAT
9743 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009744 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009745 float_T flower = tv_get_float(&argvars[0]);
9746 float_T fupper = tv_get_float(&argvars[1]);
9747 float_T factual = tv_get_float(&argvars[2]);
9748
9749 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009750 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009751 prepare_assert_error(&ga);
9752 if (argvars[3].v_type != VAR_UNKNOWN)
9753 {
9754 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9755 vim_free(tofree);
9756 }
9757 else
9758 {
9759 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9760 flower, fupper, factual);
9761 ga_concat(&ga, (char_u *)msg);
9762 }
9763 assert_error(&ga);
9764 ga_clear(&ga);
9765 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009766 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009767 }
9768 else
9769#endif
9770 {
9771 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9772 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9773 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9774
9775 if (error)
9776 return 0;
9777 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009778 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009779 prepare_assert_error(&ga);
9780 if (argvars[3].v_type != VAR_UNKNOWN)
9781 {
9782 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9783 vim_free(tofree);
9784 }
9785 else
9786 {
9787 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009788 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009789 ga_concat(&ga, (char_u *)msg);
9790 }
9791 assert_error(&ga);
9792 ga_clear(&ga);
9793 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009794 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009795 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009796 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009797}
9798
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799/*
9800 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009801 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009803 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804assert_bool(typval_T *argvars, int isTrue)
9805{
9806 int error = FALSE;
9807 garray_T ga;
9808
9809 if (argvars[0].v_type == VAR_SPECIAL
9810 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009811 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009813 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 || error)
9815 {
9816 prepare_assert_error(&ga);
9817 fill_assert_error(&ga, &argvars[1],
9818 (char_u *)(isTrue ? "True" : "False"),
9819 NULL, &argvars[0], ASSERT_OTHER);
9820 assert_error(&ga);
9821 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009822 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009824 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009825}
9826
Bram Moolenaar65a54642018-04-28 16:56:53 +02009827 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009828assert_report(typval_T *argvars)
9829{
9830 garray_T ga;
9831
9832 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009833 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009834 assert_error(&ga);
9835 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009836 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009837}
9838
Bram Moolenaar65a54642018-04-28 16:56:53 +02009839 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840assert_exception(typval_T *argvars)
9841{
9842 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009843 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009844
9845 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9846 {
9847 prepare_assert_error(&ga);
9848 ga_concat(&ga, (char_u *)"v:exception is not set");
9849 assert_error(&ga);
9850 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009851 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852 }
9853 else if (error != NULL
9854 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9855 {
9856 prepare_assert_error(&ga);
9857 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9858 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9859 assert_error(&ga);
9860 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009861 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009863 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864}
9865
Bram Moolenaar65a54642018-04-28 16:56:53 +02009866 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009867assert_beeps(typval_T *argvars)
9868{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009869 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009870 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009871 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009872
9873 called_vim_beep = FALSE;
9874 suppress_errthrow = TRUE;
9875 emsg_silent = FALSE;
9876 do_cmdline_cmd(cmd);
9877 if (!called_vim_beep)
9878 {
9879 prepare_assert_error(&ga);
9880 ga_concat(&ga, (char_u *)"command did not beep: ");
9881 ga_concat(&ga, cmd);
9882 assert_error(&ga);
9883 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009884 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009885 }
9886
9887 suppress_errthrow = FALSE;
9888 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009889 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009890}
9891
Bram Moolenaar25190db2019-05-04 15:05:28 +02009892 static void
9893assert_append_cmd_or_arg(garray_T *gap, typval_T *argvars, char_u *cmd)
9894{
9895 char_u *tofree;
9896 char_u numbuf[NUMBUFLEN];
9897
9898 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9899 {
9900 ga_concat(gap, echo_string(&argvars[2], &tofree, numbuf, 0));
9901 vim_free(tofree);
9902 }
9903 else
9904 ga_concat(gap, cmd);
9905}
9906
Bram Moolenaar65a54642018-04-28 16:56:53 +02009907 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908assert_fails(typval_T *argvars)
9909{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009910 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009912 int ret = 0;
Bram Moolenaar7780e5c2019-07-10 22:04:48 +02009913 int save_trylevel = trylevel;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914
Bram Moolenaar7780e5c2019-07-10 22:04:48 +02009915 // trylevel must be zero for a ":throw" command to be considered failed
9916 trylevel = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 called_emsg = FALSE;
9918 suppress_errthrow = TRUE;
9919 emsg_silent = TRUE;
Bram Moolenaar7780e5c2019-07-10 22:04:48 +02009920
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 do_cmdline_cmd(cmd);
9922 if (!called_emsg)
9923 {
9924 prepare_assert_error(&ga);
9925 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar25190db2019-05-04 15:05:28 +02009926 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 assert_error(&ga);
9928 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009929 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 }
9931 else if (argvars[1].v_type != VAR_UNKNOWN)
9932 {
9933 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009934 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935
9936 if (error == NULL
9937 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9938 {
9939 prepare_assert_error(&ga);
9940 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9941 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaar25190db2019-05-04 15:05:28 +02009942 ga_concat(&ga, (char_u *)": ");
9943 assert_append_cmd_or_arg(&ga, argvars, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009944 assert_error(&ga);
9945 ga_clear(&ga);
Bram Moolenaar7780e5c2019-07-10 22:04:48 +02009946 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009947 }
9948 }
9949
Bram Moolenaar7780e5c2019-07-10 22:04:48 +02009950 trylevel = save_trylevel;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 called_emsg = FALSE;
9952 suppress_errthrow = FALSE;
9953 emsg_silent = FALSE;
9954 emsg_on_display = FALSE;
9955 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009956 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957}
9958
9959/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009960 * Append "p[clen]" to "gap", escaping unprintable characters.
9961 * Changes NL to \n, CR to \r, etc.
9962 */
9963 static void
9964ga_concat_esc(garray_T *gap, char_u *p, int clen)
9965{
9966 char_u buf[NUMBUFLEN];
9967
9968 if (clen > 1)
9969 {
9970 mch_memmove(buf, p, clen);
9971 buf[clen] = NUL;
9972 ga_concat(gap, buf);
9973 }
9974 else switch (*p)
9975 {
9976 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9977 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9978 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9979 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9980 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9981 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9982 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9983 default:
9984 if (*p < ' ')
9985 {
9986 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9987 ga_concat(gap, buf);
9988 }
9989 else
9990 ga_append(gap, *p);
9991 break;
9992 }
9993}
9994
9995/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996 * Append "str" to "gap", escaping unprintable characters.
9997 * Changes NL to \n, CR to \r, etc.
9998 */
9999 static void
Bram Moolenaar86576712019-01-25 20:48:33 +010010000ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001{
10002 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +010010003 char_u *s;
10004 int c;
10005 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +010010007 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008
10009 if (str == NULL)
10010 {
10011 ga_concat(gap, (char_u *)"NULL");
10012 return;
10013 }
10014
10015 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +010010016 {
10017 same_len = 1;
10018 s = p;
10019 c = mb_ptr2char_adv(&s);
10020 clen = s - p;
10021 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022 {
Bram Moolenaar86576712019-01-25 20:48:33 +010010023 ++same_len;
10024 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010025 }
Bram Moolenaar86576712019-01-25 20:48:33 +010010026 if (same_len > 20)
10027 {
10028 ga_concat(gap, (char_u *)"\\[");
10029 ga_concat_esc(gap, p, clen);
10030 ga_concat(gap, (char_u *)" occurs ");
10031 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
10032 ga_concat(gap, buf);
10033 ga_concat(gap, (char_u *)" times]");
10034 p = s - 1;
10035 }
10036 else
10037 ga_concat_esc(gap, p, clen);
10038 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010039}
10040
10041/*
10042 * Fill "gap" with information about an assert error.
10043 */
10044 void
10045fill_assert_error(
10046 garray_T *gap,
10047 typval_T *opt_msg_tv,
10048 char_u *exp_str,
10049 typval_T *exp_tv,
10050 typval_T *got_tv,
10051 assert_type_T atype)
10052{
10053 char_u numbuf[NUMBUFLEN];
10054 char_u *tofree;
10055
10056 if (opt_msg_tv->v_type != VAR_UNKNOWN)
10057 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010058 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
10059 vim_free(tofree);
10060 ga_concat(gap, (char_u *)": ");
10061 }
10062
10063 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
10064 ga_concat(gap, (char_u *)"Pattern ");
10065 else if (atype == ASSERT_NOTEQUAL)
10066 ga_concat(gap, (char_u *)"Expected not equal to ");
10067 else
10068 ga_concat(gap, (char_u *)"Expected ");
10069 if (exp_str == NULL)
10070 {
Bram Moolenaar86576712019-01-25 20:48:33 +010010071 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010072 vim_free(tofree);
10073 }
10074 else
Bram Moolenaar86576712019-01-25 20:48:33 +010010075 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010076 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010077 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010078 if (atype == ASSERT_MATCH)
10079 ga_concat(gap, (char_u *)" does not match ");
10080 else if (atype == ASSERT_NOTMATCH)
10081 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010083 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +010010084 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +010010085 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 }
10087}
10088
Bram Moolenaar31988702018-02-13 12:57:42 +010010089/*
10090 * Compare "typ1" and "typ2". Put the result in "typ1".
10091 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010092 int
10093typval_compare(
10094 typval_T *typ1, /* first operand */
10095 typval_T *typ2, /* second operand */
10096 exptype_T type, /* operator */
10097 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +010010098 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010099{
10100 int i;
10101 varnumber_T n1, n2;
10102 char_u *s1, *s2;
10103 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
10104
Bram Moolenaar31988702018-02-13 12:57:42 +010010105 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010106 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010107 /* For "is" a different type always means FALSE, for "notis"
10108 * it means TRUE. */
10109 n1 = (type == TYPE_NEQUAL);
10110 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010111 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
10112 {
10113 if (type_is)
10114 {
10115 n1 = (typ1->v_type == typ2->v_type
10116 && typ1->vval.v_blob == typ2->vval.v_blob);
10117 if (type == TYPE_NEQUAL)
10118 n1 = !n1;
10119 }
10120 else if (typ1->v_type != typ2->v_type
10121 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10122 {
10123 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010124 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010125 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010126 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010127 clear_tv(typ1);
10128 return FAIL;
10129 }
10130 else
10131 {
10132 // Compare two Blobs for being equal or unequal.
10133 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
10134 if (type == TYPE_NEQUAL)
10135 n1 = !n1;
10136 }
10137 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010138 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
10139 {
10140 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010141 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010142 n1 = (typ1->v_type == typ2->v_type
10143 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010144 if (type == TYPE_NEQUAL)
10145 n1 = !n1;
10146 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010147 else if (typ1->v_type != typ2->v_type
10148 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010149 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010150 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010151 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010152 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010153 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010154 clear_tv(typ1);
10155 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010156 }
10157 else
10158 {
Bram Moolenaar31988702018-02-13 12:57:42 +010010159 /* Compare two Lists for being equal or unequal. */
10160 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
10161 ic, FALSE);
10162 if (type == TYPE_NEQUAL)
10163 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010164 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010165 }
Bram Moolenaar31988702018-02-13 12:57:42 +010010166
10167 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
10168 {
10169 if (type_is)
10170 {
10171 n1 = (typ1->v_type == typ2->v_type
10172 && typ1->vval.v_dict == typ2->vval.v_dict);
10173 if (type == TYPE_NEQUAL)
10174 n1 = !n1;
10175 }
10176 else if (typ1->v_type != typ2->v_type
10177 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
10178 {
10179 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010180 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010181 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010182 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010183 clear_tv(typ1);
10184 return FAIL;
10185 }
10186 else
10187 {
10188 /* Compare two Dictionaries for being equal or unequal. */
10189 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
10190 ic, FALSE);
10191 if (type == TYPE_NEQUAL)
10192 n1 = !n1;
10193 }
10194 }
10195
10196 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
10197 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
10198 {
10199 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
10200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010201 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +010010202 clear_tv(typ1);
10203 return FAIL;
10204 }
10205 if ((typ1->v_type == VAR_PARTIAL
10206 && typ1->vval.v_partial == NULL)
10207 || (typ2->v_type == VAR_PARTIAL
10208 && typ2->vval.v_partial == NULL))
10209 /* when a partial is NULL assume not equal */
10210 n1 = FALSE;
10211 else if (type_is)
10212 {
10213 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
10214 /* strings are considered the same if their value is
10215 * the same */
10216 n1 = tv_equal(typ1, typ2, ic, FALSE);
10217 else if (typ1->v_type == VAR_PARTIAL
10218 && typ2->v_type == VAR_PARTIAL)
10219 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
10220 else
10221 n1 = FALSE;
10222 }
10223 else
10224 n1 = tv_equal(typ1, typ2, ic, FALSE);
10225 if (type == TYPE_NEQUAL)
10226 n1 = !n1;
10227 }
10228
10229#ifdef FEAT_FLOAT
10230 /*
10231 * If one of the two variables is a float, compare as a float.
10232 * When using "=~" or "!~", always compare as string.
10233 */
10234 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
10235 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10236 {
10237 float_T f1, f2;
10238
Bram Moolenaar38f08e72019-02-20 22:04:32 +010010239 f1 = tv_get_float(typ1);
10240 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010241 n1 = FALSE;
10242 switch (type)
10243 {
10244 case TYPE_EQUAL: n1 = (f1 == f2); break;
10245 case TYPE_NEQUAL: n1 = (f1 != f2); break;
10246 case TYPE_GREATER: n1 = (f1 > f2); break;
10247 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
10248 case TYPE_SMALLER: n1 = (f1 < f2); break;
10249 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
10250 case TYPE_UNKNOWN:
10251 case TYPE_MATCH:
10252 case TYPE_NOMATCH: break; /* avoid gcc warning */
10253 }
10254 }
10255#endif
10256
10257 /*
10258 * If one of the two variables is a number, compare as a number.
10259 * When using "=~" or "!~", always compare as string.
10260 */
10261 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
10262 && type != TYPE_MATCH && type != TYPE_NOMATCH)
10263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010264 n1 = tv_get_number(typ1);
10265 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010266 switch (type)
10267 {
10268 case TYPE_EQUAL: n1 = (n1 == n2); break;
10269 case TYPE_NEQUAL: n1 = (n1 != n2); break;
10270 case TYPE_GREATER: n1 = (n1 > n2); break;
10271 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
10272 case TYPE_SMALLER: n1 = (n1 < n2); break;
10273 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
10274 case TYPE_UNKNOWN:
10275 case TYPE_MATCH:
10276 case TYPE_NOMATCH: break; /* avoid gcc warning */
10277 }
10278 }
10279 else
10280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010281 s1 = tv_get_string_buf(typ1, buf1);
10282 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +010010283 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
10284 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
10285 else
10286 i = 0;
10287 n1 = FALSE;
10288 switch (type)
10289 {
10290 case TYPE_EQUAL: n1 = (i == 0); break;
10291 case TYPE_NEQUAL: n1 = (i != 0); break;
10292 case TYPE_GREATER: n1 = (i > 0); break;
10293 case TYPE_GEQUAL: n1 = (i >= 0); break;
10294 case TYPE_SMALLER: n1 = (i < 0); break;
10295 case TYPE_SEQUAL: n1 = (i <= 0); break;
10296
10297 case TYPE_MATCH:
10298 case TYPE_NOMATCH:
10299 n1 = pattern_match(s2, s1, ic);
10300 if (type == TYPE_NOMATCH)
10301 n1 = !n1;
10302 break;
10303
10304 case TYPE_UNKNOWN: break; /* avoid gcc warning */
10305 }
10306 }
10307 clear_tv(typ1);
10308 typ1->v_type = VAR_NUMBER;
10309 typ1->vval.v_number = n1;
10310
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010311 return OK;
10312}
10313
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010314 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010315typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +010010316{
10317 char_u *tofree;
10318 char_u numbuf[NUMBUFLEN];
10319 char_u *ret = NULL;
10320
10321 if (arg == NULL)
10322 return vim_strsave((char_u *)"(does not exist)");
10323 ret = tv2string(arg, &tofree, numbuf, 0);
10324 /* Make a copy if we have a value but it's not in allocated memory. */
10325 if (ret != NULL && tofree == NULL)
10326 ret = vim_strsave(ret);
10327 return ret;
10328}
10329
10330 int
10331var_exists(char_u *var)
10332{
10333 char_u *name;
10334 char_u *tofree;
10335 typval_T tv;
10336 int len = 0;
10337 int n = FALSE;
10338
10339 /* get_name_len() takes care of expanding curly braces */
10340 name = var;
10341 len = get_name_len(&var, &tofree, TRUE, FALSE);
10342 if (len > 0)
10343 {
10344 if (tofree != NULL)
10345 name = tofree;
10346 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
10347 if (n)
10348 {
10349 /* handle d.key, l[idx], f(expr) */
10350 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
10351 if (n)
10352 clear_tv(&tv);
10353 }
10354 }
10355 if (*var != NUL)
10356 n = FALSE;
10357
10358 vim_free(tofree);
10359 return n;
10360}
10361
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362#endif /* FEAT_EVAL */
10363
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010365#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366
Bram Moolenaar4f974752019-02-17 17:44:42 +010010367#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368/*
10369 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
10372/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010373 * Get the short path (8.3) for the filename in "fnamep".
10374 * Only works for a valid file name.
10375 * When the path gets longer "fnamep" is changed and the allocated buffer
10376 * is put in "bufp".
10377 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10378 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 */
10380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010381get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010383 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 char_u *newbuf;
10385
10386 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010387 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (l > len - 1)
10389 {
10390 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010391 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 newbuf = vim_strnsave(*fnamep, l);
10393 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395
10396 vim_free(*bufp);
10397 *fnamep = *bufp = newbuf;
10398
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010399 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010400 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 }
10402
10403 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010404 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405}
10406
10407/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010408 * Get the short path (8.3) for the filename in "fname". The converted
10409 * path is returned in "bufp".
10410 *
10411 * Some of the directories specified in "fname" may not exist. This function
10412 * will shorten the existing directories at the beginning of the path and then
10413 * append the remaining non-existing path.
10414 *
10415 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010416 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010417 * bufp - Pointer to an allocated buffer for the filename.
10418 * fnamelen - Length of the filename pointed to by fname
10419 *
10420 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 */
10422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010423shortpath_for_invalid_fname(
10424 char_u **fname,
10425 char_u **bufp,
10426 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010428 char_u *short_fname, *save_fname, *pbuf_unused;
10429 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010431 int old_len, len;
10432 int new_len, sfx_len;
10433 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434
10435 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010436 old_len = *fnamelen;
10437 save_fname = vim_strnsave(*fname, old_len);
10438 pbuf_unused = NULL;
10439 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010441 endp = save_fname + old_len - 1; /* Find the end of the copy */
10442 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010444 /*
10445 * Try shortening the supplied path till it succeeds by removing one
10446 * directory at a time from the tail of the path.
10447 */
10448 len = 0;
10449 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010451 /* go back one path-separator */
10452 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10453 --endp;
10454 if (endp <= save_fname)
10455 break; /* processed the complete path */
10456
10457 /*
10458 * Replace the path separator with a NUL and try to shorten the
10459 * resulting path.
10460 */
10461 ch = *endp;
10462 *endp = 0;
10463 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010464 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010465 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10466 {
10467 retval = FAIL;
10468 goto theend;
10469 }
10470 *endp = ch; /* preserve the string */
10471
10472 if (len > 0)
10473 break; /* successfully shortened the path */
10474
10475 /* failed to shorten the path. Skip the path separator */
10476 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 }
10478
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010479 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010481 /*
10482 * Succeeded in shortening the path. Now concatenate the shortened
10483 * path with the remaining path at the tail.
10484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010486 /* Compute the length of the new path. */
10487 sfx_len = (int)(save_endp - endp) + 1;
10488 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010490 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010492 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010494 /* There is not enough space in the currently allocated string,
10495 * copy it to a buffer big enough. */
10496 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010498 {
10499 retval = FAIL;
10500 goto theend;
10501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 }
10503 else
10504 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010505 /* Transfer short_fname to the main buffer (it's big enough),
10506 * unless get_short_pathname() did its work in-place. */
10507 *fname = *bufp = save_fname;
10508 if (short_fname != save_fname)
10509 vim_strncpy(save_fname, short_fname, len);
10510 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010512
10513 /* concat the not-shortened part of the path */
10514 vim_strncpy(*fname + len, endp, sfx_len);
10515 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010517
10518theend:
10519 vim_free(pbuf_unused);
10520 vim_free(save_fname);
10521
10522 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523}
10524
10525/*
10526 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010527 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 */
10529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010530shortpath_for_partial(
10531 char_u **fnamep,
10532 char_u **bufp,
10533 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 int sepcount, len, tflen;
10536 char_u *p;
10537 char_u *pbuf, *tfname;
10538 int hasTilde;
10539
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010540 /* Count up the path separators from the RHS.. so we know which part
10541 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010543 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (vim_ispathsep(*p))
10545 ++sepcount;
10546
10547 /* Need full path first (use expand_env() to remove a "~/") */
10548 hasTilde = (**fnamep == '~');
10549 if (hasTilde)
10550 pbuf = tfname = expand_env_save(*fnamep);
10551 else
10552 pbuf = tfname = FullName_save(*fnamep, FALSE);
10553
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010554 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010556 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558
10559 if (len == 0)
10560 {
10561 /* Don't have a valid filename, so shorten the rest of the
10562 * path if we can. This CAN give us invalid 8.3 filenames, but
10563 * there's not a lot of point in guessing what it might be.
10564 */
10565 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010566 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569
10570 /* Count the paths backward to find the beginning of the desired string. */
10571 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010572 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010573 if (has_mbyte)
10574 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 if (vim_ispathsep(*p))
10576 {
10577 if (sepcount == 0 || (hasTilde && sepcount == 1))
10578 break;
10579 else
10580 sepcount --;
10581 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (hasTilde)
10584 {
10585 --p;
10586 if (p >= tfname)
10587 *p = '~';
10588 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 }
10591 else
10592 ++p;
10593
10594 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10595 vim_free(*bufp);
10596 *fnamelen = (int)STRLEN(p);
10597 *bufp = pbuf;
10598 *fnamep = p;
10599
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010600 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010602#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603
10604/*
10605 * Adjust a filename, according to a string of modifiers.
10606 * *fnamep must be NUL terminated when called. When returning, the length is
10607 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010608 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 * When there is an error, *fnamep is set to NULL.
10610 */
10611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010613 char_u *src, // string with modifiers
10614 int tilde_file, // "~" is a file name, not $HOME
10615 int *usedlen, // characters after src that are used
10616 char_u **fnamep, // file name so far
10617 char_u **bufp, // buffer for allocated file name or NULL
10618 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620 int valid = 0;
10621 char_u *tail;
10622 char_u *s, *p, *pbuf;
10623 char_u dirname[MAXPATHL];
10624 int c;
10625 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010626#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010627 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 int has_shortname = 0;
10629#endif
10630
10631repeat:
10632 /* ":p" - full path/file_name */
10633 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10634 {
10635 has_fullname = 1;
10636
10637 valid |= VALID_PATH;
10638 *usedlen += 2;
10639
10640 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10641 if ((*fnamep)[0] == '~'
10642#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10643 && ((*fnamep)[1] == '/'
10644# ifdef BACKSLASH_IN_FILENAME
10645 || (*fnamep)[1] == '\\'
10646# endif
10647 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010649 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 )
10651 {
10652 *fnamep = expand_env_save(*fnamep);
10653 vim_free(*bufp); /* free any allocated file name */
10654 *bufp = *fnamep;
10655 if (*fnamep == NULL)
10656 return -1;
10657 }
10658
10659 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010660 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 {
10662 if (vim_ispathsep(*p)
10663 && p[1] == '.'
10664 && (p[2] == NUL
10665 || vim_ispathsep(p[2])
10666 || (p[2] == '.'
10667 && (p[3] == NUL || vim_ispathsep(p[3])))))
10668 break;
10669 }
10670
10671 /* FullName_save() is slow, don't use it when not needed. */
10672 if (*p != NUL || !vim_isAbsName(*fnamep))
10673 {
10674 *fnamep = FullName_save(*fnamep, *p != NUL);
10675 vim_free(*bufp); /* free any allocated file name */
10676 *bufp = *fnamep;
10677 if (*fnamep == NULL)
10678 return -1;
10679 }
10680
Bram Moolenaar4f974752019-02-17 17:44:42 +010010681#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010682# if _WIN32_WINNT >= 0x0500
10683 if (vim_strchr(*fnamep, '~') != NULL)
10684 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010685 // Expand 8.3 filename to full path. Needed to make sure the same
10686 // file does not have two different names.
10687 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10688 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10689 WCHAR buf[_MAX_PATH];
10690
10691 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010692 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010693 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010694 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010695 char_u *p = utf16_to_enc(buf, NULL);
10696
10697 if (p != NULL)
10698 {
10699 vim_free(*bufp); // free any allocated file name
10700 *bufp = *fnamep = p;
10701 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010702 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010703 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010704 }
10705 }
10706# endif
10707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 /* Append a path separator to a directory. */
10709 if (mch_isdir(*fnamep))
10710 {
10711 /* Make room for one or two extra characters. */
10712 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10713 vim_free(*bufp); /* free any allocated file name */
10714 *bufp = *fnamep;
10715 if (*fnamep == NULL)
10716 return -1;
10717 add_pathsep(*fnamep);
10718 }
10719 }
10720
10721 /* ":." - path relative to the current directory */
10722 /* ":~" - path relative to the home directory */
10723 /* ":8" - shortname path - postponed till after */
10724 while (src[*usedlen] == ':'
10725 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10726 {
10727 *usedlen += 2;
10728 if (c == '8')
10729 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010730#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 has_shortname = 1; /* Postpone this. */
10732#endif
10733 continue;
10734 }
10735 pbuf = NULL;
10736 /* Need full path first (use expand_env() to remove a "~/") */
10737 if (!has_fullname)
10738 {
10739 if (c == '.' && **fnamep == '~')
10740 p = pbuf = expand_env_save(*fnamep);
10741 else
10742 p = pbuf = FullName_save(*fnamep, FALSE);
10743 }
10744 else
10745 p = *fnamep;
10746
10747 has_fullname = 0;
10748
10749 if (p != NULL)
10750 {
10751 if (c == '.')
10752 {
10753 mch_dirname(dirname, MAXPATHL);
10754 s = shorten_fname(p, dirname);
10755 if (s != NULL)
10756 {
10757 *fnamep = s;
10758 if (pbuf != NULL)
10759 {
10760 vim_free(*bufp); /* free any allocated file name */
10761 *bufp = pbuf;
10762 pbuf = NULL;
10763 }
10764 }
10765 }
10766 else
10767 {
10768 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10769 /* Only replace it when it starts with '~' */
10770 if (*dirname == '~')
10771 {
10772 s = vim_strsave(dirname);
10773 if (s != NULL)
10774 {
10775 *fnamep = s;
10776 vim_free(*bufp);
10777 *bufp = s;
10778 }
10779 }
10780 }
10781 vim_free(pbuf);
10782 }
10783 }
10784
10785 tail = gettail(*fnamep);
10786 *fnamelen = (int)STRLEN(*fnamep);
10787
10788 /* ":h" - head, remove "/file_name", can be repeated */
10789 /* Don't remove the first "/" or "c:\" */
10790 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10791 {
10792 valid |= VALID_HEAD;
10793 *usedlen += 2;
10794 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010795 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010796 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 *fnamelen = (int)(tail - *fnamep);
10798#ifdef VMS
10799 if (*fnamelen > 0)
10800 *fnamelen += 1; /* the path separator is part of the path */
10801#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010802 if (*fnamelen == 0)
10803 {
10804 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10805 p = vim_strsave((char_u *)".");
10806 if (p == NULL)
10807 return -1;
10808 vim_free(*bufp);
10809 *bufp = *fnamep = tail = p;
10810 *fnamelen = 1;
10811 }
10812 else
10813 {
10814 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010815 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 }
10818
10819 /* ":8" - shortname */
10820 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10821 {
10822 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010823#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 has_shortname = 1;
10825#endif
10826 }
10827
Bram Moolenaar4f974752019-02-17 17:44:42 +010010828#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010829 /*
10830 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 */
10832 if (has_shortname)
10833 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010834 /* Copy the string if it is shortened by :h and when it wasn't copied
10835 * yet, because we are going to change it in place. Avoids changing
10836 * the buffer name for "%:8". */
10837 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
10839 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010840 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 return -1;
10842 vim_free(*bufp);
10843 *bufp = *fnamep = p;
10844 }
10845
10846 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010847 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 if (!has_fullname && !vim_isAbsName(*fnamep))
10849 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010850 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 return -1;
10852 }
10853 else
10854 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010855 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856
Bram Moolenaardc935552011-08-17 15:23:23 +020010857 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010859 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 return -1;
10861
10862 if (l == 0)
10863 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010864 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010866 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 return -1;
10868 }
10869 *fnamelen = l;
10870 }
10871 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010872#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
10874 /* ":t" - tail, just the basename */
10875 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10876 {
10877 *usedlen += 2;
10878 *fnamelen -= (int)(tail - *fnamep);
10879 *fnamep = tail;
10880 }
10881
10882 /* ":e" - extension, can be repeated */
10883 /* ":r" - root, without extension, can be repeated */
10884 while (src[*usedlen] == ':'
10885 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10886 {
10887 /* find a '.' in the tail:
10888 * - for second :e: before the current fname
10889 * - otherwise: The last '.'
10890 */
10891 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10892 s = *fnamep - 2;
10893 else
10894 s = *fnamep + *fnamelen - 1;
10895 for ( ; s > tail; --s)
10896 if (s[0] == '.')
10897 break;
10898 if (src[*usedlen + 1] == 'e') /* :e */
10899 {
10900 if (s > tail)
10901 {
10902 *fnamelen += (int)(*fnamep - (s + 1));
10903 *fnamep = s + 1;
10904#ifdef VMS
10905 /* cut version from the extension */
10906 s = *fnamep + *fnamelen - 1;
10907 for ( ; s > *fnamep; --s)
10908 if (s[0] == ';')
10909 break;
10910 if (s > *fnamep)
10911 *fnamelen = s - *fnamep;
10912#endif
10913 }
10914 else if (*fnamep <= tail)
10915 *fnamelen = 0;
10916 }
10917 else /* :r */
10918 {
10919 if (s > tail) /* remove one extension */
10920 *fnamelen = (int)(s - *fnamep);
10921 }
10922 *usedlen += 2;
10923 }
10924
10925 /* ":s?pat?foo?" - substitute */
10926 /* ":gs?pat?foo?" - global substitute */
10927 if (src[*usedlen] == ':'
10928 && (src[*usedlen + 1] == 's'
10929 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10930 {
10931 char_u *str;
10932 char_u *pat;
10933 char_u *sub;
10934 int sep;
10935 char_u *flags;
10936 int didit = FALSE;
10937
10938 flags = (char_u *)"";
10939 s = src + *usedlen + 2;
10940 if (src[*usedlen + 1] == 'g')
10941 {
10942 flags = (char_u *)"g";
10943 ++s;
10944 }
10945
10946 sep = *s++;
10947 if (sep)
10948 {
10949 /* find end of pattern */
10950 p = vim_strchr(s, sep);
10951 if (p != NULL)
10952 {
10953 pat = vim_strnsave(s, (int)(p - s));
10954 if (pat != NULL)
10955 {
10956 s = p + 1;
10957 /* find end of substitution */
10958 p = vim_strchr(s, sep);
10959 if (p != NULL)
10960 {
10961 sub = vim_strnsave(s, (int)(p - s));
10962 str = vim_strnsave(*fnamep, *fnamelen);
10963 if (sub != NULL && str != NULL)
10964 {
10965 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010966 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 if (s != NULL)
10968 {
10969 *fnamep = s;
10970 *fnamelen = (int)STRLEN(s);
10971 vim_free(*bufp);
10972 *bufp = s;
10973 didit = TRUE;
10974 }
10975 }
10976 vim_free(sub);
10977 vim_free(str);
10978 }
10979 vim_free(pat);
10980 }
10981 }
10982 /* after using ":s", repeat all the modifiers */
10983 if (didit)
10984 goto repeat;
10985 }
10986 }
10987
Bram Moolenaar26df0922014-02-23 23:39:13 +010010988 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10989 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010990 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010991 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010992 if (c != NUL)
10993 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010994 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010995 if (c != NUL)
10996 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010997 if (p == NULL)
10998 return -1;
10999 vim_free(*bufp);
11000 *bufp = *fnamep = p;
11001 *fnamelen = (int)STRLEN(p);
11002 *usedlen += 2;
11003 }
11004
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 return valid;
11006}
11007
11008/*
11009 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011010 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 * "flags" can be "g" to do a global substitute.
11012 * Returns an allocated string, NULL for error.
11013 */
11014 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010011015do_string_sub(
11016 char_u *str,
11017 char_u *pat,
11018 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011019 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010011020 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021{
11022 int sublen;
11023 regmatch_T regmatch;
11024 int i;
11025 int do_all;
11026 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010011027 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 garray_T ga;
11029 char_u *ret;
11030 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010011031 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032
11033 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
11034 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011035 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036
11037 ga_init2(&ga, 1, 200);
11038
11039 do_all = (flags[0] == 'g');
11040
11041 regmatch.rm_ic = p_ic;
11042 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11043 if (regmatch.regprog != NULL)
11044 {
11045 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010011046 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
11048 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010011049 /* Skip empty match except for first match. */
11050 if (regmatch.startp[0] == regmatch.endp[0])
11051 {
11052 if (zero_width == regmatch.startp[0])
11053 {
11054 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020011055 i = MB_PTR2LEN(tail);
11056 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
11057 (size_t)i);
11058 ga.ga_len += i;
11059 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010011060 continue;
11061 }
11062 zero_width = regmatch.startp[0];
11063 }
11064
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 /*
11066 * Get some space for a temporary buffer to do the substitution
11067 * into. It will contain:
11068 * - The text up to where the match is.
11069 * - The substituted text.
11070 * - The text after the match.
11071 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011072 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010011073 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
11075 {
11076 ga_clear(&ga);
11077 break;
11078 }
11079
11080 /* copy the text up to where the match is */
11081 i = (int)(regmatch.startp[0] - tail);
11082 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
11083 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011084 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 + ga.ga_len + i, TRUE, TRUE, FALSE);
11086 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020011087 tail = regmatch.endp[0];
11088 if (*tail == NUL)
11089 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 if (!do_all)
11091 break;
11092 }
11093
11094 if (ga.ga_data != NULL)
11095 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
11096
Bram Moolenaar473de612013-06-08 18:19:48 +020011097 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 }
11099
11100 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
11101 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011102 if (p_cpo == empty_option)
11103 p_cpo = save_cpo;
11104 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011105 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000011106 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
11108 return ret;
11109}
11110
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111 static int
11112filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
11113{
11114 typval_T rettv;
11115 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011117
11118 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
11119 argv[0] = vimvars[VV_KEY].vv_tv;
11120 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010011121 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
11122 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123 if (map)
11124 {
11125 /* map(): replace the list item value */
11126 clear_tv(tv);
11127 rettv.v_lock = 0;
11128 *tv = rettv;
11129 }
11130 else
11131 {
11132 int error = FALSE;
11133
11134 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011135 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011136 clear_tv(&rettv);
11137 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011138 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011139 if (error)
11140 goto theend;
11141 }
11142 retval = OK;
11143theend:
11144 clear_tv(&vimvars[VV_VAL].vv_tv);
11145 return retval;
11146}
11147
11148
11149/*
11150 * Implementation of map() and filter().
11151 */
11152 void
11153filter_map(typval_T *argvars, typval_T *rettv, int map)
11154{
11155 typval_T *expr;
11156 listitem_T *li, *nli;
11157 list_T *l = NULL;
11158 dictitem_T *di;
11159 hashtab_T *ht;
11160 hashitem_T *hi;
11161 dict_T *d = NULL;
11162 typval_T save_val;
11163 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011164 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 int rem;
11166 int todo;
11167 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
11168 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
11169 : N_("filter() argument"));
11170 int save_did_emsg;
11171 int idx = 0;
11172
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011173 if (argvars[0].v_type == VAR_BLOB)
11174 {
11175 if ((b = argvars[0].vval.v_blob) == NULL)
11176 return;
11177 }
11178 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011179 {
11180 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011181 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011182 return;
11183 }
11184 else if (argvars[0].v_type == VAR_DICT)
11185 {
11186 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011187 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011188 return;
11189 }
11190 else
11191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011192 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011193 return;
11194 }
11195
11196 expr = &argvars[1];
11197 /* On type errors, the preceding call has already displayed an error
11198 * message. Avoid a misleading error message for an empty string that
11199 * was not passed as argument. */
11200 if (expr->v_type != VAR_UNKNOWN)
11201 {
11202 prepare_vimvar(VV_VAL, &save_val);
11203
11204 /* We reset "did_emsg" to be able to detect whether an error
11205 * occurred during evaluation of the expression. */
11206 save_did_emsg = did_emsg;
11207 did_emsg = FALSE;
11208
11209 prepare_vimvar(VV_KEY, &save_key);
11210 if (argvars[0].v_type == VAR_DICT)
11211 {
11212 vimvars[VV_KEY].vv_type = VAR_STRING;
11213
11214 ht = &d->dv_hashtab;
11215 hash_lock(ht);
11216 todo = (int)ht->ht_used;
11217 for (hi = ht->ht_array; todo > 0; ++hi)
11218 {
11219 if (!HASHITEM_EMPTY(hi))
11220 {
11221 int r;
11222
11223 --todo;
11224 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011225 if (map && (var_check_lock(di->di_tv.v_lock,
11226 arg_errmsg, TRUE)
11227 || var_check_ro(di->di_flags,
11228 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011229 break;
11230 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
11231 r = filter_map_one(&di->di_tv, expr, map, &rem);
11232 clear_tv(&vimvars[VV_KEY].vv_tv);
11233 if (r == FAIL || did_emsg)
11234 break;
11235 if (!map && rem)
11236 {
11237 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11238 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
11239 break;
11240 dictitem_remove(d, di);
11241 }
11242 }
11243 }
11244 hash_unlock(ht);
11245 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011246 else if (argvars[0].v_type == VAR_BLOB)
11247 {
11248 int i;
11249 typval_T tv;
11250
11251 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11252 for (i = 0; i < b->bv_ga.ga_len; i++)
11253 {
11254 tv.v_type = VAR_NUMBER;
11255 tv.vval.v_number = blob_get(b, i);
11256 vimvars[VV_KEY].vv_nr = idx;
11257 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
11258 break;
11259 if (tv.v_type != VAR_NUMBER)
11260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011261 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010011262 return;
11263 }
11264 tv.v_type = VAR_NUMBER;
11265 blob_set(b, i, tv.vval.v_number);
11266 if (!map && rem)
11267 {
11268 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
11269
11270 mch_memmove(p + idx, p + i + 1,
11271 (size_t)b->bv_ga.ga_len - i - 1);
11272 --b->bv_ga.ga_len;
11273 --i;
11274 }
11275 }
11276 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011277 else
11278 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010011279 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011280 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11281
11282 for (li = l->lv_first; li != NULL; li = nli)
11283 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011284 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 break;
11286 nli = li->li_next;
11287 vimvars[VV_KEY].vv_nr = idx;
11288 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
11289 || did_emsg)
11290 break;
11291 if (!map && rem)
11292 listitem_remove(l, li);
11293 ++idx;
11294 }
11295 }
11296
11297 restore_vimvar(VV_KEY, &save_key);
11298 restore_vimvar(VV_VAL, &save_val);
11299
11300 did_emsg |= save_did_emsg;
11301 }
11302
11303 copy_tv(&argvars[0], rettv);
11304}
11305
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */