blob: 278f3070020a288f10f5b7fce4efc085bc54b937 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000035
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010036#define NAMESPACE_CHAR (char_u *)"abglstvw"
37
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020038static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200174 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
175 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
176 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200177 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100178 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100179 {VV_NAME("false", VAR_SPECIAL), VV_RO},
180 {VV_NAME("true", VAR_SPECIAL), VV_RO},
181 {VV_NAME("null", VAR_SPECIAL), VV_RO},
182 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100183 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200184 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200185 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100195 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200196 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
197 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200198 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200199 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
200 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
201 {VV_NAME("event", VAR_DICT), VV_RO},
202 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000203};
204
205/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000206#define vv_type vv_di.di_tv.v_type
207#define vv_nr vv_di.di_tv.vval.v_number
208#define vv_float vv_di.di_tv.vval.v_float
209#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000210#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200211#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100212#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000213#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000214
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200215static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000216#define vimvarht vimvardict.dv_hashtab
217
Bram Moolenaar9937a052019-06-15 15:45:06 +0200218static void ex_let_const(exarg_T *eap, int is_const);
219static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
221static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static void list_glob_vars(int *first);
223static void list_buf_vars(int *first);
224static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_vim_vars(int *first);
227static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200229static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
230static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
233static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
234static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
235static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000236
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int eval2(char_u **arg, typval_T *rettv, int evaluate);
238static int eval3(char_u **arg, typval_T *rettv, int evaluate);
239static int eval4(char_u **arg, typval_T *rettv, int evaluate);
240static int eval5(char_u **arg, typval_T *rettv, int evaluate);
241static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
242static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000243
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
245static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200250static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100252static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100253static void list_one_var(dictitem_T *v, char *prefix, int *first);
254static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200255static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100256static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100257static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200259/* for VIM_VERSION_ defines */
260#include "version.h"
261
Bram Moolenaare21c1582019-03-02 11:57:09 +0100262/*
263 * Return "n1" divided by "n2", taking care of dividing by zero.
264 */
265 static varnumber_T
266num_divide(varnumber_T n1, varnumber_T n2)
267{
268 varnumber_T result;
269
270 if (n2 == 0) // give an error message?
271 {
272 if (n1 == 0)
273 result = VARNUM_MIN; // similar to NaN
274 else if (n1 < 0)
275 result = -VARNUM_MAX;
276 else
277 result = VARNUM_MAX;
278 }
279 else
280 result = n1 / n2;
281
282 return result;
283}
284
285/*
286 * Return "n1" modulus "n2", taking care of dividing by zero.
287 */
288 static varnumber_T
289num_modulus(varnumber_T n1, varnumber_T n2)
290{
291 // Give an error when n2 is 0?
292 return (n2 == 0) ? 0 : (n1 % n2);
293}
294
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100295
296#if defined(EBCDIC) || defined(PROTO)
297/*
298 * Compare struct fst by function name.
299 */
300 static int
301compare_func_name(const void *s1, const void *s2)
302{
303 struct fst *p1 = (struct fst *)s1;
304 struct fst *p2 = (struct fst *)s2;
305
306 return STRCMP(p1->f_name, p2->f_name);
307}
308
309/*
310 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100311 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100312 * On machines using EBCDIC we have to sort it.
313 */
314 static void
315sortFunctions(void)
316{
317 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
318
319 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
320}
321#endif
322
323
Bram Moolenaar33570922005-01-25 22:26:29 +0000324/*
325 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000326 */
327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000329{
Bram Moolenaar33570922005-01-25 22:26:29 +0000330 int i;
331 struct vimvar *p;
332
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200333 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
334 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200335 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000336 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000338
339 for (i = 0; i < VV_LEN; ++i)
340 {
341 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200342 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100344 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200345 getout(1);
346 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000347 STRCPY(p->vv_di.di_key, p->vv_name);
348 if (p->vv_flags & VV_RO)
349 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
350 else if (p->vv_flags & VV_RO_SBX)
351 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
352 else
353 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000354
355 /* add to v: scope dict, unless the value is not always available */
356 if (p->vv_type != VAR_UNKNOWN)
357 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000359 /* add to compat scope dict */
360 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000361 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100362 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200363 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100364
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000365 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100366 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100367 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100368 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100369 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370
371 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
372 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
373 set_vim_var_nr(VV_NONE, VVAL_NONE);
374 set_vim_var_nr(VV_NULL, VVAL_NULL);
375
Bram Moolenaarf562e722016-07-19 17:25:25 +0200376 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
377 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
378 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
379 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
380 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
381 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
382 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
383 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
384 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
385 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100386 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200387
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200388 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200389
390#ifdef EBCDIC
391 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100392 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200393 */
394 sortFunctions();
395#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000396}
397
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000398#if defined(EXITFREE) || defined(PROTO)
399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100400eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000401{
402 int i;
403 struct vimvar *p;
404
405 for (i = 0; i < VV_LEN; ++i)
406 {
407 p = &vimvars[i];
408 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100409 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000410 else if (p->vv_di.di_tv.v_type == VAR_LIST)
411 {
412 list_unref(p->vv_list);
413 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000414 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000415 }
416 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000417 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000418 hash_clear(&compat_hashtab);
419
Bram Moolenaard9fba312005-06-26 22:34:35 +0000420 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100421# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200422 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100423# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000424
425 /* global variables */
426 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000427
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000428 /* autoloaded script names */
429 ga_clear_strings(&ga_loaded);
430
Bram Moolenaarcca74132013-09-25 21:00:28 +0200431 /* Script-local variables. First clear all the variables and in a second
432 * loop free the scriptvar_T, because a variable in one script might hold
433 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200434 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200436 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200437 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200438 ga_clear(&ga_scripts);
439
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200440 // unreferenced lists and dicts
441 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200442
443 // functions not garbage collected
444 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000445}
446#endif
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 * Set an internal variable to a string value. Creates the variable if it does
451 * not already exist.
452 */
453 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100454set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000456 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000457 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
459 val = vim_strsave(value);
460 if (val != NULL)
461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000463 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000465 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000466 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 }
468 }
469}
470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200472#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000473static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474static char_u *redir_endp = NULL;
475static char_u *redir_varname = NULL;
476
477/*
478 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100479 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000480 * Returns OK if successfully completed the setup. FAIL otherwise.
481 */
482 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100483var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000484{
485 int save_emsg;
486 int err;
487 typval_T tv;
488
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000489 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000490 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100492 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 return FAIL;
494 }
495
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000496 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 redir_varname = vim_strsave(name);
498 if (redir_varname == NULL)
499 return FAIL;
500
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200501 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 if (redir_lval == NULL)
503 {
504 var_redir_stop();
505 return FAIL;
506 }
507
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000508 /* The output is stored in growarray "redir_ga" until redirection ends. */
509 ga_init2(&redir_ga, (int)sizeof(char), 500);
510
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100512 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000513 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
515 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200516 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 if (redir_endp != NULL && *redir_endp != NUL)
518 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100519 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100521 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000522 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000523 var_redir_stop();
524 return FAIL;
525 }
526
527 /* check if we can write to the variable: set it to or append an empty
528 * string */
529 save_emsg = did_emsg;
530 did_emsg = FALSE;
531 tv.v_type = VAR_STRING;
532 tv.vval.v_string = (char_u *)"";
533 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200534 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200536 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200537 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000539 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 if (err)
541 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 var_redir_stop();
544 return FAIL;
545 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546
547 return OK;
548}
549
550/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000551 * Append "value[value_len]" to the variable set by var_redir_start().
552 * The actual appending is postponed until redirection ends, because the value
553 * appended may in fact be the string we write to, changing it may cause freed
554 * memory to be used:
555 * :redir => foo
556 * :let foo
557 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558 */
559 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100560var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000561{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000562 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563
564 if (redir_lval == NULL)
565 return;
566
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000568 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000569 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000570 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000572 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000573 {
574 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000575 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000576 }
577 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579}
580
581/*
582 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000583 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000584 */
585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100586var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000587{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000588 typval_T tv;
589
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200590 if (EVALCMD_BUSY)
591 {
592 redir_lval = NULL;
593 return;
594 }
595
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000596 if (redir_lval != NULL)
597 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000598 /* If there was no error: assign the text to the variable. */
599 if (redir_endp != NULL)
600 {
601 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
602 tv.v_type = VAR_STRING;
603 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200604 /* Call get_lval() again, if it's inside a Dict or List it may
605 * have changed. */
606 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100607 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200608 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200609 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
610 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200611 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000612 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000613
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000614 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100615 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000616
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100619 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100623eval_charconvert(
624 char_u *enc_from,
625 char_u *enc_to,
626 char_u *fname_from,
627 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 int err = FALSE;
630
631 set_vim_var_string(VV_CC_FROM, enc_from, -1);
632 set_vim_var_string(VV_CC_TO, enc_to, -1);
633 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
634 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
635 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
636 err = TRUE;
637 set_vim_var_string(VV_CC_FROM, NULL, -1);
638 set_vim_var_string(VV_CC_TO, NULL, -1);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641
642 if (err)
643 return FAIL;
644 return OK;
645}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 int err = FALSE;
652
653 set_vim_var_string(VV_FNAME_IN, fname, -1);
654 set_vim_var_string(VV_CMDARG, args, -1);
655 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
656 err = TRUE;
657 set_vim_var_string(VV_FNAME_IN, NULL, -1);
658 set_vim_var_string(VV_CMDARG, NULL, -1);
659
660 if (err)
661 {
662 mch_remove(fname);
663 return FAIL;
664 }
665 return OK;
666}
667# endif
668
669# if defined(FEAT_DIFF) || defined(PROTO)
670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_diff(
672 char_u *origfile,
673 char_u *newfile,
674 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 int err = FALSE;
677
678 set_vim_var_string(VV_FNAME_IN, origfile, -1);
679 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
680 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
681 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
682 set_vim_var_string(VV_FNAME_IN, NULL, -1);
683 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
684 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
685}
686
687 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100688eval_patch(
689 char_u *origfile,
690 char_u *difffile,
691 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 int err;
694
695 set_vim_var_string(VV_FNAME_IN, origfile, -1);
696 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
697 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
698 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
699 set_vim_var_string(VV_FNAME_IN, NULL, -1);
700 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
701 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
702}
703# endif
704
705/*
706 * Top level evaluation function, returning a boolean.
707 * Sets "error" to TRUE if there was an error.
708 * Return TRUE or FALSE.
709 */
710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100711eval_to_bool(
712 char_u *arg,
713 int *error,
714 char_u **nextcmd,
715 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200718 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 if (skip)
721 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000722 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
725 {
726 *error = FALSE;
727 if (!skip)
728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100729 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732 }
733 if (skip)
734 --emsg_skip;
735
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200736 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100739/*
740 * Call eval1() and give an error message if not done at a lower level.
741 */
742 static int
743eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
744{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100745 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100746 int ret;
747 int did_emsg_before = did_emsg;
748 int called_emsg_before = called_emsg;
749
750 ret = eval1(arg, rettv, evaluate);
751 if (ret == FAIL)
752 {
753 // Report the invalid expression unless the expression evaluation has
754 // been cancelled due to an aborting error, an interrupt, or an
755 // exception, or we already gave a more specific error.
756 // Also check called_emsg for when using assert_fails().
757 if (!aborting() && did_emsg == did_emsg_before
758 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100759 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100760 }
761 return ret;
762}
763
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200764 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100765eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
766{
767 char_u *s;
768 int dummy;
769 char_u buf[NUMBUFLEN];
770
771 if (expr->v_type == VAR_FUNC)
772 {
773 s = expr->vval.v_string;
774 if (s == NULL || *s == NUL)
775 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200776 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100777 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
778 return FAIL;
779 }
780 else if (expr->v_type == VAR_PARTIAL)
781 {
782 partial_T *partial = expr->vval.v_partial;
783
784 s = partial_name(partial);
785 if (s == NULL || *s == NUL)
786 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200787 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100788 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
789 return FAIL;
790 }
791 else
792 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100793 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100794 if (s == NULL)
795 return FAIL;
796 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100797 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100798 return FAIL;
799 if (*s != NUL) /* check for trailing chars after expr */
800 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200801 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100802 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100803 return FAIL;
804 }
805 }
806 return OK;
807}
808
809/*
810 * Like eval_to_bool() but using a typval_T instead of a string.
811 * Works for string, funcref and partial.
812 */
813 int
814eval_expr_to_bool(typval_T *expr, int *error)
815{
816 typval_T rettv;
817 int res;
818
819 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
820 {
821 *error = TRUE;
822 return FALSE;
823 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100824 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100825 clear_tv(&rettv);
826 return res;
827}
828
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829/*
830 * Top level evaluation function, returning a string. If "skip" is TRUE,
831 * only parsing to "nextcmd" is done, without reporting errors. Return
832 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
833 */
834 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835eval_to_string_skip(
836 char_u *arg,
837 char_u **nextcmd,
838 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 char_u *retval;
842
843 if (skip)
844 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 retval = NULL;
847 else
848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100849 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 if (skip)
853 --emsg_skip;
854
855 return retval;
856}
857
858/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000859 * Skip over an expression at "*pp".
860 * Return FAIL for an error, OK otherwise.
861 */
862 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100863skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000864{
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000866
867 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000868 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000869}
870
871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000873 * When "convert" is TRUE convert a List into a sequence of lines and convert
874 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Return pointer to allocated memory, or NULL for failure.
876 */
877 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100878eval_to_string(
879 char_u *arg,
880 char_u **nextcmd,
881 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882{
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000885 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000886#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000887 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 retval = NULL;
892 else
893 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000894 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000895 {
896 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000897 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200898 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200899 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200900 if (tv.vval.v_list->lv_len > 0)
901 ga_append(&ga, NL);
902 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000903 ga_append(&ga, NUL);
904 retval = (char_u *)ga.ga_data;
905 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000906#ifdef FEAT_FLOAT
907 else if (convert && tv.v_type == VAR_FLOAT)
908 {
909 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
910 retval = vim_strsave(numbuf);
911 }
912#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000913 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100914 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000915 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917
918 return retval;
919}
920
921/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000922 * Call eval_to_string() without using current local variables and using
923 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
925 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100926eval_to_string_safe(
927 char_u *arg,
928 char_u **nextcmd,
929 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200932 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200934 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000935 if (use_sandbox)
936 ++sandbox;
937 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000938 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000939 if (use_sandbox)
940 --sandbox;
941 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200942 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 return retval;
944}
945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946/*
947 * Top level evaluation function, returning a number.
948 * Evaluates "expr" silently.
949 * Returns -1 for an error.
950 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200951 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100952eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
Bram Moolenaar33570922005-01-25 22:26:29 +0000954 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200955 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000956 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 ++emsg_off;
959
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 retval = -1;
962 else
963 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100964 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967 --emsg_off;
968
969 return retval;
970}
971
Bram Moolenaara40058a2005-07-11 22:42:07 +0000972/*
973 * Prepare v: variable "idx" to be used.
974 * Save the current typeval in "save_tv".
975 * When not used yet add the variable to the v: hashtable.
976 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000979{
980 *save_tv = vimvars[idx].vv_tv;
981 if (vimvars[idx].vv_type == VAR_UNKNOWN)
982 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
983}
984
985/*
986 * Restore v: variable "idx" to typeval "save_tv".
987 * When no longer defined, remove the variable from the v: hashtable.
988 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100990restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000991{
992 hashitem_T *hi;
993
Bram Moolenaara40058a2005-07-11 22:42:07 +0000994 vimvars[idx].vv_tv = *save_tv;
995 if (vimvars[idx].vv_type == VAR_UNKNOWN)
996 {
997 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
998 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100999 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001000 else
1001 hash_remove(&vimvarht, hi);
1002 }
1003}
1004
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001005#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001006/*
1007 * Evaluate an expression to a list with suggestions.
1008 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001009 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001010 */
1011 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001012eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001013{
1014 typval_T save_val;
1015 typval_T rettv;
1016 list_T *list = NULL;
1017 char_u *p = skipwhite(expr);
1018
1019 /* Set "v:val" to the bad word. */
1020 prepare_vimvar(VV_VAL, &save_val);
1021 vimvars[VV_VAL].vv_type = VAR_STRING;
1022 vimvars[VV_VAL].vv_str = badword;
1023 if (p_verbose == 0)
1024 ++emsg_off;
1025
1026 if (eval1(&p, &rettv, TRUE) == OK)
1027 {
1028 if (rettv.v_type != VAR_LIST)
1029 clear_tv(&rettv);
1030 else
1031 list = rettv.vval.v_list;
1032 }
1033
1034 if (p_verbose == 0)
1035 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001036 restore_vimvar(VV_VAL, &save_val);
1037
1038 return list;
1039}
1040
1041/*
1042 * "list" is supposed to contain two items: a word and a number. Return the
1043 * word in "pp" and the number as the return value.
1044 * Return -1 if anything isn't right.
1045 * Used to get the good word and score from the eval_spell_expr() result.
1046 */
1047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001049{
1050 listitem_T *li;
1051
1052 li = list->lv_first;
1053 if (li == NULL)
1054 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001055 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001056
1057 li = li->li_next;
1058 if (li == NULL)
1059 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001060 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001061}
1062#endif
1063
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001064/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001065 * Top level evaluation function.
1066 * Returns an allocated typval_T with the result.
1067 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001068 */
1069 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001070eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001071{
1072 typval_T *tv;
1073
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001074 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001075 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001076 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001077
1078 return tv;
1079}
1080
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001083 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001084 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1085 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001089call_vim_function(
1090 char_u *func,
1091 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001092 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001093 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001096 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001099 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001101 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001102 if (ret == FAIL)
1103 clear_tv(rettv);
1104
1105 return ret;
1106}
1107
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001108/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001109 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001110 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001111 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1112 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001113 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001114 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001115call_func_retnr(
1116 char_u *func,
1117 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001118 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001119{
1120 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001121 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001122
Bram Moolenaarded27a12018-08-01 19:06:03 +02001123 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001124 return -1;
1125
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001126 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001127 clear_tv(&rettv);
1128 return retval;
1129}
1130
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001131#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001132 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1133
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001134# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001135/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001136 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001137 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001138 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1139 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001140 */
1141 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001142call_func_retstr(
1143 char_u *func,
1144 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001145 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146{
1147 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001148 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001149
Bram Moolenaarded27a12018-08-01 19:06:03 +02001150 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001151 return NULL;
1152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001153 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001154 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 return retval;
1156}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001157# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001159/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001160 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001161 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1162 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001163 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001164 */
1165 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001166call_func_retlist(
1167 char_u *func,
1168 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001169 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001170{
1171 typval_T rettv;
1172
Bram Moolenaarded27a12018-08-01 19:06:03 +02001173 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001174 return NULL;
1175
1176 if (rettv.v_type != VAR_LIST)
1177 {
1178 clear_tv(&rettv);
1179 return NULL;
1180 }
1181
1182 return rettv.vval.v_list;
1183}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185
Bram Moolenaar05159a02005-02-26 23:04:13 +00001186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#ifdef FEAT_FOLDING
1188/*
1189 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1190 * it in "*cp". Doesn't give error messages.
1191 */
1192 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001193eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
Bram Moolenaar33570922005-01-25 22:26:29 +00001195 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001196 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001198 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1199 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001202 if (use_sandbox)
1203 ++sandbox;
1204 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 retval = 0;
1208 else
1209 {
1210 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 if (tv.v_type == VAR_NUMBER)
1212 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001213 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 retval = 0;
1215 else
1216 {
1217 /* If the result is a string, check if there is a non-digit before
1218 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (!VIM_ISDIGIT(*s) && *s != '-')
1221 *cp = *s++;
1222 retval = atol((char *)s);
1223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
1226 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 if (use_sandbox)
1228 --sandbox;
1229 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001231 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232}
1233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001236 * Get a list of lines from a HERE document. The here document is a list of
1237 * lines surrounded by a marker.
1238 * cmd << {marker}
1239 * {line1}
1240 * {line2}
1241 * ....
1242 * {marker}
1243 *
1244 * The {marker} is a string. If the optional 'trim' word is supplied before the
1245 * marker, then the leading indentation before the lines (matching the
1246 * indentation in the 'cmd' line) is stripped.
1247 * Returns a List with {lines} or NULL.
1248 */
1249 static list_T *
1250heredoc_get(exarg_T *eap, char_u *cmd)
1251{
1252 char_u *theline;
1253 char_u *marker;
1254 list_T *l;
1255 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001256 int marker_indent_len = 0;
1257 int text_indent_len = 0;
1258 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001259
1260 if (eap->getline == NULL)
1261 {
1262 emsg(_("E991: cannot use =<< here"));
1263 return NULL;
1264 }
1265
1266 // Check for the optional 'trim' word before the marker
1267 cmd = skipwhite(cmd);
1268 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1269 {
1270 cmd = skipwhite(cmd + 4);
1271
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001272 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001273 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001274 // the first line after the :let command line. To find the end marker
1275 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001276 p = *eap->cmdlinep;
1277 while (VIM_ISWHITE(*p))
1278 {
1279 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001280 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001281 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001282 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001283 }
1284
Bram Moolenaar24582002019-07-21 14:14:26 +02001285 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001286 if (*cmd != NUL && *cmd != '"')
1287 {
1288 marker = skipwhite(cmd);
1289 p = skiptowhite(marker);
1290 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1291 {
1292 emsg(_(e_trailing));
1293 return NULL;
1294 }
1295 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001296 if (vim_islower(*marker))
1297 {
1298 emsg(_("E221: Marker cannot start with lower case letter"));
1299 return NULL;
1300 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001301 }
1302 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001303 {
1304 emsg(_("E172: Missing marker"));
1305 return NULL;
1306 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001307
1308 l = list_alloc();
1309 if (l == NULL)
1310 return NULL;
1311
1312 for (;;)
1313 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001314 int mi = 0;
1315 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001316
Bram Moolenaare96a2492019-06-25 04:12:16 +02001317 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001318 if (theline == NULL)
1319 {
1320 semsg(_("E990: Missing end marker '%s'"), marker);
1321 break;
1322 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001323
1324 // with "trim": skip the indent matching the :let line to find the
1325 // marker
1326 if (marker_indent_len > 0
1327 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1328 mi = marker_indent_len;
1329 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001330 {
1331 vim_free(theline);
1332 break;
1333 }
1334
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001335 if (text_indent_len == -1 && *theline != NUL)
1336 {
1337 // set the text indent from the first line.
1338 p = theline;
1339 text_indent_len = 0;
1340 while (VIM_ISWHITE(*p))
1341 {
1342 p++;
1343 text_indent_len++;
1344 }
1345 text_indent = vim_strnsave(theline, text_indent_len);
1346 }
1347 // with "trim": skip the indent matching the first line
1348 if (text_indent != NULL)
1349 for (ti = 0; ti < text_indent_len; ++ti)
1350 if (theline[ti] != text_indent[ti])
1351 break;
1352
1353 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001354 break;
1355 vim_free(theline);
1356 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001357 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001358
1359 return l;
1360}
1361
1362/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 * ":let" list all variable values
1364 * ":let var1 var2" list variable values
1365 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 * ":let var += expr" assignment command.
1367 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001368 * ":let var *= expr" assignment command.
1369 * ":let var /= expr" assignment command.
1370 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001372 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001376ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001378 ex_let_const(eap, FALSE);
1379}
1380
1381/*
1382 * ":const" list all variable values
1383 * ":const var1 var2" list variable values
1384 * ":const var = expr" assignment command.
1385 * ":const [var1, var2] = expr" unpack list.
1386 */
1387 void
1388ex_const(exarg_T *eap)
1389{
1390 ex_let_const(eap, TRUE);
1391}
1392
1393 static void
1394ex_let_const(exarg_T *eap, int is_const)
1395{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001398 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001400 int var_count = 0;
1401 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001403 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001404 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001405 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaardb552d602006-03-23 22:59:57 +00001407 argend = skip_var_list(arg, &var_count, &semicolon);
1408 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001409 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001410 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001411 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001412 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001413 concat = expr[0] == '.'
1414 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1415 || (expr[1] == '.' && expr[2] == '='));
1416 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1417 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001419 /*
1420 * ":let" without "=": list variables
1421 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001423 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001424 else if (expr[0] == '.')
1425 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001426 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001428 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001432 list_glob_vars(&first);
1433 list_buf_vars(&first);
1434 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001435 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001436 list_script_vars(&first);
1437 list_func_vars(&first);
1438 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 eap->nextcmd = check_nextcmd(arg);
1441 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001442 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1443 {
1444 list_T *l;
1445
1446 // HERE document
1447 l = heredoc_get(eap, expr + 3);
1448 if (l != NULL)
1449 {
1450 rettv_list_set(&rettv, l);
1451 op[0] = '=';
1452 op[1] = NUL;
1453 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001454 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001455 clear_tv(&rettv);
1456 }
1457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 else
1459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 op[0] = '=';
1461 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001462 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001464 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001465 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001466 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001467 if (expr[0] == '.' && expr[1] == '.') // ..=
1468 ++expr;
1469 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001470 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001472 else
1473 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001474
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (eap->skip)
1476 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (eap->skip)
1479 {
1480 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 --emsg_skip;
1483 }
1484 else if (i != FAIL)
1485 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001486 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001487 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 }
1490 }
1491}
1492
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001493/*
1494 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1495 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001496 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1498 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499 * Returns OK or FAIL;
1500 */
1501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502ex_let_vars(
1503 char_u *arg_start,
1504 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001505 int copy, // copy values from "tv", don't move
1506 int semicolon, // from skip_var_list()
1507 int var_count, // from skip_var_list()
1508 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001509 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001510{
1511 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001512 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001514 listitem_T *item;
1515 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001516
1517 if (*arg != '[')
1518 {
1519 /*
1520 * ":let var = expr" or ":for var in list"
1521 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001522 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001523 return FAIL;
1524 return OK;
1525 }
1526
1527 /*
1528 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1529 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001530 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001532 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533 return FAIL;
1534 }
1535
1536 i = list_len(l);
1537 if (semicolon == 0 && var_count < i)
1538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001539 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001540 return FAIL;
1541 }
1542 if (var_count - semicolon > i)
1543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001545 return FAIL;
1546 }
1547
1548 item = l->lv_first;
1549 while (*arg != ']')
1550 {
1551 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001552 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001553 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 item = item->li_next;
1555 if (arg == NULL)
1556 return FAIL;
1557
1558 arg = skipwhite(arg);
1559 if (*arg == ';')
1560 {
1561 /* Put the rest of the list (may be empty) in the var after ';'.
1562 * Create a new list for this. */
1563 l = list_alloc();
1564 if (l == NULL)
1565 return FAIL;
1566 while (item != NULL)
1567 {
1568 list_append_tv(l, &item->li_tv);
1569 item = item->li_next;
1570 }
1571
1572 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001573 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 ltv.vval.v_list = l;
1575 l->lv_refcount = 1;
1576
Bram Moolenaar9937a052019-06-15 15:45:06 +02001577 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001578 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 clear_tv(&ltv);
1580 if (arg == NULL)
1581 return FAIL;
1582 break;
1583 }
1584 else if (*arg != ',' && *arg != ']')
1585 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001586 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001587 return FAIL;
1588 }
1589 }
1590
1591 return OK;
1592}
1593
1594/*
1595 * Skip over assignable variable "var" or list of variables "[var, var]".
1596 * Used for ":let varvar = expr" and ":for varvar in expr".
1597 * For "[var, var]" increment "*var_count" for each variable.
1598 * for "[var, var; var]" set "semicolon".
1599 * Return NULL for an error.
1600 */
1601 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602skip_var_list(
1603 char_u *arg,
1604 int *var_count,
1605 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606{
1607 char_u *p, *s;
1608
1609 if (*arg == '[')
1610 {
1611 /* "[var, var]": find the matching ']'. */
1612 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614 {
1615 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1616 s = skip_var_one(p);
1617 if (s == p)
1618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001619 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 return NULL;
1621 }
1622 ++*var_count;
1623
1624 p = skipwhite(s);
1625 if (*p == ']')
1626 break;
1627 else if (*p == ';')
1628 {
1629 if (*semicolon == 1)
1630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001631 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 return NULL;
1633 }
1634 *semicolon = 1;
1635 }
1636 else if (*p != ',')
1637 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001638 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 return NULL;
1640 }
1641 }
1642 return p + 1;
1643 }
1644 else
1645 return skip_var_one(arg);
1646}
1647
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001648/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001649 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001650 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001651 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001653skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001655 if (*arg == '@' && arg[1] != NUL)
1656 return arg + 2;
1657 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1658 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659}
1660
Bram Moolenaara7043832005-01-21 11:56:39 +00001661/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 * List variables for hashtab "ht" with prefix "prefix".
1663 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001664 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001666list_hashtable_vars(
1667 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001668 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001669 int empty,
1670 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001671{
Bram Moolenaar33570922005-01-25 22:26:29 +00001672 hashitem_T *hi;
1673 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001674 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001675 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001677 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001678 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1679 {
1680 if (!HASHITEM_EMPTY(hi))
1681 {
1682 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001684
1685 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001686 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1687 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001688 if (message_filtered(buf))
1689 continue;
1690
Bram Moolenaar33570922005-01-25 22:26:29 +00001691 if (empty || di->di_tv.v_type != VAR_STRING
1692 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001693 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 }
1695 }
1696}
1697
1698/*
1699 * List global variables.
1700 */
1701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001702list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001703{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001704 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001705}
1706
1707/*
1708 * List buffer variables.
1709 */
1710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001711list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001712{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001713 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001714}
1715
1716/*
1717 * List window variables.
1718 */
1719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001721{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001722 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001723}
1724
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001725/*
1726 * List tab page variables.
1727 */
1728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001729list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001730{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001731 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001732}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001733
Bram Moolenaara7043832005-01-21 11:56:39 +00001734/*
1735 * List Vim variables.
1736 */
1737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001738list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001739{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001740 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741}
1742
1743/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001744 * List script-local variables, if there is a script.
1745 */
1746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001748{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001749 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1750 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001751 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001752}
1753
1754/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 * List variables in "arg".
1756 */
1757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759{
1760 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001761 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001763 char_u *name_start;
1764 char_u *arg_subsc;
1765 char_u *tofree;
1766 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767
1768 while (!ends_excmd(*arg) && !got_int)
1769 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001770 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001772 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001773 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001774 {
1775 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001776 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001777 break;
1778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001782 /* get_name_len() takes care of expanding curly braces */
1783 name_start = name = arg;
1784 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1785 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001787 /* This is mainly to keep test 49 working: when expanding
1788 * curly braces fails overrule the exception error message. */
1789 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001791 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001792 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001793 break;
1794 }
1795 error = TRUE;
1796 }
1797 else
1798 {
1799 if (tofree != NULL)
1800 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001801 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 else
1804 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001805 /* handle d.key, l[idx], f(expr) */
1806 arg_subsc = arg;
1807 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001808 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001811 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001812 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001813 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001814 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001815 case 'g': list_glob_vars(first); break;
1816 case 'b': list_buf_vars(first); break;
1817 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001818 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001819 case 'v': list_vim_vars(first); break;
1820 case 's': list_script_vars(first); break;
1821 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001822 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001823 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001824 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001825 }
1826 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 {
1828 char_u numbuf[NUMBUFLEN];
1829 char_u *tf;
1830 int c;
1831 char_u *s;
1832
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001833 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001834 c = *arg;
1835 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001836 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001837 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 tv.v_type,
1839 s == NULL ? (char_u *)"" : s,
1840 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841 *arg = c;
1842 vim_free(tf);
1843 }
1844 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 }
1847 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848
1849 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851
1852 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 }
1854
1855 return arg;
1856}
1857
1858/*
1859 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1860 * Returns a pointer to the char just after the var name.
1861 * Returns NULL if there is an error.
1862 */
1863 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001864ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001865 char_u *arg, // points to variable name
1866 typval_T *tv, // value to assign to variable
1867 int copy, // copy value from "tv"
1868 int is_const, // lock variable for const
1869 char_u *endchars, // valid chars after variable name or NULL
1870 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871{
1872 int c1;
1873 char_u *name;
1874 char_u *p;
1875 char_u *arg_end = NULL;
1876 int len;
1877 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879
1880 /*
1881 * ":let $VAR = expr": Set environment variable.
1882 */
1883 if (*arg == '$')
1884 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001885 if (is_const)
1886 {
1887 emsg(_("E996: Cannot lock an environment variable"));
1888 return NULL;
1889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 /* Find the end of the name. */
1891 ++arg;
1892 name = arg;
1893 len = get_env_len(&arg);
1894 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 else
1897 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001898 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001903 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 {
1905 c1 = name[len];
1906 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001907 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001908 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 {
1910 int mustfree = FALSE;
1911 char_u *s = vim_getenv(name, &mustfree);
1912
1913 if (s != NULL)
1914 {
1915 p = tofree = concat_str(s, p);
1916 if (mustfree)
1917 vim_free(s);
1918 }
1919 }
1920 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001921 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001923 if (STRICMP(name, "HOME") == 0)
1924 init_homedir();
1925 else if (didset_vim && STRICMP(name, "VIM") == 0)
1926 didset_vim = FALSE;
1927 else if (didset_vimruntime
1928 && STRICMP(name, "VIMRUNTIME") == 0)
1929 didset_vimruntime = FALSE;
1930 arg_end = arg;
1931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 }
1935 }
1936 }
1937
1938 /*
1939 * ":let &option = expr": Set option value.
1940 * ":let &l:option = expr": Set local option value.
1941 * ":let &g:option = expr": Set global option value.
1942 */
1943 else if (*arg == '&')
1944 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001945 if (is_const)
1946 {
1947 emsg(_("E996: Cannot lock an option"));
1948 return NULL;
1949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 /* Find the end of the name. */
1951 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 if (p == NULL || (endchars != NULL
1953 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001954 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 else
1956 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 long n;
1958 int opt_type;
1959 long numval;
1960 char_u *stringval = NULL;
1961 char_u *s;
1962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 c1 = *p;
1964 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001966 n = (long)tv_get_number(tv);
1967 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 {
1970 opt_type = get_option_value(arg, &numval,
1971 &stringval, opt_flags);
1972 if ((opt_type == 1 && *op == '.')
1973 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001976 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 else
1979 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001980 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001982 switch (*op)
1983 {
1984 case '+': n = numval + n; break;
1985 case '-': n = numval - n; break;
1986 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001987 case '/': n = (long)num_divide(numval, n); break;
1988 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001991 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 {
1993 s = concat_str(stringval, s);
1994 vim_free(stringval);
1995 stringval = s;
1996 }
1997 }
1998 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001999 if (s != NULL)
2000 {
2001 set_option_value(arg, n, s, opt_flags);
2002 arg_end = p;
2003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 }
2007 }
2008
2009 /*
2010 * ":let @r = expr": Set register contents.
2011 */
2012 else if (*arg == '@')
2013 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002014 if (is_const)
2015 {
2016 emsg(_("E996: Cannot lock a register"));
2017 return NULL;
2018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002020 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 else
2026 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002027 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 char_u *s;
2029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002030 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002031 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002033 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034 if (s != NULL)
2035 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002036 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037 vim_free(s);
2038 }
2039 }
2040 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002043 arg_end = arg + 1;
2044 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002045 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 }
2047 }
2048
2049 /*
2050 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002053 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002055 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002057 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002061 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 else
2063 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002064 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 arg_end = p;
2066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 }
2070
2071 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073
2074 return arg_end;
2075}
2076
2077/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 * Get an lval: variable, Dict item or List item that can be assigned a value
2079 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2080 * "name.key", "name.key[expr]" etc.
2081 * Indexing only works if "name" is an existing List or Dictionary.
2082 * "name" points to the start of the name.
2083 * If "rettv" is not NULL it points to the value to be assigned.
2084 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2085 * wrong; must end in space or cmd separator.
2086 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002087 * flags:
2088 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002089 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002090 * GLV_NO_AUTOLOAD: do not use script autoloading
2091 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002097get_lval(
2098 char_u *name,
2099 typval_T *rettv,
2100 lval_T *lp,
2101 int unlet,
2102 int skip,
2103 int flags, /* GLV_ values */
2104 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 char_u *expr_start, *expr_end;
2108 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 dictitem_T *v;
2110 typval_T var1;
2111 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002117 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121
2122 if (skip)
2123 {
2124 /* When skipping just find the end of the name. */
2125 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002126 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 }
2128
2129 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002130 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 if (expr_start != NULL)
2132 {
2133 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002134 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 && *p != '[' && *p != '.')
2136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002137 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 return NULL;
2139 }
2140
2141 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2142 if (lp->ll_exp_name == NULL)
2143 {
2144 /* Report an invalid expression in braces, unless the
2145 * expression evaluation has been cancelled due to an
2146 * aborting error, an interrupt, or an exception. */
2147 if (!aborting() && !quiet)
2148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002149 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002150 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
2152 }
2153 }
2154 lp->ll_name = lp->ll_exp_name;
2155 }
2156 else
2157 lp->ll_name = name;
2158
2159 /* Without [idx] or .key we are done. */
2160 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2161 return p;
2162
2163 cc = *p;
2164 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002165 /* Only pass &ht when we would write to the variable, it prevents autoload
2166 * as well. */
2167 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2168 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 if (v == NULL)
2173 return NULL;
2174
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 /*
2176 * Loop until no more [idx] or .key is following.
2177 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002179 var1.v_type = VAR_UNKNOWN;
2180 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2184 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002185 && lp->ll_tv->vval.v_dict != NULL)
2186 && !(lp->ll_tv->v_type == VAR_BLOB
2187 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002190 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002199
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 len = -1;
2201 if (*p == '.')
2202 {
2203 key = p + 1;
2204 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2205 ;
2206 if (len == 0)
2207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 }
2212 p = key + len;
2213 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002214 else
2215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002217 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 if (*p == ':')
2219 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002220 else
2221 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002222 empty1 = FALSE;
2223 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002225 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 {
2227 /* not a number or string */
2228 clear_tv(&var1);
2229 return NULL;
2230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 }
2232
2233 /* Optionally get the second index [ :expr]. */
2234 if (*p == ':')
2235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002237 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002240 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002242 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002243 if (rettv != NULL
2244 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002245 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002246 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002247 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002250 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002251 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 }
2254 p = skipwhite(p + 1);
2255 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 else
2258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002260 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2261 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002262 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002264 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002265 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002266 {
2267 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002268 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 clear_tv(&var2);
2270 return NULL;
2271 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002274 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002277
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002282 clear_tv(&var1);
2283 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002285 }
2286
2287 /* Skip to past ']'. */
2288 ++p;
2289 }
2290
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002292 {
2293 if (len == -1)
2294 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002296 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002297 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002299 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002301 }
2302 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 lp->ll_list = NULL;
2304 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002305 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002306
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002307 /* When assigning to a scope dictionary check that a function and
2308 * variable name is valid (only variable name unless it is l: or
2309 * g: dictionary). Disallow overwriting a builtin function. */
2310 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002311 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002312 int prevval;
2313 int wrong;
2314
2315 if (len != -1)
2316 {
2317 prevval = key[len];
2318 key[len] = NUL;
2319 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002320 else
2321 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002322 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2323 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002324 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002325 || !valid_varname(key);
2326 if (len != -1)
2327 key[len] = prevval;
2328 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002329 return NULL;
2330 }
2331
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002333 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002334 // Can't add "v:" or "a:" variable.
2335 if (lp->ll_dict == &vimvardict
2336 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002338 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002339 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002340 return NULL;
2341 }
2342
Bram Moolenaar31b81602019-02-10 22:14:27 +01002343 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 }
2351 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002355 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 p = NULL;
2358 break;
2359 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002360 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002361 else if ((flags & GLV_READ_ONLY) == 0
2362 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002363 {
2364 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002365 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002366 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002367
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002368 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002371 else if (lp->ll_tv->v_type == VAR_BLOB)
2372 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002373 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2374
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002375 /*
2376 * Get the number and item for the only or first index of the List.
2377 */
2378 if (empty1)
2379 lp->ll_n1 = 0;
2380 else
2381 // is number or string
2382 lp->ll_n1 = (long)tv_get_number(&var1);
2383 clear_tv(&var1);
2384
2385 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002386 || lp->ll_n1 > bloblen
2387 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002388 {
2389 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002391 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002392 return NULL;
2393 }
2394 if (lp->ll_range && !lp->ll_empty2)
2395 {
2396 lp->ll_n2 = (long)tv_get_number(&var2);
2397 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002398 if (lp->ll_n2 < 0
2399 || lp->ll_n2 >= bloblen
2400 || lp->ll_n2 < lp->ll_n1)
2401 {
2402 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002403 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002404 return NULL;
2405 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002406 }
2407 lp->ll_blob = lp->ll_tv->vval.v_blob;
2408 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002409 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002410 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 else
2412 {
2413 /*
2414 * Get the number and item for the only or first index of the List.
2415 */
2416 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002419 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002420 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002421 clear_tv(&var1);
2422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 lp->ll_list = lp->ll_tv->vval.v_list;
2425 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2426 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002428 if (lp->ll_n1 < 0)
2429 {
2430 lp->ll_n1 = 0;
2431 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2432 }
2433 }
2434 if (lp->ll_li == NULL)
2435 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002436 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002437 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 }
2441
2442 /*
2443 * May need to find the item or absolute index for the second
2444 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 * When no index given: "lp->ll_empty2" is TRUE.
2446 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002450 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002451 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002457 {
2458 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002459 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002461 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
2464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2466 if (lp->ll_n1 < 0)
2467 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2468 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002469 {
2470 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002471 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002473 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474 }
2475
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 return p;
2482}
2483
2484/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002485 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002488clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489{
2490 vim_free(lp->ll_exp_name);
2491 vim_free(lp->ll_newkey);
2492}
2493
2494/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002495 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002497 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2498 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 */
2500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002501set_var_lval(
2502 lval_T *lp,
2503 char_u *endp,
2504 typval_T *rettv,
2505 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002506 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002507 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508{
2509 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 listitem_T *ri;
2511 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512
2513 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002515 cc = *endp;
2516 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002517 if (lp->ll_blob != NULL)
2518 {
2519 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002521 if (op != NULL && *op != '=')
2522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002523 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002524 return;
2525 }
2526
2527 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2528 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002529 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002530
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002531 if (lp->ll_empty2)
2532 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2533
2534 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002536 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002537 return;
2538 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002539 if (lp->ll_empty2)
2540 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002541
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002542 ir = 0;
2543 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2544 blob_set(lp->ll_blob, il,
2545 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002546 }
2547 else
2548 {
2549 val = (int)tv_get_number_chk(rettv, &error);
2550 if (!error)
2551 {
2552 garray_T *gap = &lp->ll_blob->bv_ga;
2553
2554 // Allow for appending a byte. Setting a byte beyond
2555 // the end is an error otherwise.
2556 if (lp->ll_n1 < gap->ga_len
2557 || (lp->ll_n1 == gap->ga_len
2558 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2559 {
2560 blob_set(lp->ll_blob, lp->ll_n1, val);
2561 if (lp->ll_n1 == gap->ga_len)
2562 ++gap->ga_len;
2563 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002564 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002565 }
2566 }
2567 }
2568 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002570 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571
Bram Moolenaar9937a052019-06-15 15:45:06 +02002572 if (is_const)
2573 {
2574 emsg(_(e_cannot_mod));
2575 *endp = cc;
2576 return;
2577 }
2578
Bram Moolenaarff697e62019-02-12 22:28:33 +01002579 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002580 di = NULL;
2581 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2582 &tv, &di, TRUE, FALSE) == OK)
2583 {
2584 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002585 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2586 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002587 && tv_op(&tv, rettv, op) == OK)
2588 set_var(lp->ll_name, &tv, FALSE);
2589 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002592 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002593 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002594 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002596 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002597 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002598 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002599 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 else if (lp->ll_range)
2601 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002602 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002603 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002604
Bram Moolenaar9937a052019-06-15 15:45:06 +02002605 if (is_const)
2606 {
2607 emsg(_("E996: Cannot lock a range"));
2608 return;
2609 }
2610
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002611 /*
2612 * Check whether any of the list items is locked
2613 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002614 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002615 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002616 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002617 return;
2618 ri = ri->li_next;
2619 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2620 break;
2621 ll_li = ll_li->li_next;
2622 ++ll_n1;
2623 }
2624
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 /*
2626 * Assign the List values to the list items.
2627 */
2628 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 if (op != NULL && *op != '=')
2631 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2632 else
2633 {
2634 clear_tv(&lp->ll_li->li_tv);
2635 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 ri = ri->li_next;
2638 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2639 break;
2640 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002643 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 ri = NULL;
2646 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 lp->ll_li = lp->ll_li->li_next;
2650 ++lp->ll_n1;
2651 }
2652 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002653 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654 else if (lp->ll_empty2
2655 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 }
2659 else
2660 {
2661 /*
2662 * Assign to a List or Dictionary item.
2663 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002664 if (is_const)
2665 {
2666 emsg(_("E996: Cannot lock a list or dict"));
2667 return;
2668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (lp->ll_newkey != NULL)
2670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 if (op != NULL && *op != '=')
2672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002673 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674 return;
2675 }
2676
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002678 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (di == NULL)
2680 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002681 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2682 {
2683 vim_free(di);
2684 return;
2685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 else if (op != NULL && *op != '=')
2689 {
2690 tv_op(lp->ll_tv, rettv, op);
2691 return;
2692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 /*
2697 * Assign the value to the variable or list item.
2698 */
2699 if (copy)
2700 copy_tv(rettv, lp->ll_tv);
2701 else
2702 {
2703 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002704 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706 }
2707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002708}
2709
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002711 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2712 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 * Returns OK or FAIL.
2714 */
2715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002716tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002718 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 char_u numbuf[NUMBUFLEN];
2720 char_u *s;
2721
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002722 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2723 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2724 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 {
2726 switch (tv1->v_type)
2727 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002728 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002729 case VAR_DICT:
2730 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002731 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002732 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002733 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002734 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 break;
2736
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002737 case VAR_BLOB:
2738 if (*op != '+' || tv2->v_type != VAR_BLOB)
2739 break;
2740 // BLOB += BLOB
2741 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2742 {
2743 blob_T *b1 = tv1->vval.v_blob;
2744 blob_T *b2 = tv2->vval.v_blob;
2745 int i, len = blob_len(b2);
2746 for (i = 0; i < len; i++)
2747 ga_append(&b1->bv_ga, blob_get(b2, i));
2748 }
2749 return OK;
2750
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002751 case VAR_LIST:
2752 if (*op != '+' || tv2->v_type != VAR_LIST)
2753 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002754 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2756 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2757 return OK;
2758
2759 case VAR_NUMBER:
2760 case VAR_STRING:
2761 if (tv2->v_type == VAR_LIST)
2762 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002763 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002765 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002766 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002767#ifdef FEAT_FLOAT
2768 if (tv2->v_type == VAR_FLOAT)
2769 {
2770 float_T f = n;
2771
Bram Moolenaarff697e62019-02-12 22:28:33 +01002772 if (*op == '%')
2773 break;
2774 switch (*op)
2775 {
2776 case '+': f += tv2->vval.v_float; break;
2777 case '-': f -= tv2->vval.v_float; break;
2778 case '*': f *= tv2->vval.v_float; break;
2779 case '/': f /= tv2->vval.v_float; break;
2780 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002781 clear_tv(tv1);
2782 tv1->v_type = VAR_FLOAT;
2783 tv1->vval.v_float = f;
2784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002786#endif
2787 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002788 switch (*op)
2789 {
2790 case '+': n += tv_get_number(tv2); break;
2791 case '-': n -= tv_get_number(tv2); break;
2792 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002793 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2794 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002795 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002796 clear_tv(tv1);
2797 tv1->v_type = VAR_NUMBER;
2798 tv1->vval.v_number = n;
2799 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002800 }
2801 else
2802 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002803 if (tv2->v_type == VAR_FLOAT)
2804 break;
2805
Bram Moolenaarff697e62019-02-12 22:28:33 +01002806 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002807 s = tv_get_string(tv1);
2808 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 clear_tv(tv1);
2810 tv1->v_type = VAR_STRING;
2811 tv1->vval.v_string = s;
2812 }
2813 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002814
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002815 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002816#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002817 {
2818 float_T f;
2819
Bram Moolenaarff697e62019-02-12 22:28:33 +01002820 if (*op == '%' || *op == '.'
2821 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002822 && tv2->v_type != VAR_NUMBER
2823 && tv2->v_type != VAR_STRING))
2824 break;
2825 if (tv2->v_type == VAR_FLOAT)
2826 f = tv2->vval.v_float;
2827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002828 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002829 switch (*op)
2830 {
2831 case '+': tv1->vval.v_float += f; break;
2832 case '-': tv1->vval.v_float -= f; break;
2833 case '*': tv1->vval.v_float *= f; break;
2834 case '/': tv1->vval.v_float /= f; break;
2835 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002836 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002837#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002838 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002839 }
2840 }
2841
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 return FAIL;
2844}
2845
2846/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847 * Evaluate the expression used in a ":for var in expr" command.
2848 * "arg" points to "var".
2849 * Set "*errp" to TRUE for an error, FALSE otherwise;
2850 * Return a pointer that holds the info. Null when there is an error.
2851 */
2852 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002853eval_for_line(
2854 char_u *arg,
2855 int *errp,
2856 char_u **nextcmdp,
2857 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 typval_T tv;
2862 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863
2864 *errp = TRUE; /* default: there is an error */
2865
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002866 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002867 if (fi == NULL)
2868 return NULL;
2869
2870 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2871 if (expr == NULL)
2872 return fi;
2873
2874 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002875 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002877 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 return fi;
2879 }
2880
2881 if (skip)
2882 ++emsg_skip;
2883 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2884 {
2885 *errp = FALSE;
2886 if (!skip)
2887 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002888 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002889 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002890 l = tv.vval.v_list;
2891 if (l == NULL)
2892 {
2893 // a null list is like an empty list: do nothing
2894 clear_tv(&tv);
2895 }
2896 else
2897 {
2898 // No need to increment the refcount, it's already set for
2899 // the list being used in "tv".
2900 fi->fi_list = l;
2901 list_add_watch(l, &fi->fi_lw);
2902 fi->fi_lw.lw_item = l->lv_first;
2903 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002904 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002905 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002906 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002907 fi->fi_bi = 0;
2908 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002909 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002910 typval_T btv;
2911
2912 // Make a copy, so that the iteration still works when the
2913 // blob is changed.
2914 blob_copy(&tv, &btv);
2915 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002916 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002917 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002918 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919 else
2920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002921 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002922 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 }
2924 }
2925 }
2926 if (skip)
2927 --emsg_skip;
2928
2929 return fi;
2930}
2931
2932/*
2933 * Use the first item in a ":for" list. Advance to the next.
2934 * Assign the values to the variable (list). "arg" points to the first one.
2935 * Return TRUE when a valid item was found, FALSE when at end of list or
2936 * something wrong.
2937 */
2938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002941 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002945 if (fi->fi_blob != NULL)
2946 {
2947 typval_T tv;
2948
2949 if (fi->fi_bi >= blob_len(fi->fi_blob))
2950 return FALSE;
2951 tv.v_type = VAR_NUMBER;
2952 tv.v_lock = VAR_FIXED;
2953 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2954 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002955 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2956 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002957 }
2958
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 item = fi->fi_lw.lw_item;
2960 if (item == NULL)
2961 result = FALSE;
2962 else
2963 {
2964 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002965 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2966 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967 }
2968 return result;
2969}
2970
2971/*
2972 * Free the structure used to store info used by ":for".
2973 */
2974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002975free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976{
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002979 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002980 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002982 list_unref(fi->fi_list);
2983 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002984 if (fi != NULL && fi->fi_blob != NULL)
2985 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986 vim_free(fi);
2987}
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2990
2991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002992set_context_for_expression(
2993 expand_T *xp,
2994 char_u *arg,
2995 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 int got_eq = FALSE;
2998 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002999 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 if (cmdidx == CMD_let)
3002 {
3003 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003004 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 {
3006 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003007 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008 {
3009 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003010 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003011 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012 break;
3013 }
3014 return;
3015 }
3016 }
3017 else
3018 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3019 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 while ((xp->xp_pattern = vim_strpbrk(arg,
3021 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3022 {
3023 c = *xp->xp_pattern;
3024 if (c == '&')
3025 {
3026 c = xp->xp_pattern[1];
3027 if (c == '&')
3028 {
3029 ++xp->xp_pattern;
3030 xp->xp_context = cmdidx != CMD_let || got_eq
3031 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3032 }
3033 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003036 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3037 xp->xp_pattern += 2;
3038
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041 else if (c == '$')
3042 {
3043 /* environment variable */
3044 xp->xp_context = EXPAND_ENV_VARS;
3045 }
3046 else if (c == '=')
3047 {
3048 got_eq = TRUE;
3049 xp->xp_context = EXPAND_EXPRESSION;
3050 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003051 else if (c == '#'
3052 && xp->xp_context == EXPAND_EXPRESSION)
3053 {
3054 /* Autoload function/variable contains '#'. */
3055 break;
3056 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003057 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 && xp->xp_context == EXPAND_FUNCTIONS
3059 && vim_strchr(xp->xp_pattern, '(') == NULL)
3060 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003061 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 break;
3063 }
3064 else if (cmdidx != CMD_let || got_eq)
3065 {
3066 if (c == '"') /* string */
3067 {
3068 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3069 if (c == '\\' && xp->xp_pattern[1] != NUL)
3070 ++xp->xp_pattern;
3071 xp->xp_context = EXPAND_NOTHING;
3072 }
3073 else if (c == '\'') /* literal string */
3074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003075 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3077 /* skip */ ;
3078 xp->xp_context = EXPAND_NOTHING;
3079 }
3080 else if (c == '|')
3081 {
3082 if (xp->xp_pattern[1] == '|')
3083 {
3084 ++xp->xp_pattern;
3085 xp->xp_context = EXPAND_EXPRESSION;
3086 }
3087 else
3088 xp->xp_context = EXPAND_COMMANDS;
3089 }
3090 else
3091 xp->xp_context = EXPAND_EXPRESSION;
3092 }
3093 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094 /* Doesn't look like something valid, expand as an expression
3095 * anyway. */
3096 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 arg = xp->xp_pattern;
3098 if (*arg != NUL)
3099 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3100 /* skip */ ;
3101 }
3102 xp->xp_pattern = arg;
3103}
3104
3105#endif /* FEAT_CMDL_COMPL */
3106
3107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * ":unlet[!] var1 ... " command.
3109 */
3110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003111ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003113 ex_unletlock(eap, eap->arg, 0);
3114}
3115
3116/*
3117 * ":lockvar" and ":unlockvar" commands
3118 */
3119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003121{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003123 int deep = 2;
3124
3125 if (eap->forceit)
3126 deep = -1;
3127 else if (vim_isdigit(*arg))
3128 {
3129 deep = getdigits(&arg);
3130 arg = skipwhite(arg);
3131 }
3132
3133 ex_unletlock(eap, arg, deep);
3134}
3135
3136/*
3137 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3138 */
3139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003140ex_unletlock(
3141 exarg_T *eap,
3142 char_u *argstart,
3143 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003144{
3145 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003148 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 do
3151 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003152 if (*arg == '$')
3153 {
3154 char_u *name = ++arg;
3155
3156 if (get_env_len(&arg) == 0)
3157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003158 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003159 return;
3160 }
3161 vim_unsetenv(name);
3162 arg = skipwhite(arg);
3163 continue;
3164 }
3165
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003166 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003167 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003168 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003169 if (lv.ll_name == NULL)
3170 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003171 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 if (name_end != NULL)
3175 {
3176 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003177 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003178 }
3179 if (!(eap->skip || error))
3180 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 }
3183
3184 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003185 {
3186 if (eap->cmdidx == CMD_unlet)
3187 {
3188 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3189 error = TRUE;
3190 }
3191 else
3192 {
3193 if (do_lock_var(&lv, name_end, deep,
3194 eap->cmdidx == CMD_lockvar) == FAIL)
3195 error = TRUE;
3196 }
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199 if (!eap->skip)
3200 clear_lval(&lv);
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 arg = skipwhite(name_end);
3203 } while (!ends_excmd(*arg));
3204
3205 eap->nextcmd = check_nextcmd(arg);
3206}
3207
Bram Moolenaar8c711452005-01-14 21:53:12 +00003208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209do_unlet_var(
3210 lval_T *lp,
3211 char_u *name_end,
3212 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003213{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003214 int ret = OK;
3215 int cc;
3216
3217 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 cc = *name_end;
3220 *name_end = NUL;
3221
3222 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003223 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003227 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003228 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003229 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003230 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003231 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 else if (lp->ll_range)
3233 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003235 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003236 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003237
3238 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3239 {
3240 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003241 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003242 return FAIL;
3243 ll_li = li;
3244 ++ll_n1;
3245 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003246
3247 /* Delete a range of List items. */
3248 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3249 {
3250 li = lp->ll_li->li_next;
3251 listitem_remove(lp->ll_list, lp->ll_li);
3252 lp->ll_li = li;
3253 ++lp->ll_n1;
3254 }
3255 }
3256 else
3257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 /* unlet a List item. */
3260 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003261 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003263 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 }
3265
3266 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267}
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269/*
3270 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
3273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003274do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 hashtab_T *ht;
3277 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003278 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003279 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003280 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 ht = find_var_ht(name, &varname);
3283 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003285 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003286 if (d == NULL)
3287 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003288 if (ht == &globvarht)
3289 d = &globvardict;
3290 else if (ht == &compat_hashtab)
3291 d = &vimvardict;
3292 else
3293 {
3294 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3295 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3296 }
3297 if (d == NULL)
3298 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003299 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300 return FAIL;
3301 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003304 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003305 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003306 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003307 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003308 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003309 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003310 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003311 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003312 return FAIL;
3313
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003314 delete_var(ht, hi);
3315 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 if (forceit)
3319 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003320 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return FAIL;
3322}
3323
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003324/*
3325 * Lock or unlock variable indicated by "lp".
3326 * "deep" is the levels to go (-1 for unlimited);
3327 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3328 */
3329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003330do_lock_var(
3331 lval_T *lp,
3332 char_u *name_end,
3333 int deep,
3334 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003335{
3336 int ret = OK;
3337 int cc;
3338 dictitem_T *di;
3339
3340 if (deep == 0) /* nothing to do */
3341 return OK;
3342
3343 if (lp->ll_tv == NULL)
3344 {
3345 cc = *name_end;
3346 *name_end = NUL;
3347
3348 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003349 di = find_var(lp->ll_name, NULL, TRUE);
3350 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003351 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003352 else if ((di->di_flags & DI_FLAGS_FIX)
3353 && di->di_tv.v_type != VAR_DICT
3354 && di->di_tv.v_type != VAR_LIST)
3355 /* For historic reasons this error is not given for a list or dict.
3356 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003357 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358 else
3359 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003360 if (lock)
3361 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003362 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003363 di->di_flags &= ~DI_FLAGS_LOCK;
3364 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365 }
3366 *name_end = cc;
3367 }
3368 else if (lp->ll_range)
3369 {
3370 listitem_T *li = lp->ll_li;
3371
3372 /* (un)lock a range of List items. */
3373 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3374 {
3375 item_lock(&li->li_tv, deep, lock);
3376 li = li->li_next;
3377 ++lp->ll_n1;
3378 }
3379 }
3380 else if (lp->ll_list != NULL)
3381 /* (un)lock a List item. */
3382 item_lock(&lp->ll_li->li_tv, deep, lock);
3383 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003384 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003385 item_lock(&lp->ll_di->di_tv, deep, lock);
3386
3387 return ret;
3388}
3389
3390/*
3391 * Lock or unlock an item. "deep" is nr of levels to go.
3392 */
3393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003394item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003395{
3396 static int recurse = 0;
3397 list_T *l;
3398 listitem_T *li;
3399 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003400 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 hashitem_T *hi;
3402 int todo;
3403
3404 if (recurse >= DICT_MAXNEST)
3405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003406 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407 return;
3408 }
3409 if (deep == 0)
3410 return;
3411 ++recurse;
3412
3413 /* lock/unlock the item itself */
3414 if (lock)
3415 tv->v_lock |= VAR_LOCKED;
3416 else
3417 tv->v_lock &= ~VAR_LOCKED;
3418
3419 switch (tv->v_type)
3420 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003421 case VAR_UNKNOWN:
3422 case VAR_NUMBER:
3423 case VAR_STRING:
3424 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003425 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003426 case VAR_FLOAT:
3427 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003428 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003429 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003430 break;
3431
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003432 case VAR_BLOB:
3433 if ((b = tv->vval.v_blob) != NULL)
3434 {
3435 if (lock)
3436 b->bv_lock |= VAR_LOCKED;
3437 else
3438 b->bv_lock &= ~VAR_LOCKED;
3439 }
3440 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003441 case VAR_LIST:
3442 if ((l = tv->vval.v_list) != NULL)
3443 {
3444 if (lock)
3445 l->lv_lock |= VAR_LOCKED;
3446 else
3447 l->lv_lock &= ~VAR_LOCKED;
3448 if (deep < 0 || deep > 1)
3449 /* recursive: lock/unlock the items the List contains */
3450 for (li = l->lv_first; li != NULL; li = li->li_next)
3451 item_lock(&li->li_tv, deep - 1, lock);
3452 }
3453 break;
3454 case VAR_DICT:
3455 if ((d = tv->vval.v_dict) != NULL)
3456 {
3457 if (lock)
3458 d->dv_lock |= VAR_LOCKED;
3459 else
3460 d->dv_lock &= ~VAR_LOCKED;
3461 if (deep < 0 || deep > 1)
3462 {
3463 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003464 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003465 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3466 {
3467 if (!HASHITEM_EMPTY(hi))
3468 {
3469 --todo;
3470 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3471 }
3472 }
3473 }
3474 }
3475 }
3476 --recurse;
3477}
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3480/*
3481 * Delete all "menutrans_" variables.
3482 */
3483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003487 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003491 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003492 {
3493 if (!HASHITEM_EMPTY(hi))
3494 {
3495 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003496 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3497 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003498 }
3499 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502#endif
3503
3504#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3505
3506/*
3507 * Local string buffer for the next two functions to store a variable name
3508 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3509 * get_user_var_name().
3510 */
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512static char_u *varnamebuf = NULL;
3513static int varnamebuflen = 0;
3514
3515/*
3516 * Function to concatenate a prefix and a variable name.
3517 */
3518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003519cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 int len;
3522
3523 len = (int)STRLEN(name) + 3;
3524 if (len > varnamebuflen)
3525 {
3526 vim_free(varnamebuf);
3527 len += 10; /* some additional space */
3528 varnamebuf = alloc(len);
3529 if (varnamebuf == NULL)
3530 {
3531 varnamebuflen = 0;
3532 return NULL;
3533 }
3534 varnamebuflen = len;
3535 }
3536 *varnamebuf = prefix;
3537 varnamebuf[1] = ':';
3538 STRCPY(varnamebuf + 2, name);
3539 return varnamebuf;
3540}
3541
3542/*
3543 * Function given to ExpandGeneric() to obtain the list of user defined
3544 * (global/buffer/window/built-in) variable names.
3545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003547get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003549 static long_u gdone;
3550 static long_u bdone;
3551 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003552 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003553 static int vidx;
3554 static hashitem_T *hi;
3555 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003558 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003559 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003561 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003562
3563 /* Global variables */
3564 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003566 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003567 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003568 else
3569 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003570 while (HASHITEM_EMPTY(hi))
3571 ++hi;
3572 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3573 return cat_prefix_varname('g', hi->hi_key);
3574 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003576
3577 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003578 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003582 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003583 else
3584 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 while (HASHITEM_EMPTY(hi))
3586 ++hi;
3587 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003589
3590 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003591 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003594 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003596 else
3597 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003598 while (HASHITEM_EMPTY(hi))
3599 ++hi;
3600 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003602
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003603 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003604 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003605 if (tdone < ht->ht_used)
3606 {
3607 if (tdone++ == 0)
3608 hi = ht->ht_array;
3609 else
3610 ++hi;
3611 while (HASHITEM_EMPTY(hi))
3612 ++hi;
3613 return cat_prefix_varname('t', hi->hi_key);
3614 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003615
Bram Moolenaar33570922005-01-25 22:26:29 +00003616 /* v: variables */
3617 if (vidx < VV_LEN)
3618 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Bram Moolenaard23a8232018-02-10 18:45:26 +01003620 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 varnamebuflen = 0;
3622 return NULL;
3623}
3624
3625#endif /* FEAT_CMDL_COMPL */
3626
3627/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003628 * Return TRUE if "pat" matches "text".
3629 * Does not use 'cpo' and always uses 'magic'.
3630 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003631 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003632pattern_match(char_u *pat, char_u *text, int ic)
3633{
3634 int matches = FALSE;
3635 char_u *save_cpo;
3636 regmatch_T regmatch;
3637
3638 /* avoid 'l' flag in 'cpoptions' */
3639 save_cpo = p_cpo;
3640 p_cpo = (char_u *)"";
3641 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3642 if (regmatch.regprog != NULL)
3643 {
3644 regmatch.rm_ic = ic;
3645 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3646 vim_regfree(regmatch.regprog);
3647 }
3648 p_cpo = save_cpo;
3649 return matches;
3650}
3651
3652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3656 */
3657
3658/*
3659 * Handle zero level expression.
3660 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003662 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 * Return OK or FAIL.
3664 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003666eval0(
3667 char_u *arg,
3668 typval_T *rettv,
3669 char_u **nextcmd,
3670 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 int ret;
3673 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003674 int did_emsg_before = did_emsg;
3675 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (ret == FAIL || !ends_excmd(*p))
3680 {
3681 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 /*
3684 * Report the invalid expression unless the expression evaluation has
3685 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003686 * exception, or we already gave a more specific error.
3687 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003689 if (!aborting() && did_emsg == did_emsg_before
3690 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003691 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 ret = FAIL;
3693 }
3694 if (nextcmd != NULL)
3695 *nextcmd = check_nextcmd(p);
3696
3697 return ret;
3698}
3699
3700/*
3701 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003702 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 *
3704 * "arg" must point to the first non-white of the expression.
3705 * "arg" is advanced to the next non-white after the recognized expression.
3706 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003707 * Note: "rettv.v_lock" is not set.
3708 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 * Return OK or FAIL.
3710 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003712eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
3718 * Get the first variable.
3719 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 return FAIL;
3722
3723 if ((*arg)[0] == '?')
3724 {
3725 result = FALSE;
3726 if (evaluate)
3727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003728 int error = FALSE;
3729
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003730 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003733 if (error)
3734 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736
3737 /*
3738 * Get the second variable.
3739 */
3740 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743
3744 /*
3745 * Check for the ":".
3746 */
3747 if ((*arg)[0] != ':')
3748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003749 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 return FAIL;
3753 }
3754
3755 /*
3756 * Get the third variable.
3757 */
3758 *arg = skipwhite(*arg + 1);
3759 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3760 {
3761 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return FAIL;
3764 }
3765 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768
3769 return OK;
3770}
3771
3772/*
3773 * Handle first level expression:
3774 * expr2 || expr2 || expr2 logical OR
3775 *
3776 * "arg" must point to the first non-white of the expression.
3777 * "arg" is advanced to the next non-white after the recognized expression.
3778 *
3779 * Return OK or FAIL.
3780 */
3781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 long result;
3786 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003787 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 /*
3790 * Get the first variable.
3791 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003792 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 return FAIL;
3794
3795 /*
3796 * Repeat until there is no following "||".
3797 */
3798 first = TRUE;
3799 result = FALSE;
3800 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3801 {
3802 if (evaluate && first)
3803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003804 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 if (error)
3808 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 first = FALSE;
3810 }
3811
3812 /*
3813 * Get the second variable.
3814 */
3815 *arg = skipwhite(*arg + 2);
3816 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3817 return FAIL;
3818
3819 /*
3820 * Compute the result.
3821 */
3822 if (evaluate && !result)
3823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003824 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003827 if (error)
3828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 if (evaluate)
3831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003832 rettv->v_type = VAR_NUMBER;
3833 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 }
3836
3837 return OK;
3838}
3839
3840/*
3841 * Handle second level expression:
3842 * expr3 && expr3 && expr3 logical AND
3843 *
3844 * "arg" must point to the first non-white of the expression.
3845 * "arg" is advanced to the next non-white after the recognized expression.
3846 *
3847 * Return OK or FAIL.
3848 */
3849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003850eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 long result;
3854 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857 /*
3858 * Get the first variable.
3859 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return FAIL;
3862
3863 /*
3864 * Repeat until there is no following "&&".
3865 */
3866 first = TRUE;
3867 result = TRUE;
3868 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3869 {
3870 if (evaluate && first)
3871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003872 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003875 if (error)
3876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 first = FALSE;
3878 }
3879
3880 /*
3881 * Get the second variable.
3882 */
3883 *arg = skipwhite(*arg + 2);
3884 if (eval4(arg, &var2, evaluate && result) == FAIL)
3885 return FAIL;
3886
3887 /*
3888 * Compute the result.
3889 */
3890 if (evaluate && result)
3891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003892 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003894 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003895 if (error)
3896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 if (evaluate)
3899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 rettv->v_type = VAR_NUMBER;
3901 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 }
3904
3905 return OK;
3906}
3907
3908/*
3909 * Handle third level expression:
3910 * var1 == var2
3911 * var1 =~ var2
3912 * var1 != var2
3913 * var1 !~ var2
3914 * var1 > var2
3915 * var1 >= var2
3916 * var1 < var2
3917 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003918 * var1 is var2
3919 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 *
3921 * "arg" must point to the first non-white of the expression.
3922 * "arg" is advanced to the next non-white after the recognized expression.
3923 *
3924 * Return OK or FAIL.
3925 */
3926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003927eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 char_u *p;
3931 int i;
3932 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003933 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 /*
3938 * Get the first variable.
3939 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 return FAIL;
3942
3943 p = *arg;
3944 switch (p[0])
3945 {
3946 case '=': if (p[1] == '=')
3947 type = TYPE_EQUAL;
3948 else if (p[1] == '~')
3949 type = TYPE_MATCH;
3950 break;
3951 case '!': if (p[1] == '=')
3952 type = TYPE_NEQUAL;
3953 else if (p[1] == '~')
3954 type = TYPE_NOMATCH;
3955 break;
3956 case '>': if (p[1] != '=')
3957 {
3958 type = TYPE_GREATER;
3959 len = 1;
3960 }
3961 else
3962 type = TYPE_GEQUAL;
3963 break;
3964 case '<': if (p[1] != '=')
3965 {
3966 type = TYPE_SMALLER;
3967 len = 1;
3968 }
3969 else
3970 type = TYPE_SEQUAL;
3971 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003972 case 'i': if (p[1] == 's')
3973 {
3974 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3975 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003976 i = p[len];
3977 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003978 {
3979 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3980 type_is = TRUE;
3981 }
3982 }
3983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
3985
3986 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003987 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 */
3989 if (type != TYPE_UNKNOWN)
3990 {
3991 /* extra question mark appended: ignore case */
3992 if (p[len] == '?')
3993 {
3994 ic = TRUE;
3995 ++len;
3996 }
3997 /* extra '#' appended: match case */
3998 else if (p[len] == '#')
3999 {
4000 ic = FALSE;
4001 ++len;
4002 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004003 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 else
4005 ic = p_ic;
4006
4007 /*
4008 * Get the second variable.
4009 */
4010 *arg = skipwhite(p + len);
4011 if (eval5(arg, &var2, evaluate) == FAIL)
4012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return FAIL;
4015 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004016 if (evaluate)
4017 {
4018 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4019
4020 clear_tv(&var2);
4021 return ret;
4022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024
4025 return OK;
4026}
4027
4028/*
4029 * Handle fourth level expression:
4030 * + number addition
4031 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004032 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004033 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 *
4035 * "arg" must point to the first non-white of the expression.
4036 * "arg" is advanced to the next non-white after the recognized expression.
4037 *
4038 * Return OK or FAIL.
4039 */
4040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 typval_T var2;
4044 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004046 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004047#ifdef FEAT_FLOAT
4048 float_T f1 = 0, f2 = 0;
4049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 char_u *s1, *s2;
4051 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4052 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004053 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 /*
4056 * Get the first variable.
4057 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004058 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return FAIL;
4060
4061 /*
4062 * Repeat computing, until no '+', '-' or '.' is following.
4063 */
4064 for (;;)
4065 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004066 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004068 concat = op == '.'
4069 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4070 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 break;
4072
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004073 if ((op != '+' || (rettv->v_type != VAR_LIST
4074 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004075#ifdef FEAT_FLOAT
4076 && (op == '.' || rettv->v_type != VAR_FLOAT)
4077#endif
4078 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079 {
4080 /* For "list + ...", an illegal use of the first operand as
4081 * a number cannot be determined before evaluating the 2nd
4082 * operand: if this is also a list, all is ok.
4083 * For "something . ...", "something - ..." or "non-list + ...",
4084 * we know that the first operand needs to be a string or number
4085 * without evaluating the 2nd operand. So check before to avoid
4086 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004087 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 {
4089 clear_tv(rettv);
4090 return FAIL;
4091 }
4092 }
4093
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 /*
4095 * Get the second variable.
4096 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004097 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4098 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004100 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return FAIL;
4104 }
4105
4106 if (evaluate)
4107 {
4108 /*
4109 * Compute the result.
4110 */
4111 if (op == '.')
4112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004113 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4114 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 if (s2 == NULL) /* type error ? */
4116 {
4117 clear_tv(rettv);
4118 clear_tv(&var2);
4119 return FAIL;
4120 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004121 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
4123 rettv->v_type = VAR_STRING;
4124 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004126 else if (op == '+' && rettv->v_type == VAR_BLOB
4127 && var2.v_type == VAR_BLOB)
4128 {
4129 blob_T *b1 = rettv->vval.v_blob;
4130 blob_T *b2 = var2.vval.v_blob;
4131 blob_T *b = blob_alloc();
4132 int i;
4133
4134 if (b != NULL)
4135 {
4136 for (i = 0; i < blob_len(b1); i++)
4137 ga_append(&b->bv_ga, blob_get(b1, i));
4138 for (i = 0; i < blob_len(b2); i++)
4139 ga_append(&b->bv_ga, blob_get(b2, i));
4140
4141 clear_tv(rettv);
4142 rettv_blob_set(rettv, b);
4143 }
4144 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004145 else if (op == '+' && rettv->v_type == VAR_LIST
4146 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004147 {
4148 /* concatenate Lists */
4149 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4150 &var3) == FAIL)
4151 {
4152 clear_tv(rettv);
4153 clear_tv(&var2);
4154 return FAIL;
4155 }
4156 clear_tv(rettv);
4157 *rettv = var3;
4158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 else
4160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 int error = FALSE;
4162
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004163#ifdef FEAT_FLOAT
4164 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 f1 = rettv->vval.v_float;
4167 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004169 else
4170#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004172 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004173 if (error)
4174 {
4175 /* This can only happen for "list + non-list". For
4176 * "non-list + ..." or "something - ...", we returned
4177 * before evaluating the 2nd operand. */
4178 clear_tv(rettv);
4179 return FAIL;
4180 }
4181#ifdef FEAT_FLOAT
4182 if (var2.v_type == VAR_FLOAT)
4183 f1 = n1;
4184#endif
4185 }
4186#ifdef FEAT_FLOAT
4187 if (var2.v_type == VAR_FLOAT)
4188 {
4189 f2 = var2.vval.v_float;
4190 n2 = 0;
4191 }
4192 else
4193#endif
4194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004195 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004196 if (error)
4197 {
4198 clear_tv(rettv);
4199 clear_tv(&var2);
4200 return FAIL;
4201 }
4202#ifdef FEAT_FLOAT
4203 if (rettv->v_type == VAR_FLOAT)
4204 f2 = n2;
4205#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004208
4209#ifdef FEAT_FLOAT
4210 /* If there is a float on either side the result is a float. */
4211 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4212 {
4213 if (op == '+')
4214 f1 = f1 + f2;
4215 else
4216 f1 = f1 - f2;
4217 rettv->v_type = VAR_FLOAT;
4218 rettv->vval.v_float = f1;
4219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004221#endif
4222 {
4223 if (op == '+')
4224 n1 = n1 + n2;
4225 else
4226 n1 = n1 - n2;
4227 rettv->v_type = VAR_NUMBER;
4228 rettv->vval.v_number = n1;
4229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 }
4234 return OK;
4235}
4236
4237/*
4238 * Handle fifth level expression:
4239 * * number multiplication
4240 * / number division
4241 * % number modulo
4242 *
4243 * "arg" must point to the first non-white of the expression.
4244 * "arg" is advanced to the next non-white after the recognized expression.
4245 *
4246 * Return OK or FAIL.
4247 */
4248 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249eval6(
4250 char_u **arg,
4251 typval_T *rettv,
4252 int evaluate,
4253 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
Bram Moolenaar33570922005-01-25 22:26:29 +00004255 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004257 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004258#ifdef FEAT_FLOAT
4259 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004260 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004261#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 /*
4265 * Get the first variable.
4266 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004267 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 return FAIL;
4269
4270 /*
4271 * Repeat computing, until no '*', '/' or '%' is following.
4272 */
4273 for (;;)
4274 {
4275 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004276 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 break;
4278
4279 if (evaluate)
4280 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004281#ifdef FEAT_FLOAT
4282 if (rettv->v_type == VAR_FLOAT)
4283 {
4284 f1 = rettv->vval.v_float;
4285 use_float = TRUE;
4286 n1 = 0;
4287 }
4288 else
4289#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004290 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (error)
4293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 else
4296 n1 = 0;
4297
4298 /*
4299 * Get the second variable.
4300 */
4301 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004302 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return FAIL;
4304
4305 if (evaluate)
4306 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004307#ifdef FEAT_FLOAT
4308 if (var2.v_type == VAR_FLOAT)
4309 {
4310 if (!use_float)
4311 {
4312 f1 = n1;
4313 use_float = TRUE;
4314 }
4315 f2 = var2.vval.v_float;
4316 n2 = 0;
4317 }
4318 else
4319#endif
4320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004321 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004322 clear_tv(&var2);
4323 if (error)
4324 return FAIL;
4325#ifdef FEAT_FLOAT
4326 if (use_float)
4327 f2 = n2;
4328#endif
4329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 /*
4332 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004333 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004335#ifdef FEAT_FLOAT
4336 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004338 if (op == '*')
4339 f1 = f1 * f2;
4340 else if (op == '/')
4341 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004342# ifdef VMS
4343 /* VMS crashes on divide by zero, work around it */
4344 if (f2 == 0.0)
4345 {
4346 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004347 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004348 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004349 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004350 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004351 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004352 }
4353 else
4354 f1 = f1 / f2;
4355# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004356 /* We rely on the floating point library to handle divide
4357 * by zero to result in "inf" and not a crash. */
4358 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004359# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004363 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004364 return FAIL;
4365 }
4366 rettv->v_type = VAR_FLOAT;
4367 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 if (op == '*')
4373 n1 = n1 * n2;
4374 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004375 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004377 n1 = num_modulus(n1, n2);
4378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004379 rettv->v_type = VAR_NUMBER;
4380 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 }
4384
4385 return OK;
4386}
4387
4388/*
4389 * Handle sixth level expression:
4390 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004391 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004392 * "string" string constant
4393 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 * &option-name option value
4395 * @r register contents
4396 * identifier variable value
4397 * function() function call
4398 * $VAR environment variable
4399 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004400 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004401 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004402 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 *
4404 * Also handle:
4405 * ! in front logical NOT
4406 * - in front unary minus
4407 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004408 * trailing [] subscript in String or List
4409 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 *
4411 * "arg" must point to the first non-white of the expression.
4412 * "arg" is advanced to the next non-white after the recognized expression.
4413 *
4414 * Return OK or FAIL.
4415 */
4416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004417eval7(
4418 char_u **arg,
4419 typval_T *rettv,
4420 int evaluate,
4421 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004423 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 int len;
4425 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 char_u *start_leader, *end_leader;
4427 int ret = OK;
4428 char_u *alias;
4429
4430 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004432 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004437 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 */
4439 start_leader = *arg;
4440 while (**arg == '!' || **arg == '-' || **arg == '+')
4441 *arg = skipwhite(*arg + 1);
4442 end_leader = *arg;
4443
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004444 if (**arg == '.' && (!isdigit(*(*arg + 1))
4445#ifdef FEAT_FLOAT
4446 || current_sctx.sc_version < 2
4447#endif
4448 ))
4449 {
4450 semsg(_(e_invexpr2), *arg);
4451 ++*arg;
4452 return FAIL;
4453 }
4454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 switch (**arg)
4456 {
4457 /*
4458 * Number constant.
4459 */
4460 case '0':
4461 case '1':
4462 case '2':
4463 case '3':
4464 case '4':
4465 case '5':
4466 case '6':
4467 case '7':
4468 case '8':
4469 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004470 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471 {
4472#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004473 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004474 int get_float = FALSE;
4475
4476 /* We accept a float when the format matches
4477 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004478 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004479 * With script version 2 and later the leading digit can be
4480 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004481 * Don't look for a float after the "." operator, so that
4482 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004483 if (**arg == '.')
4484 p = *arg;
4485 else
4486 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004487 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004489 get_float = TRUE;
4490 p = skipdigits(p + 2);
4491 if (*p == 'e' || *p == 'E')
4492 {
4493 ++p;
4494 if (*p == '-' || *p == '+')
4495 ++p;
4496 if (!vim_isdigit(*p))
4497 get_float = FALSE;
4498 else
4499 p = skipdigits(p + 1);
4500 }
4501 if (ASCII_ISALPHA(*p) || *p == '.')
4502 get_float = FALSE;
4503 }
4504 if (get_float)
4505 {
4506 float_T f;
4507
4508 *arg += string2float(*arg, &f);
4509 if (evaluate)
4510 {
4511 rettv->v_type = VAR_FLOAT;
4512 rettv->vval.v_float = f;
4513 }
4514 }
4515 else
4516#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004517 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004518 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004519 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004520 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004521
4522 // Blob constant: 0z0123456789abcdef
4523 if (evaluate)
4524 blob = blob_alloc();
4525 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4526 {
4527 if (!vim_isxdigit(bp[1]))
4528 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004529 if (blob != NULL)
4530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004531 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004532 ga_clear(&blob->bv_ga);
4533 VIM_CLEAR(blob);
4534 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004535 ret = FAIL;
4536 break;
4537 }
4538 if (blob != NULL)
4539 ga_append(&blob->bv_ga,
4540 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004541 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4542 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004543 }
4544 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004545 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004546 *arg = bp;
4547 }
4548 else
4549 {
4550 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004551 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4552 if (len == 0)
4553 {
4554 semsg(_(e_invexpr2), *arg);
4555 ret = FAIL;
4556 break;
4557 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004558 *arg += len;
4559 if (evaluate)
4560 {
4561 rettv->v_type = VAR_NUMBER;
4562 rettv->vval.v_number = n;
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 /*
4569 * String constant: "string".
4570 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 break;
4573
4574 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 break;
4579
4580 /*
4581 * List: [expr, expr]
4582 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585
4586 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004587 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004588 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004589 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004590 {
4591 ++*arg;
4592 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4593 }
4594 else
4595 ret = NOTDONE;
4596 break;
4597
4598 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004599 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004600 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004602 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4603 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004604 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 break;
4606
4607 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004608 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004610 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 /*
4614 * Environment variable: $VAR.
4615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618
4619 /*
4620 * Register contents: @r.
4621 */
4622 case '@': ++*arg;
4623 if (evaluate)
4624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004626 rettv->vval.v_string = get_reg_contents(**arg,
4627 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 if (**arg != NUL)
4630 ++*arg;
4631 break;
4632
4633 /*
4634 * nested expression: (expression).
4635 */
4636 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (**arg == ')')
4639 ++*arg;
4640 else if (ret == OK)
4641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004642 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 ret = FAIL;
4645 }
4646 break;
4647
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651
4652 if (ret == NOTDONE)
4653 {
4654 /*
4655 * Must be a variable or function name.
4656 * Can also be a curly-braces kind of name: {expr}.
4657 */
4658 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004659 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 if (alias != NULL)
4661 s = alias;
4662
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 ret = FAIL;
4665 else
4666 {
4667 if (**arg == '(') /* recursive! */
4668 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004669 partial_T *partial;
4670
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004671 if (!evaluate)
4672 check_vars(s, len);
4673
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 /* If "s" is the name of a variable of type VAR_FUNC
4675 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004676 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004678 /* Need to make a copy, in case evaluating the arguments makes
4679 * the name invalid. */
4680 s = vim_strsave(s);
4681 if (s == NULL)
4682 ret = FAIL;
4683 else
4684 /* Invoke the function. */
4685 ret = get_func_tv(s, len, rettv, arg,
4686 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4687 &len, evaluate, partial, NULL);
4688 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004689
4690 /* If evaluate is FALSE rettv->v_type was not set in
4691 * get_func_tv, but it's needed in handle_subscript() to parse
4692 * what follows. So set it here. */
4693 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4694 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004695 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004696 rettv->v_type = VAR_FUNC;
4697 }
4698
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 /* Stop the expression evaluation when immediately
4700 * aborting on error, or when an interrupt occurred or
4701 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004702 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004703 {
4704 if (ret == OK)
4705 clear_tv(rettv);
4706 ret = FAIL;
4707 }
4708 }
4709 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004710 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004711 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004712 {
4713 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004714 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004717 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 }
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 *arg = skipwhite(*arg);
4721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004722 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4723 * expr(expr). */
4724 if (ret == OK)
4725 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4729 */
4730 if (ret == OK && evaluate && end_leader > start_leader)
4731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004733 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#ifdef FEAT_FLOAT
4735 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004736
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737 if (rettv->v_type == VAR_FLOAT)
4738 f = rettv->vval.v_float;
4739 else
4740#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004741 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 clear_tv(rettv);
4745 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 else
4748 {
4749 while (end_leader > start_leader)
4750 {
4751 --end_leader;
4752 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753 {
4754#ifdef FEAT_FLOAT
4755 if (rettv->v_type == VAR_FLOAT)
4756 f = !f;
4757 else
4758#endif
4759 val = !val;
4760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004762 {
4763#ifdef FEAT_FLOAT
4764 if (rettv->v_type == VAR_FLOAT)
4765 f = -f;
4766 else
4767#endif
4768 val = -val;
4769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#ifdef FEAT_FLOAT
4772 if (rettv->v_type == VAR_FLOAT)
4773 {
4774 clear_tv(rettv);
4775 rettv->vval.v_float = f;
4776 }
4777 else
4778#endif
4779 {
4780 clear_tv(rettv);
4781 rettv->v_type = VAR_NUMBER;
4782 rettv->vval.v_number = val;
4783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786
4787 return ret;
4788}
4789
4790/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004791 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4792 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4794 */
4795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004796eval_index(
4797 char_u **arg,
4798 typval_T *rettv,
4799 int evaluate,
4800 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801{
4802 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004804 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806 long len = -1;
4807 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810
Bram Moolenaara03f2332016-02-06 18:09:59 +01004811 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004813 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004814 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004815 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004816 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004817 return FAIL;
4818 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004819#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004820 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004821 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004822 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004823#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004824 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004825 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004826 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004827 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004828 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004829 return FAIL;
4830 case VAR_UNKNOWN:
4831 if (evaluate)
4832 return FAIL;
4833 /* FALLTHROUGH */
4834
4835 case VAR_STRING:
4836 case VAR_NUMBER:
4837 case VAR_LIST:
4838 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004839 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004840 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004841 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004843 init_tv(&var1);
4844 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004845 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004847 /*
4848 * dict.name
4849 */
4850 key = *arg + 1;
4851 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4852 ;
4853 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004855 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 }
4857 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 /*
4860 * something[idx]
4861 *
4862 * Get the (first) variable from inside the [].
4863 */
4864 *arg = skipwhite(*arg + 1);
4865 if (**arg == ':')
4866 empty1 = TRUE;
4867 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4868 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004869 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 {
4871 /* not a number or string */
4872 clear_tv(&var1);
4873 return FAIL;
4874 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004875
4876 /*
4877 * Get the second variable from inside the [:].
4878 */
4879 if (**arg == ':')
4880 {
4881 range = TRUE;
4882 *arg = skipwhite(*arg + 1);
4883 if (**arg == ']')
4884 empty2 = TRUE;
4885 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004887 if (!empty1)
4888 clear_tv(&var1);
4889 return FAIL;
4890 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004891 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004892 {
4893 /* not a number or string */
4894 if (!empty1)
4895 clear_tv(&var1);
4896 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004897 return FAIL;
4898 }
4899 }
4900
4901 /* Check for the ']'. */
4902 if (**arg != ']')
4903 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004904 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004905 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906 clear_tv(&var1);
4907 if (range)
4908 clear_tv(&var2);
4909 return FAIL;
4910 }
4911 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912 }
4913
4914 if (evaluate)
4915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 n1 = 0;
4917 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004919 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004921 }
4922 if (range)
4923 {
4924 if (empty2)
4925 n2 = -1;
4926 else
4927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004928 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 }
4931 }
4932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004935 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004936 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004937 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004938 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004939 case VAR_SPECIAL:
4940 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004941 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004942 break; /* not evaluating, skipping over subscript */
4943
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004944 case VAR_NUMBER:
4945 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 len = (long)STRLEN(s);
4948 if (range)
4949 {
4950 /* The resulting variable is a substring. If the indexes
4951 * are out of range the result is empty. */
4952 if (n1 < 0)
4953 {
4954 n1 = len + n1;
4955 if (n1 < 0)
4956 n1 = 0;
4957 }
4958 if (n2 < 0)
4959 n2 = len + n2;
4960 else if (n2 >= len)
4961 n2 = len;
4962 if (n1 >= len || n2 < 0 || n1 > n2)
4963 s = NULL;
4964 else
4965 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4966 }
4967 else
4968 {
4969 /* The resulting variable is a string of a single
4970 * character. If the index is too big or negative the
4971 * result is empty. */
4972 if (n1 >= len || n1 < 0)
4973 s = NULL;
4974 else
4975 s = vim_strnsave(s + n1, 1);
4976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977 clear_tv(rettv);
4978 rettv->v_type = VAR_STRING;
4979 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004980 break;
4981
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004982 case VAR_BLOB:
4983 len = blob_len(rettv->vval.v_blob);
4984 if (range)
4985 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004986 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004987 // are out of range the result is empty.
4988 if (n1 < 0)
4989 {
4990 n1 = len + n1;
4991 if (n1 < 0)
4992 n1 = 0;
4993 }
4994 if (n2 < 0)
4995 n2 = len + n2;
4996 else if (n2 >= len)
4997 n2 = len - 1;
4998 if (n1 >= len || n2 < 0 || n1 > n2)
4999 {
5000 clear_tv(rettv);
5001 rettv->v_type = VAR_BLOB;
5002 rettv->vval.v_blob = NULL;
5003 }
5004 else
5005 {
5006 blob_T *blob = blob_alloc();
5007
5008 if (blob != NULL)
5009 {
5010 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5011 {
5012 blob_free(blob);
5013 return FAIL;
5014 }
5015 blob->bv_ga.ga_len = n2 - n1 + 1;
5016 for (i = n1; i <= n2; i++)
5017 blob_set(blob, i - n1,
5018 blob_get(rettv->vval.v_blob, i));
5019
5020 clear_tv(rettv);
5021 rettv_blob_set(rettv, blob);
5022 }
5023 }
5024 }
5025 else
5026 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005027 // The resulting variable is a byte value.
5028 // If the index is too big or negative that is an error.
5029 if (n1 < 0)
5030 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005031 if (n1 < len && n1 >= 0)
5032 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005033 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005034
5035 clear_tv(rettv);
5036 rettv->v_type = VAR_NUMBER;
5037 rettv->vval.v_number = v;
5038 }
5039 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005040 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005041 }
5042 break;
5043
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005044 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005046 if (n1 < 0)
5047 n1 = len + n1;
5048 if (!empty1 && (n1 < 0 || n1 >= len))
5049 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005050 /* For a range we allow invalid values and return an empty
5051 * list. A list index out of range is an error. */
5052 if (!range)
5053 {
5054 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005055 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005056 return FAIL;
5057 }
5058 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005059 }
5060 if (range)
5061 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005062 list_T *l;
5063 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005064
5065 if (n2 < 0)
5066 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005067 else if (n2 >= len)
5068 n2 = len - 1;
5069 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005070 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005071 l = list_alloc();
5072 if (l == NULL)
5073 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 n1 <= n2; ++n1)
5076 {
5077 if (list_append_tv(l, &item->li_tv) == FAIL)
5078 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005079 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 return FAIL;
5081 }
5082 item = item->li_next;
5083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005085 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 }
5087 else
5088 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005089 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 clear_tv(rettv);
5091 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 }
5093 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094
5095 case VAR_DICT:
5096 if (range)
5097 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005098 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005099 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 if (len == -1)
5101 clear_tv(&var1);
5102 return FAIL;
5103 }
5104 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005105 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106
5107 if (len == -1)
5108 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005110 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 clear_tv(&var1);
5113 return FAIL;
5114 }
5115 }
5116
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005117 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005119 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005120 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 if (len == -1)
5122 clear_tv(&var1);
5123 if (item == NULL)
5124 return FAIL;
5125
5126 copy_tv(&item->di_tv, &var1);
5127 clear_tv(rettv);
5128 *rettv = var1;
5129 }
5130 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 }
5132 }
5133
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005134 return OK;
5135}
5136
5137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 * Get an option value.
5139 * "arg" points to the '&' or '+' before the option name.
5140 * "arg" is advanced to character after the option name.
5141 * Return OK or FAIL.
5142 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005143 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005144get_option_tv(
5145 char_u **arg,
5146 typval_T *rettv, /* when NULL, only check if option exists */
5147 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148{
5149 char_u *option_end;
5150 long numval;
5151 char_u *stringval;
5152 int opt_type;
5153 int c;
5154 int working = (**arg == '+'); /* has("+option") */
5155 int ret = OK;
5156 int opt_flags;
5157
5158 /*
5159 * Isolate the option name and find its value.
5160 */
5161 option_end = find_option_end(arg, &opt_flags);
5162 if (option_end == NULL)
5163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005165 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return FAIL;
5167 }
5168
5169 if (!evaluate)
5170 {
5171 *arg = option_end;
5172 return OK;
5173 }
5174
5175 c = *option_end;
5176 *option_end = NUL;
5177 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 if (opt_type == -3) /* invalid name */
5181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005183 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 ret = FAIL;
5185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
5188 if (opt_type == -2) /* hidden string option */
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
5191 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 else if (opt_type == -1) /* hidden number option */
5194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 rettv->v_type = VAR_NUMBER;
5196 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 else if (opt_type == 1) /* number option */
5199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 rettv->v_type = VAR_NUMBER;
5201 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 else /* string option */
5204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 rettv->v_type = VAR_STRING;
5206 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 }
5209 else if (working && (opt_type == -2 || opt_type == -1))
5210 ret = FAIL;
5211
5212 *option_end = c; /* put back for error messages */
5213 *arg = option_end;
5214
5215 return ret;
5216}
5217
5218/*
5219 * Allocate a variable for a string constant.
5220 * Return OK or FAIL.
5221 */
5222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005223get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 char_u *p;
5226 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 int extra = 0;
5228
5229 /*
5230 * Find the end of the string, skipping backslashed characters.
5231 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005232 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 if (*p == '\\' && p[1] != NUL)
5235 {
5236 ++p;
5237 /* A "\<x>" form occupies at least 4 characters, and produces up
5238 * to 6 characters: reserve space for 2 extra */
5239 if (*p == '<')
5240 extra += 2;
5241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243
5244 if (*p != '"')
5245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005246 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 return FAIL;
5248 }
5249
5250 /* If only parsing, set *arg and return here */
5251 if (!evaluate)
5252 {
5253 *arg = p + 1;
5254 return OK;
5255 }
5256
5257 /*
5258 * Copy the string into allocated memory, handling backslashed
5259 * characters.
5260 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005261 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (name == NULL)
5263 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 rettv->v_type = VAR_STRING;
5265 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 if (*p == '\\')
5270 {
5271 switch (*++p)
5272 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 case 'b': *name++ = BS; ++p; break;
5274 case 'e': *name++ = ESC; ++p; break;
5275 case 'f': *name++ = FF; ++p; break;
5276 case 'n': *name++ = NL; ++p; break;
5277 case 'r': *name++ = CAR; ++p; break;
5278 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280 case 'X': /* hex: "\x1", "\x12" */
5281 case 'x':
5282 case 'u': /* Unicode: "\u0023" */
5283 case 'U':
5284 if (vim_isxdigit(p[1]))
5285 {
5286 int n, nr;
5287 int c = toupper(*p);
5288
5289 if (c == 'X')
5290 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005291 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005293 else
5294 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 nr = 0;
5296 while (--n >= 0 && vim_isxdigit(p[1]))
5297 {
5298 ++p;
5299 nr = (nr << 4) + hex2nr(*p);
5300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 /* For "\u" store the number according to
5303 * 'encoding'. */
5304 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 break;
5310
5311 /* octal: "\1", "\12", "\123" */
5312 case '0':
5313 case '1':
5314 case '2':
5315 case '3':
5316 case '4':
5317 case '5':
5318 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 case '7': *name = *p++ - '0';
5320 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 *name = (*name << 3) + *p++ - '0';
5323 if (*p >= '0' && *p <= '7')
5324 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 break;
5328
5329 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005330 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (extra != 0)
5332 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 break;
5335 }
5336 /* FALLTHROUGH */
5337
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 break;
5340 }
5341 }
5342 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005347 if (*p != NUL) /* just in case */
5348 ++p;
5349 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 return OK;
5352}
5353
5354/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 * Return OK or FAIL.
5357 */
5358 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005359get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005362 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005363 int reduce = 0;
5364
5365 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005367 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005368 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005373 break;
5374 ++reduce;
5375 ++p;
5376 }
5377 }
5378
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005381 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005382 return FAIL;
5383 }
5384
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005386 if (!evaluate)
5387 {
5388 *arg = p + 1;
5389 return OK;
5390 }
5391
5392 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005394 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005395 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005396 if (str == NULL)
5397 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 rettv->v_type = VAR_STRING;
5399 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005400
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005404 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005406 break;
5407 ++p;
5408 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005410 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005412 *arg = p + 1;
5413
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005414 return OK;
5415}
5416
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005417/*
5418 * Return the function name of the partial.
5419 */
5420 char_u *
5421partial_name(partial_T *pt)
5422{
5423 if (pt->pt_name != NULL)
5424 return pt->pt_name;
5425 return pt->pt_func->uf_name;
5426}
5427
Bram Moolenaarddecc252016-04-06 22:59:37 +02005428 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005429partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005430{
5431 int i;
5432
5433 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005434 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005435 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005436 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005437 if (pt->pt_name != NULL)
5438 {
5439 func_unref(pt->pt_name);
5440 vim_free(pt->pt_name);
5441 }
5442 else
5443 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005444 vim_free(pt);
5445}
5446
5447/*
5448 * Unreference a closure: decrement the reference count and free it when it
5449 * becomes zero.
5450 */
5451 void
5452partial_unref(partial_T *pt)
5453{
5454 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005455 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005456}
5457
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005458static int tv_equal_recurse_limit;
5459
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005460 static int
5461func_equal(
5462 typval_T *tv1,
5463 typval_T *tv2,
5464 int ic) /* ignore case */
5465{
5466 char_u *s1, *s2;
5467 dict_T *d1, *d2;
5468 int a1, a2;
5469 int i;
5470
5471 /* empty and NULL function name considered the same */
5472 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005473 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005474 if (s1 != NULL && *s1 == NUL)
5475 s1 = NULL;
5476 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005477 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005478 if (s2 != NULL && *s2 == NUL)
5479 s2 = NULL;
5480 if (s1 == NULL || s2 == NULL)
5481 {
5482 if (s1 != s2)
5483 return FALSE;
5484 }
5485 else if (STRCMP(s1, s2) != 0)
5486 return FALSE;
5487
5488 /* empty dict and NULL dict is different */
5489 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5490 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5491 if (d1 == NULL || d2 == NULL)
5492 {
5493 if (d1 != d2)
5494 return FALSE;
5495 }
5496 else if (!dict_equal(d1, d2, ic, TRUE))
5497 return FALSE;
5498
5499 /* empty list and no list considered the same */
5500 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5501 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5502 if (a1 != a2)
5503 return FALSE;
5504 for (i = 0; i < a1; ++i)
5505 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5506 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5507 return FALSE;
5508
5509 return TRUE;
5510}
5511
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005512/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005514 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005515 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005516 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005518tv_equal(
5519 typval_T *tv1,
5520 typval_T *tv2,
5521 int ic, /* ignore case */
5522 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523{
5524 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005525 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005526 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005527 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005529 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005530 * recursiveness to a limit. We guess they are equal then.
5531 * A fixed limit has the problem of still taking an awful long time.
5532 * Reduce the limit every time running into it. That should work fine for
5533 * deeply linked structures that are not recursively linked and catch
5534 * recursiveness quickly. */
5535 if (!recursive)
5536 tv_equal_recurse_limit = 1000;
5537 if (recursive_cnt >= tv_equal_recurse_limit)
5538 {
5539 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005540 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005541 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005542
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005543 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5544 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005545 if ((tv1->v_type == VAR_FUNC
5546 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5547 && (tv2->v_type == VAR_FUNC
5548 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5549 {
5550 ++recursive_cnt;
5551 r = func_equal(tv1, tv2, ic);
5552 --recursive_cnt;
5553 return r;
5554 }
5555
5556 if (tv1->v_type != tv2->v_type)
5557 return FALSE;
5558
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005559 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005560 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005561 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005562 ++recursive_cnt;
5563 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5564 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005565 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005566
5567 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005568 ++recursive_cnt;
5569 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5570 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005571 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005572
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005573 case VAR_BLOB:
5574 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5575
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005576 case VAR_NUMBER:
5577 return tv1->vval.v_number == tv2->vval.v_number;
5578
5579 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005580 s1 = tv_get_string_buf(tv1, buf1);
5581 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005582 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005583
5584 case VAR_SPECIAL:
5585 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005586
5587 case VAR_FLOAT:
5588#ifdef FEAT_FLOAT
5589 return tv1->vval.v_float == tv2->vval.v_float;
5590#endif
5591 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005592#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005593 return tv1->vval.v_job == tv2->vval.v_job;
5594#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005595 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005596#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005597 return tv1->vval.v_channel == tv2->vval.v_channel;
5598#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005599 case VAR_FUNC:
5600 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005601 case VAR_UNKNOWN:
5602 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005603 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005604
Bram Moolenaara03f2332016-02-06 18:09:59 +01005605 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5606 * does not equal anything, not even itself. */
5607 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005608}
5609
5610/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005611 * Return the next (unique) copy ID.
5612 * Used for serializing nested structures.
5613 */
5614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005615get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005616{
5617 current_copyID += COPYID_INC;
5618 return current_copyID;
5619}
5620
5621/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005622 * Garbage collection for lists and dictionaries.
5623 *
5624 * We use reference counts to be able to free most items right away when they
5625 * are no longer used. But for composite items it's possible that it becomes
5626 * unused while the reference count is > 0: When there is a recursive
5627 * reference. Example:
5628 * :let l = [1, 2, 3]
5629 * :let d = {9: l}
5630 * :let l[1] = d
5631 *
5632 * Since this is quite unusual we handle this with garbage collection: every
5633 * once in a while find out which lists and dicts are not referenced from any
5634 * variable.
5635 *
5636 * Here is a good reference text about garbage collection (refers to Python
5637 * but it applies to all reference-counting mechanisms):
5638 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005639 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005640
5641/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005642 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005643 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005644 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005645 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005646 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005647garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005648{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005649 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005650 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005651 buf_T *buf;
5652 win_T *wp;
5653 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005654 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005655 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005656
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005657 if (!testing)
5658 {
5659 /* Only do this once. */
5660 want_garbage_collect = FALSE;
5661 may_garbage_collect = FALSE;
5662 garbage_collect_at_exit = FALSE;
5663 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005664
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005665 /* We advance by two because we add one for items referenced through
5666 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005667 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005668
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005669 /*
5670 * 1. Go through all accessible variables and mark all lists and dicts
5671 * with copyID.
5672 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005673
5674 /* Don't free variables in the previous_funccal list unless they are only
5675 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005676 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005678
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005679 /* script-local variables */
5680 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005681 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005682
5683 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005684 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005685 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5686 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005687
5688 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005689 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005690 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5691 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005692 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005693 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5694 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005695#ifdef FEAT_TEXT_PROP
5696 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5697 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5698 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005699 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005700 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005701 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5702 NULL, NULL);
5703#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005704
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005705 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005706 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005707 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5708 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005709 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005710 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005711
5712 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005714
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005715 /* named functions (matters for closures) */
5716 abort = abort || set_ref_in_functions(copyID);
5717
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005718 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005720
Bram Moolenaard812df62008-11-09 12:46:09 +00005721 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005723
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005724 // callbacks in buffers
5725 abort = abort || set_ref_in_buffers(copyID);
5726
Bram Moolenaar1dced572012-04-05 16:54:08 +02005727#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005728 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005729#endif
5730
Bram Moolenaardb913952012-06-29 12:54:53 +02005731#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005732 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005733#endif
5734
5735#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005737#endif
5738
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005739#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005740 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005741 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005742#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005743#ifdef FEAT_NETBEANS_INTG
5744 abort = abort || set_ref_in_nb_channel(copyID);
5745#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005746
Bram Moolenaare3188e22016-05-31 21:13:04 +02005747#ifdef FEAT_TIMERS
5748 abort = abort || set_ref_in_timer(copyID);
5749#endif
5750
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005751#ifdef FEAT_QUICKFIX
5752 abort = abort || set_ref_in_quickfix(copyID);
5753#endif
5754
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005755#ifdef FEAT_TERMINAL
5756 abort = abort || set_ref_in_term(copyID);
5757#endif
5758
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005759#ifdef FEAT_TEXT_PROP
5760 abort = abort || set_ref_in_popups(copyID);
5761#endif
5762
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005763 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005764 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005765 /*
5766 * 2. Free lists and dictionaries that are not referenced.
5767 */
5768 did_free = free_unref_items(copyID);
5769
5770 /*
5771 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005773 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005775 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 else if (p_verbose > 0)
5777 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005778 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005779 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005780
5781 return did_free;
5782}
5783
5784/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005785 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005786 */
5787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005789{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005790 int did_free = FALSE;
5791
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005792 /* Let all "free" functions know that we are here. This means no
5793 * dictionaries, lists, channels or jobs are to be freed, because we will
5794 * do that here. */
5795 in_free_unref_items = TRUE;
5796
5797 /*
5798 * PASS 1: free the contents of the items. We don't free the items
5799 * themselves yet, so that it is possible to decrement refcount counters
5800 */
5801
Bram Moolenaarcd524592016-07-17 14:57:05 +02005802 /* Go through the list of dicts and free items without the copyID. */
5803 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005804
Bram Moolenaarda861d62016-07-17 15:46:27 +02005805 /* Go through the list of lists and free items without the copyID. */
5806 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005807
5808#ifdef FEAT_JOB_CHANNEL
5809 /* Go through the list of jobs and free items without the copyID. This
5810 * must happen before doing channels, because jobs refer to channels, but
5811 * the reference from the channel to the job isn't tracked. */
5812 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5813
5814 /* Go through the list of channels and free items without the copyID. */
5815 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5816#endif
5817
5818 /*
5819 * PASS 2: free the items themselves.
5820 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005821 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005822 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005823
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005824#ifdef FEAT_JOB_CHANNEL
5825 /* Go through the list of jobs and free items without the copyID. This
5826 * must happen before doing channels, because jobs refer to channels, but
5827 * the reference from the channel to the job isn't tracked. */
5828 free_unused_jobs(copyID, COPYID_MASK);
5829
5830 /* Go through the list of channels and free items without the copyID. */
5831 free_unused_channels(copyID, COPYID_MASK);
5832#endif
5833
5834 in_free_unref_items = FALSE;
5835
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005836 return did_free;
5837}
5838
5839/*
5840 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005841 * "list_stack" is used to add lists to be marked. Can be NULL.
5842 *
5843 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005844 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005846set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005847{
5848 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005850 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005851 hashtab_T *cur_ht;
5852 ht_stack_T *ht_stack = NULL;
5853 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005854
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005855 cur_ht = ht;
5856 for (;;)
5857 {
5858 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005859 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005860 /* Mark each item in the hashtab. If the item contains a hashtab
5861 * it is added to ht_stack, if it contains a list it is added to
5862 * list_stack. */
5863 todo = (int)cur_ht->ht_used;
5864 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5865 if (!HASHITEM_EMPTY(hi))
5866 {
5867 --todo;
5868 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5869 &ht_stack, list_stack);
5870 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005871 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005872
5873 if (ht_stack == NULL)
5874 break;
5875
5876 /* take an item from the stack */
5877 cur_ht = ht_stack->ht;
5878 tempitem = ht_stack;
5879 ht_stack = ht_stack->prev;
5880 free(tempitem);
5881 }
5882
5883 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005884}
5885
5886/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005887 * Mark a dict and its items with "copyID".
5888 * Returns TRUE if setting references failed somehow.
5889 */
5890 int
5891set_ref_in_dict(dict_T *d, int copyID)
5892{
5893 if (d != NULL && d->dv_copyID != copyID)
5894 {
5895 d->dv_copyID = copyID;
5896 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5897 }
5898 return FALSE;
5899}
5900
5901/*
5902 * Mark a list and its items with "copyID".
5903 * Returns TRUE if setting references failed somehow.
5904 */
5905 int
5906set_ref_in_list(list_T *ll, int copyID)
5907{
5908 if (ll != NULL && ll->lv_copyID != copyID)
5909 {
5910 ll->lv_copyID = copyID;
5911 return set_ref_in_list_items(ll, copyID, NULL);
5912 }
5913 return FALSE;
5914}
5915
5916/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005918 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5919 *
5920 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005921 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005922 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005923set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005925 listitem_T *li;
5926 int abort = FALSE;
5927 list_T *cur_l;
5928 list_stack_T *list_stack = NULL;
5929 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005930
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005931 cur_l = l;
5932 for (;;)
5933 {
5934 if (!abort)
5935 /* Mark each item in the list. If the item contains a hashtab
5936 * it is added to ht_stack, if it contains a list it is added to
5937 * list_stack. */
5938 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5939 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5940 ht_stack, &list_stack);
5941 if (list_stack == NULL)
5942 break;
5943
5944 /* take an item from the stack */
5945 cur_l = list_stack->list;
5946 tempitem = list_stack;
5947 list_stack = list_stack->prev;
5948 free(tempitem);
5949 }
5950
5951 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005952}
5953
5954/*
5955 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005956 * "list_stack" is used to add lists to be marked. Can be NULL.
5957 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5958 *
5959 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005962set_ref_in_item(
5963 typval_T *tv,
5964 int copyID,
5965 ht_stack_T **ht_stack,
5966 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005967{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005968 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005969
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005970 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005971 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005972 dict_T *dd = tv->vval.v_dict;
5973
Bram Moolenaara03f2332016-02-06 18:09:59 +01005974 if (dd != NULL && dd->dv_copyID != copyID)
5975 {
5976 /* Didn't see this dict yet. */
5977 dd->dv_copyID = copyID;
5978 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005979 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005980 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5981 }
5982 else
5983 {
5984 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5985 if (newitem == NULL)
5986 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005987 else
5988 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005989 newitem->ht = &dd->dv_hashtab;
5990 newitem->prev = *ht_stack;
5991 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005992 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005993 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005994 }
5995 }
5996 else if (tv->v_type == VAR_LIST)
5997 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005998 list_T *ll = tv->vval.v_list;
5999
Bram Moolenaara03f2332016-02-06 18:09:59 +01006000 if (ll != NULL && ll->lv_copyID != copyID)
6001 {
6002 /* Didn't see this list yet. */
6003 ll->lv_copyID = copyID;
6004 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006005 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006006 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006007 }
6008 else
6009 {
6010 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006011 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006012 if (newitem == NULL)
6013 abort = TRUE;
6014 else
6015 {
6016 newitem->list = ll;
6017 newitem->prev = *list_stack;
6018 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006019 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006020 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006021 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006022 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006023 else if (tv->v_type == VAR_FUNC)
6024 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006025 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006026 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006027 else if (tv->v_type == VAR_PARTIAL)
6028 {
6029 partial_T *pt = tv->vval.v_partial;
6030 int i;
6031
6032 /* A partial does not have a copyID, because it cannot contain itself.
6033 */
6034 if (pt != NULL)
6035 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006036 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006037
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006038 if (pt->pt_dict != NULL)
6039 {
6040 typval_T dtv;
6041
6042 dtv.v_type = VAR_DICT;
6043 dtv.vval.v_dict = pt->pt_dict;
6044 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6045 }
6046
6047 for (i = 0; i < pt->pt_argc; ++i)
6048 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6049 ht_stack, list_stack);
6050 }
6051 }
6052#ifdef FEAT_JOB_CHANNEL
6053 else if (tv->v_type == VAR_JOB)
6054 {
6055 job_T *job = tv->vval.v_job;
6056 typval_T dtv;
6057
6058 if (job != NULL && job->jv_copyID != copyID)
6059 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006060 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006061 if (job->jv_channel != NULL)
6062 {
6063 dtv.v_type = VAR_CHANNEL;
6064 dtv.vval.v_channel = job->jv_channel;
6065 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6066 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006067 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006068 {
6069 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006070 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006071 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6072 }
6073 }
6074 }
6075 else if (tv->v_type == VAR_CHANNEL)
6076 {
6077 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006078 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006079 typval_T dtv;
6080 jsonq_T *jq;
6081 cbq_T *cq;
6082
6083 if (ch != NULL && ch->ch_copyID != copyID)
6084 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006085 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006086 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006087 {
6088 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6089 jq = jq->jq_next)
6090 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6091 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6092 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006093 if (cq->cq_callback.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 = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6098 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006099 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006100 {
6101 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006102 dtv.vval.v_partial =
6103 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6105 }
6106 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006107 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006108 {
6109 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006110 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006111 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6112 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006113 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006114 {
6115 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006116 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006117 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6118 }
6119 }
6120 }
6121#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006122 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006123}
6124
Bram Moolenaar17a13432016-01-24 14:22:10 +01006125 static char *
6126get_var_special_name(int nr)
6127{
6128 switch (nr)
6129 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006130 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006131 case VVAL_TRUE: return "v:true";
6132 case VVAL_NONE: return "v:none";
6133 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006134 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006135 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006136 return "42";
6137}
6138
Bram Moolenaar8c711452005-01-14 21:53:12 +00006139/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 * Return a string with the string representation of a variable.
6141 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006143 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006144 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6145 * stings as "string()", otherwise does not put quotes around strings, as
6146 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006147 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6148 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006149 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006151 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006152echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006153 typval_T *tv,
6154 char_u **tofree,
6155 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006156 int copyID,
6157 int echo_style,
6158 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006159 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 static int recurse = 0;
6162 char_u *r = NULL;
6163
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006165 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006166 if (!did_echo_string_emsg)
6167 {
6168 /* Only give this message once for a recursive call to avoid
6169 * flooding the user with errors. And stop iterating over lists
6170 * and dicts. */
6171 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006172 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006173 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006174 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006175 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006176 }
6177 ++recurse;
6178
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 switch (tv->v_type)
6180 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006181 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006182 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006183 {
6184 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006185 r = tv->vval.v_string;
6186 if (r == NULL)
6187 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006188 }
6189 else
6190 {
6191 *tofree = string_quote(tv->vval.v_string, FALSE);
6192 r = *tofree;
6193 }
6194 break;
6195
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006197 if (echo_style)
6198 {
6199 *tofree = NULL;
6200 r = tv->vval.v_string;
6201 }
6202 else
6203 {
6204 *tofree = string_quote(tv->vval.v_string, TRUE);
6205 r = *tofree;
6206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006208
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006209 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006210 {
6211 partial_T *pt = tv->vval.v_partial;
6212 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006213 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006214 garray_T ga;
6215 int i;
6216 char_u *tf;
6217
6218 ga_init2(&ga, 1, 100);
6219 ga_concat(&ga, (char_u *)"function(");
6220 if (fname != NULL)
6221 {
6222 ga_concat(&ga, fname);
6223 vim_free(fname);
6224 }
6225 if (pt != NULL && pt->pt_argc > 0)
6226 {
6227 ga_concat(&ga, (char_u *)", [");
6228 for (i = 0; i < pt->pt_argc; ++i)
6229 {
6230 if (i > 0)
6231 ga_concat(&ga, (char_u *)", ");
6232 ga_concat(&ga,
6233 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6234 vim_free(tf);
6235 }
6236 ga_concat(&ga, (char_u *)"]");
6237 }
6238 if (pt != NULL && pt->pt_dict != NULL)
6239 {
6240 typval_T dtv;
6241
6242 ga_concat(&ga, (char_u *)", ");
6243 dtv.v_type = VAR_DICT;
6244 dtv.vval.v_dict = pt->pt_dict;
6245 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6246 vim_free(tf);
6247 }
6248 ga_concat(&ga, (char_u *)")");
6249
6250 *tofree = ga.ga_data;
6251 r = *tofree;
6252 break;
6253 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006254
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006255 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006256 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006257 break;
6258
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006260 if (tv->vval.v_list == NULL)
6261 {
6262 *tofree = NULL;
6263 r = NULL;
6264 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006265 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6266 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006267 {
6268 *tofree = NULL;
6269 r = (char_u *)"[...]";
6270 }
6271 else
6272 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006273 int old_copyID = tv->vval.v_list->lv_copyID;
6274
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006275 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006276 *tofree = list2string(tv, copyID, restore_copyID);
6277 if (restore_copyID)
6278 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006279 r = *tofree;
6280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006281 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006282
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006284 if (tv->vval.v_dict == NULL)
6285 {
6286 *tofree = NULL;
6287 r = NULL;
6288 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006289 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6290 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006291 {
6292 *tofree = NULL;
6293 r = (char_u *)"{...}";
6294 }
6295 else
6296 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006297 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006298 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006299 *tofree = dict2string(tv, copyID, restore_copyID);
6300 if (restore_copyID)
6301 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006302 r = *tofree;
6303 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006305
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006306 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006307 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006308 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006309 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006310 break;
6311
Bram Moolenaar835dc632016-02-07 14:27:38 +01006312 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006313 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006315 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006316 if (composite_val)
6317 {
6318 *tofree = string_quote(r, FALSE);
6319 r = *tofree;
6320 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006322
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006323 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006324#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006325 *tofree = NULL;
6326 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6327 r = numbuf;
6328 break;
6329#endif
6330
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006331 case VAR_SPECIAL:
6332 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006333 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006334 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336
Bram Moolenaar8502c702014-06-17 12:51:16 +02006337 if (--recurse == 0)
6338 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006339 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006340}
6341
6342/*
6343 * Return a string with the string representation of a variable.
6344 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6345 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006346 * Does not put quotes around strings, as ":echo" displays values.
6347 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6348 * May return NULL.
6349 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006351echo_string(
6352 typval_T *tv,
6353 char_u **tofree,
6354 char_u *numbuf,
6355 int copyID)
6356{
6357 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6358}
6359
6360/*
6361 * Return a string with the string representation of a variable.
6362 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6363 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006364 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006365 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006366 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006367 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368tv2string(
6369 typval_T *tv,
6370 char_u **tofree,
6371 char_u *numbuf,
6372 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006373{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006374 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375}
6376
6377/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 * Return string "str" in ' quotes, doubling ' characters.
6379 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006381 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006382 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006384{
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006386 char_u *p, *r, *s;
6387
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 len = (function ? 13 : 3);
6389 if (str != NULL)
6390 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006391 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006392 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 if (*p == '\'')
6394 ++len;
6395 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006396 s = r = alloc(len);
6397 if (r != NULL)
6398 {
6399 if (function)
6400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006402 r += 10;
6403 }
6404 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 if (str != NULL)
6407 for (p = str; *p != NUL; )
6408 {
6409 if (*p == '\'')
6410 *r++ = '\'';
6411 MB_COPY_CHAR(p, r);
6412 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006413 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006414 if (function)
6415 *r++ = ')';
6416 *r++ = NUL;
6417 }
6418 return s;
6419}
6420
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006421#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006422/*
6423 * Convert the string "text" to a floating point number.
6424 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6425 * this always uses a decimal point.
6426 * Returns the length of the text that was consumed.
6427 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429string2float(
6430 char_u *text,
6431 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006432{
6433 char *s = (char *)text;
6434 float_T f;
6435
Bram Moolenaar62473612017-01-08 19:25:40 +01006436 /* MS-Windows does not deal with "inf" and "nan" properly. */
6437 if (STRNICMP(text, "inf", 3) == 0)
6438 {
6439 *value = INFINITY;
6440 return 3;
6441 }
6442 if (STRNICMP(text, "-inf", 3) == 0)
6443 {
6444 *value = -INFINITY;
6445 return 4;
6446 }
6447 if (STRNICMP(text, "nan", 3) == 0)
6448 {
6449 *value = NAN;
6450 return 3;
6451 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006452 f = strtod(s, &s);
6453 *value = f;
6454 return (int)((char_u *)s - text);
6455}
6456#endif
6457
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 * Get the value of an environment variable.
6460 * "arg" is pointing to the '$'. It is advanced to after the name.
6461 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006462 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 */
6464 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006465get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
6467 char_u *string = NULL;
6468 int len;
6469 int cc;
6470 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006471 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
6473 ++*arg;
6474 name = *arg;
6475 len = get_env_len(arg);
6476 if (evaluate)
6477 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006478 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006479 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006480
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006481 cc = name[len];
6482 name[len] = NUL;
6483 /* first try vim_getenv(), fast for normal environment vars */
6484 string = vim_getenv(name, &mustfree);
6485 if (string != NULL && *string != NUL)
6486 {
6487 if (!mustfree)
6488 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006490 else
6491 {
6492 if (mustfree)
6493 vim_free(string);
6494
6495 /* next try expanding things like $VIM and ${HOME} */
6496 string = expand_env_save(name - 1);
6497 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006498 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006499 }
6500 name[len] = cc;
6501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006502 rettv->v_type = VAR_STRING;
6503 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
6505
6506 return OK;
6507}
6508
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006509
6510
6511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006513 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516var2fpos(
6517 typval_T *varp,
6518 int dollar_lnum, /* TRUE when $ is last line */
6519 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006521 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006523 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaara5525202006-03-02 22:52:09 +00006525 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006526 if (varp->v_type == VAR_LIST)
6527 {
6528 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006529 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006530 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006531 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006532
6533 l = varp->vval.v_list;
6534 if (l == NULL)
6535 return NULL;
6536
6537 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006538 pos.lnum = list_find_nr(l, 0L, &error);
6539 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006540 return NULL; /* invalid line number */
6541
6542 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006543 pos.col = list_find_nr(l, 1L, &error);
6544 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006545 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006546 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006547
6548 /* We accept "$" for the column number: last column. */
6549 li = list_find(l, 1L);
6550 if (li != NULL && li->li_tv.v_type == VAR_STRING
6551 && li->li_tv.vval.v_string != NULL
6552 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6553 pos.col = len + 1;
6554
Bram Moolenaara5525202006-03-02 22:52:09 +00006555 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006556 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006557 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006558 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006559
Bram Moolenaara5525202006-03-02 22:52:09 +00006560 /* Get the virtual offset. Defaults to zero. */
6561 pos.coladd = list_find_nr(l, 2L, &error);
6562 if (error)
6563 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006564
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006565 return &pos;
6566 }
6567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006568 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006569 if (name == NULL)
6570 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006571 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006573 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6574 {
6575 if (VIsual_active)
6576 return &VIsual;
6577 return &curwin->w_cursor;
6578 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006579 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006581 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6583 return NULL;
6584 return pp;
6585 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006586
Bram Moolenaara5525202006-03-02 22:52:09 +00006587 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006588
Bram Moolenaar477933c2007-07-17 14:32:23 +00006589 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006590 {
6591 pos.col = 0;
6592 if (name[1] == '0') /* "w0": first visible line */
6593 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006594 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006595 /* In silent Ex mode topline is zero, but that's not a valid line
6596 * number; use one instead. */
6597 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006598 return &pos;
6599 }
6600 else if (name[1] == '$') /* "w$": last visible line */
6601 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006602 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006603 /* In silent Ex mode botline is zero, return zero then. */
6604 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006605 return &pos;
6606 }
6607 }
6608 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006610 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 {
6612 pos.lnum = curbuf->b_ml.ml_line_count;
6613 pos.col = 0;
6614 }
6615 else
6616 {
6617 pos.lnum = curwin->w_cursor.lnum;
6618 pos.col = (colnr_T)STRLEN(ml_get_curline());
6619 }
6620 return &pos;
6621 }
6622 return NULL;
6623}
6624
6625/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006626 * Convert list in "arg" into a position and optional file number.
6627 * When "fnump" is NULL there is no file number, only 3 items.
6628 * Note that the column is passed on as-is, the caller may want to decrement
6629 * it to use 1 for the first column.
6630 * Return FAIL when conversion is not possible, doesn't check the position for
6631 * validity.
6632 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634list2fpos(
6635 typval_T *arg,
6636 pos_T *posp,
6637 int *fnump,
6638 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006639{
6640 list_T *l = arg->vval.v_list;
6641 long i = 0;
6642 long n;
6643
Bram Moolenaar493c1782014-05-28 14:34:46 +02006644 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6645 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006646 if (arg->v_type != VAR_LIST
6647 || l == NULL
6648 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006649 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006650 return FAIL;
6651
6652 if (fnump != NULL)
6653 {
6654 n = list_find_nr(l, i++, NULL); /* fnum */
6655 if (n < 0)
6656 return FAIL;
6657 if (n == 0)
6658 n = curbuf->b_fnum; /* current buffer */
6659 *fnump = n;
6660 }
6661
6662 n = list_find_nr(l, i++, NULL); /* lnum */
6663 if (n < 0)
6664 return FAIL;
6665 posp->lnum = n;
6666
6667 n = list_find_nr(l, i++, NULL); /* col */
6668 if (n < 0)
6669 return FAIL;
6670 posp->col = n;
6671
Bram Moolenaar493c1782014-05-28 14:34:46 +02006672 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006673 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006674 posp->coladd = 0;
6675 else
6676 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006677
Bram Moolenaar493c1782014-05-28 14:34:46 +02006678 if (curswantp != NULL)
6679 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6680
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006681 return OK;
6682}
6683
6684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 * Get the length of an environment variable name.
6686 * Advance "arg" to the first character after the name.
6687 * Return 0 for error.
6688 */
6689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006690get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 char_u *p;
6693 int len;
6694
6695 for (p = *arg; vim_isIDc(*p); ++p)
6696 ;
6697 if (p == *arg) /* no name found */
6698 return 0;
6699
6700 len = (int)(p - *arg);
6701 *arg = p;
6702 return len;
6703}
6704
6705/*
6706 * Get the length of the name of a function or internal variable.
6707 * "arg" is advanced to the first non-white character after the name.
6708 * Return 0 if something is wrong.
6709 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006711get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 char_u *p;
6714 int len;
6715
6716 /* Find the end of the name. */
6717 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006718 {
6719 if (*p == ':')
6720 {
6721 /* "s:" is start of "s:var", but "n:" is not and can be used in
6722 * slice "[n:]". Also "xx:" is not a namespace. */
6723 len = (int)(p - *arg);
6724 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6725 || len > 1)
6726 break;
6727 }
6728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (p == *arg) /* no name found */
6730 return 0;
6731
6732 len = (int)(p - *arg);
6733 *arg = skipwhite(p);
6734
6735 return len;
6736}
6737
6738/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006739 * Get the length of the name of a variable or function.
6740 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006742 * Return -1 if curly braces expansion failed.
6743 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 * If the name contains 'magic' {}'s, expand them and return the
6745 * expanded name in an allocated string via 'alias' - caller must free.
6746 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006748get_name_len(
6749 char_u **arg,
6750 char_u **alias,
6751 int evaluate,
6752 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 char_u *p;
6756 char_u *expr_start;
6757 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 *alias = NULL; /* default to no alias */
6760
6761 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6762 && (*arg)[2] == (int)KE_SNR)
6763 {
6764 /* hard coded <SNR>, already translated */
6765 *arg += 3;
6766 return get_id_len(arg) + 3;
6767 }
6768 len = eval_fname_script(*arg);
6769 if (len > 0)
6770 {
6771 /* literal "<SID>", "s:" or "<SNR>" */
6772 *arg += len;
6773 }
6774
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006776 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006778 p = find_name_end(*arg, &expr_start, &expr_end,
6779 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (expr_start != NULL)
6781 {
6782 char_u *temp_string;
6783
6784 if (!evaluate)
6785 {
6786 len += (int)(p - *arg);
6787 *arg = skipwhite(p);
6788 return len;
6789 }
6790
6791 /*
6792 * Include any <SID> etc in the expanded string:
6793 * Thus the -len here.
6794 */
6795 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6796 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006797 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 *alias = temp_string;
6799 *arg = skipwhite(p);
6800 return (int)STRLEN(temp_string);
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006804 // Only give an error when there is something, otherwise it will be
6805 // reported at a higher level.
6806 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006807 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
6809 return len;
6810}
6811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812/*
6813 * Find the end of a variable or function name, taking care of magic braces.
6814 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6815 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006816 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 * Return a pointer to just after the name. Equal to "arg" if there is no
6818 * valid name.
6819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821find_name_end(
6822 char_u *arg,
6823 char_u **expr_start,
6824 char_u **expr_end,
6825 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006827 int mb_nest = 0;
6828 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006830 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 *expr_start = NULL;
6835 *expr_end = NULL;
6836 }
6837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006838 /* Quick check for valid starting character. */
6839 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6840 return arg;
6841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 for (p = arg; *p != NUL
6843 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006845 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006846 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006847 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006848 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006849 if (*p == '\'')
6850 {
6851 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006852 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006853 ;
6854 if (*p == NUL)
6855 break;
6856 }
6857 else if (*p == '"')
6858 {
6859 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006860 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006861 if (*p == '\\' && p[1] != NUL)
6862 ++p;
6863 if (*p == NUL)
6864 break;
6865 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006866 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6867 {
6868 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006869 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006870 len = (int)(p - arg);
6871 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006872 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006873 break;
6874 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006876 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878 if (*p == '[')
6879 ++br_nest;
6880 else if (*p == ']')
6881 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006884 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 if (*p == '{')
6887 {
6888 mb_nest++;
6889 if (expr_start != NULL && *expr_start == NULL)
6890 *expr_start = p;
6891 }
6892 else if (*p == '}')
6893 {
6894 mb_nest--;
6895 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6896 *expr_end = p;
6897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900
6901 return p;
6902}
6903
6904/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006905 * Expands out the 'magic' {}'s in a variable/function name.
6906 * Note that this can call itself recursively, to deal with
6907 * constructs like foo{bar}{baz}{bam}
6908 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6909 * "in_start" ^
6910 * "expr_start" ^
6911 * "expr_end" ^
6912 * "in_end" ^
6913 *
6914 * Returns a new allocated string, which the caller must free.
6915 * Returns NULL for failure.
6916 */
6917 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006918make_expanded_name(
6919 char_u *in_start,
6920 char_u *expr_start,
6921 char_u *expr_end,
6922 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006923{
6924 char_u c1;
6925 char_u *retval = NULL;
6926 char_u *temp_result;
6927 char_u *nextcmd = NULL;
6928
6929 if (expr_end == NULL || in_end == NULL)
6930 return NULL;
6931 *expr_start = NUL;
6932 *expr_end = NUL;
6933 c1 = *in_end;
6934 *in_end = NUL;
6935
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006936 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006937 if (temp_result != NULL && nextcmd == NULL)
6938 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006939 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6940 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006941 if (retval != NULL)
6942 {
6943 STRCPY(retval, in_start);
6944 STRCAT(retval, temp_result);
6945 STRCAT(retval, expr_end + 1);
6946 }
6947 }
6948 vim_free(temp_result);
6949
6950 *in_end = c1; /* put char back for error messages */
6951 *expr_start = '{';
6952 *expr_end = '}';
6953
6954 if (retval != NULL)
6955 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006956 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006957 if (expr_start != NULL)
6958 {
6959 /* Further expansion! */
6960 temp_result = make_expanded_name(retval, expr_start,
6961 expr_end, temp_result);
6962 vim_free(retval);
6963 retval = temp_result;
6964 }
6965 }
6966
6967 return retval;
6968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006975eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006977 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6978}
6979
6980/*
6981 * Return TRUE if character "c" can be used as the first character in a
6982 * variable or function name (excluding '{' and '}').
6983 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006985eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006986{
6987 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988}
6989
6990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 * Set number v: variable to "val".
6992 */
6993 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006994set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997}
6998
6999/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007000 * Get typval_T v: variable value.
7001 */
7002 typval_T *
7003get_vim_var_tv(int idx)
7004{
7005 return &vimvars[idx].vv_tv;
7006}
7007
7008/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007009 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007011 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007012get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007014 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015}
7016
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007017/*
7018 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007019 * If the String variable has never been set, return an empty string.
7020 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007021 */
7022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007024{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007025 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007026}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007029 * Get List v: variable value. Caller must take care of reference count when
7030 * needed.
7031 */
7032 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007033get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007034{
7035 return vimvars[idx].vv_list;
7036}
7037
7038/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007039 * Get Dict v: variable value. Caller must take care of reference count when
7040 * needed.
7041 */
7042 dict_T *
7043get_vim_var_dict(int idx)
7044{
7045 return vimvars[idx].vv_dict;
7046}
7047
7048/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007049 * Set v:char to character "c".
7050 */
7051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007052set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007053{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007054 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007055
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007056 if (has_mbyte)
7057 buf[(*mb_char2bytes)(c, buf)] = NUL;
7058 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007059 {
7060 buf[0] = c;
7061 buf[1] = NUL;
7062 }
7063 set_vim_var_string(VV_CHAR, buf, -1);
7064}
7065
7066/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007067 * Set v:count to "count" and v:count1 to "count1".
7068 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 */
7070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007071set_vcount(
7072 long count,
7073 long count1,
7074 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007076 if (set_prevcount)
7077 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007078 vimvars[VV_COUNT].vv_nr = count;
7079 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080}
7081
7082/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007083 * Save variables that might be changed as a side effect. Used when executing
7084 * a timer callback.
7085 */
7086 void
7087save_vimvars(vimvars_save_T *vvsave)
7088{
7089 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7090 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7091 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7092}
7093
7094/*
7095 * Restore variables saved by save_vimvars().
7096 */
7097 void
7098restore_vimvars(vimvars_save_T *vvsave)
7099{
7100 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7101 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7102 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7103}
7104
7105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 * Set string v: variable to a copy of "val".
7107 */
7108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109set_vim_var_string(
7110 int idx,
7111 char_u *val,
7112 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
Bram Moolenaara542c682016-01-31 16:28:04 +01007114 clear_tv(&vimvars[idx].vv_di.di_tv);
7115 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122}
7123
7124/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007125 * Set List v: variable to "val".
7126 */
7127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007128set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007129{
Bram Moolenaara542c682016-01-31 16:28:04 +01007130 clear_tv(&vimvars[idx].vv_di.di_tv);
7131 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007132 vimvars[idx].vv_list = val;
7133 if (val != NULL)
7134 ++val->lv_refcount;
7135}
7136
7137/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007138 * Set Dictionary v: variable to "val".
7139 */
7140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007142{
Bram Moolenaara542c682016-01-31 16:28:04 +01007143 clear_tv(&vimvars[idx].vv_di.di_tv);
7144 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007145 vimvars[idx].vv_dict = val;
7146 if (val != NULL)
7147 {
7148 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007149 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007150 }
7151}
7152
7153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 * Set v:register if needed.
7155 */
7156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 char_u regname;
7160
7161 if (c == 0 || c == ' ')
7162 regname = '"';
7163 else
7164 regname = c;
7165 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 set_vim_var_string(VV_REG, &regname, 1);
7168}
7169
7170/*
7171 * Get or set v:exception. If "oldval" == NULL, return the current value.
7172 * Otherwise, restore the value to "oldval" and return NULL.
7173 * Must always be called in pairs to save and restore v:exception! Does not
7174 * take care of memory allocations.
7175 */
7176 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007177v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 return NULL;
7184}
7185
7186/*
7187 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7188 * Otherwise, restore the value to "oldval" and return NULL.
7189 * Must always be called in pairs to save and restore v:throwpoint! Does not
7190 * take care of memory allocations.
7191 */
7192 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007193v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 return NULL;
7200}
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202/*
7203 * Set v:cmdarg.
7204 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7205 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7206 * Must always be called in pairs!
7207 */
7208 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 char_u *oldval;
7212 char_u *newval;
7213 unsigned len;
7214
Bram Moolenaare9a41262005-01-15 22:18:47 +00007215 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007216 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007218 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007219 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007220 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007223 if (eap->force_bin == FORCE_BIN)
7224 len = 6;
7225 else if (eap->force_bin == FORCE_NOBIN)
7226 len = 8;
7227 else
7228 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007229
7230 if (eap->read_edit)
7231 len += 7;
7232
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007233 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007234 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007235 if (eap->force_enc != 0)
7236 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007237 if (eap->bad_char != 0)
7238 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007239
7240 newval = alloc(len + 1);
7241 if (newval == NULL)
7242 return NULL;
7243
7244 if (eap->force_bin == FORCE_BIN)
7245 sprintf((char *)newval, " ++bin");
7246 else if (eap->force_bin == FORCE_NOBIN)
7247 sprintf((char *)newval, " ++nobin");
7248 else
7249 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007250
7251 if (eap->read_edit)
7252 STRCAT(newval, " ++edit");
7253
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007254 if (eap->force_ff != 0)
7255 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007256 eap->force_ff == 'u' ? "unix"
7257 : eap->force_ff == 'd' ? "dos"
7258 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007259 if (eap->force_enc != 0)
7260 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7261 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007262 if (eap->bad_char == BAD_KEEP)
7263 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7264 else if (eap->bad_char == BAD_DROP)
7265 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7266 else if (eap->bad_char != 0)
7267 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007268 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007269 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
7272/*
7273 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007274 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277get_var_tv(
7278 char_u *name,
7279 int len, /* length of "name" */
7280 typval_T *rettv, /* NULL when only checking existence */
7281 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7282 int verbose, /* may give error message */
7283 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
7290 /* truncate the name, so that we can use strcmp() */
7291 cc = name[len];
7292 name[len] = NUL;
7293
7294 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 * Check for user-defined variables.
7296 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007297 v = find_var(name, NULL, no_autoload);
7298 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007300 tv = &v->di_tv;
7301 if (dip != NULL)
7302 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 }
7304
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007307 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007308 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 ret = FAIL;
7310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007311 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
7314 name[len] = cc;
7315
7316 return ret;
7317}
7318
7319/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007320 * Check if variable "name[len]" is a local variable or an argument.
7321 * If so, "*eval_lavars_used" is set to TRUE.
7322 */
7323 static void
7324check_vars(char_u *name, int len)
7325{
7326 int cc;
7327 char_u *varname;
7328 hashtab_T *ht;
7329
7330 if (eval_lavars_used == NULL)
7331 return;
7332
7333 /* truncate the name, so that we can use strcmp() */
7334 cc = name[len];
7335 name[len] = NUL;
7336
7337 ht = find_var_ht(name, &varname);
7338 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7339 {
7340 if (find_var(name, NULL, TRUE) != NULL)
7341 *eval_lavars_used = TRUE;
7342 }
7343
7344 name[len] = cc;
7345}
7346
7347/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007348 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7349 * Also handle function call with Funcref variable: func(expr)
7350 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7351 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353handle_subscript(
7354 char_u **arg,
7355 typval_T *rettv,
7356 int evaluate, /* do more than finding the end */
7357 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007358{
7359 int ret = OK;
7360 dict_T *selfdict = NULL;
7361 char_u *s;
7362 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007363 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007364
Bram Moolenaar61343f02019-07-20 21:11:13 +02007365 // "." is ".name" lookup when we found a dict or when evaluating and
7366 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007367 while (ret == OK
7368 && (**arg == '['
Bram Moolenaar61343f02019-07-20 21:11:13 +02007369 || (**arg == '.' && (rettv->v_type == VAR_DICT
7370 || (!evaluate
7371 && (*arg)[1] != '.'
7372 && current_sctx.sc_version >= 2)))
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007373 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7374 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007375 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007376 {
7377 if (**arg == '(')
7378 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007379 partial_T *pt = NULL;
7380
Bram Moolenaard9fba312005-06-26 22:34:35 +00007381 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007382 if (evaluate)
7383 {
7384 functv = *rettv;
7385 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007386
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007387 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007388 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007389 {
7390 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007391 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007392 }
7393 else
7394 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007395 }
7396 else
7397 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007398 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007399 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007400 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007401
7402 /* Clear the funcref afterwards, so that deleting it while
7403 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007404 if (evaluate)
7405 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007406
7407 /* Stop the expression evaluation when immediately aborting on
7408 * error, or when an interrupt occurred or an exception was thrown
7409 * but not caught. */
7410 if (aborting())
7411 {
7412 if (ret == OK)
7413 clear_tv(rettv);
7414 ret = FAIL;
7415 }
7416 dict_unref(selfdict);
7417 selfdict = NULL;
7418 }
7419 else /* **arg == '[' || **arg == '.' */
7420 {
7421 dict_unref(selfdict);
7422 if (rettv->v_type == VAR_DICT)
7423 {
7424 selfdict = rettv->vval.v_dict;
7425 if (selfdict != NULL)
7426 ++selfdict->dv_refcount;
7427 }
7428 else
7429 selfdict = NULL;
7430 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7431 {
7432 clear_tv(rettv);
7433 ret = FAIL;
7434 }
7435 }
7436 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007437
Bram Moolenaar1d429612016-05-24 15:44:17 +02007438 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7439 * Don't do this when "Func" is already a partial that was bound
7440 * explicitly (pt_auto is FALSE). */
7441 if (selfdict != NULL
7442 && (rettv->v_type == VAR_FUNC
7443 || (rettv->v_type == VAR_PARTIAL
7444 && (rettv->vval.v_partial->pt_auto
7445 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007446 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007447
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007448 dict_unref(selfdict);
7449 return ret;
7450}
7451
7452/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007453 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 * value).
7455 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007456 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007458{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007459 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460}
7461
7462/*
7463 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 * The string "s" must have been allocated, it is consumed.
7465 * Return NULL for out of memory, the variable otherwise.
7466 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007468alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469{
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472 rettv = alloc_tv();
7473 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007475 rettv->v_type = VAR_STRING;
7476 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478 else
7479 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481}
7482
7483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 if (varp != NULL)
7490 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007491 switch (varp->v_type)
7492 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007493 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007494 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007495 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007496 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007497 vim_free(varp->vval.v_string);
7498 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007499 case VAR_PARTIAL:
7500 partial_unref(varp->vval.v_partial);
7501 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007502 case VAR_BLOB:
7503 blob_unref(varp->vval.v_blob);
7504 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007505 case VAR_LIST:
7506 list_unref(varp->vval.v_list);
7507 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007508 case VAR_DICT:
7509 dict_unref(varp->vval.v_dict);
7510 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007511 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007513 job_unref(varp->vval.v_job);
7514 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007515#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007516 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007517#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007518 channel_unref(varp->vval.v_channel);
7519 break;
7520#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007521 case VAR_NUMBER:
7522 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007523 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007524 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 vim_free(varp);
7528 }
7529}
7530
7531/*
7532 * Free the memory for a variable value and set the value to NULL or 0.
7533 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
7537 if (varp != NULL)
7538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007539 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007541 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007543 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007544 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007545 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007546 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007547 case VAR_PARTIAL:
7548 partial_unref(varp->vval.v_partial);
7549 varp->vval.v_partial = NULL;
7550 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007551 case VAR_BLOB:
7552 blob_unref(varp->vval.v_blob);
7553 varp->vval.v_blob = NULL;
7554 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007555 case VAR_LIST:
7556 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007558 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 case VAR_DICT:
7560 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007563 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007564 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 varp->vval.v_number = 0;
7566 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007567 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007568#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569 varp->vval.v_float = 0.0;
7570 break;
7571#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007572 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007573#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007574 job_unref(varp->vval.v_job);
7575 varp->vval.v_job = NULL;
7576#endif
7577 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007578 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007579#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007580 channel_unref(varp->vval.v_channel);
7581 varp->vval.v_channel = NULL;
7582#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583 case VAR_UNKNOWN:
7584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007586 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588}
7589
7590/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591 * Set the value of a variable to NULL without freeing items.
7592 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007594init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595{
7596 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598}
7599
7600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 * Get the number value of a variable.
7602 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007603 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007604 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007605 * caller of incompatible types: it sets *denote to TRUE if "denote"
7606 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007608 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007609tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007611 int error = FALSE;
7612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007614}
7615
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007616 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007617tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007618{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007619 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007624 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007625 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007626#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007627 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007628 break;
7629#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007630 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007631 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007632 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 break;
7634 case VAR_STRING:
7635 if (varp->vval.v_string != NULL)
7636 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007637 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007638 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007639 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007640 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007641 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007642 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007644 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007645 case VAR_SPECIAL:
7646 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7647 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007648 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007650 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007651 break;
7652#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007653 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007654#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007655 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007656 break;
7657#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007658 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007659 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007660 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007661 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007662 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007665 if (denote == NULL) /* useful for values that must be unsigned */
7666 n = -1;
7667 else
7668 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670}
7671
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007672#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007674tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007675{
7676 switch (varp->v_type)
7677 {
7678 case VAR_NUMBER:
7679 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007680 case VAR_FLOAT:
7681 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007682 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007683 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007684 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007685 break;
7686 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007687 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007688 break;
7689 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007690 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007691 break;
7692 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007693 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007694 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007695 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007696 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007697 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007698 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007699# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007701 break;
7702# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007703 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007704# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007705 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007706 break;
7707# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007708 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007709 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007710 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007711 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007712 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007713 break;
7714 }
7715 return 0;
7716}
7717#endif
7718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 * Get the string value of a variable.
7721 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007722 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7723 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 * If the String variable has never been set, return an empty string.
7725 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007726 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007727 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007729 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007730tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 static char_u mybuf[NUMBUFLEN];
7733
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007734 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
7736
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007737 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007738tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007740 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007741
7742 return res != NULL ? res : (char_u *)"";
7743}
7744
Bram Moolenaar7d647822014-04-05 21:28:56 +02007745/*
7746 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7747 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007748 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007749tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007750{
7751 static char_u mybuf[NUMBUFLEN];
7752
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007753 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754}
7755
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007756 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007757tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007758{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007759 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007762 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007763 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007764 return buf;
7765 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007766 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007767 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 break;
7769 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007770 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 break;
7772 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007773 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007775 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007776#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007777 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007778 break;
7779#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780 case VAR_STRING:
7781 if (varp->vval.v_string != NULL)
7782 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007784 case VAR_SPECIAL:
7785 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7786 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007787 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007788 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007789 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007790 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007791#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007792 {
7793 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007794 char *status;
7795
7796 if (job == NULL)
7797 return (char_u *)"no process";
7798 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007799 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007800 : "run";
7801# ifdef UNIX
7802 vim_snprintf((char *)buf, NUMBUFLEN,
7803 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007804# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007805 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007806 "process %ld %s",
7807 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007808 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007809# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007810 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007811 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7812# endif
7813 return buf;
7814 }
7815#endif
7816 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007817 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007818#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007819 {
7820 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007821 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007822
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007823 if (channel == NULL)
7824 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7825 else
7826 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007827 "channel %d %s", channel->ch_id, status);
7828 return buf;
7829 }
7830#endif
7831 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007832 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007833 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007836 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
7839/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007840 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7841 * string() on Dict, List, etc.
7842 */
7843 char_u *
7844tv_stringify(typval_T *varp, char_u *buf)
7845{
7846 if (varp->v_type == VAR_LIST
7847 || varp->v_type == VAR_DICT
7848 || varp->v_type == VAR_FUNC
7849 || varp->v_type == VAR_PARTIAL
7850 || varp->v_type == VAR_FLOAT)
7851 {
7852 typval_T tmp;
7853
7854 f_string(varp, &tmp);
7855 tv_get_string_buf(&tmp, buf);
7856 clear_tv(varp);
7857 *varp = tmp;
7858 return tmp.vval.v_string;
7859 }
7860 return tv_get_string_buf(varp, buf);
7861}
7862
7863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 * Find variable "name" in the list of variables.
7865 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007866 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007867 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007870 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007871find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007875 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
Bram Moolenaara7043832005-01-21 11:56:39 +00007877 ht = find_var_ht(name, &varname);
7878 if (htp != NULL)
7879 *htp = ht;
7880 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007882 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7883 if (ret != NULL)
7884 return ret;
7885
7886 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007887 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007891 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007892 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895find_var_in_ht(
7896 hashtab_T *ht,
7897 int htname,
7898 char_u *varname,
7899 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007900{
Bram Moolenaar33570922005-01-25 22:26:29 +00007901 hashitem_T *hi;
7902
7903 if (*varname == NUL)
7904 {
7905 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007906 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007908 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 case 'g': return &globvars_var;
7910 case 'v': return &vimvars_var;
7911 case 'b': return &curbuf->b_bufvar;
7912 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007913 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007914 case 'l': return get_funccal_local_var();
7915 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 }
7917 return NULL;
7918 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007919
7920 hi = hash_find(ht, varname);
7921 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007922 {
7923 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007924 * worked find the variable again. Don't auto-load a script if it was
7925 * loaded already, otherwise it would be loaded every time when
7926 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007927 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007928 {
7929 /* Note: script_autoload() may make "hi" invalid. It must either
7930 * be obtained again or not used. */
7931 if (!script_autoload(varname, FALSE) || aborting())
7932 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007933 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007934 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007935 if (HASHITEM_EMPTY(hi))
7936 return NULL;
7937 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007939}
7940
7941/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007943 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007944 * Set "varname" to the start of name without ':'.
7945 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007946 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007947find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007949 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007950 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007951
Bram Moolenaar73627d02015-08-11 15:46:09 +02007952 if (name[0] == NUL)
7953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 if (name[1] != ':')
7955 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007956 /* The name must not start with a colon or #. */
7957 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 return NULL;
7959 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007960
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007961 // "version" is "v:version" in all scopes if scriptversion < 3.
7962 // Same for a few other variables marked with VV_COMPAT.
7963 if (current_sctx.sc_version < 3)
7964 {
7965 hi = hash_find(&compat_hashtab, name);
7966 if (!HASHITEM_EMPTY(hi))
7967 return &compat_hashtab;
7968 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007969
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007970 ht = get_funccal_local_ht();
7971 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007973 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 }
7975 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007976 if (*name == 'g') /* global variable */
7977 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02007978 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007979 if (vim_strchr(name + 2, ':') != NULL
7980 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007981 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007983 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007985 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007986 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007987 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 if (*name == 'v') /* v: variable */
7989 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007990 if (*name == 'a') /* a: function argument */
7991 return get_funccal_args_ht();
7992 if (*name == 'l') /* l: local function variable */
7993 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007995 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7996 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 return NULL;
7998}
7999
8000/*
8001 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008002 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * Returns NULL when it doesn't exist.
8004 */
8005 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008006get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
Bram Moolenaar33570922005-01-25 22:26:29 +00008008 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008010 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (v == NULL)
8012 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008013 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014}
8015
8016/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 * sourcing this script and when executing functions defined in the script.
8019 */
8020 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008021new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
Bram Moolenaara7043832005-01-21 11:56:39 +00008023 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 hashtab_T *ht;
8025 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008029 /* Re-allocating ga_data means that an ht_array pointing to
8030 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008032 for (i = 1; i <= ga_scripts.ga_len; ++i)
8033 {
8034 ht = &SCRIPT_VARS(i);
8035 if (ht->ht_mask == HT_INIT_SIZE - 1)
8036 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008037 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008039 }
8040
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 while (ga_scripts.ga_len < id)
8042 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008043 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008044 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008045 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048 }
8049}
8050
8051/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008052 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8053 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 */
8055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008056init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008059 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008060 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008061 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008062 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008063 dict_var->di_tv.vval.v_dict = dict;
8064 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008065 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8067 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
8070/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008071 * Unreference a dictionary initialized by init_var_dict().
8072 */
8073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008074unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008075{
8076 /* Now the dict needs to be freed if no one else is using it, go back to
8077 * normal reference counting. */
8078 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8079 dict_unref(dict);
8080}
8081
8082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 * Frees all allocated variables and the value they contain.
8085 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 */
8087 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008088vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008089{
8090 vars_clear_ext(ht, TRUE);
8091}
8092
8093/*
8094 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8095 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
Bram Moolenaara7043832005-01-21 11:56:39 +00008099 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 hashitem_T *hi;
8101 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102
Bram Moolenaar33570922005-01-25 22:26:29 +00008103 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008105 for (hi = ht->ht_array; todo > 0; ++hi)
8106 {
8107 if (!HASHITEM_EMPTY(hi))
8108 {
8109 --todo;
8110
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008112 * ht_array might change then. hash_clear() takes care of it
8113 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 v = HI2DI(hi);
8115 if (free_val)
8116 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008117 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008119 }
8120 }
8121 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008122 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123}
8124
Bram Moolenaara7043832005-01-21 11:56:39 +00008125/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 * Delete a variable from hashtab "ht" at item "hi".
8127 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008130delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008133
8134 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 clear_tv(&di->di_tv);
8136 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137}
8138
8139/*
8140 * List the value of one internal variable.
8141 */
8142 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008143list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 char_u *tofree;
8146 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008147 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008149 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008151 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008156list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008157 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008158 char_u *name,
8159 int type,
8160 char_u *string,
8161 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
Bram Moolenaar31859182007-08-14 20:41:13 +00008163 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8164 msg_start();
8165 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008167 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 msg_putchar(' ');
8169 msg_advance(22);
8170 if (type == VAR_NUMBER)
8171 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008172 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 msg_putchar('*');
8174 else if (type == VAR_LIST)
8175 {
8176 msg_putchar('[');
8177 if (*string == '[')
8178 ++string;
8179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008180 else if (type == VAR_DICT)
8181 {
8182 msg_putchar('{');
8183 if (*string == '{')
8184 ++string;
8185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 else
8187 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008190
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008191 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008192 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008193 if (*first)
8194 {
8195 msg_clr_eos();
8196 *first = FALSE;
8197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198}
8199
8200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 * If the variable already exists, the value is updated.
8203 * Otherwise the variable is created.
8204 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008206set_var(
8207 char_u *name,
8208 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008209 int copy) // make copy of value in "tv"
8210{
8211 set_var_const(name, tv, copy, FALSE);
8212}
8213
8214/*
8215 * Set variable "name" to value in "tv".
8216 * If the variable already exists and "is_const" is FALSE the value is updated.
8217 * Otherwise the variable is created.
8218 */
8219 static void
8220set_var_const(
8221 char_u *name,
8222 typval_T *tv,
8223 int copy, // make copy of value in "tv"
8224 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008230 ht = find_var_ht(name, &varname);
8231 if (ht == NULL || *varname == NUL)
8232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008233 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008234 return;
8235 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008236 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008237
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008238 /* Search in parent scope which is possible to reference from lambda */
8239 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008240 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008241
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008242 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8243 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008244 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245
Bram Moolenaar33570922005-01-25 22:26:29 +00008246 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008248 if (is_const)
8249 {
8250 emsg(_(e_cannot_mod));
8251 return;
8252 }
8253
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008255 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008256 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008258
8259 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008260 * Handle setting internal v: variables separately where needed to
8261 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008262 */
8263 if (ht == &vimvarht)
8264 {
8265 if (v->di_tv.v_type == VAR_STRING)
8266 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008267 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008269 {
8270 char_u *val = tv_get_string(tv);
8271
8272 // Careful: when assigning to v:errmsg and tv_get_string()
8273 // causes an error message the variable will alrady be set.
8274 if (v->di_tv.vval.v_string == NULL)
8275 v->di_tv.vval.v_string = vim_strsave(val);
8276 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 else
8278 {
8279 /* Take over the string to avoid an extra alloc/free. */
8280 v->di_tv.vval.v_string = tv->vval.v_string;
8281 tv->vval.v_string = NULL;
8282 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008283 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008285 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008287 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008288 if (STRCMP(varname, "searchforward") == 0)
8289 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008290#ifdef FEAT_SEARCH_EXTRA
8291 else if (STRCMP(varname, "hlsearch") == 0)
8292 {
8293 no_hlsearch = !v->di_tv.vval.v_number;
8294 redraw_all_later(SOME_VALID);
8295 }
8296#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008297 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008299 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008301 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008302 return;
8303 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 }
8305
8306 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else /* add a new variable */
8309 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008310 // Can't add "v:" or "a:" variable.
8311 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008313 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008314 return;
8315 }
8316
Bram Moolenaar31b81602019-02-10 22:14:27 +01008317 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008318 if (!valid_varname(varname))
8319 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008320
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008321 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008322 if (v == NULL)
8323 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008327 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 return;
8329 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008330 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008331 if (is_const)
8332 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008334
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008335 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008337 else
8338 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008340 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008342 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008343
8344 if (is_const)
8345 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008349 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 * Also give an error message.
8351 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008353var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008354{
8355 if (flags & DI_FLAGS_RO)
8356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008357 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 return TRUE;
8359 }
8360 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008362 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008363 return TRUE;
8364 }
8365 return FALSE;
8366}
8367
8368/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008369 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8370 * Also give an error message.
8371 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008373var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008374{
8375 if (flags & DI_FLAGS_FIX)
8376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008377 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008378 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008379 return TRUE;
8380 }
8381 return FALSE;
8382}
8383
8384/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008385 * Check if a funcref is assigned to a valid variable name.
8386 * Return TRUE and give an error if not.
8387 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008389var_check_func_name(
8390 char_u *name, /* points to start of variable name */
8391 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008392{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008393 /* Allow for w: b: s: and t:. */
8394 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008395 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8396 ? name[2] : name[0]))
8397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008398 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008399 name);
8400 return TRUE;
8401 }
8402 /* Don't allow hiding a function. When "v" is not NULL we might be
8403 * assigning another function to the same var, the type is checked
8404 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008405 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008407 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008408 name);
8409 return TRUE;
8410 }
8411 return FALSE;
8412}
8413
8414/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008415 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008416 * Also give an error message, using "name" or _("name") when use_gettext is
8417 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008418 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008419 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008420var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008421{
8422 if (lock & VAR_LOCKED)
8423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008424 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008425 name == NULL ? (char_u *)_("Unknown")
8426 : use_gettext ? (char_u *)_(name)
8427 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008428 return TRUE;
8429 }
8430 if (lock & VAR_FIXED)
8431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008432 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008433 name == NULL ? (char_u *)_("Unknown")
8434 : use_gettext ? (char_u *)_(name)
8435 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008436 return TRUE;
8437 }
8438 return FALSE;
8439}
8440
8441/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008442 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8443 * Also give an error message, using "name" or _("name") when use_gettext is
8444 * TRUE.
8445 */
8446 static int
8447tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8448{
8449 int lock = 0;
8450
8451 switch (tv->v_type)
8452 {
8453 case VAR_BLOB:
8454 if (tv->vval.v_blob != NULL)
8455 lock = tv->vval.v_blob->bv_lock;
8456 break;
8457 case VAR_LIST:
8458 if (tv->vval.v_list != NULL)
8459 lock = tv->vval.v_list->lv_lock;
8460 break;
8461 case VAR_DICT:
8462 if (tv->vval.v_dict != NULL)
8463 lock = tv->vval.v_dict->dv_lock;
8464 break;
8465 default:
8466 break;
8467 }
8468 return var_check_lock(tv->v_lock, name, use_gettext)
8469 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8470}
8471
8472/*
8473 * Check if a variable name is valid.
8474 * Return FALSE and give an error if not.
8475 */
8476 int
8477valid_varname(char_u *varname)
8478{
8479 char_u *p;
8480
8481 for (p = varname; *p != NUL; ++p)
8482 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8483 && *p != AUTOLOAD_CHAR)
8484 {
8485 semsg(_(e_illvar), varname);
8486 return FALSE;
8487 }
8488 return TRUE;
8489}
8490
8491/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008493 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008494 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008495 * It is OK for "from" and "to" to point to the same item. This is used to
8496 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008498 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008499copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008502 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 switch (from->v_type)
8504 {
8505 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008506 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 to->vval.v_number = from->vval.v_number;
8508 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008509 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008510#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008511 to->vval.v_float = from->vval.v_float;
8512 break;
8513#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008514 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008516 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008517 if (to->vval.v_job != NULL)
8518 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008519 break;
8520#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008521 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008522#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008523 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008524 if (to->vval.v_channel != NULL)
8525 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008526 break;
8527#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 case VAR_STRING:
8529 case VAR_FUNC:
8530 if (from->vval.v_string == NULL)
8531 to->vval.v_string = NULL;
8532 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008535 if (from->v_type == VAR_FUNC)
8536 func_ref(to->vval.v_string);
8537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008539 case VAR_PARTIAL:
8540 if (from->vval.v_partial == NULL)
8541 to->vval.v_partial = NULL;
8542 else
8543 {
8544 to->vval.v_partial = from->vval.v_partial;
8545 ++to->vval.v_partial->pt_refcount;
8546 }
8547 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008548 case VAR_BLOB:
8549 if (from->vval.v_blob == NULL)
8550 to->vval.v_blob = NULL;
8551 else
8552 {
8553 to->vval.v_blob = from->vval.v_blob;
8554 ++to->vval.v_blob->bv_refcount;
8555 }
8556 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 case VAR_LIST:
8558 if (from->vval.v_list == NULL)
8559 to->vval.v_list = NULL;
8560 else
8561 {
8562 to->vval.v_list = from->vval.v_list;
8563 ++to->vval.v_list->lv_refcount;
8564 }
8565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 case VAR_DICT:
8567 if (from->vval.v_dict == NULL)
8568 to->vval.v_dict = NULL;
8569 else
8570 {
8571 to->vval.v_dict = from->vval.v_dict;
8572 ++to->vval.v_dict->dv_refcount;
8573 }
8574 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008575 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008576 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577 break;
8578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579}
8580
8581/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008582 * Make a copy of an item.
8583 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8585 * reference to an already copied list/dict can be used.
8586 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008588 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008589item_copy(
8590 typval_T *from,
8591 typval_T *to,
8592 int deep,
8593 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008594{
8595 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008596 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008600 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008601 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 }
8603 ++recurse;
8604
8605 switch (from->v_type)
8606 {
8607 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008608 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008609 case VAR_STRING:
8610 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008611 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008612 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008613 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008614 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008615 copy_tv(from, to);
8616 break;
8617 case VAR_LIST:
8618 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008619 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008620 if (from->vval.v_list == NULL)
8621 to->vval.v_list = NULL;
8622 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8623 {
8624 /* use the copy made earlier */
8625 to->vval.v_list = from->vval.v_list->lv_copylist;
8626 ++to->vval.v_list->lv_refcount;
8627 }
8628 else
8629 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8630 if (to->vval.v_list == NULL)
8631 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008632 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008633 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008634 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008635 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008636 case VAR_DICT:
8637 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008638 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008639 if (from->vval.v_dict == NULL)
8640 to->vval.v_dict = NULL;
8641 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8642 {
8643 /* use the copy made earlier */
8644 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8645 ++to->vval.v_dict->dv_refcount;
8646 }
8647 else
8648 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8649 if (to->vval.v_dict == NULL)
8650 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008652 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008653 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008654 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655 }
8656 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008657 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008658}
8659
8660/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661 * This function is used by f_input() and f_inputdialog() functions. The third
8662 * argument to f_input() specifies the type of completion to use at the
8663 * prompt. The third argument to f_inputdialog() specifies the value to return
8664 * when the user cancels the prompt.
8665 */
8666 void
8667get_user_input(
8668 typval_T *argvars,
8669 typval_T *rettv,
8670 int inputdialog,
8671 int secret)
8672{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008673 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 char_u *p = NULL;
8675 int c;
8676 char_u buf[NUMBUFLEN];
8677 int cmd_silent_save = cmd_silent;
8678 char_u *defstr = (char_u *)"";
8679 int xp_type = EXPAND_NOTHING;
8680 char_u *xp_arg = NULL;
8681
8682 rettv->v_type = VAR_STRING;
8683 rettv->vval.v_string = NULL;
8684
8685#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008686 /* While starting up, there is no place to enter text. When running tests
8687 * with --not-a-term we assume feedkeys() will be used. */
8688 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689 return;
8690#endif
8691
8692 cmd_silent = FALSE; /* Want to see the prompt. */
8693 if (prompt != NULL)
8694 {
8695 /* Only the part of the message after the last NL is considered as
8696 * prompt for the command line */
8697 p = vim_strrchr(prompt, '\n');
8698 if (p == NULL)
8699 p = prompt;
8700 else
8701 {
8702 ++p;
8703 c = *p;
8704 *p = NUL;
8705 msg_start();
8706 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008707 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 msg_didout = FALSE;
8709 msg_starthere();
8710 *p = c;
8711 }
8712 cmdline_row = msg_row;
8713
8714 if (argvars[1].v_type != VAR_UNKNOWN)
8715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008716 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 if (defstr != NULL)
8718 stuffReadbuffSpec(defstr);
8719
8720 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8721 {
8722 char_u *xp_name;
8723 int xp_namelen;
8724 long argt;
8725
8726 /* input() with a third argument: completion */
8727 rettv->vval.v_string = NULL;
8728
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008729 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 if (xp_name == NULL)
8731 return;
8732
8733 xp_namelen = (int)STRLEN(xp_name);
8734
8735 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8736 &xp_arg) == FAIL)
8737 return;
8738 }
8739 }
8740
8741 if (defstr != NULL)
8742 {
8743 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 ex_normal_busy = 0;
8746 rettv->vval.v_string =
8747 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8748 xp_type, xp_arg);
8749 ex_normal_busy = save_ex_normal_busy;
8750 }
8751 if (inputdialog && rettv->vval.v_string == NULL
8752 && argvars[1].v_type != VAR_UNKNOWN
8753 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 &argvars[2], buf));
8756
8757 vim_free(xp_arg);
8758
8759 /* since the user typed this, no need to wait for return */
8760 need_wait_return = FALSE;
8761 msg_didout = FALSE;
8762 }
8763 cmd_silent = cmd_silent_save;
8764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * ":echo expr1 ..." print each argument separated with a space, add a
8768 * newline at the end.
8769 * ":echon expr1 ..." print each argument plain.
8770 */
8771 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008772ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008776 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 char_u *p;
8778 int needclr = TRUE;
8779 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008780 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008781 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008782 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 if (eap->skip)
8785 ++emsg_skip;
8786 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008788 /* If eval1() causes an error message the text from the command may
8789 * still need to be cleared. E.g., "echo 22,44". */
8790 need_clr_eos = needclr;
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
8795 /*
8796 * Report the invalid expression unless the expression evaluation
8797 * has been cancelled due to an aborting error, an interrupt, or an
8798 * exception.
8799 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008800 if (!aborting() && did_emsg == did_emsg_before
8801 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008802 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008803 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 break;
8805 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008806 need_clr_eos = FALSE;
8807
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 if (!eap->skip)
8809 {
8810 if (atstart)
8811 {
8812 atstart = FALSE;
8813 /* Call msg_start() after eval1(), evaluating the expression
8814 * may cause a message to appear. */
8815 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008816 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008817 /* Mark the saved text as finishing the line, so that what
8818 * follows is displayed on a new line when scrolling back
8819 * at the more prompt. */
8820 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 }
8824 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008825 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008826 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008827 if (p != NULL)
8828 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008830 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 if (*p != TAB && needclr)
8833 {
8834 /* remove any text still there from the command */
8835 msg_clr_eos();
8836 needclr = FALSE;
8837 }
8838 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 }
8840 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008841 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008842 if (has_mbyte)
8843 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008844 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008845
8846 (void)msg_outtrans_len_attr(p, i, echo_attr);
8847 p += i - 1;
8848 }
8849 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008850 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008853 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 arg = skipwhite(arg);
8857 }
8858 eap->nextcmd = check_nextcmd(arg);
8859
8860 if (eap->skip)
8861 --emsg_skip;
8862 else
8863 {
8864 /* remove text that may still be there from the command */
8865 if (needclr)
8866 msg_clr_eos();
8867 if (eap->cmdidx == CMD_echo)
8868 msg_end();
8869 }
8870}
8871
8872/*
8873 * ":echohl {name}".
8874 */
8875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008878 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880
8881/*
8882 * ":execute expr1 ..." execute the result of an expression.
8883 * ":echomsg expr1 ..." Print a message
8884 * ":echoerr expr1 ..." Print an error
8885 * Each gets spaces around each argument and a newline at the end for
8886 * echo commands
8887 */
8888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008889ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 int ret = OK;
8894 char_u *p;
8895 garray_T ga;
8896 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008897 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
8899 ga_init2(&ga, 1, 80);
8900
8901 if (eap->skip)
8902 ++emsg_skip;
8903 while (*arg != NUL && *arg != '|' && *arg != '\n')
8904 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008905 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8906 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909 if (!eap->skip)
8910 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008911 char_u buf[NUMBUFLEN];
8912
8913 if (eap->cmdidx == CMD_execute)
8914 p = tv_get_string_buf(&rettv, buf);
8915 else
8916 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 len = (int)STRLEN(p);
8918 if (ga_grow(&ga, len + 2) == FAIL)
8919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 ret = FAIL;
8922 break;
8923 }
8924 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 ga.ga_len += len;
8928 }
8929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 arg = skipwhite(arg);
8932 }
8933
8934 if (ret != FAIL && ga.ga_data != NULL)
8935 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008936 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8937 {
8938 /* Mark the already saved text as finishing the line, so that what
8939 * follows is displayed on a new line when scrolling back at the
8940 * more prompt. */
8941 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008942 }
8943
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008945 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008946 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008947 out_flush();
8948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 else if (eap->cmdidx == CMD_echoerr)
8950 {
8951 /* We don't want to abort following commands, restore did_emsg. */
8952 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008953 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (!force_abort)
8955 did_emsg = save_did_emsg;
8956 }
8957 else if (eap->cmdidx == CMD_execute)
8958 do_cmdline((char_u *)ga.ga_data,
8959 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8960 }
8961
8962 ga_clear(&ga);
8963
8964 if (eap->skip)
8965 --emsg_skip;
8966
8967 eap->nextcmd = check_nextcmd(arg);
8968}
8969
8970/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 * Find window specified by "vp" in tabpage "tp".
8972 */
8973 win_T *
8974find_win_by_nr(
8975 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008976 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008978 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008979 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 if (nr < 0)
8982 return NULL;
8983 if (nr == 0)
8984 return curwin;
8985
Bram Moolenaar29323592016-07-24 22:04:11 +02008986 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8987 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 if (nr >= LOWEST_WIN_ID)
8989 {
8990 if (wp->w_id == nr)
8991 return wp;
8992 }
8993 else if (--nr <= 0)
8994 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008995 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008997 {
8998#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008999 // check tab-local popup windows
9000 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009001 if (wp->w_id == nr)
9002 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009003 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009004 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9005 if (wp->w_id == nr)
9006 return wp;
9007#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009009 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011}
9012
9013/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009014 * Find a window: When using a Window ID in any tab page, when using a number
9015 * in the current tab page.
9016 */
9017 win_T *
9018find_win_by_nr_or_id(typval_T *vp)
9019{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009020 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009021
9022 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009023 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009024 return find_win_by_nr(vp, NULL);
9025}
9026
9027/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009029 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 */
9031 win_T *
9032find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009033 typval_T *wvp, // VAR_UNKNOWN for current window
9034 typval_T *tvp, // VAR_UNKNOWN for current tab page
9035 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036{
9037 win_T *wp = NULL;
9038 tabpage_T *tp = NULL;
9039 long n;
9040
9041 if (wvp->v_type != VAR_UNKNOWN)
9042 {
9043 if (tvp->v_type != VAR_UNKNOWN)
9044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009045 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046 if (n >= 0)
9047 tp = find_tabpage(n);
9048 }
9049 else
9050 tp = curtab;
9051
9052 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009053 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009055 if (wp == NULL && wvp->v_type == VAR_NUMBER
9056 && wvp->vval.v_number != -1)
9057 // A window with the specified number is not found
9058 tp = NULL;
9059 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 }
9061 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009062 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009064 tp = curtab;
9065 }
9066
9067 if (ptp != NULL)
9068 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069
9070 return wp;
9071}
9072
9073/*
9074 * getwinvar() and gettabwinvar()
9075 */
9076 void
9077getwinvar(
9078 typval_T *argvars,
9079 typval_T *rettv,
9080 int off) /* 1 for gettabwinvar() */
9081{
9082 win_T *win;
9083 char_u *varname;
9084 dictitem_T *v;
9085 tabpage_T *tp = NULL;
9086 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 win_T *oldcurwin;
9088 tabpage_T *oldtabpage;
9089 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009092 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 else
9094 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009096 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 ++emsg_off;
9098
9099 rettv->v_type = VAR_STRING;
9100 rettv->vval.v_string = NULL;
9101
9102 if (win != NULL && varname != NULL)
9103 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 /* Set curwin to be our win, temporarily. Also set the tabpage,
9105 * otherwise the window is not valid. Only do this when needed,
9106 * autocommands get blocked. */
9107 need_switch_win = !(tp == curtab && win == curwin);
9108 if (!need_switch_win
9109 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009111 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009113 if (varname[1] == NUL)
9114 {
9115 /* get all window-local options in a dict */
9116 dict_T *opts = get_winbuf_options(FALSE);
9117
9118 if (opts != NULL)
9119 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009120 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009121 done = TRUE;
9122 }
9123 }
9124 else if (get_option_tv(&varname, rettv, 1) == OK)
9125 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 done = TRUE;
9127 }
9128 else
9129 {
9130 /* Look up the variable. */
9131 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9132 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9133 varname, FALSE);
9134 if (v != NULL)
9135 {
9136 copy_tv(&v->di_tv, rettv);
9137 done = TRUE;
9138 }
9139 }
9140 }
9141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142 if (need_switch_win)
9143 /* restore previous notion of curwin */
9144 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 }
9146
9147 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9148 /* use the default return value */
9149 copy_tv(&argvars[off + 2], rettv);
9150
9151 --emsg_off;
9152}
9153
9154/*
9155 * "setwinvar()" and "settabwinvar()" functions
9156 */
9157 void
9158setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9159{
9160 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 win_T *save_curwin;
9162 tabpage_T *save_curtab;
9163 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009164 char_u *varname, *winvarname;
9165 typval_T *varp;
9166 char_u nbuf[NUMBUFLEN];
9167 tabpage_T *tp = NULL;
9168
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009169 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 return;
9171
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009173 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174 else
9175 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009176 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009177 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 varp = &argvars[off + 2];
9179
9180 if (win != NULL && varname != NULL && varp != NULL)
9181 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 need_switch_win = !(tp == curtab && win == curwin);
9183 if (!need_switch_win
9184 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185 {
9186 if (*varname == '&')
9187 {
9188 long numval;
9189 char_u *strval;
9190 int error = FALSE;
9191
9192 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009193 numval = (long)tv_get_number_chk(varp, &error);
9194 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 if (!error && strval != NULL)
9196 set_option_value(varname, numval, strval, OPT_LOCAL);
9197 }
9198 else
9199 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009200 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 if (winvarname != NULL)
9202 {
9203 STRCPY(winvarname, "w:");
9204 STRCPY(winvarname + 2, varname);
9205 set_var(winvarname, varp, TRUE);
9206 vim_free(winvarname);
9207 }
9208 }
9209 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009210 if (need_switch_win)
9211 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009212 }
9213}
9214
9215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9217 * "arg" points to the "&" or '+' when called, to "option" when returning.
9218 * Returns NULL when no option name found. Otherwise pointer to the char
9219 * after the option name.
9220 */
9221 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009222find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 char_u *p = *arg;
9225
9226 ++p;
9227 if (*p == 'g' && p[1] == ':')
9228 {
9229 *opt_flags = OPT_GLOBAL;
9230 p += 2;
9231 }
9232 else if (*p == 'l' && p[1] == ':')
9233 {
9234 *opt_flags = OPT_LOCAL;
9235 p += 2;
9236 }
9237 else
9238 *opt_flags = 0;
9239
9240 if (!ASCII_ISALPHA(*p))
9241 return NULL;
9242 *arg = p;
9243
9244 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9245 p += 4; /* termcap option */
9246 else
9247 while (ASCII_ISALPHA(*p))
9248 ++p;
9249 return p;
9250}
9251
9252/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009253 * Return the autoload script name for a function or variable name.
9254 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009255 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009257 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009258autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009259{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009260 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009261 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009262
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009263 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009264 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009265 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009266 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009267 STRCPY(scriptname, "autoload/");
9268 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009269 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9270 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009271 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009272 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009273 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274}
9275
9276/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009277 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009278 * Return TRUE if a package was loaded.
9279 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009281script_autoload(
9282 char_u *name,
9283 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009284{
9285 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009286 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009287 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009288 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009289
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009290 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009291 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009292 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009293 return FALSE;
9294
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009295 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009296
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009297 /* Find the name in the list of previously loaded package names. Skip
9298 * "autoload/", it's always the same. */
9299 for (i = 0; i < ga_loaded.ga_len; ++i)
9300 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9301 break;
9302 if (!reload && i < ga_loaded.ga_len)
9303 ret = FALSE; /* was loaded already */
9304 else
9305 {
9306 /* Remember the name if it wasn't loaded already. */
9307 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9308 {
9309 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9310 tofree = NULL;
9311 }
9312
9313 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009314 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009315 ret = TRUE;
9316 }
9317
9318 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009319 return ret;
9320}
9321
Bram Moolenaar661b1822005-07-28 22:36:45 +00009322/*
9323 * Display script name where an item was last set.
9324 * Should only be invoked when 'verbose' is non-zero.
9325 */
9326 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009327last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009328{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009329 char_u *p;
9330
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009331 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009332 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009333 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009334 if (p != NULL)
9335 {
9336 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009337 msg_puts(_("\n\tLast set from "));
9338 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009339 if (script_ctx.sc_lnum > 0)
9340 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009341 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009342 msg_outnum((long)script_ctx.sc_lnum);
9343 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009344 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009345 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009346 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009347 }
9348}
9349
Bram Moolenaar61be3762019-03-19 23:04:17 +01009350/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009351 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9352 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009353 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009356{
9357 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9358 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009359 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9360 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009361 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009362 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009363}
9364
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 * Add an assert error to v:errors.
9367 */
9368 void
9369assert_error(garray_T *gap)
9370{
9371 struct vimvar *vp = &vimvars[VV_ERRORS];
9372
9373 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9374 /* Make sure v:errors is a list. */
9375 set_vim_var_list(VV_ERRORS, list_alloc());
9376 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9377}
Bram Moolenaar31988702018-02-13 12:57:42 +01009378/*
9379 * Compare "typ1" and "typ2". Put the result in "typ1".
9380 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009381 int
9382typval_compare(
9383 typval_T *typ1, /* first operand */
9384 typval_T *typ2, /* second operand */
9385 exptype_T type, /* operator */
9386 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009387 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009388{
9389 int i;
9390 varnumber_T n1, n2;
9391 char_u *s1, *s2;
9392 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9393
Bram Moolenaar31988702018-02-13 12:57:42 +01009394 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009395 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009396 /* For "is" a different type always means FALSE, for "notis"
9397 * it means TRUE. */
9398 n1 = (type == TYPE_NEQUAL);
9399 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009400 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9401 {
9402 if (type_is)
9403 {
9404 n1 = (typ1->v_type == typ2->v_type
9405 && typ1->vval.v_blob == typ2->vval.v_blob);
9406 if (type == TYPE_NEQUAL)
9407 n1 = !n1;
9408 }
9409 else if (typ1->v_type != typ2->v_type
9410 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9411 {
9412 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009413 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009414 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009415 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009416 clear_tv(typ1);
9417 return FAIL;
9418 }
9419 else
9420 {
9421 // Compare two Blobs for being equal or unequal.
9422 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9423 if (type == TYPE_NEQUAL)
9424 n1 = !n1;
9425 }
9426 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009427 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9428 {
9429 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009430 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009431 n1 = (typ1->v_type == typ2->v_type
9432 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009433 if (type == TYPE_NEQUAL)
9434 n1 = !n1;
9435 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009436 else if (typ1->v_type != typ2->v_type
9437 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009438 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009439 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009440 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009441 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009442 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009443 clear_tv(typ1);
9444 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009445 }
9446 else
9447 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009448 /* Compare two Lists for being equal or unequal. */
9449 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9450 ic, FALSE);
9451 if (type == TYPE_NEQUAL)
9452 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009453 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009454 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009455
9456 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9457 {
9458 if (type_is)
9459 {
9460 n1 = (typ1->v_type == typ2->v_type
9461 && typ1->vval.v_dict == typ2->vval.v_dict);
9462 if (type == TYPE_NEQUAL)
9463 n1 = !n1;
9464 }
9465 else if (typ1->v_type != typ2->v_type
9466 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9467 {
9468 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009469 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009470 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009471 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009472 clear_tv(typ1);
9473 return FAIL;
9474 }
9475 else
9476 {
9477 /* Compare two Dictionaries for being equal or unequal. */
9478 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9479 ic, FALSE);
9480 if (type == TYPE_NEQUAL)
9481 n1 = !n1;
9482 }
9483 }
9484
9485 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9486 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9487 {
9488 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009490 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009491 clear_tv(typ1);
9492 return FAIL;
9493 }
9494 if ((typ1->v_type == VAR_PARTIAL
9495 && typ1->vval.v_partial == NULL)
9496 || (typ2->v_type == VAR_PARTIAL
9497 && typ2->vval.v_partial == NULL))
9498 /* when a partial is NULL assume not equal */
9499 n1 = FALSE;
9500 else if (type_is)
9501 {
9502 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9503 /* strings are considered the same if their value is
9504 * the same */
9505 n1 = tv_equal(typ1, typ2, ic, FALSE);
9506 else if (typ1->v_type == VAR_PARTIAL
9507 && typ2->v_type == VAR_PARTIAL)
9508 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9509 else
9510 n1 = FALSE;
9511 }
9512 else
9513 n1 = tv_equal(typ1, typ2, ic, FALSE);
9514 if (type == TYPE_NEQUAL)
9515 n1 = !n1;
9516 }
9517
9518#ifdef FEAT_FLOAT
9519 /*
9520 * If one of the two variables is a float, compare as a float.
9521 * When using "=~" or "!~", always compare as string.
9522 */
9523 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9524 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9525 {
9526 float_T f1, f2;
9527
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009528 f1 = tv_get_float(typ1);
9529 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009530 n1 = FALSE;
9531 switch (type)
9532 {
9533 case TYPE_EQUAL: n1 = (f1 == f2); break;
9534 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9535 case TYPE_GREATER: n1 = (f1 > f2); break;
9536 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9537 case TYPE_SMALLER: n1 = (f1 < f2); break;
9538 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9539 case TYPE_UNKNOWN:
9540 case TYPE_MATCH:
9541 case TYPE_NOMATCH: break; /* avoid gcc warning */
9542 }
9543 }
9544#endif
9545
9546 /*
9547 * If one of the two variables is a number, compare as a number.
9548 * When using "=~" or "!~", always compare as string.
9549 */
9550 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9551 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009553 n1 = tv_get_number(typ1);
9554 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009555 switch (type)
9556 {
9557 case TYPE_EQUAL: n1 = (n1 == n2); break;
9558 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9559 case TYPE_GREATER: n1 = (n1 > n2); break;
9560 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9561 case TYPE_SMALLER: n1 = (n1 < n2); break;
9562 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9563 case TYPE_UNKNOWN:
9564 case TYPE_MATCH:
9565 case TYPE_NOMATCH: break; /* avoid gcc warning */
9566 }
9567 }
9568 else
9569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009570 s1 = tv_get_string_buf(typ1, buf1);
9571 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009572 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9573 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9574 else
9575 i = 0;
9576 n1 = FALSE;
9577 switch (type)
9578 {
9579 case TYPE_EQUAL: n1 = (i == 0); break;
9580 case TYPE_NEQUAL: n1 = (i != 0); break;
9581 case TYPE_GREATER: n1 = (i > 0); break;
9582 case TYPE_GEQUAL: n1 = (i >= 0); break;
9583 case TYPE_SMALLER: n1 = (i < 0); break;
9584 case TYPE_SEQUAL: n1 = (i <= 0); break;
9585
9586 case TYPE_MATCH:
9587 case TYPE_NOMATCH:
9588 n1 = pattern_match(s2, s1, ic);
9589 if (type == TYPE_NOMATCH)
9590 n1 = !n1;
9591 break;
9592
9593 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9594 }
9595 }
9596 clear_tv(typ1);
9597 typ1->v_type = VAR_NUMBER;
9598 typ1->vval.v_number = n1;
9599
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009600 return OK;
9601}
9602
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009603 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009604typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009605{
9606 char_u *tofree;
9607 char_u numbuf[NUMBUFLEN];
9608 char_u *ret = NULL;
9609
9610 if (arg == NULL)
9611 return vim_strsave((char_u *)"(does not exist)");
9612 ret = tv2string(arg, &tofree, numbuf, 0);
9613 /* Make a copy if we have a value but it's not in allocated memory. */
9614 if (ret != NULL && tofree == NULL)
9615 ret = vim_strsave(ret);
9616 return ret;
9617}
9618
9619 int
9620var_exists(char_u *var)
9621{
9622 char_u *name;
9623 char_u *tofree;
9624 typval_T tv;
9625 int len = 0;
9626 int n = FALSE;
9627
9628 /* get_name_len() takes care of expanding curly braces */
9629 name = var;
9630 len = get_name_len(&var, &tofree, TRUE, FALSE);
9631 if (len > 0)
9632 {
9633 if (tofree != NULL)
9634 name = tofree;
9635 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9636 if (n)
9637 {
9638 /* handle d.key, l[idx], f(expr) */
9639 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9640 if (n)
9641 clear_tv(&tv);
9642 }
9643 }
9644 if (*var != NUL)
9645 n = FALSE;
9646
9647 vim_free(tofree);
9648 return n;
9649}
9650
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#endif /* FEAT_EVAL */
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009654#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655
Bram Moolenaar4f974752019-02-17 17:44:42 +01009656#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657/*
9658 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
9661/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009662 * Get the short path (8.3) for the filename in "fnamep".
9663 * Only works for a valid file name.
9664 * When the path gets longer "fnamep" is changed and the allocated buffer
9665 * is put in "bufp".
9666 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9667 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 */
9669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009670get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009672 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 char_u *newbuf;
9674
9675 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009676 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (l > len - 1)
9678 {
9679 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009680 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 newbuf = vim_strnsave(*fnamep, l);
9682 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009683 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684
9685 vim_free(*bufp);
9686 *fnamep = *bufp = newbuf;
9687
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009688 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009689 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 }
9691
9692 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009693 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694}
9695
9696/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009697 * Get the short path (8.3) for the filename in "fname". The converted
9698 * path is returned in "bufp".
9699 *
9700 * Some of the directories specified in "fname" may not exist. This function
9701 * will shorten the existing directories at the beginning of the path and then
9702 * append the remaining non-existing path.
9703 *
9704 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009705 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009706 * bufp - Pointer to an allocated buffer for the filename.
9707 * fnamelen - Length of the filename pointed to by fname
9708 *
9709 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 */
9711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009712shortpath_for_invalid_fname(
9713 char_u **fname,
9714 char_u **bufp,
9715 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009717 char_u *short_fname, *save_fname, *pbuf_unused;
9718 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009720 int old_len, len;
9721 int new_len, sfx_len;
9722 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723
9724 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009725 old_len = *fnamelen;
9726 save_fname = vim_strnsave(*fname, old_len);
9727 pbuf_unused = NULL;
9728 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009730 endp = save_fname + old_len - 1; /* Find the end of the copy */
9731 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009733 /*
9734 * Try shortening the supplied path till it succeeds by removing one
9735 * directory at a time from the tail of the path.
9736 */
9737 len = 0;
9738 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009740 /* go back one path-separator */
9741 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9742 --endp;
9743 if (endp <= save_fname)
9744 break; /* processed the complete path */
9745
9746 /*
9747 * Replace the path separator with a NUL and try to shorten the
9748 * resulting path.
9749 */
9750 ch = *endp;
9751 *endp = 0;
9752 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009753 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009754 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9755 {
9756 retval = FAIL;
9757 goto theend;
9758 }
9759 *endp = ch; /* preserve the string */
9760
9761 if (len > 0)
9762 break; /* successfully shortened the path */
9763
9764 /* failed to shorten the path. Skip the path separator */
9765 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 }
9767
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009768 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009770 /*
9771 * Succeeded in shortening the path. Now concatenate the shortened
9772 * path with the remaining path at the tail.
9773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009775 /* Compute the length of the new path. */
9776 sfx_len = (int)(save_endp - endp) + 1;
9777 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009779 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009781 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009783 /* There is not enough space in the currently allocated string,
9784 * copy it to a buffer big enough. */
9785 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009787 {
9788 retval = FAIL;
9789 goto theend;
9790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 }
9792 else
9793 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009794 /* Transfer short_fname to the main buffer (it's big enough),
9795 * unless get_short_pathname() did its work in-place. */
9796 *fname = *bufp = save_fname;
9797 if (short_fname != save_fname)
9798 vim_strncpy(save_fname, short_fname, len);
9799 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009801
9802 /* concat the not-shortened part of the path */
9803 vim_strncpy(*fname + len, endp, sfx_len);
9804 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009806
9807theend:
9808 vim_free(pbuf_unused);
9809 vim_free(save_fname);
9810
9811 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
9815 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009816 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 */
9818 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009819shortpath_for_partial(
9820 char_u **fnamep,
9821 char_u **bufp,
9822 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 int sepcount, len, tflen;
9825 char_u *p;
9826 char_u *pbuf, *tfname;
9827 int hasTilde;
9828
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009829 /* Count up the path separators from the RHS.. so we know which part
9830 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009832 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (vim_ispathsep(*p))
9834 ++sepcount;
9835
9836 /* Need full path first (use expand_env() to remove a "~/") */
9837 hasTilde = (**fnamep == '~');
9838 if (hasTilde)
9839 pbuf = tfname = expand_env_save(*fnamep);
9840 else
9841 pbuf = tfname = FullName_save(*fnamep, FALSE);
9842
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009843 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009845 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9846 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
9848 if (len == 0)
9849 {
9850 /* Don't have a valid filename, so shorten the rest of the
9851 * path if we can. This CAN give us invalid 8.3 filenames, but
9852 * there's not a lot of point in guessing what it might be.
9853 */
9854 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009855 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9856 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 }
9858
9859 /* Count the paths backward to find the beginning of the desired string. */
9860 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009861 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009862 if (has_mbyte)
9863 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 if (vim_ispathsep(*p))
9865 {
9866 if (sepcount == 0 || (hasTilde && sepcount == 1))
9867 break;
9868 else
9869 sepcount --;
9870 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 if (hasTilde)
9873 {
9874 --p;
9875 if (p >= tfname)
9876 *p = '~';
9877 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009878 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 }
9880 else
9881 ++p;
9882
9883 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9884 vim_free(*bufp);
9885 *fnamelen = (int)STRLEN(p);
9886 *bufp = pbuf;
9887 *fnamep = p;
9888
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009889 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
Bram Moolenaar4f974752019-02-17 17:44:42 +01009891#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
9893/*
9894 * Adjust a filename, according to a string of modifiers.
9895 * *fnamep must be NUL terminated when called. When returning, the length is
9896 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009897 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 * When there is an error, *fnamep is set to NULL.
9899 */
9900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009901modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009902 char_u *src, // string with modifiers
9903 int tilde_file, // "~" is a file name, not $HOME
9904 int *usedlen, // characters after src that are used
9905 char_u **fnamep, // file name so far
9906 char_u **bufp, // buffer for allocated file name or NULL
9907 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
9909 int valid = 0;
9910 char_u *tail;
9911 char_u *s, *p, *pbuf;
9912 char_u dirname[MAXPATHL];
9913 int c;
9914 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009915#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +02009916 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 int has_shortname = 0;
9918#endif
9919
9920repeat:
9921 /* ":p" - full path/file_name */
9922 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9923 {
9924 has_fullname = 1;
9925
9926 valid |= VALID_PATH;
9927 *usedlen += 2;
9928
9929 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9930 if ((*fnamep)[0] == '~'
9931#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9932 && ((*fnamep)[1] == '/'
9933# ifdef BACKSLASH_IN_FILENAME
9934 || (*fnamep)[1] == '\\'
9935# endif
9936 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009938 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 )
9940 {
9941 *fnamep = expand_env_save(*fnamep);
9942 vim_free(*bufp); /* free any allocated file name */
9943 *bufp = *fnamep;
9944 if (*fnamep == NULL)
9945 return -1;
9946 }
9947
9948 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009949 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
9951 if (vim_ispathsep(*p)
9952 && p[1] == '.'
9953 && (p[2] == NUL
9954 || vim_ispathsep(p[2])
9955 || (p[2] == '.'
9956 && (p[3] == NUL || vim_ispathsep(p[3])))))
9957 break;
9958 }
9959
9960 /* FullName_save() is slow, don't use it when not needed. */
9961 if (*p != NUL || !vim_isAbsName(*fnamep))
9962 {
9963 *fnamep = FullName_save(*fnamep, *p != NUL);
9964 vim_free(*bufp); /* free any allocated file name */
9965 *bufp = *fnamep;
9966 if (*fnamep == NULL)
9967 return -1;
9968 }
9969
Bram Moolenaar4f974752019-02-17 17:44:42 +01009970#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009971# if _WIN32_WINNT >= 0x0500
9972 if (vim_strchr(*fnamep, '~') != NULL)
9973 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +01009974 // Expand 8.3 filename to full path. Needed to make sure the same
9975 // file does not have two different names.
9976 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
9977 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
9978 WCHAR buf[_MAX_PATH];
9979
9980 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009981 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +01009982 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009983 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +01009984 char_u *p = utf16_to_enc(buf, NULL);
9985
9986 if (p != NULL)
9987 {
9988 vim_free(*bufp); // free any allocated file name
9989 *bufp = *fnamep = p;
9990 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009991 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +01009992 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009993 }
9994 }
9995# endif
9996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 /* Append a path separator to a directory. */
9998 if (mch_isdir(*fnamep))
9999 {
10000 /* Make room for one or two extra characters. */
10001 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10002 vim_free(*bufp); /* free any allocated file name */
10003 *bufp = *fnamep;
10004 if (*fnamep == NULL)
10005 return -1;
10006 add_pathsep(*fnamep);
10007 }
10008 }
10009
10010 /* ":." - path relative to the current directory */
10011 /* ":~" - path relative to the home directory */
10012 /* ":8" - shortname path - postponed till after */
10013 while (src[*usedlen] == ':'
10014 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10015 {
10016 *usedlen += 2;
10017 if (c == '8')
10018 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010019#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 has_shortname = 1; /* Postpone this. */
10021#endif
10022 continue;
10023 }
10024 pbuf = NULL;
10025 /* Need full path first (use expand_env() to remove a "~/") */
10026 if (!has_fullname)
10027 {
10028 if (c == '.' && **fnamep == '~')
10029 p = pbuf = expand_env_save(*fnamep);
10030 else
10031 p = pbuf = FullName_save(*fnamep, FALSE);
10032 }
10033 else
10034 p = *fnamep;
10035
10036 has_fullname = 0;
10037
10038 if (p != NULL)
10039 {
10040 if (c == '.')
10041 {
10042 mch_dirname(dirname, MAXPATHL);
10043 s = shorten_fname(p, dirname);
10044 if (s != NULL)
10045 {
10046 *fnamep = s;
10047 if (pbuf != NULL)
10048 {
10049 vim_free(*bufp); /* free any allocated file name */
10050 *bufp = pbuf;
10051 pbuf = NULL;
10052 }
10053 }
10054 }
10055 else
10056 {
10057 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10058 /* Only replace it when it starts with '~' */
10059 if (*dirname == '~')
10060 {
10061 s = vim_strsave(dirname);
10062 if (s != NULL)
10063 {
10064 *fnamep = s;
10065 vim_free(*bufp);
10066 *bufp = s;
10067 }
10068 }
10069 }
10070 vim_free(pbuf);
10071 }
10072 }
10073
10074 tail = gettail(*fnamep);
10075 *fnamelen = (int)STRLEN(*fnamep);
10076
10077 /* ":h" - head, remove "/file_name", can be repeated */
10078 /* Don't remove the first "/" or "c:\" */
10079 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10080 {
10081 valid |= VALID_HEAD;
10082 *usedlen += 2;
10083 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010084 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010085 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 *fnamelen = (int)(tail - *fnamep);
10087#ifdef VMS
10088 if (*fnamelen > 0)
10089 *fnamelen += 1; /* the path separator is part of the path */
10090#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010091 if (*fnamelen == 0)
10092 {
10093 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10094 p = vim_strsave((char_u *)".");
10095 if (p == NULL)
10096 return -1;
10097 vim_free(*bufp);
10098 *bufp = *fnamep = tail = p;
10099 *fnamelen = 1;
10100 }
10101 else
10102 {
10103 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010104 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107
10108 /* ":8" - shortname */
10109 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10110 {
10111 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010112#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 has_shortname = 1;
10114#endif
10115 }
10116
Bram Moolenaar4f974752019-02-17 17:44:42 +010010117#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010118 /*
10119 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 */
10121 if (has_shortname)
10122 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010123 /* Copy the string if it is shortened by :h and when it wasn't copied
10124 * yet, because we are going to change it in place. Avoids changing
10125 * the buffer name for "%:8". */
10126 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 {
10128 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010129 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 return -1;
10131 vim_free(*bufp);
10132 *bufp = *fnamep = p;
10133 }
10134
10135 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010136 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (!has_fullname && !vim_isAbsName(*fnamep))
10138 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010139 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 return -1;
10141 }
10142 else
10143 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010144 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
Bram Moolenaardc935552011-08-17 15:23:23 +020010146 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010148 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 return -1;
10150
10151 if (l == 0)
10152 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010153 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010155 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 return -1;
10157 }
10158 *fnamelen = l;
10159 }
10160 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010161#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
10163 /* ":t" - tail, just the basename */
10164 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10165 {
10166 *usedlen += 2;
10167 *fnamelen -= (int)(tail - *fnamep);
10168 *fnamep = tail;
10169 }
10170
10171 /* ":e" - extension, can be repeated */
10172 /* ":r" - root, without extension, can be repeated */
10173 while (src[*usedlen] == ':'
10174 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10175 {
10176 /* find a '.' in the tail:
10177 * - for second :e: before the current fname
10178 * - otherwise: The last '.'
10179 */
10180 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10181 s = *fnamep - 2;
10182 else
10183 s = *fnamep + *fnamelen - 1;
10184 for ( ; s > tail; --s)
10185 if (s[0] == '.')
10186 break;
10187 if (src[*usedlen + 1] == 'e') /* :e */
10188 {
10189 if (s > tail)
10190 {
10191 *fnamelen += (int)(*fnamep - (s + 1));
10192 *fnamep = s + 1;
10193#ifdef VMS
10194 /* cut version from the extension */
10195 s = *fnamep + *fnamelen - 1;
10196 for ( ; s > *fnamep; --s)
10197 if (s[0] == ';')
10198 break;
10199 if (s > *fnamep)
10200 *fnamelen = s - *fnamep;
10201#endif
10202 }
10203 else if (*fnamep <= tail)
10204 *fnamelen = 0;
10205 }
10206 else /* :r */
10207 {
10208 if (s > tail) /* remove one extension */
10209 *fnamelen = (int)(s - *fnamep);
10210 }
10211 *usedlen += 2;
10212 }
10213
10214 /* ":s?pat?foo?" - substitute */
10215 /* ":gs?pat?foo?" - global substitute */
10216 if (src[*usedlen] == ':'
10217 && (src[*usedlen + 1] == 's'
10218 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10219 {
10220 char_u *str;
10221 char_u *pat;
10222 char_u *sub;
10223 int sep;
10224 char_u *flags;
10225 int didit = FALSE;
10226
10227 flags = (char_u *)"";
10228 s = src + *usedlen + 2;
10229 if (src[*usedlen + 1] == 'g')
10230 {
10231 flags = (char_u *)"g";
10232 ++s;
10233 }
10234
10235 sep = *s++;
10236 if (sep)
10237 {
10238 /* find end of pattern */
10239 p = vim_strchr(s, sep);
10240 if (p != NULL)
10241 {
10242 pat = vim_strnsave(s, (int)(p - s));
10243 if (pat != NULL)
10244 {
10245 s = p + 1;
10246 /* find end of substitution */
10247 p = vim_strchr(s, sep);
10248 if (p != NULL)
10249 {
10250 sub = vim_strnsave(s, (int)(p - s));
10251 str = vim_strnsave(*fnamep, *fnamelen);
10252 if (sub != NULL && str != NULL)
10253 {
10254 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010255 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 if (s != NULL)
10257 {
10258 *fnamep = s;
10259 *fnamelen = (int)STRLEN(s);
10260 vim_free(*bufp);
10261 *bufp = s;
10262 didit = TRUE;
10263 }
10264 }
10265 vim_free(sub);
10266 vim_free(str);
10267 }
10268 vim_free(pat);
10269 }
10270 }
10271 /* after using ":s", repeat all the modifiers */
10272 if (didit)
10273 goto repeat;
10274 }
10275 }
10276
Bram Moolenaar26df0922014-02-23 23:39:13 +010010277 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10278 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010279 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010280 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010281 if (c != NUL)
10282 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010283 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010284 if (c != NUL)
10285 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010286 if (p == NULL)
10287 return -1;
10288 vim_free(*bufp);
10289 *bufp = *fnamep = p;
10290 *fnamelen = (int)STRLEN(p);
10291 *usedlen += 2;
10292 }
10293
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 return valid;
10295}
10296
10297/*
10298 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010299 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 * "flags" can be "g" to do a global substitute.
10301 * Returns an allocated string, NULL for error.
10302 */
10303 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010304do_string_sub(
10305 char_u *str,
10306 char_u *pat,
10307 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010308 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010309 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310{
10311 int sublen;
10312 regmatch_T regmatch;
10313 int i;
10314 int do_all;
10315 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010316 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 garray_T ga;
10318 char_u *ret;
10319 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010320 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321
10322 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10323 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010324 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325
10326 ga_init2(&ga, 1, 200);
10327
10328 do_all = (flags[0] == 'g');
10329
10330 regmatch.rm_ic = p_ic;
10331 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10332 if (regmatch.regprog != NULL)
10333 {
10334 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010335 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10337 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010338 /* Skip empty match except for first match. */
10339 if (regmatch.startp[0] == regmatch.endp[0])
10340 {
10341 if (zero_width == regmatch.startp[0])
10342 {
10343 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010344 i = MB_PTR2LEN(tail);
10345 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10346 (size_t)i);
10347 ga.ga_len += i;
10348 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010349 continue;
10350 }
10351 zero_width = regmatch.startp[0];
10352 }
10353
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 /*
10355 * Get some space for a temporary buffer to do the substitution
10356 * into. It will contain:
10357 * - The text up to where the match is.
10358 * - The substituted text.
10359 * - The text after the match.
10360 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010361 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010362 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10364 {
10365 ga_clear(&ga);
10366 break;
10367 }
10368
10369 /* copy the text up to where the match is */
10370 i = (int)(regmatch.startp[0] - tail);
10371 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10372 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010373 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 + ga.ga_len + i, TRUE, TRUE, FALSE);
10375 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010376 tail = regmatch.endp[0];
10377 if (*tail == NUL)
10378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 if (!do_all)
10380 break;
10381 }
10382
10383 if (ga.ga_data != NULL)
10384 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10385
Bram Moolenaar473de612013-06-08 18:19:48 +020010386 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 }
10388
10389 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10390 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010391 if (p_cpo == empty_option)
10392 p_cpo = save_cpo;
10393 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010394 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010395 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396
10397 return ret;
10398}
10399
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010400 static int
10401filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10402{
10403 typval_T rettv;
10404 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010406
10407 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10408 argv[0] = vimvars[VV_KEY].vv_tv;
10409 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010410 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10411 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010412 if (map)
10413 {
10414 /* map(): replace the list item value */
10415 clear_tv(tv);
10416 rettv.v_lock = 0;
10417 *tv = rettv;
10418 }
10419 else
10420 {
10421 int error = FALSE;
10422
10423 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010424 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010425 clear_tv(&rettv);
10426 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010427 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428 if (error)
10429 goto theend;
10430 }
10431 retval = OK;
10432theend:
10433 clear_tv(&vimvars[VV_VAL].vv_tv);
10434 return retval;
10435}
10436
10437
10438/*
10439 * Implementation of map() and filter().
10440 */
10441 void
10442filter_map(typval_T *argvars, typval_T *rettv, int map)
10443{
10444 typval_T *expr;
10445 listitem_T *li, *nli;
10446 list_T *l = NULL;
10447 dictitem_T *di;
10448 hashtab_T *ht;
10449 hashitem_T *hi;
10450 dict_T *d = NULL;
10451 typval_T save_val;
10452 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010453 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010454 int rem;
10455 int todo;
10456 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10457 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10458 : N_("filter() argument"));
10459 int save_did_emsg;
10460 int idx = 0;
10461
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010462 if (argvars[0].v_type == VAR_BLOB)
10463 {
10464 if ((b = argvars[0].vval.v_blob) == NULL)
10465 return;
10466 }
10467 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010468 {
10469 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010470 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010471 return;
10472 }
10473 else if (argvars[0].v_type == VAR_DICT)
10474 {
10475 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010476 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 return;
10478 }
10479 else
10480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010481 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010482 return;
10483 }
10484
10485 expr = &argvars[1];
10486 /* On type errors, the preceding call has already displayed an error
10487 * message. Avoid a misleading error message for an empty string that
10488 * was not passed as argument. */
10489 if (expr->v_type != VAR_UNKNOWN)
10490 {
10491 prepare_vimvar(VV_VAL, &save_val);
10492
10493 /* We reset "did_emsg" to be able to detect whether an error
10494 * occurred during evaluation of the expression. */
10495 save_did_emsg = did_emsg;
10496 did_emsg = FALSE;
10497
10498 prepare_vimvar(VV_KEY, &save_key);
10499 if (argvars[0].v_type == VAR_DICT)
10500 {
10501 vimvars[VV_KEY].vv_type = VAR_STRING;
10502
10503 ht = &d->dv_hashtab;
10504 hash_lock(ht);
10505 todo = (int)ht->ht_used;
10506 for (hi = ht->ht_array; todo > 0; ++hi)
10507 {
10508 if (!HASHITEM_EMPTY(hi))
10509 {
10510 int r;
10511
10512 --todo;
10513 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010514 if (map && (var_check_lock(di->di_tv.v_lock,
10515 arg_errmsg, TRUE)
10516 || var_check_ro(di->di_flags,
10517 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010518 break;
10519 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10520 r = filter_map_one(&di->di_tv, expr, map, &rem);
10521 clear_tv(&vimvars[VV_KEY].vv_tv);
10522 if (r == FAIL || did_emsg)
10523 break;
10524 if (!map && rem)
10525 {
10526 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10527 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10528 break;
10529 dictitem_remove(d, di);
10530 }
10531 }
10532 }
10533 hash_unlock(ht);
10534 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010535 else if (argvars[0].v_type == VAR_BLOB)
10536 {
10537 int i;
10538 typval_T tv;
10539
10540 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10541 for (i = 0; i < b->bv_ga.ga_len; i++)
10542 {
10543 tv.v_type = VAR_NUMBER;
10544 tv.vval.v_number = blob_get(b, i);
10545 vimvars[VV_KEY].vv_nr = idx;
10546 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10547 break;
10548 if (tv.v_type != VAR_NUMBER)
10549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010550 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010551 return;
10552 }
10553 tv.v_type = VAR_NUMBER;
10554 blob_set(b, i, tv.vval.v_number);
10555 if (!map && rem)
10556 {
10557 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10558
10559 mch_memmove(p + idx, p + i + 1,
10560 (size_t)b->bv_ga.ga_len - i - 1);
10561 --b->bv_ga.ga_len;
10562 --i;
10563 }
10564 }
10565 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010566 else
10567 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010568 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10570
10571 for (li = l->lv_first; li != NULL; li = nli)
10572 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010573 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010574 break;
10575 nli = li->li_next;
10576 vimvars[VV_KEY].vv_nr = idx;
10577 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10578 || did_emsg)
10579 break;
10580 if (!map && rem)
10581 listitem_remove(l, li);
10582 ++idx;
10583 }
10584 }
10585
10586 restore_vimvar(VV_KEY, &save_key);
10587 restore_vimvar(VV_VAL, &save_val);
10588
10589 did_emsg |= save_did_emsg;
10590 }
10591
10592 copy_tv(&argvars[0], rettv);
10593}
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */