blob: b3e2be5d0e396cd3123467dc2709fac6803f02da [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 Moolenaar33570922005-01-25 22:26:29 +0000195};
196
197/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000198#define vv_type vv_di.di_tv.v_type
199#define vv_nr vv_di.di_tv.vval.v_number
200#define vv_float vv_di.di_tv.vval.v_float
201#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000202#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200203#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000204#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000205
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200206static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207#define vimvarht vimvardict.dv_hashtab
208
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100209static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
210static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
211static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100212static void list_glob_vars(int *first);
213static void list_buf_vars(int *first);
214static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100216static void list_vim_vars(int *first);
217static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100218static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
219static 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 +0100220static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
221static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100222static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
223static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
224static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
225static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000226
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static int eval2(char_u **arg, typval_T *rettv, int evaluate);
228static int eval3(char_u **arg, typval_T *rettv, int evaluate);
229static int eval4(char_u **arg, typval_T *rettv, int evaluate);
230static int eval5(char_u **arg, typval_T *rettv, int evaluate);
231static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
232static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000233
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
236static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static 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 +0200241static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static void delete_var(hashtab_T *ht, hashitem_T *hi);
244static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
245static 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 +0100246static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248/* for VIM_VERSION_ defines */
249#include "version.h"
250
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100251
252#if defined(EBCDIC) || defined(PROTO)
253/*
254 * Compare struct fst by function name.
255 */
256 static int
257compare_func_name(const void *s1, const void *s2)
258{
259 struct fst *p1 = (struct fst *)s1;
260 struct fst *p2 = (struct fst *)s2;
261
262 return STRCMP(p1->f_name, p2->f_name);
263}
264
265/*
266 * Sort the function table by function name.
267 * The sorting of the table above is ASCII dependant.
268 * On machines using EBCDIC we have to sort it.
269 */
270 static void
271sortFunctions(void)
272{
273 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
274
275 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
276}
277#endif
278
279
Bram Moolenaar33570922005-01-25 22:26:29 +0000280/*
281 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000282 */
283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100284eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000285{
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 int i;
287 struct vimvar *p;
288
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200289 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
290 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200291 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000292 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200293 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295 for (i = 0; i < VV_LEN; ++i)
296 {
297 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200298 if (STRLEN(p->vv_name) > 16)
299 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100300 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200301 getout(1);
302 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000303 STRCPY(p->vv_di.di_key, p->vv_name);
304 if (p->vv_flags & VV_RO)
305 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
306 else if (p->vv_flags & VV_RO_SBX)
307 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
308 else
309 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000310
311 /* add to v: scope dict, unless the value is not always available */
312 if (p->vv_type != VAR_UNKNOWN)
313 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000314 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000315 /* add to compat scope dict */
316 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000317 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100318 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
319
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100321 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200322 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100323 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100324
325 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
326 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
327 set_vim_var_nr(VV_NONE, VVAL_NONE);
328 set_vim_var_nr(VV_NULL, VVAL_NULL);
329
Bram Moolenaarf562e722016-07-19 17:25:25 +0200330 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
331 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
332 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
333 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
334 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
335 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
336 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
337 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
338 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
339 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
340
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200341 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200342
343#ifdef EBCDIC
344 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100345 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200346 */
347 sortFunctions();
348#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000349}
350
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000351#if defined(EXITFREE) || defined(PROTO)
352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000354{
355 int i;
356 struct vimvar *p;
357
358 for (i = 0; i < VV_LEN; ++i)
359 {
360 p = &vimvars[i];
361 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000362 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000363 vim_free(p->vv_str);
364 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 }
366 else if (p->vv_di.di_tv.v_type == VAR_LIST)
367 {
368 list_unref(p->vv_list);
369 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000370 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000371 }
372 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000373 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374 hash_clear(&compat_hashtab);
375
Bram Moolenaard9fba312005-06-26 22:34:35 +0000376 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100377# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200378 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100379# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000380
381 /* global variables */
382 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000383
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000384 /* autoloaded script names */
385 ga_clear_strings(&ga_loaded);
386
Bram Moolenaarcca74132013-09-25 21:00:28 +0200387 /* Script-local variables. First clear all the variables and in a second
388 * loop free the scriptvar_T, because a variable in one script might hold
389 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200390 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200391 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200392 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200393 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200394 ga_clear(&ga_scripts);
395
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000396 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200397 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000398
399 /* functions */
400 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000401}
402#endif
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 * Set an internal variable to a string value. Creates the variable if it does
407 * not already exist.
408 */
409 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100410set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000412 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000413 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415 val = vim_strsave(value);
416 if (val != NULL)
417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000418 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000419 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000421 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000422 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 }
424 }
425}
426
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000427static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200428#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000429static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000430static char_u *redir_endp = NULL;
431static char_u *redir_varname = NULL;
432
433/*
434 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100435 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000436 * Returns OK if successfully completed the setup. FAIL otherwise.
437 */
438 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100439var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000440{
441 int save_emsg;
442 int err;
443 typval_T tv;
444
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000445 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000446 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000447 {
448 EMSG(_(e_invarg));
449 return FAIL;
450 }
451
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000452 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000453 redir_varname = vim_strsave(name);
454 if (redir_varname == NULL)
455 return FAIL;
456
457 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
458 if (redir_lval == NULL)
459 {
460 var_redir_stop();
461 return FAIL;
462 }
463
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000464 /* The output is stored in growarray "redir_ga" until redirection ends. */
465 ga_init2(&redir_ga, (int)sizeof(char), 500);
466
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100468 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000469 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000470 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
471 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200472 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 if (redir_endp != NULL && *redir_endp != NUL)
474 /* Trailing characters are present after the variable name */
475 EMSG(_(e_trailing));
476 else
477 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000478 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479 var_redir_stop();
480 return FAIL;
481 }
482
483 /* check if we can write to the variable: set it to or append an empty
484 * string */
485 save_emsg = did_emsg;
486 did_emsg = FALSE;
487 tv.v_type = VAR_STRING;
488 tv.vval.v_string = (char_u *)"";
489 if (append)
490 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
491 else
492 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200493 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000495 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 if (err)
497 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000498 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 var_redir_stop();
500 return FAIL;
501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502
503 return OK;
504}
505
506/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000507 * Append "value[value_len]" to the variable set by var_redir_start().
508 * The actual appending is postponed until redirection ends, because the value
509 * appended may in fact be the string we write to, changing it may cause freed
510 * memory to be used:
511 * :redir => foo
512 * :let foo
513 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 */
515 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100516var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000518 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000519
520 if (redir_lval == NULL)
521 return;
522
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000523 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000524 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000526 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000527
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000528 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000529 {
530 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000531 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000532 }
533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000534 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535}
536
537/*
538 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000539 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 */
541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100542var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000544 typval_T tv;
545
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200546 if (EVALCMD_BUSY)
547 {
548 redir_lval = NULL;
549 return;
550 }
551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000552 if (redir_lval != NULL)
553 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000554 /* If there was no error: assign the text to the variable. */
555 if (redir_endp != NULL)
556 {
557 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
558 tv.v_type = VAR_STRING;
559 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200560 /* Call get_lval() again, if it's inside a Dict or List it may
561 * have changed. */
562 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100563 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200564 if (redir_endp != NULL && redir_lval->ll_name != NULL)
565 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
566 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000567 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000569 /* free the collected output */
570 vim_free(redir_ga.ga_data);
571 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000572
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573 vim_free(redir_lval);
574 redir_lval = NULL;
575 }
576 vim_free(redir_varname);
577 redir_varname = NULL;
578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# if defined(FEAT_MBYTE) || defined(PROTO)
581 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100582eval_charconvert(
583 char_u *enc_from,
584 char_u *enc_to,
585 char_u *fname_from,
586 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 int err = FALSE;
589
590 set_vim_var_string(VV_CC_FROM, enc_from, -1);
591 set_vim_var_string(VV_CC_TO, enc_to, -1);
592 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
593 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
594 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
595 err = TRUE;
596 set_vim_var_string(VV_CC_FROM, NULL, -1);
597 set_vim_var_string(VV_CC_TO, NULL, -1);
598 set_vim_var_string(VV_FNAME_IN, NULL, -1);
599 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
600
601 if (err)
602 return FAIL;
603 return OK;
604}
605# endif
606
607# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 int err = FALSE;
612
613 set_vim_var_string(VV_FNAME_IN, fname, -1);
614 set_vim_var_string(VV_CMDARG, args, -1);
615 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
616 err = TRUE;
617 set_vim_var_string(VV_FNAME_IN, NULL, -1);
618 set_vim_var_string(VV_CMDARG, NULL, -1);
619
620 if (err)
621 {
622 mch_remove(fname);
623 return FAIL;
624 }
625 return OK;
626}
627# endif
628
629# if defined(FEAT_DIFF) || defined(PROTO)
630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_diff(
632 char_u *origfile,
633 char_u *newfile,
634 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int err = FALSE;
637
638 set_vim_var_string(VV_FNAME_IN, origfile, -1);
639 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
640 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
641 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
642 set_vim_var_string(VV_FNAME_IN, NULL, -1);
643 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
644 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
645}
646
647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648eval_patch(
649 char_u *origfile,
650 char_u *difffile,
651 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err;
654
655 set_vim_var_string(VV_FNAME_IN, origfile, -1);
656 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
657 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
658 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
661 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
662}
663# endif
664
665/*
666 * Top level evaluation function, returning a boolean.
667 * Sets "error" to TRUE if there was an error.
668 * Return TRUE or FALSE.
669 */
670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_to_bool(
672 char_u *arg,
673 int *error,
674 char_u **nextcmd,
675 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar33570922005-01-25 22:26:29 +0000677 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200678 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 if (skip)
681 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
685 {
686 *error = FALSE;
687 if (!skip)
688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
692 }
693 if (skip)
694 --emsg_skip;
695
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200696 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
Bram Moolenaar48570482017-10-30 21:48:41 +0100699 static int
700eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
701{
702 char_u *s;
703 int dummy;
704 char_u buf[NUMBUFLEN];
705
706 if (expr->v_type == VAR_FUNC)
707 {
708 s = expr->vval.v_string;
709 if (s == NULL || *s == NUL)
710 return FAIL;
711 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
712 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
713 return FAIL;
714 }
715 else if (expr->v_type == VAR_PARTIAL)
716 {
717 partial_T *partial = expr->vval.v_partial;
718
719 s = partial_name(partial);
720 if (s == NULL || *s == NUL)
721 return FAIL;
722 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
723 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
724 return FAIL;
725 }
726 else
727 {
728 s = get_tv_string_buf_chk(expr, buf);
729 if (s == NULL)
730 return FAIL;
731 s = skipwhite(s);
732 if (eval1(&s, rettv, TRUE) == FAIL)
733 return FAIL;
734 if (*s != NUL) /* check for trailing chars after expr */
735 {
736 EMSG2(_(e_invexpr2), s);
737 return FAIL;
738 }
739 }
740 return OK;
741}
742
743/*
744 * Like eval_to_bool() but using a typval_T instead of a string.
745 * Works for string, funcref and partial.
746 */
747 int
748eval_expr_to_bool(typval_T *expr, int *error)
749{
750 typval_T rettv;
751 int res;
752
753 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
754 {
755 *error = TRUE;
756 return FALSE;
757 }
758 res = (get_tv_number_chk(&rettv, error) != 0);
759 clear_tv(&rettv);
760 return res;
761}
762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763/*
764 * Top level evaluation function, returning a string. If "skip" is TRUE,
765 * only parsing to "nextcmd" is done, without reporting errors. Return
766 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
767 */
768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100769eval_to_string_skip(
770 char_u *arg,
771 char_u **nextcmd,
772 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 char_u *retval;
776
777 if (skip)
778 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 retval = NULL;
781 else
782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783 retval = vim_strsave(get_tv_string(&tv));
784 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 if (skip)
787 --emsg_skip;
788
789 return retval;
790}
791
792/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000793 * Skip over an expression at "*pp".
794 * Return FAIL for an error, OK otherwise.
795 */
796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000798{
Bram Moolenaar33570922005-01-25 22:26:29 +0000799 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800
801 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000803}
804
805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000807 * When "convert" is TRUE convert a List into a sequence of lines and convert
808 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 * Return pointer to allocated memory, or NULL for failure.
810 */
811 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100812eval_to_string(
813 char_u *arg,
814 char_u **nextcmd,
815 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000819 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000820#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000821 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 retval = NULL;
826 else
827 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000828 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829 {
830 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000831 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200832 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200833 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200834 if (tv.vval.v_list->lv_len > 0)
835 ga_append(&ga, NL);
836 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837 ga_append(&ga, NUL);
838 retval = (char_u *)ga.ga_data;
839 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000840#ifdef FEAT_FLOAT
841 else if (convert && tv.v_type == VAR_FLOAT)
842 {
843 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
844 retval = vim_strsave(numbuf);
845 }
846#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000847 else
848 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
852 return retval;
853}
854
855/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000856 * Call eval_to_string() without using current local variables and using
857 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 */
859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100860eval_to_string_safe(
861 char_u *arg,
862 char_u **nextcmd,
863 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 char_u *retval;
866 void *save_funccalp;
867
868 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000869 if (use_sandbox)
870 ++sandbox;
871 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000872 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000873 if (use_sandbox)
874 --sandbox;
875 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 restore_funccal(save_funccalp);
877 return retval;
878}
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880/*
881 * Top level evaluation function, returning a number.
882 * Evaluates "expr" silently.
883 * Returns -1 for an error.
884 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200885 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200889 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000890 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 ++emsg_off;
893
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 retval = -1;
896 else
897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000898 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901 --emsg_off;
902
903 return retval;
904}
905
Bram Moolenaara40058a2005-07-11 22:42:07 +0000906/*
907 * Prepare v: variable "idx" to be used.
908 * Save the current typeval in "save_tv".
909 * When not used yet add the variable to the v: hashtable.
910 */
911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000913{
914 *save_tv = vimvars[idx].vv_tv;
915 if (vimvars[idx].vv_type == VAR_UNKNOWN)
916 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
917}
918
919/*
920 * Restore v: variable "idx" to typeval "save_tv".
921 * When no longer defined, remove the variable from the v: hashtable.
922 */
923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100924restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000925{
926 hashitem_T *hi;
927
Bram Moolenaara40058a2005-07-11 22:42:07 +0000928 vimvars[idx].vv_tv = *save_tv;
929 if (vimvars[idx].vv_type == VAR_UNKNOWN)
930 {
931 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
932 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100933 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000934 else
935 hash_remove(&vimvarht, hi);
936 }
937}
938
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000939#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000940/*
941 * Evaluate an expression to a list with suggestions.
942 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000943 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000944 */
945 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000947{
948 typval_T save_val;
949 typval_T rettv;
950 list_T *list = NULL;
951 char_u *p = skipwhite(expr);
952
953 /* Set "v:val" to the bad word. */
954 prepare_vimvar(VV_VAL, &save_val);
955 vimvars[VV_VAL].vv_type = VAR_STRING;
956 vimvars[VV_VAL].vv_str = badword;
957 if (p_verbose == 0)
958 ++emsg_off;
959
960 if (eval1(&p, &rettv, TRUE) == OK)
961 {
962 if (rettv.v_type != VAR_LIST)
963 clear_tv(&rettv);
964 else
965 list = rettv.vval.v_list;
966 }
967
968 if (p_verbose == 0)
969 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000970 restore_vimvar(VV_VAL, &save_val);
971
972 return list;
973}
974
975/*
976 * "list" is supposed to contain two items: a word and a number. Return the
977 * word in "pp" and the number as the return value.
978 * Return -1 if anything isn't right.
979 * Used to get the good word and score from the eval_spell_expr() result.
980 */
981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000983{
984 listitem_T *li;
985
986 li = list->lv_first;
987 if (li == NULL)
988 return -1;
989 *pp = get_tv_string(&li->li_tv);
990
991 li = li->li_next;
992 if (li == NULL)
993 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200994 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000995}
996#endif
997
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000999 * Top level evaluation function.
1000 * Returns an allocated typval_T with the result.
1001 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001002 */
1003 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001005{
1006 typval_T *tv;
1007
1008 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001009 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001010 {
1011 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001013 }
1014
1015 return tv;
1016}
1017
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001020 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001021 * Uses argv[argc] for the function arguments. Only Number and String
1022 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001023 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026call_vim_function(
1027 char_u *func,
1028 int argc,
1029 char_u **argv,
1030 int safe, /* use the sandbox */
1031 int str_arg_only, /* all arguments are strings */
1032 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001035 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int len;
1037 int i;
1038 int doesrange;
1039 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001040 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001042 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001044 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 for (i = 0; i < argc; i++)
1047 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001048 /* Pass a NULL or empty argument as an empty string */
1049 if (argv[i] == NULL || *argv[i] == NUL)
1050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001051 argvars[i].v_type = VAR_STRING;
1052 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001053 continue;
1054 }
1055
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001056 if (str_arg_only)
1057 len = 0;
1058 else
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001059 {
1060 /* Recognize a number argument, the others must be strings. A dash
1061 * is a string too. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001062 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaarffd99f72017-11-02 15:44:14 +01001063 if (len == 1 && *argv[i] == '-')
1064 len = 0;
1065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (len != 0 && len == (int)STRLEN(argv[i]))
1067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001068 argvars[i].v_type = VAR_NUMBER;
1069 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
1071 else
1072 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 argvars[i].v_type = VAR_STRING;
1074 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 }
1077
1078 if (safe)
1079 {
1080 save_funccalp = save_funccal();
1081 ++sandbox;
1082 }
1083
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001084 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001085 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001087 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (safe)
1089 {
1090 --sandbox;
1091 restore_funccal(save_funccalp);
1092 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001093 vim_free(argvars);
1094
1095 if (ret == FAIL)
1096 clear_tv(rettv);
1097
1098 return ret;
1099}
1100
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001101/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001102 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001103 * Returns -1 when calling the function fails.
1104 * Uses argv[argc] for the function arguments.
1105 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001106 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001107call_func_retnr(
1108 char_u *func,
1109 int argc,
1110 char_u **argv,
1111 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001112{
1113 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001114 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001115
1116 /* All arguments are passed as strings, no conversion to number. */
1117 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1118 return -1;
1119
1120 retval = get_tv_number_chk(&rettv, NULL);
1121 clear_tv(&rettv);
1122 return retval;
1123}
1124
1125#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1126 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1127
Bram Moolenaar4f688582007-07-24 12:34:30 +00001128# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001129/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001130 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001131 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001132 * Uses argv[argc] for the function arguments.
1133 */
1134 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001135call_func_retstr(
1136 char_u *func,
1137 int argc,
1138 char_u **argv,
1139 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001140{
1141 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001142 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001143
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001144 /* All arguments are passed as strings, no conversion to number. */
1145 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146 return NULL;
1147
1148 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001149 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 return retval;
1151}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001152# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001153
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001154/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001155 * Call Vim script function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001157 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158 */
1159 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001160call_func_retlist(
1161 char_u *func,
1162 int argc,
1163 char_u **argv,
1164 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165{
1166 typval_T rettv;
1167
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001168 /* All arguments are passed as strings, no conversion to number. */
1169 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001170 return NULL;
1171
1172 if (rettv.v_type != VAR_LIST)
1173 {
1174 clear_tv(&rettv);
1175 return NULL;
1176 }
1177
1178 return rettv.vval.v_list;
1179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181
Bram Moolenaar05159a02005-02-26 23:04:13 +00001182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#ifdef FEAT_FOLDING
1184/*
1185 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1186 * it in "*cp". Doesn't give error messages.
1187 */
1188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001189eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001192 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001194 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1195 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001198 if (use_sandbox)
1199 ++sandbox;
1200 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 retval = 0;
1204 else
1205 {
1206 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (tv.v_type == VAR_NUMBER)
1208 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001209 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = 0;
1211 else
1212 {
1213 /* If the result is a string, check if there is a non-digit before
1214 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (!VIM_ISDIGIT(*s) && *s != '-')
1217 *cp = *s++;
1218 retval = atol((char *)s);
1219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001223 if (use_sandbox)
1224 --sandbox;
1225 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001227 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228}
1229#endif
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 * ":let" list all variable values
1233 * ":let var1 var2" list variable values
1234 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001235 * ":let var += expr" assignment command.
1236 * ":let var -= expr" assignment command.
1237 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
1240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001245 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247 int var_count = 0;
1248 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001250 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001251 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaardb552d602006-03-23 22:59:57 +00001253 argend = skip_var_list(arg, &var_count, &semicolon);
1254 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001255 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001256 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1257 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001258 expr = skipwhite(argend);
1259 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1260 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001262 /*
1263 * ":let" without "=": list variables
1264 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001265 if (*arg == '[')
1266 EMSG(_(e_invarg));
1267 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001269 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001273 list_glob_vars(&first);
1274 list_buf_vars(&first);
1275 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001276 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001277 list_script_vars(&first);
1278 list_func_vars(&first);
1279 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 eap->nextcmd = check_nextcmd(arg);
1282 }
1283 else
1284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 op[0] = '=';
1286 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001287 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001289 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1290 op[0] = *expr; /* +=, -= or .= */
1291 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001292 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001293 else
1294 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (eap->skip)
1297 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001298 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (eap->skip)
1300 {
1301 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 --emsg_skip;
1304 }
1305 else if (i != FAIL)
1306 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001307 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001308 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312}
1313
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001314/*
1315 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1316 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 * When "nextchars" is not NULL it points to a string with characters that
1318 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1319 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001320 * Returns OK or FAIL;
1321 */
1322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001323ex_let_vars(
1324 char_u *arg_start,
1325 typval_T *tv,
1326 int copy, /* copy values from "tv", don't move */
1327 int semicolon, /* from skip_var_list() */
1328 int var_count, /* from skip_var_list() */
1329 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001330{
1331 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001333 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 listitem_T *item;
1335 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001336
1337 if (*arg != '[')
1338 {
1339 /*
1340 * ":let var = expr" or ":for var in list"
1341 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001342 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001343 return FAIL;
1344 return OK;
1345 }
1346
1347 /*
1348 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1349 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001350 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001351 {
1352 EMSG(_(e_listreq));
1353 return FAIL;
1354 }
1355
1356 i = list_len(l);
1357 if (semicolon == 0 && var_count < i)
1358 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001359 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001360 return FAIL;
1361 }
1362 if (var_count - semicolon > i)
1363 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001364 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001365 return FAIL;
1366 }
1367
1368 item = l->lv_first;
1369 while (*arg != ']')
1370 {
1371 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001373 item = item->li_next;
1374 if (arg == NULL)
1375 return FAIL;
1376
1377 arg = skipwhite(arg);
1378 if (*arg == ';')
1379 {
1380 /* Put the rest of the list (may be empty) in the var after ';'.
1381 * Create a new list for this. */
1382 l = list_alloc();
1383 if (l == NULL)
1384 return FAIL;
1385 while (item != NULL)
1386 {
1387 list_append_tv(l, &item->li_tv);
1388 item = item->li_next;
1389 }
1390
1391 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001392 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 ltv.vval.v_list = l;
1394 l->lv_refcount = 1;
1395
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1397 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001398 clear_tv(&ltv);
1399 if (arg == NULL)
1400 return FAIL;
1401 break;
1402 }
1403 else if (*arg != ',' && *arg != ']')
1404 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001405 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001406 return FAIL;
1407 }
1408 }
1409
1410 return OK;
1411}
1412
1413/*
1414 * Skip over assignable variable "var" or list of variables "[var, var]".
1415 * Used for ":let varvar = expr" and ":for varvar in expr".
1416 * For "[var, var]" increment "*var_count" for each variable.
1417 * for "[var, var; var]" set "semicolon".
1418 * Return NULL for an error.
1419 */
1420 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001421skip_var_list(
1422 char_u *arg,
1423 int *var_count,
1424 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425{
1426 char_u *p, *s;
1427
1428 if (*arg == '[')
1429 {
1430 /* "[var, var]": find the matching ']'. */
1431 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 {
1434 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1435 s = skip_var_one(p);
1436 if (s == p)
1437 {
1438 EMSG2(_(e_invarg2), p);
1439 return NULL;
1440 }
1441 ++*var_count;
1442
1443 p = skipwhite(s);
1444 if (*p == ']')
1445 break;
1446 else if (*p == ';')
1447 {
1448 if (*semicolon == 1)
1449 {
1450 EMSG(_("Double ; in list of variables"));
1451 return NULL;
1452 }
1453 *semicolon = 1;
1454 }
1455 else if (*p != ',')
1456 {
1457 EMSG2(_(e_invarg2), p);
1458 return NULL;
1459 }
1460 }
1461 return p + 1;
1462 }
1463 else
1464 return skip_var_one(arg);
1465}
1466
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001467/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001468 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001469 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001470 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001473{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001474 if (*arg == '@' && arg[1] != NUL)
1475 return arg + 2;
1476 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1477 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001478}
1479
Bram Moolenaara7043832005-01-21 11:56:39 +00001480/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001481 * List variables for hashtab "ht" with prefix "prefix".
1482 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001483 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001485list_hashtable_vars(
1486 hashtab_T *ht,
1487 char_u *prefix,
1488 int empty,
1489 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001490{
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 hashitem_T *hi;
1492 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001493 int todo;
1494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001495 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001496 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1497 {
1498 if (!HASHITEM_EMPTY(hi))
1499 {
1500 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 di = HI2DI(hi);
1502 if (empty || di->di_tv.v_type != VAR_STRING
1503 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001504 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001505 }
1506 }
1507}
1508
1509/*
1510 * List global variables.
1511 */
1512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001513list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001514{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001515 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001516}
1517
1518/*
1519 * List buffer variables.
1520 */
1521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001523{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001524 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001525 TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001526}
1527
1528/*
1529 * List window variables.
1530 */
1531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001532list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001533{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001534 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001535 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001536}
1537
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001538/*
1539 * List tab page variables.
1540 */
1541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001542list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001543{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001544 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001545 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001546}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001547
Bram Moolenaara7043832005-01-21 11:56:39 +00001548/*
1549 * List Vim variables.
1550 */
1551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001554 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001555}
1556
1557/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001558 * List script-local variables, if there is a script.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001562{
1563 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001564 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1565 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001566}
1567
1568/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 * List variables in "arg".
1570 */
1571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573{
1574 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001577 char_u *name_start;
1578 char_u *arg_subsc;
1579 char_u *tofree;
1580 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581
1582 while (!ends_excmd(*arg) && !got_int)
1583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001584 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001586 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001587 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 {
1589 emsg_severe = TRUE;
1590 EMSG(_(e_trailing));
1591 break;
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001596 /* get_name_len() takes care of expanding curly braces */
1597 name_start = name = arg;
1598 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1599 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001601 /* This is mainly to keep test 49 working: when expanding
1602 * curly braces fails overrule the exception error message. */
1603 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001605 emsg_severe = TRUE;
1606 EMSG2(_(e_invarg2), arg);
1607 break;
1608 }
1609 error = TRUE;
1610 }
1611 else
1612 {
1613 if (tofree != NULL)
1614 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001615 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 else
1618 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001619 /* handle d.key, l[idx], f(expr) */
1620 arg_subsc = arg;
1621 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001622 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001624 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001625 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001626 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001628 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001629 case 'g': list_glob_vars(first); break;
1630 case 'b': list_buf_vars(first); break;
1631 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001632 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001633 case 'v': list_vim_vars(first); break;
1634 case 's': list_script_vars(first); break;
1635 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636 default:
1637 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001638 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001639 }
1640 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001641 {
1642 char_u numbuf[NUMBUFLEN];
1643 char_u *tf;
1644 int c;
1645 char_u *s;
1646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001647 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001648 c = *arg;
1649 *arg = NUL;
1650 list_one_var_a((char_u *)"",
1651 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001652 tv.v_type,
1653 s == NULL ? (char_u *)"" : s,
1654 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 *arg = c;
1656 vim_free(tf);
1657 }
1658 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 }
1661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001662
1663 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001665
1666 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001667 }
1668
1669 return arg;
1670}
1671
1672/*
1673 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1674 * Returns a pointer to the char just after the var name.
1675 * Returns NULL if there is an error.
1676 */
1677 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001678ex_let_one(
1679 char_u *arg, /* points to variable name */
1680 typval_T *tv, /* value to assign to variable */
1681 int copy, /* copy value from "tv" */
1682 char_u *endchars, /* valid chars after variable name or NULL */
1683 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684{
1685 int c1;
1686 char_u *name;
1687 char_u *p;
1688 char_u *arg_end = NULL;
1689 int len;
1690 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692
1693 /*
1694 * ":let $VAR = expr": Set environment variable.
1695 */
1696 if (*arg == '$')
1697 {
1698 /* Find the end of the name. */
1699 ++arg;
1700 name = arg;
1701 len = get_env_len(&arg);
1702 if (len == 0)
1703 EMSG2(_(e_invarg2), name - 1);
1704 else
1705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 if (op != NULL && (*op == '+' || *op == '-'))
1707 EMSG2(_(e_letwrong), op);
1708 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001710 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001711 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 {
1713 c1 = name[len];
1714 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001715 p = get_tv_string_chk(tv);
1716 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 {
1718 int mustfree = FALSE;
1719 char_u *s = vim_getenv(name, &mustfree);
1720
1721 if (s != NULL)
1722 {
1723 p = tofree = concat_str(s, p);
1724 if (mustfree)
1725 vim_free(s);
1726 }
1727 }
1728 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001729 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001731 if (STRICMP(name, "HOME") == 0)
1732 init_homedir();
1733 else if (didset_vim && STRICMP(name, "VIM") == 0)
1734 didset_vim = FALSE;
1735 else if (didset_vimruntime
1736 && STRICMP(name, "VIMRUNTIME") == 0)
1737 didset_vimruntime = FALSE;
1738 arg_end = arg;
1739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 }
1743 }
1744 }
1745
1746 /*
1747 * ":let &option = expr": Set option value.
1748 * ":let &l:option = expr": Set local option value.
1749 * ":let &g:option = expr": Set global option value.
1750 */
1751 else if (*arg == '&')
1752 {
1753 /* Find the end of the name. */
1754 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 if (p == NULL || (endchars != NULL
1756 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 EMSG(_(e_letunexp));
1758 else
1759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 long n;
1761 int opt_type;
1762 long numval;
1763 char_u *stringval = NULL;
1764 char_u *s;
1765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766 c1 = *p;
1767 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001769 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770 s = get_tv_string_chk(tv); /* != NULL if number or string */
1771 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 {
1773 opt_type = get_option_value(arg, &numval,
1774 &stringval, opt_flags);
1775 if ((opt_type == 1 && *op == '.')
1776 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 EMSG2(_(e_letwrong), op);
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001779 s = NULL; /* don't set the value */
1780 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 else
1782 {
1783 if (opt_type == 1) /* number */
1784 {
1785 if (*op == '+')
1786 n = numval + n;
1787 else
1788 n = numval - n;
1789 }
1790 else if (opt_type == 0 && stringval != NULL) /* string */
1791 {
1792 s = concat_str(stringval, s);
1793 vim_free(stringval);
1794 stringval = s;
1795 }
1796 }
1797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001798 if (s != NULL)
1799 {
1800 set_option_value(arg, n, s, opt_flags);
1801 arg_end = p;
1802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 }
1806 }
1807
1808 /*
1809 * ":let @r = expr": Set register contents.
1810 */
1811 else if (*arg == '@')
1812 {
1813 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814 if (op != NULL && (*op == '+' || *op == '-'))
1815 EMSG2(_(e_letwrong), op);
1816 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 EMSG(_(e_letunexp));
1819 else
1820 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001821 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 char_u *s;
1823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001824 p = get_tv_string_chk(tv);
1825 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001827 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 if (s != NULL)
1829 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001830 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 vim_free(s);
1832 }
1833 }
1834 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001835 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001837 arg_end = arg + 1;
1838 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001839 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 }
1841 }
1842
1843 /*
1844 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001845 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001847 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001851 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1855 EMSG(_(e_letunexp));
1856 else
1857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001858 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001859 arg_end = p;
1860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 }
1864
1865 else
1866 EMSG2(_(e_invarg2), arg);
1867
1868 return arg_end;
1869}
1870
1871/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001872 * Get an lval: variable, Dict item or List item that can be assigned a value
1873 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1874 * "name.key", "name.key[expr]" etc.
1875 * Indexing only works if "name" is an existing List or Dictionary.
1876 * "name" points to the start of the name.
1877 * If "rettv" is not NULL it points to the value to be assigned.
1878 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1879 * wrong; must end in space or cmd separator.
1880 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001881 * flags:
1882 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001883 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001884 * GLV_NO_AUTOLOAD: do not use script autoloading
1885 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891get_lval(
1892 char_u *name,
1893 typval_T *rettv,
1894 lval_T *lp,
1895 int unlet,
1896 int skip,
1897 int flags, /* GLV_ values */
1898 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001901 char_u *expr_start, *expr_end;
1902 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 dictitem_T *v;
1904 typval_T var1;
1905 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001908 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001909 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001911 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001915
1916 if (skip)
1917 {
1918 /* When skipping just find the end of the name. */
1919 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001920 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 }
1922
1923 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001924 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 if (expr_start != NULL)
1926 {
1927 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001928 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 && *p != '[' && *p != '.')
1930 {
1931 EMSG(_(e_trailing));
1932 return NULL;
1933 }
1934
1935 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1936 if (lp->ll_exp_name == NULL)
1937 {
1938 /* Report an invalid expression in braces, unless the
1939 * expression evaluation has been cancelled due to an
1940 * aborting error, an interrupt, or an exception. */
1941 if (!aborting() && !quiet)
1942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001943 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 EMSG2(_(e_invarg2), name);
1945 return NULL;
1946 }
1947 }
1948 lp->ll_name = lp->ll_exp_name;
1949 }
1950 else
1951 lp->ll_name = name;
1952
1953 /* Without [idx] or .key we are done. */
1954 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1955 return p;
1956
1957 cc = *p;
1958 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001959 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 if (v == NULL && !quiet)
1961 EMSG2(_(e_undefvar), lp->ll_name);
1962 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 if (v == NULL)
1964 return NULL;
1965
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966 /*
1967 * Loop until no more [idx] or .key is following.
1968 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001970 var1.v_type = VAR_UNKNOWN;
1971 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1975 && !(lp->ll_tv->v_type == VAR_DICT
1976 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001978 if (!quiet)
1979 EMSG(_("E689: Can only index a List or Dictionary"));
1980 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 if (!quiet)
1985 EMSG(_("E708: [:] must come last"));
1986 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001988
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 len = -1;
1990 if (*p == '.')
1991 {
1992 key = p + 1;
1993 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1994 ;
1995 if (len == 0)
1996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 if (!quiet)
1998 EMSG(_(e_emptykey));
1999 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002000 }
2001 p = key + len;
2002 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002003 else
2004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002005 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002006 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002007 if (*p == ':')
2008 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002009 else
2010 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002011 empty1 = FALSE;
2012 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002014 if (get_tv_string_chk(&var1) == NULL)
2015 {
2016 /* not a number or string */
2017 clear_tv(&var1);
2018 return NULL;
2019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002020 }
2021
2022 /* Optionally get the second index [ :expr]. */
2023 if (*p == ':')
2024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002026 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 EMSG(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002029 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (rettv != NULL && (rettv->v_type != VAR_LIST
2033 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035 if (!quiet)
2036 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002037 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 }
2040 p = skipwhite(p + 1);
2041 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002043 else
2044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002046 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2047 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002048 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002051 if (get_tv_string_chk(&var2) == NULL)
2052 {
2053 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002054 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002055 clear_tv(&var2);
2056 return NULL;
2057 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002063
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 if (!quiet)
2067 EMSG(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002068 clear_tv(&var1);
2069 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002071 }
2072
2073 /* Skip to past ']'. */
2074 ++p;
2075 }
2076
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002078 {
2079 if (len == -1)
2080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002082 key = get_tv_string_chk(&var1); /* is number or string */
2083 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002087 }
2088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 lp->ll_list = NULL;
2090 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002091 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002092
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002093 /* When assigning to a scope dictionary check that a function and
2094 * variable name is valid (only variable name unless it is l: or
2095 * g: dictionary). Disallow overwriting a builtin function. */
2096 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002097 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002098 int prevval;
2099 int wrong;
2100
2101 if (len != -1)
2102 {
2103 prevval = key[len];
2104 key[len] = NUL;
2105 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002106 else
2107 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002108 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2109 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002110 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002111 || !valid_varname(key);
2112 if (len != -1)
2113 key[len] = prevval;
2114 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002115 return NULL;
2116 }
2117
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002120 /* Can't add "v:" variable. */
2121 if (lp->ll_dict == &vimvardict)
2122 {
2123 EMSG2(_(e_illvar), name);
2124 return NULL;
2125 }
2126
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002127 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002131 EMSG2(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002132 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002134 }
2135 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002139 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002141 p = NULL;
2142 break;
2143 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002144 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002145 else if ((flags & GLV_READ_ONLY) == 0
2146 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002147 {
2148 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002149 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002150 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002151
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002152 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002154 }
2155 else
2156 {
2157 /*
2158 * Get the number and item for the only or first index of the List.
2159 */
2160 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002162 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002163 /* is number or string */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002164 lp->ll_n1 = (long)get_tv_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002165 clear_tv(&var1);
2166
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 lp->ll_list = lp->ll_tv->vval.v_list;
2169 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2170 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002172 if (lp->ll_n1 < 0)
2173 {
2174 lp->ll_n1 = 0;
2175 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2176 }
2177 }
2178 if (lp->ll_li == NULL)
2179 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002180 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002181 if (!quiet)
2182 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002184 }
2185
2186 /*
2187 * May need to find the item or absolute index for the second
2188 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 * When no index given: "lp->ll_empty2" is TRUE.
2190 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002191 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002194 lp->ll_n2 = (long)get_tv_number(&var2);
2195 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002196 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002198 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002201 {
2202 if (!quiet)
2203 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002205 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 }
2208
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2210 if (lp->ll_n1 < 0)
2211 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2212 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002213 {
2214 if (!quiet)
2215 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002217 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002218 }
2219
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
2223
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002224 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 return p;
2226}
2227
2228/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002229 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002232clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233{
2234 vim_free(lp->ll_exp_name);
2235 vim_free(lp->ll_newkey);
2236}
2237
2238/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002239 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 */
2243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244set_var_lval(
2245 lval_T *lp,
2246 char_u *endp,
2247 typval_T *rettv,
2248 int copy,
2249 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250{
2251 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 listitem_T *ri;
2253 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254
2255 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002257 cc = *endp;
2258 *endp = NUL;
2259 if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002261 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002262
Bram Moolenaar79518e22017-02-17 16:31:35 +01002263 /* handle +=, -= and .= */
2264 di = NULL;
2265 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2266 &tv, &di, TRUE, FALSE) == OK)
2267 {
2268 if ((di == NULL
2269 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2270 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2271 FALSE)))
2272 && tv_op(&tv, rettv, op) == OK)
2273 set_var(lp->ll_name, &tv, FALSE);
2274 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002277 else
2278 set_var(lp->ll_name, rettv, copy);
2279 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002281 else if (tv_check_lock(lp->ll_newkey == NULL
2282 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002283 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002284 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 else if (lp->ll_range)
2286 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002287 listitem_T *ll_li = lp->ll_li;
2288 int ll_n1 = lp->ll_n1;
2289
2290 /*
2291 * Check whether any of the list items is locked
2292 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002293 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002294 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002295 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002296 return;
2297 ri = ri->li_next;
2298 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2299 break;
2300 ll_li = ll_li->li_next;
2301 ++ll_n1;
2302 }
2303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 /*
2305 * Assign the List values to the list items.
2306 */
2307 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002308 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 if (op != NULL && *op != '=')
2310 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2311 else
2312 {
2313 clear_tv(&lp->ll_li->li_tv);
2314 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2315 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 ri = ri->li_next;
2317 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2318 break;
2319 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002320 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002322 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002323 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 ri = NULL;
2325 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002326 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002327 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 lp->ll_li = lp->ll_li->li_next;
2329 ++lp->ll_n1;
2330 }
2331 if (ri != NULL)
2332 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 else if (lp->ll_empty2
2334 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 : lp->ll_n1 != lp->ll_n2)
2336 EMSG(_("E711: List value has not enough items"));
2337 }
2338 else
2339 {
2340 /*
2341 * Assign to a List or Dictionary item.
2342 */
2343 if (lp->ll_newkey != NULL)
2344 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 if (op != NULL && *op != '=')
2346 {
2347 EMSG2(_(e_letwrong), op);
2348 return;
2349 }
2350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002352 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 if (di == NULL)
2354 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002355 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2356 {
2357 vim_free(di);
2358 return;
2359 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002361 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 else if (op != NULL && *op != '=')
2363 {
2364 tv_op(lp->ll_tv, rettv, op);
2365 return;
2366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 /*
2371 * Assign the value to the variable or list item.
2372 */
2373 if (copy)
2374 copy_tv(rettv, lp->ll_tv);
2375 else
2376 {
2377 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002378 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 }
2381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382}
2383
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2386 * Returns OK or FAIL.
2387 */
2388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002389tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002391 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 char_u numbuf[NUMBUFLEN];
2393 char_u *s;
2394
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002395 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2396 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2397 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 {
2399 switch (tv1->v_type)
2400 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002401 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 case VAR_DICT:
2403 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002404 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002405 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002406 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002407 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 break;
2409
2410 case VAR_LIST:
2411 if (*op != '+' || tv2->v_type != VAR_LIST)
2412 break;
2413 /* List += List */
2414 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2415 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2416 return OK;
2417
2418 case VAR_NUMBER:
2419 case VAR_STRING:
2420 if (tv2->v_type == VAR_LIST)
2421 break;
2422 if (*op == '+' || *op == '-')
2423 {
2424 /* nr += nr or nr -= nr*/
2425 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002426#ifdef FEAT_FLOAT
2427 if (tv2->v_type == VAR_FLOAT)
2428 {
2429 float_T f = n;
2430
2431 if (*op == '+')
2432 f += tv2->vval.v_float;
2433 else
2434 f -= tv2->vval.v_float;
2435 clear_tv(tv1);
2436 tv1->v_type = VAR_FLOAT;
2437 tv1->vval.v_float = f;
2438 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002440#endif
2441 {
2442 if (*op == '+')
2443 n += get_tv_number(tv2);
2444 else
2445 n -= get_tv_number(tv2);
2446 clear_tv(tv1);
2447 tv1->v_type = VAR_NUMBER;
2448 tv1->vval.v_number = n;
2449 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 }
2451 else
2452 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002453 if (tv2->v_type == VAR_FLOAT)
2454 break;
2455
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 /* str .= str */
2457 s = get_tv_string(tv1);
2458 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2459 clear_tv(tv1);
2460 tv1->v_type = VAR_STRING;
2461 tv1->vval.v_string = s;
2462 }
2463 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002464
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002465 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002466#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467 {
2468 float_T f;
2469
2470 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2471 && tv2->v_type != VAR_NUMBER
2472 && tv2->v_type != VAR_STRING))
2473 break;
2474 if (tv2->v_type == VAR_FLOAT)
2475 f = tv2->vval.v_float;
2476 else
2477 f = get_tv_number(tv2);
2478 if (*op == '+')
2479 tv1->vval.v_float += f;
2480 else
2481 tv1->vval.v_float -= f;
2482 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002483#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002484 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 }
2486 }
2487
2488 EMSG2(_(e_letwrong), op);
2489 return FAIL;
2490}
2491
2492/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002493 * Evaluate the expression used in a ":for var in expr" command.
2494 * "arg" points to "var".
2495 * Set "*errp" to TRUE for an error, FALSE otherwise;
2496 * Return a pointer that holds the info. Null when there is an error.
2497 */
2498 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499eval_for_line(
2500 char_u *arg,
2501 int *errp,
2502 char_u **nextcmdp,
2503 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504{
Bram Moolenaar33570922005-01-25 22:26:29 +00002505 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002507 typval_T tv;
2508 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002509
2510 *errp = TRUE; /* default: there is an error */
2511
Bram Moolenaar33570922005-01-25 22:26:29 +00002512 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002513 if (fi == NULL)
2514 return NULL;
2515
2516 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2517 if (expr == NULL)
2518 return fi;
2519
2520 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002521 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002522 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002523 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524 return fi;
2525 }
2526
2527 if (skip)
2528 ++emsg_skip;
2529 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2530 {
2531 *errp = FALSE;
2532 if (!skip)
2533 {
2534 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002535 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002536 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002537 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002538 clear_tv(&tv);
2539 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002540 else if (l == NULL)
2541 {
2542 /* a null list is like an empty list: do nothing */
2543 clear_tv(&tv);
2544 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545 else
2546 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002547 /* No need to increment the refcount, it's already set for the
2548 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002549 fi->fi_list = l;
2550 list_add_watch(l, &fi->fi_lw);
2551 fi->fi_lw.lw_item = l->lv_first;
2552 }
2553 }
2554 }
2555 if (skip)
2556 --emsg_skip;
2557
2558 return fi;
2559}
2560
2561/*
2562 * Use the first item in a ":for" list. Advance to the next.
2563 * Assign the values to the variable (list). "arg" points to the first one.
2564 * Return TRUE when a valid item was found, FALSE when at end of list or
2565 * something wrong.
2566 */
2567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002569{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002570 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002571 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573
2574 item = fi->fi_lw.lw_item;
2575 if (item == NULL)
2576 result = FALSE;
2577 else
2578 {
2579 fi->fi_lw.lw_item = item->li_next;
2580 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2581 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2582 }
2583 return result;
2584}
2585
2586/*
2587 * Free the structure used to store info used by ":for".
2588 */
2589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002590free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002591{
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002593
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002594 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002595 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002596 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002597 list_unref(fi->fi_list);
2598 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002599 vim_free(fi);
2600}
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2603
2604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002605set_context_for_expression(
2606 expand_T *xp,
2607 char_u *arg,
2608 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 int got_eq = FALSE;
2611 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002612 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002614 if (cmdidx == CMD_let)
2615 {
2616 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002617 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002618 {
2619 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002620 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002621 {
2622 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002623 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002624 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002625 break;
2626 }
2627 return;
2628 }
2629 }
2630 else
2631 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2632 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 while ((xp->xp_pattern = vim_strpbrk(arg,
2634 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2635 {
2636 c = *xp->xp_pattern;
2637 if (c == '&')
2638 {
2639 c = xp->xp_pattern[1];
2640 if (c == '&')
2641 {
2642 ++xp->xp_pattern;
2643 xp->xp_context = cmdidx != CMD_let || got_eq
2644 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2645 }
2646 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002649 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2650 xp->xp_pattern += 2;
2651
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654 else if (c == '$')
2655 {
2656 /* environment variable */
2657 xp->xp_context = EXPAND_ENV_VARS;
2658 }
2659 else if (c == '=')
2660 {
2661 got_eq = TRUE;
2662 xp->xp_context = EXPAND_EXPRESSION;
2663 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002664 else if (c == '#'
2665 && xp->xp_context == EXPAND_EXPRESSION)
2666 {
2667 /* Autoload function/variable contains '#'. */
2668 break;
2669 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002670 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 && xp->xp_context == EXPAND_FUNCTIONS
2672 && vim_strchr(xp->xp_pattern, '(') == NULL)
2673 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002674 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 break;
2676 }
2677 else if (cmdidx != CMD_let || got_eq)
2678 {
2679 if (c == '"') /* string */
2680 {
2681 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2682 if (c == '\\' && xp->xp_pattern[1] != NUL)
2683 ++xp->xp_pattern;
2684 xp->xp_context = EXPAND_NOTHING;
2685 }
2686 else if (c == '\'') /* literal string */
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2690 /* skip */ ;
2691 xp->xp_context = EXPAND_NOTHING;
2692 }
2693 else if (c == '|')
2694 {
2695 if (xp->xp_pattern[1] == '|')
2696 {
2697 ++xp->xp_pattern;
2698 xp->xp_context = EXPAND_EXPRESSION;
2699 }
2700 else
2701 xp->xp_context = EXPAND_COMMANDS;
2702 }
2703 else
2704 xp->xp_context = EXPAND_EXPRESSION;
2705 }
2706 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707 /* Doesn't look like something valid, expand as an expression
2708 * anyway. */
2709 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 arg = xp->xp_pattern;
2711 if (*arg != NUL)
2712 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2713 /* skip */ ;
2714 }
2715 xp->xp_pattern = arg;
2716}
2717
2718#endif /* FEAT_CMDL_COMPL */
2719
2720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 * ":unlet[!] var1 ... " command.
2722 */
2723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002724ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002726 ex_unletlock(eap, eap->arg, 0);
2727}
2728
2729/*
2730 * ":lockvar" and ":unlockvar" commands
2731 */
2732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002733ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002734{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002736 int deep = 2;
2737
2738 if (eap->forceit)
2739 deep = -1;
2740 else if (vim_isdigit(*arg))
2741 {
2742 deep = getdigits(&arg);
2743 arg = skipwhite(arg);
2744 }
2745
2746 ex_unletlock(eap, arg, deep);
2747}
2748
2749/*
2750 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2751 */
2752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002753ex_unletlock(
2754 exarg_T *eap,
2755 char_u *argstart,
2756 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002757{
2758 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002761 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 do
2764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002766 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002767 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lv.ll_name == NULL)
2769 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002770 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (name_end != NULL)
2774 {
2775 emsg_severe = TRUE;
2776 EMSG(_(e_trailing));
2777 }
2778 if (!(eap->skip || error))
2779 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 break;
2781 }
2782
2783 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002784 {
2785 if (eap->cmdidx == CMD_unlet)
2786 {
2787 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2788 error = TRUE;
2789 }
2790 else
2791 {
2792 if (do_lock_var(&lv, name_end, deep,
2793 eap->cmdidx == CMD_lockvar) == FAIL)
2794 error = TRUE;
2795 }
2796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (!eap->skip)
2799 clear_lval(&lv);
2800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 arg = skipwhite(name_end);
2802 } while (!ends_excmd(*arg));
2803
2804 eap->nextcmd = check_nextcmd(arg);
2805}
2806
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002808do_unlet_var(
2809 lval_T *lp,
2810 char_u *name_end,
2811 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 int ret = OK;
2814 int cc;
2815
2816 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 cc = *name_end;
2819 *name_end = NUL;
2820
2821 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002822 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002826 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002827 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002828 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002829 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002830 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 else if (lp->ll_range)
2832 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002834 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002835 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002836
2837 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2838 {
2839 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002840 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002841 return FAIL;
2842 ll_li = li;
2843 ++ll_n1;
2844 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845
2846 /* Delete a range of List items. */
2847 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2848 {
2849 li = lp->ll_li->li_next;
2850 listitem_remove(lp->ll_list, lp->ll_li);
2851 lp->ll_li = li;
2852 ++lp->ll_n1;
2853 }
2854 }
2855 else
2856 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 /* unlet a List item. */
2859 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002862 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 }
2864
2865 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866}
2867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868/*
2869 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002870 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
2872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002873do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 hashtab_T *ht;
2876 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002877 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002878 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002879 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 ht = find_var_ht(name, &varname);
2882 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002885 if (d == NULL)
2886 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 if (ht == &globvarht)
2888 d = &globvardict;
2889 else if (ht == &compat_hashtab)
2890 d = &vimvardict;
2891 else
2892 {
2893 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2894 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2895 }
2896 if (d == NULL)
2897 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002898 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899 return FAIL;
2900 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002901 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002903 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002904 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002905 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002906 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002907 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002908 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002909 || var_check_ro(di->di_flags, name, FALSE)
2910 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002911 return FAIL;
2912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913 delete_var(ht, hi);
2914 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002917 if (forceit)
2918 return OK;
2919 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 return FAIL;
2921}
2922
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002923/*
2924 * Lock or unlock variable indicated by "lp".
2925 * "deep" is the levels to go (-1 for unlimited);
2926 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2927 */
2928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002929do_lock_var(
2930 lval_T *lp,
2931 char_u *name_end,
2932 int deep,
2933 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002934{
2935 int ret = OK;
2936 int cc;
2937 dictitem_T *di;
2938
2939 if (deep == 0) /* nothing to do */
2940 return OK;
2941
2942 if (lp->ll_tv == NULL)
2943 {
2944 cc = *name_end;
2945 *name_end = NUL;
2946
2947 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01002948 di = find_var(lp->ll_name, NULL, TRUE);
2949 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01002951 else if ((di->di_flags & DI_FLAGS_FIX)
2952 && di->di_tv.v_type != VAR_DICT
2953 && di->di_tv.v_type != VAR_LIST)
2954 /* For historic reasons this error is not given for a list or dict.
2955 * E.g., the b: dict could be locked/unlocked. */
2956 EMSG2(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002957 else
2958 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002959 if (lock)
2960 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01002962 di->di_flags &= ~DI_FLAGS_LOCK;
2963 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 }
2965 *name_end = cc;
2966 }
2967 else if (lp->ll_range)
2968 {
2969 listitem_T *li = lp->ll_li;
2970
2971 /* (un)lock a range of List items. */
2972 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2973 {
2974 item_lock(&li->li_tv, deep, lock);
2975 li = li->li_next;
2976 ++lp->ll_n1;
2977 }
2978 }
2979 else if (lp->ll_list != NULL)
2980 /* (un)lock a List item. */
2981 item_lock(&lp->ll_li->li_tv, deep, lock);
2982 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002983 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002984 item_lock(&lp->ll_di->di_tv, deep, lock);
2985
2986 return ret;
2987}
2988
2989/*
2990 * Lock or unlock an item. "deep" is nr of levels to go.
2991 */
2992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002993item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002994{
2995 static int recurse = 0;
2996 list_T *l;
2997 listitem_T *li;
2998 dict_T *d;
2999 hashitem_T *hi;
3000 int todo;
3001
3002 if (recurse >= DICT_MAXNEST)
3003 {
3004 EMSG(_("E743: variable nested too deep for (un)lock"));
3005 return;
3006 }
3007 if (deep == 0)
3008 return;
3009 ++recurse;
3010
3011 /* lock/unlock the item itself */
3012 if (lock)
3013 tv->v_lock |= VAR_LOCKED;
3014 else
3015 tv->v_lock &= ~VAR_LOCKED;
3016
3017 switch (tv->v_type)
3018 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003019 case VAR_UNKNOWN:
3020 case VAR_NUMBER:
3021 case VAR_STRING:
3022 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003023 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003024 case VAR_FLOAT:
3025 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003026 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003027 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003028 break;
3029
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003030 case VAR_LIST:
3031 if ((l = tv->vval.v_list) != NULL)
3032 {
3033 if (lock)
3034 l->lv_lock |= VAR_LOCKED;
3035 else
3036 l->lv_lock &= ~VAR_LOCKED;
3037 if (deep < 0 || deep > 1)
3038 /* recursive: lock/unlock the items the List contains */
3039 for (li = l->lv_first; li != NULL; li = li->li_next)
3040 item_lock(&li->li_tv, deep - 1, lock);
3041 }
3042 break;
3043 case VAR_DICT:
3044 if ((d = tv->vval.v_dict) != NULL)
3045 {
3046 if (lock)
3047 d->dv_lock |= VAR_LOCKED;
3048 else
3049 d->dv_lock &= ~VAR_LOCKED;
3050 if (deep < 0 || deep > 1)
3051 {
3052 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003053 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003054 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3055 {
3056 if (!HASHITEM_EMPTY(hi))
3057 {
3058 --todo;
3059 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3060 }
3061 }
3062 }
3063 }
3064 }
3065 --recurse;
3066}
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3069/*
3070 * Delete all "menutrans_" variables.
3071 */
3072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
Bram Moolenaar33570922005-01-25 22:26:29 +00003075 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003076 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaar33570922005-01-25 22:26:29 +00003078 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003079 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003081 {
3082 if (!HASHITEM_EMPTY(hi))
3083 {
3084 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3086 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003087 }
3088 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090}
3091#endif
3092
3093#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3094
3095/*
3096 * Local string buffer for the next two functions to store a variable name
3097 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3098 * get_user_var_name().
3099 */
3100
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003101static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
3103static char_u *varnamebuf = NULL;
3104static int varnamebuflen = 0;
3105
3106/*
3107 * Function to concatenate a prefix and a variable name.
3108 */
3109 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003110cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int len;
3113
3114 len = (int)STRLEN(name) + 3;
3115 if (len > varnamebuflen)
3116 {
3117 vim_free(varnamebuf);
3118 len += 10; /* some additional space */
3119 varnamebuf = alloc(len);
3120 if (varnamebuf == NULL)
3121 {
3122 varnamebuflen = 0;
3123 return NULL;
3124 }
3125 varnamebuflen = len;
3126 }
3127 *varnamebuf = prefix;
3128 varnamebuf[1] = ':';
3129 STRCPY(varnamebuf + 2, name);
3130 return varnamebuf;
3131}
3132
3133/*
3134 * Function given to ExpandGeneric() to obtain the list of user defined
3135 * (global/buffer/window/built-in) variable names.
3136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003138get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003140 static long_u gdone;
3141 static long_u bdone;
3142 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003143 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003144 static int vidx;
3145 static hashitem_T *hi;
3146 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003149 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003150 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003151 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003152 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003153
3154 /* Global variables */
3155 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003157 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003159 else
3160 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003161 while (HASHITEM_EMPTY(hi))
3162 ++hi;
3163 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3164 return cat_prefix_varname('g', hi->hi_key);
3165 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003167
3168 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003169 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003172 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003174 else
3175 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003176 while (HASHITEM_EMPTY(hi))
3177 ++hi;
3178 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003180
3181 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003182 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003185 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003187 else
3188 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003189 while (HASHITEM_EMPTY(hi))
3190 ++hi;
3191 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003193
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003194 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003195 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003196 if (tdone < ht->ht_used)
3197 {
3198 if (tdone++ == 0)
3199 hi = ht->ht_array;
3200 else
3201 ++hi;
3202 while (HASHITEM_EMPTY(hi))
3203 ++hi;
3204 return cat_prefix_varname('t', hi->hi_key);
3205 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003206
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 /* v: variables */
3208 if (vidx < VV_LEN)
3209 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 vim_free(varnamebuf);
3212 varnamebuf = NULL;
3213 varnamebuflen = 0;
3214 return NULL;
3215}
3216
3217#endif /* FEAT_CMDL_COMPL */
3218
3219/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003220 * Return TRUE if "pat" matches "text".
3221 * Does not use 'cpo' and always uses 'magic'.
3222 */
3223 static int
3224pattern_match(char_u *pat, char_u *text, int ic)
3225{
3226 int matches = FALSE;
3227 char_u *save_cpo;
3228 regmatch_T regmatch;
3229
3230 /* avoid 'l' flag in 'cpoptions' */
3231 save_cpo = p_cpo;
3232 p_cpo = (char_u *)"";
3233 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3234 if (regmatch.regprog != NULL)
3235 {
3236 regmatch.rm_ic = ic;
3237 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3238 vim_regfree(regmatch.regprog);
3239 }
3240 p_cpo = save_cpo;
3241 return matches;
3242}
3243
3244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * types for expressions.
3246 */
3247typedef enum
3248{
3249 TYPE_UNKNOWN = 0
3250 , TYPE_EQUAL /* == */
3251 , TYPE_NEQUAL /* != */
3252 , TYPE_GREATER /* > */
3253 , TYPE_GEQUAL /* >= */
3254 , TYPE_SMALLER /* < */
3255 , TYPE_SEQUAL /* <= */
3256 , TYPE_MATCH /* =~ */
3257 , TYPE_NOMATCH /* !~ */
3258} exptype_T;
3259
3260/*
3261 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003262 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3264 */
3265
3266/*
3267 * Handle zero level expression.
3268 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003270 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 * Return OK or FAIL.
3272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003274eval0(
3275 char_u *arg,
3276 typval_T *rettv,
3277 char_u **nextcmd,
3278 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 int ret;
3281 char_u *p;
3282
3283 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003284 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (ret == FAIL || !ends_excmd(*p))
3286 {
3287 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003288 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 /*
3290 * Report the invalid expression unless the expression evaluation has
3291 * been cancelled due to an aborting error, an interrupt, or an
3292 * exception.
3293 */
3294 if (!aborting())
3295 EMSG2(_(e_invexpr2), arg);
3296 ret = FAIL;
3297 }
3298 if (nextcmd != NULL)
3299 *nextcmd = check_nextcmd(p);
3300
3301 return ret;
3302}
3303
3304/*
3305 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003306 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 *
3308 * "arg" must point to the first non-white of the expression.
3309 * "arg" is advanced to the next non-white after the recognized expression.
3310 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003311 * Note: "rettv.v_lock" is not set.
3312 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 * Return OK or FAIL.
3314 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003315 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003316eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 /*
3322 * Get the first variable.
3323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return FAIL;
3326
3327 if ((*arg)[0] == '?')
3328 {
3329 result = FALSE;
3330 if (evaluate)
3331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 int error = FALSE;
3333
3334 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 if (error)
3338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340
3341 /*
3342 * Get the second variable.
3343 */
3344 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 return FAIL;
3347
3348 /*
3349 * Check for the ":".
3350 */
3351 if ((*arg)[0] != ':')
3352 {
3353 EMSG(_("E109: Missing ':' after '?'"));
3354 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 return FAIL;
3357 }
3358
3359 /*
3360 * Get the third variable.
3361 */
3362 *arg = skipwhite(*arg + 1);
3363 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3364 {
3365 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 return FAIL;
3368 }
3369 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003370 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372
3373 return OK;
3374}
3375
3376/*
3377 * Handle first level expression:
3378 * expr2 || expr2 || expr2 logical OR
3379 *
3380 * "arg" must point to the first non-white of the expression.
3381 * "arg" is advanced to the next non-white after the recognized expression.
3382 *
3383 * Return OK or FAIL.
3384 */
3385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003386eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 long result;
3390 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003391 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 /*
3394 * Get the first variable.
3395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003396 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return FAIL;
3398
3399 /*
3400 * Repeat until there is no following "||".
3401 */
3402 first = TRUE;
3403 result = FALSE;
3404 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3405 {
3406 if (evaluate && first)
3407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003408 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003411 if (error)
3412 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 first = FALSE;
3414 }
3415
3416 /*
3417 * Get the second variable.
3418 */
3419 *arg = skipwhite(*arg + 2);
3420 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3421 return FAIL;
3422
3423 /*
3424 * Compute the result.
3425 */
3426 if (evaluate && !result)
3427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003428 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003431 if (error)
3432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 if (evaluate)
3435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 rettv->v_type = VAR_NUMBER;
3437 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 }
3440
3441 return OK;
3442}
3443
3444/*
3445 * Handle second level expression:
3446 * expr3 && expr3 && expr3 logical AND
3447 *
3448 * "arg" must point to the first non-white of the expression.
3449 * "arg" is advanced to the next non-white after the recognized expression.
3450 *
3451 * Return OK or FAIL.
3452 */
3453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 long result;
3458 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003459 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
3461 /*
3462 * Get the first variable.
3463 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003464 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 return FAIL;
3466
3467 /*
3468 * Repeat until there is no following "&&".
3469 */
3470 first = TRUE;
3471 result = TRUE;
3472 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3473 {
3474 if (evaluate && first)
3475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003476 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003479 if (error)
3480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 first = FALSE;
3482 }
3483
3484 /*
3485 * Get the second variable.
3486 */
3487 *arg = skipwhite(*arg + 2);
3488 if (eval4(arg, &var2, evaluate && result) == FAIL)
3489 return FAIL;
3490
3491 /*
3492 * Compute the result.
3493 */
3494 if (evaluate && result)
3495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003496 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003498 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 if (error)
3500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 if (evaluate)
3503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 rettv->v_type = VAR_NUMBER;
3505 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507 }
3508
3509 return OK;
3510}
3511
3512/*
3513 * Handle third level expression:
3514 * var1 == var2
3515 * var1 =~ var2
3516 * var1 != var2
3517 * var1 !~ var2
3518 * var1 > var2
3519 * var1 >= var2
3520 * var1 < var2
3521 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003522 * var1 is var2
3523 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 *
3525 * "arg" must point to the first non-white of the expression.
3526 * "arg" is advanced to the next non-white after the recognized expression.
3527 *
3528 * Return OK or FAIL.
3529 */
3530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003531eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 char_u *p;
3535 int i;
3536 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003537 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003539 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_u *s1, *s2;
3541 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 /*
3545 * Get the first variable.
3546 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return FAIL;
3549
3550 p = *arg;
3551 switch (p[0])
3552 {
3553 case '=': if (p[1] == '=')
3554 type = TYPE_EQUAL;
3555 else if (p[1] == '~')
3556 type = TYPE_MATCH;
3557 break;
3558 case '!': if (p[1] == '=')
3559 type = TYPE_NEQUAL;
3560 else if (p[1] == '~')
3561 type = TYPE_NOMATCH;
3562 break;
3563 case '>': if (p[1] != '=')
3564 {
3565 type = TYPE_GREATER;
3566 len = 1;
3567 }
3568 else
3569 type = TYPE_GEQUAL;
3570 break;
3571 case '<': if (p[1] != '=')
3572 {
3573 type = TYPE_SMALLER;
3574 len = 1;
3575 }
3576 else
3577 type = TYPE_SEQUAL;
3578 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003579 case 'i': if (p[1] == 's')
3580 {
3581 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3582 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003583 i = p[len];
3584 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003585 {
3586 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3587 type_is = TRUE;
3588 }
3589 }
3590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592
3593 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003594 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 */
3596 if (type != TYPE_UNKNOWN)
3597 {
3598 /* extra question mark appended: ignore case */
3599 if (p[len] == '?')
3600 {
3601 ic = TRUE;
3602 ++len;
3603 }
3604 /* extra '#' appended: match case */
3605 else if (p[len] == '#')
3606 {
3607 ic = FALSE;
3608 ++len;
3609 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003610 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 else
3612 ic = p_ic;
3613
3614 /*
3615 * Get the second variable.
3616 */
3617 *arg = skipwhite(p + len);
3618 if (eval5(arg, &var2, evaluate) == FAIL)
3619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003620 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 return FAIL;
3622 }
3623
3624 if (evaluate)
3625 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003626 if (type_is && rettv->v_type != var2.v_type)
3627 {
3628 /* For "is" a different type always means FALSE, for "notis"
3629 * it means TRUE. */
3630 n1 = (type == TYPE_NEQUAL);
3631 }
3632 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3633 {
3634 if (type_is)
3635 {
3636 n1 = (rettv->v_type == var2.v_type
3637 && rettv->vval.v_list == var2.vval.v_list);
3638 if (type == TYPE_NEQUAL)
3639 n1 = !n1;
3640 }
3641 else if (rettv->v_type != var2.v_type
3642 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3643 {
3644 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003645 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003646 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003647 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003648 clear_tv(rettv);
3649 clear_tv(&var2);
3650 return FAIL;
3651 }
3652 else
3653 {
3654 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003655 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3656 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003657 if (type == TYPE_NEQUAL)
3658 n1 = !n1;
3659 }
3660 }
3661
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003662 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3663 {
3664 if (type_is)
3665 {
3666 n1 = (rettv->v_type == var2.v_type
3667 && rettv->vval.v_dict == var2.vval.v_dict);
3668 if (type == TYPE_NEQUAL)
3669 n1 = !n1;
3670 }
3671 else if (rettv->v_type != var2.v_type
3672 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3673 {
3674 if (rettv->v_type != var2.v_type)
3675 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3676 else
3677 EMSG(_("E736: Invalid operation for Dictionary"));
3678 clear_tv(rettv);
3679 clear_tv(&var2);
3680 return FAIL;
3681 }
3682 else
3683 {
3684 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003685 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3686 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003687 if (type == TYPE_NEQUAL)
3688 n1 = !n1;
3689 }
3690 }
3691
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003692 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3693 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003694 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003695 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003696 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003697 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003698 clear_tv(rettv);
3699 clear_tv(&var2);
3700 return FAIL;
3701 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003702 if ((rettv->v_type == VAR_PARTIAL
3703 && rettv->vval.v_partial == NULL)
3704 || (var2.v_type == VAR_PARTIAL
3705 && var2.vval.v_partial == NULL))
3706 /* when a partial is NULL assume not equal */
3707 n1 = FALSE;
3708 else if (type_is)
3709 {
3710 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3711 /* strings are considered the same if their value is
3712 * the same */
3713 n1 = tv_equal(rettv, &var2, ic, FALSE);
3714 else if (rettv->v_type == VAR_PARTIAL
3715 && var2.v_type == VAR_PARTIAL)
3716 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3717 else
3718 n1 = FALSE;
3719 }
3720 else
3721 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003722 if (type == TYPE_NEQUAL)
3723 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003724 }
3725
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003726#ifdef FEAT_FLOAT
3727 /*
3728 * If one of the two variables is a float, compare as a float.
3729 * When using "=~" or "!~", always compare as string.
3730 */
3731 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3732 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3733 {
3734 float_T f1, f2;
3735
3736 if (rettv->v_type == VAR_FLOAT)
3737 f1 = rettv->vval.v_float;
3738 else
3739 f1 = get_tv_number(rettv);
3740 if (var2.v_type == VAR_FLOAT)
3741 f2 = var2.vval.v_float;
3742 else
3743 f2 = get_tv_number(&var2);
3744 n1 = FALSE;
3745 switch (type)
3746 {
3747 case TYPE_EQUAL: n1 = (f1 == f2); break;
3748 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3749 case TYPE_GREATER: n1 = (f1 > f2); break;
3750 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3751 case TYPE_SMALLER: n1 = (f1 < f2); break;
3752 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3753 case TYPE_UNKNOWN:
3754 case TYPE_MATCH:
3755 case TYPE_NOMATCH: break; /* avoid gcc warning */
3756 }
3757 }
3758#endif
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 /*
3761 * If one of the two variables is a number, compare as a number.
3762 * When using "=~" or "!~", always compare as string.
3763 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003764 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 n1 = get_tv_number(rettv);
3768 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 switch (type)
3770 {
3771 case TYPE_EQUAL: n1 = (n1 == n2); break;
3772 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3773 case TYPE_GREATER: n1 = (n1 > n2); break;
3774 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3775 case TYPE_SMALLER: n1 = (n1 < n2); break;
3776 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3777 case TYPE_UNKNOWN:
3778 case TYPE_MATCH:
3779 case TYPE_NOMATCH: break; /* avoid gcc warning */
3780 }
3781 }
3782 else
3783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784 s1 = get_tv_string_buf(rettv, buf1);
3785 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3787 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3788 else
3789 i = 0;
3790 n1 = FALSE;
3791 switch (type)
3792 {
3793 case TYPE_EQUAL: n1 = (i == 0); break;
3794 case TYPE_NEQUAL: n1 = (i != 0); break;
3795 case TYPE_GREATER: n1 = (i > 0); break;
3796 case TYPE_GEQUAL: n1 = (i >= 0); break;
3797 case TYPE_SMALLER: n1 = (i < 0); break;
3798 case TYPE_SEQUAL: n1 = (i <= 0); break;
3799
3800 case TYPE_MATCH:
3801 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003802 n1 = pattern_match(s2, s1, ic);
3803 if (type == TYPE_NOMATCH)
3804 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 break;
3806
3807 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3808 }
3809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 clear_tv(rettv);
3811 clear_tv(&var2);
3812 rettv->v_type = VAR_NUMBER;
3813 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815 }
3816
3817 return OK;
3818}
3819
3820/*
3821 * Handle fourth level expression:
3822 * + number addition
3823 * - number subtraction
3824 * . string concatenation
3825 *
3826 * "arg" must point to the first non-white of the expression.
3827 * "arg" is advanced to the next non-white after the recognized expression.
3828 *
3829 * Return OK or FAIL.
3830 */
3831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003832eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 typval_T var2;
3835 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003837 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003838#ifdef FEAT_FLOAT
3839 float_T f1 = 0, f2 = 0;
3840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 char_u *s1, *s2;
3842 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3843 char_u *p;
3844
3845 /*
3846 * Get the first variable.
3847 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003848 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return FAIL;
3850
3851 /*
3852 * Repeat computing, until no '+', '-' or '.' is following.
3853 */
3854 for (;;)
3855 {
3856 op = **arg;
3857 if (op != '+' && op != '-' && op != '.')
3858 break;
3859
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003860 if ((op != '+' || rettv->v_type != VAR_LIST)
3861#ifdef FEAT_FLOAT
3862 && (op == '.' || rettv->v_type != VAR_FLOAT)
3863#endif
3864 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 {
3866 /* For "list + ...", an illegal use of the first operand as
3867 * a number cannot be determined before evaluating the 2nd
3868 * operand: if this is also a list, all is ok.
3869 * For "something . ...", "something - ..." or "non-list + ...",
3870 * we know that the first operand needs to be a string or number
3871 * without evaluating the 2nd operand. So check before to avoid
3872 * side effects after an error. */
3873 if (evaluate && get_tv_string_chk(rettv) == NULL)
3874 {
3875 clear_tv(rettv);
3876 return FAIL;
3877 }
3878 }
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 /*
3881 * Get the second variable.
3882 */
3883 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003884 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003886 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 return FAIL;
3888 }
3889
3890 if (evaluate)
3891 {
3892 /*
3893 * Compute the result.
3894 */
3895 if (op == '.')
3896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003897 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3898 s2 = get_tv_string_buf_chk(&var2, buf2);
3899 if (s2 == NULL) /* type error ? */
3900 {
3901 clear_tv(rettv);
3902 clear_tv(&var2);
3903 return FAIL;
3904 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003905 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003906 clear_tv(rettv);
3907 rettv->v_type = VAR_STRING;
3908 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003910 else if (op == '+' && rettv->v_type == VAR_LIST
3911 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003912 {
3913 /* concatenate Lists */
3914 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3915 &var3) == FAIL)
3916 {
3917 clear_tv(rettv);
3918 clear_tv(&var2);
3919 return FAIL;
3920 }
3921 clear_tv(rettv);
3922 *rettv = var3;
3923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 else
3925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 int error = FALSE;
3927
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003928#ifdef FEAT_FLOAT
3929 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931 f1 = rettv->vval.v_float;
3932 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003934 else
3935#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003937 n1 = get_tv_number_chk(rettv, &error);
3938 if (error)
3939 {
3940 /* This can only happen for "list + non-list". For
3941 * "non-list + ..." or "something - ...", we returned
3942 * before evaluating the 2nd operand. */
3943 clear_tv(rettv);
3944 return FAIL;
3945 }
3946#ifdef FEAT_FLOAT
3947 if (var2.v_type == VAR_FLOAT)
3948 f1 = n1;
3949#endif
3950 }
3951#ifdef FEAT_FLOAT
3952 if (var2.v_type == VAR_FLOAT)
3953 {
3954 f2 = var2.vval.v_float;
3955 n2 = 0;
3956 }
3957 else
3958#endif
3959 {
3960 n2 = get_tv_number_chk(&var2, &error);
3961 if (error)
3962 {
3963 clear_tv(rettv);
3964 clear_tv(&var2);
3965 return FAIL;
3966 }
3967#ifdef FEAT_FLOAT
3968 if (rettv->v_type == VAR_FLOAT)
3969 f2 = n2;
3970#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003971 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003973
3974#ifdef FEAT_FLOAT
3975 /* If there is a float on either side the result is a float. */
3976 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3977 {
3978 if (op == '+')
3979 f1 = f1 + f2;
3980 else
3981 f1 = f1 - f2;
3982 rettv->v_type = VAR_FLOAT;
3983 rettv->vval.v_float = f1;
3984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003986#endif
3987 {
3988 if (op == '+')
3989 n1 = n1 + n2;
3990 else
3991 n1 = n1 - n2;
3992 rettv->v_type = VAR_NUMBER;
3993 rettv->vval.v_number = n1;
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998 }
3999 return OK;
4000}
4001
4002/*
4003 * Handle fifth level expression:
4004 * * number multiplication
4005 * / number division
4006 * % number modulo
4007 *
4008 * "arg" must point to the first non-white of the expression.
4009 * "arg" is advanced to the next non-white after the recognized expression.
4010 *
4011 * Return OK or FAIL.
4012 */
4013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004014eval6(
4015 char_u **arg,
4016 typval_T *rettv,
4017 int evaluate,
4018 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004022 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004023#ifdef FEAT_FLOAT
4024 int use_float = FALSE;
4025 float_T f1 = 0, f2;
4026#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004027 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 /*
4030 * Get the first variable.
4031 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004032 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return FAIL;
4034
4035 /*
4036 * Repeat computing, until no '*', '/' or '%' is following.
4037 */
4038 for (;;)
4039 {
4040 op = **arg;
4041 if (op != '*' && op != '/' && op != '%')
4042 break;
4043
4044 if (evaluate)
4045 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004046#ifdef FEAT_FLOAT
4047 if (rettv->v_type == VAR_FLOAT)
4048 {
4049 f1 = rettv->vval.v_float;
4050 use_float = TRUE;
4051 n1 = 0;
4052 }
4053 else
4054#endif
4055 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004057 if (error)
4058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 else
4061 n1 = 0;
4062
4063 /*
4064 * Get the second variable.
4065 */
4066 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004067 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return FAIL;
4069
4070 if (evaluate)
4071 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072#ifdef FEAT_FLOAT
4073 if (var2.v_type == VAR_FLOAT)
4074 {
4075 if (!use_float)
4076 {
4077 f1 = n1;
4078 use_float = TRUE;
4079 }
4080 f2 = var2.vval.v_float;
4081 n2 = 0;
4082 }
4083 else
4084#endif
4085 {
4086 n2 = get_tv_number_chk(&var2, &error);
4087 clear_tv(&var2);
4088 if (error)
4089 return FAIL;
4090#ifdef FEAT_FLOAT
4091 if (use_float)
4092 f2 = n2;
4093#endif
4094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 /*
4097 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004098 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004100#ifdef FEAT_FLOAT
4101 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 if (op == '*')
4104 f1 = f1 * f2;
4105 else if (op == '/')
4106 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004107# ifdef VMS
4108 /* VMS crashes on divide by zero, work around it */
4109 if (f2 == 0.0)
4110 {
4111 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004112 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004113 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004114 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004115 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004116 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004117 }
4118 else
4119 f1 = f1 / f2;
4120# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004121 /* We rely on the floating point library to handle divide
4122 * by zero to result in "inf" and not a crash. */
4123 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004124# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004127 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004128 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004129 return FAIL;
4130 }
4131 rettv->v_type = VAR_FLOAT;
4132 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004137 if (op == '*')
4138 n1 = n1 * n2;
4139 else if (op == '/')
4140 {
4141 if (n2 == 0) /* give an error message? */
4142 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004143 if (n1 == 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004144 n1 = VARNUM_MIN; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004145 else if (n1 < 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004146 n1 = -VARNUM_MAX;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004147 else
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004148 n1 = VARNUM_MAX;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004149 }
4150 else
4151 n1 = n1 / n2;
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004154 {
4155 if (n2 == 0) /* give an error message? */
4156 n1 = 0;
4157 else
4158 n1 = n1 % n2;
4159 }
4160 rettv->v_type = VAR_NUMBER;
4161 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 }
4165
4166 return OK;
4167}
4168
4169/*
4170 * Handle sixth level expression:
4171 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004172 * "string" string constant
4173 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * &option-name option value
4175 * @r register contents
4176 * identifier variable value
4177 * function() function call
4178 * $VAR environment variable
4179 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004180 * [expr, expr] List
4181 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 *
4183 * Also handle:
4184 * ! in front logical NOT
4185 * - in front unary minus
4186 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004187 * trailing [] subscript in String or List
4188 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 *
4190 * "arg" must point to the first non-white of the expression.
4191 * "arg" is advanced to the next non-white after the recognized expression.
4192 *
4193 * Return OK or FAIL.
4194 */
4195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004196eval7(
4197 char_u **arg,
4198 typval_T *rettv,
4199 int evaluate,
4200 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004202 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 int len;
4204 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 char_u *start_leader, *end_leader;
4206 int ret = OK;
4207 char_u *alias;
4208
4209 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004216 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
4218 start_leader = *arg;
4219 while (**arg == '!' || **arg == '-' || **arg == '+')
4220 *arg = skipwhite(*arg + 1);
4221 end_leader = *arg;
4222
4223 switch (**arg)
4224 {
4225 /*
4226 * Number constant.
4227 */
4228 case '0':
4229 case '1':
4230 case '2':
4231 case '3':
4232 case '4':
4233 case '5':
4234 case '6':
4235 case '7':
4236 case '8':
4237 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004238 {
4239#ifdef FEAT_FLOAT
4240 char_u *p = skipdigits(*arg + 1);
4241 int get_float = FALSE;
4242
4243 /* We accept a float when the format matches
4244 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004245 * strict to avoid backwards compatibility problems.
4246 * Don't look for a float after the "." operator, so that
4247 * ":let vers = 1.2.3" doesn't fail. */
4248 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004250 get_float = TRUE;
4251 p = skipdigits(p + 2);
4252 if (*p == 'e' || *p == 'E')
4253 {
4254 ++p;
4255 if (*p == '-' || *p == '+')
4256 ++p;
4257 if (!vim_isdigit(*p))
4258 get_float = FALSE;
4259 else
4260 p = skipdigits(p + 1);
4261 }
4262 if (ASCII_ISALPHA(*p) || *p == '.')
4263 get_float = FALSE;
4264 }
4265 if (get_float)
4266 {
4267 float_T f;
4268
4269 *arg += string2float(*arg, &f);
4270 if (evaluate)
4271 {
4272 rettv->v_type = VAR_FLOAT;
4273 rettv->vval.v_float = f;
4274 }
4275 }
4276 else
4277#endif
4278 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004279 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004280 *arg += len;
4281 if (evaluate)
4282 {
4283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = n;
4285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 /*
4291 * String constant: "string".
4292 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 break;
4295
4296 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004300 break;
4301
4302 /*
4303 * List: [expr, expr]
4304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 break;
4307
4308 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004309 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 * Dictionary: {key: val, key: val}
4311 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004312 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4313 if (ret == NOTDONE)
4314 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 break;
4316
4317 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004318 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004320 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 break;
4322
4323 /*
4324 * Environment variable: $VAR.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 break;
4328
4329 /*
4330 * Register contents: @r.
4331 */
4332 case '@': ++*arg;
4333 if (evaluate)
4334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004336 rettv->vval.v_string = get_reg_contents(**arg,
4337 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 if (**arg != NUL)
4340 ++*arg;
4341 break;
4342
4343 /*
4344 * nested expression: (expression).
4345 */
4346 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 if (**arg == ')')
4349 ++*arg;
4350 else if (ret == OK)
4351 {
4352 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 ret = FAIL;
4355 }
4356 break;
4357
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 break;
4360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004361
4362 if (ret == NOTDONE)
4363 {
4364 /*
4365 * Must be a variable or function name.
4366 * Can also be a curly-braces kind of name: {expr}.
4367 */
4368 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004369 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 if (alias != NULL)
4371 s = alias;
4372
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004373 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004374 ret = FAIL;
4375 else
4376 {
4377 if (**arg == '(') /* recursive! */
4378 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004379 partial_T *partial;
4380
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004381 if (!evaluate)
4382 check_vars(s, len);
4383
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384 /* If "s" is the name of a variable of type VAR_FUNC
4385 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004386 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004388 /* Need to make a copy, in case evaluating the arguments makes
4389 * the name invalid. */
4390 s = vim_strsave(s);
4391 if (s == NULL)
4392 ret = FAIL;
4393 else
4394 /* Invoke the function. */
4395 ret = get_func_tv(s, len, rettv, arg,
4396 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4397 &len, evaluate, partial, NULL);
4398 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004399
4400 /* If evaluate is FALSE rettv->v_type was not set in
4401 * get_func_tv, but it's needed in handle_subscript() to parse
4402 * what follows. So set it here. */
4403 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4404 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004405 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004406 rettv->v_type = VAR_FUNC;
4407 }
4408
Bram Moolenaar8c711452005-01-14 21:53:12 +00004409 /* Stop the expression evaluation when immediately
4410 * aborting on error, or when an interrupt occurred or
4411 * an exception was thrown but not caught. */
4412 if (aborting())
4413 {
4414 if (ret == OK)
4415 clear_tv(rettv);
4416 ret = FAIL;
4417 }
4418 }
4419 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004420 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004421 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004422 {
4423 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004424 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004426 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004427 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004428 }
4429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 *arg = skipwhite(*arg);
4431
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004432 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4433 * expr(expr). */
4434 if (ret == OK)
4435 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 /*
4438 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4439 */
4440 if (ret == OK && evaluate && end_leader > start_leader)
4441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004443 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004444#ifdef FEAT_FLOAT
4445 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004447 if (rettv->v_type == VAR_FLOAT)
4448 f = rettv->vval.v_float;
4449 else
4450#endif
4451 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004454 clear_tv(rettv);
4455 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 else
4458 {
4459 while (end_leader > start_leader)
4460 {
4461 --end_leader;
4462 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463 {
4464#ifdef FEAT_FLOAT
4465 if (rettv->v_type == VAR_FLOAT)
4466 f = !f;
4467 else
4468#endif
4469 val = !val;
4470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004471 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004472 {
4473#ifdef FEAT_FLOAT
4474 if (rettv->v_type == VAR_FLOAT)
4475 f = -f;
4476 else
4477#endif
4478 val = -val;
4479 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004481#ifdef FEAT_FLOAT
4482 if (rettv->v_type == VAR_FLOAT)
4483 {
4484 clear_tv(rettv);
4485 rettv->vval.v_float = f;
4486 }
4487 else
4488#endif
4489 {
4490 clear_tv(rettv);
4491 rettv->v_type = VAR_NUMBER;
4492 rettv->vval.v_number = val;
4493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496
4497 return ret;
4498}
4499
4500/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004501 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4502 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4504 */
4505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004506eval_index(
4507 char_u **arg,
4508 typval_T *rettv,
4509 int evaluate,
4510 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511{
4512 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004513 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515 long len = -1;
4516 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004519
Bram Moolenaara03f2332016-02-06 18:09:59 +01004520 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004523 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004524 if (verbose)
4525 EMSG(_("E695: Cannot index a Funcref"));
4526 return FAIL;
4527 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004528#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004529 if (verbose)
4530 EMSG(_(e_float_as_string));
4531 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004532#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004533 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004534 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004535 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004536 if (verbose)
4537 EMSG(_("E909: Cannot index a special variable"));
4538 return FAIL;
4539 case VAR_UNKNOWN:
4540 if (evaluate)
4541 return FAIL;
4542 /* FALLTHROUGH */
4543
4544 case VAR_STRING:
4545 case VAR_NUMBER:
4546 case VAR_LIST:
4547 case VAR_DICT:
4548 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004549 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004551 init_tv(&var1);
4552 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 /*
4556 * dict.name
4557 */
4558 key = *arg + 1;
4559 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4560 ;
4561 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004564 }
4565 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 /*
4568 * something[idx]
4569 *
4570 * Get the (first) variable from inside the [].
4571 */
4572 *arg = skipwhite(*arg + 1);
4573 if (**arg == ':')
4574 empty1 = TRUE;
4575 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4576 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004577 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4578 {
4579 /* not a number or string */
4580 clear_tv(&var1);
4581 return FAIL;
4582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004583
4584 /*
4585 * Get the second variable from inside the [:].
4586 */
4587 if (**arg == ':')
4588 {
4589 range = TRUE;
4590 *arg = skipwhite(*arg + 1);
4591 if (**arg == ']')
4592 empty2 = TRUE;
4593 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 if (!empty1)
4596 clear_tv(&var1);
4597 return FAIL;
4598 }
4599 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4600 {
4601 /* not a number or string */
4602 if (!empty1)
4603 clear_tv(&var1);
4604 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 return FAIL;
4606 }
4607 }
4608
4609 /* Check for the ']'. */
4610 if (**arg != ']')
4611 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004612 if (verbose)
4613 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 clear_tv(&var1);
4615 if (range)
4616 clear_tv(&var2);
4617 return FAIL;
4618 }
4619 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 }
4621
4622 if (evaluate)
4623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 n1 = 0;
4625 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 n1 = get_tv_number(&var1);
4628 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 }
4630 if (range)
4631 {
4632 if (empty2)
4633 n2 = -1;
4634 else
4635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 n2 = get_tv_number(&var2);
4637 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 }
4639 }
4640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004643 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004644 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004645 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004646 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004647 case VAR_SPECIAL:
4648 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004649 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004650 break; /* not evaluating, skipping over subscript */
4651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 case VAR_NUMBER:
4653 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 len = (long)STRLEN(s);
4656 if (range)
4657 {
4658 /* The resulting variable is a substring. If the indexes
4659 * are out of range the result is empty. */
4660 if (n1 < 0)
4661 {
4662 n1 = len + n1;
4663 if (n1 < 0)
4664 n1 = 0;
4665 }
4666 if (n2 < 0)
4667 n2 = len + n2;
4668 else if (n2 >= len)
4669 n2 = len;
4670 if (n1 >= len || n2 < 0 || n1 > n2)
4671 s = NULL;
4672 else
4673 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4674 }
4675 else
4676 {
4677 /* The resulting variable is a string of a single
4678 * character. If the index is too big or negative the
4679 * result is empty. */
4680 if (n1 >= len || n1 < 0)
4681 s = NULL;
4682 else
4683 s = vim_strnsave(s + n1, 1);
4684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 clear_tv(rettv);
4686 rettv->v_type = VAR_STRING;
4687 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 break;
4689
4690 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 if (n1 < 0)
4693 n1 = len + n1;
4694 if (!empty1 && (n1 < 0 || n1 >= len))
4695 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004696 /* For a range we allow invalid values and return an empty
4697 * list. A list index out of range is an error. */
4698 if (!range)
4699 {
4700 if (verbose)
4701 EMSGN(_(e_listidx), n1);
4702 return FAIL;
4703 }
4704 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 }
4706 if (range)
4707 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 list_T *l;
4709 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710
4711 if (n2 < 0)
4712 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004713 else if (n2 >= len)
4714 n2 = len - 1;
4715 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004716 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 l = list_alloc();
4718 if (l == NULL)
4719 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 n1 <= n2; ++n1)
4722 {
4723 if (list_append_tv(l, &item->li_tv) == FAIL)
4724 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004725 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 return FAIL;
4727 }
4728 item = item->li_next;
4729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004731 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 }
4733 else
4734 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004735 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004736 clear_tv(rettv);
4737 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 }
4739 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740
4741 case VAR_DICT:
4742 if (range)
4743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004744 if (verbose)
4745 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 if (len == -1)
4747 clear_tv(&var1);
4748 return FAIL;
4749 }
4750 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004751 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752
4753 if (len == -1)
4754 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004755 key = get_tv_string_chk(&var1);
4756 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 clear_tv(&var1);
4759 return FAIL;
4760 }
4761 }
4762
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004763 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004765 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004766 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 if (len == -1)
4768 clear_tv(&var1);
4769 if (item == NULL)
4770 return FAIL;
4771
4772 copy_tv(&item->di_tv, &var1);
4773 clear_tv(rettv);
4774 *rettv = var1;
4775 }
4776 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 }
4778 }
4779
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 return OK;
4781}
4782
4783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 * Get an option value.
4785 * "arg" points to the '&' or '+' before the option name.
4786 * "arg" is advanced to character after the option name.
4787 * Return OK or FAIL.
4788 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004790get_option_tv(
4791 char_u **arg,
4792 typval_T *rettv, /* when NULL, only check if option exists */
4793 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 char_u *option_end;
4796 long numval;
4797 char_u *stringval;
4798 int opt_type;
4799 int c;
4800 int working = (**arg == '+'); /* has("+option") */
4801 int ret = OK;
4802 int opt_flags;
4803
4804 /*
4805 * Isolate the option name and find its value.
4806 */
4807 option_end = find_option_end(arg, &opt_flags);
4808 if (option_end == NULL)
4809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004810 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 EMSG2(_("E112: Option name missing: %s"), *arg);
4812 return FAIL;
4813 }
4814
4815 if (!evaluate)
4816 {
4817 *arg = option_end;
4818 return OK;
4819 }
4820
4821 c = *option_end;
4822 *option_end = NUL;
4823 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
4826 if (opt_type == -3) /* invalid name */
4827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 EMSG2(_("E113: Unknown option: %s"), *arg);
4830 ret = FAIL;
4831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 if (opt_type == -2) /* hidden string option */
4835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 rettv->v_type = VAR_STRING;
4837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839 else if (opt_type == -1) /* hidden number option */
4840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 rettv->v_type = VAR_NUMBER;
4842 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else if (opt_type == 1) /* number option */
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 rettv->v_type = VAR_NUMBER;
4847 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 else /* string option */
4850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 rettv->v_type = VAR_STRING;
4852 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855 else if (working && (opt_type == -2 || opt_type == -1))
4856 ret = FAIL;
4857
4858 *option_end = c; /* put back for error messages */
4859 *arg = option_end;
4860
4861 return ret;
4862}
4863
4864/*
4865 * Allocate a variable for a string constant.
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 char_u *p;
4872 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 int extra = 0;
4874
4875 /*
4876 * Find the end of the string, skipping backslashed characters.
4877 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004878 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 if (*p == '\\' && p[1] != NUL)
4881 {
4882 ++p;
4883 /* A "\<x>" form occupies at least 4 characters, and produces up
4884 * to 6 characters: reserve space for 2 extra */
4885 if (*p == '<')
4886 extra += 2;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889
4890 if (*p != '"')
4891 {
4892 EMSG2(_("E114: Missing quote: %s"), *arg);
4893 return FAIL;
4894 }
4895
4896 /* If only parsing, set *arg and return here */
4897 if (!evaluate)
4898 {
4899 *arg = p + 1;
4900 return OK;
4901 }
4902
4903 /*
4904 * Copy the string into allocated memory, handling backslashed
4905 * characters.
4906 */
4907 name = alloc((unsigned)(p - *arg + extra));
4908 if (name == NULL)
4909 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004910 rettv->v_type = VAR_STRING;
4911 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
4915 if (*p == '\\')
4916 {
4917 switch (*++p)
4918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004919 case 'b': *name++ = BS; ++p; break;
4920 case 'e': *name++ = ESC; ++p; break;
4921 case 'f': *name++ = FF; ++p; break;
4922 case 'n': *name++ = NL; ++p; break;
4923 case 'r': *name++ = CAR; ++p; break;
4924 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 case 'X': /* hex: "\x1", "\x12" */
4927 case 'x':
4928 case 'u': /* Unicode: "\u0023" */
4929 case 'U':
4930 if (vim_isxdigit(p[1]))
4931 {
4932 int n, nr;
4933 int c = toupper(*p);
4934
4935 if (c == 'X')
4936 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004937 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004939 else
4940 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 nr = 0;
4942 while (--n >= 0 && vim_isxdigit(p[1]))
4943 {
4944 ++p;
4945 nr = (nr << 4) + hex2nr(*p);
4946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#ifdef FEAT_MBYTE
4949 /* For "\u" store the number according to
4950 * 'encoding'. */
4951 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 else
4954#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 break;
4958
4959 /* octal: "\1", "\12", "\123" */
4960 case '0':
4961 case '1':
4962 case '2':
4963 case '3':
4964 case '4':
4965 case '5':
4966 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 case '7': *name = *p++ - '0';
4968 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 *name = (*name << 3) + *p++ - '0';
4971 if (*p >= '0' && *p <= '7')
4972 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 break;
4976
4977 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004978 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (extra != 0)
4980 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 break;
4983 }
4984 /* FALLTHROUGH */
4985
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988 }
4989 }
4990 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004995 if (*p != NUL) /* just in case */
4996 ++p;
4997 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return OK;
5000}
5001
5002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 * Return OK or FAIL.
5005 */
5006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005007get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005011 int reduce = 0;
5012
5013 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005015 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005016 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005021 break;
5022 ++reduce;
5023 ++p;
5024 }
5025 }
5026
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005030 return FAIL;
5031 }
5032
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005034 if (!evaluate)
5035 {
5036 *arg = p + 1;
5037 return OK;
5038 }
5039
5040 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005042 */
5043 str = alloc((unsigned)((p - *arg) - reduce));
5044 if (str == NULL)
5045 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 rettv->v_type = VAR_STRING;
5047 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005048
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005050 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054 break;
5055 ++p;
5056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 *arg = p + 1;
5061
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 return OK;
5063}
5064
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005065/*
5066 * Return the function name of the partial.
5067 */
5068 char_u *
5069partial_name(partial_T *pt)
5070{
5071 if (pt->pt_name != NULL)
5072 return pt->pt_name;
5073 return pt->pt_func->uf_name;
5074}
5075
Bram Moolenaarddecc252016-04-06 22:59:37 +02005076 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005077partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005078{
5079 int i;
5080
5081 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005083 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005084 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005085 if (pt->pt_name != NULL)
5086 {
5087 func_unref(pt->pt_name);
5088 vim_free(pt->pt_name);
5089 }
5090 else
5091 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005092 vim_free(pt);
5093}
5094
5095/*
5096 * Unreference a closure: decrement the reference count and free it when it
5097 * becomes zero.
5098 */
5099 void
5100partial_unref(partial_T *pt)
5101{
5102 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005103 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005104}
5105
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005106static int tv_equal_recurse_limit;
5107
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005108 static int
5109func_equal(
5110 typval_T *tv1,
5111 typval_T *tv2,
5112 int ic) /* ignore case */
5113{
5114 char_u *s1, *s2;
5115 dict_T *d1, *d2;
5116 int a1, a2;
5117 int i;
5118
5119 /* empty and NULL function name considered the same */
5120 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005121 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005122 if (s1 != NULL && *s1 == NUL)
5123 s1 = NULL;
5124 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005125 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005126 if (s2 != NULL && *s2 == NUL)
5127 s2 = NULL;
5128 if (s1 == NULL || s2 == NULL)
5129 {
5130 if (s1 != s2)
5131 return FALSE;
5132 }
5133 else if (STRCMP(s1, s2) != 0)
5134 return FALSE;
5135
5136 /* empty dict and NULL dict is different */
5137 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5138 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5139 if (d1 == NULL || d2 == NULL)
5140 {
5141 if (d1 != d2)
5142 return FALSE;
5143 }
5144 else if (!dict_equal(d1, d2, ic, TRUE))
5145 return FALSE;
5146
5147 /* empty list and no list considered the same */
5148 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5149 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5150 if (a1 != a2)
5151 return FALSE;
5152 for (i = 0; i < a1; ++i)
5153 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5154 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5155 return FALSE;
5156
5157 return TRUE;
5158}
5159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005160/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005161 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005162 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005164 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005166tv_equal(
5167 typval_T *tv1,
5168 typval_T *tv2,
5169 int ic, /* ignore case */
5170 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005171{
5172 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005173 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005174 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005175 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005176
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005177 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005178 * recursiveness to a limit. We guess they are equal then.
5179 * A fixed limit has the problem of still taking an awful long time.
5180 * Reduce the limit every time running into it. That should work fine for
5181 * deeply linked structures that are not recursively linked and catch
5182 * recursiveness quickly. */
5183 if (!recursive)
5184 tv_equal_recurse_limit = 1000;
5185 if (recursive_cnt >= tv_equal_recurse_limit)
5186 {
5187 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005188 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005189 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005190
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005191 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5192 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005193 if ((tv1->v_type == VAR_FUNC
5194 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5195 && (tv2->v_type == VAR_FUNC
5196 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5197 {
5198 ++recursive_cnt;
5199 r = func_equal(tv1, tv2, ic);
5200 --recursive_cnt;
5201 return r;
5202 }
5203
5204 if (tv1->v_type != tv2->v_type)
5205 return FALSE;
5206
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005207 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005208 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005209 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005210 ++recursive_cnt;
5211 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5212 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005213 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005214
5215 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005216 ++recursive_cnt;
5217 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5218 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005219 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005220
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005221 case VAR_NUMBER:
5222 return tv1->vval.v_number == tv2->vval.v_number;
5223
5224 case VAR_STRING:
5225 s1 = get_tv_string_buf(tv1, buf1);
5226 s2 = get_tv_string_buf(tv2, buf2);
5227 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005228
5229 case VAR_SPECIAL:
5230 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005231
5232 case VAR_FLOAT:
5233#ifdef FEAT_FLOAT
5234 return tv1->vval.v_float == tv2->vval.v_float;
5235#endif
5236 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005237#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005238 return tv1->vval.v_job == tv2->vval.v_job;
5239#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005240 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005241#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005242 return tv1->vval.v_channel == tv2->vval.v_channel;
5243#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005244 case VAR_FUNC:
5245 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005246 case VAR_UNKNOWN:
5247 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005248 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005249
Bram Moolenaara03f2332016-02-06 18:09:59 +01005250 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5251 * does not equal anything, not even itself. */
5252 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005253}
5254
5255/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005256 * Return the next (unique) copy ID.
5257 * Used for serializing nested structures.
5258 */
5259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005261{
5262 current_copyID += COPYID_INC;
5263 return current_copyID;
5264}
5265
5266/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005267 * Garbage collection for lists and dictionaries.
5268 *
5269 * We use reference counts to be able to free most items right away when they
5270 * are no longer used. But for composite items it's possible that it becomes
5271 * unused while the reference count is > 0: When there is a recursive
5272 * reference. Example:
5273 * :let l = [1, 2, 3]
5274 * :let d = {9: l}
5275 * :let l[1] = d
5276 *
5277 * Since this is quite unusual we handle this with garbage collection: every
5278 * once in a while find out which lists and dicts are not referenced from any
5279 * variable.
5280 *
5281 * Here is a good reference text about garbage collection (refers to Python
5282 * but it applies to all reference-counting mechanisms):
5283 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005284 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005285
5286/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005287 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005288 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005289 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005290 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005291 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005292garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005293{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005294 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005295 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005296 buf_T *buf;
5297 win_T *wp;
5298 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005299 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005300 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005301
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005302 if (!testing)
5303 {
5304 /* Only do this once. */
5305 want_garbage_collect = FALSE;
5306 may_garbage_collect = FALSE;
5307 garbage_collect_at_exit = FALSE;
5308 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005309
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005310 /* We advance by two because we add one for items referenced through
5311 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005312 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005313
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314 /*
5315 * 1. Go through all accessible variables and mark all lists and dicts
5316 * with copyID.
5317 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005318
5319 /* Don't free variables in the previous_funccal list unless they are only
5320 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005321 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005323
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005324 /* script-local variables */
5325 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327
5328 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005329 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005330 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5331 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005332
5333 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005334 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5336 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005337#ifdef FEAT_AUTOCMD
5338 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005339 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5340 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005341#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005342
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005343 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005344 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005345 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5346 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005347 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005349
5350 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005351 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005352
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005353 /* named functions (matters for closures) */
5354 abort = abort || set_ref_in_functions(copyID);
5355
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005356 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005357 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005358
Bram Moolenaard812df62008-11-09 12:46:09 +00005359 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005360 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005361
Bram Moolenaar1dced572012-04-05 16:54:08 +02005362#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005363 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005364#endif
5365
Bram Moolenaardb913952012-06-29 12:54:53 +02005366#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005367 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005368#endif
5369
5370#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005371 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005372#endif
5373
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005374#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005375 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005376 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005377#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005378#ifdef FEAT_NETBEANS_INTG
5379 abort = abort || set_ref_in_nb_channel(copyID);
5380#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005381
Bram Moolenaare3188e22016-05-31 21:13:04 +02005382#ifdef FEAT_TIMERS
5383 abort = abort || set_ref_in_timer(copyID);
5384#endif
5385
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005386#ifdef FEAT_QUICKFIX
5387 abort = abort || set_ref_in_quickfix(copyID);
5388#endif
5389
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005390#ifdef FEAT_TERMINAL
5391 abort = abort || set_ref_in_term(copyID);
5392#endif
5393
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005395 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 /*
5397 * 2. Free lists and dictionaries that are not referenced.
5398 */
5399 did_free = free_unref_items(copyID);
5400
5401 /*
5402 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005404 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005406 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005407 else if (p_verbose > 0)
5408 {
5409 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5410 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005411
5412 return did_free;
5413}
5414
5415/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005416 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005417 */
5418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005420{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005421 int did_free = FALSE;
5422
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 /* Let all "free" functions know that we are here. This means no
5424 * dictionaries, lists, channels or jobs are to be freed, because we will
5425 * do that here. */
5426 in_free_unref_items = TRUE;
5427
5428 /*
5429 * PASS 1: free the contents of the items. We don't free the items
5430 * themselves yet, so that it is possible to decrement refcount counters
5431 */
5432
Bram Moolenaarcd524592016-07-17 14:57:05 +02005433 /* Go through the list of dicts and free items without the copyID. */
5434 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005435
Bram Moolenaarda861d62016-07-17 15:46:27 +02005436 /* Go through the list of lists and free items without the copyID. */
5437 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005438
5439#ifdef FEAT_JOB_CHANNEL
5440 /* Go through the list of jobs and free items without the copyID. This
5441 * must happen before doing channels, because jobs refer to channels, but
5442 * the reference from the channel to the job isn't tracked. */
5443 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5444
5445 /* Go through the list of channels and free items without the copyID. */
5446 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5447#endif
5448
5449 /*
5450 * PASS 2: free the items themselves.
5451 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005452 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005453 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005454
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005455#ifdef FEAT_JOB_CHANNEL
5456 /* Go through the list of jobs and free items without the copyID. This
5457 * must happen before doing channels, because jobs refer to channels, but
5458 * the reference from the channel to the job isn't tracked. */
5459 free_unused_jobs(copyID, COPYID_MASK);
5460
5461 /* Go through the list of channels and free items without the copyID. */
5462 free_unused_channels(copyID, COPYID_MASK);
5463#endif
5464
5465 in_free_unref_items = FALSE;
5466
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005467 return did_free;
5468}
5469
5470/*
5471 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 * "list_stack" is used to add lists to be marked. Can be NULL.
5473 *
5474 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005475 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005477set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478{
5479 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005480 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005481 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005482 hashtab_T *cur_ht;
5483 ht_stack_T *ht_stack = NULL;
5484 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005485
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 cur_ht = ht;
5487 for (;;)
5488 {
5489 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005490 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005491 /* Mark each item in the hashtab. If the item contains a hashtab
5492 * it is added to ht_stack, if it contains a list it is added to
5493 * list_stack. */
5494 todo = (int)cur_ht->ht_used;
5495 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5496 if (!HASHITEM_EMPTY(hi))
5497 {
5498 --todo;
5499 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5500 &ht_stack, list_stack);
5501 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005502 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005503
5504 if (ht_stack == NULL)
5505 break;
5506
5507 /* take an item from the stack */
5508 cur_ht = ht_stack->ht;
5509 tempitem = ht_stack;
5510 ht_stack = ht_stack->prev;
5511 free(tempitem);
5512 }
5513
5514 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005515}
5516
5517/*
5518 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005519 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5520 *
5521 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005522 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005524set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005525{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005526 listitem_T *li;
5527 int abort = FALSE;
5528 list_T *cur_l;
5529 list_stack_T *list_stack = NULL;
5530 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005531
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005532 cur_l = l;
5533 for (;;)
5534 {
5535 if (!abort)
5536 /* Mark each item in the list. If the item contains a hashtab
5537 * it is added to ht_stack, if it contains a list it is added to
5538 * list_stack. */
5539 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5540 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5541 ht_stack, &list_stack);
5542 if (list_stack == NULL)
5543 break;
5544
5545 /* take an item from the stack */
5546 cur_l = list_stack->list;
5547 tempitem = list_stack;
5548 list_stack = list_stack->prev;
5549 free(tempitem);
5550 }
5551
5552 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005553}
5554
5555/*
5556 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005557 * "list_stack" is used to add lists to be marked. Can be NULL.
5558 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5559 *
5560 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005561 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005563set_ref_in_item(
5564 typval_T *tv,
5565 int copyID,
5566 ht_stack_T **ht_stack,
5567 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005568{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005569 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005570
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005571 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005572 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005573 dict_T *dd = tv->vval.v_dict;
5574
Bram Moolenaara03f2332016-02-06 18:09:59 +01005575 if (dd != NULL && dd->dv_copyID != copyID)
5576 {
5577 /* Didn't see this dict yet. */
5578 dd->dv_copyID = copyID;
5579 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005580 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005581 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5582 }
5583 else
5584 {
5585 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5586 if (newitem == NULL)
5587 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005588 else
5589 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005590 newitem->ht = &dd->dv_hashtab;
5591 newitem->prev = *ht_stack;
5592 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005593 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005594 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005595 }
5596 }
5597 else if (tv->v_type == VAR_LIST)
5598 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005599 list_T *ll = tv->vval.v_list;
5600
Bram Moolenaara03f2332016-02-06 18:09:59 +01005601 if (ll != NULL && ll->lv_copyID != copyID)
5602 {
5603 /* Didn't see this list yet. */
5604 ll->lv_copyID = copyID;
5605 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005606 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005607 abort = set_ref_in_list(ll, copyID, ht_stack);
5608 }
5609 else
5610 {
5611 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005612 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005613 if (newitem == NULL)
5614 abort = TRUE;
5615 else
5616 {
5617 newitem->list = ll;
5618 newitem->prev = *list_stack;
5619 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005620 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005621 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005622 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005623 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005624 else if (tv->v_type == VAR_FUNC)
5625 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005626 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005627 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005628 else if (tv->v_type == VAR_PARTIAL)
5629 {
5630 partial_T *pt = tv->vval.v_partial;
5631 int i;
5632
5633 /* A partial does not have a copyID, because it cannot contain itself.
5634 */
5635 if (pt != NULL)
5636 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005637 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005638
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005639 if (pt->pt_dict != NULL)
5640 {
5641 typval_T dtv;
5642
5643 dtv.v_type = VAR_DICT;
5644 dtv.vval.v_dict = pt->pt_dict;
5645 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5646 }
5647
5648 for (i = 0; i < pt->pt_argc; ++i)
5649 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5650 ht_stack, list_stack);
5651 }
5652 }
5653#ifdef FEAT_JOB_CHANNEL
5654 else if (tv->v_type == VAR_JOB)
5655 {
5656 job_T *job = tv->vval.v_job;
5657 typval_T dtv;
5658
5659 if (job != NULL && job->jv_copyID != copyID)
5660 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005661 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005662 if (job->jv_channel != NULL)
5663 {
5664 dtv.v_type = VAR_CHANNEL;
5665 dtv.vval.v_channel = job->jv_channel;
5666 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5667 }
5668 if (job->jv_exit_partial != NULL)
5669 {
5670 dtv.v_type = VAR_PARTIAL;
5671 dtv.vval.v_partial = job->jv_exit_partial;
5672 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5673 }
5674 }
5675 }
5676 else if (tv->v_type == VAR_CHANNEL)
5677 {
5678 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005679 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005680 typval_T dtv;
5681 jsonq_T *jq;
5682 cbq_T *cq;
5683
5684 if (ch != NULL && ch->ch_copyID != copyID)
5685 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005686 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005687 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005688 {
5689 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5690 jq = jq->jq_next)
5691 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5692 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5693 cq = cq->cq_next)
5694 if (cq->cq_partial != NULL)
5695 {
5696 dtv.v_type = VAR_PARTIAL;
5697 dtv.vval.v_partial = cq->cq_partial;
5698 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5699 }
5700 if (ch->ch_part[part].ch_partial != NULL)
5701 {
5702 dtv.v_type = VAR_PARTIAL;
5703 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5704 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5705 }
5706 }
5707 if (ch->ch_partial != NULL)
5708 {
5709 dtv.v_type = VAR_PARTIAL;
5710 dtv.vval.v_partial = ch->ch_partial;
5711 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5712 }
5713 if (ch->ch_close_partial != NULL)
5714 {
5715 dtv.v_type = VAR_PARTIAL;
5716 dtv.vval.v_partial = ch->ch_close_partial;
5717 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5718 }
5719 }
5720 }
5721#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005723}
5724
Bram Moolenaar17a13432016-01-24 14:22:10 +01005725 static char *
5726get_var_special_name(int nr)
5727{
5728 switch (nr)
5729 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005730 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005731 case VVAL_TRUE: return "v:true";
5732 case VVAL_NONE: return "v:none";
5733 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005734 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005735 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005736 return "42";
5737}
5738
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 * Return a string with the string representation of a variable.
5741 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005742 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005743 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005744 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5745 * stings as "string()", otherwise does not put quotes around strings, as
5746 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005747 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5748 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005749 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005751 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005752echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005753 typval_T *tv,
5754 char_u **tofree,
5755 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005756 int copyID,
5757 int echo_style,
5758 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005759 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005761 static int recurse = 0;
5762 char_u *r = NULL;
5763
Bram Moolenaar33570922005-01-25 22:26:29 +00005764 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005765 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005766 if (!did_echo_string_emsg)
5767 {
5768 /* Only give this message once for a recursive call to avoid
5769 * flooding the user with errors. And stop iterating over lists
5770 * and dicts. */
5771 did_echo_string_emsg = TRUE;
5772 EMSG(_("E724: variable nested too deep for displaying"));
5773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005774 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005775 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005776 }
5777 ++recurse;
5778
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 switch (tv->v_type)
5780 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005781 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005782 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005783 {
5784 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005785 r = tv->vval.v_string;
5786 if (r == NULL)
5787 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005788 }
5789 else
5790 {
5791 *tofree = string_quote(tv->vval.v_string, FALSE);
5792 r = *tofree;
5793 }
5794 break;
5795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005797 if (echo_style)
5798 {
5799 *tofree = NULL;
5800 r = tv->vval.v_string;
5801 }
5802 else
5803 {
5804 *tofree = string_quote(tv->vval.v_string, TRUE);
5805 r = *tofree;
5806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005807 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005808
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005809 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005810 {
5811 partial_T *pt = tv->vval.v_partial;
5812 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005813 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005814 garray_T ga;
5815 int i;
5816 char_u *tf;
5817
5818 ga_init2(&ga, 1, 100);
5819 ga_concat(&ga, (char_u *)"function(");
5820 if (fname != NULL)
5821 {
5822 ga_concat(&ga, fname);
5823 vim_free(fname);
5824 }
5825 if (pt != NULL && pt->pt_argc > 0)
5826 {
5827 ga_concat(&ga, (char_u *)", [");
5828 for (i = 0; i < pt->pt_argc; ++i)
5829 {
5830 if (i > 0)
5831 ga_concat(&ga, (char_u *)", ");
5832 ga_concat(&ga,
5833 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5834 vim_free(tf);
5835 }
5836 ga_concat(&ga, (char_u *)"]");
5837 }
5838 if (pt != NULL && pt->pt_dict != NULL)
5839 {
5840 typval_T dtv;
5841
5842 ga_concat(&ga, (char_u *)", ");
5843 dtv.v_type = VAR_DICT;
5844 dtv.vval.v_dict = pt->pt_dict;
5845 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5846 vim_free(tf);
5847 }
5848 ga_concat(&ga, (char_u *)")");
5849
5850 *tofree = ga.ga_data;
5851 r = *tofree;
5852 break;
5853 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005854
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005856 if (tv->vval.v_list == NULL)
5857 {
5858 *tofree = NULL;
5859 r = NULL;
5860 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005861 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5862 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005863 {
5864 *tofree = NULL;
5865 r = (char_u *)"[...]";
5866 }
5867 else
5868 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005869 int old_copyID = tv->vval.v_list->lv_copyID;
5870
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005871 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005872 *tofree = list2string(tv, copyID, restore_copyID);
5873 if (restore_copyID)
5874 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005875 r = *tofree;
5876 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005878
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005880 if (tv->vval.v_dict == NULL)
5881 {
5882 *tofree = NULL;
5883 r = NULL;
5884 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005885 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5886 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005887 {
5888 *tofree = NULL;
5889 r = (char_u *)"{...}";
5890 }
5891 else
5892 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005893 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005894 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005895 *tofree = dict2string(tv, copyID, restore_copyID);
5896 if (restore_copyID)
5897 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005898 r = *tofree;
5899 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005900 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005902 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005903 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005904 *tofree = NULL;
5905 r = get_tv_string_buf(tv, numbuf);
5906 break;
5907
Bram Moolenaar835dc632016-02-07 14:27:38 +01005908 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005909 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910 *tofree = NULL;
5911 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005912 if (composite_val)
5913 {
5914 *tofree = string_quote(r, FALSE);
5915 r = *tofree;
5916 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005918
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005919 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005920#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005921 *tofree = NULL;
5922 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5923 r = numbuf;
5924 break;
5925#endif
5926
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005927 case VAR_SPECIAL:
5928 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005929 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005930 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932
Bram Moolenaar8502c702014-06-17 12:51:16 +02005933 if (--recurse == 0)
5934 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936}
5937
5938/*
5939 * Return a string with the string representation of a variable.
5940 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5941 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005942 * Does not put quotes around strings, as ":echo" displays values.
5943 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5944 * May return NULL.
5945 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005946 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005947echo_string(
5948 typval_T *tv,
5949 char_u **tofree,
5950 char_u *numbuf,
5951 int copyID)
5952{
5953 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5954}
5955
5956/*
5957 * Return a string with the string representation of a variable.
5958 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5959 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005961 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005964tv2string(
5965 typval_T *tv,
5966 char_u **tofree,
5967 char_u *numbuf,
5968 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005970 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971}
5972
5973/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005974 * Return string "str" in ' quotes, doubling ' characters.
5975 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005978 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005980{
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982 char_u *p, *r, *s;
5983
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 len = (function ? 13 : 3);
5985 if (str != NULL)
5986 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005987 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005988 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 if (*p == '\'')
5990 ++len;
5991 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005992 s = r = alloc(len);
5993 if (r != NULL)
5994 {
5995 if (function)
5996 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005997 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005998 r += 10;
5999 }
6000 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006001 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 if (str != NULL)
6003 for (p = str; *p != NUL; )
6004 {
6005 if (*p == '\'')
6006 *r++ = '\'';
6007 MB_COPY_CHAR(p, r);
6008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 if (function)
6011 *r++ = ')';
6012 *r++ = NUL;
6013 }
6014 return s;
6015}
6016
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006017#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006018/*
6019 * Convert the string "text" to a floating point number.
6020 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6021 * this always uses a decimal point.
6022 * Returns the length of the text that was consumed.
6023 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025string2float(
6026 char_u *text,
6027 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006028{
6029 char *s = (char *)text;
6030 float_T f;
6031
Bram Moolenaar62473612017-01-08 19:25:40 +01006032 /* MS-Windows does not deal with "inf" and "nan" properly. */
6033 if (STRNICMP(text, "inf", 3) == 0)
6034 {
6035 *value = INFINITY;
6036 return 3;
6037 }
6038 if (STRNICMP(text, "-inf", 3) == 0)
6039 {
6040 *value = -INFINITY;
6041 return 4;
6042 }
6043 if (STRNICMP(text, "nan", 3) == 0)
6044 {
6045 *value = NAN;
6046 return 3;
6047 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006048 f = strtod(s, &s);
6049 *value = f;
6050 return (int)((char_u *)s - text);
6051}
6052#endif
6053
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 * Get the value of an environment variable.
6056 * "arg" is pointing to the '$'. It is advanced to after the name.
6057 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006058 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 */
6060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 char_u *string = NULL;
6064 int len;
6065 int cc;
6066 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006067 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
6069 ++*arg;
6070 name = *arg;
6071 len = get_env_len(arg);
6072 if (evaluate)
6073 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006074 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006075 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006076
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006077 cc = name[len];
6078 name[len] = NUL;
6079 /* first try vim_getenv(), fast for normal environment vars */
6080 string = vim_getenv(name, &mustfree);
6081 if (string != NULL && *string != NUL)
6082 {
6083 if (!mustfree)
6084 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006086 else
6087 {
6088 if (mustfree)
6089 vim_free(string);
6090
6091 /* next try expanding things like $VIM and ${HOME} */
6092 string = expand_env_save(name - 1);
6093 if (string != NULL && *string == '$')
6094 {
6095 vim_free(string);
6096 string = NULL;
6097 }
6098 }
6099 name[len] = cc;
6100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006101 rettv->v_type = VAR_STRING;
6102 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 }
6104
6105 return OK;
6106}
6107
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006108
6109
6110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006112 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006115var2fpos(
6116 typval_T *varp,
6117 int dollar_lnum, /* TRUE when $ is last line */
6118 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006120 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006122 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
Bram Moolenaara5525202006-03-02 22:52:09 +00006124 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006125 if (varp->v_type == VAR_LIST)
6126 {
6127 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006128 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006129 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006130 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006131
6132 l = varp->vval.v_list;
6133 if (l == NULL)
6134 return NULL;
6135
6136 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006137 pos.lnum = list_find_nr(l, 0L, &error);
6138 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006139 return NULL; /* invalid line number */
6140
6141 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006142 pos.col = list_find_nr(l, 1L, &error);
6143 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006144 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006145 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006146
6147 /* We accept "$" for the column number: last column. */
6148 li = list_find(l, 1L);
6149 if (li != NULL && li->li_tv.v_type == VAR_STRING
6150 && li->li_tv.vval.v_string != NULL
6151 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6152 pos.col = len + 1;
6153
Bram Moolenaara5525202006-03-02 22:52:09 +00006154 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006155 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006156 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006157 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006158
Bram Moolenaara5525202006-03-02 22:52:09 +00006159#ifdef FEAT_VIRTUALEDIT
6160 /* Get the virtual offset. Defaults to zero. */
6161 pos.coladd = list_find_nr(l, 2L, &error);
6162 if (error)
6163 pos.coladd = 0;
6164#endif
6165
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006166 return &pos;
6167 }
6168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006169 name = get_tv_string_chk(varp);
6170 if (name == NULL)
6171 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006172 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006174 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6175 {
6176 if (VIsual_active)
6177 return &VIsual;
6178 return &curwin->w_cursor;
6179 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006180 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006182 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6184 return NULL;
6185 return pp;
6186 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006187
6188#ifdef FEAT_VIRTUALEDIT
6189 pos.coladd = 0;
6190#endif
6191
Bram Moolenaar477933c2007-07-17 14:32:23 +00006192 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006193 {
6194 pos.col = 0;
6195 if (name[1] == '0') /* "w0": first visible line */
6196 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006198 /* In silent Ex mode topline is zero, but that's not a valid line
6199 * number; use one instead. */
6200 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006201 return &pos;
6202 }
6203 else if (name[1] == '$') /* "w$": last visible line */
6204 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006206 /* In silent Ex mode botline is zero, return zero then. */
6207 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006208 return &pos;
6209 }
6210 }
6211 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006213 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 pos.lnum = curbuf->b_ml.ml_line_count;
6216 pos.col = 0;
6217 }
6218 else
6219 {
6220 pos.lnum = curwin->w_cursor.lnum;
6221 pos.col = (colnr_T)STRLEN(ml_get_curline());
6222 }
6223 return &pos;
6224 }
6225 return NULL;
6226}
6227
6228/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006229 * Convert list in "arg" into a position and optional file number.
6230 * When "fnump" is NULL there is no file number, only 3 items.
6231 * Note that the column is passed on as-is, the caller may want to decrement
6232 * it to use 1 for the first column.
6233 * Return FAIL when conversion is not possible, doesn't check the position for
6234 * validity.
6235 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006237list2fpos(
6238 typval_T *arg,
6239 pos_T *posp,
6240 int *fnump,
6241 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006242{
6243 list_T *l = arg->vval.v_list;
6244 long i = 0;
6245 long n;
6246
Bram Moolenaar493c1782014-05-28 14:34:46 +02006247 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6248 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006249 if (arg->v_type != VAR_LIST
6250 || l == NULL
6251 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006252 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006253 return FAIL;
6254
6255 if (fnump != NULL)
6256 {
6257 n = list_find_nr(l, i++, NULL); /* fnum */
6258 if (n < 0)
6259 return FAIL;
6260 if (n == 0)
6261 n = curbuf->b_fnum; /* current buffer */
6262 *fnump = n;
6263 }
6264
6265 n = list_find_nr(l, i++, NULL); /* lnum */
6266 if (n < 0)
6267 return FAIL;
6268 posp->lnum = n;
6269
6270 n = list_find_nr(l, i++, NULL); /* col */
6271 if (n < 0)
6272 return FAIL;
6273 posp->col = n;
6274
6275#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006276 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006277 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006278 posp->coladd = 0;
6279 else
6280 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006281#endif
6282
Bram Moolenaar493c1782014-05-28 14:34:46 +02006283 if (curswantp != NULL)
6284 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6285
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006286 return OK;
6287}
6288
6289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 * Get the length of an environment variable name.
6291 * Advance "arg" to the first character after the name.
6292 * Return 0 for error.
6293 */
6294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006295get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 char_u *p;
6298 int len;
6299
6300 for (p = *arg; vim_isIDc(*p); ++p)
6301 ;
6302 if (p == *arg) /* no name found */
6303 return 0;
6304
6305 len = (int)(p - *arg);
6306 *arg = p;
6307 return len;
6308}
6309
6310/*
6311 * Get the length of the name of a function or internal variable.
6312 * "arg" is advanced to the first non-white character after the name.
6313 * Return 0 if something is wrong.
6314 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006315 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006316get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 char_u *p;
6319 int len;
6320
6321 /* Find the end of the name. */
6322 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006323 {
6324 if (*p == ':')
6325 {
6326 /* "s:" is start of "s:var", but "n:" is not and can be used in
6327 * slice "[n:]". Also "xx:" is not a namespace. */
6328 len = (int)(p - *arg);
6329 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6330 || len > 1)
6331 break;
6332 }
6333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (p == *arg) /* no name found */
6335 return 0;
6336
6337 len = (int)(p - *arg);
6338 *arg = skipwhite(p);
6339
6340 return len;
6341}
6342
6343/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006344 * Get the length of the name of a variable or function.
6345 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006347 * Return -1 if curly braces expansion failed.
6348 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 * If the name contains 'magic' {}'s, expand them and return the
6350 * expanded name in an allocated string via 'alias' - caller must free.
6351 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353get_name_len(
6354 char_u **arg,
6355 char_u **alias,
6356 int evaluate,
6357 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 char_u *p;
6361 char_u *expr_start;
6362 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
6364 *alias = NULL; /* default to no alias */
6365
6366 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6367 && (*arg)[2] == (int)KE_SNR)
6368 {
6369 /* hard coded <SNR>, already translated */
6370 *arg += 3;
6371 return get_id_len(arg) + 3;
6372 }
6373 len = eval_fname_script(*arg);
6374 if (len > 0)
6375 {
6376 /* literal "<SID>", "s:" or "<SNR>" */
6377 *arg += len;
6378 }
6379
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006381 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006383 p = find_name_end(*arg, &expr_start, &expr_end,
6384 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (expr_start != NULL)
6386 {
6387 char_u *temp_string;
6388
6389 if (!evaluate)
6390 {
6391 len += (int)(p - *arg);
6392 *arg = skipwhite(p);
6393 return len;
6394 }
6395
6396 /*
6397 * Include any <SID> etc in the expanded string:
6398 * Thus the -len here.
6399 */
6400 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6401 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006402 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 *alias = temp_string;
6404 *arg = skipwhite(p);
6405 return (int)STRLEN(temp_string);
6406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006409 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 EMSG2(_(e_invexpr2), *arg);
6411
6412 return len;
6413}
6414
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006415/*
6416 * Find the end of a variable or function name, taking care of magic braces.
6417 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6418 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006419 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 * Return a pointer to just after the name. Equal to "arg" if there is no
6421 * valid name.
6422 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006424find_name_end(
6425 char_u *arg,
6426 char_u **expr_start,
6427 char_u **expr_end,
6428 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006430 int mb_nest = 0;
6431 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006433 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006437 *expr_start = NULL;
6438 *expr_end = NULL;
6439 }
6440
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006441 /* Quick check for valid starting character. */
6442 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6443 return arg;
6444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006445 for (p = arg; *p != NUL
6446 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006447 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006448 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006449 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006450 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006451 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006452 if (*p == '\'')
6453 {
6454 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006455 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006456 ;
6457 if (*p == NUL)
6458 break;
6459 }
6460 else if (*p == '"')
6461 {
6462 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006463 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006464 if (*p == '\\' && p[1] != NUL)
6465 ++p;
6466 if (*p == NUL)
6467 break;
6468 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006469 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6470 {
6471 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006472 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006473 len = (int)(p - arg);
6474 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006475 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006476 break;
6477 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006479 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481 if (*p == '[')
6482 ++br_nest;
6483 else if (*p == ']')
6484 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006489 if (*p == '{')
6490 {
6491 mb_nest++;
6492 if (expr_start != NULL && *expr_start == NULL)
6493 *expr_start = p;
6494 }
6495 else if (*p == '}')
6496 {
6497 mb_nest--;
6498 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6499 *expr_end = p;
6500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 }
6503
6504 return p;
6505}
6506
6507/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006508 * Expands out the 'magic' {}'s in a variable/function name.
6509 * Note that this can call itself recursively, to deal with
6510 * constructs like foo{bar}{baz}{bam}
6511 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6512 * "in_start" ^
6513 * "expr_start" ^
6514 * "expr_end" ^
6515 * "in_end" ^
6516 *
6517 * Returns a new allocated string, which the caller must free.
6518 * Returns NULL for failure.
6519 */
6520 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521make_expanded_name(
6522 char_u *in_start,
6523 char_u *expr_start,
6524 char_u *expr_end,
6525 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006526{
6527 char_u c1;
6528 char_u *retval = NULL;
6529 char_u *temp_result;
6530 char_u *nextcmd = NULL;
6531
6532 if (expr_end == NULL || in_end == NULL)
6533 return NULL;
6534 *expr_start = NUL;
6535 *expr_end = NUL;
6536 c1 = *in_end;
6537 *in_end = NUL;
6538
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006539 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006540 if (temp_result != NULL && nextcmd == NULL)
6541 {
6542 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6543 + (in_end - expr_end) + 1));
6544 if (retval != NULL)
6545 {
6546 STRCPY(retval, in_start);
6547 STRCAT(retval, temp_result);
6548 STRCAT(retval, expr_end + 1);
6549 }
6550 }
6551 vim_free(temp_result);
6552
6553 *in_end = c1; /* put char back for error messages */
6554 *expr_start = '{';
6555 *expr_end = '}';
6556
6557 if (retval != NULL)
6558 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006559 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006560 if (expr_start != NULL)
6561 {
6562 /* Further expansion! */
6563 temp_result = make_expanded_name(retval, expr_start,
6564 expr_end, temp_result);
6565 vim_free(retval);
6566 retval = temp_result;
6567 }
6568 }
6569
6570 return retval;
6571}
6572
6573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006578eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006580 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6581}
6582
6583/*
6584 * Return TRUE if character "c" can be used as the first character in a
6585 * variable or function name (excluding '{' and '}').
6586 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006588eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006589{
6590 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591}
6592
6593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 * Set number v: variable to "val".
6595 */
6596 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006597set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006599 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600}
6601
6602/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006603 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006605 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006606get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
6610
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006611/*
6612 * Get string v: variable value. Uses a static buffer, can only be used once.
6613 */
6614 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006615get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006616{
6617 return get_tv_string(&vimvars[idx].vv_tv);
6618}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006621 * Get List v: variable value. Caller must take care of reference count when
6622 * needed.
6623 */
6624 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006626{
6627 return vimvars[idx].vv_list;
6628}
6629
6630/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006631 * Set v:char to character "c".
6632 */
6633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006635{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006636 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006637
6638#ifdef FEAT_MBYTE
6639 if (has_mbyte)
6640 buf[(*mb_char2bytes)(c, buf)] = NUL;
6641 else
6642#endif
6643 {
6644 buf[0] = c;
6645 buf[1] = NUL;
6646 }
6647 set_vim_var_string(VV_CHAR, buf, -1);
6648}
6649
6650/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006651 * Set v:count to "count" and v:count1 to "count1".
6652 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
6654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006655set_vcount(
6656 long count,
6657 long count1,
6658 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006660 if (set_prevcount)
6661 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006662 vimvars[VV_COUNT].vv_nr = count;
6663 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
6666/*
6667 * Set string v: variable to a copy of "val".
6668 */
6669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670set_vim_var_string(
6671 int idx,
6672 char_u *val,
6673 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
Bram Moolenaara542c682016-01-31 16:28:04 +01006675 clear_tv(&vimvars[idx].vv_di.di_tv);
6676 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006678 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683}
6684
6685/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006686 * Set List v: variable to "val".
6687 */
6688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006689set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006690{
Bram Moolenaara542c682016-01-31 16:28:04 +01006691 clear_tv(&vimvars[idx].vv_di.di_tv);
6692 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006693 vimvars[idx].vv_list = val;
6694 if (val != NULL)
6695 ++val->lv_refcount;
6696}
6697
6698/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006699 * Set Dictionary v: variable to "val".
6700 */
6701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006703{
6704 int todo;
6705 hashitem_T *hi;
6706
Bram Moolenaara542c682016-01-31 16:28:04 +01006707 clear_tv(&vimvars[idx].vv_di.di_tv);
6708 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006709 vimvars[idx].vv_dict = val;
6710 if (val != NULL)
6711 {
6712 ++val->dv_refcount;
6713
6714 /* Set readonly */
6715 todo = (int)val->dv_hashtab.ht_used;
6716 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6717 {
6718 if (HASHITEM_EMPTY(hi))
6719 continue;
6720 --todo;
Bram Moolenaar14c2e182017-02-25 21:39:17 +01006721 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006722 }
6723 }
6724}
6725
6726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 * Set v:register if needed.
6728 */
6729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006730set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 char_u regname;
6733
6734 if (c == 0 || c == ' ')
6735 regname = '"';
6736 else
6737 regname = c;
6738 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 set_vim_var_string(VV_REG, &regname, 1);
6741}
6742
6743/*
6744 * Get or set v:exception. If "oldval" == NULL, return the current value.
6745 * Otherwise, restore the value to "oldval" and return NULL.
6746 * Must always be called in pairs to save and restore v:exception! Does not
6747 * take care of memory allocations.
6748 */
6749 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006750v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006753 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 return NULL;
6757}
6758
6759/*
6760 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6761 * Otherwise, restore the value to "oldval" and return NULL.
6762 * Must always be called in pairs to save and restore v:throwpoint! Does not
6763 * take care of memory allocations.
6764 */
6765 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006766v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 return NULL;
6773}
6774
6775#if defined(FEAT_AUTOCMD) || defined(PROTO)
6776/*
6777 * Set v:cmdarg.
6778 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6779 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6780 * Must always be called in pairs!
6781 */
6782 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006783set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 char_u *oldval;
6786 char_u *newval;
6787 unsigned len;
6788
Bram Moolenaare9a41262005-01-15 22:18:47 +00006789 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006790 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006792 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006793 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006794 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 }
6796
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006797 if (eap->force_bin == FORCE_BIN)
6798 len = 6;
6799 else if (eap->force_bin == FORCE_NOBIN)
6800 len = 8;
6801 else
6802 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006803
6804 if (eap->read_edit)
6805 len += 7;
6806
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006807 if (eap->force_ff != 0)
6808 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6809# ifdef FEAT_MBYTE
6810 if (eap->force_enc != 0)
6811 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006812 if (eap->bad_char != 0)
6813 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006814# endif
6815
6816 newval = alloc(len + 1);
6817 if (newval == NULL)
6818 return NULL;
6819
6820 if (eap->force_bin == FORCE_BIN)
6821 sprintf((char *)newval, " ++bin");
6822 else if (eap->force_bin == FORCE_NOBIN)
6823 sprintf((char *)newval, " ++nobin");
6824 else
6825 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826
6827 if (eap->read_edit)
6828 STRCAT(newval, " ++edit");
6829
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006830 if (eap->force_ff != 0)
6831 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6832 eap->cmd + eap->force_ff);
6833# ifdef FEAT_MBYTE
6834 if (eap->force_enc != 0)
6835 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6836 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006837 if (eap->bad_char == BAD_KEEP)
6838 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6839 else if (eap->bad_char == BAD_DROP)
6840 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6841 else if (eap->bad_char != 0)
6842 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006843# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006844 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006845 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846}
6847#endif
6848
6849/*
6850 * Get the value of internal variable "name".
6851 * Return OK or FAIL.
6852 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006854get_var_tv(
6855 char_u *name,
6856 int len, /* length of "name" */
6857 typval_T *rettv, /* NULL when only checking existence */
6858 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6859 int verbose, /* may give error message */
6860 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867 /* truncate the name, so that we can use strcmp() */
6868 cc = name[len];
6869 name[len] = NUL;
6870
6871 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 * Check for user-defined variables.
6873 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006874 v = find_var(name, NULL, no_autoload);
6875 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006877 tv = &v->di_tv;
6878 if (dip != NULL)
6879 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006884 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006885 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 ret = FAIL;
6887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006888 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890
6891 name[len] = cc;
6892
6893 return ret;
6894}
6895
6896/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006897 * Check if variable "name[len]" is a local variable or an argument.
6898 * If so, "*eval_lavars_used" is set to TRUE.
6899 */
6900 static void
6901check_vars(char_u *name, int len)
6902{
6903 int cc;
6904 char_u *varname;
6905 hashtab_T *ht;
6906
6907 if (eval_lavars_used == NULL)
6908 return;
6909
6910 /* truncate the name, so that we can use strcmp() */
6911 cc = name[len];
6912 name[len] = NUL;
6913
6914 ht = find_var_ht(name, &varname);
6915 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6916 {
6917 if (find_var(name, NULL, TRUE) != NULL)
6918 *eval_lavars_used = TRUE;
6919 }
6920
6921 name[len] = cc;
6922}
6923
6924/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006925 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6926 * Also handle function call with Funcref variable: func(expr)
6927 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6928 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006930handle_subscript(
6931 char_u **arg,
6932 typval_T *rettv,
6933 int evaluate, /* do more than finding the end */
6934 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006935{
6936 int ret = OK;
6937 dict_T *selfdict = NULL;
6938 char_u *s;
6939 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006940 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006941
6942 while (ret == OK
6943 && (**arg == '['
6944 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006945 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6946 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01006947 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006948 {
6949 if (**arg == '(')
6950 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006951 partial_T *pt = NULL;
6952
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006954 if (evaluate)
6955 {
6956 functv = *rettv;
6957 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006958
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006959 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006960 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006961 {
6962 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006963 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006964 }
6965 else
6966 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006967 }
6968 else
6969 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006970 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006972 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973
6974 /* Clear the funcref afterwards, so that deleting it while
6975 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006976 if (evaluate)
6977 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006978
6979 /* Stop the expression evaluation when immediately aborting on
6980 * error, or when an interrupt occurred or an exception was thrown
6981 * but not caught. */
6982 if (aborting())
6983 {
6984 if (ret == OK)
6985 clear_tv(rettv);
6986 ret = FAIL;
6987 }
6988 dict_unref(selfdict);
6989 selfdict = NULL;
6990 }
6991 else /* **arg == '[' || **arg == '.' */
6992 {
6993 dict_unref(selfdict);
6994 if (rettv->v_type == VAR_DICT)
6995 {
6996 selfdict = rettv->vval.v_dict;
6997 if (selfdict != NULL)
6998 ++selfdict->dv_refcount;
6999 }
7000 else
7001 selfdict = NULL;
7002 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7003 {
7004 clear_tv(rettv);
7005 ret = FAIL;
7006 }
7007 }
7008 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007009
Bram Moolenaar1d429612016-05-24 15:44:17 +02007010 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7011 * Don't do this when "Func" is already a partial that was bound
7012 * explicitly (pt_auto is FALSE). */
7013 if (selfdict != NULL
7014 && (rettv->v_type == VAR_FUNC
7015 || (rettv->v_type == VAR_PARTIAL
7016 && (rettv->vval.v_partial->pt_auto
7017 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007018 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007019
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020 dict_unref(selfdict);
7021 return ret;
7022}
7023
7024/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007025 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007026 * value).
7027 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007028 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007029alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007030{
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007032}
7033
7034/*
7035 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 * The string "s" must have been allocated, it is consumed.
7037 * Return NULL for out of memory, the variable otherwise.
7038 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007040alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007044 rettv = alloc_tv();
7045 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007047 rettv->v_type = VAR_STRING;
7048 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050 else
7051 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007052 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
7055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007056 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 if (varp != NULL)
7062 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007063 switch (varp->v_type)
7064 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007065 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007066 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007067 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007068 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007069 vim_free(varp->vval.v_string);
7070 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007071 case VAR_PARTIAL:
7072 partial_unref(varp->vval.v_partial);
7073 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007074 case VAR_LIST:
7075 list_unref(varp->vval.v_list);
7076 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007077 case VAR_DICT:
7078 dict_unref(varp->vval.v_dict);
7079 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007080 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007081#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007082 job_unref(varp->vval.v_job);
7083 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007084#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007085 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007086#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007087 channel_unref(varp->vval.v_channel);
7088 break;
7089#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007090 case VAR_NUMBER:
7091 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007092 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007093 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007094 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 vim_free(varp);
7097 }
7098}
7099
7100/*
7101 * Free the memory for a variable value and set the value to NULL or 0.
7102 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 if (varp != NULL)
7107 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007110 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007111 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007112 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007113 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 vim_free(varp->vval.v_string);
7115 varp->vval.v_string = NULL;
7116 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007117 case VAR_PARTIAL:
7118 partial_unref(varp->vval.v_partial);
7119 varp->vval.v_partial = NULL;
7120 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 case VAR_LIST:
7122 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007124 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 case VAR_DICT:
7126 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007129 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007130 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007131 varp->vval.v_number = 0;
7132 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007133 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007134#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007135 varp->vval.v_float = 0.0;
7136 break;
7137#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007138 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007140 job_unref(varp->vval.v_job);
7141 varp->vval.v_job = NULL;
7142#endif
7143 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007144 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007146 channel_unref(varp->vval.v_channel);
7147 varp->vval.v_channel = NULL;
7148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149 case VAR_UNKNOWN:
7150 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007152 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
7154}
7155
7156/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157 * Set the value of a variable to NULL without freeing items.
7158 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007160init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161{
7162 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007164}
7165
7166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 * Get the number value of a variable.
7168 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007169 * For incompatible types, return 0.
7170 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7171 * caller of incompatible types: it sets *denote to TRUE if "denote"
7172 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007174 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007175get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007177 int error = FALSE;
7178
7179 return get_tv_number_chk(varp, &error); /* return 0L on error */
7180}
7181
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007182 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007183get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007184{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007185 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007187 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007189 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007190 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007191 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007192#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007193 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007194 break;
7195#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007196 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007197 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007198 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 break;
7200 case VAR_STRING:
7201 if (varp->vval.v_string != NULL)
7202 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007203 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007204 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007205 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007206 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007207 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007208 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007209 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007210 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007211 case VAR_SPECIAL:
7212 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7213 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007214 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007215#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007216 EMSG(_("E910: Using a Job as a Number"));
7217 break;
7218#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007219 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007220#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007221 EMSG(_("E913: Using a Channel as a Number"));
7222 break;
7223#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007224 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007225 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007228 if (denote == NULL) /* useful for values that must be unsigned */
7229 n = -1;
7230 else
7231 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007232 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233}
7234
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007235#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007238{
7239 switch (varp->v_type)
7240 {
7241 case VAR_NUMBER:
7242 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007243 case VAR_FLOAT:
7244 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007245 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007246 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007247 EMSG(_("E891: Using a Funcref as a Float"));
7248 break;
7249 case VAR_STRING:
7250 EMSG(_("E892: Using a String as a Float"));
7251 break;
7252 case VAR_LIST:
7253 EMSG(_("E893: Using a List as a Float"));
7254 break;
7255 case VAR_DICT:
7256 EMSG(_("E894: Using a Dictionary as a Float"));
7257 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007258 case VAR_SPECIAL:
7259 EMSG(_("E907: Using a special value as a Float"));
7260 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007261 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007262# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007263 EMSG(_("E911: Using a Job as a Float"));
7264 break;
7265# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007266 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007267# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007268 EMSG(_("E914: Using a Channel as a Float"));
7269 break;
7270# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007271 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007272 internal_error("get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007273 break;
7274 }
7275 return 0;
7276}
7277#endif
7278
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 * Get the string value of a variable.
7281 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007282 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7283 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 * If the String variable has never been set, return an empty string.
7285 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007286 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7287 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007289 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 static char_u mybuf[NUMBUFLEN];
7293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007294 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295}
7296
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007297 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007298get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007300 char_u *res = get_tv_string_buf_chk(varp, buf);
7301
7302 return res != NULL ? res : (char_u *)"";
7303}
7304
Bram Moolenaar7d647822014-04-05 21:28:56 +02007305/*
7306 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7307 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007308 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007309get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007310{
7311 static char_u mybuf[NUMBUFLEN];
7312
7313 return get_tv_string_buf_chk(varp, mybuf);
7314}
7315
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007317get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007321 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007322 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7323 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007324 return buf;
7325 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007326 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007327 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007328 break;
7329 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007330 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 break;
7332 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007333 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007334 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007335 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007336#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007337 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007338 break;
7339#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 case VAR_STRING:
7341 if (varp->vval.v_string != NULL)
7342 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007343 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007344 case VAR_SPECIAL:
7345 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7346 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007347 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007348#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007349 {
7350 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007351 char *status;
7352
7353 if (job == NULL)
7354 return (char_u *)"no process";
7355 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007356 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007357 : "run";
7358# ifdef UNIX
7359 vim_snprintf((char *)buf, NUMBUFLEN,
7360 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007361# elif defined(WIN32)
7362 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007363 "process %ld %s",
7364 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007365 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007366# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007367 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007368 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7369# endif
7370 return buf;
7371 }
7372#endif
7373 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007374 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007375#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007376 {
7377 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007378 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007379
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007380 if (channel == NULL)
7381 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7382 else
7383 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007384 "channel %d %s", channel->ch_id, status);
7385 return buf;
7386 }
7387#endif
7388 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007389 case VAR_UNKNOWN:
7390 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007393 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394}
7395
7396/*
7397 * Find variable "name" in the list of variables.
7398 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007400 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007403 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007408 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
Bram Moolenaara7043832005-01-21 11:56:39 +00007410 ht = find_var_ht(name, &varname);
7411 if (htp != NULL)
7412 *htp = ht;
7413 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007415 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7416 if (ret != NULL)
7417 return ret;
7418
7419 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007420 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
7423/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007424 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007425 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428find_var_in_ht(
7429 hashtab_T *ht,
7430 int htname,
7431 char_u *varname,
7432 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007433{
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 hashitem_T *hi;
7435
7436 if (*varname == NUL)
7437 {
7438 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007439 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007441 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 case 'g': return &globvars_var;
7443 case 'v': return &vimvars_var;
7444 case 'b': return &curbuf->b_bufvar;
7445 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007447 case 'l': return get_funccal_local_var();
7448 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 }
7450 return NULL;
7451 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007452
7453 hi = hash_find(ht, varname);
7454 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007455 {
7456 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007457 * worked find the variable again. Don't auto-load a script if it was
7458 * loaded already, otherwise it would be loaded every time when
7459 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007460 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007461 {
7462 /* Note: script_autoload() may make "hi" invalid. It must either
7463 * be obtained again or not used. */
7464 if (!script_autoload(varname, FALSE) || aborting())
7465 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007466 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007467 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007468 if (HASHITEM_EMPTY(hi))
7469 return NULL;
7470 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007472}
7473
7474/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007476 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007477 * Set "varname" to the start of name without ':'.
7478 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007479 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007482 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007483 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007484
Bram Moolenaar73627d02015-08-11 15:46:09 +02007485 if (name[0] == NUL)
7486 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (name[1] != ':')
7488 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007489 /* The name must not start with a colon or #. */
7490 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 return NULL;
7492 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007493
7494 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007495 hi = hash_find(&compat_hashtab, name);
7496 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007497 return &compat_hashtab;
7498
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007499 ht = get_funccal_local_ht();
7500 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007502 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007505 if (*name == 'g') /* global variable */
7506 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007507 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7508 */
7509 if (vim_strchr(name + 2, ':') != NULL
7510 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007511 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007513 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007515 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007516 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007517 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 if (*name == 'v') /* v: variable */
7519 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007520 if (*name == 'a') /* a: function argument */
7521 return get_funccal_args_ht();
7522 if (*name == 'l') /* l: local function variable */
7523 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 if (*name == 's' /* script variable */
7525 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7526 return &SCRIPT_VARS(current_SID);
7527 return NULL;
7528}
7529
7530/*
7531 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007532 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 * Returns NULL when it doesn't exist.
7534 */
7535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007540 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 if (v == NULL)
7542 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544}
7545
7546/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 * sourcing this script and when executing functions defined in the script.
7549 */
7550 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007551new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
Bram Moolenaara7043832005-01-21 11:56:39 +00007553 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hashtab_T *ht;
7555 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7558 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007559 /* Re-allocating ga_data means that an ht_array pointing to
7560 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007562 for (i = 1; i <= ga_scripts.ga_len; ++i)
7563 {
7564 ht = &SCRIPT_VARS(i);
7565 if (ht->ht_mask == HT_INIT_SIZE - 1)
7566 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007567 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007569 }
7570
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 while (ga_scripts.ga_len < id)
7572 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007573 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007574 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007575 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
7578 }
7579}
7580
7581/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7583 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 */
7585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007586init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007589 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007590 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007591 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007592 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 dict_var->di_tv.vval.v_dict = dict;
7594 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007595 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7597 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007601 * Unreference a dictionary initialized by init_var_dict().
7602 */
7603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007605{
7606 /* Now the dict needs to be freed if no one else is using it, go back to
7607 * normal reference counting. */
7608 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7609 dict_unref(dict);
7610}
7611
7612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 * Frees all allocated variables and the value they contain.
7615 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 */
7617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007618vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619{
7620 vars_clear_ext(ht, TRUE);
7621}
7622
7623/*
7624 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7625 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007627vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaara7043832005-01-21 11:56:39 +00007629 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 hashitem_T *hi;
7631 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007634 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007635 for (hi = ht->ht_array; todo > 0; ++hi)
7636 {
7637 if (!HASHITEM_EMPTY(hi))
7638 {
7639 --todo;
7640
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007642 * ht_array might change then. hash_clear() takes care of it
7643 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 v = HI2DI(hi);
7645 if (free_val)
7646 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007647 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007649 }
7650 }
7651 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007652 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
Bram Moolenaara7043832005-01-21 11:56:39 +00007655/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 * Delete a variable from hashtab "ht" at item "hi".
7657 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007660delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007663
7664 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 clear_tv(&di->di_tv);
7666 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667}
7668
7669/*
7670 * List the value of one internal variable.
7671 */
7672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007673list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 char_u *tofree;
7676 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007677 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007678
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007679 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007681 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007682 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683}
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686list_one_var_a(
7687 char_u *prefix,
7688 char_u *name,
7689 int type,
7690 char_u *string,
7691 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
Bram Moolenaar31859182007-08-14 20:41:13 +00007693 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7694 msg_start();
7695 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (name != NULL) /* "a:" vars don't have a name stored */
7697 msg_puts(name);
7698 msg_putchar(' ');
7699 msg_advance(22);
7700 if (type == VAR_NUMBER)
7701 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007702 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007703 msg_putchar('*');
7704 else if (type == VAR_LIST)
7705 {
7706 msg_putchar('[');
7707 if (*string == '[')
7708 ++string;
7709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 else if (type == VAR_DICT)
7711 {
7712 msg_putchar('{');
7713 if (*string == '{')
7714 ++string;
7715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 else
7717 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007720
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007721 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007723 if (*first)
7724 {
7725 msg_clr_eos();
7726 *first = FALSE;
7727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
7730/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 * If the variable already exists, the value is updated.
7733 * Otherwise the variable is created.
7734 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736set_var(
7737 char_u *name,
7738 typval_T *tv,
7739 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007745 ht = find_var_ht(name, &varname);
7746 if (ht == NULL || *varname == NUL)
7747 {
7748 EMSG2(_(e_illvar), name);
7749 return;
7750 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007751 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007752
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007753 /* Search in parent scope which is possible to reference from lambda */
7754 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007755 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007756
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007757 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7758 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007759 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007760
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007764 if (var_check_ro(v->di_flags, name, FALSE)
7765 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007767
7768 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007769 * Handle setting internal v: variables separately where needed to
7770 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 */
7772 if (ht == &vimvarht)
7773 {
7774 if (v->di_tv.v_type == VAR_STRING)
7775 {
7776 vim_free(v->di_tv.vval.v_string);
7777 if (copy || tv->v_type != VAR_STRING)
7778 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7779 else
7780 {
7781 /* Take over the string to avoid an extra alloc/free. */
7782 v->di_tv.vval.v_string = tv->vval.v_string;
7783 tv->vval.v_string = NULL;
7784 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007785 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007787 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007788 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007790 if (STRCMP(varname, "searchforward") == 0)
7791 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007792#ifdef FEAT_SEARCH_EXTRA
7793 else if (STRCMP(varname, "hlsearch") == 0)
7794 {
7795 no_hlsearch = !v->di_tv.vval.v_number;
7796 redraw_all_later(SOME_VALID);
7797 }
7798#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007799 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007800 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007801 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007802 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 }
7804
7805 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807 else /* add a new variable */
7808 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007809 /* Can't add "v:" variable. */
7810 if (ht == &vimvarht)
7811 {
7812 EMSG2(_(e_illvar), name);
7813 return;
7814 }
7815
Bram Moolenaar92124a32005-06-17 22:03:40 +00007816 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007817 if (!valid_varname(varname))
7818 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007819
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007820 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7821 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007822 if (v == NULL)
7823 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007827 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 return;
7829 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007830 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007832
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007833 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007835 else
7836 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007837 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007838 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841}
7842
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007844 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 * Also give an error message.
7846 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007849{
7850 if (flags & DI_FLAGS_RO)
7851 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007852 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 return TRUE;
7854 }
7855 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7856 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007857 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 return TRUE;
7859 }
7860 return FALSE;
7861}
7862
7863/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007864 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7865 * Also give an error message.
7866 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007868var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007869{
7870 if (flags & DI_FLAGS_FIX)
7871 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007872 EMSG2(_("E795: Cannot delete variable %s"),
7873 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007874 return TRUE;
7875 }
7876 return FALSE;
7877}
7878
7879/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007880 * Check if a funcref is assigned to a valid variable name.
7881 * Return TRUE and give an error if not.
7882 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884var_check_func_name(
7885 char_u *name, /* points to start of variable name */
7886 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007887{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007888 /* Allow for w: b: s: and t:. */
7889 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007890 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7891 ? name[2] : name[0]))
7892 {
7893 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7894 name);
7895 return TRUE;
7896 }
7897 /* Don't allow hiding a function. When "v" is not NULL we might be
7898 * assigning another function to the same var, the type is checked
7899 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007900 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007901 {
7902 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7903 name);
7904 return TRUE;
7905 }
7906 return FALSE;
7907}
7908
7909/*
7910 * Check if a variable name is valid.
7911 * Return FALSE and give an error if not.
7912 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007914valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007915{
7916 char_u *p;
7917
7918 for (p = varname; *p != NUL; ++p)
7919 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7920 && *p != AUTOLOAD_CHAR)
7921 {
7922 EMSG2(_(e_illvar), varname);
7923 return FALSE;
7924 }
7925 return TRUE;
7926}
7927
7928/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007929 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007930 * Also give an error message, using "name" or _("name") when use_gettext is
7931 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007932 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007933 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007934tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007935{
7936 if (lock & VAR_LOCKED)
7937 {
7938 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007939 name == NULL ? (char_u *)_("Unknown")
7940 : use_gettext ? (char_u *)_(name)
7941 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007942 return TRUE;
7943 }
7944 if (lock & VAR_FIXED)
7945 {
7946 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007947 name == NULL ? (char_u *)_("Unknown")
7948 : use_gettext ? (char_u *)_(name)
7949 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007950 return TRUE;
7951 }
7952 return FALSE;
7953}
7954
7955/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007957 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007959 * It is OK for "from" and "to" to point to the same item. This is used to
7960 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007963copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007965 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007966 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 switch (from->v_type)
7968 {
7969 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007970 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007971 to->vval.v_number = from->vval.v_number;
7972 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007973 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007974#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975 to->vval.v_float = from->vval.v_float;
7976 break;
7977#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007978 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007979#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007980 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007981 if (to->vval.v_job != NULL)
7982 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007983 break;
7984#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007985 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007986#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007987 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007988 if (to->vval.v_channel != NULL)
7989 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007990 break;
7991#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007992 case VAR_STRING:
7993 case VAR_FUNC:
7994 if (from->vval.v_string == NULL)
7995 to->vval.v_string = NULL;
7996 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007998 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007999 if (from->v_type == VAR_FUNC)
8000 func_ref(to->vval.v_string);
8001 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008003 case VAR_PARTIAL:
8004 if (from->vval.v_partial == NULL)
8005 to->vval.v_partial = NULL;
8006 else
8007 {
8008 to->vval.v_partial = from->vval.v_partial;
8009 ++to->vval.v_partial->pt_refcount;
8010 }
8011 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008012 case VAR_LIST:
8013 if (from->vval.v_list == NULL)
8014 to->vval.v_list = NULL;
8015 else
8016 {
8017 to->vval.v_list = from->vval.v_list;
8018 ++to->vval.v_list->lv_refcount;
8019 }
8020 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021 case VAR_DICT:
8022 if (from->vval.v_dict == NULL)
8023 to->vval.v_dict = NULL;
8024 else
8025 {
8026 to->vval.v_dict = from->vval.v_dict;
8027 ++to->vval.v_dict->dv_refcount;
8028 }
8029 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008030 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008031 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032 break;
8033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034}
8035
8036/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 * Make a copy of an item.
8038 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008039 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8040 * reference to an already copied list/dict can be used.
8041 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008044item_copy(
8045 typval_T *from,
8046 typval_T *to,
8047 int deep,
8048 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008049{
8050 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008051 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008052
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054 {
8055 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008057 }
8058 ++recurse;
8059
8060 switch (from->v_type)
8061 {
8062 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064 case VAR_STRING:
8065 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008066 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008067 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008068 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008069 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070 copy_tv(from, to);
8071 break;
8072 case VAR_LIST:
8073 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008074 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008075 if (from->vval.v_list == NULL)
8076 to->vval.v_list = NULL;
8077 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8078 {
8079 /* use the copy made earlier */
8080 to->vval.v_list = from->vval.v_list->lv_copylist;
8081 ++to->vval.v_list->lv_refcount;
8082 }
8083 else
8084 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8085 if (to->vval.v_list == NULL)
8086 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008087 break;
8088 case VAR_DICT:
8089 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008090 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008091 if (from->vval.v_dict == NULL)
8092 to->vval.v_dict = NULL;
8093 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8094 {
8095 /* use the copy made earlier */
8096 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8097 ++to->vval.v_dict->dv_refcount;
8098 }
8099 else
8100 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8101 if (to->vval.v_dict == NULL)
8102 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008104 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008105 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008106 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 }
8108 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008109 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008110}
8111
8112/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 * This function is used by f_input() and f_inputdialog() functions. The third
8114 * argument to f_input() specifies the type of completion to use at the
8115 * prompt. The third argument to f_inputdialog() specifies the value to return
8116 * when the user cancels the prompt.
8117 */
8118 void
8119get_user_input(
8120 typval_T *argvars,
8121 typval_T *rettv,
8122 int inputdialog,
8123 int secret)
8124{
8125 char_u *prompt = get_tv_string_chk(&argvars[0]);
8126 char_u *p = NULL;
8127 int c;
8128 char_u buf[NUMBUFLEN];
8129 int cmd_silent_save = cmd_silent;
8130 char_u *defstr = (char_u *)"";
8131 int xp_type = EXPAND_NOTHING;
8132 char_u *xp_arg = NULL;
8133
8134 rettv->v_type = VAR_STRING;
8135 rettv->vval.v_string = NULL;
8136
8137#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008138 /* While starting up, there is no place to enter text. When running tests
8139 * with --not-a-term we assume feedkeys() will be used. */
8140 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 return;
8142#endif
8143
8144 cmd_silent = FALSE; /* Want to see the prompt. */
8145 if (prompt != NULL)
8146 {
8147 /* Only the part of the message after the last NL is considered as
8148 * prompt for the command line */
8149 p = vim_strrchr(prompt, '\n');
8150 if (p == NULL)
8151 p = prompt;
8152 else
8153 {
8154 ++p;
8155 c = *p;
8156 *p = NUL;
8157 msg_start();
8158 msg_clr_eos();
8159 msg_puts_attr(prompt, echo_attr);
8160 msg_didout = FALSE;
8161 msg_starthere();
8162 *p = c;
8163 }
8164 cmdline_row = msg_row;
8165
8166 if (argvars[1].v_type != VAR_UNKNOWN)
8167 {
8168 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8169 if (defstr != NULL)
8170 stuffReadbuffSpec(defstr);
8171
8172 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8173 {
8174 char_u *xp_name;
8175 int xp_namelen;
8176 long argt;
8177
8178 /* input() with a third argument: completion */
8179 rettv->vval.v_string = NULL;
8180
8181 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8182 if (xp_name == NULL)
8183 return;
8184
8185 xp_namelen = (int)STRLEN(xp_name);
8186
8187 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8188 &xp_arg) == FAIL)
8189 return;
8190 }
8191 }
8192
8193 if (defstr != NULL)
8194 {
8195 int save_ex_normal_busy = ex_normal_busy;
8196 ex_normal_busy = 0;
8197 rettv->vval.v_string =
8198 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8199 xp_type, xp_arg);
8200 ex_normal_busy = save_ex_normal_busy;
8201 }
8202 if (inputdialog && rettv->vval.v_string == NULL
8203 && argvars[1].v_type != VAR_UNKNOWN
8204 && argvars[2].v_type != VAR_UNKNOWN)
8205 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8206 &argvars[2], buf));
8207
8208 vim_free(xp_arg);
8209
8210 /* since the user typed this, no need to wait for return */
8211 need_wait_return = FALSE;
8212 msg_didout = FALSE;
8213 }
8214 cmd_silent = cmd_silent_save;
8215}
8216
8217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 * ":echo expr1 ..." print each argument separated with a space, add a
8219 * newline at the end.
8220 * ":echon expr1 ..." print each argument plain.
8221 */
8222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008223ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008227 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 char_u *p;
8229 int needclr = TRUE;
8230 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008231 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
8233 if (eap->skip)
8234 ++emsg_skip;
8235 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8236 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237 /* If eval1() causes an error message the text from the command may
8238 * still need to be cleared. E.g., "echo 22,44". */
8239 need_clr_eos = needclr;
8240
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 /*
8245 * Report the invalid expression unless the expression evaluation
8246 * has been cancelled due to an aborting error, an interrupt, or an
8247 * exception.
8248 */
8249 if (!aborting())
8250 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008251 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 break;
8253 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008254 need_clr_eos = FALSE;
8255
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 if (!eap->skip)
8257 {
8258 if (atstart)
8259 {
8260 atstart = FALSE;
8261 /* Call msg_start() after eval1(), evaluating the expression
8262 * may cause a message to appear. */
8263 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008264 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008265 /* Mark the saved text as finishing the line, so that what
8266 * follows is displayed on a new line when scrolling back
8267 * at the more prompt. */
8268 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
8272 else if (eap->cmdidx == CMD_echo)
8273 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008274 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008275 if (p != NULL)
8276 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008278 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008280 if (*p != TAB && needclr)
8281 {
8282 /* remove any text still there from the command */
8283 msg_clr_eos();
8284 needclr = FALSE;
8285 }
8286 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 }
8288 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008289 {
8290#ifdef FEAT_MBYTE
8291 if (has_mbyte)
8292 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008293 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008294
8295 (void)msg_outtrans_len_attr(p, i, echo_attr);
8296 p += i - 1;
8297 }
8298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008300 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 arg = skipwhite(arg);
8307 }
8308 eap->nextcmd = check_nextcmd(arg);
8309
8310 if (eap->skip)
8311 --emsg_skip;
8312 else
8313 {
8314 /* remove text that may still be there from the command */
8315 if (needclr)
8316 msg_clr_eos();
8317 if (eap->cmdidx == CMD_echo)
8318 msg_end();
8319 }
8320}
8321
8322/*
8323 * ":echohl {name}".
8324 */
8325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008326ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008328 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329}
8330
8331/*
8332 * ":execute expr1 ..." execute the result of an expression.
8333 * ":echomsg expr1 ..." Print a message
8334 * ":echoerr expr1 ..." Print an error
8335 * Each gets spaces around each argument and a newline at the end for
8336 * echo commands
8337 */
8338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008339ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 int ret = OK;
8344 char_u *p;
8345 garray_T ga;
8346 int len;
8347 int save_did_emsg;
8348
8349 ga_init2(&ga, 1, 80);
8350
8351 if (eap->skip)
8352 ++emsg_skip;
8353 while (*arg != NUL && *arg != '|' && *arg != '\n')
8354 {
8355 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008356 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {
8358 /*
8359 * Report the invalid expression unless the expression evaluation
8360 * has been cancelled due to an aborting error, an interrupt, or an
8361 * exception.
8362 */
8363 if (!aborting())
8364 EMSG2(_(e_invexpr2), p);
8365 ret = FAIL;
8366 break;
8367 }
8368
8369 if (!eap->skip)
8370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 len = (int)STRLEN(p);
8373 if (ga_grow(&ga, len + 2) == FAIL)
8374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008375 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 ret = FAIL;
8377 break;
8378 }
8379 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 ga.ga_len += len;
8383 }
8384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 arg = skipwhite(arg);
8387 }
8388
8389 if (ret != FAIL && ga.ga_data != NULL)
8390 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008391 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8392 {
8393 /* Mark the already saved text as finishing the line, so that what
8394 * follows is displayed on a new line when scrolling back at the
8395 * more prompt. */
8396 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008397 }
8398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008402 out_flush();
8403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 else if (eap->cmdidx == CMD_echoerr)
8405 {
8406 /* We don't want to abort following commands, restore did_emsg. */
8407 save_did_emsg = did_emsg;
8408 EMSG((char_u *)ga.ga_data);
8409 if (!force_abort)
8410 did_emsg = save_did_emsg;
8411 }
8412 else if (eap->cmdidx == CMD_execute)
8413 do_cmdline((char_u *)ga.ga_data,
8414 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8415 }
8416
8417 ga_clear(&ga);
8418
8419 if (eap->skip)
8420 --emsg_skip;
8421
8422 eap->nextcmd = check_nextcmd(arg);
8423}
8424
8425/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 * Find window specified by "vp" in tabpage "tp".
8427 */
8428 win_T *
8429find_win_by_nr(
8430 typval_T *vp,
8431 tabpage_T *tp UNUSED) /* NULL for current tab page */
8432{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 int nr;
8435
8436 nr = (int)get_tv_number_chk(vp, NULL);
8437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 if (nr < 0)
8439 return NULL;
8440 if (nr == 0)
8441 return curwin;
8442
Bram Moolenaar29323592016-07-24 22:04:11 +02008443 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8444 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445 if (nr >= LOWEST_WIN_ID)
8446 {
8447 if (wp->w_id == nr)
8448 return wp;
8449 }
8450 else if (--nr <= 0)
8451 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008452 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008453 if (nr >= LOWEST_WIN_ID)
8454 return NULL;
8455 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456}
8457
8458/*
8459 * Find window specified by "wvp" in tabpage "tvp".
8460 */
8461 win_T *
8462find_tabwin(
8463 typval_T *wvp, /* VAR_UNKNOWN for current window */
8464 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8465{
8466 win_T *wp = NULL;
8467 tabpage_T *tp = NULL;
8468 long n;
8469
8470 if (wvp->v_type != VAR_UNKNOWN)
8471 {
8472 if (tvp->v_type != VAR_UNKNOWN)
8473 {
8474 n = (long)get_tv_number(tvp);
8475 if (n >= 0)
8476 tp = find_tabpage(n);
8477 }
8478 else
8479 tp = curtab;
8480
8481 if (tp != NULL)
8482 wp = find_win_by_nr(wvp, tp);
8483 }
8484 else
8485 wp = curwin;
8486
8487 return wp;
8488}
8489
8490/*
8491 * getwinvar() and gettabwinvar()
8492 */
8493 void
8494getwinvar(
8495 typval_T *argvars,
8496 typval_T *rettv,
8497 int off) /* 1 for gettabwinvar() */
8498{
8499 win_T *win;
8500 char_u *varname;
8501 dictitem_T *v;
8502 tabpage_T *tp = NULL;
8503 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 win_T *oldcurwin;
8505 tabpage_T *oldtabpage;
8506 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 if (off == 1)
8509 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8510 else
8511 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 win = find_win_by_nr(&argvars[off], tp);
8513 varname = get_tv_string_chk(&argvars[off + 1]);
8514 ++emsg_off;
8515
8516 rettv->v_type = VAR_STRING;
8517 rettv->vval.v_string = NULL;
8518
8519 if (win != NULL && varname != NULL)
8520 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 /* Set curwin to be our win, temporarily. Also set the tabpage,
8522 * otherwise the window is not valid. Only do this when needed,
8523 * autocommands get blocked. */
8524 need_switch_win = !(tp == curtab && win == curwin);
8525 if (!need_switch_win
8526 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008527 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008528 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008530 if (varname[1] == NUL)
8531 {
8532 /* get all window-local options in a dict */
8533 dict_T *opts = get_winbuf_options(FALSE);
8534
8535 if (opts != NULL)
8536 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008537 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008538 done = TRUE;
8539 }
8540 }
8541 else if (get_option_tv(&varname, rettv, 1) == OK)
8542 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 done = TRUE;
8544 }
8545 else
8546 {
8547 /* Look up the variable. */
8548 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8549 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8550 varname, FALSE);
8551 if (v != NULL)
8552 {
8553 copy_tv(&v->di_tv, rettv);
8554 done = TRUE;
8555 }
8556 }
8557 }
8558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 if (need_switch_win)
8560 /* restore previous notion of curwin */
8561 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 }
8563
8564 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8565 /* use the default return value */
8566 copy_tv(&argvars[off + 2], rettv);
8567
8568 --emsg_off;
8569}
8570
8571/*
8572 * "setwinvar()" and "settabwinvar()" functions
8573 */
8574 void
8575setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8576{
8577 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 win_T *save_curwin;
8579 tabpage_T *save_curtab;
8580 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 char_u *varname, *winvarname;
8582 typval_T *varp;
8583 char_u nbuf[NUMBUFLEN];
8584 tabpage_T *tp = NULL;
8585
8586 if (check_restricted() || check_secure())
8587 return;
8588
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 if (off == 1)
8590 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8591 else
8592 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 win = find_win_by_nr(&argvars[off], tp);
8594 varname = get_tv_string_chk(&argvars[off + 1]);
8595 varp = &argvars[off + 2];
8596
8597 if (win != NULL && varname != NULL && varp != NULL)
8598 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 need_switch_win = !(tp == curtab && win == curwin);
8600 if (!need_switch_win
8601 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 {
8603 if (*varname == '&')
8604 {
8605 long numval;
8606 char_u *strval;
8607 int error = FALSE;
8608
8609 ++varname;
8610 numval = (long)get_tv_number_chk(varp, &error);
8611 strval = get_tv_string_buf_chk(varp, nbuf);
8612 if (!error && strval != NULL)
8613 set_option_value(varname, numval, strval, OPT_LOCAL);
8614 }
8615 else
8616 {
8617 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8618 if (winvarname != NULL)
8619 {
8620 STRCPY(winvarname, "w:");
8621 STRCPY(winvarname + 2, varname);
8622 set_var(winvarname, varp, TRUE);
8623 vim_free(winvarname);
8624 }
8625 }
8626 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 if (need_switch_win)
8628 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 }
8630}
8631
8632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8634 * "arg" points to the "&" or '+' when called, to "option" when returning.
8635 * Returns NULL when no option name found. Otherwise pointer to the char
8636 * after the option name.
8637 */
8638 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008639find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 char_u *p = *arg;
8642
8643 ++p;
8644 if (*p == 'g' && p[1] == ':')
8645 {
8646 *opt_flags = OPT_GLOBAL;
8647 p += 2;
8648 }
8649 else if (*p == 'l' && p[1] == ':')
8650 {
8651 *opt_flags = OPT_LOCAL;
8652 p += 2;
8653 }
8654 else
8655 *opt_flags = 0;
8656
8657 if (!ASCII_ISALPHA(*p))
8658 return NULL;
8659 *arg = p;
8660
8661 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8662 p += 4; /* termcap option */
8663 else
8664 while (ASCII_ISALPHA(*p))
8665 ++p;
8666 return p;
8667}
8668
8669/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008670 * Return the autoload script name for a function or variable name.
8671 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008673 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008674autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008675{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008676 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008677 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008678
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008679 /* Get the script file name: replace '#' with '/', append ".vim". */
8680 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8681 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008682 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008683 STRCPY(scriptname, "autoload/");
8684 STRCAT(scriptname, name);
8685 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8686 STRCAT(scriptname, ".vim");
8687 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8688 *p = '/';
8689 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008690}
8691
8692/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008693 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008694 * Return TRUE if a package was loaded.
8695 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008697script_autoload(
8698 char_u *name,
8699 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700{
8701 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008702 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008703 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008704 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008705
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008706 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008707 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008708 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008709 return FALSE;
8710
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008711 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008712
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008713 /* Find the name in the list of previously loaded package names. Skip
8714 * "autoload/", it's always the same. */
8715 for (i = 0; i < ga_loaded.ga_len; ++i)
8716 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8717 break;
8718 if (!reload && i < ga_loaded.ga_len)
8719 ret = FALSE; /* was loaded already */
8720 else
8721 {
8722 /* Remember the name if it wasn't loaded already. */
8723 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8724 {
8725 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8726 tofree = NULL;
8727 }
8728
8729 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008730 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008731 ret = TRUE;
8732 }
8733
8734 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008735 return ret;
8736}
8737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8739typedef enum
8740{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008741 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8742 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8743 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744} var_flavour_T;
8745
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008746static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
8748 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008749var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751 char_u *p = varname;
8752
8753 if (ASCII_ISUPPER(*p))
8754 {
8755 while (*(++p))
8756 if (ASCII_ISLOWER(*p))
8757 return VAR_FLAVOUR_SESSION;
8758 return VAR_FLAVOUR_VIMINFO;
8759 }
8760 else
8761 return VAR_FLAVOUR_DEFAULT;
8762}
8763#endif
8764
8765#if defined(FEAT_VIMINFO) || defined(PROTO)
8766/*
8767 * Restore global vars that start with a capital from the viminfo file
8768 */
8769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008770read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008773 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008774 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008775 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776
8777 if (!writing && (find_viminfo_parameter('!') != NULL))
8778 {
8779 tab = vim_strchr(virp->vir_line + 1, '\t');
8780 if (tab != NULL)
8781 {
8782 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008783 switch (*tab)
8784 {
8785 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008786#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008787 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008788#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008789 case 'D': type = VAR_DICT; break;
8790 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008791 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
8794 tab = vim_strchr(tab, '\t');
8795 if (tab != NULL)
8796 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008797 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008798 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008799 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008801#ifdef FEAT_FLOAT
8802 else if (type == VAR_FLOAT)
8803 (void)string2float(tab + 1, &tv.vval.v_float);
8804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008806 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008807 if (type == VAR_DICT || type == VAR_LIST)
8808 {
8809 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8810
8811 if (etv == NULL)
8812 /* Failed to parse back the dict or list, use it as a
8813 * string. */
8814 tv.v_type = VAR_STRING;
8815 else
8816 {
8817 vim_free(tv.vval.v_string);
8818 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008819 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008820 }
8821 }
8822
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008823 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008824 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008825 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008826 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008827
8828 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008829 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008830 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8831 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
8833 }
8834 }
8835
8836 return viminfo_readline(virp);
8837}
8838
8839/*
8840 * Write global vars that start with a capital to the viminfo file
8841 */
8842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008843write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 hashitem_T *hi;
8846 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008847 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008848 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008849 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008851 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 if (find_viminfo_parameter('!') == NULL)
8854 return;
8855
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008856 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008858 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008861 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008863 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 this_var = HI2DI(hi);
8865 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008868 {
8869 case VAR_STRING: s = "STR"; break;
8870 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008871 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008872 case VAR_DICT: s = "DIC"; break;
8873 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008874 case VAR_SPECIAL: s = "XPL"; break;
8875
8876 case VAR_UNKNOWN:
8877 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008878 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008879 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008880 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008881 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008884 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008885 if (p != NULL)
8886 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008887 vim_free(tofree);
8888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
8890 }
8891}
8892#endif
8893
8894#if defined(FEAT_SESSION) || defined(PROTO)
8895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008896store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
Bram Moolenaar33570922005-01-25 22:26:29 +00008898 hashitem_T *hi;
8899 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008900 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 char_u *p, *t;
8902
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008903 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008906 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008908 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008909 this_var = HI2DI(hi);
8910 if ((this_var->di_tv.v_type == VAR_NUMBER
8911 || this_var->di_tv.v_type == VAR_STRING)
8912 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008913 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008914 /* Escape special characters with a backslash. Turn a LF and
8915 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008917 (char_u *)"\\\"\n\r");
8918 if (p == NULL) /* out of memory */
8919 break;
8920 for (t = p; *t != NUL; ++t)
8921 if (*t == '\n')
8922 *t = 'n';
8923 else if (*t == '\r')
8924 *t = 'r';
8925 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008926 this_var->di_key,
8927 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8928 : ' ',
8929 p,
8930 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8931 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008932 || put_eol(fd) == FAIL)
8933 {
8934 vim_free(p);
8935 return FAIL;
8936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 vim_free(p);
8938 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008939#ifdef FEAT_FLOAT
8940 else if (this_var->di_tv.v_type == VAR_FLOAT
8941 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8942 {
8943 float_T f = this_var->di_tv.vval.v_float;
8944 int sign = ' ';
8945
8946 if (f < 0)
8947 {
8948 f = -f;
8949 sign = '-';
8950 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008951 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008952 this_var->di_key, sign, f) < 0)
8953 || put_eol(fd) == FAIL)
8954 return FAIL;
8955 }
8956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 }
8959 return OK;
8960}
8961#endif
8962
Bram Moolenaar661b1822005-07-28 22:36:45 +00008963/*
8964 * Display script name where an item was last set.
8965 * Should only be invoked when 'verbose' is non-zero.
8966 */
8967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008969{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008970 char_u *p;
8971
Bram Moolenaar661b1822005-07-28 22:36:45 +00008972 if (scriptID != 0)
8973 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008974 p = home_replace_save(NULL, get_scriptname(scriptID));
8975 if (p != NULL)
8976 {
8977 verbose_enter();
8978 MSG_PUTS(_("\n\tLast set from "));
8979 MSG_PUTS(p);
8980 vim_free(p);
8981 verbose_leave();
8982 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008983 }
8984}
8985
Bram Moolenaar53744302015-07-17 17:38:22 +02008986/* reset v:option_new, v:option_old and v:option_type */
8987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008989{
8990 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8991 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8992 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8993}
8994
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995/*
8996 * Prepare "gap" for an assert error and add the sourcing position.
8997 */
8998 void
8999prepare_assert_error(garray_T *gap)
9000{
9001 char buf[NUMBUFLEN];
9002
9003 ga_init2(gap, 1, 100);
9004 if (sourcing_name != NULL)
9005 {
9006 ga_concat(gap, sourcing_name);
9007 if (sourcing_lnum > 0)
9008 ga_concat(gap, (char_u *)" ");
9009 }
9010 if (sourcing_lnum > 0)
9011 {
9012 sprintf(buf, "line %ld", (long)sourcing_lnum);
9013 ga_concat(gap, (char_u *)buf);
9014 }
9015 if (sourcing_name != NULL || sourcing_lnum > 0)
9016 ga_concat(gap, (char_u *)": ");
9017}
9018
9019/*
9020 * Add an assert error to v:errors.
9021 */
9022 void
9023assert_error(garray_T *gap)
9024{
9025 struct vimvar *vp = &vimvars[VV_ERRORS];
9026
9027 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9028 /* Make sure v:errors is a list. */
9029 set_vim_var_list(VV_ERRORS, list_alloc());
9030 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9031}
9032
9033 void
9034assert_equal_common(typval_T *argvars, assert_type_T atype)
9035{
9036 garray_T ga;
9037
9038 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9039 != (atype == ASSERT_EQUAL))
9040 {
9041 prepare_assert_error(&ga);
9042 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9043 atype);
9044 assert_error(&ga);
9045 ga_clear(&ga);
9046 }
9047}
9048
9049 void
9050assert_match_common(typval_T *argvars, assert_type_T atype)
9051{
9052 garray_T ga;
9053 char_u buf1[NUMBUFLEN];
9054 char_u buf2[NUMBUFLEN];
9055 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9056 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9057
9058 if (pat == NULL || text == NULL)
9059 EMSG(_(e_invarg));
9060 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9061 {
9062 prepare_assert_error(&ga);
9063 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9064 atype);
9065 assert_error(&ga);
9066 ga_clear(&ga);
9067 }
9068}
9069
Bram Moolenaar61c04492016-07-23 15:35:35 +02009070 void
9071assert_inrange(typval_T *argvars)
9072{
9073 garray_T ga;
9074 int error = FALSE;
9075 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9076 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9077 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9078 char_u *tofree;
9079 char msg[200];
9080 char_u numbuf[NUMBUFLEN];
9081
9082 if (error)
9083 return;
9084 if (actual < lower || actual > upper)
9085 {
9086 prepare_assert_error(&ga);
9087 if (argvars[3].v_type != VAR_UNKNOWN)
9088 {
9089 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9090 vim_free(tofree);
9091 }
9092 else
9093 {
9094 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9095 (long)lower, (long)upper, (long)actual);
9096 ga_concat(&ga, (char_u *)msg);
9097 }
9098 assert_error(&ga);
9099 ga_clear(&ga);
9100 }
9101}
9102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103/*
9104 * Common for assert_true() and assert_false().
9105 */
9106 void
9107assert_bool(typval_T *argvars, int isTrue)
9108{
9109 int error = FALSE;
9110 garray_T ga;
9111
9112 if (argvars[0].v_type == VAR_SPECIAL
9113 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9114 return;
9115 if (argvars[0].v_type != VAR_NUMBER
9116 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9117 || error)
9118 {
9119 prepare_assert_error(&ga);
9120 fill_assert_error(&ga, &argvars[1],
9121 (char_u *)(isTrue ? "True" : "False"),
9122 NULL, &argvars[0], ASSERT_OTHER);
9123 assert_error(&ga);
9124 ga_clear(&ga);
9125 }
9126}
9127
9128 void
Bram Moolenaar42205552017-03-18 19:42:22 +01009129assert_report(typval_T *argvars)
9130{
9131 garray_T ga;
9132
9133 prepare_assert_error(&ga);
9134 ga_concat(&ga, get_tv_string(&argvars[0]));
9135 assert_error(&ga);
9136 ga_clear(&ga);
9137}
9138
9139 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009140assert_exception(typval_T *argvars)
9141{
9142 garray_T ga;
9143 char_u *error = get_tv_string_chk(&argvars[0]);
9144
9145 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9146 {
9147 prepare_assert_error(&ga);
9148 ga_concat(&ga, (char_u *)"v:exception is not set");
9149 assert_error(&ga);
9150 ga_clear(&ga);
9151 }
9152 else if (error != NULL
9153 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9154 {
9155 prepare_assert_error(&ga);
9156 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9157 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9158 assert_error(&ga);
9159 ga_clear(&ga);
9160 }
9161}
9162
9163 void
9164assert_fails(typval_T *argvars)
9165{
9166 char_u *cmd = get_tv_string_chk(&argvars[0]);
9167 garray_T ga;
9168
9169 called_emsg = FALSE;
9170 suppress_errthrow = TRUE;
9171 emsg_silent = TRUE;
9172 do_cmdline_cmd(cmd);
9173 if (!called_emsg)
9174 {
9175 prepare_assert_error(&ga);
9176 ga_concat(&ga, (char_u *)"command did not fail: ");
9177 ga_concat(&ga, cmd);
9178 assert_error(&ga);
9179 ga_clear(&ga);
9180 }
9181 else if (argvars[1].v_type != VAR_UNKNOWN)
9182 {
9183 char_u buf[NUMBUFLEN];
9184 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9185
9186 if (error == NULL
9187 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9188 {
9189 prepare_assert_error(&ga);
9190 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9191 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9192 assert_error(&ga);
9193 ga_clear(&ga);
9194 }
9195 }
9196
9197 called_emsg = FALSE;
9198 suppress_errthrow = FALSE;
9199 emsg_silent = FALSE;
9200 emsg_on_display = FALSE;
9201 set_vim_var_string(VV_ERRMSG, NULL, 0);
9202}
9203
9204/*
9205 * Append "str" to "gap", escaping unprintable characters.
9206 * Changes NL to \n, CR to \r, etc.
9207 */
9208 static void
9209ga_concat_esc(garray_T *gap, char_u *str)
9210{
9211 char_u *p;
9212 char_u buf[NUMBUFLEN];
9213
9214 if (str == NULL)
9215 {
9216 ga_concat(gap, (char_u *)"NULL");
9217 return;
9218 }
9219
9220 for (p = str; *p != NUL; ++p)
9221 switch (*p)
9222 {
9223 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9224 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9225 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9226 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9227 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9228 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9229 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9230 default:
9231 if (*p < ' ')
9232 {
9233 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9234 ga_concat(gap, buf);
9235 }
9236 else
9237 ga_append(gap, *p);
9238 break;
9239 }
9240}
9241
9242/*
9243 * Fill "gap" with information about an assert error.
9244 */
9245 void
9246fill_assert_error(
9247 garray_T *gap,
9248 typval_T *opt_msg_tv,
9249 char_u *exp_str,
9250 typval_T *exp_tv,
9251 typval_T *got_tv,
9252 assert_type_T atype)
9253{
9254 char_u numbuf[NUMBUFLEN];
9255 char_u *tofree;
9256
9257 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9258 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009259 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9260 vim_free(tofree);
9261 ga_concat(gap, (char_u *)": ");
9262 }
9263
9264 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9265 ga_concat(gap, (char_u *)"Pattern ");
9266 else if (atype == ASSERT_NOTEQUAL)
9267 ga_concat(gap, (char_u *)"Expected not equal to ");
9268 else
9269 ga_concat(gap, (char_u *)"Expected ");
9270 if (exp_str == NULL)
9271 {
9272 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273 vim_free(tofree);
9274 }
9275 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009276 ga_concat_esc(gap, exp_str);
9277 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009279 if (atype == ASSERT_MATCH)
9280 ga_concat(gap, (char_u *)" does not match ");
9281 else if (atype == ASSERT_NOTMATCH)
9282 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009284 ga_concat(gap, (char_u *)" but got ");
9285 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9286 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 }
9288}
9289
Bram Moolenaar53744302015-07-17 17:38:22 +02009290
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291#endif /* FEAT_EVAL */
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009294#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
9296#ifdef WIN3264
9297/*
9298 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9299 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009300static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9301static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9302static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303
9304/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009305 * Get the short path (8.3) for the filename in "fnamep".
9306 * Only works for a valid file name.
9307 * When the path gets longer "fnamep" is changed and the allocated buffer
9308 * is put in "bufp".
9309 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9310 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 */
9312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009315 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 char_u *newbuf;
9317
9318 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009319 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 if (l > len - 1)
9321 {
9322 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009323 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 newbuf = vim_strnsave(*fnamep, l);
9325 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 vim_free(*bufp);
9329 *fnamep = *bufp = newbuf;
9330
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009331 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009332 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 }
9334
9335 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009336 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337}
9338
9339/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009340 * Get the short path (8.3) for the filename in "fname". The converted
9341 * path is returned in "bufp".
9342 *
9343 * Some of the directories specified in "fname" may not exist. This function
9344 * will shorten the existing directories at the beginning of the path and then
9345 * append the remaining non-existing path.
9346 *
9347 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009348 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009349 * bufp - Pointer to an allocated buffer for the filename.
9350 * fnamelen - Length of the filename pointed to by fname
9351 *
9352 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 */
9354 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355shortpath_for_invalid_fname(
9356 char_u **fname,
9357 char_u **bufp,
9358 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009360 char_u *short_fname, *save_fname, *pbuf_unused;
9361 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009363 int old_len, len;
9364 int new_len, sfx_len;
9365 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366
9367 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009368 old_len = *fnamelen;
9369 save_fname = vim_strnsave(*fname, old_len);
9370 pbuf_unused = NULL;
9371 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009373 endp = save_fname + old_len - 1; /* Find the end of the copy */
9374 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009376 /*
9377 * Try shortening the supplied path till it succeeds by removing one
9378 * directory at a time from the tail of the path.
9379 */
9380 len = 0;
9381 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009383 /* go back one path-separator */
9384 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9385 --endp;
9386 if (endp <= save_fname)
9387 break; /* processed the complete path */
9388
9389 /*
9390 * Replace the path separator with a NUL and try to shorten the
9391 * resulting path.
9392 */
9393 ch = *endp;
9394 *endp = 0;
9395 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009396 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009397 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9398 {
9399 retval = FAIL;
9400 goto theend;
9401 }
9402 *endp = ch; /* preserve the string */
9403
9404 if (len > 0)
9405 break; /* successfully shortened the path */
9406
9407 /* failed to shorten the path. Skip the path separator */
9408 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
9410
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009411 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009413 /*
9414 * Succeeded in shortening the path. Now concatenate the shortened
9415 * path with the remaining path at the tail.
9416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009418 /* Compute the length of the new path. */
9419 sfx_len = (int)(save_endp - endp) + 1;
9420 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009422 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009424 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009426 /* There is not enough space in the currently allocated string,
9427 * copy it to a buffer big enough. */
9428 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009430 {
9431 retval = FAIL;
9432 goto theend;
9433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
9435 else
9436 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009437 /* Transfer short_fname to the main buffer (it's big enough),
9438 * unless get_short_pathname() did its work in-place. */
9439 *fname = *bufp = save_fname;
9440 if (short_fname != save_fname)
9441 vim_strncpy(save_fname, short_fname, len);
9442 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009444
9445 /* concat the not-shortened part of the path */
9446 vim_strncpy(*fname + len, endp, sfx_len);
9447 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009449
9450theend:
9451 vim_free(pbuf_unused);
9452 vim_free(save_fname);
9453
9454 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455}
9456
9457/*
9458 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009459 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 */
9461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462shortpath_for_partial(
9463 char_u **fnamep,
9464 char_u **bufp,
9465 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 int sepcount, len, tflen;
9468 char_u *p;
9469 char_u *pbuf, *tfname;
9470 int hasTilde;
9471
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009472 /* Count up the path separators from the RHS.. so we know which part
9473 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009475 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 if (vim_ispathsep(*p))
9477 ++sepcount;
9478
9479 /* Need full path first (use expand_env() to remove a "~/") */
9480 hasTilde = (**fnamep == '~');
9481 if (hasTilde)
9482 pbuf = tfname = expand_env_save(*fnamep);
9483 else
9484 pbuf = tfname = FullName_save(*fnamep, FALSE);
9485
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009486 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009488 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9489 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490
9491 if (len == 0)
9492 {
9493 /* Don't have a valid filename, so shorten the rest of the
9494 * path if we can. This CAN give us invalid 8.3 filenames, but
9495 * there's not a lot of point in guessing what it might be.
9496 */
9497 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009498 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9499 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 }
9501
9502 /* Count the paths backward to find the beginning of the desired string. */
9503 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009504 {
9505#ifdef FEAT_MBYTE
9506 if (has_mbyte)
9507 p -= mb_head_off(tfname, p);
9508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (vim_ispathsep(*p))
9510 {
9511 if (sepcount == 0 || (hasTilde && sepcount == 1))
9512 break;
9513 else
9514 sepcount --;
9515 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 if (hasTilde)
9518 {
9519 --p;
9520 if (p >= tfname)
9521 *p = '~';
9522 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009523 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 }
9525 else
9526 ++p;
9527
9528 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9529 vim_free(*bufp);
9530 *fnamelen = (int)STRLEN(p);
9531 *bufp = pbuf;
9532 *fnamep = p;
9533
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009534 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535}
9536#endif /* WIN3264 */
9537
9538/*
9539 * Adjust a filename, according to a string of modifiers.
9540 * *fnamep must be NUL terminated when called. When returning, the length is
9541 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009542 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 * When there is an error, *fnamep is set to NULL.
9544 */
9545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546modify_fname(
9547 char_u *src, /* string with modifiers */
9548 int *usedlen, /* characters after src that are used */
9549 char_u **fnamep, /* file name so far */
9550 char_u **bufp, /* buffer for allocated file name or NULL */
9551 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 int valid = 0;
9554 char_u *tail;
9555 char_u *s, *p, *pbuf;
9556 char_u dirname[MAXPATHL];
9557 int c;
9558 int has_fullname = 0;
9559#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009560 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 int has_shortname = 0;
9562#endif
9563
9564repeat:
9565 /* ":p" - full path/file_name */
9566 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9567 {
9568 has_fullname = 1;
9569
9570 valid |= VALID_PATH;
9571 *usedlen += 2;
9572
9573 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9574 if ((*fnamep)[0] == '~'
9575#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9576 && ((*fnamep)[1] == '/'
9577# ifdef BACKSLASH_IN_FILENAME
9578 || (*fnamep)[1] == '\\'
9579# endif
9580 || (*fnamep)[1] == NUL)
9581
9582#endif
9583 )
9584 {
9585 *fnamep = expand_env_save(*fnamep);
9586 vim_free(*bufp); /* free any allocated file name */
9587 *bufp = *fnamep;
9588 if (*fnamep == NULL)
9589 return -1;
9590 }
9591
9592 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009593 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
9595 if (vim_ispathsep(*p)
9596 && p[1] == '.'
9597 && (p[2] == NUL
9598 || vim_ispathsep(p[2])
9599 || (p[2] == '.'
9600 && (p[3] == NUL || vim_ispathsep(p[3])))))
9601 break;
9602 }
9603
9604 /* FullName_save() is slow, don't use it when not needed. */
9605 if (*p != NUL || !vim_isAbsName(*fnamep))
9606 {
9607 *fnamep = FullName_save(*fnamep, *p != NUL);
9608 vim_free(*bufp); /* free any allocated file name */
9609 *bufp = *fnamep;
9610 if (*fnamep == NULL)
9611 return -1;
9612 }
9613
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009614#ifdef WIN3264
9615# if _WIN32_WINNT >= 0x0500
9616 if (vim_strchr(*fnamep, '~') != NULL)
9617 {
9618 /* Expand 8.3 filename to full path. Needed to make sure the same
9619 * file does not have two different names.
9620 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9621 p = alloc(_MAX_PATH + 1);
9622 if (p != NULL)
9623 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009624 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009625 {
9626 vim_free(*bufp);
9627 *bufp = *fnamep = p;
9628 }
9629 else
9630 vim_free(p);
9631 }
9632 }
9633# endif
9634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 /* Append a path separator to a directory. */
9636 if (mch_isdir(*fnamep))
9637 {
9638 /* Make room for one or two extra characters. */
9639 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9640 vim_free(*bufp); /* free any allocated file name */
9641 *bufp = *fnamep;
9642 if (*fnamep == NULL)
9643 return -1;
9644 add_pathsep(*fnamep);
9645 }
9646 }
9647
9648 /* ":." - path relative to the current directory */
9649 /* ":~" - path relative to the home directory */
9650 /* ":8" - shortname path - postponed till after */
9651 while (src[*usedlen] == ':'
9652 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9653 {
9654 *usedlen += 2;
9655 if (c == '8')
9656 {
9657#ifdef WIN3264
9658 has_shortname = 1; /* Postpone this. */
9659#endif
9660 continue;
9661 }
9662 pbuf = NULL;
9663 /* Need full path first (use expand_env() to remove a "~/") */
9664 if (!has_fullname)
9665 {
9666 if (c == '.' && **fnamep == '~')
9667 p = pbuf = expand_env_save(*fnamep);
9668 else
9669 p = pbuf = FullName_save(*fnamep, FALSE);
9670 }
9671 else
9672 p = *fnamep;
9673
9674 has_fullname = 0;
9675
9676 if (p != NULL)
9677 {
9678 if (c == '.')
9679 {
9680 mch_dirname(dirname, MAXPATHL);
9681 s = shorten_fname(p, dirname);
9682 if (s != NULL)
9683 {
9684 *fnamep = s;
9685 if (pbuf != NULL)
9686 {
9687 vim_free(*bufp); /* free any allocated file name */
9688 *bufp = pbuf;
9689 pbuf = NULL;
9690 }
9691 }
9692 }
9693 else
9694 {
9695 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9696 /* Only replace it when it starts with '~' */
9697 if (*dirname == '~')
9698 {
9699 s = vim_strsave(dirname);
9700 if (s != NULL)
9701 {
9702 *fnamep = s;
9703 vim_free(*bufp);
9704 *bufp = s;
9705 }
9706 }
9707 }
9708 vim_free(pbuf);
9709 }
9710 }
9711
9712 tail = gettail(*fnamep);
9713 *fnamelen = (int)STRLEN(*fnamep);
9714
9715 /* ":h" - head, remove "/file_name", can be repeated */
9716 /* Don't remove the first "/" or "c:\" */
9717 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9718 {
9719 valid |= VALID_HEAD;
9720 *usedlen += 2;
9721 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009722 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009723 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 *fnamelen = (int)(tail - *fnamep);
9725#ifdef VMS
9726 if (*fnamelen > 0)
9727 *fnamelen += 1; /* the path separator is part of the path */
9728#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009729 if (*fnamelen == 0)
9730 {
9731 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9732 p = vim_strsave((char_u *)".");
9733 if (p == NULL)
9734 return -1;
9735 vim_free(*bufp);
9736 *bufp = *fnamep = tail = p;
9737 *fnamelen = 1;
9738 }
9739 else
9740 {
9741 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009742 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
9745
9746 /* ":8" - shortname */
9747 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9748 {
9749 *usedlen += 2;
9750#ifdef WIN3264
9751 has_shortname = 1;
9752#endif
9753 }
9754
9755#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009756 /*
9757 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 */
9759 if (has_shortname)
9760 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009761 /* Copy the string if it is shortened by :h and when it wasn't copied
9762 * yet, because we are going to change it in place. Avoids changing
9763 * the buffer name for "%:8". */
9764 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
9766 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009767 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 return -1;
9769 vim_free(*bufp);
9770 *bufp = *fnamep = p;
9771 }
9772
9773 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009774 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (!has_fullname && !vim_isAbsName(*fnamep))
9776 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009777 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 return -1;
9779 }
9780 else
9781 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009782 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
Bram Moolenaardc935552011-08-17 15:23:23 +02009784 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009786 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 return -1;
9788
9789 if (l == 0)
9790 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009791 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009793 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 return -1;
9795 }
9796 *fnamelen = l;
9797 }
9798 }
9799#endif /* WIN3264 */
9800
9801 /* ":t" - tail, just the basename */
9802 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9803 {
9804 *usedlen += 2;
9805 *fnamelen -= (int)(tail - *fnamep);
9806 *fnamep = tail;
9807 }
9808
9809 /* ":e" - extension, can be repeated */
9810 /* ":r" - root, without extension, can be repeated */
9811 while (src[*usedlen] == ':'
9812 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9813 {
9814 /* find a '.' in the tail:
9815 * - for second :e: before the current fname
9816 * - otherwise: The last '.'
9817 */
9818 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9819 s = *fnamep - 2;
9820 else
9821 s = *fnamep + *fnamelen - 1;
9822 for ( ; s > tail; --s)
9823 if (s[0] == '.')
9824 break;
9825 if (src[*usedlen + 1] == 'e') /* :e */
9826 {
9827 if (s > tail)
9828 {
9829 *fnamelen += (int)(*fnamep - (s + 1));
9830 *fnamep = s + 1;
9831#ifdef VMS
9832 /* cut version from the extension */
9833 s = *fnamep + *fnamelen - 1;
9834 for ( ; s > *fnamep; --s)
9835 if (s[0] == ';')
9836 break;
9837 if (s > *fnamep)
9838 *fnamelen = s - *fnamep;
9839#endif
9840 }
9841 else if (*fnamep <= tail)
9842 *fnamelen = 0;
9843 }
9844 else /* :r */
9845 {
9846 if (s > tail) /* remove one extension */
9847 *fnamelen = (int)(s - *fnamep);
9848 }
9849 *usedlen += 2;
9850 }
9851
9852 /* ":s?pat?foo?" - substitute */
9853 /* ":gs?pat?foo?" - global substitute */
9854 if (src[*usedlen] == ':'
9855 && (src[*usedlen + 1] == 's'
9856 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9857 {
9858 char_u *str;
9859 char_u *pat;
9860 char_u *sub;
9861 int sep;
9862 char_u *flags;
9863 int didit = FALSE;
9864
9865 flags = (char_u *)"";
9866 s = src + *usedlen + 2;
9867 if (src[*usedlen + 1] == 'g')
9868 {
9869 flags = (char_u *)"g";
9870 ++s;
9871 }
9872
9873 sep = *s++;
9874 if (sep)
9875 {
9876 /* find end of pattern */
9877 p = vim_strchr(s, sep);
9878 if (p != NULL)
9879 {
9880 pat = vim_strnsave(s, (int)(p - s));
9881 if (pat != NULL)
9882 {
9883 s = p + 1;
9884 /* find end of substitution */
9885 p = vim_strchr(s, sep);
9886 if (p != NULL)
9887 {
9888 sub = vim_strnsave(s, (int)(p - s));
9889 str = vim_strnsave(*fnamep, *fnamelen);
9890 if (sub != NULL && str != NULL)
9891 {
9892 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009893 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 if (s != NULL)
9895 {
9896 *fnamep = s;
9897 *fnamelen = (int)STRLEN(s);
9898 vim_free(*bufp);
9899 *bufp = s;
9900 didit = TRUE;
9901 }
9902 }
9903 vim_free(sub);
9904 vim_free(str);
9905 }
9906 vim_free(pat);
9907 }
9908 }
9909 /* after using ":s", repeat all the modifiers */
9910 if (didit)
9911 goto repeat;
9912 }
9913 }
9914
Bram Moolenaar26df0922014-02-23 23:39:13 +01009915 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9916 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009917 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009918 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009919 if (c != NUL)
9920 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009921 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009922 if (c != NUL)
9923 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009924 if (p == NULL)
9925 return -1;
9926 vim_free(*bufp);
9927 *bufp = *fnamep = p;
9928 *fnamelen = (int)STRLEN(p);
9929 *usedlen += 2;
9930 }
9931
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 return valid;
9933}
9934
9935/*
9936 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009937 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 * "flags" can be "g" to do a global substitute.
9939 * Returns an allocated string, NULL for error.
9940 */
9941 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009942do_string_sub(
9943 char_u *str,
9944 char_u *pat,
9945 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009946 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949 int sublen;
9950 regmatch_T regmatch;
9951 int i;
9952 int do_all;
9953 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009954 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 garray_T ga;
9956 char_u *ret;
9957 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009958 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
9960 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9961 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009962 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
9964 ga_init2(&ga, 1, 200);
9965
9966 do_all = (flags[0] == 'g');
9967
9968 regmatch.rm_ic = p_ic;
9969 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9970 if (regmatch.regprog != NULL)
9971 {
9972 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009973 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9975 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009976 /* Skip empty match except for first match. */
9977 if (regmatch.startp[0] == regmatch.endp[0])
9978 {
9979 if (zero_width == regmatch.startp[0])
9980 {
9981 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009982 i = MB_PTR2LEN(tail);
9983 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9984 (size_t)i);
9985 ga.ga_len += i;
9986 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009987 continue;
9988 }
9989 zero_width = regmatch.startp[0];
9990 }
9991
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 /*
9993 * Get some space for a temporary buffer to do the substitution
9994 * into. It will contain:
9995 * - The text up to where the match is.
9996 * - The substituted text.
9997 * - The text after the match.
9998 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009999 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010000 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10002 {
10003 ga_clear(&ga);
10004 break;
10005 }
10006
10007 /* copy the text up to where the match is */
10008 i = (int)(regmatch.startp[0] - tail);
10009 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10010 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010011 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 + ga.ga_len + i, TRUE, TRUE, FALSE);
10013 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010014 tail = regmatch.endp[0];
10015 if (*tail == NUL)
10016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if (!do_all)
10018 break;
10019 }
10020
10021 if (ga.ga_data != NULL)
10022 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10023
Bram Moolenaar473de612013-06-08 18:19:48 +020010024 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 }
10026
10027 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10028 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010029 if (p_cpo == empty_option)
10030 p_cpo = save_cpo;
10031 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010032 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010033 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
10035 return ret;
10036}
10037
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 static int
10039filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10040{
10041 typval_T rettv;
10042 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010044
10045 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10046 argv[0] = vimvars[VV_KEY].vv_tv;
10047 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010048 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10049 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010050 if (map)
10051 {
10052 /* map(): replace the list item value */
10053 clear_tv(tv);
10054 rettv.v_lock = 0;
10055 *tv = rettv;
10056 }
10057 else
10058 {
10059 int error = FALSE;
10060
10061 /* filter(): when expr is zero remove the item */
10062 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10063 clear_tv(&rettv);
10064 /* On type error, nothing has been removed; return FAIL to stop the
10065 * loop. The error message was given by get_tv_number_chk(). */
10066 if (error)
10067 goto theend;
10068 }
10069 retval = OK;
10070theend:
10071 clear_tv(&vimvars[VV_VAL].vv_tv);
10072 return retval;
10073}
10074
10075
10076/*
10077 * Implementation of map() and filter().
10078 */
10079 void
10080filter_map(typval_T *argvars, typval_T *rettv, int map)
10081{
10082 typval_T *expr;
10083 listitem_T *li, *nli;
10084 list_T *l = NULL;
10085 dictitem_T *di;
10086 hashtab_T *ht;
10087 hashitem_T *hi;
10088 dict_T *d = NULL;
10089 typval_T save_val;
10090 typval_T save_key;
10091 int rem;
10092 int todo;
10093 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10094 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10095 : N_("filter() argument"));
10096 int save_did_emsg;
10097 int idx = 0;
10098
10099 if (argvars[0].v_type == VAR_LIST)
10100 {
10101 if ((l = argvars[0].vval.v_list) == NULL
10102 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10103 return;
10104 }
10105 else if (argvars[0].v_type == VAR_DICT)
10106 {
10107 if ((d = argvars[0].vval.v_dict) == NULL
10108 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10109 return;
10110 }
10111 else
10112 {
10113 EMSG2(_(e_listdictarg), ermsg);
10114 return;
10115 }
10116
10117 expr = &argvars[1];
10118 /* On type errors, the preceding call has already displayed an error
10119 * message. Avoid a misleading error message for an empty string that
10120 * was not passed as argument. */
10121 if (expr->v_type != VAR_UNKNOWN)
10122 {
10123 prepare_vimvar(VV_VAL, &save_val);
10124
10125 /* We reset "did_emsg" to be able to detect whether an error
10126 * occurred during evaluation of the expression. */
10127 save_did_emsg = did_emsg;
10128 did_emsg = FALSE;
10129
10130 prepare_vimvar(VV_KEY, &save_key);
10131 if (argvars[0].v_type == VAR_DICT)
10132 {
10133 vimvars[VV_KEY].vv_type = VAR_STRING;
10134
10135 ht = &d->dv_hashtab;
10136 hash_lock(ht);
10137 todo = (int)ht->ht_used;
10138 for (hi = ht->ht_array; todo > 0; ++hi)
10139 {
10140 if (!HASHITEM_EMPTY(hi))
10141 {
10142 int r;
10143
10144 --todo;
10145 di = HI2DI(hi);
10146 if (map &&
10147 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10148 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10149 break;
10150 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10151 r = filter_map_one(&di->di_tv, expr, map, &rem);
10152 clear_tv(&vimvars[VV_KEY].vv_tv);
10153 if (r == FAIL || did_emsg)
10154 break;
10155 if (!map && rem)
10156 {
10157 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10158 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10159 break;
10160 dictitem_remove(d, di);
10161 }
10162 }
10163 }
10164 hash_unlock(ht);
10165 }
10166 else
10167 {
10168 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10169
10170 for (li = l->lv_first; li != NULL; li = nli)
10171 {
10172 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10173 break;
10174 nli = li->li_next;
10175 vimvars[VV_KEY].vv_nr = idx;
10176 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10177 || did_emsg)
10178 break;
10179 if (!map && rem)
10180 listitem_remove(l, li);
10181 ++idx;
10182 }
10183 }
10184
10185 restore_vimvar(VV_KEY, &save_key);
10186 restore_vimvar(VV_VAL, &save_val);
10187
10188 did_emsg |= save_did_emsg;
10189 }
10190
10191 copy_tv(&argvars[0], rettv);
10192}
10193
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */