blob: 2c93e4ec4b67f70681a2ec0b77ef84e126f51034 [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 Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
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 */
81} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000082
Bram Moolenaara7043832005-01-21 11:56:39 +000083
84/*
Bram Moolenaar33570922005-01-25 22:26:29 +000085 * Array to hold the value of v: variables.
86 * The value is in a dictitem, so that it can also be used in the v: scope.
87 * The reason to use this table anyway is for very quick access to the
88 * variables with the VV_ defines.
89 */
Bram Moolenaar33570922005-01-25 22:26:29 +000090
91/* values for vv_flags: */
92#define VV_COMPAT 1 /* compatible, also used without "v:" */
93#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000094#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000095
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010096#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000097
98static struct vimvar
99{
100 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100101 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000102 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
103} vimvars[VV_LEN] =
104{
105 /*
106 * The order here must match the VV_ defines in vim.h!
107 * Initializing a union does not work, leave tv.vval empty to get zero's.
108 */
109 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
110 {VV_NAME("count1", VAR_NUMBER), VV_RO},
111 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
112 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
113 {VV_NAME("warningmsg", VAR_STRING), 0},
114 {VV_NAME("statusmsg", VAR_STRING), 0},
115 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
116 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
117 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
119 {VV_NAME("termresponse", VAR_STRING), VV_RO},
120 {VV_NAME("fname", VAR_STRING), VV_RO},
121 {VV_NAME("lang", VAR_STRING), VV_RO},
122 {VV_NAME("lc_time", VAR_STRING), VV_RO},
123 {VV_NAME("ctype", VAR_STRING), VV_RO},
124 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
125 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
126 {VV_NAME("fname_in", VAR_STRING), VV_RO},
127 {VV_NAME("fname_out", VAR_STRING), VV_RO},
128 {VV_NAME("fname_new", VAR_STRING), VV_RO},
129 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
130 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
131 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
132 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
133 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
134 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("progname", VAR_STRING), VV_RO},
136 {VV_NAME("servername", VAR_STRING), VV_RO},
137 {VV_NAME("dying", VAR_NUMBER), VV_RO},
138 {VV_NAME("exception", VAR_STRING), VV_RO},
139 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
140 {VV_NAME("register", VAR_STRING), VV_RO},
141 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
142 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000143 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
144 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000145 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000146 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
147 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000148 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
149 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200150 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
153 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000154 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000155 {VV_NAME("swapname", VAR_STRING), VV_RO},
156 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000157 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200158 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000159 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200160 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
162 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000163 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000164 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100165 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000166 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200168 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200169 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200170 {VV_NAME("option_new", VAR_STRING), VV_RO},
171 {VV_NAME("option_old", VAR_STRING), VV_RO},
172 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100173 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {VV_NAME("false", VAR_SPECIAL), VV_RO},
175 {VV_NAME("true", VAR_SPECIAL), VV_RO},
176 {VV_NAME("null", VAR_SPECIAL), VV_RO},
177 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100178 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200179 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200180 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
181 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
182 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200190 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
191 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200192 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
193 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
194 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100195 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000196};
197
198/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_type vv_di.di_tv.v_type
200#define vv_nr vv_di.di_tv.vval.v_number
201#define vv_float vv_di.di_tv.vval.v_float
202#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000203#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200204#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000205#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000206
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200207static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000208#define vimvarht vimvardict.dv_hashtab
209
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100210static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
211static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
212static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_glob_vars(int *first);
214static void list_buf_vars(int *first);
215static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100216static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_vim_vars(int *first);
218static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
220static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
222static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
224static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
225static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
226static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000227
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static int eval2(char_u **arg, typval_T *rettv, int evaluate);
229static int eval3(char_u **arg, typval_T *rettv, int evaluate);
230static int eval4(char_u **arg, typval_T *rettv, int evaluate);
231static int eval5(char_u **arg, typval_T *rettv, int evaluate);
232static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
233static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000234
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
237static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static 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 +0200242static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static void delete_var(hashtab_T *ht, hashitem_T *hi);
245static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
246static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249/* for VIM_VERSION_ defines */
250#include "version.h"
251
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100252
253#if defined(EBCDIC) || defined(PROTO)
254/*
255 * Compare struct fst by function name.
256 */
257 static int
258compare_func_name(const void *s1, const void *s2)
259{
260 struct fst *p1 = (struct fst *)s1;
261 struct fst *p2 = (struct fst *)s2;
262
263 return STRCMP(p1->f_name, p2->f_name);
264}
265
266/*
267 * Sort the function table by function name.
268 * The sorting of the table above is ASCII dependant.
269 * On machines using EBCDIC we have to sort it.
270 */
271 static void
272sortFunctions(void)
273{
274 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
275
276 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
277}
278#endif
279
280
Bram Moolenaar33570922005-01-25 22:26:29 +0000281/*
282 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000283 */
284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100285eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000286{
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 int i;
288 struct vimvar *p;
289
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200290 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
291 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200292 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000293 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200294 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
296 for (i = 0; i < VV_LEN; ++i)
297 {
298 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200299 if (STRLEN(p->vv_name) > 16)
300 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100301 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200302 getout(1);
303 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 STRCPY(p->vv_di.di_key, p->vv_name);
305 if (p->vv_flags & VV_RO)
306 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
307 else if (p->vv_flags & VV_RO_SBX)
308 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
309 else
310 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000311
312 /* add to v: scope dict, unless the value is not always available */
313 if (p->vv_type != VAR_UNKNOWN)
314 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000315 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000316 /* add to compat scope dict */
317 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000318 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100319 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
320
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000321 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100322 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100323 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100324 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100325 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100326
327 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
328 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
329 set_vim_var_nr(VV_NONE, VVAL_NONE);
330 set_vim_var_nr(VV_NULL, VVAL_NULL);
331
Bram Moolenaarf562e722016-07-19 17:25:25 +0200332 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
333 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
334 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
335 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
336 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
337 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
338 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
339 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
340 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
341 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
342
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200343 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200344
345#ifdef EBCDIC
346 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100347 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200348 */
349 sortFunctions();
350#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000351}
352
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000353#if defined(EXITFREE) || defined(PROTO)
354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100355eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000356{
357 int i;
358 struct vimvar *p;
359
360 for (i = 0; i < VV_LEN; ++i)
361 {
362 p = &vimvars[i];
363 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100364 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 else if (p->vv_di.di_tv.v_type == VAR_LIST)
366 {
367 list_unref(p->vv_list);
368 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000369 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000370 }
371 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000372 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000373 hash_clear(&compat_hashtab);
374
Bram Moolenaard9fba312005-06-26 22:34:35 +0000375 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100376# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200377 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100378# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000379
380 /* global variables */
381 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000382
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000383 /* autoloaded script names */
384 ga_clear_strings(&ga_loaded);
385
Bram Moolenaarcca74132013-09-25 21:00:28 +0200386 /* Script-local variables. First clear all the variables and in a second
387 * loop free the scriptvar_T, because a variable in one script might hold
388 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200389 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200391 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200392 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 ga_clear(&ga_scripts);
394
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000395 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200396 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000397
398 /* functions */
399 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400}
401#endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 * Set an internal variable to a string value. Creates the variable if it does
406 * not already exist.
407 */
408 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100409set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000411 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000412 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 val = vim_strsave(value);
415 if (val != NULL)
416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000417 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000418 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000420 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
423 }
424}
425
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000426static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200427#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000428static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000429static char_u *redir_endp = NULL;
430static char_u *redir_varname = NULL;
431
432/*
433 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100434 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000435 * Returns OK if successfully completed the setup. FAIL otherwise.
436 */
437 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100438var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000439{
440 int save_emsg;
441 int err;
442 typval_T tv;
443
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000444 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000445 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000446 {
447 EMSG(_(e_invarg));
448 return FAIL;
449 }
450
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000451 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 redir_varname = vim_strsave(name);
453 if (redir_varname == NULL)
454 return FAIL;
455
456 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
457 if (redir_lval == NULL)
458 {
459 var_redir_stop();
460 return FAIL;
461 }
462
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000463 /* The output is stored in growarray "redir_ga" until redirection ends. */
464 ga_init2(&redir_ga, (int)sizeof(char), 500);
465
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000466 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100467 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000468 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
470 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200471 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 if (redir_endp != NULL && *redir_endp != NUL)
473 /* Trailing characters are present after the variable name */
474 EMSG(_(e_trailing));
475 else
476 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000477 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000478 var_redir_stop();
479 return FAIL;
480 }
481
482 /* check if we can write to the variable: set it to or append an empty
483 * string */
484 save_emsg = did_emsg;
485 did_emsg = FALSE;
486 tv.v_type = VAR_STRING;
487 tv.vval.v_string = (char_u *)"";
488 if (append)
489 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
490 else
491 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200492 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000494 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 if (err)
496 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 var_redir_stop();
499 return FAIL;
500 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000501
502 return OK;
503}
504
505/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000506 * Append "value[value_len]" to the variable set by var_redir_start().
507 * The actual appending is postponed until redirection ends, because the value
508 * appended may in fact be the string we write to, changing it may cause freed
509 * memory to be used:
510 * :redir => foo
511 * :let foo
512 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 */
514 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100515var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000517 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518
519 if (redir_lval == NULL)
520 return;
521
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000522 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000523 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000525 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000526
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000527 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000528 {
529 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000530 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000531 }
532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534}
535
536/*
537 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000538 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 */
540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100541var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000543 typval_T tv;
544
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200545 if (EVALCMD_BUSY)
546 {
547 redir_lval = NULL;
548 return;
549 }
550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 if (redir_lval != NULL)
552 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000553 /* If there was no error: assign the text to the variable. */
554 if (redir_endp != NULL)
555 {
556 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
557 tv.v_type = VAR_STRING;
558 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200559 /* Call get_lval() again, if it's inside a Dict or List it may
560 * have changed. */
561 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100562 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200563 if (redir_endp != NULL && redir_lval->ll_name != NULL)
564 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
565 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000566 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000568 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100569 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000570
Bram Moolenaard23a8232018-02-10 18:45:26 +0100571 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100573 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576# if defined(FEAT_MBYTE) || defined(PROTO)
577 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100578eval_charconvert(
579 char_u *enc_from,
580 char_u *enc_to,
581 char_u *fname_from,
582 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 int err = FALSE;
585
586 set_vim_var_string(VV_CC_FROM, enc_from, -1);
587 set_vim_var_string(VV_CC_TO, enc_to, -1);
588 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
589 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
590 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
591 err = TRUE;
592 set_vim_var_string(VV_CC_FROM, NULL, -1);
593 set_vim_var_string(VV_CC_TO, NULL, -1);
594 set_vim_var_string(VV_FNAME_IN, NULL, -1);
595 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
596
597 if (err)
598 return FAIL;
599 return OK;
600}
601# endif
602
603# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100605eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 int err = FALSE;
608
609 set_vim_var_string(VV_FNAME_IN, fname, -1);
610 set_vim_var_string(VV_CMDARG, args, -1);
611 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
612 err = TRUE;
613 set_vim_var_string(VV_FNAME_IN, NULL, -1);
614 set_vim_var_string(VV_CMDARG, NULL, -1);
615
616 if (err)
617 {
618 mch_remove(fname);
619 return FAIL;
620 }
621 return OK;
622}
623# endif
624
625# if defined(FEAT_DIFF) || defined(PROTO)
626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100627eval_diff(
628 char_u *origfile,
629 char_u *newfile,
630 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 int err = FALSE;
633
634 set_vim_var_string(VV_FNAME_IN, origfile, -1);
635 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
636 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
637 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
638 set_vim_var_string(VV_FNAME_IN, NULL, -1);
639 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641}
642
643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100644eval_patch(
645 char_u *origfile,
646 char_u *difffile,
647 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 int err;
650
651 set_vim_var_string(VV_FNAME_IN, origfile, -1);
652 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
653 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
654 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
655 set_vim_var_string(VV_FNAME_IN, NULL, -1);
656 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
657 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
658}
659# endif
660
661/*
662 * Top level evaluation function, returning a boolean.
663 * Sets "error" to TRUE if there was an error.
664 * Return TRUE or FALSE.
665 */
666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100667eval_to_bool(
668 char_u *arg,
669 int *error,
670 char_u **nextcmd,
671 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
Bram Moolenaar33570922005-01-25 22:26:29 +0000673 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200674 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 if (skip)
677 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000678 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
681 {
682 *error = FALSE;
683 if (!skip)
684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000685 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000686 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 }
689 if (skip)
690 --emsg_skip;
691
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200692 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
Bram Moolenaar48570482017-10-30 21:48:41 +0100695 static int
696eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
697{
698 char_u *s;
699 int dummy;
700 char_u buf[NUMBUFLEN];
701
702 if (expr->v_type == VAR_FUNC)
703 {
704 s = expr->vval.v_string;
705 if (s == NULL || *s == NUL)
706 return FAIL;
707 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
708 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
709 return FAIL;
710 }
711 else if (expr->v_type == VAR_PARTIAL)
712 {
713 partial_T *partial = expr->vval.v_partial;
714
715 s = partial_name(partial);
716 if (s == NULL || *s == NUL)
717 return FAIL;
718 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
719 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
720 return FAIL;
721 }
722 else
723 {
724 s = get_tv_string_buf_chk(expr, buf);
725 if (s == NULL)
726 return FAIL;
727 s = skipwhite(s);
728 if (eval1(&s, rettv, TRUE) == FAIL)
729 return FAIL;
730 if (*s != NUL) /* check for trailing chars after expr */
731 {
732 EMSG2(_(e_invexpr2), s);
733 return FAIL;
734 }
735 }
736 return OK;
737}
738
739/*
740 * Like eval_to_bool() but using a typval_T instead of a string.
741 * Works for string, funcref and partial.
742 */
743 int
744eval_expr_to_bool(typval_T *expr, int *error)
745{
746 typval_T rettv;
747 int res;
748
749 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
750 {
751 *error = TRUE;
752 return FALSE;
753 }
754 res = (get_tv_number_chk(&rettv, error) != 0);
755 clear_tv(&rettv);
756 return res;
757}
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759/*
760 * Top level evaluation function, returning a string. If "skip" is TRUE,
761 * only parsing to "nextcmd" is done, without reporting errors. Return
762 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
763 */
764 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100765eval_to_string_skip(
766 char_u *arg,
767 char_u **nextcmd,
768 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
Bram Moolenaar33570922005-01-25 22:26:29 +0000770 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 char_u *retval;
772
773 if (skip)
774 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000775 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 retval = NULL;
777 else
778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 retval = vim_strsave(get_tv_string(&tv));
780 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 if (skip)
783 --emsg_skip;
784
785 return retval;
786}
787
788/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000789 * Skip over an expression at "*pp".
790 * Return FAIL for an error, OK otherwise.
791 */
792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000796
797 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000798 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000799}
800
801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000803 * When "convert" is TRUE convert a List into a sequence of lines and convert
804 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * Return pointer to allocated memory, or NULL for failure.
806 */
807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100808eval_to_string(
809 char_u *arg,
810 char_u **nextcmd,
811 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
Bram Moolenaar33570922005-01-25 22:26:29 +0000813 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000815 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000816#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000817 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000820 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 retval = NULL;
822 else
823 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000824 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000825 {
826 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000827 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200828 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200829 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200830 if (tv.vval.v_list->lv_len > 0)
831 ga_append(&ga, NL);
832 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833 ga_append(&ga, NUL);
834 retval = (char_u *)ga.ga_data;
835 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000836#ifdef FEAT_FLOAT
837 else if (convert && tv.v_type == VAR_FLOAT)
838 {
839 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
840 retval = vim_strsave(numbuf);
841 }
842#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000843 else
844 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847
848 return retval;
849}
850
851/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000852 * Call eval_to_string() without using current local variables and using
853 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
855 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100856eval_to_string_safe(
857 char_u *arg,
858 char_u **nextcmd,
859 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 char_u *retval;
862 void *save_funccalp;
863
864 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
867 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000868 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000869 if (use_sandbox)
870 --sandbox;
871 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 restore_funccal(save_funccalp);
873 return retval;
874}
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876/*
877 * Top level evaluation function, returning a number.
878 * Evaluates "expr" silently.
879 * Returns -1 for an error.
880 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200881 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100882eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200885 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000886 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 ++emsg_off;
889
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 retval = -1;
892 else
893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000894 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 --emsg_off;
898
899 return retval;
900}
901
Bram Moolenaara40058a2005-07-11 22:42:07 +0000902/*
903 * Prepare v: variable "idx" to be used.
904 * Save the current typeval in "save_tv".
905 * When not used yet add the variable to the v: hashtable.
906 */
907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000909{
910 *save_tv = vimvars[idx].vv_tv;
911 if (vimvars[idx].vv_type == VAR_UNKNOWN)
912 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
913}
914
915/*
916 * Restore v: variable "idx" to typeval "save_tv".
917 * When no longer defined, remove the variable from the v: hashtable.
918 */
919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000921{
922 hashitem_T *hi;
923
Bram Moolenaara40058a2005-07-11 22:42:07 +0000924 vimvars[idx].vv_tv = *save_tv;
925 if (vimvars[idx].vv_type == VAR_UNKNOWN)
926 {
927 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
928 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100929 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000930 else
931 hash_remove(&vimvarht, hi);
932 }
933}
934
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000935#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000936/*
937 * Evaluate an expression to a list with suggestions.
938 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000939 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000940 */
941 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100942eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000943{
944 typval_T save_val;
945 typval_T rettv;
946 list_T *list = NULL;
947 char_u *p = skipwhite(expr);
948
949 /* Set "v:val" to the bad word. */
950 prepare_vimvar(VV_VAL, &save_val);
951 vimvars[VV_VAL].vv_type = VAR_STRING;
952 vimvars[VV_VAL].vv_str = badword;
953 if (p_verbose == 0)
954 ++emsg_off;
955
956 if (eval1(&p, &rettv, TRUE) == OK)
957 {
958 if (rettv.v_type != VAR_LIST)
959 clear_tv(&rettv);
960 else
961 list = rettv.vval.v_list;
962 }
963
964 if (p_verbose == 0)
965 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000966 restore_vimvar(VV_VAL, &save_val);
967
968 return list;
969}
970
971/*
972 * "list" is supposed to contain two items: a word and a number. Return the
973 * word in "pp" and the number as the return value.
974 * Return -1 if anything isn't right.
975 * Used to get the good word and score from the eval_spell_expr() result.
976 */
977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000979{
980 listitem_T *li;
981
982 li = list->lv_first;
983 if (li == NULL)
984 return -1;
985 *pp = get_tv_string(&li->li_tv);
986
987 li = li->li_next;
988 if (li == NULL)
989 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200990 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000991}
992#endif
993
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000994/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000995 * Top level evaluation function.
996 * Returns an allocated typval_T with the result.
997 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998 */
999 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001000eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001001{
1002 typval_T *tv;
1003
1004 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001005 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001006 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001007
1008 return tv;
1009}
1010
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001013 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001014 * Uses argv[argc] for the function arguments. Only Number and String
1015 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001016 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001019call_vim_function(
1020 char_u *func,
1021 int argc,
1022 char_u **argv,
1023 int safe, /* use the sandbox */
1024 int str_arg_only, /* all arguments are strings */
1025 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar33570922005-01-25 22:26:29 +00001027 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001028 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 int len;
1030 int i;
1031 int doesrange;
1032 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001033 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001035 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001037 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
1039 for (i = 0; i < argc; i++)
1040 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001041 /* Pass a NULL or empty argument as an empty string */
1042 if (argv[i] == NULL || *argv[i] == NUL)
1043 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001044 argvars[i].v_type = VAR_STRING;
1045 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001046 continue;
1047 }
1048
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001049 if (str_arg_only)
1050 len = 0;
1051 else
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001052 {
1053 /* Recognize a number argument, the others must be strings. A dash
1054 * is a string too. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001055 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001056 if (len == 1 && *argv[i] == '-')
1057 len = 0;
1058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (len != 0 && len == (int)STRLEN(argv[i]))
1060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001061 argvars[i].v_type = VAR_NUMBER;
1062 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064 else
1065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001066 argvars[i].v_type = VAR_STRING;
1067 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 }
1070
1071 if (safe)
1072 {
1073 save_funccalp = save_funccal();
1074 ++sandbox;
1075 }
1076
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001077 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001078 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001080 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (safe)
1082 {
1083 --sandbox;
1084 restore_funccal(save_funccalp);
1085 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001086 vim_free(argvars);
1087
1088 if (ret == FAIL)
1089 clear_tv(rettv);
1090
1091 return ret;
1092}
1093
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001094/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001095 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001096 * Returns -1 when calling the function fails.
1097 * Uses argv[argc] for the function arguments.
1098 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001099 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001100call_func_retnr(
1101 char_u *func,
1102 int argc,
1103 char_u **argv,
1104 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001105{
1106 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001107 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001108
1109 /* All arguments are passed as strings, no conversion to number. */
1110 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1111 return -1;
1112
1113 retval = get_tv_number_chk(&rettv, NULL);
1114 clear_tv(&rettv);
1115 return retval;
1116}
1117
1118#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1119 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1120
Bram Moolenaar4f688582007-07-24 12:34:30 +00001121# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001122/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001123 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001124 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001125 * Uses argv[argc] for the function arguments.
1126 */
1127 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001128call_func_retstr(
1129 char_u *func,
1130 int argc,
1131 char_u **argv,
1132 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001133{
1134 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001135 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001136
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001137 /* All arguments are passed as strings, no conversion to number. */
1138 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001139 return NULL;
1140
1141 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001142 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 return retval;
1144}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001145# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001147/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001148 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001149 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001150 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001151 */
1152 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001153call_func_retlist(
1154 char_u *func,
1155 int argc,
1156 char_u **argv,
1157 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158{
1159 typval_T rettv;
1160
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001161 /* All arguments are passed as strings, no conversion to number. */
1162 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001163 return NULL;
1164
1165 if (rettv.v_type != VAR_LIST)
1166 {
1167 clear_tv(&rettv);
1168 return NULL;
1169 }
1170
1171 return rettv.vval.v_list;
1172}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174
Bram Moolenaar05159a02005-02-26 23:04:13 +00001175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#ifdef FEAT_FOLDING
1177/*
1178 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1179 * it in "*cp". Doesn't give error messages.
1180 */
1181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001185 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001187 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1188 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
1190 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001191 if (use_sandbox)
1192 ++sandbox;
1193 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 retval = 0;
1197 else
1198 {
1199 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 if (tv.v_type == VAR_NUMBER)
1201 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001202 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 retval = 0;
1204 else
1205 {
1206 /* If the result is a string, check if there is a non-digit before
1207 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (!VIM_ISDIGIT(*s) && *s != '-')
1210 *cp = *s++;
1211 retval = atol((char *)s);
1212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001216 if (use_sandbox)
1217 --sandbox;
1218 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001220 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221}
1222#endif
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 * ":let" list all variable values
1226 * ":let var1 var2" list variable values
1227 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001228 * ":let var += expr" assignment command.
1229 * ":let var -= expr" assignment command.
1230 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
1233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001234ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001238 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 int var_count = 0;
1241 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001242 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001243 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001244 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
Bram Moolenaardb552d602006-03-23 22:59:57 +00001246 argend = skip_var_list(arg, &var_count, &semicolon);
1247 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001248 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001249 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1250 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001251 expr = skipwhite(argend);
1252 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1253 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001255 /*
1256 * ":let" without "=": list variables
1257 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001258 if (*arg == '[')
1259 EMSG(_(e_invarg));
1260 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001262 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001266 list_glob_vars(&first);
1267 list_buf_vars(&first);
1268 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001269 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001270 list_script_vars(&first);
1271 list_func_vars(&first);
1272 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 eap->nextcmd = check_nextcmd(arg);
1275 }
1276 else
1277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278 op[0] = '=';
1279 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001280 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001282 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1283 op[0] = *expr; /* +=, -= or .= */
1284 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001286 else
1287 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (eap->skip)
1290 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001291 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (eap->skip)
1293 {
1294 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 --emsg_skip;
1297 }
1298 else if (i != FAIL)
1299 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001300 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001301 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 }
1305}
1306
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307/*
1308 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1309 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 * When "nextchars" is not NULL it points to a string with characters that
1311 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1312 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001313 * Returns OK or FAIL;
1314 */
1315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001316ex_let_vars(
1317 char_u *arg_start,
1318 typval_T *tv,
1319 int copy, /* copy values from "tv", don't move */
1320 int semicolon, /* from skip_var_list() */
1321 int var_count, /* from skip_var_list() */
1322 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001323{
1324 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001326 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 listitem_T *item;
1328 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001329
1330 if (*arg != '[')
1331 {
1332 /*
1333 * ":let var = expr" or ":for var in list"
1334 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336 return FAIL;
1337 return OK;
1338 }
1339
1340 /*
1341 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1342 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001343 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001344 {
1345 EMSG(_(e_listreq));
1346 return FAIL;
1347 }
1348
1349 i = list_len(l);
1350 if (semicolon == 0 && var_count < i)
1351 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001352 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001353 return FAIL;
1354 }
1355 if (var_count - semicolon > i)
1356 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001357 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001358 return FAIL;
1359 }
1360
1361 item = l->lv_first;
1362 while (*arg != ']')
1363 {
1364 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001366 item = item->li_next;
1367 if (arg == NULL)
1368 return FAIL;
1369
1370 arg = skipwhite(arg);
1371 if (*arg == ';')
1372 {
1373 /* Put the rest of the list (may be empty) in the var after ';'.
1374 * Create a new list for this. */
1375 l = list_alloc();
1376 if (l == NULL)
1377 return FAIL;
1378 while (item != NULL)
1379 {
1380 list_append_tv(l, &item->li_tv);
1381 item = item->li_next;
1382 }
1383
1384 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001385 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001386 ltv.vval.v_list = l;
1387 l->lv_refcount = 1;
1388
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1390 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001391 clear_tv(&ltv);
1392 if (arg == NULL)
1393 return FAIL;
1394 break;
1395 }
1396 else if (*arg != ',' && *arg != ']')
1397 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001398 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001399 return FAIL;
1400 }
1401 }
1402
1403 return OK;
1404}
1405
1406/*
1407 * Skip over assignable variable "var" or list of variables "[var, var]".
1408 * Used for ":let varvar = expr" and ":for varvar in expr".
1409 * For "[var, var]" increment "*var_count" for each variable.
1410 * for "[var, var; var]" set "semicolon".
1411 * Return NULL for an error.
1412 */
1413 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414skip_var_list(
1415 char_u *arg,
1416 int *var_count,
1417 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001418{
1419 char_u *p, *s;
1420
1421 if (*arg == '[')
1422 {
1423 /* "[var, var]": find the matching ']'. */
1424 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001426 {
1427 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1428 s = skip_var_one(p);
1429 if (s == p)
1430 {
1431 EMSG2(_(e_invarg2), p);
1432 return NULL;
1433 }
1434 ++*var_count;
1435
1436 p = skipwhite(s);
1437 if (*p == ']')
1438 break;
1439 else if (*p == ';')
1440 {
1441 if (*semicolon == 1)
1442 {
1443 EMSG(_("Double ; in list of variables"));
1444 return NULL;
1445 }
1446 *semicolon = 1;
1447 }
1448 else if (*p != ',')
1449 {
1450 EMSG2(_(e_invarg2), p);
1451 return NULL;
1452 }
1453 }
1454 return p + 1;
1455 }
1456 else
1457 return skip_var_one(arg);
1458}
1459
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001460/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001461 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001462 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001463 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001464 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001465skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001466{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001467 if (*arg == '@' && arg[1] != NUL)
1468 return arg + 2;
1469 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1470 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471}
1472
Bram Moolenaara7043832005-01-21 11:56:39 +00001473/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001474 * List variables for hashtab "ht" with prefix "prefix".
1475 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001476 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001478list_hashtable_vars(
1479 hashtab_T *ht,
1480 char_u *prefix,
1481 int empty,
1482 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001483{
Bram Moolenaar33570922005-01-25 22:26:29 +00001484 hashitem_T *hi;
1485 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001486 int todo;
1487
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001488 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001489 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1490 {
1491 if (!HASHITEM_EMPTY(hi))
1492 {
1493 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001494 di = HI2DI(hi);
1495 if (empty || di->di_tv.v_type != VAR_STRING
1496 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001497 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001498 }
1499 }
1500}
1501
1502/*
1503 * List global variables.
1504 */
1505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001507{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001508 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001509}
1510
1511/*
1512 * List buffer variables.
1513 */
1514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001516{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001517 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001518 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001519}
1520
1521/*
1522 * List window variables.
1523 */
1524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001526{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001527 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001528 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001529}
1530
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001531/*
1532 * List tab page variables.
1533 */
1534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001536{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001537 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001538 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001539}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001540
Bram Moolenaara7043832005-01-21 11:56:39 +00001541/*
1542 * List Vim variables.
1543 */
1544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001547 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548}
1549
1550/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001551 * List script-local variables, if there is a script.
1552 */
1553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001554list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001555{
1556 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001557 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1558 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001559}
1560
1561/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001562 * List variables in "arg".
1563 */
1564 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001565list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001566{
1567 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001568 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001570 char_u *name_start;
1571 char_u *arg_subsc;
1572 char_u *tofree;
1573 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001574
1575 while (!ends_excmd(*arg) && !got_int)
1576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001579 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001580 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001581 {
1582 emsg_severe = TRUE;
1583 EMSG(_(e_trailing));
1584 break;
1585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001586 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001589 /* get_name_len() takes care of expanding curly braces */
1590 name_start = name = arg;
1591 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1592 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001594 /* This is mainly to keep test 49 working: when expanding
1595 * curly braces fails overrule the exception error message. */
1596 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001598 emsg_severe = TRUE;
1599 EMSG2(_(e_invarg2), arg);
1600 break;
1601 }
1602 error = TRUE;
1603 }
1604 else
1605 {
1606 if (tofree != NULL)
1607 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001608 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 else
1611 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001612 /* handle d.key, l[idx], f(expr) */
1613 arg_subsc = arg;
1614 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001615 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001617 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001618 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001620 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001621 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001622 case 'g': list_glob_vars(first); break;
1623 case 'b': list_buf_vars(first); break;
1624 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001625 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001626 case 'v': list_vim_vars(first); break;
1627 case 's': list_script_vars(first); break;
1628 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001629 default:
1630 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001632 }
1633 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634 {
1635 char_u numbuf[NUMBUFLEN];
1636 char_u *tf;
1637 int c;
1638 char_u *s;
1639
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001640 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001641 c = *arg;
1642 *arg = NUL;
1643 list_one_var_a((char_u *)"",
1644 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001645 tv.v_type,
1646 s == NULL ? (char_u *)"" : s,
1647 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001648 *arg = c;
1649 vim_free(tf);
1650 }
1651 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 }
1654 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655
1656 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001658
1659 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 }
1661
1662 return arg;
1663}
1664
1665/*
1666 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1667 * Returns a pointer to the char just after the var name.
1668 * Returns NULL if there is an error.
1669 */
1670 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671ex_let_one(
1672 char_u *arg, /* points to variable name */
1673 typval_T *tv, /* value to assign to variable */
1674 int copy, /* copy value from "tv" */
1675 char_u *endchars, /* valid chars after variable name or NULL */
1676 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677{
1678 int c1;
1679 char_u *name;
1680 char_u *p;
1681 char_u *arg_end = NULL;
1682 int len;
1683 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685
1686 /*
1687 * ":let $VAR = expr": Set environment variable.
1688 */
1689 if (*arg == '$')
1690 {
1691 /* Find the end of the name. */
1692 ++arg;
1693 name = arg;
1694 len = get_env_len(&arg);
1695 if (len == 0)
1696 EMSG2(_(e_invarg2), name - 1);
1697 else
1698 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 if (op != NULL && (*op == '+' || *op == '-'))
1700 EMSG2(_(e_letwrong), op);
1701 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001704 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001705 {
1706 c1 = name[len];
1707 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001708 p = get_tv_string_chk(tv);
1709 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 {
1711 int mustfree = FALSE;
1712 char_u *s = vim_getenv(name, &mustfree);
1713
1714 if (s != NULL)
1715 {
1716 p = tofree = concat_str(s, p);
1717 if (mustfree)
1718 vim_free(s);
1719 }
1720 }
1721 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001723 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001724 if (STRICMP(name, "HOME") == 0)
1725 init_homedir();
1726 else if (didset_vim && STRICMP(name, "VIM") == 0)
1727 didset_vim = FALSE;
1728 else if (didset_vimruntime
1729 && STRICMP(name, "VIMRUNTIME") == 0)
1730 didset_vimruntime = FALSE;
1731 arg_end = arg;
1732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 }
1736 }
1737 }
1738
1739 /*
1740 * ":let &option = expr": Set option value.
1741 * ":let &l:option = expr": Set local option value.
1742 * ":let &g:option = expr": Set global option value.
1743 */
1744 else if (*arg == '&')
1745 {
1746 /* Find the end of the name. */
1747 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 if (p == NULL || (endchars != NULL
1749 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 EMSG(_(e_letunexp));
1751 else
1752 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 long n;
1754 int opt_type;
1755 long numval;
1756 char_u *stringval = NULL;
1757 char_u *s;
1758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 c1 = *p;
1760 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001762 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001763 s = get_tv_string_chk(tv); /* != NULL if number or string */
1764 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 {
1766 opt_type = get_option_value(arg, &numval,
1767 &stringval, opt_flags);
1768 if ((opt_type == 1 && *op == '.')
1769 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001772 s = NULL; /* don't set the value */
1773 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 else
1775 {
1776 if (opt_type == 1) /* number */
1777 {
1778 if (*op == '+')
1779 n = numval + n;
1780 else
1781 n = numval - n;
1782 }
1783 else if (opt_type == 0 && stringval != NULL) /* string */
1784 {
1785 s = concat_str(stringval, s);
1786 vim_free(stringval);
1787 stringval = s;
1788 }
1789 }
1790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001791 if (s != NULL)
1792 {
1793 set_option_value(arg, n, s, opt_flags);
1794 arg_end = p;
1795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 }
1799 }
1800
1801 /*
1802 * ":let @r = expr": Set register contents.
1803 */
1804 else if (*arg == '@')
1805 {
1806 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 if (op != NULL && (*op == '+' || *op == '-'))
1808 EMSG2(_(e_letwrong), op);
1809 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 EMSG(_(e_letunexp));
1812 else
1813 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001814 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 char_u *s;
1816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001817 p = get_tv_string_chk(tv);
1818 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001819 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001820 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 if (s != NULL)
1822 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001823 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 vim_free(s);
1825 }
1826 }
1827 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001828 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001830 arg_end = arg + 1;
1831 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001832 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 }
1834 }
1835
1836 /*
1837 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001838 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001840 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001844 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1848 EMSG(_(e_letunexp));
1849 else
1850 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001851 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 arg_end = p;
1853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001855 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 }
1857
1858 else
1859 EMSG2(_(e_invarg2), arg);
1860
1861 return arg_end;
1862}
1863
1864/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001865 * Get an lval: variable, Dict item or List item that can be assigned a value
1866 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1867 * "name.key", "name.key[expr]" etc.
1868 * Indexing only works if "name" is an existing List or Dictionary.
1869 * "name" points to the start of the name.
1870 * If "rettv" is not NULL it points to the value to be assigned.
1871 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1872 * wrong; must end in space or cmd separator.
1873 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001874 * flags:
1875 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001876 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001877 * GLV_NO_AUTOLOAD: do not use script autoloading
1878 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001879 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001880 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001881 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001884get_lval(
1885 char_u *name,
1886 typval_T *rettv,
1887 lval_T *lp,
1888 int unlet,
1889 int skip,
1890 int flags, /* GLV_ values */
1891 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 char_u *expr_start, *expr_end;
1895 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 dictitem_T *v;
1897 typval_T var1;
1898 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001901 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001902 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001904 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908
1909 if (skip)
1910 {
1911 /* When skipping just find the end of the name. */
1912 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001913 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 }
1915
1916 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001917 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001918 if (expr_start != NULL)
1919 {
1920 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001921 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 && *p != '[' && *p != '.')
1923 {
1924 EMSG(_(e_trailing));
1925 return NULL;
1926 }
1927
1928 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1929 if (lp->ll_exp_name == NULL)
1930 {
1931 /* Report an invalid expression in braces, unless the
1932 * expression evaluation has been cancelled due to an
1933 * aborting error, an interrupt, or an exception. */
1934 if (!aborting() && !quiet)
1935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001936 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 EMSG2(_(e_invarg2), name);
1938 return NULL;
1939 }
1940 }
1941 lp->ll_name = lp->ll_exp_name;
1942 }
1943 else
1944 lp->ll_name = name;
1945
1946 /* Without [idx] or .key we are done. */
1947 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1948 return p;
1949
1950 cc = *p;
1951 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001952 /* Only pass &ht when we would write to the variable, it prevents autoload
1953 * as well. */
1954 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1955 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 if (v == NULL && !quiet)
1957 EMSG2(_(e_undefvar), lp->ll_name);
1958 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 if (v == NULL)
1960 return NULL;
1961
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001962 /*
1963 * Loop until no more [idx] or .key is following.
1964 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001966 var1.v_type = VAR_UNKNOWN;
1967 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1971 && !(lp->ll_tv->v_type == VAR_DICT
1972 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!quiet)
1975 EMSG(_("E689: Can only index a List or Dictionary"));
1976 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001980 if (!quiet)
1981 EMSG(_("E708: [:] must come last"));
1982 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001984
Bram Moolenaar8c711452005-01-14 21:53:12 +00001985 len = -1;
1986 if (*p == '.')
1987 {
1988 key = p + 1;
1989 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1990 ;
1991 if (len == 0)
1992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 if (!quiet)
1994 EMSG(_(e_emptykey));
1995 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 }
1997 p = key + len;
1998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001999 else
2000 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002002 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002003 if (*p == ':')
2004 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002005 else
2006 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002007 empty1 = FALSE;
2008 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 if (get_tv_string_chk(&var1) == NULL)
2011 {
2012 /* not a number or string */
2013 clear_tv(&var1);
2014 return NULL;
2015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002016 }
2017
2018 /* Optionally get the second index [ :expr]. */
2019 if (*p == ':')
2020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002022 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002023 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002025 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002027 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (rettv != NULL && (rettv->v_type != VAR_LIST
2029 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 if (!quiet)
2032 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002033 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002035 }
2036 p = skipwhite(p + 1);
2037 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 else
2040 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002042 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2043 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002047 if (get_tv_string_chk(&var2) == NULL)
2048 {
2049 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002050 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002051 clear_tv(&var2);
2052 return NULL;
2053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002055 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002057 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002059
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002061 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 if (!quiet)
2063 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002064 clear_tv(&var1);
2065 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002067 }
2068
2069 /* Skip to past ']'. */
2070 ++p;
2071 }
2072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 {
2075 if (len == -1)
2076 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002078 key = get_tv_string_chk(&var1); /* is number or string */
2079 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002081 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002083 }
2084 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 lp->ll_list = NULL;
2086 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002087 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002088
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002089 /* When assigning to a scope dictionary check that a function and
2090 * variable name is valid (only variable name unless it is l: or
2091 * g: dictionary). Disallow overwriting a builtin function. */
2092 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002093 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002094 int prevval;
2095 int wrong;
2096
2097 if (len != -1)
2098 {
2099 prevval = key[len];
2100 key[len] = NUL;
2101 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002102 else
2103 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002104 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2105 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002106 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002107 || !valid_varname(key);
2108 if (len != -1)
2109 key[len] = prevval;
2110 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002111 return NULL;
2112 }
2113
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002116 /* Can't add "v:" variable. */
2117 if (lp->ll_dict == &vimvardict)
2118 {
2119 EMSG2(_(e_illvar), name);
2120 return NULL;
2121 }
2122
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002123 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002128 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002130 }
2131 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 p = NULL;
2138 break;
2139 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002140 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002141 else if ((flags & GLV_READ_ONLY) == 0
2142 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002143 {
2144 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002145 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002146 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002147
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002148 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002150 }
2151 else
2152 {
2153 /*
2154 * Get the number and item for the only or first index of the List.
2155 */
2156 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002159 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002160 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002161 clear_tv(&var1);
2162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 lp->ll_list = lp->ll_tv->vval.v_list;
2165 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2166 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002167 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002168 if (lp->ll_n1 < 0)
2169 {
2170 lp->ll_n1 = 0;
2171 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2172 }
2173 }
2174 if (lp->ll_li == NULL)
2175 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002176 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002177 if (!quiet)
2178 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 }
2181
2182 /*
2183 * May need to find the item or absolute index for the second
2184 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 * When no index given: "lp->ll_empty2" is TRUE.
2186 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002187 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002190 lp->ll_n2 = (long)get_tv_number(&var2);
2191 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002196 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002197 {
2198 if (!quiet)
2199 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002201 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 }
2204
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2206 if (lp->ll_n1 < 0)
2207 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2208 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002209 {
2210 if (!quiet)
2211 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002213 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002214 }
2215
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
2219
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002220 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 return p;
2222}
2223
2224/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002225 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229{
2230 vim_free(lp->ll_exp_name);
2231 vim_free(lp->ll_newkey);
2232}
2233
2234/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002235 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002237 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 */
2239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240set_var_lval(
2241 lval_T *lp,
2242 char_u *endp,
2243 typval_T *rettv,
2244 int copy,
2245 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246{
2247 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 listitem_T *ri;
2249 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250
2251 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002253 cc = *endp;
2254 *endp = NUL;
2255 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002257 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258
Bram Moolenaar79518e22017-02-17 16:31:35 +01002259 /* handle +=, -= and .= */
2260 di = NULL;
2261 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2262 &tv, &di, TRUE, FALSE) == OK)
2263 {
2264 if ((di == NULL
2265 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2266 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2267 FALSE)))
2268 && tv_op(&tv, rettv, op) == OK)
2269 set_var(lp->ll_name, &tv, FALSE);
2270 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002273 else
2274 set_var(lp->ll_name, rettv, copy);
2275 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002277 else if (tv_check_lock(lp->ll_newkey == NULL
2278 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002279 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002280 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 else if (lp->ll_range)
2282 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002283 listitem_T *ll_li = lp->ll_li;
2284 int ll_n1 = lp->ll_n1;
2285
2286 /*
2287 * Check whether any of the list items is locked
2288 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002289 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002290 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002291 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002292 return;
2293 ri = ri->li_next;
2294 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2295 break;
2296 ll_li = ll_li->li_next;
2297 ++ll_n1;
2298 }
2299
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 /*
2301 * Assign the List values to the list items.
2302 */
2303 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002304 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 if (op != NULL && *op != '=')
2306 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2307 else
2308 {
2309 clear_tv(&lp->ll_li->li_tv);
2310 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2311 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 ri = ri->li_next;
2313 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2314 break;
2315 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002318 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002319 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 ri = NULL;
2321 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002322 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 lp->ll_li = lp->ll_li->li_next;
2325 ++lp->ll_n1;
2326 }
2327 if (ri != NULL)
2328 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 else if (lp->ll_empty2
2330 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 : lp->ll_n1 != lp->ll_n2)
2332 EMSG(_("E711: List value has not enough items"));
2333 }
2334 else
2335 {
2336 /*
2337 * Assign to a List or Dictionary item.
2338 */
2339 if (lp->ll_newkey != NULL)
2340 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 if (op != NULL && *op != '=')
2342 {
2343 EMSG2(_(e_letwrong), op);
2344 return;
2345 }
2346
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002348 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 if (di == NULL)
2350 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002351 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2352 {
2353 vim_free(di);
2354 return;
2355 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002357 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 else if (op != NULL && *op != '=')
2359 {
2360 tv_op(lp->ll_tv, rettv, op);
2361 return;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /*
2367 * Assign the value to the variable or list item.
2368 */
2369 if (copy)
2370 copy_tv(rettv, lp->ll_tv);
2371 else
2372 {
2373 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002374 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
2377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378}
2379
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2382 * Returns OK or FAIL.
2383 */
2384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002385tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002387 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 char_u numbuf[NUMBUFLEN];
2389 char_u *s;
2390
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002391 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2392 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2393 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
2395 switch (tv1->v_type)
2396 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002397 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 case VAR_DICT:
2399 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002400 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002401 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002402 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002403 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 break;
2405
2406 case VAR_LIST:
2407 if (*op != '+' || tv2->v_type != VAR_LIST)
2408 break;
2409 /* List += List */
2410 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2411 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2412 return OK;
2413
2414 case VAR_NUMBER:
2415 case VAR_STRING:
2416 if (tv2->v_type == VAR_LIST)
2417 break;
2418 if (*op == '+' || *op == '-')
2419 {
2420 /* nr += nr or nr -= nr*/
2421 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002422#ifdef FEAT_FLOAT
2423 if (tv2->v_type == VAR_FLOAT)
2424 {
2425 float_T f = n;
2426
2427 if (*op == '+')
2428 f += tv2->vval.v_float;
2429 else
2430 f -= tv2->vval.v_float;
2431 clear_tv(tv1);
2432 tv1->v_type = VAR_FLOAT;
2433 tv1->vval.v_float = f;
2434 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002436#endif
2437 {
2438 if (*op == '+')
2439 n += get_tv_number(tv2);
2440 else
2441 n -= get_tv_number(tv2);
2442 clear_tv(tv1);
2443 tv1->v_type = VAR_NUMBER;
2444 tv1->vval.v_number = n;
2445 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 }
2447 else
2448 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002449 if (tv2->v_type == VAR_FLOAT)
2450 break;
2451
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 /* str .= str */
2453 s = get_tv_string(tv1);
2454 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2455 clear_tv(tv1);
2456 tv1->v_type = VAR_STRING;
2457 tv1->vval.v_string = s;
2458 }
2459 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002460
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002461 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002462#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002463 {
2464 float_T f;
2465
2466 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2467 && tv2->v_type != VAR_NUMBER
2468 && tv2->v_type != VAR_STRING))
2469 break;
2470 if (tv2->v_type == VAR_FLOAT)
2471 f = tv2->vval.v_float;
2472 else
2473 f = get_tv_number(tv2);
2474 if (*op == '+')
2475 tv1->vval.v_float += f;
2476 else
2477 tv1->vval.v_float -= f;
2478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002479#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002480 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 }
2482 }
2483
2484 EMSG2(_(e_letwrong), op);
2485 return FAIL;
2486}
2487
2488/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002489 * Evaluate the expression used in a ":for var in expr" command.
2490 * "arg" points to "var".
2491 * Set "*errp" to TRUE for an error, FALSE otherwise;
2492 * Return a pointer that holds the info. Null when there is an error.
2493 */
2494 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002495eval_for_line(
2496 char_u *arg,
2497 int *errp,
2498 char_u **nextcmdp,
2499 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002500{
Bram Moolenaar33570922005-01-25 22:26:29 +00002501 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002502 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 typval_T tv;
2504 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002505
2506 *errp = TRUE; /* default: there is an error */
2507
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509 if (fi == NULL)
2510 return NULL;
2511
2512 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2513 if (expr == NULL)
2514 return fi;
2515
2516 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002517 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002518 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002519 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002520 return fi;
2521 }
2522
2523 if (skip)
2524 ++emsg_skip;
2525 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2526 {
2527 *errp = FALSE;
2528 if (!skip)
2529 {
2530 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002531 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002532 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002533 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002534 clear_tv(&tv);
2535 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002536 else if (l == NULL)
2537 {
2538 /* a null list is like an empty list: do nothing */
2539 clear_tv(&tv);
2540 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002541 else
2542 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002543 /* No need to increment the refcount, it's already set for the
2544 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545 fi->fi_list = l;
2546 list_add_watch(l, &fi->fi_lw);
2547 fi->fi_lw.lw_item = l->lv_first;
2548 }
2549 }
2550 }
2551 if (skip)
2552 --emsg_skip;
2553
2554 return fi;
2555}
2556
2557/*
2558 * Use the first item in a ":for" list. Advance to the next.
2559 * Assign the values to the variable (list). "arg" points to the first one.
2560 * Return TRUE when a valid item was found, FALSE when at end of list or
2561 * something wrong.
2562 */
2563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002564next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002565{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002566 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002567 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002569
2570 item = fi->fi_lw.lw_item;
2571 if (item == NULL)
2572 result = FALSE;
2573 else
2574 {
2575 fi->fi_lw.lw_item = item->li_next;
2576 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2577 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2578 }
2579 return result;
2580}
2581
2582/*
2583 * Free the structure used to store info used by ":for".
2584 */
2585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002586free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002587{
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002589
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002590 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002591 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002592 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002593 list_unref(fi->fi_list);
2594 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002595 vim_free(fi);
2596}
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2599
2600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002601set_context_for_expression(
2602 expand_T *xp,
2603 char_u *arg,
2604 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 int got_eq = FALSE;
2607 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002608 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002610 if (cmdidx == CMD_let)
2611 {
2612 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002613 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002614 {
2615 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002616 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002617 {
2618 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002619 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002620 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002621 break;
2622 }
2623 return;
2624 }
2625 }
2626 else
2627 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2628 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 while ((xp->xp_pattern = vim_strpbrk(arg,
2630 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2631 {
2632 c = *xp->xp_pattern;
2633 if (c == '&')
2634 {
2635 c = xp->xp_pattern[1];
2636 if (c == '&')
2637 {
2638 ++xp->xp_pattern;
2639 xp->xp_context = cmdidx != CMD_let || got_eq
2640 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2641 }
2642 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002645 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2646 xp->xp_pattern += 2;
2647
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 else if (c == '$')
2651 {
2652 /* environment variable */
2653 xp->xp_context = EXPAND_ENV_VARS;
2654 }
2655 else if (c == '=')
2656 {
2657 got_eq = TRUE;
2658 xp->xp_context = EXPAND_EXPRESSION;
2659 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002660 else if (c == '#'
2661 && xp->xp_context == EXPAND_EXPRESSION)
2662 {
2663 /* Autoload function/variable contains '#'. */
2664 break;
2665 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002666 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 && xp->xp_context == EXPAND_FUNCTIONS
2668 && vim_strchr(xp->xp_pattern, '(') == NULL)
2669 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002670 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 break;
2672 }
2673 else if (cmdidx != CMD_let || got_eq)
2674 {
2675 if (c == '"') /* string */
2676 {
2677 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2678 if (c == '\\' && xp->xp_pattern[1] != NUL)
2679 ++xp->xp_pattern;
2680 xp->xp_context = EXPAND_NOTHING;
2681 }
2682 else if (c == '\'') /* literal string */
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2686 /* skip */ ;
2687 xp->xp_context = EXPAND_NOTHING;
2688 }
2689 else if (c == '|')
2690 {
2691 if (xp->xp_pattern[1] == '|')
2692 {
2693 ++xp->xp_pattern;
2694 xp->xp_context = EXPAND_EXPRESSION;
2695 }
2696 else
2697 xp->xp_context = EXPAND_COMMANDS;
2698 }
2699 else
2700 xp->xp_context = EXPAND_EXPRESSION;
2701 }
2702 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002703 /* Doesn't look like something valid, expand as an expression
2704 * anyway. */
2705 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 arg = xp->xp_pattern;
2707 if (*arg != NUL)
2708 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2709 /* skip */ ;
2710 }
2711 xp->xp_pattern = arg;
2712}
2713
2714#endif /* FEAT_CMDL_COMPL */
2715
2716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 * ":unlet[!] var1 ... " command.
2718 */
2719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002720ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002722 ex_unletlock(eap, eap->arg, 0);
2723}
2724
2725/*
2726 * ":lockvar" and ":unlockvar" commands
2727 */
2728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002730{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002732 int deep = 2;
2733
2734 if (eap->forceit)
2735 deep = -1;
2736 else if (vim_isdigit(*arg))
2737 {
2738 deep = getdigits(&arg);
2739 arg = skipwhite(arg);
2740 }
2741
2742 ex_unletlock(eap, arg, deep);
2743}
2744
2745/*
2746 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2747 */
2748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002749ex_unletlock(
2750 exarg_T *eap,
2751 char_u *argstart,
2752 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002753{
2754 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002757 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 do
2760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002762 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002763 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lv.ll_name == NULL)
2765 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002766 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (name_end != NULL)
2770 {
2771 emsg_severe = TRUE;
2772 EMSG(_(e_trailing));
2773 }
2774 if (!(eap->skip || error))
2775 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 break;
2777 }
2778
2779 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002780 {
2781 if (eap->cmdidx == CMD_unlet)
2782 {
2783 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2784 error = TRUE;
2785 }
2786 else
2787 {
2788 if (do_lock_var(&lv, name_end, deep,
2789 eap->cmdidx == CMD_lockvar) == FAIL)
2790 error = TRUE;
2791 }
2792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!eap->skip)
2795 clear_lval(&lv);
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 arg = skipwhite(name_end);
2798 } while (!ends_excmd(*arg));
2799
2800 eap->nextcmd = check_nextcmd(arg);
2801}
2802
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002804do_unlet_var(
2805 lval_T *lp,
2806 char_u *name_end,
2807 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 int ret = OK;
2810 int cc;
2811
2812 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 cc = *name_end;
2815 *name_end = NUL;
2816
2817 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002818 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002822 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002823 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002824 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002825 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002826 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 else if (lp->ll_range)
2828 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002830 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002831 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002832
2833 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2834 {
2835 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002836 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002837 return FAIL;
2838 ll_li = li;
2839 ++ll_n1;
2840 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841
2842 /* Delete a range of List items. */
2843 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2844 {
2845 li = lp->ll_li->li_next;
2846 listitem_remove(lp->ll_list, lp->ll_li);
2847 lp->ll_li = li;
2848 ++lp->ll_n1;
2849 }
2850 }
2851 else
2852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 /* unlet a List item. */
2855 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002858 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 }
2860
2861 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862}
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864/*
2865 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002866 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
2868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 hashtab_T *ht;
2872 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002873 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002874 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002875 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 ht = find_var_ht(name, &varname);
2878 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002880 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002881 if (d == NULL)
2882 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883 if (ht == &globvarht)
2884 d = &globvardict;
2885 else if (ht == &compat_hashtab)
2886 d = &vimvardict;
2887 else
2888 {
2889 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2890 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2891 }
2892 if (d == NULL)
2893 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002894 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002895 return FAIL;
2896 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002897 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002899 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002900 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002901 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002902 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002903 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002904 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002905 || var_check_ro(di->di_flags, name, FALSE)
2906 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002907 return FAIL;
2908
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909 delete_var(ht, hi);
2910 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913 if (forceit)
2914 return OK;
2915 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 return FAIL;
2917}
2918
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919/*
2920 * Lock or unlock variable indicated by "lp".
2921 * "deep" is the levels to go (-1 for unlimited);
2922 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2923 */
2924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925do_lock_var(
2926 lval_T *lp,
2927 char_u *name_end,
2928 int deep,
2929 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930{
2931 int ret = OK;
2932 int cc;
2933 dictitem_T *di;
2934
2935 if (deep == 0) /* nothing to do */
2936 return OK;
2937
2938 if (lp->ll_tv == NULL)
2939 {
2940 cc = *name_end;
2941 *name_end = NUL;
2942
2943 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002944 di = find_var(lp->ll_name, NULL, TRUE);
2945 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002947 else if ((di->di_flags & DI_FLAGS_FIX)
2948 && di->di_tv.v_type != VAR_DICT
2949 && di->di_tv.v_type != VAR_LIST)
2950 /* For historic reasons this error is not given for a list or dict.
2951 * E.g., the b: dict could be locked/unlocked. */
2952 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 else
2954 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002955 if (lock)
2956 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002957 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002958 di->di_flags &= ~DI_FLAGS_LOCK;
2959 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 }
2961 *name_end = cc;
2962 }
2963 else if (lp->ll_range)
2964 {
2965 listitem_T *li = lp->ll_li;
2966
2967 /* (un)lock a range of List items. */
2968 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2969 {
2970 item_lock(&li->li_tv, deep, lock);
2971 li = li->li_next;
2972 ++lp->ll_n1;
2973 }
2974 }
2975 else if (lp->ll_list != NULL)
2976 /* (un)lock a List item. */
2977 item_lock(&lp->ll_li->li_tv, deep, lock);
2978 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002979 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002980 item_lock(&lp->ll_di->di_tv, deep, lock);
2981
2982 return ret;
2983}
2984
2985/*
2986 * Lock or unlock an item. "deep" is nr of levels to go.
2987 */
2988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002989item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002990{
2991 static int recurse = 0;
2992 list_T *l;
2993 listitem_T *li;
2994 dict_T *d;
2995 hashitem_T *hi;
2996 int todo;
2997
2998 if (recurse >= DICT_MAXNEST)
2999 {
3000 EMSG(_("E743: variable nested too deep for (un)lock"));
3001 return;
3002 }
3003 if (deep == 0)
3004 return;
3005 ++recurse;
3006
3007 /* lock/unlock the item itself */
3008 if (lock)
3009 tv->v_lock |= VAR_LOCKED;
3010 else
3011 tv->v_lock &= ~VAR_LOCKED;
3012
3013 switch (tv->v_type)
3014 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003015 case VAR_UNKNOWN:
3016 case VAR_NUMBER:
3017 case VAR_STRING:
3018 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003019 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003020 case VAR_FLOAT:
3021 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003022 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003023 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003024 break;
3025
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003026 case VAR_LIST:
3027 if ((l = tv->vval.v_list) != NULL)
3028 {
3029 if (lock)
3030 l->lv_lock |= VAR_LOCKED;
3031 else
3032 l->lv_lock &= ~VAR_LOCKED;
3033 if (deep < 0 || deep > 1)
3034 /* recursive: lock/unlock the items the List contains */
3035 for (li = l->lv_first; li != NULL; li = li->li_next)
3036 item_lock(&li->li_tv, deep - 1, lock);
3037 }
3038 break;
3039 case VAR_DICT:
3040 if ((d = tv->vval.v_dict) != NULL)
3041 {
3042 if (lock)
3043 d->dv_lock |= VAR_LOCKED;
3044 else
3045 d->dv_lock &= ~VAR_LOCKED;
3046 if (deep < 0 || deep > 1)
3047 {
3048 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003049 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3051 {
3052 if (!HASHITEM_EMPTY(hi))
3053 {
3054 --todo;
3055 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3056 }
3057 }
3058 }
3059 }
3060 }
3061 --recurse;
3062}
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3065/*
3066 * Delete all "menutrans_" variables.
3067 */
3068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003069del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
Bram Moolenaar33570922005-01-25 22:26:29 +00003071 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003072 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
Bram Moolenaar33570922005-01-25 22:26:29 +00003074 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003075 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003076 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003077 {
3078 if (!HASHITEM_EMPTY(hi))
3079 {
3080 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003081 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3082 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003083 }
3084 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}
3087#endif
3088
3089#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3090
3091/*
3092 * Local string buffer for the next two functions to store a variable name
3093 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3094 * get_user_var_name().
3095 */
3096
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003097static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099static char_u *varnamebuf = NULL;
3100static int varnamebuflen = 0;
3101
3102/*
3103 * Function to concatenate a prefix and a variable name.
3104 */
3105 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003106cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 int len;
3109
3110 len = (int)STRLEN(name) + 3;
3111 if (len > varnamebuflen)
3112 {
3113 vim_free(varnamebuf);
3114 len += 10; /* some additional space */
3115 varnamebuf = alloc(len);
3116 if (varnamebuf == NULL)
3117 {
3118 varnamebuflen = 0;
3119 return NULL;
3120 }
3121 varnamebuflen = len;
3122 }
3123 *varnamebuf = prefix;
3124 varnamebuf[1] = ':';
3125 STRCPY(varnamebuf + 2, name);
3126 return varnamebuf;
3127}
3128
3129/*
3130 * Function given to ExpandGeneric() to obtain the list of user defined
3131 * (global/buffer/window/built-in) variable names.
3132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003134get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003136 static long_u gdone;
3137 static long_u bdone;
3138 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003140 static int vidx;
3141 static hashitem_T *hi;
3142 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003145 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003146 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003147 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003148 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003149
3150 /* Global variables */
3151 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003153 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003155 else
3156 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003157 while (HASHITEM_EMPTY(hi))
3158 ++hi;
3159 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3160 return cat_prefix_varname('g', hi->hi_key);
3161 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003163
3164 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003165 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003168 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003170 else
3171 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003172 while (HASHITEM_EMPTY(hi))
3173 ++hi;
3174 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003176
3177 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003178 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003181 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003183 else
3184 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003185 while (HASHITEM_EMPTY(hi))
3186 ++hi;
3187 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003189
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003190 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003191 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003192 if (tdone < ht->ht_used)
3193 {
3194 if (tdone++ == 0)
3195 hi = ht->ht_array;
3196 else
3197 ++hi;
3198 while (HASHITEM_EMPTY(hi))
3199 ++hi;
3200 return cat_prefix_varname('t', hi->hi_key);
3201 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003202
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 /* v: variables */
3204 if (vidx < VV_LEN)
3205 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaard23a8232018-02-10 18:45:26 +01003207 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 varnamebuflen = 0;
3209 return NULL;
3210}
3211
3212#endif /* FEAT_CMDL_COMPL */
3213
3214/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003215 * Return TRUE if "pat" matches "text".
3216 * Does not use 'cpo' and always uses 'magic'.
3217 */
3218 static int
3219pattern_match(char_u *pat, char_u *text, int ic)
3220{
3221 int matches = FALSE;
3222 char_u *save_cpo;
3223 regmatch_T regmatch;
3224
3225 /* avoid 'l' flag in 'cpoptions' */
3226 save_cpo = p_cpo;
3227 p_cpo = (char_u *)"";
3228 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3229 if (regmatch.regprog != NULL)
3230 {
3231 regmatch.rm_ic = ic;
3232 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3233 vim_regfree(regmatch.regprog);
3234 }
3235 p_cpo = save_cpo;
3236 return matches;
3237}
3238
3239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003241 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3243 */
3244
3245/*
3246 * Handle zero level expression.
3247 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003248 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003249 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * Return OK or FAIL.
3251 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253eval0(
3254 char_u *arg,
3255 typval_T *rettv,
3256 char_u **nextcmd,
3257 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 int ret;
3260 char_u *p;
3261
3262 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003263 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (ret == FAIL || !ends_excmd(*p))
3265 {
3266 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003267 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 /*
3269 * Report the invalid expression unless the expression evaluation has
3270 * been cancelled due to an aborting error, an interrupt, or an
3271 * exception.
3272 */
3273 if (!aborting())
3274 EMSG2(_(e_invexpr2), arg);
3275 ret = FAIL;
3276 }
3277 if (nextcmd != NULL)
3278 *nextcmd = check_nextcmd(p);
3279
3280 return ret;
3281}
3282
3283/*
3284 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003285 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 *
3287 * "arg" must point to the first non-white of the expression.
3288 * "arg" is advanced to the next non-white after the recognized expression.
3289 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003290 * Note: "rettv.v_lock" is not set.
3291 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 * Return OK or FAIL.
3293 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300 /*
3301 * Get the first variable.
3302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003303 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return FAIL;
3305
3306 if ((*arg)[0] == '?')
3307 {
3308 result = FALSE;
3309 if (evaluate)
3310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311 int error = FALSE;
3312
3313 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003315 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003316 if (error)
3317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319
3320 /*
3321 * Get the second variable.
3322 */
3323 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return FAIL;
3326
3327 /*
3328 * Check for the ":".
3329 */
3330 if ((*arg)[0] != ':')
3331 {
3332 EMSG(_("E109: Missing ':' after '?'"));
3333 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return FAIL;
3336 }
3337
3338 /*
3339 * Get the third variable.
3340 */
3341 *arg = skipwhite(*arg + 1);
3342 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3343 {
3344 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 return FAIL;
3347 }
3348 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
3351
3352 return OK;
3353}
3354
3355/*
3356 * Handle first level expression:
3357 * expr2 || expr2 || expr2 logical OR
3358 *
3359 * "arg" must point to the first non-white of the expression.
3360 * "arg" is advanced to the next non-white after the recognized expression.
3361 *
3362 * Return OK or FAIL.
3363 */
3364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
Bram Moolenaar33570922005-01-25 22:26:29 +00003367 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 long result;
3369 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003370 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372 /*
3373 * Get the first variable.
3374 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003375 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return FAIL;
3377
3378 /*
3379 * Repeat until there is no following "||".
3380 */
3381 first = TRUE;
3382 result = FALSE;
3383 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3384 {
3385 if (evaluate && first)
3386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003387 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003389 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003390 if (error)
3391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 first = FALSE;
3393 }
3394
3395 /*
3396 * Get the second variable.
3397 */
3398 *arg = skipwhite(*arg + 2);
3399 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3400 return FAIL;
3401
3402 /*
3403 * Compute the result.
3404 */
3405 if (evaluate && !result)
3406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 if (error)
3411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413 if (evaluate)
3414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 rettv->v_type = VAR_NUMBER;
3416 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418 }
3419
3420 return OK;
3421}
3422
3423/*
3424 * Handle second level expression:
3425 * expr3 && expr3 && expr3 logical AND
3426 *
3427 * "arg" must point to the first non-white of the expression.
3428 * "arg" is advanced to the next non-white after the recognized expression.
3429 *
3430 * Return OK or FAIL.
3431 */
3432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003433eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Bram Moolenaar33570922005-01-25 22:26:29 +00003435 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 long result;
3437 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003438 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /*
3441 * Get the first variable.
3442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003443 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 return FAIL;
3445
3446 /*
3447 * Repeat until there is no following "&&".
3448 */
3449 first = TRUE;
3450 result = TRUE;
3451 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3452 {
3453 if (evaluate && first)
3454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003455 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003457 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003458 if (error)
3459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 first = FALSE;
3461 }
3462
3463 /*
3464 * Get the second variable.
3465 */
3466 *arg = skipwhite(*arg + 2);
3467 if (eval4(arg, &var2, evaluate && result) == FAIL)
3468 return FAIL;
3469
3470 /*
3471 * Compute the result.
3472 */
3473 if (evaluate && result)
3474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003475 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003477 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003478 if (error)
3479 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 if (evaluate)
3482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 rettv->v_type = VAR_NUMBER;
3484 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486 }
3487
3488 return OK;
3489}
3490
3491/*
3492 * Handle third level expression:
3493 * var1 == var2
3494 * var1 =~ var2
3495 * var1 != var2
3496 * var1 !~ var2
3497 * var1 > var2
3498 * var1 >= var2
3499 * var1 < var2
3500 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003501 * var1 is var2
3502 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *
3504 * "arg" must point to the first non-white of the expression.
3505 * "arg" is advanced to the next non-white after the recognized expression.
3506 *
3507 * Return OK or FAIL.
3508 */
3509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003510eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
Bram Moolenaar33570922005-01-25 22:26:29 +00003512 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 char_u *p;
3514 int i;
3515 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003516 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
3520 /*
3521 * Get the first variable.
3522 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 return FAIL;
3525
3526 p = *arg;
3527 switch (p[0])
3528 {
3529 case '=': if (p[1] == '=')
3530 type = TYPE_EQUAL;
3531 else if (p[1] == '~')
3532 type = TYPE_MATCH;
3533 break;
3534 case '!': if (p[1] == '=')
3535 type = TYPE_NEQUAL;
3536 else if (p[1] == '~')
3537 type = TYPE_NOMATCH;
3538 break;
3539 case '>': if (p[1] != '=')
3540 {
3541 type = TYPE_GREATER;
3542 len = 1;
3543 }
3544 else
3545 type = TYPE_GEQUAL;
3546 break;
3547 case '<': if (p[1] != '=')
3548 {
3549 type = TYPE_SMALLER;
3550 len = 1;
3551 }
3552 else
3553 type = TYPE_SEQUAL;
3554 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003555 case 'i': if (p[1] == 's')
3556 {
3557 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3558 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003559 i = p[len];
3560 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003561 {
3562 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3563 type_is = TRUE;
3564 }
3565 }
3566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
3569 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 */
3572 if (type != TYPE_UNKNOWN)
3573 {
3574 /* extra question mark appended: ignore case */
3575 if (p[len] == '?')
3576 {
3577 ic = TRUE;
3578 ++len;
3579 }
3580 /* extra '#' appended: match case */
3581 else if (p[len] == '#')
3582 {
3583 ic = FALSE;
3584 ++len;
3585 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003586 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else
3588 ic = p_ic;
3589
3590 /*
3591 * Get the second variable.
3592 */
3593 *arg = skipwhite(p + len);
3594 if (eval5(arg, &var2, evaluate) == FAIL)
3595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return FAIL;
3598 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003599 return typval_compare(rettv, &var2, type, type_is, ic, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601
3602 return OK;
3603}
3604
3605/*
3606 * Handle fourth level expression:
3607 * + number addition
3608 * - number subtraction
3609 * . string concatenation
3610 *
3611 * "arg" must point to the first non-white of the expression.
3612 * "arg" is advanced to the next non-white after the recognized expression.
3613 *
3614 * Return OK or FAIL.
3615 */
3616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
Bram Moolenaar33570922005-01-25 22:26:29 +00003619 typval_T var2;
3620 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003622 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003623#ifdef FEAT_FLOAT
3624 float_T f1 = 0, f2 = 0;
3625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_u *s1, *s2;
3627 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3628 char_u *p;
3629
3630 /*
3631 * Get the first variable.
3632 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003633 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 return FAIL;
3635
3636 /*
3637 * Repeat computing, until no '+', '-' or '.' is following.
3638 */
3639 for (;;)
3640 {
3641 op = **arg;
3642 if (op != '+' && op != '-' && op != '.')
3643 break;
3644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003645 if ((op != '+' || rettv->v_type != VAR_LIST)
3646#ifdef FEAT_FLOAT
3647 && (op == '.' || rettv->v_type != VAR_FLOAT)
3648#endif
3649 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003650 {
3651 /* For "list + ...", an illegal use of the first operand as
3652 * a number cannot be determined before evaluating the 2nd
3653 * operand: if this is also a list, all is ok.
3654 * For "something . ...", "something - ..." or "non-list + ...",
3655 * we know that the first operand needs to be a string or number
3656 * without evaluating the 2nd operand. So check before to avoid
3657 * side effects after an error. */
3658 if (evaluate && get_tv_string_chk(rettv) == NULL)
3659 {
3660 clear_tv(rettv);
3661 return FAIL;
3662 }
3663 }
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 /*
3666 * Get the second variable.
3667 */
3668 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003669 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FAIL;
3673 }
3674
3675 if (evaluate)
3676 {
3677 /*
3678 * Compute the result.
3679 */
3680 if (op == '.')
3681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3683 s2 = get_tv_string_buf_chk(&var2, buf2);
3684 if (s2 == NULL) /* type error ? */
3685 {
3686 clear_tv(rettv);
3687 clear_tv(&var2);
3688 return FAIL;
3689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003690 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003691 clear_tv(rettv);
3692 rettv->v_type = VAR_STRING;
3693 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003695 else if (op == '+' && rettv->v_type == VAR_LIST
3696 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003697 {
3698 /* concatenate Lists */
3699 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3700 &var3) == FAIL)
3701 {
3702 clear_tv(rettv);
3703 clear_tv(&var2);
3704 return FAIL;
3705 }
3706 clear_tv(rettv);
3707 *rettv = var3;
3708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else
3710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003711 int error = FALSE;
3712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003713#ifdef FEAT_FLOAT
3714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003716 f1 = rettv->vval.v_float;
3717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003719 else
3720#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003722 n1 = get_tv_number_chk(rettv, &error);
3723 if (error)
3724 {
3725 /* This can only happen for "list + non-list". For
3726 * "non-list + ..." or "something - ...", we returned
3727 * before evaluating the 2nd operand. */
3728 clear_tv(rettv);
3729 return FAIL;
3730 }
3731#ifdef FEAT_FLOAT
3732 if (var2.v_type == VAR_FLOAT)
3733 f1 = n1;
3734#endif
3735 }
3736#ifdef FEAT_FLOAT
3737 if (var2.v_type == VAR_FLOAT)
3738 {
3739 f2 = var2.vval.v_float;
3740 n2 = 0;
3741 }
3742 else
3743#endif
3744 {
3745 n2 = get_tv_number_chk(&var2, &error);
3746 if (error)
3747 {
3748 clear_tv(rettv);
3749 clear_tv(&var2);
3750 return FAIL;
3751 }
3752#ifdef FEAT_FLOAT
3753 if (rettv->v_type == VAR_FLOAT)
3754 f2 = n2;
3755#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003758
3759#ifdef FEAT_FLOAT
3760 /* If there is a float on either side the result is a float. */
3761 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3762 {
3763 if (op == '+')
3764 f1 = f1 + f2;
3765 else
3766 f1 = f1 - f2;
3767 rettv->v_type = VAR_FLOAT;
3768 rettv->vval.v_float = f1;
3769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003771#endif
3772 {
3773 if (op == '+')
3774 n1 = n1 + n2;
3775 else
3776 n1 = n1 - n2;
3777 rettv->v_type = VAR_NUMBER;
3778 rettv->vval.v_number = n1;
3779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 }
3784 return OK;
3785}
3786
3787/*
3788 * Handle fifth level expression:
3789 * * number multiplication
3790 * / number division
3791 * % number modulo
3792 *
3793 * "arg" must point to the first non-white of the expression.
3794 * "arg" is advanced to the next non-white after the recognized expression.
3795 *
3796 * Return OK or FAIL.
3797 */
3798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003799eval6(
3800 char_u **arg,
3801 typval_T *rettv,
3802 int evaluate,
3803 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003807 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003808#ifdef FEAT_FLOAT
3809 int use_float = FALSE;
3810 float_T f1 = 0, f2;
3811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 /*
3815 * Get the first variable.
3816 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003817 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return FAIL;
3819
3820 /*
3821 * Repeat computing, until no '*', '/' or '%' is following.
3822 */
3823 for (;;)
3824 {
3825 op = **arg;
3826 if (op != '*' && op != '/' && op != '%')
3827 break;
3828
3829 if (evaluate)
3830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003831#ifdef FEAT_FLOAT
3832 if (rettv->v_type == VAR_FLOAT)
3833 {
3834 f1 = rettv->vval.v_float;
3835 use_float = TRUE;
3836 n1 = 0;
3837 }
3838 else
3839#endif
3840 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 if (error)
3843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 else
3846 n1 = 0;
3847
3848 /*
3849 * Get the second variable.
3850 */
3851 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003852 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return FAIL;
3854
3855 if (evaluate)
3856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003857#ifdef FEAT_FLOAT
3858 if (var2.v_type == VAR_FLOAT)
3859 {
3860 if (!use_float)
3861 {
3862 f1 = n1;
3863 use_float = TRUE;
3864 }
3865 f2 = var2.vval.v_float;
3866 n2 = 0;
3867 }
3868 else
3869#endif
3870 {
3871 n2 = get_tv_number_chk(&var2, &error);
3872 clear_tv(&var2);
3873 if (error)
3874 return FAIL;
3875#ifdef FEAT_FLOAT
3876 if (use_float)
3877 f2 = n2;
3878#endif
3879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 /*
3882 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003883 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003885#ifdef FEAT_FLOAT
3886 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003888 if (op == '*')
3889 f1 = f1 * f2;
3890 else if (op == '/')
3891 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003892# ifdef VMS
3893 /* VMS crashes on divide by zero, work around it */
3894 if (f2 == 0.0)
3895 {
3896 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003897 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003898 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003899 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003900 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003901 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003902 }
3903 else
3904 f1 = f1 / f2;
3905# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003906 /* We rely on the floating point library to handle divide
3907 * by zero to result in "inf" and not a crash. */
3908 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003909# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003912 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00003913 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003914 return FAIL;
3915 }
3916 rettv->v_type = VAR_FLOAT;
3917 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003922 if (op == '*')
3923 n1 = n1 * n2;
3924 else if (op == '/')
3925 {
3926 if (n2 == 0) /* give an error message? */
3927 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003928 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003929 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003930 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003931 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003932 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003933 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003934 }
3935 else
3936 n1 = n1 / n2;
3937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003939 {
3940 if (n2 == 0) /* give an error message? */
3941 n1 = 0;
3942 else
3943 n1 = n1 % n2;
3944 }
3945 rettv->v_type = VAR_NUMBER;
3946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 }
3950
3951 return OK;
3952}
3953
3954/*
3955 * Handle sixth level expression:
3956 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003957 * "string" string constant
3958 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 * &option-name option value
3960 * @r register contents
3961 * identifier variable value
3962 * function() function call
3963 * $VAR environment variable
3964 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003965 * [expr, expr] List
3966 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 *
3968 * Also handle:
3969 * ! in front logical NOT
3970 * - in front unary minus
3971 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003972 * trailing [] subscript in String or List
3973 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 *
3975 * "arg" must point to the first non-white of the expression.
3976 * "arg" is advanced to the next non-white after the recognized expression.
3977 *
3978 * Return OK or FAIL.
3979 */
3980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003981eval7(
3982 char_u **arg,
3983 typval_T *rettv,
3984 int evaluate,
3985 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003987 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int len;
3989 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 char_u *start_leader, *end_leader;
3991 int ret = OK;
3992 char_u *alias;
3993
3994 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003996 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004001 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 */
4003 start_leader = *arg;
4004 while (**arg == '!' || **arg == '-' || **arg == '+')
4005 *arg = skipwhite(*arg + 1);
4006 end_leader = *arg;
4007
4008 switch (**arg)
4009 {
4010 /*
4011 * Number constant.
4012 */
4013 case '0':
4014 case '1':
4015 case '2':
4016 case '3':
4017 case '4':
4018 case '5':
4019 case '6':
4020 case '7':
4021 case '8':
4022 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004023 {
4024#ifdef FEAT_FLOAT
4025 char_u *p = skipdigits(*arg + 1);
4026 int get_float = FALSE;
4027
4028 /* We accept a float when the format matches
4029 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004030 * strict to avoid backwards compatibility problems.
4031 * Don't look for a float after the "." operator, so that
4032 * ":let vers = 1.2.3" doesn't fail. */
4033 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004035 get_float = TRUE;
4036 p = skipdigits(p + 2);
4037 if (*p == 'e' || *p == 'E')
4038 {
4039 ++p;
4040 if (*p == '-' || *p == '+')
4041 ++p;
4042 if (!vim_isdigit(*p))
4043 get_float = FALSE;
4044 else
4045 p = skipdigits(p + 1);
4046 }
4047 if (ASCII_ISALPHA(*p) || *p == '.')
4048 get_float = FALSE;
4049 }
4050 if (get_float)
4051 {
4052 float_T f;
4053
4054 *arg += string2float(*arg, &f);
4055 if (evaluate)
4056 {
4057 rettv->v_type = VAR_FLOAT;
4058 rettv->vval.v_float = f;
4059 }
4060 }
4061 else
4062#endif
4063 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004064 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004065 *arg += len;
4066 if (evaluate)
4067 {
4068 rettv->v_type = VAR_NUMBER;
4069 rettv->vval.v_number = n;
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 /*
4076 * String constant: "string".
4077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 break;
4080
4081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004082 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004085 break;
4086
4087 /*
4088 * List: [expr, expr]
4089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 break;
4092
4093 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004094 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004095 * Dictionary: {key: val, key: val}
4096 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004097 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4098 if (ret == NOTDONE)
4099 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004100 break;
4101
4102 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004103 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004105 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 break;
4107
4108 /*
4109 * Environment variable: $VAR.
4110 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 break;
4113
4114 /*
4115 * Register contents: @r.
4116 */
4117 case '@': ++*arg;
4118 if (evaluate)
4119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004121 rettv->vval.v_string = get_reg_contents(**arg,
4122 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 if (**arg != NUL)
4125 ++*arg;
4126 break;
4127
4128 /*
4129 * nested expression: (expression).
4130 */
4131 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (**arg == ')')
4134 ++*arg;
4135 else if (ret == OK)
4136 {
4137 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 ret = FAIL;
4140 }
4141 break;
4142
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 break;
4145 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146
4147 if (ret == NOTDONE)
4148 {
4149 /*
4150 * Must be a variable or function name.
4151 * Can also be a curly-braces kind of name: {expr}.
4152 */
4153 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004154 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004155 if (alias != NULL)
4156 s = alias;
4157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004158 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004159 ret = FAIL;
4160 else
4161 {
4162 if (**arg == '(') /* recursive! */
4163 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004164 partial_T *partial;
4165
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004166 if (!evaluate)
4167 check_vars(s, len);
4168
Bram Moolenaar8c711452005-01-14 21:53:12 +00004169 /* If "s" is the name of a variable of type VAR_FUNC
4170 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004171 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004172
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004173 /* Need to make a copy, in case evaluating the arguments makes
4174 * the name invalid. */
4175 s = vim_strsave(s);
4176 if (s == NULL)
4177 ret = FAIL;
4178 else
4179 /* Invoke the function. */
4180 ret = get_func_tv(s, len, rettv, arg,
4181 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4182 &len, evaluate, partial, NULL);
4183 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004184
4185 /* If evaluate is FALSE rettv->v_type was not set in
4186 * get_func_tv, but it's needed in handle_subscript() to parse
4187 * what follows. So set it here. */
4188 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4189 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004190 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004191 rettv->v_type = VAR_FUNC;
4192 }
4193
Bram Moolenaar8c711452005-01-14 21:53:12 +00004194 /* Stop the expression evaluation when immediately
4195 * aborting on error, or when an interrupt occurred or
4196 * an exception was thrown but not caught. */
4197 if (aborting())
4198 {
4199 if (ret == OK)
4200 clear_tv(rettv);
4201 ret = FAIL;
4202 }
4203 }
4204 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004205 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004206 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004207 {
4208 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004209 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004211 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004212 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004213 }
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 *arg = skipwhite(*arg);
4216
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004217 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4218 * expr(expr). */
4219 if (ret == OK)
4220 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
4222 /*
4223 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4224 */
4225 if (ret == OK && evaluate && end_leader > start_leader)
4226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004228 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004229#ifdef FEAT_FLOAT
4230 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004232 if (rettv->v_type == VAR_FLOAT)
4233 f = rettv->vval.v_float;
4234 else
4235#endif
4236 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 clear_tv(rettv);
4240 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 else
4243 {
4244 while (end_leader > start_leader)
4245 {
4246 --end_leader;
4247 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004248 {
4249#ifdef FEAT_FLOAT
4250 if (rettv->v_type == VAR_FLOAT)
4251 f = !f;
4252 else
4253#endif
4254 val = !val;
4255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004257 {
4258#ifdef FEAT_FLOAT
4259 if (rettv->v_type == VAR_FLOAT)
4260 f = -f;
4261 else
4262#endif
4263 val = -val;
4264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004266#ifdef FEAT_FLOAT
4267 if (rettv->v_type == VAR_FLOAT)
4268 {
4269 clear_tv(rettv);
4270 rettv->vval.v_float = f;
4271 }
4272 else
4273#endif
4274 {
4275 clear_tv(rettv);
4276 rettv->v_type = VAR_NUMBER;
4277 rettv->vval.v_number = val;
4278 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281
4282 return ret;
4283}
4284
4285/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004286 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4287 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004288 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4289 */
4290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004291eval_index(
4292 char_u **arg,
4293 typval_T *rettv,
4294 int evaluate,
4295 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004296{
4297 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004299 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300 long len = -1;
4301 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004302 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004303 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004304
Bram Moolenaara03f2332016-02-06 18:09:59 +01004305 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004306 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004307 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004308 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004309 if (verbose)
4310 EMSG(_("E695: Cannot index a Funcref"));
4311 return FAIL;
4312 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004313#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004314 if (verbose)
4315 EMSG(_(e_float_as_string));
4316 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004317#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004318 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004319 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004320 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004321 if (verbose)
4322 EMSG(_("E909: Cannot index a special variable"));
4323 return FAIL;
4324 case VAR_UNKNOWN:
4325 if (evaluate)
4326 return FAIL;
4327 /* FALLTHROUGH */
4328
4329 case VAR_STRING:
4330 case VAR_NUMBER:
4331 case VAR_LIST:
4332 case VAR_DICT:
4333 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004334 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004335
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004336 init_tv(&var1);
4337 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004340 /*
4341 * dict.name
4342 */
4343 key = *arg + 1;
4344 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4345 ;
4346 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004347 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004348 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004349 }
4350 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004351 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 /*
4353 * something[idx]
4354 *
4355 * Get the (first) variable from inside the [].
4356 */
4357 *arg = skipwhite(*arg + 1);
4358 if (**arg == ':')
4359 empty1 = TRUE;
4360 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4361 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4363 {
4364 /* not a number or string */
4365 clear_tv(&var1);
4366 return FAIL;
4367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368
4369 /*
4370 * Get the second variable from inside the [:].
4371 */
4372 if (**arg == ':')
4373 {
4374 range = TRUE;
4375 *arg = skipwhite(*arg + 1);
4376 if (**arg == ']')
4377 empty2 = TRUE;
4378 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 if (!empty1)
4381 clear_tv(&var1);
4382 return FAIL;
4383 }
4384 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4385 {
4386 /* not a number or string */
4387 if (!empty1)
4388 clear_tv(&var1);
4389 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 return FAIL;
4391 }
4392 }
4393
4394 /* Check for the ']'. */
4395 if (**arg != ']')
4396 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004397 if (verbose)
4398 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004399 clear_tv(&var1);
4400 if (range)
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
4404 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004405 }
4406
4407 if (evaluate)
4408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004409 n1 = 0;
4410 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 n1 = get_tv_number(&var1);
4413 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414 }
4415 if (range)
4416 {
4417 if (empty2)
4418 n2 = -1;
4419 else
4420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004421 n2 = get_tv_number(&var2);
4422 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 }
4424 }
4425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004428 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004429 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004430 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004431 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004432 case VAR_SPECIAL:
4433 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004434 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004435 break; /* not evaluating, skipping over subscript */
4436
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 case VAR_NUMBER:
4438 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 len = (long)STRLEN(s);
4441 if (range)
4442 {
4443 /* The resulting variable is a substring. If the indexes
4444 * are out of range the result is empty. */
4445 if (n1 < 0)
4446 {
4447 n1 = len + n1;
4448 if (n1 < 0)
4449 n1 = 0;
4450 }
4451 if (n2 < 0)
4452 n2 = len + n2;
4453 else if (n2 >= len)
4454 n2 = len;
4455 if (n1 >= len || n2 < 0 || n1 > n2)
4456 s = NULL;
4457 else
4458 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4459 }
4460 else
4461 {
4462 /* The resulting variable is a string of a single
4463 * character. If the index is too big or negative the
4464 * result is empty. */
4465 if (n1 >= len || n1 < 0)
4466 s = NULL;
4467 else
4468 s = vim_strnsave(s + n1, 1);
4469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 clear_tv(rettv);
4471 rettv->v_type = VAR_STRING;
4472 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 break;
4474
4475 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 if (n1 < 0)
4478 n1 = len + n1;
4479 if (!empty1 && (n1 < 0 || n1 >= len))
4480 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004481 /* For a range we allow invalid values and return an empty
4482 * list. A list index out of range is an error. */
4483 if (!range)
4484 {
4485 if (verbose)
4486 EMSGN(_(e_listidx), n1);
4487 return FAIL;
4488 }
4489 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004490 }
4491 if (range)
4492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004493 list_T *l;
4494 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495
4496 if (n2 < 0)
4497 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004498 else if (n2 >= len)
4499 n2 = len - 1;
4500 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004501 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 l = list_alloc();
4503 if (l == NULL)
4504 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 n1 <= n2; ++n1)
4507 {
4508 if (list_append_tv(l, &item->li_tv) == FAIL)
4509 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004510 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 return FAIL;
4512 }
4513 item = item->li_next;
4514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004516 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 }
4518 else
4519 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004520 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 clear_tv(rettv);
4522 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004523 }
4524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525
4526 case VAR_DICT:
4527 if (range)
4528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004529 if (verbose)
4530 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531 if (len == -1)
4532 clear_tv(&var1);
4533 return FAIL;
4534 }
4535 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004536 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537
4538 if (len == -1)
4539 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004540 key = get_tv_string_chk(&var1);
4541 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 clear_tv(&var1);
4544 return FAIL;
4545 }
4546 }
4547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004548 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004550 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004551 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 if (len == -1)
4553 clear_tv(&var1);
4554 if (item == NULL)
4555 return FAIL;
4556
4557 copy_tv(&item->di_tv, &var1);
4558 clear_tv(rettv);
4559 *rettv = var1;
4560 }
4561 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 }
4563 }
4564
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004565 return OK;
4566}
4567
4568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 * Get an option value.
4570 * "arg" points to the '&' or '+' before the option name.
4571 * "arg" is advanced to character after the option name.
4572 * Return OK or FAIL.
4573 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004575get_option_tv(
4576 char_u **arg,
4577 typval_T *rettv, /* when NULL, only check if option exists */
4578 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579{
4580 char_u *option_end;
4581 long numval;
4582 char_u *stringval;
4583 int opt_type;
4584 int c;
4585 int working = (**arg == '+'); /* has("+option") */
4586 int ret = OK;
4587 int opt_flags;
4588
4589 /*
4590 * Isolate the option name and find its value.
4591 */
4592 option_end = find_option_end(arg, &opt_flags);
4593 if (option_end == NULL)
4594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 EMSG2(_("E112: Option name missing: %s"), *arg);
4597 return FAIL;
4598 }
4599
4600 if (!evaluate)
4601 {
4602 *arg = option_end;
4603 return OK;
4604 }
4605
4606 c = *option_end;
4607 *option_end = NUL;
4608 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 if (opt_type == -3) /* invalid name */
4612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 EMSG2(_("E113: Unknown option: %s"), *arg);
4615 ret = FAIL;
4616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619 if (opt_type == -2) /* hidden string option */
4620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621 rettv->v_type = VAR_STRING;
4622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 else if (opt_type == -1) /* hidden number option */
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 rettv->v_type = VAR_NUMBER;
4627 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 else if (opt_type == 1) /* number option */
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 rettv->v_type = VAR_NUMBER;
4632 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634 else /* string option */
4635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 rettv->v_type = VAR_STRING;
4637 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 }
4640 else if (working && (opt_type == -2 || opt_type == -1))
4641 ret = FAIL;
4642
4643 *option_end = c; /* put back for error messages */
4644 *arg = option_end;
4645
4646 return ret;
4647}
4648
4649/*
4650 * Allocate a variable for a string constant.
4651 * Return OK or FAIL.
4652 */
4653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004654get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 char_u *p;
4657 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 int extra = 0;
4659
4660 /*
4661 * Find the end of the string, skipping backslashed characters.
4662 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004663 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
4665 if (*p == '\\' && p[1] != NUL)
4666 {
4667 ++p;
4668 /* A "\<x>" form occupies at least 4 characters, and produces up
4669 * to 6 characters: reserve space for 2 extra */
4670 if (*p == '<')
4671 extra += 2;
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674
4675 if (*p != '"')
4676 {
4677 EMSG2(_("E114: Missing quote: %s"), *arg);
4678 return FAIL;
4679 }
4680
4681 /* If only parsing, set *arg and return here */
4682 if (!evaluate)
4683 {
4684 *arg = p + 1;
4685 return OK;
4686 }
4687
4688 /*
4689 * Copy the string into allocated memory, handling backslashed
4690 * characters.
4691 */
4692 name = alloc((unsigned)(p - *arg + extra));
4693 if (name == NULL)
4694 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695 rettv->v_type = VAR_STRING;
4696 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 if (*p == '\\')
4701 {
4702 switch (*++p)
4703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 case 'b': *name++ = BS; ++p; break;
4705 case 'e': *name++ = ESC; ++p; break;
4706 case 'f': *name++ = FF; ++p; break;
4707 case 'n': *name++ = NL; ++p; break;
4708 case 'r': *name++ = CAR; ++p; break;
4709 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 case 'X': /* hex: "\x1", "\x12" */
4712 case 'x':
4713 case 'u': /* Unicode: "\u0023" */
4714 case 'U':
4715 if (vim_isxdigit(p[1]))
4716 {
4717 int n, nr;
4718 int c = toupper(*p);
4719
4720 if (c == 'X')
4721 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004722 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004724 else
4725 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 nr = 0;
4727 while (--n >= 0 && vim_isxdigit(p[1]))
4728 {
4729 ++p;
4730 nr = (nr << 4) + hex2nr(*p);
4731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#ifdef FEAT_MBYTE
4734 /* For "\u" store the number according to
4735 * 'encoding'. */
4736 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 else
4739#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 break;
4743
4744 /* octal: "\1", "\12", "\123" */
4745 case '0':
4746 case '1':
4747 case '2':
4748 case '3':
4749 case '4':
4750 case '5':
4751 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 case '7': *name = *p++ - '0';
4753 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 *name = (*name << 3) + *p++ - '0';
4756 if (*p >= '0' && *p <= '7')
4757 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 break;
4761
4762 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004763 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (extra != 0)
4765 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 break;
4768 }
4769 /* FALLTHROUGH */
4770
Bram Moolenaar8c711452005-01-14 21:53:12 +00004771 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 break;
4773 }
4774 }
4775 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004780 if (*p != NUL) /* just in case */
4781 ++p;
4782 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 return OK;
4785}
4786
4787/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 * Return OK or FAIL.
4790 */
4791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004792get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004795 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004796 int reduce = 0;
4797
4798 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004800 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004801 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004806 break;
4807 ++reduce;
4808 ++p;
4809 }
4810 }
4811
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004815 return FAIL;
4816 }
4817
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004819 if (!evaluate)
4820 {
4821 *arg = p + 1;
4822 return OK;
4823 }
4824
4825 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004827 */
4828 str = alloc((unsigned)((p - *arg) - reduce));
4829 if (str == NULL)
4830 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831 rettv->v_type = VAR_STRING;
4832 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004833
Bram Moolenaar8c711452005-01-14 21:53:12 +00004834 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004836 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004839 break;
4840 ++p;
4841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004843 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004844 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004845 *arg = p + 1;
4846
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004847 return OK;
4848}
4849
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004850/*
4851 * Return the function name of the partial.
4852 */
4853 char_u *
4854partial_name(partial_T *pt)
4855{
4856 if (pt->pt_name != NULL)
4857 return pt->pt_name;
4858 return pt->pt_func->uf_name;
4859}
4860
Bram Moolenaarddecc252016-04-06 22:59:37 +02004861 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004862partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004863{
4864 int i;
4865
4866 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004868 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004869 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004870 if (pt->pt_name != NULL)
4871 {
4872 func_unref(pt->pt_name);
4873 vim_free(pt->pt_name);
4874 }
4875 else
4876 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004877 vim_free(pt);
4878}
4879
4880/*
4881 * Unreference a closure: decrement the reference count and free it when it
4882 * becomes zero.
4883 */
4884 void
4885partial_unref(partial_T *pt)
4886{
4887 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004888 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004889}
4890
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004891static int tv_equal_recurse_limit;
4892
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004893 static int
4894func_equal(
4895 typval_T *tv1,
4896 typval_T *tv2,
4897 int ic) /* ignore case */
4898{
4899 char_u *s1, *s2;
4900 dict_T *d1, *d2;
4901 int a1, a2;
4902 int i;
4903
4904 /* empty and NULL function name considered the same */
4905 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004906 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004907 if (s1 != NULL && *s1 == NUL)
4908 s1 = NULL;
4909 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004910 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004911 if (s2 != NULL && *s2 == NUL)
4912 s2 = NULL;
4913 if (s1 == NULL || s2 == NULL)
4914 {
4915 if (s1 != s2)
4916 return FALSE;
4917 }
4918 else if (STRCMP(s1, s2) != 0)
4919 return FALSE;
4920
4921 /* empty dict and NULL dict is different */
4922 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4923 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4924 if (d1 == NULL || d2 == NULL)
4925 {
4926 if (d1 != d2)
4927 return FALSE;
4928 }
4929 else if (!dict_equal(d1, d2, ic, TRUE))
4930 return FALSE;
4931
4932 /* empty list and no list considered the same */
4933 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4934 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4935 if (a1 != a2)
4936 return FALSE;
4937 for (i = 0; i < a1; ++i)
4938 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4939 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4940 return FALSE;
4941
4942 return TRUE;
4943}
4944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004945/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004946 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004947 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004949 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004951tv_equal(
4952 typval_T *tv1,
4953 typval_T *tv2,
4954 int ic, /* ignore case */
4955 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004956{
4957 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004958 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004959 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004960 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004961
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004962 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004963 * recursiveness to a limit. We guess they are equal then.
4964 * A fixed limit has the problem of still taking an awful long time.
4965 * Reduce the limit every time running into it. That should work fine for
4966 * deeply linked structures that are not recursively linked and catch
4967 * recursiveness quickly. */
4968 if (!recursive)
4969 tv_equal_recurse_limit = 1000;
4970 if (recursive_cnt >= tv_equal_recurse_limit)
4971 {
4972 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004973 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004974 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004975
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004976 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4977 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004978 if ((tv1->v_type == VAR_FUNC
4979 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4980 && (tv2->v_type == VAR_FUNC
4981 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4982 {
4983 ++recursive_cnt;
4984 r = func_equal(tv1, tv2, ic);
4985 --recursive_cnt;
4986 return r;
4987 }
4988
4989 if (tv1->v_type != tv2->v_type)
4990 return FALSE;
4991
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004992 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004993 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004994 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004995 ++recursive_cnt;
4996 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4997 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004998 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004999
5000 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005001 ++recursive_cnt;
5002 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5003 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005004 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005005
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005006 case VAR_NUMBER:
5007 return tv1->vval.v_number == tv2->vval.v_number;
5008
5009 case VAR_STRING:
5010 s1 = get_tv_string_buf(tv1, buf1);
5011 s2 = get_tv_string_buf(tv2, buf2);
5012 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005013
5014 case VAR_SPECIAL:
5015 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005016
5017 case VAR_FLOAT:
5018#ifdef FEAT_FLOAT
5019 return tv1->vval.v_float == tv2->vval.v_float;
5020#endif
5021 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005023 return tv1->vval.v_job == tv2->vval.v_job;
5024#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005025 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005026#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005027 return tv1->vval.v_channel == tv2->vval.v_channel;
5028#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005029 case VAR_FUNC:
5030 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005031 case VAR_UNKNOWN:
5032 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005033 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005034
Bram Moolenaara03f2332016-02-06 18:09:59 +01005035 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5036 * does not equal anything, not even itself. */
5037 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005038}
5039
5040/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005041 * Return the next (unique) copy ID.
5042 * Used for serializing nested structures.
5043 */
5044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005045get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005046{
5047 current_copyID += COPYID_INC;
5048 return current_copyID;
5049}
5050
5051/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005052 * Garbage collection for lists and dictionaries.
5053 *
5054 * We use reference counts to be able to free most items right away when they
5055 * are no longer used. But for composite items it's possible that it becomes
5056 * unused while the reference count is > 0: When there is a recursive
5057 * reference. Example:
5058 * :let l = [1, 2, 3]
5059 * :let d = {9: l}
5060 * :let l[1] = d
5061 *
5062 * Since this is quite unusual we handle this with garbage collection: every
5063 * once in a while find out which lists and dicts are not referenced from any
5064 * variable.
5065 *
5066 * Here is a good reference text about garbage collection (refers to Python
5067 * but it applies to all reference-counting mechanisms):
5068 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005069 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005070
5071/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005072 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005073 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005074 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005075 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005076 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005077garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005078{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005079 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005080 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005081 buf_T *buf;
5082 win_T *wp;
5083 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005084 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005085 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005086
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005087 if (!testing)
5088 {
5089 /* Only do this once. */
5090 want_garbage_collect = FALSE;
5091 may_garbage_collect = FALSE;
5092 garbage_collect_at_exit = FALSE;
5093 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005094
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005095 /* We advance by two because we add one for items referenced through
5096 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005097 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005098
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005099 /*
5100 * 1. Go through all accessible variables and mark all lists and dicts
5101 * with copyID.
5102 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005103
5104 /* Don't free variables in the previous_funccal list unless they are only
5105 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005106 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005108
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109 /* script-local variables */
5110 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005111 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005112
5113 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005114 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005115 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5116 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005117
5118 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005119 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005120 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5121 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005122#ifdef FEAT_AUTOCMD
5123 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005124 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5125 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005126#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005127
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005128 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005129 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005130 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5131 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005132 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005133 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005134
5135 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005136 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005137
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005138 /* named functions (matters for closures) */
5139 abort = abort || set_ref_in_functions(copyID);
5140
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005141 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005142 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005143
Bram Moolenaard812df62008-11-09 12:46:09 +00005144 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005145 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005146
Bram Moolenaar1dced572012-04-05 16:54:08 +02005147#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005148 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005149#endif
5150
Bram Moolenaardb913952012-06-29 12:54:53 +02005151#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005153#endif
5154
5155#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005156 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005157#endif
5158
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005159#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005160 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005161 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005162#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005163#ifdef FEAT_NETBEANS_INTG
5164 abort = abort || set_ref_in_nb_channel(copyID);
5165#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005166
Bram Moolenaare3188e22016-05-31 21:13:04 +02005167#ifdef FEAT_TIMERS
5168 abort = abort || set_ref_in_timer(copyID);
5169#endif
5170
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005171#ifdef FEAT_QUICKFIX
5172 abort = abort || set_ref_in_quickfix(copyID);
5173#endif
5174
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005175#ifdef FEAT_TERMINAL
5176 abort = abort || set_ref_in_term(copyID);
5177#endif
5178
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005179 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005180 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005181 /*
5182 * 2. Free lists and dictionaries that are not referenced.
5183 */
5184 did_free = free_unref_items(copyID);
5185
5186 /*
5187 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005189 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005190 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005191 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005192 else if (p_verbose > 0)
5193 {
5194 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5195 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005196
5197 return did_free;
5198}
5199
5200/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005201 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005202 */
5203 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005204free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005205{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005206 int did_free = FALSE;
5207
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 /* Let all "free" functions know that we are here. This means no
5209 * dictionaries, lists, channels or jobs are to be freed, because we will
5210 * do that here. */
5211 in_free_unref_items = TRUE;
5212
5213 /*
5214 * PASS 1: free the contents of the items. We don't free the items
5215 * themselves yet, so that it is possible to decrement refcount counters
5216 */
5217
Bram Moolenaarcd524592016-07-17 14:57:05 +02005218 /* Go through the list of dicts and free items without the copyID. */
5219 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220
Bram Moolenaarda861d62016-07-17 15:46:27 +02005221 /* Go through the list of lists and free items without the copyID. */
5222 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005223
5224#ifdef FEAT_JOB_CHANNEL
5225 /* Go through the list of jobs and free items without the copyID. This
5226 * must happen before doing channels, because jobs refer to channels, but
5227 * the reference from the channel to the job isn't tracked. */
5228 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5229
5230 /* Go through the list of channels and free items without the copyID. */
5231 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5232#endif
5233
5234 /*
5235 * PASS 2: free the items themselves.
5236 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005237 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005238 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005239
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005240#ifdef FEAT_JOB_CHANNEL
5241 /* Go through the list of jobs and free items without the copyID. This
5242 * must happen before doing channels, because jobs refer to channels, but
5243 * the reference from the channel to the job isn't tracked. */
5244 free_unused_jobs(copyID, COPYID_MASK);
5245
5246 /* Go through the list of channels and free items without the copyID. */
5247 free_unused_channels(copyID, COPYID_MASK);
5248#endif
5249
5250 in_free_unref_items = FALSE;
5251
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252 return did_free;
5253}
5254
5255/*
5256 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 * "list_stack" is used to add lists to be marked. Can be NULL.
5258 *
5259 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005262set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005263{
5264 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005266 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005267 hashtab_T *cur_ht;
5268 ht_stack_T *ht_stack = NULL;
5269 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005270
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005271 cur_ht = ht;
5272 for (;;)
5273 {
5274 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005276 /* Mark each item in the hashtab. If the item contains a hashtab
5277 * it is added to ht_stack, if it contains a list it is added to
5278 * list_stack. */
5279 todo = (int)cur_ht->ht_used;
5280 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5281 if (!HASHITEM_EMPTY(hi))
5282 {
5283 --todo;
5284 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5285 &ht_stack, list_stack);
5286 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005287 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005288
5289 if (ht_stack == NULL)
5290 break;
5291
5292 /* take an item from the stack */
5293 cur_ht = ht_stack->ht;
5294 tempitem = ht_stack;
5295 ht_stack = ht_stack->prev;
5296 free(tempitem);
5297 }
5298
5299 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005300}
5301
5302/*
5303 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5305 *
5306 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005307 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005309set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005310{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005311 listitem_T *li;
5312 int abort = FALSE;
5313 list_T *cur_l;
5314 list_stack_T *list_stack = NULL;
5315 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005316
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005317 cur_l = l;
5318 for (;;)
5319 {
5320 if (!abort)
5321 /* Mark each item in the list. If the item contains a hashtab
5322 * it is added to ht_stack, if it contains a list it is added to
5323 * list_stack. */
5324 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5325 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5326 ht_stack, &list_stack);
5327 if (list_stack == NULL)
5328 break;
5329
5330 /* take an item from the stack */
5331 cur_l = list_stack->list;
5332 tempitem = list_stack;
5333 list_stack = list_stack->prev;
5334 free(tempitem);
5335 }
5336
5337 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005338}
5339
5340/*
5341 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005342 * "list_stack" is used to add lists to be marked. Can be NULL.
5343 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5344 *
5345 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005346 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005348set_ref_in_item(
5349 typval_T *tv,
5350 int copyID,
5351 ht_stack_T **ht_stack,
5352 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005353{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005354 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005355
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005356 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005357 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005358 dict_T *dd = tv->vval.v_dict;
5359
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 if (dd != NULL && dd->dv_copyID != copyID)
5361 {
5362 /* Didn't see this dict yet. */
5363 dd->dv_copyID = copyID;
5364 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005365 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5367 }
5368 else
5369 {
5370 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5371 if (newitem == NULL)
5372 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005373 else
5374 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 newitem->ht = &dd->dv_hashtab;
5376 newitem->prev = *ht_stack;
5377 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005378 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005379 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005380 }
5381 }
5382 else if (tv->v_type == VAR_LIST)
5383 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 list_T *ll = tv->vval.v_list;
5385
Bram Moolenaara03f2332016-02-06 18:09:59 +01005386 if (ll != NULL && ll->lv_copyID != copyID)
5387 {
5388 /* Didn't see this list yet. */
5389 ll->lv_copyID = copyID;
5390 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005391 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005392 abort = set_ref_in_list(ll, copyID, ht_stack);
5393 }
5394 else
5395 {
5396 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005397 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 if (newitem == NULL)
5399 abort = TRUE;
5400 else
5401 {
5402 newitem->list = ll;
5403 newitem->prev = *list_stack;
5404 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005405 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005406 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005408 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005409 else if (tv->v_type == VAR_FUNC)
5410 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005411 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005412 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005413 else if (tv->v_type == VAR_PARTIAL)
5414 {
5415 partial_T *pt = tv->vval.v_partial;
5416 int i;
5417
5418 /* A partial does not have a copyID, because it cannot contain itself.
5419 */
5420 if (pt != NULL)
5421 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005422 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005423
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005424 if (pt->pt_dict != NULL)
5425 {
5426 typval_T dtv;
5427
5428 dtv.v_type = VAR_DICT;
5429 dtv.vval.v_dict = pt->pt_dict;
5430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5431 }
5432
5433 for (i = 0; i < pt->pt_argc; ++i)
5434 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5435 ht_stack, list_stack);
5436 }
5437 }
5438#ifdef FEAT_JOB_CHANNEL
5439 else if (tv->v_type == VAR_JOB)
5440 {
5441 job_T *job = tv->vval.v_job;
5442 typval_T dtv;
5443
5444 if (job != NULL && job->jv_copyID != copyID)
5445 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005446 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005447 if (job->jv_channel != NULL)
5448 {
5449 dtv.v_type = VAR_CHANNEL;
5450 dtv.vval.v_channel = job->jv_channel;
5451 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5452 }
5453 if (job->jv_exit_partial != NULL)
5454 {
5455 dtv.v_type = VAR_PARTIAL;
5456 dtv.vval.v_partial = job->jv_exit_partial;
5457 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5458 }
5459 }
5460 }
5461 else if (tv->v_type == VAR_CHANNEL)
5462 {
5463 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005464 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005465 typval_T dtv;
5466 jsonq_T *jq;
5467 cbq_T *cq;
5468
5469 if (ch != NULL && ch->ch_copyID != copyID)
5470 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005471 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005472 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005473 {
5474 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5475 jq = jq->jq_next)
5476 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5477 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5478 cq = cq->cq_next)
5479 if (cq->cq_partial != NULL)
5480 {
5481 dtv.v_type = VAR_PARTIAL;
5482 dtv.vval.v_partial = cq->cq_partial;
5483 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5484 }
5485 if (ch->ch_part[part].ch_partial != NULL)
5486 {
5487 dtv.v_type = VAR_PARTIAL;
5488 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5489 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5490 }
5491 }
5492 if (ch->ch_partial != NULL)
5493 {
5494 dtv.v_type = VAR_PARTIAL;
5495 dtv.vval.v_partial = ch->ch_partial;
5496 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5497 }
5498 if (ch->ch_close_partial != NULL)
5499 {
5500 dtv.v_type = VAR_PARTIAL;
5501 dtv.vval.v_partial = ch->ch_close_partial;
5502 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5503 }
5504 }
5505 }
5506#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005507 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005508}
5509
Bram Moolenaar17a13432016-01-24 14:22:10 +01005510 static char *
5511get_var_special_name(int nr)
5512{
5513 switch (nr)
5514 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005515 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005516 case VVAL_TRUE: return "v:true";
5517 case VVAL_NONE: return "v:none";
5518 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005519 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005520 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005521 return "42";
5522}
5523
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 * Return a string with the string representation of a variable.
5526 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005527 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005528 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005529 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5530 * stings as "string()", otherwise does not put quotes around strings, as
5531 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005532 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5533 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005534 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005536 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005537echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005538 typval_T *tv,
5539 char_u **tofree,
5540 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005541 int copyID,
5542 int echo_style,
5543 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005544 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005546 static int recurse = 0;
5547 char_u *r = NULL;
5548
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005550 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005551 if (!did_echo_string_emsg)
5552 {
5553 /* Only give this message once for a recursive call to avoid
5554 * flooding the user with errors. And stop iterating over lists
5555 * and dicts. */
5556 did_echo_string_emsg = TRUE;
5557 EMSG(_("E724: variable nested too deep for displaying"));
5558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005559 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005560 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005561 }
5562 ++recurse;
5563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 switch (tv->v_type)
5565 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005566 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005567 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005568 {
5569 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005570 r = tv->vval.v_string;
5571 if (r == NULL)
5572 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005573 }
5574 else
5575 {
5576 *tofree = string_quote(tv->vval.v_string, FALSE);
5577 r = *tofree;
5578 }
5579 break;
5580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005582 if (echo_style)
5583 {
5584 *tofree = NULL;
5585 r = tv->vval.v_string;
5586 }
5587 else
5588 {
5589 *tofree = string_quote(tv->vval.v_string, TRUE);
5590 r = *tofree;
5591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005592 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005593
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005594 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005595 {
5596 partial_T *pt = tv->vval.v_partial;
5597 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005598 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005599 garray_T ga;
5600 int i;
5601 char_u *tf;
5602
5603 ga_init2(&ga, 1, 100);
5604 ga_concat(&ga, (char_u *)"function(");
5605 if (fname != NULL)
5606 {
5607 ga_concat(&ga, fname);
5608 vim_free(fname);
5609 }
5610 if (pt != NULL && pt->pt_argc > 0)
5611 {
5612 ga_concat(&ga, (char_u *)", [");
5613 for (i = 0; i < pt->pt_argc; ++i)
5614 {
5615 if (i > 0)
5616 ga_concat(&ga, (char_u *)", ");
5617 ga_concat(&ga,
5618 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5619 vim_free(tf);
5620 }
5621 ga_concat(&ga, (char_u *)"]");
5622 }
5623 if (pt != NULL && pt->pt_dict != NULL)
5624 {
5625 typval_T dtv;
5626
5627 ga_concat(&ga, (char_u *)", ");
5628 dtv.v_type = VAR_DICT;
5629 dtv.vval.v_dict = pt->pt_dict;
5630 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5631 vim_free(tf);
5632 }
5633 ga_concat(&ga, (char_u *)")");
5634
5635 *tofree = ga.ga_data;
5636 r = *tofree;
5637 break;
5638 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005639
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005641 if (tv->vval.v_list == NULL)
5642 {
5643 *tofree = NULL;
5644 r = NULL;
5645 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005646 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5647 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005648 {
5649 *tofree = NULL;
5650 r = (char_u *)"[...]";
5651 }
5652 else
5653 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005654 int old_copyID = tv->vval.v_list->lv_copyID;
5655
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005656 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005657 *tofree = list2string(tv, copyID, restore_copyID);
5658 if (restore_copyID)
5659 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005660 r = *tofree;
5661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005663
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005665 if (tv->vval.v_dict == NULL)
5666 {
5667 *tofree = NULL;
5668 r = NULL;
5669 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005670 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5671 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005672 {
5673 *tofree = NULL;
5674 r = (char_u *)"{...}";
5675 }
5676 else
5677 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005678 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005679 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005680 *tofree = dict2string(tv, copyID, restore_copyID);
5681 if (restore_copyID)
5682 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005683 r = *tofree;
5684 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005685 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005688 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005689 *tofree = NULL;
5690 r = get_tv_string_buf(tv, numbuf);
5691 break;
5692
Bram Moolenaar835dc632016-02-07 14:27:38 +01005693 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005694 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005695 *tofree = NULL;
5696 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005697 if (composite_val)
5698 {
5699 *tofree = string_quote(r, FALSE);
5700 r = *tofree;
5701 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005703
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005704 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005705#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005706 *tofree = NULL;
5707 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5708 r = numbuf;
5709 break;
5710#endif
5711
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005712 case VAR_SPECIAL:
5713 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005714 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005715 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005717
Bram Moolenaar8502c702014-06-17 12:51:16 +02005718 if (--recurse == 0)
5719 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005720 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721}
5722
5723/*
5724 * Return a string with the string representation of a variable.
5725 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5726 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005727 * Does not put quotes around strings, as ":echo" displays values.
5728 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5729 * May return NULL.
5730 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005731 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005732echo_string(
5733 typval_T *tv,
5734 char_u **tofree,
5735 char_u *numbuf,
5736 int copyID)
5737{
5738 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5739}
5740
5741/*
5742 * Return a string with the string representation of a variable.
5743 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5744 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005746 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005748 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749tv2string(
5750 typval_T *tv,
5751 char_u **tofree,
5752 char_u *numbuf,
5753 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005755 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756}
5757
5758/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005759 * Return string "str" in ' quotes, doubling ' characters.
5760 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765{
Bram Moolenaar33570922005-01-25 22:26:29 +00005766 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 char_u *p, *r, *s;
5768
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 len = (function ? 13 : 3);
5770 if (str != NULL)
5771 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005772 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005773 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 if (*p == '\'')
5775 ++len;
5776 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 s = r = alloc(len);
5778 if (r != NULL)
5779 {
5780 if (function)
5781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 r += 10;
5784 }
5785 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 if (str != NULL)
5788 for (p = str; *p != NUL; )
5789 {
5790 if (*p == '\'')
5791 *r++ = '\'';
5792 MB_COPY_CHAR(p, r);
5793 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 if (function)
5796 *r++ = ')';
5797 *r++ = NUL;
5798 }
5799 return s;
5800}
5801
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005802#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005803/*
5804 * Convert the string "text" to a floating point number.
5805 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5806 * this always uses a decimal point.
5807 * Returns the length of the text that was consumed.
5808 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810string2float(
5811 char_u *text,
5812 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005813{
5814 char *s = (char *)text;
5815 float_T f;
5816
Bram Moolenaar62473612017-01-08 19:25:40 +01005817 /* MS-Windows does not deal with "inf" and "nan" properly. */
5818 if (STRNICMP(text, "inf", 3) == 0)
5819 {
5820 *value = INFINITY;
5821 return 3;
5822 }
5823 if (STRNICMP(text, "-inf", 3) == 0)
5824 {
5825 *value = -INFINITY;
5826 return 4;
5827 }
5828 if (STRNICMP(text, "nan", 3) == 0)
5829 {
5830 *value = NAN;
5831 return 3;
5832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005833 f = strtod(s, &s);
5834 *value = f;
5835 return (int)((char_u *)s - text);
5836}
5837#endif
5838
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 * Get the value of an environment variable.
5841 * "arg" is pointing to the '$'. It is advanced to after the name.
5842 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005843 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 */
5845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005846get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 char_u *string = NULL;
5849 int len;
5850 int cc;
5851 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005852 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854 ++*arg;
5855 name = *arg;
5856 len = get_env_len(arg);
5857 if (evaluate)
5858 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005859 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01005860 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005861
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005862 cc = name[len];
5863 name[len] = NUL;
5864 /* first try vim_getenv(), fast for normal environment vars */
5865 string = vim_getenv(name, &mustfree);
5866 if (string != NULL && *string != NUL)
5867 {
5868 if (!mustfree)
5869 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005871 else
5872 {
5873 if (mustfree)
5874 vim_free(string);
5875
5876 /* next try expanding things like $VIM and ${HOME} */
5877 string = expand_env_save(name - 1);
5878 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005879 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005880 }
5881 name[len] = cc;
5882
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005883 rettv->v_type = VAR_STRING;
5884 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
5886
5887 return OK;
5888}
5889
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005890
5891
5892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005894 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005897var2fpos(
5898 typval_T *varp,
5899 int dollar_lnum, /* TRUE when $ is last line */
5900 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005902 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005904 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaara5525202006-03-02 22:52:09 +00005906 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005907 if (varp->v_type == VAR_LIST)
5908 {
5909 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005910 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005911 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005912 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005913
5914 l = varp->vval.v_list;
5915 if (l == NULL)
5916 return NULL;
5917
5918 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005919 pos.lnum = list_find_nr(l, 0L, &error);
5920 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005921 return NULL; /* invalid line number */
5922
5923 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005924 pos.col = list_find_nr(l, 1L, &error);
5925 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005926 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005927 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005928
5929 /* We accept "$" for the column number: last column. */
5930 li = list_find(l, 1L);
5931 if (li != NULL && li->li_tv.v_type == VAR_STRING
5932 && li->li_tv.vval.v_string != NULL
5933 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5934 pos.col = len + 1;
5935
Bram Moolenaara5525202006-03-02 22:52:09 +00005936 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005937 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005938 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005939 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005940
Bram Moolenaara5525202006-03-02 22:52:09 +00005941#ifdef FEAT_VIRTUALEDIT
5942 /* Get the virtual offset. Defaults to zero. */
5943 pos.coladd = list_find_nr(l, 2L, &error);
5944 if (error)
5945 pos.coladd = 0;
5946#endif
5947
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005948 return &pos;
5949 }
5950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005951 name = get_tv_string_chk(varp);
5952 if (name == NULL)
5953 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005954 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005956 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5957 {
5958 if (VIsual_active)
5959 return &VIsual;
5960 return &curwin->w_cursor;
5961 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005962 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005964 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5966 return NULL;
5967 return pp;
5968 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005969
5970#ifdef FEAT_VIRTUALEDIT
5971 pos.coladd = 0;
5972#endif
5973
Bram Moolenaar477933c2007-07-17 14:32:23 +00005974 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005975 {
5976 pos.col = 0;
5977 if (name[1] == '0') /* "w0": first visible line */
5978 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005979 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005980 /* In silent Ex mode topline is zero, but that's not a valid line
5981 * number; use one instead. */
5982 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005983 return &pos;
5984 }
5985 else if (name[1] == '$') /* "w$": last visible line */
5986 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005988 /* In silent Ex mode botline is zero, return zero then. */
5989 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005990 return &pos;
5991 }
5992 }
5993 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005995 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 {
5997 pos.lnum = curbuf->b_ml.ml_line_count;
5998 pos.col = 0;
5999 }
6000 else
6001 {
6002 pos.lnum = curwin->w_cursor.lnum;
6003 pos.col = (colnr_T)STRLEN(ml_get_curline());
6004 }
6005 return &pos;
6006 }
6007 return NULL;
6008}
6009
6010/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006011 * Convert list in "arg" into a position and optional file number.
6012 * When "fnump" is NULL there is no file number, only 3 items.
6013 * Note that the column is passed on as-is, the caller may want to decrement
6014 * it to use 1 for the first column.
6015 * Return FAIL when conversion is not possible, doesn't check the position for
6016 * validity.
6017 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006019list2fpos(
6020 typval_T *arg,
6021 pos_T *posp,
6022 int *fnump,
6023 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006024{
6025 list_T *l = arg->vval.v_list;
6026 long i = 0;
6027 long n;
6028
Bram Moolenaar493c1782014-05-28 14:34:46 +02006029 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6030 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006031 if (arg->v_type != VAR_LIST
6032 || l == NULL
6033 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006034 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006035 return FAIL;
6036
6037 if (fnump != NULL)
6038 {
6039 n = list_find_nr(l, i++, NULL); /* fnum */
6040 if (n < 0)
6041 return FAIL;
6042 if (n == 0)
6043 n = curbuf->b_fnum; /* current buffer */
6044 *fnump = n;
6045 }
6046
6047 n = list_find_nr(l, i++, NULL); /* lnum */
6048 if (n < 0)
6049 return FAIL;
6050 posp->lnum = n;
6051
6052 n = list_find_nr(l, i++, NULL); /* col */
6053 if (n < 0)
6054 return FAIL;
6055 posp->col = n;
6056
6057#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006058 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006059 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006060 posp->coladd = 0;
6061 else
6062 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006063#endif
6064
Bram Moolenaar493c1782014-05-28 14:34:46 +02006065 if (curswantp != NULL)
6066 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6067
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006068 return OK;
6069}
6070
6071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 * Get the length of an environment variable name.
6073 * Advance "arg" to the first character after the name.
6074 * Return 0 for error.
6075 */
6076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 char_u *p;
6080 int len;
6081
6082 for (p = *arg; vim_isIDc(*p); ++p)
6083 ;
6084 if (p == *arg) /* no name found */
6085 return 0;
6086
6087 len = (int)(p - *arg);
6088 *arg = p;
6089 return len;
6090}
6091
6092/*
6093 * Get the length of the name of a function or internal variable.
6094 * "arg" is advanced to the first non-white character after the name.
6095 * Return 0 if something is wrong.
6096 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 char_u *p;
6101 int len;
6102
6103 /* Find the end of the name. */
6104 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006105 {
6106 if (*p == ':')
6107 {
6108 /* "s:" is start of "s:var", but "n:" is not and can be used in
6109 * slice "[n:]". Also "xx:" is not a namespace. */
6110 len = (int)(p - *arg);
6111 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6112 || len > 1)
6113 break;
6114 }
6115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (p == *arg) /* no name found */
6117 return 0;
6118
6119 len = (int)(p - *arg);
6120 *arg = skipwhite(p);
6121
6122 return len;
6123}
6124
6125/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006126 * Get the length of the name of a variable or function.
6127 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006129 * Return -1 if curly braces expansion failed.
6130 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 * If the name contains 'magic' {}'s, expand them and return the
6132 * expanded name in an allocated string via 'alias' - caller must free.
6133 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135get_name_len(
6136 char_u **arg,
6137 char_u **alias,
6138 int evaluate,
6139 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
6141 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 char_u *p;
6143 char_u *expr_start;
6144 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
6146 *alias = NULL; /* default to no alias */
6147
6148 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6149 && (*arg)[2] == (int)KE_SNR)
6150 {
6151 /* hard coded <SNR>, already translated */
6152 *arg += 3;
6153 return get_id_len(arg) + 3;
6154 }
6155 len = eval_fname_script(*arg);
6156 if (len > 0)
6157 {
6158 /* literal "<SID>", "s:" or "<SNR>" */
6159 *arg += len;
6160 }
6161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006165 p = find_name_end(*arg, &expr_start, &expr_end,
6166 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (expr_start != NULL)
6168 {
6169 char_u *temp_string;
6170
6171 if (!evaluate)
6172 {
6173 len += (int)(p - *arg);
6174 *arg = skipwhite(p);
6175 return len;
6176 }
6177
6178 /*
6179 * Include any <SID> etc in the expanded string:
6180 * Thus the -len here.
6181 */
6182 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6183 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006184 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 *alias = temp_string;
6186 *arg = skipwhite(p);
6187 return (int)STRLEN(temp_string);
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006191 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 EMSG2(_(e_invexpr2), *arg);
6193
6194 return len;
6195}
6196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006197/*
6198 * Find the end of a variable or function name, taking care of magic braces.
6199 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6200 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006201 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006202 * Return a pointer to just after the name. Equal to "arg" if there is no
6203 * valid name.
6204 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006205 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006206find_name_end(
6207 char_u *arg,
6208 char_u **expr_start,
6209 char_u **expr_end,
6210 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006212 int mb_nest = 0;
6213 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006215 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006217 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006219 *expr_start = NULL;
6220 *expr_end = NULL;
6221 }
6222
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006223 /* Quick check for valid starting character. */
6224 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6225 return arg;
6226
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006227 for (p = arg; *p != NUL
6228 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006229 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006230 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006231 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006232 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006233 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006234 if (*p == '\'')
6235 {
6236 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006237 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006238 ;
6239 if (*p == NUL)
6240 break;
6241 }
6242 else if (*p == '"')
6243 {
6244 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006245 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006246 if (*p == '\\' && p[1] != NUL)
6247 ++p;
6248 if (*p == NUL)
6249 break;
6250 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006251 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6252 {
6253 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006254 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006255 len = (int)(p - arg);
6256 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006257 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006258 break;
6259 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006260
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006261 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006263 if (*p == '[')
6264 ++br_nest;
6265 else if (*p == ']')
6266 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006268
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006269 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006271 if (*p == '{')
6272 {
6273 mb_nest++;
6274 if (expr_start != NULL && *expr_start == NULL)
6275 *expr_start = p;
6276 }
6277 else if (*p == '}')
6278 {
6279 mb_nest--;
6280 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6281 *expr_end = p;
6282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285
6286 return p;
6287}
6288
6289/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006290 * Expands out the 'magic' {}'s in a variable/function name.
6291 * Note that this can call itself recursively, to deal with
6292 * constructs like foo{bar}{baz}{bam}
6293 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6294 * "in_start" ^
6295 * "expr_start" ^
6296 * "expr_end" ^
6297 * "in_end" ^
6298 *
6299 * Returns a new allocated string, which the caller must free.
6300 * Returns NULL for failure.
6301 */
6302 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006303make_expanded_name(
6304 char_u *in_start,
6305 char_u *expr_start,
6306 char_u *expr_end,
6307 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006308{
6309 char_u c1;
6310 char_u *retval = NULL;
6311 char_u *temp_result;
6312 char_u *nextcmd = NULL;
6313
6314 if (expr_end == NULL || in_end == NULL)
6315 return NULL;
6316 *expr_start = NUL;
6317 *expr_end = NUL;
6318 c1 = *in_end;
6319 *in_end = NUL;
6320
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006321 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006322 if (temp_result != NULL && nextcmd == NULL)
6323 {
6324 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6325 + (in_end - expr_end) + 1));
6326 if (retval != NULL)
6327 {
6328 STRCPY(retval, in_start);
6329 STRCAT(retval, temp_result);
6330 STRCAT(retval, expr_end + 1);
6331 }
6332 }
6333 vim_free(temp_result);
6334
6335 *in_end = c1; /* put char back for error messages */
6336 *expr_start = '{';
6337 *expr_end = '}';
6338
6339 if (retval != NULL)
6340 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006341 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006342 if (expr_start != NULL)
6343 {
6344 /* Further expansion! */
6345 temp_result = make_expanded_name(retval, expr_start,
6346 expr_end, temp_result);
6347 vim_free(retval);
6348 retval = temp_result;
6349 }
6350 }
6351
6352 return retval;
6353}
6354
6355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006359 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006362 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6363}
6364
6365/*
6366 * Return TRUE if character "c" can be used as the first character in a
6367 * variable or function name (excluding '{' and '}').
6368 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006371{
6372 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373}
6374
6375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * Set number v: variable to "val".
6377 */
6378 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006379set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382}
6383
6384/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006385 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006387 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006388get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391}
6392
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006393/*
6394 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006395 * If the String variable has never been set, return an empty string.
6396 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006397 */
6398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006399get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006400{
6401 return get_tv_string(&vimvars[idx].vv_tv);
6402}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006405 * Get List v: variable value. Caller must take care of reference count when
6406 * needed.
6407 */
6408 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006410{
6411 return vimvars[idx].vv_list;
6412}
6413
6414/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006415 * Get Dict v: variable value. Caller must take care of reference count when
6416 * needed.
6417 */
6418 dict_T *
6419get_vim_var_dict(int idx)
6420{
6421 return vimvars[idx].vv_dict;
6422}
6423
6424/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006425 * Set v:char to character "c".
6426 */
6427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006429{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006430 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006431
6432#ifdef FEAT_MBYTE
6433 if (has_mbyte)
6434 buf[(*mb_char2bytes)(c, buf)] = NUL;
6435 else
6436#endif
6437 {
6438 buf[0] = c;
6439 buf[1] = NUL;
6440 }
6441 set_vim_var_string(VV_CHAR, buf, -1);
6442}
6443
6444/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006445 * Set v:count to "count" and v:count1 to "count1".
6446 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 */
6448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449set_vcount(
6450 long count,
6451 long count1,
6452 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006454 if (set_prevcount)
6455 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 vimvars[VV_COUNT].vv_nr = count;
6457 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458}
6459
6460/*
6461 * Set string v: variable to a copy of "val".
6462 */
6463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006464set_vim_var_string(
6465 int idx,
6466 char_u *val,
6467 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaara542c682016-01-31 16:28:04 +01006469 clear_tv(&vimvars[idx].vv_di.di_tv);
6470 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006476 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477}
6478
6479/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006480 * Set List v: variable to "val".
6481 */
6482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006484{
Bram Moolenaara542c682016-01-31 16:28:04 +01006485 clear_tv(&vimvars[idx].vv_di.di_tv);
6486 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006487 vimvars[idx].vv_list = val;
6488 if (val != NULL)
6489 ++val->lv_refcount;
6490}
6491
6492/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006493 * Set Dictionary v: variable to "val".
6494 */
6495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006497{
Bram Moolenaara542c682016-01-31 16:28:04 +01006498 clear_tv(&vimvars[idx].vv_di.di_tv);
6499 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006500 vimvars[idx].vv_dict = val;
6501 if (val != NULL)
6502 {
6503 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006504 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006505 }
6506}
6507
6508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 * Set v:register if needed.
6510 */
6511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 char_u regname;
6515
6516 if (c == 0 || c == ' ')
6517 regname = '"';
6518 else
6519 regname = c;
6520 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 set_vim_var_string(VV_REG, &regname, 1);
6523}
6524
6525/*
6526 * Get or set v:exception. If "oldval" == NULL, return the current value.
6527 * Otherwise, restore the value to "oldval" and return NULL.
6528 * Must always be called in pairs to save and restore v:exception! Does not
6529 * take care of memory allocations.
6530 */
6531 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return NULL;
6539}
6540
6541/*
6542 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6543 * Otherwise, restore the value to "oldval" and return NULL.
6544 * Must always be called in pairs to save and restore v:throwpoint! Does not
6545 * take care of memory allocations.
6546 */
6547 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006548v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 return NULL;
6555}
6556
6557#if defined(FEAT_AUTOCMD) || defined(PROTO)
6558/*
6559 * Set v:cmdarg.
6560 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6561 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6562 * Must always be called in pairs!
6563 */
6564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 char_u *oldval;
6568 char_u *newval;
6569 unsigned len;
6570
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006572 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006574 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006576 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 }
6578
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006579 if (eap->force_bin == FORCE_BIN)
6580 len = 6;
6581 else if (eap->force_bin == FORCE_NOBIN)
6582 len = 8;
6583 else
6584 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006585
6586 if (eap->read_edit)
6587 len += 7;
6588
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006589 if (eap->force_ff != 0)
6590 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6591# ifdef FEAT_MBYTE
6592 if (eap->force_enc != 0)
6593 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006594 if (eap->bad_char != 0)
6595 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006596# endif
6597
6598 newval = alloc(len + 1);
6599 if (newval == NULL)
6600 return NULL;
6601
6602 if (eap->force_bin == FORCE_BIN)
6603 sprintf((char *)newval, " ++bin");
6604 else if (eap->force_bin == FORCE_NOBIN)
6605 sprintf((char *)newval, " ++nobin");
6606 else
6607 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006608
6609 if (eap->read_edit)
6610 STRCAT(newval, " ++edit");
6611
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006612 if (eap->force_ff != 0)
6613 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6614 eap->cmd + eap->force_ff);
6615# ifdef FEAT_MBYTE
6616 if (eap->force_enc != 0)
6617 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6618 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006619 if (eap->bad_char == BAD_KEEP)
6620 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6621 else if (eap->bad_char == BAD_DROP)
6622 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6623 else if (eap->bad_char != 0)
6624 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006625# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006626 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006627 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628}
6629#endif
6630
6631/*
6632 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006633 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636get_var_tv(
6637 char_u *name,
6638 int len, /* length of "name" */
6639 typval_T *rettv, /* NULL when only checking existence */
6640 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6641 int verbose, /* may give error message */
6642 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
6649 /* truncate the name, so that we can use strcmp() */
6650 cc = name[len];
6651 name[len] = NUL;
6652
6653 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 * Check for user-defined variables.
6655 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006656 v = find_var(name, NULL, no_autoload);
6657 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006659 tv = &v->di_tv;
6660 if (dip != NULL)
6661 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006666 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006667 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 ret = FAIL;
6669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006670 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006671 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 name[len] = cc;
6674
6675 return ret;
6676}
6677
6678/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006679 * Check if variable "name[len]" is a local variable or an argument.
6680 * If so, "*eval_lavars_used" is set to TRUE.
6681 */
6682 static void
6683check_vars(char_u *name, int len)
6684{
6685 int cc;
6686 char_u *varname;
6687 hashtab_T *ht;
6688
6689 if (eval_lavars_used == NULL)
6690 return;
6691
6692 /* truncate the name, so that we can use strcmp() */
6693 cc = name[len];
6694 name[len] = NUL;
6695
6696 ht = find_var_ht(name, &varname);
6697 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6698 {
6699 if (find_var(name, NULL, TRUE) != NULL)
6700 *eval_lavars_used = TRUE;
6701 }
6702
6703 name[len] = cc;
6704}
6705
6706/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006707 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6708 * Also handle function call with Funcref variable: func(expr)
6709 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6710 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712handle_subscript(
6713 char_u **arg,
6714 typval_T *rettv,
6715 int evaluate, /* do more than finding the end */
6716 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006717{
6718 int ret = OK;
6719 dict_T *selfdict = NULL;
6720 char_u *s;
6721 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006722 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006723
6724 while (ret == OK
6725 && (**arg == '['
6726 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006727 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6728 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006729 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006730 {
6731 if (**arg == '(')
6732 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006733 partial_T *pt = NULL;
6734
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006736 if (evaluate)
6737 {
6738 functv = *rettv;
6739 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006741 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006742 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006743 {
6744 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006745 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006746 }
6747 else
6748 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006749 }
6750 else
6751 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006752 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006753 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006754 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756 /* Clear the funcref afterwards, so that deleting it while
6757 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006758 if (evaluate)
6759 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006760
6761 /* Stop the expression evaluation when immediately aborting on
6762 * error, or when an interrupt occurred or an exception was thrown
6763 * but not caught. */
6764 if (aborting())
6765 {
6766 if (ret == OK)
6767 clear_tv(rettv);
6768 ret = FAIL;
6769 }
6770 dict_unref(selfdict);
6771 selfdict = NULL;
6772 }
6773 else /* **arg == '[' || **arg == '.' */
6774 {
6775 dict_unref(selfdict);
6776 if (rettv->v_type == VAR_DICT)
6777 {
6778 selfdict = rettv->vval.v_dict;
6779 if (selfdict != NULL)
6780 ++selfdict->dv_refcount;
6781 }
6782 else
6783 selfdict = NULL;
6784 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6785 {
6786 clear_tv(rettv);
6787 ret = FAIL;
6788 }
6789 }
6790 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006791
Bram Moolenaar1d429612016-05-24 15:44:17 +02006792 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6793 * Don't do this when "Func" is already a partial that was bound
6794 * explicitly (pt_auto is FALSE). */
6795 if (selfdict != NULL
6796 && (rettv->v_type == VAR_FUNC
6797 || (rettv->v_type == VAR_PARTIAL
6798 && (rettv->vval.v_partial->pt_auto
6799 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006801
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006802 dict_unref(selfdict);
6803 return ret;
6804}
6805
6806/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006807 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006808 * value).
6809 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006810 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006812{
Bram Moolenaar33570922005-01-25 22:26:29 +00006813 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814}
6815
6816/*
6817 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * The string "s" must have been allocated, it is consumed.
6819 * Return NULL for out of memory, the variable otherwise.
6820 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
Bram Moolenaar33570922005-01-25 22:26:29 +00006824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006826 rettv = alloc_tv();
6827 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829 rettv->v_type = VAR_STRING;
6830 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832 else
6833 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835}
6836
6837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 if (varp != NULL)
6844 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 switch (varp->v_type)
6846 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006848 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006849 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006850 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006851 vim_free(varp->vval.v_string);
6852 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006853 case VAR_PARTIAL:
6854 partial_unref(varp->vval.v_partial);
6855 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 case VAR_LIST:
6857 list_unref(varp->vval.v_list);
6858 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006859 case VAR_DICT:
6860 dict_unref(varp->vval.v_dict);
6861 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006862 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006863#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006864 job_unref(varp->vval.v_job);
6865 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006866#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006867 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006868#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006869 channel_unref(varp->vval.v_channel);
6870 break;
6871#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006872 case VAR_NUMBER:
6873 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006874 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01006875 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00006876 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 vim_free(varp);
6879 }
6880}
6881
6882/*
6883 * Free the memory for a variable value and set the value to NULL or 0.
6884 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006885 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 if (varp != NULL)
6889 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006890 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006892 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006893 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006894 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006895 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01006896 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006898 case VAR_PARTIAL:
6899 partial_unref(varp->vval.v_partial);
6900 varp->vval.v_partial = NULL;
6901 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006902 case VAR_LIST:
6903 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006904 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006905 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906 case VAR_DICT:
6907 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006908 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006911 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006912 varp->vval.v_number = 0;
6913 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006914 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006915#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006916 varp->vval.v_float = 0.0;
6917 break;
6918#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006919 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006920#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006921 job_unref(varp->vval.v_job);
6922 varp->vval.v_job = NULL;
6923#endif
6924 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01006925 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006926#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006927 channel_unref(varp->vval.v_channel);
6928 varp->vval.v_channel = NULL;
6929#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 case VAR_UNKNOWN:
6931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006933 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 }
6935}
6936
6937/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006938 * Set the value of a variable to NULL without freeing items.
6939 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006941init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942{
6943 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00006944 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006945}
6946
6947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 * Get the number value of a variable.
6949 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006950 * For incompatible types, return 0.
6951 * get_tv_number_chk() is similar to get_tv_number(), but informs the
6952 * caller of incompatible types: it sets *denote to TRUE if "denote"
6953 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006955 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006956get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006958 int error = FALSE;
6959
6960 return get_tv_number_chk(varp, &error); /* return 0L on error */
6961}
6962
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006963 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006964get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006966 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006968 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006970 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006971 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006972 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006973#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00006974 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006975 break;
6976#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006977 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006978 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006979 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006980 break;
6981 case VAR_STRING:
6982 if (varp->vval.v_string != NULL)
6983 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006984 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006985 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006986 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006987 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006988 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006989 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00006990 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006991 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006992 case VAR_SPECIAL:
6993 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
6994 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006995 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006996#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006997 EMSG(_("E910: Using a Job as a Number"));
6998 break;
6999#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007000 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007001#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007002 EMSG(_("E913: Using a Channel as a Number"));
7003 break;
7004#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007005 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007006 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007007 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007009 if (denote == NULL) /* useful for values that must be unsigned */
7010 n = -1;
7011 else
7012 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007013 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007016#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007019{
7020 switch (varp->v_type)
7021 {
7022 case VAR_NUMBER:
7023 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007024 case VAR_FLOAT:
7025 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007026 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007027 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007028 EMSG(_("E891: Using a Funcref as a Float"));
7029 break;
7030 case VAR_STRING:
7031 EMSG(_("E892: Using a String as a Float"));
7032 break;
7033 case VAR_LIST:
7034 EMSG(_("E893: Using a List as a Float"));
7035 break;
7036 case VAR_DICT:
7037 EMSG(_("E894: Using a Dictionary as a Float"));
7038 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007039 case VAR_SPECIAL:
7040 EMSG(_("E907: Using a special value as a Float"));
7041 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007042 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007043# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007044 EMSG(_("E911: Using a Job as a Float"));
7045 break;
7046# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007047 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007048# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007049 EMSG(_("E914: Using a Channel as a Float"));
7050 break;
7051# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007052 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007053 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007054 break;
7055 }
7056 return 0;
7057}
7058#endif
7059
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 * Get the string value of a variable.
7062 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007063 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7064 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 * If the String variable has never been set, return an empty string.
7066 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007067 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7068 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007070 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007071get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 static char_u mybuf[NUMBUFLEN];
7074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007078 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007079get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007081 char_u *res = get_tv_string_buf_chk(varp, buf);
7082
7083 return res != NULL ? res : (char_u *)"";
7084}
7085
Bram Moolenaar7d647822014-04-05 21:28:56 +02007086/*
7087 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7088 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007089 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007090get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007091{
7092 static char_u mybuf[NUMBUFLEN];
7093
7094 return get_tv_string_buf_chk(varp, mybuf);
7095}
7096
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007097 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007098get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007099{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007100 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007102 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007103 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7104 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007105 return buf;
7106 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007107 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007108 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007109 break;
7110 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007111 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 break;
7113 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007114 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007116 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007117#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007118 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007119 break;
7120#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 case VAR_STRING:
7122 if (varp->vval.v_string != NULL)
7123 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007124 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007125 case VAR_SPECIAL:
7126 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7127 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007128 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007130 {
7131 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007132 char *status;
7133
7134 if (job == NULL)
7135 return (char_u *)"no process";
7136 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007137 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007138 : "run";
7139# ifdef UNIX
7140 vim_snprintf((char *)buf, NUMBUFLEN,
7141 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007142# elif defined(WIN32)
7143 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007144 "process %ld %s",
7145 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007146 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007147# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007148 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007149 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7150# endif
7151 return buf;
7152 }
7153#endif
7154 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007155 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007157 {
7158 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007159 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007160
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007161 if (channel == NULL)
7162 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7163 else
7164 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007165 "channel %d %s", channel->ch_id, status);
7166 return buf;
7167 }
7168#endif
7169 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 case VAR_UNKNOWN:
7171 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175}
7176
7177/*
7178 * Find variable "name" in the list of variables.
7179 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007181 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007184 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007189 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaara7043832005-01-21 11:56:39 +00007191 ht = find_var_ht(name, &varname);
7192 if (htp != NULL)
7193 *htp = ht;
7194 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007196 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7197 if (ret != NULL)
7198 return ret;
7199
7200 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007201 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202}
7203
7204/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007205 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007206 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209find_var_in_ht(
7210 hashtab_T *ht,
7211 int htname,
7212 char_u *varname,
7213 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007214{
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 hashitem_T *hi;
7216
7217 if (*varname == NUL)
7218 {
7219 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007220 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007222 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 case 'g': return &globvars_var;
7224 case 'v': return &vimvars_var;
7225 case 'b': return &curbuf->b_bufvar;
7226 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007227 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007228 case 'l': return get_funccal_local_var();
7229 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 }
7231 return NULL;
7232 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007233
7234 hi = hash_find(ht, varname);
7235 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007236 {
7237 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007238 * worked find the variable again. Don't auto-load a script if it was
7239 * loaded already, otherwise it would be loaded every time when
7240 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007241 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007242 {
7243 /* Note: script_autoload() may make "hi" invalid. It must either
7244 * be obtained again or not used. */
7245 if (!script_autoload(varname, FALSE) || aborting())
7246 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007247 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007248 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007249 if (HASHITEM_EMPTY(hi))
7250 return NULL;
7251 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007253}
7254
7255/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007257 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007258 * Set "varname" to the start of name without ':'.
7259 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007260 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007261find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007263 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007264 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007265
Bram Moolenaar73627d02015-08-11 15:46:09 +02007266 if (name[0] == NUL)
7267 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 if (name[1] != ':')
7269 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007270 /* The name must not start with a colon or #. */
7271 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 return NULL;
7273 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007274
7275 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007276 hi = hash_find(&compat_hashtab, name);
7277 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007278 return &compat_hashtab;
7279
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007280 ht = get_funccal_local_ht();
7281 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007283 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
7285 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007286 if (*name == 'g') /* global variable */
7287 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007288 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7289 */
7290 if (vim_strchr(name + 2, ':') != NULL
7291 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007292 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007294 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007296 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007297 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007298 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 if (*name == 'v') /* v: variable */
7300 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007301 if (*name == 'a') /* a: function argument */
7302 return get_funccal_args_ht();
7303 if (*name == 'l') /* l: local function variable */
7304 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (*name == 's' /* script variable */
7306 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7307 return &SCRIPT_VARS(current_SID);
7308 return NULL;
7309}
7310
7311/*
7312 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007313 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 * Returns NULL when it doesn't exist.
7315 */
7316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007317get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007321 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 if (v == NULL)
7323 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
7327/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 * sourcing this script and when executing functions defined in the script.
7330 */
7331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007332new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaara7043832005-01-21 11:56:39 +00007334 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hashtab_T *ht;
7336 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007337
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7339 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007340 /* Re-allocating ga_data means that an ht_array pointing to
7341 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007343 for (i = 1; i <= ga_scripts.ga_len; ++i)
7344 {
7345 ht = &SCRIPT_VARS(i);
7346 if (ht->ht_mask == HT_INIT_SIZE - 1)
7347 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007348 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007350 }
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 while (ga_scripts.ga_len < id)
7353 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007354 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007355 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007356 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 }
7359 }
7360}
7361
7362/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7364 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
7366 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007367init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007370 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007371 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007372 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007373 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dict_var->di_tv.vval.v_dict = dict;
7375 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007376 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7378 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379}
7380
7381/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007382 * Unreference a dictionary initialized by init_var_dict().
7383 */
7384 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007385unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007386{
7387 /* Now the dict needs to be freed if no one else is using it, go back to
7388 * normal reference counting. */
7389 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7390 dict_unref(dict);
7391}
7392
7393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 * Frees all allocated variables and the value they contain.
7396 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 */
7398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007399vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007400{
7401 vars_clear_ext(ht, TRUE);
7402}
7403
7404/*
7405 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7406 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007408vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
Bram Moolenaara7043832005-01-21 11:56:39 +00007410 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 hashitem_T *hi;
7412 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007415 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007416 for (hi = ht->ht_array; todo > 0; ++hi)
7417 {
7418 if (!HASHITEM_EMPTY(hi))
7419 {
7420 --todo;
7421
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007423 * ht_array might change then. hash_clear() takes care of it
7424 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 v = HI2DI(hi);
7426 if (free_val)
7427 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007428 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007430 }
7431 }
7432 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007433 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
Bram Moolenaara7043832005-01-21 11:56:39 +00007436/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 * Delete a variable from hashtab "ht" at item "hi".
7438 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007444
7445 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 clear_tv(&di->di_tv);
7447 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
7450/*
7451 * List the value of one internal variable.
7452 */
7453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 char_u *tofree;
7457 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007458 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007460 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007462 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007463 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464}
7465
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007467list_one_var_a(
7468 char_u *prefix,
7469 char_u *name,
7470 int type,
7471 char_u *string,
7472 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
Bram Moolenaar31859182007-08-14 20:41:13 +00007474 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7475 msg_start();
7476 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (name != NULL) /* "a:" vars don't have a name stored */
7478 msg_puts(name);
7479 msg_putchar(' ');
7480 msg_advance(22);
7481 if (type == VAR_NUMBER)
7482 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007483 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484 msg_putchar('*');
7485 else if (type == VAR_LIST)
7486 {
7487 msg_putchar('[');
7488 if (*string == '[')
7489 ++string;
7490 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 else if (type == VAR_DICT)
7492 {
7493 msg_putchar('{');
7494 if (*string == '{')
7495 ++string;
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
7498 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007499
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007501
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007502 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007503 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007504 if (*first)
7505 {
7506 msg_clr_eos();
7507 *first = FALSE;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
7510
7511/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 * If the variable already exists, the value is updated.
7514 * Otherwise the variable is created.
7515 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007517set_var(
7518 char_u *name,
7519 typval_T *tv,
7520 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007526 ht = find_var_ht(name, &varname);
7527 if (ht == NULL || *varname == NUL)
7528 {
7529 EMSG2(_(e_illvar), name);
7530 return;
7531 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007532 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007533
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007534 /* Search in parent scope which is possible to reference from lambda */
7535 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007536 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007537
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007538 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7539 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007540 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007541
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007545 if (var_check_ro(v->di_flags, name, FALSE)
7546 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548
7549 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007550 * Handle setting internal v: variables separately where needed to
7551 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 */
7553 if (ht == &vimvarht)
7554 {
7555 if (v->di_tv.v_type == VAR_STRING)
7556 {
7557 vim_free(v->di_tv.vval.v_string);
7558 if (copy || tv->v_type != VAR_STRING)
7559 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7560 else
7561 {
7562 /* Take over the string to avoid an extra alloc/free. */
7563 v->di_tv.vval.v_string = tv->vval.v_string;
7564 tv->vval.v_string = NULL;
7565 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007566 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007568 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007569 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571 if (STRCMP(varname, "searchforward") == 0)
7572 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007573#ifdef FEAT_SEARCH_EXTRA
7574 else if (STRCMP(varname, "hlsearch") == 0)
7575 {
7576 no_hlsearch = !v->di_tv.vval.v_number;
7577 redraw_all_later(SOME_VALID);
7578 }
7579#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007580 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007581 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007582 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007583 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 }
7585
7586 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 else /* add a new variable */
7589 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007590 /* Can't add "v:" variable. */
7591 if (ht == &vimvarht)
7592 {
7593 EMSG2(_(e_illvar), name);
7594 return;
7595 }
7596
Bram Moolenaar92124a32005-06-17 22:03:40 +00007597 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007598 if (!valid_varname(varname))
7599 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007600
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007601 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7602 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007603 if (v == NULL)
7604 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007608 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 return;
7610 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007611 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007613
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007614 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007616 else
7617 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007619 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007625 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 * Also give an error message.
7627 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007630{
7631 if (flags & DI_FLAGS_RO)
7632 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007633 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007634 return TRUE;
7635 }
7636 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7637 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007638 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 return TRUE;
7640 }
7641 return FALSE;
7642}
7643
7644/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007645 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7646 * Also give an error message.
7647 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007649var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007650{
7651 if (flags & DI_FLAGS_FIX)
7652 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007653 EMSG2(_("E795: Cannot delete variable %s"),
7654 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007655 return TRUE;
7656 }
7657 return FALSE;
7658}
7659
7660/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007661 * Check if a funcref is assigned to a valid variable name.
7662 * Return TRUE and give an error if not.
7663 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665var_check_func_name(
7666 char_u *name, /* points to start of variable name */
7667 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007668{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007669 /* Allow for w: b: s: and t:. */
7670 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007671 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7672 ? name[2] : name[0]))
7673 {
7674 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7675 name);
7676 return TRUE;
7677 }
7678 /* Don't allow hiding a function. When "v" is not NULL we might be
7679 * assigning another function to the same var, the type is checked
7680 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007681 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007682 {
7683 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7684 name);
7685 return TRUE;
7686 }
7687 return FALSE;
7688}
7689
7690/*
7691 * Check if a variable name is valid.
7692 * Return FALSE and give an error if not.
7693 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007696{
7697 char_u *p;
7698
7699 for (p = varname; *p != NUL; ++p)
7700 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7701 && *p != AUTOLOAD_CHAR)
7702 {
7703 EMSG2(_(e_illvar), varname);
7704 return FALSE;
7705 }
7706 return TRUE;
7707}
7708
7709/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007710 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007711 * Also give an error message, using "name" or _("name") when use_gettext is
7712 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007713 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007716{
7717 if (lock & VAR_LOCKED)
7718 {
7719 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007720 name == NULL ? (char_u *)_("Unknown")
7721 : use_gettext ? (char_u *)_(name)
7722 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007723 return TRUE;
7724 }
7725 if (lock & VAR_FIXED)
7726 {
7727 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007728 name == NULL ? (char_u *)_("Unknown")
7729 : use_gettext ? (char_u *)_(name)
7730 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007731 return TRUE;
7732 }
7733 return FALSE;
7734}
7735
7736/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007738 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007740 * It is OK for "from" and "to" to point to the same item. This is used to
7741 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007742 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007744copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007746 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007747 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 switch (from->v_type)
7749 {
7750 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007751 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 to->vval.v_number = from->vval.v_number;
7753 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007754 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007755#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007756 to->vval.v_float = from->vval.v_float;
7757 break;
7758#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007761 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007762 if (to->vval.v_job != NULL)
7763 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007764 break;
7765#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007766 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007767#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007768 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007769 if (to->vval.v_channel != NULL)
7770 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 break;
7772#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 case VAR_STRING:
7774 case VAR_FUNC:
7775 if (from->vval.v_string == NULL)
7776 to->vval.v_string = NULL;
7777 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007780 if (from->v_type == VAR_FUNC)
7781 func_ref(to->vval.v_string);
7782 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007783 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007784 case VAR_PARTIAL:
7785 if (from->vval.v_partial == NULL)
7786 to->vval.v_partial = NULL;
7787 else
7788 {
7789 to->vval.v_partial = from->vval.v_partial;
7790 ++to->vval.v_partial->pt_refcount;
7791 }
7792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 case VAR_LIST:
7794 if (from->vval.v_list == NULL)
7795 to->vval.v_list = NULL;
7796 else
7797 {
7798 to->vval.v_list = from->vval.v_list;
7799 ++to->vval.v_list->lv_refcount;
7800 }
7801 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802 case VAR_DICT:
7803 if (from->vval.v_dict == NULL)
7804 to->vval.v_dict = NULL;
7805 else
7806 {
7807 to->vval.v_dict = from->vval.v_dict;
7808 ++to->vval.v_dict->dv_refcount;
7809 }
7810 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007811 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007812 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 break;
7814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815}
7816
7817/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 * Make a copy of an item.
7819 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007820 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7821 * reference to an already copied list/dict can be used.
7822 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825item_copy(
7826 typval_T *from,
7827 typval_T *to,
7828 int deep,
7829 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830{
7831 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007832 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 {
7836 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007837 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007838 }
7839 ++recurse;
7840
7841 switch (from->v_type)
7842 {
7843 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 case VAR_STRING:
7846 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007847 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007848 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007849 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007850 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 copy_tv(from, to);
7852 break;
7853 case VAR_LIST:
7854 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007855 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007856 if (from->vval.v_list == NULL)
7857 to->vval.v_list = NULL;
7858 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7859 {
7860 /* use the copy made earlier */
7861 to->vval.v_list = from->vval.v_list->lv_copylist;
7862 ++to->vval.v_list->lv_refcount;
7863 }
7864 else
7865 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
7866 if (to->vval.v_list == NULL)
7867 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 break;
7869 case VAR_DICT:
7870 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007871 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007872 if (from->vval.v_dict == NULL)
7873 to->vval.v_dict = NULL;
7874 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7875 {
7876 /* use the copy made earlier */
7877 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7878 ++to->vval.v_dict->dv_refcount;
7879 }
7880 else
7881 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
7882 if (to->vval.v_dict == NULL)
7883 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007885 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007886 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007887 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 }
7889 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007890 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891}
7892
7893/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 * This function is used by f_input() and f_inputdialog() functions. The third
7895 * argument to f_input() specifies the type of completion to use at the
7896 * prompt. The third argument to f_inputdialog() specifies the value to return
7897 * when the user cancels the prompt.
7898 */
7899 void
7900get_user_input(
7901 typval_T *argvars,
7902 typval_T *rettv,
7903 int inputdialog,
7904 int secret)
7905{
7906 char_u *prompt = get_tv_string_chk(&argvars[0]);
7907 char_u *p = NULL;
7908 int c;
7909 char_u buf[NUMBUFLEN];
7910 int cmd_silent_save = cmd_silent;
7911 char_u *defstr = (char_u *)"";
7912 int xp_type = EXPAND_NOTHING;
7913 char_u *xp_arg = NULL;
7914
7915 rettv->v_type = VAR_STRING;
7916 rettv->vval.v_string = NULL;
7917
7918#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007919 /* While starting up, there is no place to enter text. When running tests
7920 * with --not-a-term we assume feedkeys() will be used. */
7921 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922 return;
7923#endif
7924
7925 cmd_silent = FALSE; /* Want to see the prompt. */
7926 if (prompt != NULL)
7927 {
7928 /* Only the part of the message after the last NL is considered as
7929 * prompt for the command line */
7930 p = vim_strrchr(prompt, '\n');
7931 if (p == NULL)
7932 p = prompt;
7933 else
7934 {
7935 ++p;
7936 c = *p;
7937 *p = NUL;
7938 msg_start();
7939 msg_clr_eos();
7940 msg_puts_attr(prompt, echo_attr);
7941 msg_didout = FALSE;
7942 msg_starthere();
7943 *p = c;
7944 }
7945 cmdline_row = msg_row;
7946
7947 if (argvars[1].v_type != VAR_UNKNOWN)
7948 {
7949 defstr = get_tv_string_buf_chk(&argvars[1], buf);
7950 if (defstr != NULL)
7951 stuffReadbuffSpec(defstr);
7952
7953 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
7954 {
7955 char_u *xp_name;
7956 int xp_namelen;
7957 long argt;
7958
7959 /* input() with a third argument: completion */
7960 rettv->vval.v_string = NULL;
7961
7962 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
7963 if (xp_name == NULL)
7964 return;
7965
7966 xp_namelen = (int)STRLEN(xp_name);
7967
7968 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
7969 &xp_arg) == FAIL)
7970 return;
7971 }
7972 }
7973
7974 if (defstr != NULL)
7975 {
7976 int save_ex_normal_busy = ex_normal_busy;
7977 ex_normal_busy = 0;
7978 rettv->vval.v_string =
7979 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
7980 xp_type, xp_arg);
7981 ex_normal_busy = save_ex_normal_busy;
7982 }
7983 if (inputdialog && rettv->vval.v_string == NULL
7984 && argvars[1].v_type != VAR_UNKNOWN
7985 && argvars[2].v_type != VAR_UNKNOWN)
7986 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
7987 &argvars[2], buf));
7988
7989 vim_free(xp_arg);
7990
7991 /* since the user typed this, no need to wait for return */
7992 need_wait_return = FALSE;
7993 msg_didout = FALSE;
7994 }
7995 cmd_silent = cmd_silent_save;
7996}
7997
7998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 * ":echo expr1 ..." print each argument separated with a space, add a
8000 * newline at the end.
8001 * ":echon expr1 ..." print each argument plain.
8002 */
8003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008004ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008007 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 char_u *p;
8010 int needclr = TRUE;
8011 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008012 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 if (eap->skip)
8015 ++emsg_skip;
8016 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018 /* If eval1() causes an error message the text from the command may
8019 * still need to be cleared. E.g., "echo 22,44". */
8020 need_clr_eos = needclr;
8021
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
8025 /*
8026 * Report the invalid expression unless the expression evaluation
8027 * has been cancelled due to an aborting error, an interrupt, or an
8028 * exception.
8029 */
8030 if (!aborting())
8031 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008032 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 break;
8034 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035 need_clr_eos = FALSE;
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 if (!eap->skip)
8038 {
8039 if (atstart)
8040 {
8041 atstart = FALSE;
8042 /* Call msg_start() after eval1(), evaluating the expression
8043 * may cause a message to appear. */
8044 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008045 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008046 /* Mark the saved text as finishing the line, so that what
8047 * follows is displayed on a new line when scrolling back
8048 * at the more prompt. */
8049 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 else if (eap->cmdidx == CMD_echo)
8054 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008055 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 if (p != NULL)
8057 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008059 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008061 if (*p != TAB && needclr)
8062 {
8063 /* remove any text still there from the command */
8064 msg_clr_eos();
8065 needclr = FALSE;
8066 }
8067 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 }
8069 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008070 {
8071#ifdef FEAT_MBYTE
8072 if (has_mbyte)
8073 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008074 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008075
8076 (void)msg_outtrans_len_attr(p, i, echo_attr);
8077 p += i - 1;
8078 }
8079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008081 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008086 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 arg = skipwhite(arg);
8088 }
8089 eap->nextcmd = check_nextcmd(arg);
8090
8091 if (eap->skip)
8092 --emsg_skip;
8093 else
8094 {
8095 /* remove text that may still be there from the command */
8096 if (needclr)
8097 msg_clr_eos();
8098 if (eap->cmdidx == CMD_echo)
8099 msg_end();
8100 }
8101}
8102
8103/*
8104 * ":echohl {name}".
8105 */
8106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008107ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008109 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110}
8111
8112/*
8113 * ":execute expr1 ..." execute the result of an expression.
8114 * ":echomsg expr1 ..." Print a message
8115 * ":echoerr expr1 ..." Print an error
8116 * Each gets spaces around each argument and a newline at the end for
8117 * echo commands
8118 */
8119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008120ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 int ret = OK;
8125 char_u *p;
8126 garray_T ga;
8127 int len;
8128 int save_did_emsg;
8129
8130 ga_init2(&ga, 1, 80);
8131
8132 if (eap->skip)
8133 ++emsg_skip;
8134 while (*arg != NUL && *arg != '|' && *arg != '\n')
8135 {
8136 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
8139 /*
8140 * Report the invalid expression unless the expression evaluation
8141 * has been cancelled due to an aborting error, an interrupt, or an
8142 * exception.
8143 */
8144 if (!aborting())
8145 EMSG2(_(e_invexpr2), p);
8146 ret = FAIL;
8147 break;
8148 }
8149
8150 if (!eap->skip)
8151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 len = (int)STRLEN(p);
8154 if (ga_grow(&ga, len + 2) == FAIL)
8155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 ret = FAIL;
8158 break;
8159 }
8160 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 ga.ga_len += len;
8164 }
8165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 arg = skipwhite(arg);
8168 }
8169
8170 if (ret != FAIL && ga.ga_data != NULL)
8171 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008172 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8173 {
8174 /* Mark the already saved text as finishing the line, so that what
8175 * follows is displayed on a new line when scrolling back at the
8176 * more prompt. */
8177 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008178 }
8179
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008183 out_flush();
8184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 else if (eap->cmdidx == CMD_echoerr)
8186 {
8187 /* We don't want to abort following commands, restore did_emsg. */
8188 save_did_emsg = did_emsg;
8189 EMSG((char_u *)ga.ga_data);
8190 if (!force_abort)
8191 did_emsg = save_did_emsg;
8192 }
8193 else if (eap->cmdidx == CMD_execute)
8194 do_cmdline((char_u *)ga.ga_data,
8195 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8196 }
8197
8198 ga_clear(&ga);
8199
8200 if (eap->skip)
8201 --emsg_skip;
8202
8203 eap->nextcmd = check_nextcmd(arg);
8204}
8205
8206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 * Find window specified by "vp" in tabpage "tp".
8208 */
8209 win_T *
8210find_win_by_nr(
8211 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008212 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 int nr;
8216
8217 nr = (int)get_tv_number_chk(vp, NULL);
8218
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219 if (nr < 0)
8220 return NULL;
8221 if (nr == 0)
8222 return curwin;
8223
Bram Moolenaar29323592016-07-24 22:04:11 +02008224 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8225 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 if (nr >= LOWEST_WIN_ID)
8227 {
8228 if (wp->w_id == nr)
8229 return wp;
8230 }
8231 else if (--nr <= 0)
8232 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008233 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 if (nr >= LOWEST_WIN_ID)
8235 return NULL;
8236 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237}
8238
8239/*
8240 * Find window specified by "wvp" in tabpage "tvp".
8241 */
8242 win_T *
8243find_tabwin(
8244 typval_T *wvp, /* VAR_UNKNOWN for current window */
8245 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8246{
8247 win_T *wp = NULL;
8248 tabpage_T *tp = NULL;
8249 long n;
8250
8251 if (wvp->v_type != VAR_UNKNOWN)
8252 {
8253 if (tvp->v_type != VAR_UNKNOWN)
8254 {
8255 n = (long)get_tv_number(tvp);
8256 if (n >= 0)
8257 tp = find_tabpage(n);
8258 }
8259 else
8260 tp = curtab;
8261
8262 if (tp != NULL)
8263 wp = find_win_by_nr(wvp, tp);
8264 }
8265 else
8266 wp = curwin;
8267
8268 return wp;
8269}
8270
8271/*
8272 * getwinvar() and gettabwinvar()
8273 */
8274 void
8275getwinvar(
8276 typval_T *argvars,
8277 typval_T *rettv,
8278 int off) /* 1 for gettabwinvar() */
8279{
8280 win_T *win;
8281 char_u *varname;
8282 dictitem_T *v;
8283 tabpage_T *tp = NULL;
8284 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 win_T *oldcurwin;
8286 tabpage_T *oldtabpage;
8287 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (off == 1)
8290 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8291 else
8292 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 win = find_win_by_nr(&argvars[off], tp);
8294 varname = get_tv_string_chk(&argvars[off + 1]);
8295 ++emsg_off;
8296
8297 rettv->v_type = VAR_STRING;
8298 rettv->vval.v_string = NULL;
8299
8300 if (win != NULL && varname != NULL)
8301 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 /* Set curwin to be our win, temporarily. Also set the tabpage,
8303 * otherwise the window is not valid. Only do this when needed,
8304 * autocommands get blocked. */
8305 need_switch_win = !(tp == curtab && win == curwin);
8306 if (!need_switch_win
8307 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008309 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008311 if (varname[1] == NUL)
8312 {
8313 /* get all window-local options in a dict */
8314 dict_T *opts = get_winbuf_options(FALSE);
8315
8316 if (opts != NULL)
8317 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008318 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008319 done = TRUE;
8320 }
8321 }
8322 else if (get_option_tv(&varname, rettv, 1) == OK)
8323 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 done = TRUE;
8325 }
8326 else
8327 {
8328 /* Look up the variable. */
8329 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8330 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8331 varname, FALSE);
8332 if (v != NULL)
8333 {
8334 copy_tv(&v->di_tv, rettv);
8335 done = TRUE;
8336 }
8337 }
8338 }
8339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 if (need_switch_win)
8341 /* restore previous notion of curwin */
8342 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 }
8344
8345 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8346 /* use the default return value */
8347 copy_tv(&argvars[off + 2], rettv);
8348
8349 --emsg_off;
8350}
8351
8352/*
8353 * "setwinvar()" and "settabwinvar()" functions
8354 */
8355 void
8356setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8357{
8358 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 win_T *save_curwin;
8360 tabpage_T *save_curtab;
8361 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 char_u *varname, *winvarname;
8363 typval_T *varp;
8364 char_u nbuf[NUMBUFLEN];
8365 tabpage_T *tp = NULL;
8366
8367 if (check_restricted() || check_secure())
8368 return;
8369
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 if (off == 1)
8371 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8372 else
8373 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 win = find_win_by_nr(&argvars[off], tp);
8375 varname = get_tv_string_chk(&argvars[off + 1]);
8376 varp = &argvars[off + 2];
8377
8378 if (win != NULL && varname != NULL && varp != NULL)
8379 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 need_switch_win = !(tp == curtab && win == curwin);
8381 if (!need_switch_win
8382 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 {
8384 if (*varname == '&')
8385 {
8386 long numval;
8387 char_u *strval;
8388 int error = FALSE;
8389
8390 ++varname;
8391 numval = (long)get_tv_number_chk(varp, &error);
8392 strval = get_tv_string_buf_chk(varp, nbuf);
8393 if (!error && strval != NULL)
8394 set_option_value(varname, numval, strval, OPT_LOCAL);
8395 }
8396 else
8397 {
8398 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8399 if (winvarname != NULL)
8400 {
8401 STRCPY(winvarname, "w:");
8402 STRCPY(winvarname + 2, varname);
8403 set_var(winvarname, varp, TRUE);
8404 vim_free(winvarname);
8405 }
8406 }
8407 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (need_switch_win)
8409 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 }
8411}
8412
8413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8415 * "arg" points to the "&" or '+' when called, to "option" when returning.
8416 * Returns NULL when no option name found. Otherwise pointer to the char
8417 * after the option name.
8418 */
8419 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008420find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 char_u *p = *arg;
8423
8424 ++p;
8425 if (*p == 'g' && p[1] == ':')
8426 {
8427 *opt_flags = OPT_GLOBAL;
8428 p += 2;
8429 }
8430 else if (*p == 'l' && p[1] == ':')
8431 {
8432 *opt_flags = OPT_LOCAL;
8433 p += 2;
8434 }
8435 else
8436 *opt_flags = 0;
8437
8438 if (!ASCII_ISALPHA(*p))
8439 return NULL;
8440 *arg = p;
8441
8442 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8443 p += 4; /* termcap option */
8444 else
8445 while (ASCII_ISALPHA(*p))
8446 ++p;
8447 return p;
8448}
8449
8450/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008451 * Return the autoload script name for a function or variable name.
8452 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008454 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008455autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008456{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008457 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008458 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008459
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008460 /* Get the script file name: replace '#' with '/', append ".vim". */
8461 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8462 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008463 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008464 STRCPY(scriptname, "autoload/");
8465 STRCAT(scriptname, name);
8466 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8467 STRCAT(scriptname, ".vim");
8468 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8469 *p = '/';
8470 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471}
8472
8473/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 * Return TRUE if a package was loaded.
8476 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008478script_autoload(
8479 char_u *name,
8480 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008481{
8482 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008483 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008484 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008485 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008486
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008487 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008488 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008489 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008490 return FALSE;
8491
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008492 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008494 /* Find the name in the list of previously loaded package names. Skip
8495 * "autoload/", it's always the same. */
8496 for (i = 0; i < ga_loaded.ga_len; ++i)
8497 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8498 break;
8499 if (!reload && i < ga_loaded.ga_len)
8500 ret = FALSE; /* was loaded already */
8501 else
8502 {
8503 /* Remember the name if it wasn't loaded already. */
8504 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8505 {
8506 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8507 tofree = NULL;
8508 }
8509
8510 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008511 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008512 ret = TRUE;
8513 }
8514
8515 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 return ret;
8517}
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8520typedef enum
8521{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008522 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8523 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8524 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525} var_flavour_T;
8526
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008527static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 char_u *p = varname;
8533
8534 if (ASCII_ISUPPER(*p))
8535 {
8536 while (*(++p))
8537 if (ASCII_ISLOWER(*p))
8538 return VAR_FLAVOUR_SESSION;
8539 return VAR_FLAVOUR_VIMINFO;
8540 }
8541 else
8542 return VAR_FLAVOUR_DEFAULT;
8543}
8544#endif
8545
8546#if defined(FEAT_VIMINFO) || defined(PROTO)
8547/*
8548 * Restore global vars that start with a capital from the viminfo file
8549 */
8550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008554 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008556 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558 if (!writing && (find_viminfo_parameter('!') != NULL))
8559 {
8560 tab = vim_strchr(virp->vir_line + 1, '\t');
8561 if (tab != NULL)
8562 {
8563 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008564 switch (*tab)
8565 {
8566 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008567#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008568 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008569#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008570 case 'D': type = VAR_DICT; break;
8571 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008572 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575 tab = vim_strchr(tab, '\t');
8576 if (tab != NULL)
8577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008578 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008579 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008580 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008582#ifdef FEAT_FLOAT
8583 else if (type == VAR_FLOAT)
8584 (void)string2float(tab + 1, &tv.vval.v_float);
8585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008587 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008588 if (type == VAR_DICT || type == VAR_LIST)
8589 {
8590 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8591
8592 if (etv == NULL)
8593 /* Failed to parse back the dict or list, use it as a
8594 * string. */
8595 tv.v_type = VAR_STRING;
8596 else
8597 {
8598 vim_free(tv.vval.v_string);
8599 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008600 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008601 }
8602 }
8603
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008604 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008605 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008606 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008607 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008608
8609 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008610 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008611 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8612 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614 }
8615 }
8616
8617 return viminfo_readline(virp);
8618}
8619
8620/*
8621 * Write global vars that start with a capital to the viminfo file
8622 */
8623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008624write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 hashitem_T *hi;
8627 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008628 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008629 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008630 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008632 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 if (find_viminfo_parameter('!') == NULL)
8635 return;
8636
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008637 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008638
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008639 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008642 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008644 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 this_var = HI2DI(hi);
8646 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008649 {
8650 case VAR_STRING: s = "STR"; break;
8651 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008652 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008653 case VAR_DICT: s = "DIC"; break;
8654 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008655 case VAR_SPECIAL: s = "XPL"; break;
8656
8657 case VAR_UNKNOWN:
8658 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008659 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008660 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008661 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008662 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008663 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008665 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008666 if (p != NULL)
8667 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008668 vim_free(tofree);
8669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
8671 }
8672}
8673#endif
8674
8675#if defined(FEAT_SESSION) || defined(PROTO)
8676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008677store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 hashitem_T *hi;
8680 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008681 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 char_u *p, *t;
8683
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008684 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008687 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008689 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 this_var = HI2DI(hi);
8691 if ((this_var->di_tv.v_type == VAR_NUMBER
8692 || this_var->di_tv.v_type == VAR_STRING)
8693 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008694 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008695 /* Escape special characters with a backslash. Turn a LF and
8696 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008698 (char_u *)"\\\"\n\r");
8699 if (p == NULL) /* out of memory */
8700 break;
8701 for (t = p; *t != NUL; ++t)
8702 if (*t == '\n')
8703 *t = 'n';
8704 else if (*t == '\r')
8705 *t = 'r';
8706 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 this_var->di_key,
8708 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8709 : ' ',
8710 p,
8711 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8712 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008713 || put_eol(fd) == FAIL)
8714 {
8715 vim_free(p);
8716 return FAIL;
8717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 vim_free(p);
8719 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008720#ifdef FEAT_FLOAT
8721 else if (this_var->di_tv.v_type == VAR_FLOAT
8722 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8723 {
8724 float_T f = this_var->di_tv.vval.v_float;
8725 int sign = ' ';
8726
8727 if (f < 0)
8728 {
8729 f = -f;
8730 sign = '-';
8731 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008732 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008733 this_var->di_key, sign, f) < 0)
8734 || put_eol(fd) == FAIL)
8735 return FAIL;
8736 }
8737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
8739 }
8740 return OK;
8741}
8742#endif
8743
Bram Moolenaar661b1822005-07-28 22:36:45 +00008744/*
8745 * Display script name where an item was last set.
8746 * Should only be invoked when 'verbose' is non-zero.
8747 */
8748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008749last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008750{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008751 char_u *p;
8752
Bram Moolenaar661b1822005-07-28 22:36:45 +00008753 if (scriptID != 0)
8754 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008755 p = home_replace_save(NULL, get_scriptname(scriptID));
8756 if (p != NULL)
8757 {
8758 verbose_enter();
8759 MSG_PUTS(_("\n\tLast set from "));
8760 MSG_PUTS(p);
8761 vim_free(p);
8762 verbose_leave();
8763 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008764 }
8765}
8766
Bram Moolenaar53744302015-07-17 17:38:22 +02008767/* reset v:option_new, v:option_old and v:option_type */
8768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008769reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008770{
8771 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8772 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8773 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8774}
8775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776/*
8777 * Prepare "gap" for an assert error and add the sourcing position.
8778 */
8779 void
8780prepare_assert_error(garray_T *gap)
8781{
8782 char buf[NUMBUFLEN];
8783
8784 ga_init2(gap, 1, 100);
8785 if (sourcing_name != NULL)
8786 {
8787 ga_concat(gap, sourcing_name);
8788 if (sourcing_lnum > 0)
8789 ga_concat(gap, (char_u *)" ");
8790 }
8791 if (sourcing_lnum > 0)
8792 {
8793 sprintf(buf, "line %ld", (long)sourcing_lnum);
8794 ga_concat(gap, (char_u *)buf);
8795 }
8796 if (sourcing_name != NULL || sourcing_lnum > 0)
8797 ga_concat(gap, (char_u *)": ");
8798}
8799
8800/*
8801 * Add an assert error to v:errors.
8802 */
8803 void
8804assert_error(garray_T *gap)
8805{
8806 struct vimvar *vp = &vimvars[VV_ERRORS];
8807
8808 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
8809 /* Make sure v:errors is a list. */
8810 set_vim_var_list(VV_ERRORS, list_alloc());
8811 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
8812}
8813
8814 void
8815assert_equal_common(typval_T *argvars, assert_type_T atype)
8816{
8817 garray_T ga;
8818
8819 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
8820 != (atype == ASSERT_EQUAL))
8821 {
8822 prepare_assert_error(&ga);
8823 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8824 atype);
8825 assert_error(&ga);
8826 ga_clear(&ga);
8827 }
8828}
8829
8830 void
8831assert_match_common(typval_T *argvars, assert_type_T atype)
8832{
8833 garray_T ga;
8834 char_u buf1[NUMBUFLEN];
8835 char_u buf2[NUMBUFLEN];
8836 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
8837 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
8838
8839 if (pat == NULL || text == NULL)
8840 EMSG(_(e_invarg));
8841 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
8842 {
8843 prepare_assert_error(&ga);
8844 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
8845 atype);
8846 assert_error(&ga);
8847 ga_clear(&ga);
8848 }
8849}
8850
Bram Moolenaar61c04492016-07-23 15:35:35 +02008851 void
8852assert_inrange(typval_T *argvars)
8853{
8854 garray_T ga;
8855 int error = FALSE;
8856 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
8857 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
8858 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
8859 char_u *tofree;
8860 char msg[200];
8861 char_u numbuf[NUMBUFLEN];
8862
8863 if (error)
8864 return;
8865 if (actual < lower || actual > upper)
8866 {
8867 prepare_assert_error(&ga);
8868 if (argvars[3].v_type != VAR_UNKNOWN)
8869 {
8870 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
8871 vim_free(tofree);
8872 }
8873 else
8874 {
8875 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
8876 (long)lower, (long)upper, (long)actual);
8877 ga_concat(&ga, (char_u *)msg);
8878 }
8879 assert_error(&ga);
8880 ga_clear(&ga);
8881 }
8882}
8883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884/*
8885 * Common for assert_true() and assert_false().
8886 */
8887 void
8888assert_bool(typval_T *argvars, int isTrue)
8889{
8890 int error = FALSE;
8891 garray_T ga;
8892
8893 if (argvars[0].v_type == VAR_SPECIAL
8894 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
8895 return;
8896 if (argvars[0].v_type != VAR_NUMBER
8897 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
8898 || error)
8899 {
8900 prepare_assert_error(&ga);
8901 fill_assert_error(&ga, &argvars[1],
8902 (char_u *)(isTrue ? "True" : "False"),
8903 NULL, &argvars[0], ASSERT_OTHER);
8904 assert_error(&ga);
8905 ga_clear(&ga);
8906 }
8907}
8908
8909 void
Bram Moolenaar42205552017-03-18 19:42:22 +01008910assert_report(typval_T *argvars)
8911{
8912 garray_T ga;
8913
8914 prepare_assert_error(&ga);
8915 ga_concat(&ga, get_tv_string(&argvars[0]));
8916 assert_error(&ga);
8917 ga_clear(&ga);
8918}
8919
8920 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921assert_exception(typval_T *argvars)
8922{
8923 garray_T ga;
8924 char_u *error = get_tv_string_chk(&argvars[0]);
8925
8926 if (vimvars[VV_EXCEPTION].vv_str == NULL)
8927 {
8928 prepare_assert_error(&ga);
8929 ga_concat(&ga, (char_u *)"v:exception is not set");
8930 assert_error(&ga);
8931 ga_clear(&ga);
8932 }
8933 else if (error != NULL
8934 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
8935 {
8936 prepare_assert_error(&ga);
8937 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
8938 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
8939 assert_error(&ga);
8940 ga_clear(&ga);
8941 }
8942}
8943
8944 void
8945assert_fails(typval_T *argvars)
8946{
8947 char_u *cmd = get_tv_string_chk(&argvars[0]);
8948 garray_T ga;
8949
8950 called_emsg = FALSE;
8951 suppress_errthrow = TRUE;
8952 emsg_silent = TRUE;
8953 do_cmdline_cmd(cmd);
8954 if (!called_emsg)
8955 {
8956 prepare_assert_error(&ga);
8957 ga_concat(&ga, (char_u *)"command did not fail: ");
8958 ga_concat(&ga, cmd);
8959 assert_error(&ga);
8960 ga_clear(&ga);
8961 }
8962 else if (argvars[1].v_type != VAR_UNKNOWN)
8963 {
8964 char_u buf[NUMBUFLEN];
8965 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
8966
8967 if (error == NULL
8968 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
8969 {
8970 prepare_assert_error(&ga);
8971 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
8972 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
8973 assert_error(&ga);
8974 ga_clear(&ga);
8975 }
8976 }
8977
8978 called_emsg = FALSE;
8979 suppress_errthrow = FALSE;
8980 emsg_silent = FALSE;
8981 emsg_on_display = FALSE;
8982 set_vim_var_string(VV_ERRMSG, NULL, 0);
8983}
8984
8985/*
8986 * Append "str" to "gap", escaping unprintable characters.
8987 * Changes NL to \n, CR to \r, etc.
8988 */
8989 static void
8990ga_concat_esc(garray_T *gap, char_u *str)
8991{
8992 char_u *p;
8993 char_u buf[NUMBUFLEN];
8994
8995 if (str == NULL)
8996 {
8997 ga_concat(gap, (char_u *)"NULL");
8998 return;
8999 }
9000
9001 for (p = str; *p != NUL; ++p)
9002 switch (*p)
9003 {
9004 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9005 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9006 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9007 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9008 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9009 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9010 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9011 default:
9012 if (*p < ' ')
9013 {
9014 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9015 ga_concat(gap, buf);
9016 }
9017 else
9018 ga_append(gap, *p);
9019 break;
9020 }
9021}
9022
9023/*
9024 * Fill "gap" with information about an assert error.
9025 */
9026 void
9027fill_assert_error(
9028 garray_T *gap,
9029 typval_T *opt_msg_tv,
9030 char_u *exp_str,
9031 typval_T *exp_tv,
9032 typval_T *got_tv,
9033 assert_type_T atype)
9034{
9035 char_u numbuf[NUMBUFLEN];
9036 char_u *tofree;
9037
9038 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9039 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009040 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9041 vim_free(tofree);
9042 ga_concat(gap, (char_u *)": ");
9043 }
9044
9045 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9046 ga_concat(gap, (char_u *)"Pattern ");
9047 else if (atype == ASSERT_NOTEQUAL)
9048 ga_concat(gap, (char_u *)"Expected not equal to ");
9049 else
9050 ga_concat(gap, (char_u *)"Expected ");
9051 if (exp_str == NULL)
9052 {
9053 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 vim_free(tofree);
9055 }
9056 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009057 ga_concat_esc(gap, exp_str);
9058 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009060 if (atype == ASSERT_MATCH)
9061 ga_concat(gap, (char_u *)" does not match ");
9062 else if (atype == ASSERT_NOTMATCH)
9063 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009065 ga_concat(gap, (char_u *)" but got ");
9066 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9067 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 }
9069}
9070
Bram Moolenaar53744302015-07-17 17:38:22 +02009071
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009072 int
9073typval_compare(
9074 typval_T *typ1, /* first operand */
9075 typval_T *typ2, /* second operand */
9076 exptype_T type, /* operator */
9077 int type_is, /* TRUE for "is" and "isnot" */
9078 int ic, /* ignore case */
9079 int evaluate)
9080{
9081 int i;
9082 varnumber_T n1, n2;
9083 char_u *s1, *s2;
9084 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9085
9086 if (evaluate)
9087 {
9088 if (type_is && typ1->v_type != typ2->v_type)
9089 {
9090 /* For "is" a different type always means FALSE, for "notis"
9091 * it means TRUE. */
9092 n1 = (type == TYPE_NEQUAL);
9093 }
9094 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9095 {
9096 if (type_is)
9097 {
9098 n1 = (typ1->v_type == typ2->v_type
9099 && typ1->vval.v_list == typ2->vval.v_list);
9100 if (type == TYPE_NEQUAL)
9101 n1 = !n1;
9102 }
9103 else if (typ1->v_type != typ2->v_type
9104 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9105 {
9106 if (typ1->v_type != typ2->v_type)
9107 EMSG(_("E691: Can only compare List with List"));
9108 else
9109 EMSG(_("E692: Invalid operation for List"));
9110 clear_tv(typ1);
9111 clear_tv(typ2);
9112 return FAIL;
9113 }
9114 else
9115 {
9116 /* Compare two Lists for being equal or unequal. */
9117 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9118 ic, FALSE);
9119 if (type == TYPE_NEQUAL)
9120 n1 = !n1;
9121 }
9122 }
9123
9124 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9125 {
9126 if (type_is)
9127 {
9128 n1 = (typ1->v_type == typ2->v_type
9129 && typ1->vval.v_dict == typ2->vval.v_dict);
9130 if (type == TYPE_NEQUAL)
9131 n1 = !n1;
9132 }
9133 else if (typ1->v_type != typ2->v_type
9134 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9135 {
9136 if (typ1->v_type != typ2->v_type)
9137 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
9138 else
9139 EMSG(_("E736: Invalid operation for Dictionary"));
9140 clear_tv(typ1);
9141 clear_tv(typ2);
9142 return FAIL;
9143 }
9144 else
9145 {
9146 /* Compare two Dictionaries for being equal or unequal. */
9147 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9148 ic, FALSE);
9149 if (type == TYPE_NEQUAL)
9150 n1 = !n1;
9151 }
9152 }
9153
9154 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9155 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9156 {
9157 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9158 {
9159 EMSG(_("E694: Invalid operation for Funcrefs"));
9160 clear_tv(typ1);
9161 clear_tv(typ2);
9162 return FAIL;
9163 }
9164 if ((typ1->v_type == VAR_PARTIAL
9165 && typ1->vval.v_partial == NULL)
9166 || (typ2->v_type == VAR_PARTIAL
9167 && typ2->vval.v_partial == NULL))
9168 /* when a partial is NULL assume not equal */
9169 n1 = FALSE;
9170 else if (type_is)
9171 {
9172 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9173 /* strings are considered the same if their value is
9174 * the same */
9175 n1 = tv_equal(typ1, typ2, ic, FALSE);
9176 else if (typ1->v_type == VAR_PARTIAL
9177 && typ2->v_type == VAR_PARTIAL)
9178 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9179 else
9180 n1 = FALSE;
9181 }
9182 else
9183 n1 = tv_equal(typ1, typ2, ic, FALSE);
9184 if (type == TYPE_NEQUAL)
9185 n1 = !n1;
9186 }
9187
9188#ifdef FEAT_FLOAT
9189 /*
9190 * If one of the two variables is a float, compare as a float.
9191 * When using "=~" or "!~", always compare as string.
9192 */
9193 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9194 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9195 {
9196 float_T f1, f2;
9197
9198 if (typ1->v_type == VAR_FLOAT)
9199 f1 = typ1->vval.v_float;
9200 else
9201 f1 = get_tv_number(typ1);
9202 if (typ2->v_type == VAR_FLOAT)
9203 f2 = typ2->vval.v_float;
9204 else
9205 f2 = get_tv_number(typ2);
9206 n1 = FALSE;
9207 switch (type)
9208 {
9209 case TYPE_EQUAL: n1 = (f1 == f2); break;
9210 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9211 case TYPE_GREATER: n1 = (f1 > f2); break;
9212 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9213 case TYPE_SMALLER: n1 = (f1 < f2); break;
9214 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9215 case TYPE_UNKNOWN:
9216 case TYPE_MATCH:
9217 case TYPE_NOMATCH: break; /* avoid gcc warning */
9218 }
9219 }
9220#endif
9221
9222 /*
9223 * If one of the two variables is a number, compare as a number.
9224 * When using "=~" or "!~", always compare as string.
9225 */
9226 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9227 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9228 {
9229 n1 = get_tv_number(typ1);
9230 n2 = get_tv_number(typ2);
9231 switch (type)
9232 {
9233 case TYPE_EQUAL: n1 = (n1 == n2); break;
9234 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9235 case TYPE_GREATER: n1 = (n1 > n2); break;
9236 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9237 case TYPE_SMALLER: n1 = (n1 < n2); break;
9238 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9239 case TYPE_UNKNOWN:
9240 case TYPE_MATCH:
9241 case TYPE_NOMATCH: break; /* avoid gcc warning */
9242 }
9243 }
9244 else
9245 {
9246 s1 = get_tv_string_buf(typ1, buf1);
9247 s2 = get_tv_string_buf(typ2, buf2);
9248 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9249 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9250 else
9251 i = 0;
9252 n1 = FALSE;
9253 switch (type)
9254 {
9255 case TYPE_EQUAL: n1 = (i == 0); break;
9256 case TYPE_NEQUAL: n1 = (i != 0); break;
9257 case TYPE_GREATER: n1 = (i > 0); break;
9258 case TYPE_GEQUAL: n1 = (i >= 0); break;
9259 case TYPE_SMALLER: n1 = (i < 0); break;
9260 case TYPE_SEQUAL: n1 = (i <= 0); break;
9261
9262 case TYPE_MATCH:
9263 case TYPE_NOMATCH:
9264 n1 = pattern_match(s2, s1, ic);
9265 if (type == TYPE_NOMATCH)
9266 n1 = !n1;
9267 break;
9268
9269 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9270 }
9271 }
9272 clear_tv(typ1);
9273 clear_tv(typ2);
9274 typ1->v_type = VAR_NUMBER;
9275 typ1->vval.v_number = n1;
9276 }
9277 return OK;
9278}
9279
9280 int
9281typval_copy(typ1, typ2)
9282 typval_T *typ1;
9283 typval_T *typ2;
9284{
9285 if (typ2 == NULL)
9286 rettv_list_alloc(typ2);
9287
9288 if (typ1 != NULL && typ2 != NULL)
9289 return item_copy(typ1, typ2, TRUE, 0);
9290
9291 return FAIL;
9292}
9293
9294 char_u *
9295typval_tostring(arg)
9296 typval_T *arg;
9297{
9298 char_u *tofree;
9299 char_u numbuf[NUMBUFLEN];
9300 char_u *ret = NULL;
9301
9302 if (arg == NULL)
9303 return vim_strsave((char_u *)"(does not exist)");
9304 ret = tv2string(arg, &tofree, numbuf, 0);
9305 /* Make a copy if we have a value but it's not in allocated memory. */
9306 if (ret != NULL && tofree == NULL)
9307 ret = vim_strsave(ret);
9308 return ret;
9309}
9310
9311 int
9312var_exists(char_u *var)
9313{
9314 char_u *name;
9315 char_u *tofree;
9316 typval_T tv;
9317 int len = 0;
9318 int n = FALSE;
9319
9320 /* get_name_len() takes care of expanding curly braces */
9321 name = var;
9322 len = get_name_len(&var, &tofree, TRUE, FALSE);
9323 if (len > 0)
9324 {
9325 if (tofree != NULL)
9326 name = tofree;
9327 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9328 if (n)
9329 {
9330 /* handle d.key, l[idx], f(expr) */
9331 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9332 if (n)
9333 clear_tv(&tv);
9334 }
9335 }
9336 if (*var != NUL)
9337 n = FALSE;
9338
9339 vim_free(tofree);
9340 return n;
9341}
9342
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343#endif /* FEAT_EVAL */
9344
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009346#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348#ifdef WIN3264
9349/*
9350 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9351 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009352static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9353static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9354static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
9356/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009357 * Get the short path (8.3) for the filename in "fnamep".
9358 * Only works for a valid file name.
9359 * When the path gets longer "fnamep" is changed and the allocated buffer
9360 * is put in "bufp".
9361 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9362 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 */
9364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009365get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009367 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 char_u *newbuf;
9369
9370 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009371 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (l > len - 1)
9373 {
9374 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009375 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 newbuf = vim_strnsave(*fnamep, l);
9377 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379
9380 vim_free(*bufp);
9381 *fnamep = *bufp = newbuf;
9382
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009383 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009384 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 }
9386
9387 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009388 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009392 * Get the short path (8.3) for the filename in "fname". The converted
9393 * path is returned in "bufp".
9394 *
9395 * Some of the directories specified in "fname" may not exist. This function
9396 * will shorten the existing directories at the beginning of the path and then
9397 * append the remaining non-existing path.
9398 *
9399 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009400 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009401 * bufp - Pointer to an allocated buffer for the filename.
9402 * fnamelen - Length of the filename pointed to by fname
9403 *
9404 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 */
9406 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407shortpath_for_invalid_fname(
9408 char_u **fname,
9409 char_u **bufp,
9410 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009412 char_u *short_fname, *save_fname, *pbuf_unused;
9413 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009415 int old_len, len;
9416 int new_len, sfx_len;
9417 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
9419 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009420 old_len = *fnamelen;
9421 save_fname = vim_strnsave(*fname, old_len);
9422 pbuf_unused = NULL;
9423 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009425 endp = save_fname + old_len - 1; /* Find the end of the copy */
9426 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009428 /*
9429 * Try shortening the supplied path till it succeeds by removing one
9430 * directory at a time from the tail of the path.
9431 */
9432 len = 0;
9433 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009435 /* go back one path-separator */
9436 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9437 --endp;
9438 if (endp <= save_fname)
9439 break; /* processed the complete path */
9440
9441 /*
9442 * Replace the path separator with a NUL and try to shorten the
9443 * resulting path.
9444 */
9445 ch = *endp;
9446 *endp = 0;
9447 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009448 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009449 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9450 {
9451 retval = FAIL;
9452 goto theend;
9453 }
9454 *endp = ch; /* preserve the string */
9455
9456 if (len > 0)
9457 break; /* successfully shortened the path */
9458
9459 /* failed to shorten the path. Skip the path separator */
9460 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 }
9462
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009463 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009465 /*
9466 * Succeeded in shortening the path. Now concatenate the shortened
9467 * path with the remaining path at the tail.
9468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009470 /* Compute the length of the new path. */
9471 sfx_len = (int)(save_endp - endp) + 1;
9472 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009474 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009476 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009478 /* There is not enough space in the currently allocated string,
9479 * copy it to a buffer big enough. */
9480 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009482 {
9483 retval = FAIL;
9484 goto theend;
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 }
9487 else
9488 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009489 /* Transfer short_fname to the main buffer (it's big enough),
9490 * unless get_short_pathname() did its work in-place. */
9491 *fname = *bufp = save_fname;
9492 if (short_fname != save_fname)
9493 vim_strncpy(save_fname, short_fname, len);
9494 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009496
9497 /* concat the not-shortened part of the path */
9498 vim_strncpy(*fname + len, endp, sfx_len);
9499 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009501
9502theend:
9503 vim_free(pbuf_unused);
9504 vim_free(save_fname);
9505
9506 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509/*
9510 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009511 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 */
9513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514shortpath_for_partial(
9515 char_u **fnamep,
9516 char_u **bufp,
9517 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 int sepcount, len, tflen;
9520 char_u *p;
9521 char_u *pbuf, *tfname;
9522 int hasTilde;
9523
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009524 /* Count up the path separators from the RHS.. so we know which part
9525 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009527 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (vim_ispathsep(*p))
9529 ++sepcount;
9530
9531 /* Need full path first (use expand_env() to remove a "~/") */
9532 hasTilde = (**fnamep == '~');
9533 if (hasTilde)
9534 pbuf = tfname = expand_env_save(*fnamep);
9535 else
9536 pbuf = tfname = FullName_save(*fnamep, FALSE);
9537
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009538 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009540 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9541 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542
9543 if (len == 0)
9544 {
9545 /* Don't have a valid filename, so shorten the rest of the
9546 * path if we can. This CAN give us invalid 8.3 filenames, but
9547 * there's not a lot of point in guessing what it might be.
9548 */
9549 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009550 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9551 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 }
9553
9554 /* Count the paths backward to find the beginning of the desired string. */
9555 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009556 {
9557#ifdef FEAT_MBYTE
9558 if (has_mbyte)
9559 p -= mb_head_off(tfname, p);
9560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 if (vim_ispathsep(*p))
9562 {
9563 if (sepcount == 0 || (hasTilde && sepcount == 1))
9564 break;
9565 else
9566 sepcount --;
9567 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 if (hasTilde)
9570 {
9571 --p;
9572 if (p >= tfname)
9573 *p = '~';
9574 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009575 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 }
9577 else
9578 ++p;
9579
9580 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9581 vim_free(*bufp);
9582 *fnamelen = (int)STRLEN(p);
9583 *bufp = pbuf;
9584 *fnamep = p;
9585
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009586 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
9588#endif /* WIN3264 */
9589
9590/*
9591 * Adjust a filename, according to a string of modifiers.
9592 * *fnamep must be NUL terminated when called. When returning, the length is
9593 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009594 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 * When there is an error, *fnamep is set to NULL.
9596 */
9597 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009598modify_fname(
9599 char_u *src, /* string with modifiers */
9600 int *usedlen, /* characters after src that are used */
9601 char_u **fnamep, /* file name so far */
9602 char_u **bufp, /* buffer for allocated file name or NULL */
9603 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 int valid = 0;
9606 char_u *tail;
9607 char_u *s, *p, *pbuf;
9608 char_u dirname[MAXPATHL];
9609 int c;
9610 int has_fullname = 0;
9611#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009612 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 int has_shortname = 0;
9614#endif
9615
9616repeat:
9617 /* ":p" - full path/file_name */
9618 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9619 {
9620 has_fullname = 1;
9621
9622 valid |= VALID_PATH;
9623 *usedlen += 2;
9624
9625 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9626 if ((*fnamep)[0] == '~'
9627#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9628 && ((*fnamep)[1] == '/'
9629# ifdef BACKSLASH_IN_FILENAME
9630 || (*fnamep)[1] == '\\'
9631# endif
9632 || (*fnamep)[1] == NUL)
9633
9634#endif
9635 )
9636 {
9637 *fnamep = expand_env_save(*fnamep);
9638 vim_free(*bufp); /* free any allocated file name */
9639 *bufp = *fnamep;
9640 if (*fnamep == NULL)
9641 return -1;
9642 }
9643
9644 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009645 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
9647 if (vim_ispathsep(*p)
9648 && p[1] == '.'
9649 && (p[2] == NUL
9650 || vim_ispathsep(p[2])
9651 || (p[2] == '.'
9652 && (p[3] == NUL || vim_ispathsep(p[3])))))
9653 break;
9654 }
9655
9656 /* FullName_save() is slow, don't use it when not needed. */
9657 if (*p != NUL || !vim_isAbsName(*fnamep))
9658 {
9659 *fnamep = FullName_save(*fnamep, *p != NUL);
9660 vim_free(*bufp); /* free any allocated file name */
9661 *bufp = *fnamep;
9662 if (*fnamep == NULL)
9663 return -1;
9664 }
9665
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009666#ifdef WIN3264
9667# if _WIN32_WINNT >= 0x0500
9668 if (vim_strchr(*fnamep, '~') != NULL)
9669 {
9670 /* Expand 8.3 filename to full path. Needed to make sure the same
9671 * file does not have two different names.
9672 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9673 p = alloc(_MAX_PATH + 1);
9674 if (p != NULL)
9675 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009676 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009677 {
9678 vim_free(*bufp);
9679 *bufp = *fnamep = p;
9680 }
9681 else
9682 vim_free(p);
9683 }
9684 }
9685# endif
9686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 /* Append a path separator to a directory. */
9688 if (mch_isdir(*fnamep))
9689 {
9690 /* Make room for one or two extra characters. */
9691 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9692 vim_free(*bufp); /* free any allocated file name */
9693 *bufp = *fnamep;
9694 if (*fnamep == NULL)
9695 return -1;
9696 add_pathsep(*fnamep);
9697 }
9698 }
9699
9700 /* ":." - path relative to the current directory */
9701 /* ":~" - path relative to the home directory */
9702 /* ":8" - shortname path - postponed till after */
9703 while (src[*usedlen] == ':'
9704 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9705 {
9706 *usedlen += 2;
9707 if (c == '8')
9708 {
9709#ifdef WIN3264
9710 has_shortname = 1; /* Postpone this. */
9711#endif
9712 continue;
9713 }
9714 pbuf = NULL;
9715 /* Need full path first (use expand_env() to remove a "~/") */
9716 if (!has_fullname)
9717 {
9718 if (c == '.' && **fnamep == '~')
9719 p = pbuf = expand_env_save(*fnamep);
9720 else
9721 p = pbuf = FullName_save(*fnamep, FALSE);
9722 }
9723 else
9724 p = *fnamep;
9725
9726 has_fullname = 0;
9727
9728 if (p != NULL)
9729 {
9730 if (c == '.')
9731 {
9732 mch_dirname(dirname, MAXPATHL);
9733 s = shorten_fname(p, dirname);
9734 if (s != NULL)
9735 {
9736 *fnamep = s;
9737 if (pbuf != NULL)
9738 {
9739 vim_free(*bufp); /* free any allocated file name */
9740 *bufp = pbuf;
9741 pbuf = NULL;
9742 }
9743 }
9744 }
9745 else
9746 {
9747 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9748 /* Only replace it when it starts with '~' */
9749 if (*dirname == '~')
9750 {
9751 s = vim_strsave(dirname);
9752 if (s != NULL)
9753 {
9754 *fnamep = s;
9755 vim_free(*bufp);
9756 *bufp = s;
9757 }
9758 }
9759 }
9760 vim_free(pbuf);
9761 }
9762 }
9763
9764 tail = gettail(*fnamep);
9765 *fnamelen = (int)STRLEN(*fnamep);
9766
9767 /* ":h" - head, remove "/file_name", can be repeated */
9768 /* Don't remove the first "/" or "c:\" */
9769 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9770 {
9771 valid |= VALID_HEAD;
9772 *usedlen += 2;
9773 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009774 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009775 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 *fnamelen = (int)(tail - *fnamep);
9777#ifdef VMS
9778 if (*fnamelen > 0)
9779 *fnamelen += 1; /* the path separator is part of the path */
9780#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009781 if (*fnamelen == 0)
9782 {
9783 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9784 p = vim_strsave((char_u *)".");
9785 if (p == NULL)
9786 return -1;
9787 vim_free(*bufp);
9788 *bufp = *fnamep = tail = p;
9789 *fnamelen = 1;
9790 }
9791 else
9792 {
9793 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009794 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 }
9797
9798 /* ":8" - shortname */
9799 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9800 {
9801 *usedlen += 2;
9802#ifdef WIN3264
9803 has_shortname = 1;
9804#endif
9805 }
9806
9807#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009808 /*
9809 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 */
9811 if (has_shortname)
9812 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009813 /* Copy the string if it is shortened by :h and when it wasn't copied
9814 * yet, because we are going to change it in place. Avoids changing
9815 * the buffer name for "%:8". */
9816 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 {
9818 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009819 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 return -1;
9821 vim_free(*bufp);
9822 *bufp = *fnamep = p;
9823 }
9824
9825 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009826 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 if (!has_fullname && !vim_isAbsName(*fnamep))
9828 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009829 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 return -1;
9831 }
9832 else
9833 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009834 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
Bram Moolenaardc935552011-08-17 15:23:23 +02009836 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009838 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 return -1;
9840
9841 if (l == 0)
9842 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009843 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009845 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 return -1;
9847 }
9848 *fnamelen = l;
9849 }
9850 }
9851#endif /* WIN3264 */
9852
9853 /* ":t" - tail, just the basename */
9854 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9855 {
9856 *usedlen += 2;
9857 *fnamelen -= (int)(tail - *fnamep);
9858 *fnamep = tail;
9859 }
9860
9861 /* ":e" - extension, can be repeated */
9862 /* ":r" - root, without extension, can be repeated */
9863 while (src[*usedlen] == ':'
9864 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9865 {
9866 /* find a '.' in the tail:
9867 * - for second :e: before the current fname
9868 * - otherwise: The last '.'
9869 */
9870 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9871 s = *fnamep - 2;
9872 else
9873 s = *fnamep + *fnamelen - 1;
9874 for ( ; s > tail; --s)
9875 if (s[0] == '.')
9876 break;
9877 if (src[*usedlen + 1] == 'e') /* :e */
9878 {
9879 if (s > tail)
9880 {
9881 *fnamelen += (int)(*fnamep - (s + 1));
9882 *fnamep = s + 1;
9883#ifdef VMS
9884 /* cut version from the extension */
9885 s = *fnamep + *fnamelen - 1;
9886 for ( ; s > *fnamep; --s)
9887 if (s[0] == ';')
9888 break;
9889 if (s > *fnamep)
9890 *fnamelen = s - *fnamep;
9891#endif
9892 }
9893 else if (*fnamep <= tail)
9894 *fnamelen = 0;
9895 }
9896 else /* :r */
9897 {
9898 if (s > tail) /* remove one extension */
9899 *fnamelen = (int)(s - *fnamep);
9900 }
9901 *usedlen += 2;
9902 }
9903
9904 /* ":s?pat?foo?" - substitute */
9905 /* ":gs?pat?foo?" - global substitute */
9906 if (src[*usedlen] == ':'
9907 && (src[*usedlen + 1] == 's'
9908 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9909 {
9910 char_u *str;
9911 char_u *pat;
9912 char_u *sub;
9913 int sep;
9914 char_u *flags;
9915 int didit = FALSE;
9916
9917 flags = (char_u *)"";
9918 s = src + *usedlen + 2;
9919 if (src[*usedlen + 1] == 'g')
9920 {
9921 flags = (char_u *)"g";
9922 ++s;
9923 }
9924
9925 sep = *s++;
9926 if (sep)
9927 {
9928 /* find end of pattern */
9929 p = vim_strchr(s, sep);
9930 if (p != NULL)
9931 {
9932 pat = vim_strnsave(s, (int)(p - s));
9933 if (pat != NULL)
9934 {
9935 s = p + 1;
9936 /* find end of substitution */
9937 p = vim_strchr(s, sep);
9938 if (p != NULL)
9939 {
9940 sub = vim_strnsave(s, (int)(p - s));
9941 str = vim_strnsave(*fnamep, *fnamelen);
9942 if (sub != NULL && str != NULL)
9943 {
9944 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009945 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (s != NULL)
9947 {
9948 *fnamep = s;
9949 *fnamelen = (int)STRLEN(s);
9950 vim_free(*bufp);
9951 *bufp = s;
9952 didit = TRUE;
9953 }
9954 }
9955 vim_free(sub);
9956 vim_free(str);
9957 }
9958 vim_free(pat);
9959 }
9960 }
9961 /* after using ":s", repeat all the modifiers */
9962 if (didit)
9963 goto repeat;
9964 }
9965 }
9966
Bram Moolenaar26df0922014-02-23 23:39:13 +01009967 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9968 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009969 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009970 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009971 if (c != NUL)
9972 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009973 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009974 if (c != NUL)
9975 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009976 if (p == NULL)
9977 return -1;
9978 vim_free(*bufp);
9979 *bufp = *fnamep = p;
9980 *fnamelen = (int)STRLEN(p);
9981 *usedlen += 2;
9982 }
9983
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 return valid;
9985}
9986
9987/*
9988 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009989 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 * "flags" can be "g" to do a global substitute.
9991 * Returns an allocated string, NULL for error.
9992 */
9993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009994do_string_sub(
9995 char_u *str,
9996 char_u *pat,
9997 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009998 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009999 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001 int sublen;
10002 regmatch_T regmatch;
10003 int i;
10004 int do_all;
10005 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010006 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 garray_T ga;
10008 char_u *ret;
10009 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010010 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
10012 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10013 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010014 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
10016 ga_init2(&ga, 1, 200);
10017
10018 do_all = (flags[0] == 'g');
10019
10020 regmatch.rm_ic = p_ic;
10021 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10022 if (regmatch.regprog != NULL)
10023 {
10024 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010025 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10027 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010028 /* Skip empty match except for first match. */
10029 if (regmatch.startp[0] == regmatch.endp[0])
10030 {
10031 if (zero_width == regmatch.startp[0])
10032 {
10033 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010034 i = MB_PTR2LEN(tail);
10035 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10036 (size_t)i);
10037 ga.ga_len += i;
10038 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010039 continue;
10040 }
10041 zero_width = regmatch.startp[0];
10042 }
10043
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 /*
10045 * Get some space for a temporary buffer to do the substitution
10046 * into. It will contain:
10047 * - The text up to where the match is.
10048 * - The substituted text.
10049 * - The text after the match.
10050 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010051 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010052 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10054 {
10055 ga_clear(&ga);
10056 break;
10057 }
10058
10059 /* copy the text up to where the match is */
10060 i = (int)(regmatch.startp[0] - tail);
10061 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10062 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010063 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 + ga.ga_len + i, TRUE, TRUE, FALSE);
10065 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010066 tail = regmatch.endp[0];
10067 if (*tail == NUL)
10068 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (!do_all)
10070 break;
10071 }
10072
10073 if (ga.ga_data != NULL)
10074 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10075
Bram Moolenaar473de612013-06-08 18:19:48 +020010076 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078
10079 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10080 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010081 if (p_cpo == empty_option)
10082 p_cpo = save_cpo;
10083 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010084 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010085 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
10087 return ret;
10088}
10089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 static int
10091filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10092{
10093 typval_T rettv;
10094 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010095 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010096
10097 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10098 argv[0] = vimvars[VV_KEY].vv_tv;
10099 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010100 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10101 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010102 if (map)
10103 {
10104 /* map(): replace the list item value */
10105 clear_tv(tv);
10106 rettv.v_lock = 0;
10107 *tv = rettv;
10108 }
10109 else
10110 {
10111 int error = FALSE;
10112
10113 /* filter(): when expr is zero remove the item */
10114 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10115 clear_tv(&rettv);
10116 /* On type error, nothing has been removed; return FAIL to stop the
10117 * loop. The error message was given by get_tv_number_chk(). */
10118 if (error)
10119 goto theend;
10120 }
10121 retval = OK;
10122theend:
10123 clear_tv(&vimvars[VV_VAL].vv_tv);
10124 return retval;
10125}
10126
10127
10128/*
10129 * Implementation of map() and filter().
10130 */
10131 void
10132filter_map(typval_T *argvars, typval_T *rettv, int map)
10133{
10134 typval_T *expr;
10135 listitem_T *li, *nli;
10136 list_T *l = NULL;
10137 dictitem_T *di;
10138 hashtab_T *ht;
10139 hashitem_T *hi;
10140 dict_T *d = NULL;
10141 typval_T save_val;
10142 typval_T save_key;
10143 int rem;
10144 int todo;
10145 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10146 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10147 : N_("filter() argument"));
10148 int save_did_emsg;
10149 int idx = 0;
10150
10151 if (argvars[0].v_type == VAR_LIST)
10152 {
10153 if ((l = argvars[0].vval.v_list) == NULL
10154 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10155 return;
10156 }
10157 else if (argvars[0].v_type == VAR_DICT)
10158 {
10159 if ((d = argvars[0].vval.v_dict) == NULL
10160 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10161 return;
10162 }
10163 else
10164 {
10165 EMSG2(_(e_listdictarg), ermsg);
10166 return;
10167 }
10168
10169 expr = &argvars[1];
10170 /* On type errors, the preceding call has already displayed an error
10171 * message. Avoid a misleading error message for an empty string that
10172 * was not passed as argument. */
10173 if (expr->v_type != VAR_UNKNOWN)
10174 {
10175 prepare_vimvar(VV_VAL, &save_val);
10176
10177 /* We reset "did_emsg" to be able to detect whether an error
10178 * occurred during evaluation of the expression. */
10179 save_did_emsg = did_emsg;
10180 did_emsg = FALSE;
10181
10182 prepare_vimvar(VV_KEY, &save_key);
10183 if (argvars[0].v_type == VAR_DICT)
10184 {
10185 vimvars[VV_KEY].vv_type = VAR_STRING;
10186
10187 ht = &d->dv_hashtab;
10188 hash_lock(ht);
10189 todo = (int)ht->ht_used;
10190 for (hi = ht->ht_array; todo > 0; ++hi)
10191 {
10192 if (!HASHITEM_EMPTY(hi))
10193 {
10194 int r;
10195
10196 --todo;
10197 di = HI2DI(hi);
10198 if (map &&
10199 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10200 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10201 break;
10202 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10203 r = filter_map_one(&di->di_tv, expr, map, &rem);
10204 clear_tv(&vimvars[VV_KEY].vv_tv);
10205 if (r == FAIL || did_emsg)
10206 break;
10207 if (!map && rem)
10208 {
10209 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10210 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10211 break;
10212 dictitem_remove(d, di);
10213 }
10214 }
10215 }
10216 hash_unlock(ht);
10217 }
10218 else
10219 {
10220 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10221
10222 for (li = l->lv_first; li != NULL; li = nli)
10223 {
10224 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10225 break;
10226 nli = li->li_next;
10227 vimvars[VV_KEY].vv_nr = idx;
10228 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10229 || did_emsg)
10230 break;
10231 if (!map && rem)
10232 listitem_remove(l, li);
10233 ++idx;
10234 }
10235 }
10236
10237 restore_vimvar(VV_KEY, &save_key);
10238 restore_vimvar(VV_VAL, &save_val);
10239
10240 did_emsg |= save_did_emsg;
10241 }
10242
10243 copy_tv(&argvars[0], rettv);
10244}
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */