blob: 11fc035fb40750114ef4e060ae0dcc9170ce2078 [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 Moolenaar33570922005-01-25 22:26:29 +0000190};
191
192/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000193#define vv_type vv_di.di_tv.v_type
194#define vv_nr vv_di.di_tv.vval.v_number
195#define vv_float vv_di.di_tv.vval.v_float
196#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000197#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200198#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000199#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000200
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200201static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define vimvarht vimvardict.dv_hashtab
203
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100204static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
205static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
206static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100207static void list_glob_vars(int *first);
208static void list_buf_vars(int *first);
209static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000210#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100211static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000212#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100213static void list_vim_vars(int *first);
214static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100215static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
216static 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 +0100217static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
218static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100219static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
220static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
221static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
222static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000223
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static int eval2(char_u **arg, typval_T *rettv, int evaluate);
225static int eval3(char_u **arg, typval_T *rettv, int evaluate);
226static int eval4(char_u **arg, typval_T *rettv, int evaluate);
227static int eval5(char_u **arg, typval_T *rettv, int evaluate);
228static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
229static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000230
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100231static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
233static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100235static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100236static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100237static 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 +0200238static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100240static void delete_var(hashtab_T *ht, hashitem_T *hi);
241static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
242static 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 +0100243static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200244
245#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static int compare_func_name(const void *s1, const void *s2);
247static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200248#endif
249
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200250/* for VIM_VERSION_ defines */
251#include "version.h"
252
Bram Moolenaar33570922005-01-25 22:26:29 +0000253/*
254 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000255 */
256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100257eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000258{
Bram Moolenaar33570922005-01-25 22:26:29 +0000259 int i;
260 struct vimvar *p;
261
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200262 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
263 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200264 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000265 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200266 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000267
268 for (i = 0; i < VV_LEN; ++i)
269 {
270 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200271 if (STRLEN(p->vv_name) > 16)
272 {
Bram Moolenaarde330112017-01-08 20:50:52 +0100273 IEMSG("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200274 getout(1);
275 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 STRCPY(p->vv_di.di_key, p->vv_name);
277 if (p->vv_flags & VV_RO)
278 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
279 else if (p->vv_flags & VV_RO_SBX)
280 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
281 else
282 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000283
284 /* add to v: scope dict, unless the value is not always available */
285 if (p->vv_type != VAR_UNKNOWN)
286 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000288 /* add to compat scope dict */
289 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100291 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
292
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000293 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100294 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200295 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100296 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100297
298 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
299 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
300 set_vim_var_nr(VV_NONE, VVAL_NONE);
301 set_vim_var_nr(VV_NULL, VVAL_NULL);
302
Bram Moolenaarf562e722016-07-19 17:25:25 +0200303 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
304 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
305 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
306 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
307 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
308 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
309 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
310 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
311 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
312 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
313
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200314 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200315
316#ifdef EBCDIC
317 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100318 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200319 */
320 sortFunctions();
321#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000322}
323
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000324#if defined(EXITFREE) || defined(PROTO)
325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000327{
328 int i;
329 struct vimvar *p;
330
331 for (i = 0; i < VV_LEN; ++i)
332 {
333 p = &vimvars[i];
334 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000335 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000336 vim_free(p->vv_str);
337 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000338 }
339 else if (p->vv_di.di_tv.v_type == VAR_LIST)
340 {
341 list_unref(p->vv_list);
342 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000343 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000344 }
345 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000346 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000347 hash_clear(&compat_hashtab);
348
Bram Moolenaard9fba312005-06-26 22:34:35 +0000349 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100350# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200351 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100352# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000353
354 /* global variables */
355 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000356
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000357 /* autoloaded script names */
358 ga_clear_strings(&ga_loaded);
359
Bram Moolenaarcca74132013-09-25 21:00:28 +0200360 /* Script-local variables. First clear all the variables and in a second
361 * loop free the scriptvar_T, because a variable in one script might hold
362 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200363 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200364 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200365 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200366 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200367 ga_clear(&ga_scripts);
368
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000369 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200370 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371
372 /* functions */
373 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000374}
375#endif
376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 * Set an internal variable to a string value. Creates the variable if it does
380 * not already exist.
381 */
382 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100383set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000385 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000386 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
388 val = vim_strsave(value);
389 if (val != NULL)
390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000391 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000392 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000394 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000395 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 }
397 }
398}
399
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000400static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200401#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000402static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000403static char_u *redir_endp = NULL;
404static char_u *redir_varname = NULL;
405
406/*
407 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100408 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000409 * Returns OK if successfully completed the setup. FAIL otherwise.
410 */
411 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100412var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000413{
414 int save_emsg;
415 int err;
416 typval_T tv;
417
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000418 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000419 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000420 {
421 EMSG(_(e_invarg));
422 return FAIL;
423 }
424
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000425 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000426 redir_varname = vim_strsave(name);
427 if (redir_varname == NULL)
428 return FAIL;
429
430 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
431 if (redir_lval == NULL)
432 {
433 var_redir_stop();
434 return FAIL;
435 }
436
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000437 /* The output is stored in growarray "redir_ga" until redirection ends. */
438 ga_init2(&redir_ga, (int)sizeof(char), 500);
439
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000440 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100441 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000442 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000443 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
444 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200445 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000446 if (redir_endp != NULL && *redir_endp != NUL)
447 /* Trailing characters are present after the variable name */
448 EMSG(_(e_trailing));
449 else
450 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000451 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000452 var_redir_stop();
453 return FAIL;
454 }
455
456 /* check if we can write to the variable: set it to or append an empty
457 * string */
458 save_emsg = did_emsg;
459 did_emsg = FALSE;
460 tv.v_type = VAR_STRING;
461 tv.vval.v_string = (char_u *)"";
462 if (append)
463 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
464 else
465 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200466 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000468 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000469 if (err)
470 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000471 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472 var_redir_stop();
473 return FAIL;
474 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000475
476 return OK;
477}
478
479/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000480 * Append "value[value_len]" to the variable set by var_redir_start().
481 * The actual appending is postponed until redirection ends, because the value
482 * appended may in fact be the string we write to, changing it may cause freed
483 * memory to be used:
484 * :redir => foo
485 * :let foo
486 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000487 */
488 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100489var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000490{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000491 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492
493 if (redir_lval == NULL)
494 return;
495
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000496 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000497 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000499 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000500
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000501 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000502 {
503 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000504 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000505 }
506 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000507 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000508}
509
510/*
511 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000512 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 */
514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100515var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000517 typval_T tv;
518
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200519 if (EVALCMD_BUSY)
520 {
521 redir_lval = NULL;
522 return;
523 }
524
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525 if (redir_lval != NULL)
526 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000527 /* If there was no error: assign the text to the variable. */
528 if (redir_endp != NULL)
529 {
530 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
531 tv.v_type = VAR_STRING;
532 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200533 /* Call get_lval() again, if it's inside a Dict or List it may
534 * have changed. */
535 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100536 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200537 if (redir_endp != NULL && redir_lval->ll_name != NULL)
538 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
539 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000540 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000541
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 /* free the collected output */
543 vim_free(redir_ga.ga_data);
544 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000545
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546 vim_free(redir_lval);
547 redir_lval = NULL;
548 }
549 vim_free(redir_varname);
550 redir_varname = NULL;
551}
552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553# if defined(FEAT_MBYTE) || defined(PROTO)
554 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100555eval_charconvert(
556 char_u *enc_from,
557 char_u *enc_to,
558 char_u *fname_from,
559 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 int err = FALSE;
562
563 set_vim_var_string(VV_CC_FROM, enc_from, -1);
564 set_vim_var_string(VV_CC_TO, enc_to, -1);
565 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
566 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
567 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
568 err = TRUE;
569 set_vim_var_string(VV_CC_FROM, NULL, -1);
570 set_vim_var_string(VV_CC_TO, NULL, -1);
571 set_vim_var_string(VV_FNAME_IN, NULL, -1);
572 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
573
574 if (err)
575 return FAIL;
576 return OK;
577}
578# endif
579
580# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 int err = FALSE;
585
586 set_vim_var_string(VV_FNAME_IN, fname, -1);
587 set_vim_var_string(VV_CMDARG, args, -1);
588 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
589 err = TRUE;
590 set_vim_var_string(VV_FNAME_IN, NULL, -1);
591 set_vim_var_string(VV_CMDARG, NULL, -1);
592
593 if (err)
594 {
595 mch_remove(fname);
596 return FAIL;
597 }
598 return OK;
599}
600# endif
601
602# if defined(FEAT_DIFF) || defined(PROTO)
603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100604eval_diff(
605 char_u *origfile,
606 char_u *newfile,
607 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 int err = FALSE;
610
611 set_vim_var_string(VV_FNAME_IN, origfile, -1);
612 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
613 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
614 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
615 set_vim_var_string(VV_FNAME_IN, NULL, -1);
616 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
617 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
618}
619
620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100621eval_patch(
622 char_u *origfile,
623 char_u *difffile,
624 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 int err;
627
628 set_vim_var_string(VV_FNAME_IN, origfile, -1);
629 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
630 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
631 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
632 set_vim_var_string(VV_FNAME_IN, NULL, -1);
633 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
634 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
635}
636# endif
637
638/*
639 * Top level evaluation function, returning a boolean.
640 * Sets "error" to TRUE if there was an error.
641 * Return TRUE or FALSE.
642 */
643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100644eval_to_bool(
645 char_u *arg,
646 int *error,
647 char_u **nextcmd,
648 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
Bram Moolenaar33570922005-01-25 22:26:29 +0000650 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200651 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 if (skip)
654 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000655 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 else
658 {
659 *error = FALSE;
660 if (!skip)
661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000663 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665 }
666 if (skip)
667 --emsg_skip;
668
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200669 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671
672/*
673 * Top level evaluation function, returning a string. If "skip" is TRUE,
674 * only parsing to "nextcmd" is done, without reporting errors. Return
675 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
676 */
677 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100678eval_to_string_skip(
679 char_u *arg,
680 char_u **nextcmd,
681 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
Bram Moolenaar33570922005-01-25 22:26:29 +0000683 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 char_u *retval;
685
686 if (skip)
687 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000688 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 retval = NULL;
690 else
691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000692 retval = vim_strsave(get_tv_string(&tv));
693 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695 if (skip)
696 --emsg_skip;
697
698 return retval;
699}
700
701/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000702 * Skip over an expression at "*pp".
703 * Return FAIL for an error, OK otherwise.
704 */
705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100706skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000707{
Bram Moolenaar33570922005-01-25 22:26:29 +0000708 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000709
710 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000711 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000712}
713
714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000716 * When "convert" is TRUE convert a List into a sequence of lines and convert
717 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 * Return pointer to allocated memory, or NULL for failure.
719 */
720 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100721eval_to_string(
722 char_u *arg,
723 char_u **nextcmd,
724 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000728 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000729#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000730 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000733 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 retval = NULL;
735 else
736 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000737 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000738 {
739 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000740 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200741 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200742 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200743 if (tv.vval.v_list->lv_len > 0)
744 ga_append(&ga, NL);
745 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000746 ga_append(&ga, NUL);
747 retval = (char_u *)ga.ga_data;
748 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000749#ifdef FEAT_FLOAT
750 else if (convert && tv.v_type == VAR_FLOAT)
751 {
752 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
753 retval = vim_strsave(numbuf);
754 }
755#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000756 else
757 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000758 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
760
761 return retval;
762}
763
764/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000765 * Call eval_to_string() without using current local variables and using
766 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100769eval_to_string_safe(
770 char_u *arg,
771 char_u **nextcmd,
772 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
774 char_u *retval;
775 void *save_funccalp;
776
777 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000778 if (use_sandbox)
779 ++sandbox;
780 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000781 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000782 if (use_sandbox)
783 --sandbox;
784 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 restore_funccal(save_funccalp);
786 return retval;
787}
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789/*
790 * Top level evaluation function, returning a number.
791 * Evaluates "expr" silently.
792 * Returns -1 for an error.
793 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200794 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100795eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200798 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000799 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 ++emsg_off;
802
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = -1;
805 else
806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000807 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810 --emsg_off;
811
812 return retval;
813}
814
Bram Moolenaara40058a2005-07-11 22:42:07 +0000815/*
816 * Prepare v: variable "idx" to be used.
817 * Save the current typeval in "save_tv".
818 * When not used yet add the variable to the v: hashtable.
819 */
820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822{
823 *save_tv = vimvars[idx].vv_tv;
824 if (vimvars[idx].vv_type == VAR_UNKNOWN)
825 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
826}
827
828/*
829 * Restore v: variable "idx" to typeval "save_tv".
830 * When no longer defined, remove the variable from the v: hashtable.
831 */
832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100833restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000834{
835 hashitem_T *hi;
836
Bram Moolenaara40058a2005-07-11 22:42:07 +0000837 vimvars[idx].vv_tv = *save_tv;
838 if (vimvars[idx].vv_type == VAR_UNKNOWN)
839 {
840 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
841 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100842 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000843 else
844 hash_remove(&vimvarht, hi);
845 }
846}
847
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000848#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000849/*
850 * Evaluate an expression to a list with suggestions.
851 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000852 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000853 */
854 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100855eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000856{
857 typval_T save_val;
858 typval_T rettv;
859 list_T *list = NULL;
860 char_u *p = skipwhite(expr);
861
862 /* Set "v:val" to the bad word. */
863 prepare_vimvar(VV_VAL, &save_val);
864 vimvars[VV_VAL].vv_type = VAR_STRING;
865 vimvars[VV_VAL].vv_str = badword;
866 if (p_verbose == 0)
867 ++emsg_off;
868
869 if (eval1(&p, &rettv, TRUE) == OK)
870 {
871 if (rettv.v_type != VAR_LIST)
872 clear_tv(&rettv);
873 else
874 list = rettv.vval.v_list;
875 }
876
877 if (p_verbose == 0)
878 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000879 restore_vimvar(VV_VAL, &save_val);
880
881 return list;
882}
883
884/*
885 * "list" is supposed to contain two items: a word and a number. Return the
886 * word in "pp" and the number as the return value.
887 * Return -1 if anything isn't right.
888 * Used to get the good word and score from the eval_spell_expr() result.
889 */
890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100891get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000892{
893 listitem_T *li;
894
895 li = list->lv_first;
896 if (li == NULL)
897 return -1;
898 *pp = get_tv_string(&li->li_tv);
899
900 li = li->li_next;
901 if (li == NULL)
902 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200903 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000904}
905#endif
906
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000907/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000908 * Top level evaluation function.
909 * Returns an allocated typval_T with the result.
910 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000911 */
912 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100913eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000914{
915 typval_T *tv;
916
917 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000919 {
920 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000921 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000922 }
923
924 return tv;
925}
926
927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000929 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000930 * Uses argv[argc] for the function arguments. Only Number and String
931 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000932 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100935call_vim_function(
936 char_u *func,
937 int argc,
938 char_u **argv,
939 int safe, /* use the sandbox */
940 int str_arg_only, /* all arguments are strings */
941 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200944 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 int len;
946 int i;
947 int doesrange;
948 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000949 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000951 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000953 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 for (i = 0; i < argc; i++)
956 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000957 /* Pass a NULL or empty argument as an empty string */
958 if (argv[i] == NULL || *argv[i] == NUL)
959 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000960 argvars[i].v_type = VAR_STRING;
961 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000962 continue;
963 }
964
Bram Moolenaar0cbba942012-07-25 16:47:03 +0200965 if (str_arg_only)
966 len = 0;
967 else
968 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100969 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (len != 0 && len == (int)STRLEN(argv[i]))
971 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000972 argvars[i].v_type = VAR_NUMBER;
973 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 else
976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000977 argvars[i].v_type = VAR_STRING;
978 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 }
981
982 if (safe)
983 {
984 save_funccalp = save_funccal();
985 ++sandbox;
986 }
987
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000988 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200989 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100991 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (safe)
993 {
994 --sandbox;
995 restore_funccal(save_funccalp);
996 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000997 vim_free(argvars);
998
999 if (ret == FAIL)
1000 clear_tv(rettv);
1001
1002 return ret;
1003}
1004
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001005/*
1006 * Call vimL function "func" and return the result as a number.
1007 * Returns -1 when calling the function fails.
1008 * Uses argv[argc] for the function arguments.
1009 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001010 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001011call_func_retnr(
1012 char_u *func,
1013 int argc,
1014 char_u **argv,
1015 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001016{
1017 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001018 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001019
1020 /* All arguments are passed as strings, no conversion to number. */
1021 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1022 return -1;
1023
1024 retval = get_tv_number_chk(&rettv, NULL);
1025 clear_tv(&rettv);
1026 return retval;
1027}
1028
1029#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1030 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1031
Bram Moolenaar4f688582007-07-24 12:34:30 +00001032# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001033/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001034 * Call vimL function "func" and return the result as a string.
1035 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001036 * Uses argv[argc] for the function arguments.
1037 */
1038 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001039call_func_retstr(
1040 char_u *func,
1041 int argc,
1042 char_u **argv,
1043 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001044{
1045 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001046 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001047
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001048 /* All arguments are passed as strings, no conversion to number. */
1049 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001050 return NULL;
1051
1052 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001053 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 return retval;
1055}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001056# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001057
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001058/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001059 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001060 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001061 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001062 */
1063 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064call_func_retlist(
1065 char_u *func,
1066 int argc,
1067 char_u **argv,
1068 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001069{
1070 typval_T rettv;
1071
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001072 /* All arguments are passed as strings, no conversion to number. */
1073 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001074 return NULL;
1075
1076 if (rettv.v_type != VAR_LIST)
1077 {
1078 clear_tv(&rettv);
1079 return NULL;
1080 }
1081
1082 return rettv.vval.v_list;
1083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085
Bram Moolenaar05159a02005-02-26 23:04:13 +00001086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef FEAT_FOLDING
1088/*
1089 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1090 * it in "*cp". Doesn't give error messages.
1091 */
1092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001093eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar33570922005-01-25 22:26:29 +00001095 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001096 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001098 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1099 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
1101 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001102 if (use_sandbox)
1103 ++sandbox;
1104 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001106 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 retval = 0;
1108 else
1109 {
1110 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001111 if (tv.v_type == VAR_NUMBER)
1112 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001113 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 retval = 0;
1115 else
1116 {
1117 /* If the result is a string, check if there is a non-digit before
1118 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (!VIM_ISDIGIT(*s) && *s != '-')
1121 *cp = *s++;
1122 retval = atol((char *)s);
1123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001127 if (use_sandbox)
1128 --sandbox;
1129 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001131 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132}
1133#endif
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 * ":let" list all variable values
1137 * ":let var1 var2" list variable values
1138 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001139 * ":let var += expr" assignment command.
1140 * ":let var -= expr" assignment command.
1141 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 */
1144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001145ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
1147 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001148 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 int var_count = 0;
1152 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001153 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001154 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001155 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaardb552d602006-03-23 22:59:57 +00001157 argend = skip_var_list(arg, &var_count, &semicolon);
1158 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001159 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001160 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1161 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001162 expr = skipwhite(argend);
1163 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1164 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001166 /*
1167 * ":let" without "=": list variables
1168 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001169 if (*arg == '[')
1170 EMSG(_(e_invarg));
1171 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001173 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001176 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001177 list_glob_vars(&first);
1178 list_buf_vars(&first);
1179 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001180#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001181 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001182#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001183 list_script_vars(&first);
1184 list_func_vars(&first);
1185 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 eap->nextcmd = check_nextcmd(arg);
1188 }
1189 else
1190 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001191 op[0] = '=';
1192 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001193 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001194 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001195 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1196 op[0] = *expr; /* +=, -= or .= */
1197 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001198 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001199 else
1200 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (eap->skip)
1203 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001204 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (eap->skip)
1206 {
1207 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 --emsg_skip;
1210 }
1211 else if (i != FAIL)
1212 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001213 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 }
1218}
1219
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001220/*
1221 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1222 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001223 * When "nextchars" is not NULL it points to a string with characters that
1224 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1225 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001226 * Returns OK or FAIL;
1227 */
1228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001229ex_let_vars(
1230 char_u *arg_start,
1231 typval_T *tv,
1232 int copy, /* copy values from "tv", don't move */
1233 int semicolon, /* from skip_var_list() */
1234 int var_count, /* from skip_var_list() */
1235 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001236{
1237 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001239 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001240 listitem_T *item;
1241 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001242
1243 if (*arg != '[')
1244 {
1245 /*
1246 * ":let var = expr" or ":for var in list"
1247 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001248 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001249 return FAIL;
1250 return OK;
1251 }
1252
1253 /*
1254 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1255 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001256 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001257 {
1258 EMSG(_(e_listreq));
1259 return FAIL;
1260 }
1261
1262 i = list_len(l);
1263 if (semicolon == 0 && var_count < i)
1264 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001265 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266 return FAIL;
1267 }
1268 if (var_count - semicolon > i)
1269 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001270 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271 return FAIL;
1272 }
1273
1274 item = l->lv_first;
1275 while (*arg != ']')
1276 {
1277 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001279 item = item->li_next;
1280 if (arg == NULL)
1281 return FAIL;
1282
1283 arg = skipwhite(arg);
1284 if (*arg == ';')
1285 {
1286 /* Put the rest of the list (may be empty) in the var after ';'.
1287 * Create a new list for this. */
1288 l = list_alloc();
1289 if (l == NULL)
1290 return FAIL;
1291 while (item != NULL)
1292 {
1293 list_append_tv(l, &item->li_tv);
1294 item = item->li_next;
1295 }
1296
1297 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001298 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001299 ltv.vval.v_list = l;
1300 l->lv_refcount = 1;
1301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1303 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001304 clear_tv(&ltv);
1305 if (arg == NULL)
1306 return FAIL;
1307 break;
1308 }
1309 else if (*arg != ',' && *arg != ']')
1310 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001311 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001312 return FAIL;
1313 }
1314 }
1315
1316 return OK;
1317}
1318
1319/*
1320 * Skip over assignable variable "var" or list of variables "[var, var]".
1321 * Used for ":let varvar = expr" and ":for varvar in expr".
1322 * For "[var, var]" increment "*var_count" for each variable.
1323 * for "[var, var; var]" set "semicolon".
1324 * Return NULL for an error.
1325 */
1326 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327skip_var_list(
1328 char_u *arg,
1329 int *var_count,
1330 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001331{
1332 char_u *p, *s;
1333
1334 if (*arg == '[')
1335 {
1336 /* "[var, var]": find the matching ']'. */
1337 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001338 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001339 {
1340 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1341 s = skip_var_one(p);
1342 if (s == p)
1343 {
1344 EMSG2(_(e_invarg2), p);
1345 return NULL;
1346 }
1347 ++*var_count;
1348
1349 p = skipwhite(s);
1350 if (*p == ']')
1351 break;
1352 else if (*p == ';')
1353 {
1354 if (*semicolon == 1)
1355 {
1356 EMSG(_("Double ; in list of variables"));
1357 return NULL;
1358 }
1359 *semicolon = 1;
1360 }
1361 else if (*p != ',')
1362 {
1363 EMSG2(_(e_invarg2), p);
1364 return NULL;
1365 }
1366 }
1367 return p + 1;
1368 }
1369 else
1370 return skip_var_one(arg);
1371}
1372
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001373/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001374 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001375 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001376 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001378skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001379{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001380 if (*arg == '@' && arg[1] != NUL)
1381 return arg + 2;
1382 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1383 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001384}
1385
Bram Moolenaara7043832005-01-21 11:56:39 +00001386/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 * List variables for hashtab "ht" with prefix "prefix".
1388 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001389 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001391list_hashtable_vars(
1392 hashtab_T *ht,
1393 char_u *prefix,
1394 int empty,
1395 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 hashitem_T *hi;
1398 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001399 int todo;
1400
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001401 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001402 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1403 {
1404 if (!HASHITEM_EMPTY(hi))
1405 {
1406 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 di = HI2DI(hi);
1408 if (empty || di->di_tv.v_type != VAR_STRING
1409 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001410 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001411 }
1412 }
1413}
1414
1415/*
1416 * List global variables.
1417 */
1418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001419list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001420{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001421 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001422}
1423
1424/*
1425 * List buffer variables.
1426 */
1427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001428list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001429{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001430 char_u numbuf[NUMBUFLEN];
1431
Bram Moolenaar429fa852013-04-15 12:27:36 +02001432 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001433 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001434
1435 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001436 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1437 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001438}
1439
1440/*
1441 * List window variables.
1442 */
1443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001445{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001446 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001447 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001448}
1449
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001450#ifdef FEAT_WINDOWS
1451/*
1452 * List tab page variables.
1453 */
1454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001455list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001456{
Bram Moolenaar429fa852013-04-15 12:27:36 +02001457 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001458 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001459}
1460#endif
1461
Bram Moolenaara7043832005-01-21 11:56:39 +00001462/*
1463 * List Vim variables.
1464 */
1465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001466list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001468 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469}
1470
1471/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001472 * List script-local variables, if there is a script.
1473 */
1474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001475list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001476{
1477 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001478 list_hashtable_vars(&SCRIPT_VARS(current_SID),
1479 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001480}
1481
1482/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 * List variables in "arg".
1484 */
1485 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487{
1488 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001489 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001491 char_u *name_start;
1492 char_u *arg_subsc;
1493 char_u *tofree;
1494 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495
1496 while (!ends_excmd(*arg) && !got_int)
1497 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001498 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001500 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001501 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1502 {
1503 emsg_severe = TRUE;
1504 EMSG(_(e_trailing));
1505 break;
1506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001510 /* get_name_len() takes care of expanding curly braces */
1511 name_start = name = arg;
1512 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1513 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001515 /* This is mainly to keep test 49 working: when expanding
1516 * curly braces fails overrule the exception error message. */
1517 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001519 emsg_severe = TRUE;
1520 EMSG2(_(e_invarg2), arg);
1521 break;
1522 }
1523 error = TRUE;
1524 }
1525 else
1526 {
1527 if (tofree != NULL)
1528 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001529 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 else
1532 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001533 /* handle d.key, l[idx], f(expr) */
1534 arg_subsc = arg;
1535 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001536 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001539 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001541 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001542 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001543 case 'g': list_glob_vars(first); break;
1544 case 'b': list_buf_vars(first); break;
1545 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001546#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001547 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001548#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001549 case 'v': list_vim_vars(first); break;
1550 case 's': list_script_vars(first); break;
1551 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001552 default:
1553 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001554 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001555 }
1556 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 {
1558 char_u numbuf[NUMBUFLEN];
1559 char_u *tf;
1560 int c;
1561 char_u *s;
1562
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001563 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001564 c = *arg;
1565 *arg = NUL;
1566 list_one_var_a((char_u *)"",
1567 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001568 tv.v_type,
1569 s == NULL ? (char_u *)"" : s,
1570 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001571 *arg = c;
1572 vim_free(tf);
1573 }
1574 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 }
1577 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001578
1579 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001581
1582 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 }
1584
1585 return arg;
1586}
1587
1588/*
1589 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1590 * Returns a pointer to the char just after the var name.
1591 * Returns NULL if there is an error.
1592 */
1593 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594ex_let_one(
1595 char_u *arg, /* points to variable name */
1596 typval_T *tv, /* value to assign to variable */
1597 int copy, /* copy value from "tv" */
1598 char_u *endchars, /* valid chars after variable name or NULL */
1599 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600{
1601 int c1;
1602 char_u *name;
1603 char_u *p;
1604 char_u *arg_end = NULL;
1605 int len;
1606 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608
1609 /*
1610 * ":let $VAR = expr": Set environment variable.
1611 */
1612 if (*arg == '$')
1613 {
1614 /* Find the end of the name. */
1615 ++arg;
1616 name = arg;
1617 len = get_env_len(&arg);
1618 if (len == 0)
1619 EMSG2(_(e_invarg2), name - 1);
1620 else
1621 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622 if (op != NULL && (*op == '+' || *op == '-'))
1623 EMSG2(_(e_letwrong), op);
1624 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001627 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 {
1629 c1 = name[len];
1630 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001631 p = get_tv_string_chk(tv);
1632 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 {
1634 int mustfree = FALSE;
1635 char_u *s = vim_getenv(name, &mustfree);
1636
1637 if (s != NULL)
1638 {
1639 p = tofree = concat_str(s, p);
1640 if (mustfree)
1641 vim_free(s);
1642 }
1643 }
1644 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001647 if (STRICMP(name, "HOME") == 0)
1648 init_homedir();
1649 else if (didset_vim && STRICMP(name, "VIM") == 0)
1650 didset_vim = FALSE;
1651 else if (didset_vimruntime
1652 && STRICMP(name, "VIMRUNTIME") == 0)
1653 didset_vimruntime = FALSE;
1654 arg_end = arg;
1655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 }
1659 }
1660 }
1661
1662 /*
1663 * ":let &option = expr": Set option value.
1664 * ":let &l:option = expr": Set local option value.
1665 * ":let &g:option = expr": Set global option value.
1666 */
1667 else if (*arg == '&')
1668 {
1669 /* Find the end of the name. */
1670 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 if (p == NULL || (endchars != NULL
1672 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 EMSG(_(e_letunexp));
1674 else
1675 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 long n;
1677 int opt_type;
1678 long numval;
1679 char_u *stringval = NULL;
1680 char_u *s;
1681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 c1 = *p;
1683 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001685 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001686 s = get_tv_string_chk(tv); /* != NULL if number or string */
1687 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 {
1689 opt_type = get_option_value(arg, &numval,
1690 &stringval, opt_flags);
1691 if ((opt_type == 1 && *op == '.')
1692 || (opt_type == 0 && *op != '.'))
1693 EMSG2(_(e_letwrong), op);
1694 else
1695 {
1696 if (opt_type == 1) /* number */
1697 {
1698 if (*op == '+')
1699 n = numval + n;
1700 else
1701 n = numval - n;
1702 }
1703 else if (opt_type == 0 && stringval != NULL) /* string */
1704 {
1705 s = concat_str(stringval, s);
1706 vim_free(stringval);
1707 stringval = s;
1708 }
1709 }
1710 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001711 if (s != NULL)
1712 {
1713 set_option_value(arg, n, s, opt_flags);
1714 arg_end = p;
1715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 }
1719 }
1720
1721 /*
1722 * ":let @r = expr": Set register contents.
1723 */
1724 else if (*arg == '@')
1725 {
1726 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 if (op != NULL && (*op == '+' || *op == '-'))
1728 EMSG2(_(e_letwrong), op);
1729 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 EMSG(_(e_letunexp));
1732 else
1733 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 char_u *s;
1736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001737 p = get_tv_string_chk(tv);
1738 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001740 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741 if (s != NULL)
1742 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001743 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001744 vim_free(s);
1745 }
1746 }
1747 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001748 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001750 arg_end = arg + 1;
1751 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001752 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 }
1754 }
1755
1756 /*
1757 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001758 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001760 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001764 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001765 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001767 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1768 EMSG(_(e_letunexp));
1769 else
1770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001772 arg_end = p;
1773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001775 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 }
1777
1778 else
1779 EMSG2(_(e_invarg2), arg);
1780
1781 return arg_end;
1782}
1783
1784/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001785 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1786 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001787 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001789{
1790 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1791 {
1792 EMSG2(_(e_readonlyvar), arg);
1793 return TRUE;
1794 }
1795 return FALSE;
1796}
1797
1798/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001799 * Get an lval: variable, Dict item or List item that can be assigned a value
1800 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1801 * "name.key", "name.key[expr]" etc.
1802 * Indexing only works if "name" is an existing List or Dictionary.
1803 * "name" points to the start of the name.
1804 * If "rettv" is not NULL it points to the value to be assigned.
1805 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1806 * wrong; must end in space or cmd separator.
1807 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001808 * flags:
1809 * GLV_QUIET: do not give error messages
1810 * GLV_NO_AUTOLOAD: do not use script autoloading
1811 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001812 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001813 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001814 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817get_lval(
1818 char_u *name,
1819 typval_T *rettv,
1820 lval_T *lp,
1821 int unlet,
1822 int skip,
1823 int flags, /* GLV_ values */
1824 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001827 char_u *expr_start, *expr_end;
1828 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 dictitem_T *v;
1830 typval_T var1;
1831 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001832 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001834 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001835 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001837 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001839 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841
1842 if (skip)
1843 {
1844 /* When skipping just find the end of the name. */
1845 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001846 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 }
1848
1849 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001850 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001851 if (expr_start != NULL)
1852 {
1853 /* Don't expand the name when we already know there is an error. */
1854 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1855 && *p != '[' && *p != '.')
1856 {
1857 EMSG(_(e_trailing));
1858 return NULL;
1859 }
1860
1861 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1862 if (lp->ll_exp_name == NULL)
1863 {
1864 /* Report an invalid expression in braces, unless the
1865 * expression evaluation has been cancelled due to an
1866 * aborting error, an interrupt, or an exception. */
1867 if (!aborting() && !quiet)
1868 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001869 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001870 EMSG2(_(e_invarg2), name);
1871 return NULL;
1872 }
1873 }
1874 lp->ll_name = lp->ll_exp_name;
1875 }
1876 else
1877 lp->ll_name = name;
1878
1879 /* Without [idx] or .key we are done. */
1880 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1881 return p;
1882
1883 cc = *p;
1884 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001885 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001886 if (v == NULL && !quiet)
1887 EMSG2(_(e_undefvar), lp->ll_name);
1888 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 if (v == NULL)
1890 return NULL;
1891
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001892 /*
1893 * Loop until no more [idx] or .key is following.
1894 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1899 && !(lp->ll_tv->v_type == VAR_DICT
1900 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 if (!quiet)
1903 EMSG(_("E689: Can only index a List or Dictionary"));
1904 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 if (!quiet)
1909 EMSG(_("E708: [:] must come last"));
1910 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001912
Bram Moolenaar8c711452005-01-14 21:53:12 +00001913 len = -1;
1914 if (*p == '.')
1915 {
1916 key = p + 1;
1917 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1918 ;
1919 if (len == 0)
1920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 if (!quiet)
1922 EMSG(_(e_emptykey));
1923 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001924 }
1925 p = key + len;
1926 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001927 else
1928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001929 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001930 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001931 if (*p == ':')
1932 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001933 else
1934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001935 empty1 = FALSE;
1936 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001938 if (get_tv_string_chk(&var1) == NULL)
1939 {
1940 /* not a number or string */
1941 clear_tv(&var1);
1942 return NULL;
1943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001944 }
1945
1946 /* Optionally get the second index [ :expr]. */
1947 if (*p == ':')
1948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001951 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001953 if (!empty1)
1954 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001955 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 if (rettv != NULL && (rettv->v_type != VAR_LIST
1958 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 if (!quiet)
1961 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 if (!empty1)
1963 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001965 }
1966 p = skipwhite(p + 1);
1967 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001969 else
1970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001972 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001974 if (!empty1)
1975 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001978 if (get_tv_string_chk(&var2) == NULL)
1979 {
1980 /* not a number or string */
1981 if (!empty1)
1982 clear_tv(&var1);
1983 clear_tv(&var2);
1984 return NULL;
1985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001987 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001989 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001991
Bram Moolenaar8c711452005-01-14 21:53:12 +00001992 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001993 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 if (!quiet)
1995 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001996 if (!empty1)
1997 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001999 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002000 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002001 }
2002
2003 /* Skip to past ']'. */
2004 ++p;
2005 }
2006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002008 {
2009 if (len == -1)
2010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002012 key = get_tv_string_chk(&var1); /* is number or string */
2013 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002014 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002015 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002017 }
2018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 lp->ll_list = NULL;
2020 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002021 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002022
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002023 /* When assigning to a scope dictionary check that a function and
2024 * variable name is valid (only variable name unless it is l: or
2025 * g: dictionary). Disallow overwriting a builtin function. */
2026 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002027 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002028 int prevval;
2029 int wrong;
2030
2031 if (len != -1)
2032 {
2033 prevval = key[len];
2034 key[len] = NUL;
2035 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002036 else
2037 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002038 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2039 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002040 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002041 || !valid_varname(key);
2042 if (len != -1)
2043 key[len] = prevval;
2044 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002045 return NULL;
2046 }
2047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002050 /* Can't add "v:" variable. */
2051 if (lp->ll_dict == &vimvardict)
2052 {
2053 EMSG2(_(e_illvar), name);
2054 return NULL;
2055 }
2056
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002057 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002062 if (len == -1)
2063 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002065 }
2066 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002068 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 if (len == -1)
2071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 p = NULL;
2074 break;
2075 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002076 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002077 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002078 return NULL;
2079
Bram Moolenaar8c711452005-01-14 21:53:12 +00002080 if (len == -1)
2081 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002083 }
2084 else
2085 {
2086 /*
2087 * Get the number and item for the only or first index of the List.
2088 */
2089 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 else
2092 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002093 lp->ll_n1 = (long)get_tv_number(&var1);
2094 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002095 clear_tv(&var1);
2096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 lp->ll_list = lp->ll_tv->vval.v_list;
2099 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2100 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002102 if (lp->ll_n1 < 0)
2103 {
2104 lp->ll_n1 = 0;
2105 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2106 }
2107 }
2108 if (lp->ll_li == NULL)
2109 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002111 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002112 if (!quiet)
2113 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 }
2116
2117 /*
2118 * May need to find the item or absolute index for the second
2119 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 * When no index given: "lp->ll_empty2" is TRUE.
2121 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002125 lp->ll_n2 = (long)get_tv_number(&var2);
2126 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002127 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002132 {
2133 if (!quiet)
2134 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
2139
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2141 if (lp->ll_n1 < 0)
2142 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2143 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002144 {
2145 if (!quiet)
2146 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002148 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002149 }
2150
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 }
2154
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002155 return p;
2156}
2157
2158/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002159 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002160 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163{
2164 vim_free(lp->ll_exp_name);
2165 vim_free(lp->ll_newkey);
2166}
2167
2168/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002169 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 */
2173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174set_var_lval(
2175 lval_T *lp,
2176 char_u *endp,
2177 typval_T *rettv,
2178 int copy,
2179 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180{
2181 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 listitem_T *ri;
2183 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184
2185 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 cc = *endp;
2190 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002191 if (op != NULL && *op != '=')
2192 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002193 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002194
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002195 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002196 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002197 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002198 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002200 if ((di == NULL
2201 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2202 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2203 FALSE)))
2204 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 set_var(lp->ll_name, &tv, FALSE);
2206 clear_tv(&tv);
2207 }
2208 }
2209 else
2210 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002214 else if (tv_check_lock(lp->ll_newkey == NULL
2215 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002216 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002217 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 else if (lp->ll_range)
2219 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002220 listitem_T *ll_li = lp->ll_li;
2221 int ll_n1 = lp->ll_n1;
2222
2223 /*
2224 * Check whether any of the list items is locked
2225 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002226 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002227 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002228 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002229 return;
2230 ri = ri->li_next;
2231 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2232 break;
2233 ll_li = ll_li->li_next;
2234 ++ll_n1;
2235 }
2236
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 /*
2238 * Assign the List values to the list items.
2239 */
2240 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002241 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 if (op != NULL && *op != '=')
2243 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2244 else
2245 {
2246 clear_tv(&lp->ll_li->li_tv);
2247 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2248 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 ri = ri->li_next;
2250 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2251 break;
2252 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002255 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 ri = NULL;
2258 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002259 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002260 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 lp->ll_li = lp->ll_li->li_next;
2262 ++lp->ll_n1;
2263 }
2264 if (ri != NULL)
2265 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 else if (lp->ll_empty2
2267 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 : lp->ll_n1 != lp->ll_n2)
2269 EMSG(_("E711: List value has not enough items"));
2270 }
2271 else
2272 {
2273 /*
2274 * Assign to a List or Dictionary item.
2275 */
2276 if (lp->ll_newkey != NULL)
2277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 if (op != NULL && *op != '=')
2279 {
2280 EMSG2(_(e_letwrong), op);
2281 return;
2282 }
2283
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002285 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 if (di == NULL)
2287 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002288 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2289 {
2290 vim_free(di);
2291 return;
2292 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002294 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 else if (op != NULL && *op != '=')
2296 {
2297 tv_op(lp->ll_tv, rettv, op);
2298 return;
2299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002302
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 /*
2304 * Assign the value to the variable or list item.
2305 */
2306 if (copy)
2307 copy_tv(rettv, lp->ll_tv);
2308 else
2309 {
2310 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002311 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
2314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315}
2316
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002317/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2319 * Returns OK or FAIL.
2320 */
2321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002322tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002324 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 char_u numbuf[NUMBUFLEN];
2326 char_u *s;
2327
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002328 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2329 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2330 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 {
2332 switch (tv1->v_type)
2333 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002334 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 case VAR_DICT:
2336 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002337 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002338 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002339 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002340 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 break;
2342
2343 case VAR_LIST:
2344 if (*op != '+' || tv2->v_type != VAR_LIST)
2345 break;
2346 /* List += List */
2347 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2348 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2349 return OK;
2350
2351 case VAR_NUMBER:
2352 case VAR_STRING:
2353 if (tv2->v_type == VAR_LIST)
2354 break;
2355 if (*op == '+' || *op == '-')
2356 {
2357 /* nr += nr or nr -= nr*/
2358 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002359#ifdef FEAT_FLOAT
2360 if (tv2->v_type == VAR_FLOAT)
2361 {
2362 float_T f = n;
2363
2364 if (*op == '+')
2365 f += tv2->vval.v_float;
2366 else
2367 f -= tv2->vval.v_float;
2368 clear_tv(tv1);
2369 tv1->v_type = VAR_FLOAT;
2370 tv1->vval.v_float = f;
2371 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002373#endif
2374 {
2375 if (*op == '+')
2376 n += get_tv_number(tv2);
2377 else
2378 n -= get_tv_number(tv2);
2379 clear_tv(tv1);
2380 tv1->v_type = VAR_NUMBER;
2381 tv1->vval.v_number = n;
2382 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 }
2384 else
2385 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002386 if (tv2->v_type == VAR_FLOAT)
2387 break;
2388
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 /* str .= str */
2390 s = get_tv_string(tv1);
2391 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2392 clear_tv(tv1);
2393 tv1->v_type = VAR_STRING;
2394 tv1->vval.v_string = s;
2395 }
2396 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002397
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002398 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002399#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002400 {
2401 float_T f;
2402
2403 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2404 && tv2->v_type != VAR_NUMBER
2405 && tv2->v_type != VAR_STRING))
2406 break;
2407 if (tv2->v_type == VAR_FLOAT)
2408 f = tv2->vval.v_float;
2409 else
2410 f = get_tv_number(tv2);
2411 if (*op == '+')
2412 tv1->vval.v_float += f;
2413 else
2414 tv1->vval.v_float -= f;
2415 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002416#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002417 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 }
2419 }
2420
2421 EMSG2(_(e_letwrong), op);
2422 return FAIL;
2423}
2424
2425/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 * Evaluate the expression used in a ":for var in expr" command.
2427 * "arg" points to "var".
2428 * Set "*errp" to TRUE for an error, FALSE otherwise;
2429 * Return a pointer that holds the info. Null when there is an error.
2430 */
2431 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002432eval_for_line(
2433 char_u *arg,
2434 int *errp,
2435 char_u **nextcmdp,
2436 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437{
Bram Moolenaar33570922005-01-25 22:26:29 +00002438 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002440 typval_T tv;
2441 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002442
2443 *errp = TRUE; /* default: there is an error */
2444
Bram Moolenaar33570922005-01-25 22:26:29 +00002445 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002446 if (fi == NULL)
2447 return NULL;
2448
2449 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2450 if (expr == NULL)
2451 return fi;
2452
2453 expr = skipwhite(expr);
2454 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2455 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002456 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002457 return fi;
2458 }
2459
2460 if (skip)
2461 ++emsg_skip;
2462 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2463 {
2464 *errp = FALSE;
2465 if (!skip)
2466 {
2467 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002468 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002469 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002470 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471 clear_tv(&tv);
2472 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002473 else if (l == NULL)
2474 {
2475 /* a null list is like an empty list: do nothing */
2476 clear_tv(&tv);
2477 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002478 else
2479 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002480 /* No need to increment the refcount, it's already set for the
2481 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002482 fi->fi_list = l;
2483 list_add_watch(l, &fi->fi_lw);
2484 fi->fi_lw.lw_item = l->lv_first;
2485 }
2486 }
2487 }
2488 if (skip)
2489 --emsg_skip;
2490
2491 return fi;
2492}
2493
2494/*
2495 * Use the first item in a ":for" list. Advance to the next.
2496 * Assign the values to the variable (list). "arg" points to the first one.
2497 * Return TRUE when a valid item was found, FALSE when at end of list or
2498 * something wrong.
2499 */
2500 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002501next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002502{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002503 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002505 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002506
2507 item = fi->fi_lw.lw_item;
2508 if (item == NULL)
2509 result = FALSE;
2510 else
2511 {
2512 fi->fi_lw.lw_item = item->li_next;
2513 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2514 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2515 }
2516 return result;
2517}
2518
2519/*
2520 * Free the structure used to store info used by ":for".
2521 */
2522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002523free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524{
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002527 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002528 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002530 list_unref(fi->fi_list);
2531 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002532 vim_free(fi);
2533}
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2536
2537 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002538set_context_for_expression(
2539 expand_T *xp,
2540 char_u *arg,
2541 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 int got_eq = FALSE;
2544 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002545 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002547 if (cmdidx == CMD_let)
2548 {
2549 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002550 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002551 {
2552 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002553 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 {
2555 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002557 if (vim_iswhite(*p))
2558 break;
2559 }
2560 return;
2561 }
2562 }
2563 else
2564 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2565 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 while ((xp->xp_pattern = vim_strpbrk(arg,
2567 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2568 {
2569 c = *xp->xp_pattern;
2570 if (c == '&')
2571 {
2572 c = xp->xp_pattern[1];
2573 if (c == '&')
2574 {
2575 ++xp->xp_pattern;
2576 xp->xp_context = cmdidx != CMD_let || got_eq
2577 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2578 }
2579 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002582 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2583 xp->xp_pattern += 2;
2584
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587 else if (c == '$')
2588 {
2589 /* environment variable */
2590 xp->xp_context = EXPAND_ENV_VARS;
2591 }
2592 else if (c == '=')
2593 {
2594 got_eq = TRUE;
2595 xp->xp_context = EXPAND_EXPRESSION;
2596 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002597 else if (c == '#'
2598 && xp->xp_context == EXPAND_EXPRESSION)
2599 {
2600 /* Autoload function/variable contains '#'. */
2601 break;
2602 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002603 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 && xp->xp_context == EXPAND_FUNCTIONS
2605 && vim_strchr(xp->xp_pattern, '(') == NULL)
2606 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002607 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 break;
2609 }
2610 else if (cmdidx != CMD_let || got_eq)
2611 {
2612 if (c == '"') /* string */
2613 {
2614 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2615 if (c == '\\' && xp->xp_pattern[1] != NUL)
2616 ++xp->xp_pattern;
2617 xp->xp_context = EXPAND_NOTHING;
2618 }
2619 else if (c == '\'') /* literal string */
2620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2623 /* skip */ ;
2624 xp->xp_context = EXPAND_NOTHING;
2625 }
2626 else if (c == '|')
2627 {
2628 if (xp->xp_pattern[1] == '|')
2629 {
2630 ++xp->xp_pattern;
2631 xp->xp_context = EXPAND_EXPRESSION;
2632 }
2633 else
2634 xp->xp_context = EXPAND_COMMANDS;
2635 }
2636 else
2637 xp->xp_context = EXPAND_EXPRESSION;
2638 }
2639 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002640 /* Doesn't look like something valid, expand as an expression
2641 * anyway. */
2642 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 arg = xp->xp_pattern;
2644 if (*arg != NUL)
2645 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2646 /* skip */ ;
2647 }
2648 xp->xp_pattern = arg;
2649}
2650
2651#endif /* FEAT_CMDL_COMPL */
2652
2653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 * ":unlet[!] var1 ... " command.
2655 */
2656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002657ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002659 ex_unletlock(eap, eap->arg, 0);
2660}
2661
2662/*
2663 * ":lockvar" and ":unlockvar" commands
2664 */
2665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002666ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002669 int deep = 2;
2670
2671 if (eap->forceit)
2672 deep = -1;
2673 else if (vim_isdigit(*arg))
2674 {
2675 deep = getdigits(&arg);
2676 arg = skipwhite(arg);
2677 }
2678
2679 ex_unletlock(eap, arg, deep);
2680}
2681
2682/*
2683 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2684 */
2685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002686ex_unletlock(
2687 exarg_T *eap,
2688 char_u *argstart,
2689 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002690{
2691 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002694 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 do
2697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002699 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002700 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lv.ll_name == NULL)
2702 error = TRUE; /* error but continue parsing */
2703 if (name_end == NULL || (!vim_iswhite(*name_end)
2704 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (name_end != NULL)
2707 {
2708 emsg_severe = TRUE;
2709 EMSG(_(e_trailing));
2710 }
2711 if (!(eap->skip || error))
2712 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 break;
2714 }
2715
2716 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002717 {
2718 if (eap->cmdidx == CMD_unlet)
2719 {
2720 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2721 error = TRUE;
2722 }
2723 else
2724 {
2725 if (do_lock_var(&lv, name_end, deep,
2726 eap->cmdidx == CMD_lockvar) == FAIL)
2727 error = TRUE;
2728 }
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!eap->skip)
2732 clear_lval(&lv);
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 arg = skipwhite(name_end);
2735 } while (!ends_excmd(*arg));
2736
2737 eap->nextcmd = check_nextcmd(arg);
2738}
2739
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002741do_unlet_var(
2742 lval_T *lp,
2743 char_u *name_end,
2744 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 int ret = OK;
2747 int cc;
2748
2749 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 cc = *name_end;
2752 *name_end = NUL;
2753
2754 /* Normal name or expanded name. */
2755 if (check_changedtick(lp->ll_name))
2756 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002757 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002761 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002762 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002763 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02002764 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002765 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 else if (lp->ll_range)
2767 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002769 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01002770 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002771
2772 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
2773 {
2774 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02002775 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002776 return FAIL;
2777 ll_li = li;
2778 ++ll_n1;
2779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780
2781 /* Delete a range of List items. */
2782 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2783 {
2784 li = lp->ll_li->li_next;
2785 listitem_remove(lp->ll_list, lp->ll_li);
2786 lp->ll_li = li;
2787 ++lp->ll_n1;
2788 }
2789 }
2790 else
2791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 /* unlet a List item. */
2794 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002797 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 }
2799
2800 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801}
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803/*
2804 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002805 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
2807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002808do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
Bram Moolenaar33570922005-01-25 22:26:29 +00002810 hashtab_T *ht;
2811 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002812 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02002813 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002814 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 ht = find_var_ht(name, &varname);
2817 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002820 if (d == NULL)
2821 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 if (ht == &globvarht)
2823 d = &globvardict;
2824 else if (ht == &compat_hashtab)
2825 d = &vimvardict;
2826 else
2827 {
2828 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2829 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2830 }
2831 if (d == NULL)
2832 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002833 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834 return FAIL;
2835 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002836 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002837 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002838 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02002839 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002840 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002841 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00002842 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01002844 || var_check_ro(di->di_flags, name, FALSE)
2845 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01002846 return FAIL;
2847
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 delete_var(ht, hi);
2849 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002852 if (forceit)
2853 return OK;
2854 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 return FAIL;
2856}
2857
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002858/*
2859 * Lock or unlock variable indicated by "lp".
2860 * "deep" is the levels to go (-1 for unlimited);
2861 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2862 */
2863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002864do_lock_var(
2865 lval_T *lp,
2866 char_u *name_end,
2867 int deep,
2868 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869{
2870 int ret = OK;
2871 int cc;
2872 dictitem_T *di;
2873
2874 if (deep == 0) /* nothing to do */
2875 return OK;
2876
2877 if (lp->ll_tv == NULL)
2878 {
2879 cc = *name_end;
2880 *name_end = NUL;
2881
2882 /* Normal name or expanded name. */
2883 if (check_changedtick(lp->ll_name))
2884 ret = FAIL;
2885 else
2886 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002887 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002888 if (di == NULL)
2889 ret = FAIL;
2890 else
2891 {
2892 if (lock)
2893 di->di_flags |= DI_FLAGS_LOCK;
2894 else
2895 di->di_flags &= ~DI_FLAGS_LOCK;
2896 item_lock(&di->di_tv, deep, lock);
2897 }
2898 }
2899 *name_end = cc;
2900 }
2901 else if (lp->ll_range)
2902 {
2903 listitem_T *li = lp->ll_li;
2904
2905 /* (un)lock a range of List items. */
2906 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2907 {
2908 item_lock(&li->li_tv, deep, lock);
2909 li = li->li_next;
2910 ++lp->ll_n1;
2911 }
2912 }
2913 else if (lp->ll_list != NULL)
2914 /* (un)lock a List item. */
2915 item_lock(&lp->ll_li->li_tv, deep, lock);
2916 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02002917 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 item_lock(&lp->ll_di->di_tv, deep, lock);
2919
2920 return ret;
2921}
2922
2923/*
2924 * Lock or unlock an item. "deep" is nr of levels to go.
2925 */
2926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928{
2929 static int recurse = 0;
2930 list_T *l;
2931 listitem_T *li;
2932 dict_T *d;
2933 hashitem_T *hi;
2934 int todo;
2935
2936 if (recurse >= DICT_MAXNEST)
2937 {
2938 EMSG(_("E743: variable nested too deep for (un)lock"));
2939 return;
2940 }
2941 if (deep == 0)
2942 return;
2943 ++recurse;
2944
2945 /* lock/unlock the item itself */
2946 if (lock)
2947 tv->v_lock |= VAR_LOCKED;
2948 else
2949 tv->v_lock &= ~VAR_LOCKED;
2950
2951 switch (tv->v_type)
2952 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002953 case VAR_UNKNOWN:
2954 case VAR_NUMBER:
2955 case VAR_STRING:
2956 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002958 case VAR_FLOAT:
2959 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002960 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002961 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002962 break;
2963
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 case VAR_LIST:
2965 if ((l = tv->vval.v_list) != NULL)
2966 {
2967 if (lock)
2968 l->lv_lock |= VAR_LOCKED;
2969 else
2970 l->lv_lock &= ~VAR_LOCKED;
2971 if (deep < 0 || deep > 1)
2972 /* recursive: lock/unlock the items the List contains */
2973 for (li = l->lv_first; li != NULL; li = li->li_next)
2974 item_lock(&li->li_tv, deep - 1, lock);
2975 }
2976 break;
2977 case VAR_DICT:
2978 if ((d = tv->vval.v_dict) != NULL)
2979 {
2980 if (lock)
2981 d->dv_lock |= VAR_LOCKED;
2982 else
2983 d->dv_lock &= ~VAR_LOCKED;
2984 if (deep < 0 || deep > 1)
2985 {
2986 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002987 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2989 {
2990 if (!HASHITEM_EMPTY(hi))
2991 {
2992 --todo;
2993 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2994 }
2995 }
2996 }
2997 }
2998 }
2999 --recurse;
3000}
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3003/*
3004 * Delete all "menutrans_" variables.
3005 */
3006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003010 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003013 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003014 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003015 {
3016 if (!HASHITEM_EMPTY(hi))
3017 {
3018 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3020 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003021 }
3022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024}
3025#endif
3026
3027#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3028
3029/*
3030 * Local string buffer for the next two functions to store a variable name
3031 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3032 * get_user_var_name().
3033 */
3034
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003035static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037static char_u *varnamebuf = NULL;
3038static int varnamebuflen = 0;
3039
3040/*
3041 * Function to concatenate a prefix and a variable name.
3042 */
3043 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 int len;
3047
3048 len = (int)STRLEN(name) + 3;
3049 if (len > varnamebuflen)
3050 {
3051 vim_free(varnamebuf);
3052 len += 10; /* some additional space */
3053 varnamebuf = alloc(len);
3054 if (varnamebuf == NULL)
3055 {
3056 varnamebuflen = 0;
3057 return NULL;
3058 }
3059 varnamebuflen = len;
3060 }
3061 *varnamebuf = prefix;
3062 varnamebuf[1] = ':';
3063 STRCPY(varnamebuf + 2, name);
3064 return varnamebuf;
3065}
3066
3067/*
3068 * Function given to ExpandGeneric() to obtain the list of user defined
3069 * (global/buffer/window/built-in) variable names.
3070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003074 static long_u gdone;
3075 static long_u bdone;
3076 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003077#ifdef FEAT_WINDOWS
3078 static long_u tdone;
3079#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003080 static int vidx;
3081 static hashitem_T *hi;
3082 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003085 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003086 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003087#ifdef FEAT_WINDOWS
3088 tdone = 0;
3089#endif
3090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003091
3092 /* Global variables */
3093 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003095 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003097 else
3098 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003099 while (HASHITEM_EMPTY(hi))
3100 ++hi;
3101 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3102 return cat_prefix_varname('g', hi->hi_key);
3103 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003105
3106 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003107 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003110 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003112 else
3113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003114 while (HASHITEM_EMPTY(hi))
3115 ++hi;
3116 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003120 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 return (char_u *)"b:changedtick";
3122 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003123
3124 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003125 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003128 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003129 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003130 else
3131 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003132 while (HASHITEM_EMPTY(hi))
3133 ++hi;
3134 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003136
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003137#ifdef FEAT_WINDOWS
3138 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003139 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003140 if (tdone < ht->ht_used)
3141 {
3142 if (tdone++ == 0)
3143 hi = ht->ht_array;
3144 else
3145 ++hi;
3146 while (HASHITEM_EMPTY(hi))
3147 ++hi;
3148 return cat_prefix_varname('t', hi->hi_key);
3149 }
3150#endif
3151
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 /* v: variables */
3153 if (vidx < VV_LEN)
3154 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 vim_free(varnamebuf);
3157 varnamebuf = NULL;
3158 varnamebuflen = 0;
3159 return NULL;
3160}
3161
3162#endif /* FEAT_CMDL_COMPL */
3163
3164/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003165 * Return TRUE if "pat" matches "text".
3166 * Does not use 'cpo' and always uses 'magic'.
3167 */
3168 static int
3169pattern_match(char_u *pat, char_u *text, int ic)
3170{
3171 int matches = FALSE;
3172 char_u *save_cpo;
3173 regmatch_T regmatch;
3174
3175 /* avoid 'l' flag in 'cpoptions' */
3176 save_cpo = p_cpo;
3177 p_cpo = (char_u *)"";
3178 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3179 if (regmatch.regprog != NULL)
3180 {
3181 regmatch.rm_ic = ic;
3182 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3183 vim_regfree(regmatch.regprog);
3184 }
3185 p_cpo = save_cpo;
3186 return matches;
3187}
3188
3189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * types for expressions.
3191 */
3192typedef enum
3193{
3194 TYPE_UNKNOWN = 0
3195 , TYPE_EQUAL /* == */
3196 , TYPE_NEQUAL /* != */
3197 , TYPE_GREATER /* > */
3198 , TYPE_GEQUAL /* >= */
3199 , TYPE_SMALLER /* < */
3200 , TYPE_SEQUAL /* <= */
3201 , TYPE_MATCH /* =~ */
3202 , TYPE_NOMATCH /* !~ */
3203} exptype_T;
3204
3205/*
3206 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003207 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3209 */
3210
3211/*
3212 * Handle zero level expression.
3213 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003214 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003215 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Return OK or FAIL.
3217 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219eval0(
3220 char_u *arg,
3221 typval_T *rettv,
3222 char_u **nextcmd,
3223 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 int ret;
3226 char_u *p;
3227
3228 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (ret == FAIL || !ends_excmd(*p))
3231 {
3232 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /*
3235 * Report the invalid expression unless the expression evaluation has
3236 * been cancelled due to an aborting error, an interrupt, or an
3237 * exception.
3238 */
3239 if (!aborting())
3240 EMSG2(_(e_invexpr2), arg);
3241 ret = FAIL;
3242 }
3243 if (nextcmd != NULL)
3244 *nextcmd = check_nextcmd(p);
3245
3246 return ret;
3247}
3248
3249/*
3250 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003251 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 *
3253 * "arg" must point to the first non-white of the expression.
3254 * "arg" is advanced to the next non-white after the recognized expression.
3255 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003256 * Note: "rettv.v_lock" is not set.
3257 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 * Return OK or FAIL.
3259 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
3263 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 /*
3267 * Get the first variable.
3268 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 return FAIL;
3271
3272 if ((*arg)[0] == '?')
3273 {
3274 result = FALSE;
3275 if (evaluate)
3276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 int error = FALSE;
3278
3279 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003281 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003282 if (error)
3283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285
3286 /*
3287 * Get the second variable.
3288 */
3289 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003290 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292
3293 /*
3294 * Check for the ":".
3295 */
3296 if ((*arg)[0] != ':')
3297 {
3298 EMSG(_("E109: Missing ':' after '?'"));
3299 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 return FAIL;
3302 }
3303
3304 /*
3305 * Get the third variable.
3306 */
3307 *arg = skipwhite(*arg + 1);
3308 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3309 {
3310 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003311 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return FAIL;
3313 }
3314 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003315 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317
3318 return OK;
3319}
3320
3321/*
3322 * Handle first level expression:
3323 * expr2 || expr2 || expr2 logical OR
3324 *
3325 * "arg" must point to the first non-white of the expression.
3326 * "arg" is advanced to the next non-white after the recognized expression.
3327 *
3328 * Return OK or FAIL.
3329 */
3330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003331eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
Bram Moolenaar33570922005-01-25 22:26:29 +00003333 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 long result;
3335 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 /*
3339 * Get the first variable.
3340 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return FAIL;
3343
3344 /*
3345 * Repeat until there is no following "||".
3346 */
3347 first = TRUE;
3348 result = FALSE;
3349 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3350 {
3351 if (evaluate && first)
3352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 if (error)
3357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 first = FALSE;
3359 }
3360
3361 /*
3362 * Get the second variable.
3363 */
3364 *arg = skipwhite(*arg + 2);
3365 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3366 return FAIL;
3367
3368 /*
3369 * Compute the result.
3370 */
3371 if (evaluate && !result)
3372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003375 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376 if (error)
3377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 if (evaluate)
3380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 rettv->v_type = VAR_NUMBER;
3382 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 }
3385
3386 return OK;
3387}
3388
3389/*
3390 * Handle second level expression:
3391 * expr3 && expr3 && expr3 logical AND
3392 *
3393 * "arg" must point to the first non-white of the expression.
3394 * "arg" is advanced to the next non-white after the recognized expression.
3395 *
3396 * Return OK or FAIL.
3397 */
3398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaar33570922005-01-25 22:26:29 +00003401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 long result;
3403 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003404 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 /*
3407 * Get the first variable.
3408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 return FAIL;
3411
3412 /*
3413 * Repeat until there is no following "&&".
3414 */
3415 first = TRUE;
3416 result = TRUE;
3417 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3418 {
3419 if (evaluate && first)
3420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003421 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003423 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003424 if (error)
3425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 first = FALSE;
3427 }
3428
3429 /*
3430 * Get the second variable.
3431 */
3432 *arg = skipwhite(*arg + 2);
3433 if (eval4(arg, &var2, evaluate && result) == FAIL)
3434 return FAIL;
3435
3436 /*
3437 * Compute the result.
3438 */
3439 if (evaluate && result)
3440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003441 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003443 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003444 if (error)
3445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 if (evaluate)
3448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv->v_type = VAR_NUMBER;
3450 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452 }
3453
3454 return OK;
3455}
3456
3457/*
3458 * Handle third level expression:
3459 * var1 == var2
3460 * var1 =~ var2
3461 * var1 != var2
3462 * var1 !~ var2
3463 * var1 > var2
3464 * var1 >= var2
3465 * var1 < var2
3466 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003467 * var1 is var2
3468 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 *
3470 * "arg" must point to the first non-white of the expression.
3471 * "arg" is advanced to the next non-white after the recognized expression.
3472 *
3473 * Return OK or FAIL.
3474 */
3475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003476eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
Bram Moolenaar33570922005-01-25 22:26:29 +00003478 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u *p;
3480 int i;
3481 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003482 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003484 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 char_u *s1, *s2;
3486 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 /*
3490 * Get the first variable.
3491 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return FAIL;
3494
3495 p = *arg;
3496 switch (p[0])
3497 {
3498 case '=': if (p[1] == '=')
3499 type = TYPE_EQUAL;
3500 else if (p[1] == '~')
3501 type = TYPE_MATCH;
3502 break;
3503 case '!': if (p[1] == '=')
3504 type = TYPE_NEQUAL;
3505 else if (p[1] == '~')
3506 type = TYPE_NOMATCH;
3507 break;
3508 case '>': if (p[1] != '=')
3509 {
3510 type = TYPE_GREATER;
3511 len = 1;
3512 }
3513 else
3514 type = TYPE_GEQUAL;
3515 break;
3516 case '<': if (p[1] != '=')
3517 {
3518 type = TYPE_SMALLER;
3519 len = 1;
3520 }
3521 else
3522 type = TYPE_SEQUAL;
3523 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003524 case 'i': if (p[1] == 's')
3525 {
3526 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3527 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003528 i = p[len];
3529 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003530 {
3531 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3532 type_is = TRUE;
3533 }
3534 }
3535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537
3538 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 */
3541 if (type != TYPE_UNKNOWN)
3542 {
3543 /* extra question mark appended: ignore case */
3544 if (p[len] == '?')
3545 {
3546 ic = TRUE;
3547 ++len;
3548 }
3549 /* extra '#' appended: match case */
3550 else if (p[len] == '#')
3551 {
3552 ic = FALSE;
3553 ++len;
3554 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003555 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else
3557 ic = p_ic;
3558
3559 /*
3560 * Get the second variable.
3561 */
3562 *arg = skipwhite(p + len);
3563 if (eval5(arg, &var2, evaluate) == FAIL)
3564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 return FAIL;
3567 }
3568
3569 if (evaluate)
3570 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003571 if (type_is && rettv->v_type != var2.v_type)
3572 {
3573 /* For "is" a different type always means FALSE, for "notis"
3574 * it means TRUE. */
3575 n1 = (type == TYPE_NEQUAL);
3576 }
3577 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3578 {
3579 if (type_is)
3580 {
3581 n1 = (rettv->v_type == var2.v_type
3582 && rettv->vval.v_list == var2.vval.v_list);
3583 if (type == TYPE_NEQUAL)
3584 n1 = !n1;
3585 }
3586 else if (rettv->v_type != var2.v_type
3587 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3588 {
3589 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003590 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003591 else
Bram Moolenaar59838522014-05-13 13:46:33 +02003592 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003593 clear_tv(rettv);
3594 clear_tv(&var2);
3595 return FAIL;
3596 }
3597 else
3598 {
3599 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003600 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
3601 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003602 if (type == TYPE_NEQUAL)
3603 n1 = !n1;
3604 }
3605 }
3606
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003607 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3608 {
3609 if (type_is)
3610 {
3611 n1 = (rettv->v_type == var2.v_type
3612 && rettv->vval.v_dict == var2.vval.v_dict);
3613 if (type == TYPE_NEQUAL)
3614 n1 = !n1;
3615 }
3616 else if (rettv->v_type != var2.v_type
3617 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3618 {
3619 if (rettv->v_type != var2.v_type)
3620 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3621 else
3622 EMSG(_("E736: Invalid operation for Dictionary"));
3623 clear_tv(rettv);
3624 clear_tv(&var2);
3625 return FAIL;
3626 }
3627 else
3628 {
3629 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003630 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
3631 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003632 if (type == TYPE_NEQUAL)
3633 n1 = !n1;
3634 }
3635 }
3636
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003637 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
3638 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003639 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003640 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003641 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003642 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003643 clear_tv(rettv);
3644 clear_tv(&var2);
3645 return FAIL;
3646 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003647 if ((rettv->v_type == VAR_PARTIAL
3648 && rettv->vval.v_partial == NULL)
3649 || (var2.v_type == VAR_PARTIAL
3650 && var2.vval.v_partial == NULL))
3651 /* when a partial is NULL assume not equal */
3652 n1 = FALSE;
3653 else if (type_is)
3654 {
3655 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
3656 /* strings are considered the same if their value is
3657 * the same */
3658 n1 = tv_equal(rettv, &var2, ic, FALSE);
3659 else if (rettv->v_type == VAR_PARTIAL
3660 && var2.v_type == VAR_PARTIAL)
3661 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
3662 else
3663 n1 = FALSE;
3664 }
3665 else
3666 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003667 if (type == TYPE_NEQUAL)
3668 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003669 }
3670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003671#ifdef FEAT_FLOAT
3672 /*
3673 * If one of the two variables is a float, compare as a float.
3674 * When using "=~" or "!~", always compare as string.
3675 */
3676 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3677 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3678 {
3679 float_T f1, f2;
3680
3681 if (rettv->v_type == VAR_FLOAT)
3682 f1 = rettv->vval.v_float;
3683 else
3684 f1 = get_tv_number(rettv);
3685 if (var2.v_type == VAR_FLOAT)
3686 f2 = var2.vval.v_float;
3687 else
3688 f2 = get_tv_number(&var2);
3689 n1 = FALSE;
3690 switch (type)
3691 {
3692 case TYPE_EQUAL: n1 = (f1 == f2); break;
3693 case TYPE_NEQUAL: n1 = (f1 != f2); break;
3694 case TYPE_GREATER: n1 = (f1 > f2); break;
3695 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
3696 case TYPE_SMALLER: n1 = (f1 < f2); break;
3697 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
3698 case TYPE_UNKNOWN:
3699 case TYPE_MATCH:
3700 case TYPE_NOMATCH: break; /* avoid gcc warning */
3701 }
3702 }
3703#endif
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 /*
3706 * If one of the two variables is a number, compare as a number.
3707 * When using "=~" or "!~", always compare as string.
3708 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003709 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003712 n1 = get_tv_number(rettv);
3713 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 switch (type)
3715 {
3716 case TYPE_EQUAL: n1 = (n1 == n2); break;
3717 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3718 case TYPE_GREATER: n1 = (n1 > n2); break;
3719 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3720 case TYPE_SMALLER: n1 = (n1 < n2); break;
3721 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3722 case TYPE_UNKNOWN:
3723 case TYPE_MATCH:
3724 case TYPE_NOMATCH: break; /* avoid gcc warning */
3725 }
3726 }
3727 else
3728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 s1 = get_tv_string_buf(rettv, buf1);
3730 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3732 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3733 else
3734 i = 0;
3735 n1 = FALSE;
3736 switch (type)
3737 {
3738 case TYPE_EQUAL: n1 = (i == 0); break;
3739 case TYPE_NEQUAL: n1 = (i != 0); break;
3740 case TYPE_GREATER: n1 = (i > 0); break;
3741 case TYPE_GEQUAL: n1 = (i >= 0); break;
3742 case TYPE_SMALLER: n1 = (i < 0); break;
3743 case TYPE_SEQUAL: n1 = (i <= 0); break;
3744
3745 case TYPE_MATCH:
3746 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003747 n1 = pattern_match(s2, s1, ic);
3748 if (type == TYPE_NOMATCH)
3749 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 break;
3751
3752 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3753 }
3754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
3756 clear_tv(&var2);
3757 rettv->v_type = VAR_NUMBER;
3758 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 }
3761
3762 return OK;
3763}
3764
3765/*
3766 * Handle fourth level expression:
3767 * + number addition
3768 * - number subtraction
3769 * . string concatenation
3770 *
3771 * "arg" must point to the first non-white of the expression.
3772 * "arg" is advanced to the next non-white after the recognized expression.
3773 *
3774 * Return OK or FAIL.
3775 */
3776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 typval_T var2;
3780 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003782 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003783#ifdef FEAT_FLOAT
3784 float_T f1 = 0, f2 = 0;
3785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 char_u *s1, *s2;
3787 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3788 char_u *p;
3789
3790 /*
3791 * Get the first variable.
3792 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003793 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 /*
3797 * Repeat computing, until no '+', '-' or '.' is following.
3798 */
3799 for (;;)
3800 {
3801 op = **arg;
3802 if (op != '+' && op != '-' && op != '.')
3803 break;
3804
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003805 if ((op != '+' || rettv->v_type != VAR_LIST)
3806#ifdef FEAT_FLOAT
3807 && (op == '.' || rettv->v_type != VAR_FLOAT)
3808#endif
3809 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003810 {
3811 /* For "list + ...", an illegal use of the first operand as
3812 * a number cannot be determined before evaluating the 2nd
3813 * operand: if this is also a list, all is ok.
3814 * For "something . ...", "something - ..." or "non-list + ...",
3815 * we know that the first operand needs to be a string or number
3816 * without evaluating the 2nd operand. So check before to avoid
3817 * side effects after an error. */
3818 if (evaluate && get_tv_string_chk(rettv) == NULL)
3819 {
3820 clear_tv(rettv);
3821 return FAIL;
3822 }
3823 }
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 /*
3826 * Get the second variable.
3827 */
3828 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003829 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833 }
3834
3835 if (evaluate)
3836 {
3837 /*
3838 * Compute the result.
3839 */
3840 if (op == '.')
3841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3843 s2 = get_tv_string_buf_chk(&var2, buf2);
3844 if (s2 == NULL) /* type error ? */
3845 {
3846 clear_tv(rettv);
3847 clear_tv(&var2);
3848 return FAIL;
3849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003850 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 clear_tv(rettv);
3852 rettv->v_type = VAR_STRING;
3853 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003855 else if (op == '+' && rettv->v_type == VAR_LIST
3856 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003857 {
3858 /* concatenate Lists */
3859 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3860 &var3) == FAIL)
3861 {
3862 clear_tv(rettv);
3863 clear_tv(&var2);
3864 return FAIL;
3865 }
3866 clear_tv(rettv);
3867 *rettv = var3;
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
3870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 int error = FALSE;
3872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003873#ifdef FEAT_FLOAT
3874 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003876 f1 = rettv->vval.v_float;
3877 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003879 else
3880#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003881 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003882 n1 = get_tv_number_chk(rettv, &error);
3883 if (error)
3884 {
3885 /* This can only happen for "list + non-list". For
3886 * "non-list + ..." or "something - ...", we returned
3887 * before evaluating the 2nd operand. */
3888 clear_tv(rettv);
3889 return FAIL;
3890 }
3891#ifdef FEAT_FLOAT
3892 if (var2.v_type == VAR_FLOAT)
3893 f1 = n1;
3894#endif
3895 }
3896#ifdef FEAT_FLOAT
3897 if (var2.v_type == VAR_FLOAT)
3898 {
3899 f2 = var2.vval.v_float;
3900 n2 = 0;
3901 }
3902 else
3903#endif
3904 {
3905 n2 = get_tv_number_chk(&var2, &error);
3906 if (error)
3907 {
3908 clear_tv(rettv);
3909 clear_tv(&var2);
3910 return FAIL;
3911 }
3912#ifdef FEAT_FLOAT
3913 if (rettv->v_type == VAR_FLOAT)
3914 f2 = n2;
3915#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003917 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003918
3919#ifdef FEAT_FLOAT
3920 /* If there is a float on either side the result is a float. */
3921 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3922 {
3923 if (op == '+')
3924 f1 = f1 + f2;
3925 else
3926 f1 = f1 - f2;
3927 rettv->v_type = VAR_FLOAT;
3928 rettv->vval.v_float = f1;
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003931#endif
3932 {
3933 if (op == '+')
3934 n1 = n1 + n2;
3935 else
3936 n1 = n1 - n2;
3937 rettv->v_type = VAR_NUMBER;
3938 rettv->vval.v_number = n1;
3939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 }
3944 return OK;
3945}
3946
3947/*
3948 * Handle fifth level expression:
3949 * * number multiplication
3950 * / number division
3951 * % number modulo
3952 *
3953 * "arg" must point to the first non-white of the expression.
3954 * "arg" is advanced to the next non-white after the recognized expression.
3955 *
3956 * Return OK or FAIL.
3957 */
3958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959eval6(
3960 char_u **arg,
3961 typval_T *rettv,
3962 int evaluate,
3963 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003967 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003968#ifdef FEAT_FLOAT
3969 int use_float = FALSE;
3970 float_T f1 = 0, f2;
3971#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003972 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 /*
3975 * Get the first variable.
3976 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003977 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 return FAIL;
3979
3980 /*
3981 * Repeat computing, until no '*', '/' or '%' is following.
3982 */
3983 for (;;)
3984 {
3985 op = **arg;
3986 if (op != '*' && op != '/' && op != '%')
3987 break;
3988
3989 if (evaluate)
3990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003991#ifdef FEAT_FLOAT
3992 if (rettv->v_type == VAR_FLOAT)
3993 {
3994 f1 = rettv->vval.v_float;
3995 use_float = TRUE;
3996 n1 = 0;
3997 }
3998 else
3999#endif
4000 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004002 if (error)
4003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005 else
4006 n1 = 0;
4007
4008 /*
4009 * Get the second variable.
4010 */
4011 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004012 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return FAIL;
4014
4015 if (evaluate)
4016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004017#ifdef FEAT_FLOAT
4018 if (var2.v_type == VAR_FLOAT)
4019 {
4020 if (!use_float)
4021 {
4022 f1 = n1;
4023 use_float = TRUE;
4024 }
4025 f2 = var2.vval.v_float;
4026 n2 = 0;
4027 }
4028 else
4029#endif
4030 {
4031 n2 = get_tv_number_chk(&var2, &error);
4032 clear_tv(&var2);
4033 if (error)
4034 return FAIL;
4035#ifdef FEAT_FLOAT
4036 if (use_float)
4037 f2 = n2;
4038#endif
4039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 /*
4042 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004043 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004045#ifdef FEAT_FLOAT
4046 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004048 if (op == '*')
4049 f1 = f1 * f2;
4050 else if (op == '/')
4051 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004052# ifdef VMS
4053 /* VMS crashes on divide by zero, work around it */
4054 if (f2 == 0.0)
4055 {
4056 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004057 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004058 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004059 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004060 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004061 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004062 }
4063 else
4064 f1 = f1 / f2;
4065# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004066 /* We rely on the floating point library to handle divide
4067 * by zero to result in "inf" and not a crash. */
4068 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004069# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004072 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004073 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004074 return FAIL;
4075 }
4076 rettv->v_type = VAR_FLOAT;
4077 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004082 if (op == '*')
4083 n1 = n1 * n2;
4084 else if (op == '/')
4085 {
4086 if (n2 == 0) /* give an error message? */
4087 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004088#ifdef FEAT_NUM64
4089 if (n1 == 0)
Bram Moolenaaraf9c4c92016-08-17 21:51:56 +02004090 n1 = -0x7fffffffffffffffLL - 1; /* similar to NaN */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004091 else if (n1 < 0)
Bram Moolenaaraf9c4c92016-08-17 21:51:56 +02004092 n1 = -0x7fffffffffffffffLL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004093 else
Bram Moolenaaraf9c4c92016-08-17 21:51:56 +02004094 n1 = 0x7fffffffffffffffLL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004095#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004096 if (n1 == 0)
4097 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4098 else if (n1 < 0)
4099 n1 = -0x7fffffffL;
4100 else
4101 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004102#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004103 }
4104 else
4105 n1 = n1 / n2;
4106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004108 {
4109 if (n2 == 0) /* give an error message? */
4110 n1 = 0;
4111 else
4112 n1 = n1 % n2;
4113 }
4114 rettv->v_type = VAR_NUMBER;
4115 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 }
4119
4120 return OK;
4121}
4122
4123/*
4124 * Handle sixth level expression:
4125 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004126 * "string" string constant
4127 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * &option-name option value
4129 * @r register contents
4130 * identifier variable value
4131 * function() function call
4132 * $VAR environment variable
4133 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004134 * [expr, expr] List
4135 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 *
4137 * Also handle:
4138 * ! in front logical NOT
4139 * - in front unary minus
4140 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004141 * trailing [] subscript in String or List
4142 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 *
4144 * "arg" must point to the first non-white of the expression.
4145 * "arg" is advanced to the next non-white after the recognized expression.
4146 *
4147 * Return OK or FAIL.
4148 */
4149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004150eval7(
4151 char_u **arg,
4152 typval_T *rettv,
4153 int evaluate,
4154 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004156 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int len;
4158 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u *start_leader, *end_leader;
4160 int ret = OK;
4161 char_u *alias;
4162
4163 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004170 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
4172 start_leader = *arg;
4173 while (**arg == '!' || **arg == '-' || **arg == '+')
4174 *arg = skipwhite(*arg + 1);
4175 end_leader = *arg;
4176
4177 switch (**arg)
4178 {
4179 /*
4180 * Number constant.
4181 */
4182 case '0':
4183 case '1':
4184 case '2':
4185 case '3':
4186 case '4':
4187 case '5':
4188 case '6':
4189 case '7':
4190 case '8':
4191 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004192 {
4193#ifdef FEAT_FLOAT
4194 char_u *p = skipdigits(*arg + 1);
4195 int get_float = FALSE;
4196
4197 /* We accept a float when the format matches
4198 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004199 * strict to avoid backwards compatibility problems.
4200 * Don't look for a float after the "." operator, so that
4201 * ":let vers = 1.2.3" doesn't fail. */
4202 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004204 get_float = TRUE;
4205 p = skipdigits(p + 2);
4206 if (*p == 'e' || *p == 'E')
4207 {
4208 ++p;
4209 if (*p == '-' || *p == '+')
4210 ++p;
4211 if (!vim_isdigit(*p))
4212 get_float = FALSE;
4213 else
4214 p = skipdigits(p + 1);
4215 }
4216 if (ASCII_ISALPHA(*p) || *p == '.')
4217 get_float = FALSE;
4218 }
4219 if (get_float)
4220 {
4221 float_T f;
4222
4223 *arg += string2float(*arg, &f);
4224 if (evaluate)
4225 {
4226 rettv->v_type = VAR_FLOAT;
4227 rettv->vval.v_float = f;
4228 }
4229 }
4230 else
4231#endif
4232 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004233 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004234 *arg += len;
4235 if (evaluate)
4236 {
4237 rettv->v_type = VAR_NUMBER;
4238 rettv->vval.v_number = n;
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * String constant: "string".
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249
4250 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254 break;
4255
4256 /*
4257 * List: [expr, expr]
4258 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 break;
4261
4262 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004263 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004264 * Dictionary: {key: val, key: val}
4265 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004266 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4267 if (ret == NOTDONE)
4268 ret = get_dict_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004269 break;
4270
4271 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004272 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004274 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 break;
4276
4277 /*
4278 * Environment variable: $VAR.
4279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 break;
4282
4283 /*
4284 * Register contents: @r.
4285 */
4286 case '@': ++*arg;
4287 if (evaluate)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004290 rettv->vval.v_string = get_reg_contents(**arg,
4291 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (**arg != NUL)
4294 ++*arg;
4295 break;
4296
4297 /*
4298 * nested expression: (expression).
4299 */
4300 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (**arg == ')')
4303 ++*arg;
4304 else if (ret == OK)
4305 {
4306 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 ret = FAIL;
4309 }
4310 break;
4311
Bram Moolenaar8c711452005-01-14 21:53:12 +00004312 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 break;
4314 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315
4316 if (ret == NOTDONE)
4317 {
4318 /*
4319 * Must be a variable or function name.
4320 * Can also be a curly-braces kind of name: {expr}.
4321 */
4322 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004323 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324 if (alias != NULL)
4325 s = alias;
4326
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004327 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004328 ret = FAIL;
4329 else
4330 {
4331 if (**arg == '(') /* recursive! */
4332 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004333 partial_T *partial;
4334
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004335 if (!evaluate)
4336 check_vars(s, len);
4337
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338 /* If "s" is the name of a variable of type VAR_FUNC
4339 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004340 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004342 /* Need to make a copy, in case evaluating the arguments makes
4343 * the name invalid. */
4344 s = vim_strsave(s);
4345 if (s == NULL)
4346 ret = FAIL;
4347 else
4348 /* Invoke the function. */
4349 ret = get_func_tv(s, len, rettv, arg,
4350 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4351 &len, evaluate, partial, NULL);
4352 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004353
4354 /* If evaluate is FALSE rettv->v_type was not set in
4355 * get_func_tv, but it's needed in handle_subscript() to parse
4356 * what follows. So set it here. */
4357 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4358 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004359 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004360 rettv->v_type = VAR_FUNC;
4361 }
4362
Bram Moolenaar8c711452005-01-14 21:53:12 +00004363 /* Stop the expression evaluation when immediately
4364 * aborting on error, or when an interrupt occurred or
4365 * an exception was thrown but not caught. */
4366 if (aborting())
4367 {
4368 if (ret == OK)
4369 clear_tv(rettv);
4370 ret = FAIL;
4371 }
4372 }
4373 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004374 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004375 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004376 {
4377 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004378 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004381 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004382 }
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *arg = skipwhite(*arg);
4385
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004386 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4387 * expr(expr). */
4388 if (ret == OK)
4389 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /*
4392 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4393 */
4394 if (ret == OK && evaluate && end_leader > start_leader)
4395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004396 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004397 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004398#ifdef FEAT_FLOAT
4399 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004401 if (rettv->v_type == VAR_FLOAT)
4402 f = rettv->vval.v_float;
4403 else
4404#endif
4405 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004408 clear_tv(rettv);
4409 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 else
4412 {
4413 while (end_leader > start_leader)
4414 {
4415 --end_leader;
4416 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004417 {
4418#ifdef FEAT_FLOAT
4419 if (rettv->v_type == VAR_FLOAT)
4420 f = !f;
4421 else
4422#endif
4423 val = !val;
4424 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004426 {
4427#ifdef FEAT_FLOAT
4428 if (rettv->v_type == VAR_FLOAT)
4429 f = -f;
4430 else
4431#endif
4432 val = -val;
4433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004434 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004435#ifdef FEAT_FLOAT
4436 if (rettv->v_type == VAR_FLOAT)
4437 {
4438 clear_tv(rettv);
4439 rettv->vval.v_float = f;
4440 }
4441 else
4442#endif
4443 {
4444 clear_tv(rettv);
4445 rettv->v_type = VAR_NUMBER;
4446 rettv->vval.v_number = val;
4447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450
4451 return ret;
4452}
4453
4454/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004455 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4456 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4458 */
4459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004460eval_index(
4461 char_u **arg,
4462 typval_T *rettv,
4463 int evaluate,
4464 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465{
4466 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004467 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004468 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 long len = -1;
4470 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004471 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004472 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473
Bram Moolenaara03f2332016-02-06 18:09:59 +01004474 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004476 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004477 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004478 if (verbose)
4479 EMSG(_("E695: Cannot index a Funcref"));
4480 return FAIL;
4481 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004482#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004483 if (verbose)
4484 EMSG(_(e_float_as_string));
4485 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004486#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004487 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004488 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004489 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004490 if (verbose)
4491 EMSG(_("E909: Cannot index a special variable"));
4492 return FAIL;
4493 case VAR_UNKNOWN:
4494 if (evaluate)
4495 return FAIL;
4496 /* FALLTHROUGH */
4497
4498 case VAR_STRING:
4499 case VAR_NUMBER:
4500 case VAR_LIST:
4501 case VAR_DICT:
4502 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004505 init_tv(&var1);
4506 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 /*
4510 * dict.name
4511 */
4512 key = *arg + 1;
4513 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4514 ;
4515 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004518 }
4519 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 /*
4522 * something[idx]
4523 *
4524 * Get the (first) variable from inside the [].
4525 */
4526 *arg = skipwhite(*arg + 1);
4527 if (**arg == ':')
4528 empty1 = TRUE;
4529 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4530 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004531 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4532 {
4533 /* not a number or string */
4534 clear_tv(&var1);
4535 return FAIL;
4536 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537
4538 /*
4539 * Get the second variable from inside the [:].
4540 */
4541 if (**arg == ':')
4542 {
4543 range = TRUE;
4544 *arg = skipwhite(*arg + 1);
4545 if (**arg == ']')
4546 empty2 = TRUE;
4547 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004549 if (!empty1)
4550 clear_tv(&var1);
4551 return FAIL;
4552 }
4553 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4554 {
4555 /* not a number or string */
4556 if (!empty1)
4557 clear_tv(&var1);
4558 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 return FAIL;
4560 }
4561 }
4562
4563 /* Check for the ']'. */
4564 if (**arg != ']')
4565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004566 if (verbose)
4567 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 clear_tv(&var1);
4569 if (range)
4570 clear_tv(&var2);
4571 return FAIL;
4572 }
4573 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 }
4575
4576 if (evaluate)
4577 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004578 n1 = 0;
4579 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 n1 = get_tv_number(&var1);
4582 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
4584 if (range)
4585 {
4586 if (empty2)
4587 n2 = -1;
4588 else
4589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004590 n2 = get_tv_number(&var2);
4591 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 }
4593 }
4594
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004597 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004598 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004599 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004600 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004601 case VAR_SPECIAL:
4602 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004603 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004604 break; /* not evaluating, skipping over subscript */
4605
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004606 case VAR_NUMBER:
4607 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 len = (long)STRLEN(s);
4610 if (range)
4611 {
4612 /* The resulting variable is a substring. If the indexes
4613 * are out of range the result is empty. */
4614 if (n1 < 0)
4615 {
4616 n1 = len + n1;
4617 if (n1 < 0)
4618 n1 = 0;
4619 }
4620 if (n2 < 0)
4621 n2 = len + n2;
4622 else if (n2 >= len)
4623 n2 = len;
4624 if (n1 >= len || n2 < 0 || n1 > n2)
4625 s = NULL;
4626 else
4627 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4628 }
4629 else
4630 {
4631 /* The resulting variable is a string of a single
4632 * character. If the index is too big or negative the
4633 * result is empty. */
4634 if (n1 >= len || n1 < 0)
4635 s = NULL;
4636 else
4637 s = vim_strnsave(s + n1, 1);
4638 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004639 clear_tv(rettv);
4640 rettv->v_type = VAR_STRING;
4641 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 break;
4643
4644 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 if (n1 < 0)
4647 n1 = len + n1;
4648 if (!empty1 && (n1 < 0 || n1 >= len))
4649 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004650 /* For a range we allow invalid values and return an empty
4651 * list. A list index out of range is an error. */
4652 if (!range)
4653 {
4654 if (verbose)
4655 EMSGN(_(e_listidx), n1);
4656 return FAIL;
4657 }
4658 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659 }
4660 if (range)
4661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004662 list_T *l;
4663 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664
4665 if (n2 < 0)
4666 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004667 else if (n2 >= len)
4668 n2 = len - 1;
4669 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004670 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 l = list_alloc();
4672 if (l == NULL)
4673 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 n1 <= n2; ++n1)
4676 {
4677 if (list_append_tv(l, &item->li_tv) == FAIL)
4678 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004679 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 return FAIL;
4681 }
4682 item = item->li_next;
4683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 clear_tv(rettv);
4685 rettv->v_type = VAR_LIST;
4686 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004687 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 }
4689 else
4690 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004691 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 clear_tv(rettv);
4693 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 }
4695 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696
4697 case VAR_DICT:
4698 if (range)
4699 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004700 if (verbose)
4701 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 if (len == -1)
4703 clear_tv(&var1);
4704 return FAIL;
4705 }
4706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004707 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708
4709 if (len == -1)
4710 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004711 key = get_tv_string_chk(&var1);
4712 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 clear_tv(&var1);
4715 return FAIL;
4716 }
4717 }
4718
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004719 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004720
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004721 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004722 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004723 if (len == -1)
4724 clear_tv(&var1);
4725 if (item == NULL)
4726 return FAIL;
4727
4728 copy_tv(&item->di_tv, &var1);
4729 clear_tv(rettv);
4730 *rettv = var1;
4731 }
4732 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 }
4734 }
4735
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 return OK;
4737}
4738
4739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 * Get an option value.
4741 * "arg" points to the '&' or '+' before the option name.
4742 * "arg" is advanced to character after the option name.
4743 * Return OK or FAIL.
4744 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004746get_option_tv(
4747 char_u **arg,
4748 typval_T *rettv, /* when NULL, only check if option exists */
4749 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751 char_u *option_end;
4752 long numval;
4753 char_u *stringval;
4754 int opt_type;
4755 int c;
4756 int working = (**arg == '+'); /* has("+option") */
4757 int ret = OK;
4758 int opt_flags;
4759
4760 /*
4761 * Isolate the option name and find its value.
4762 */
4763 option_end = find_option_end(arg, &opt_flags);
4764 if (option_end == NULL)
4765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 EMSG2(_("E112: Option name missing: %s"), *arg);
4768 return FAIL;
4769 }
4770
4771 if (!evaluate)
4772 {
4773 *arg = option_end;
4774 return OK;
4775 }
4776
4777 c = *option_end;
4778 *option_end = NUL;
4779 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 if (opt_type == -3) /* invalid name */
4783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004784 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 EMSG2(_("E113: Unknown option: %s"), *arg);
4786 ret = FAIL;
4787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 if (opt_type == -2) /* hidden string option */
4791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004792 rettv->v_type = VAR_STRING;
4793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else if (opt_type == -1) /* hidden number option */
4796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 rettv->v_type = VAR_NUMBER;
4798 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
4800 else if (opt_type == 1) /* number option */
4801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 rettv->v_type = VAR_NUMBER;
4803 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 else /* string option */
4806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004807 rettv->v_type = VAR_STRING;
4808 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 }
4811 else if (working && (opt_type == -2 || opt_type == -1))
4812 ret = FAIL;
4813
4814 *option_end = c; /* put back for error messages */
4815 *arg = option_end;
4816
4817 return ret;
4818}
4819
4820/*
4821 * Allocate a variable for a string constant.
4822 * Return OK or FAIL.
4823 */
4824 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004825get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 char_u *p;
4828 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 int extra = 0;
4830
4831 /*
4832 * Find the end of the string, skipping backslashed characters.
4833 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004834 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 if (*p == '\\' && p[1] != NUL)
4837 {
4838 ++p;
4839 /* A "\<x>" form occupies at least 4 characters, and produces up
4840 * to 6 characters: reserve space for 2 extra */
4841 if (*p == '<')
4842 extra += 2;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845
4846 if (*p != '"')
4847 {
4848 EMSG2(_("E114: Missing quote: %s"), *arg);
4849 return FAIL;
4850 }
4851
4852 /* If only parsing, set *arg and return here */
4853 if (!evaluate)
4854 {
4855 *arg = p + 1;
4856 return OK;
4857 }
4858
4859 /*
4860 * Copy the string into allocated memory, handling backslashed
4861 * characters.
4862 */
4863 name = alloc((unsigned)(p - *arg + extra));
4864 if (name == NULL)
4865 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004866 rettv->v_type = VAR_STRING;
4867 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
4871 if (*p == '\\')
4872 {
4873 switch (*++p)
4874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004875 case 'b': *name++ = BS; ++p; break;
4876 case 'e': *name++ = ESC; ++p; break;
4877 case 'f': *name++ = FF; ++p; break;
4878 case 'n': *name++ = NL; ++p; break;
4879 case 'r': *name++ = CAR; ++p; break;
4880 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 case 'X': /* hex: "\x1", "\x12" */
4883 case 'x':
4884 case 'u': /* Unicode: "\u0023" */
4885 case 'U':
4886 if (vim_isxdigit(p[1]))
4887 {
4888 int n, nr;
4889 int c = toupper(*p);
4890
4891 if (c == 'X')
4892 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004893 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02004895 else
4896 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 nr = 0;
4898 while (--n >= 0 && vim_isxdigit(p[1]))
4899 {
4900 ++p;
4901 nr = (nr << 4) + hex2nr(*p);
4902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#ifdef FEAT_MBYTE
4905 /* For "\u" store the number according to
4906 * 'encoding'. */
4907 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 else
4910#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004911 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 break;
4914
4915 /* octal: "\1", "\12", "\123" */
4916 case '0':
4917 case '1':
4918 case '2':
4919 case '3':
4920 case '4':
4921 case '5':
4922 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 case '7': *name = *p++ - '0';
4924 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004926 *name = (*name << 3) + *p++ - '0';
4927 if (*p >= '0' && *p <= '7')
4928 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 break;
4932
4933 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02004934 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (extra != 0)
4936 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 break;
4939 }
4940 /* FALLTHROUGH */
4941
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 break;
4944 }
4945 }
4946 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02004951 if (*p != NUL) /* just in case */
4952 ++p;
4953 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 return OK;
4956}
4957
4958/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 * Return OK or FAIL.
4961 */
4962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004963get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004966 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004967 int reduce = 0;
4968
4969 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004971 */
4972 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004976 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004977 break;
4978 ++reduce;
4979 ++p;
4980 }
4981 }
4982
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004986 return FAIL;
4987 }
4988
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004990 if (!evaluate)
4991 {
4992 *arg = p + 1;
4993 return OK;
4994 }
4995
4996 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004998 */
4999 str = alloc((unsigned)((p - *arg) - reduce));
5000 if (str == NULL)
5001 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 rettv->v_type = VAR_STRING;
5003 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005004
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005006 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010 break;
5011 ++p;
5012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005014 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005016 *arg = p + 1;
5017
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005018 return OK;
5019}
5020
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005021/*
5022 * Return the function name of the partial.
5023 */
5024 char_u *
5025partial_name(partial_T *pt)
5026{
5027 if (pt->pt_name != NULL)
5028 return pt->pt_name;
5029 return pt->pt_func->uf_name;
5030}
5031
Bram Moolenaarddecc252016-04-06 22:59:37 +02005032 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005033partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005034{
5035 int i;
5036
5037 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005038 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005039 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005040 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005041 if (pt->pt_name != NULL)
5042 {
5043 func_unref(pt->pt_name);
5044 vim_free(pt->pt_name);
5045 }
5046 else
5047 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005048 vim_free(pt);
5049}
5050
5051/*
5052 * Unreference a closure: decrement the reference count and free it when it
5053 * becomes zero.
5054 */
5055 void
5056partial_unref(partial_T *pt)
5057{
5058 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005059 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005060}
5061
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005062static int tv_equal_recurse_limit;
5063
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005064 static int
5065func_equal(
5066 typval_T *tv1,
5067 typval_T *tv2,
5068 int ic) /* ignore case */
5069{
5070 char_u *s1, *s2;
5071 dict_T *d1, *d2;
5072 int a1, a2;
5073 int i;
5074
5075 /* empty and NULL function name considered the same */
5076 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005077 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005078 if (s1 != NULL && *s1 == NUL)
5079 s1 = NULL;
5080 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005081 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005082 if (s2 != NULL && *s2 == NUL)
5083 s2 = NULL;
5084 if (s1 == NULL || s2 == NULL)
5085 {
5086 if (s1 != s2)
5087 return FALSE;
5088 }
5089 else if (STRCMP(s1, s2) != 0)
5090 return FALSE;
5091
5092 /* empty dict and NULL dict is different */
5093 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5094 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5095 if (d1 == NULL || d2 == NULL)
5096 {
5097 if (d1 != d2)
5098 return FALSE;
5099 }
5100 else if (!dict_equal(d1, d2, ic, TRUE))
5101 return FALSE;
5102
5103 /* empty list and no list considered the same */
5104 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5105 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5106 if (a1 != a2)
5107 return FALSE;
5108 for (i = 0; i < a1; ++i)
5109 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5110 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5111 return FALSE;
5112
5113 return TRUE;
5114}
5115
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005116/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005117 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005118 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005120 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005122tv_equal(
5123 typval_T *tv1,
5124 typval_T *tv2,
5125 int ic, /* ignore case */
5126 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005127{
5128 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005129 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005130 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005131 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005132
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005133 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005134 * recursiveness to a limit. We guess they are equal then.
5135 * A fixed limit has the problem of still taking an awful long time.
5136 * Reduce the limit every time running into it. That should work fine for
5137 * deeply linked structures that are not recursively linked and catch
5138 * recursiveness quickly. */
5139 if (!recursive)
5140 tv_equal_recurse_limit = 1000;
5141 if (recursive_cnt >= tv_equal_recurse_limit)
5142 {
5143 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005144 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005145 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005146
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005147 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5148 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005149 if ((tv1->v_type == VAR_FUNC
5150 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5151 && (tv2->v_type == VAR_FUNC
5152 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5153 {
5154 ++recursive_cnt;
5155 r = func_equal(tv1, tv2, ic);
5156 --recursive_cnt;
5157 return r;
5158 }
5159
5160 if (tv1->v_type != tv2->v_type)
5161 return FALSE;
5162
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005163 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005164 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005165 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005166 ++recursive_cnt;
5167 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5168 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005169 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005170
5171 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005172 ++recursive_cnt;
5173 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5174 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005175 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005176
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005177 case VAR_NUMBER:
5178 return tv1->vval.v_number == tv2->vval.v_number;
5179
5180 case VAR_STRING:
5181 s1 = get_tv_string_buf(tv1, buf1);
5182 s2 = get_tv_string_buf(tv2, buf2);
5183 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005184
5185 case VAR_SPECIAL:
5186 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005187
5188 case VAR_FLOAT:
5189#ifdef FEAT_FLOAT
5190 return tv1->vval.v_float == tv2->vval.v_float;
5191#endif
5192 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005193#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005194 return tv1->vval.v_job == tv2->vval.v_job;
5195#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005196 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005197#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005198 return tv1->vval.v_channel == tv2->vval.v_channel;
5199#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005200 case VAR_FUNC:
5201 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005202 case VAR_UNKNOWN:
5203 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005204 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005205
Bram Moolenaara03f2332016-02-06 18:09:59 +01005206 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5207 * does not equal anything, not even itself. */
5208 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005209}
5210
5211/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005212 * Return the next (unique) copy ID.
5213 * Used for serializing nested structures.
5214 */
5215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005216get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005217{
5218 current_copyID += COPYID_INC;
5219 return current_copyID;
5220}
5221
5222/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223 * Garbage collection for lists and dictionaries.
5224 *
5225 * We use reference counts to be able to free most items right away when they
5226 * are no longer used. But for composite items it's possible that it becomes
5227 * unused while the reference count is > 0: When there is a recursive
5228 * reference. Example:
5229 * :let l = [1, 2, 3]
5230 * :let d = {9: l}
5231 * :let l[1] = d
5232 *
5233 * Since this is quite unusual we handle this with garbage collection: every
5234 * once in a while find out which lists and dicts are not referenced from any
5235 * variable.
5236 *
5237 * Here is a good reference text about garbage collection (refers to Python
5238 * but it applies to all reference-counting mechanisms):
5239 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005240 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005241
5242/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005243 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005244 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005246 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005248garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005249{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005250 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005251 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005252 buf_T *buf;
5253 win_T *wp;
5254 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005255 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005256#ifdef FEAT_WINDOWS
5257 tabpage_T *tp;
5258#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005259
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005260 if (!testing)
5261 {
5262 /* Only do this once. */
5263 want_garbage_collect = FALSE;
5264 may_garbage_collect = FALSE;
5265 garbage_collect_at_exit = FALSE;
5266 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005267
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005268 /* We advance by two because we add one for items referenced through
5269 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005270 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005271
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272 /*
5273 * 1. Go through all accessible variables and mark all lists and dicts
5274 * with copyID.
5275 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005276
5277 /* Don't free variables in the previous_funccal list unless they are only
5278 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005279 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005281
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005282 /* script-local variables */
5283 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005285
5286 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005287 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005288 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5289 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005290
5291 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005292 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5294 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005295#ifdef FEAT_AUTOCMD
5296 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005297 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5298 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005299#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005300
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301#ifdef FEAT_WINDOWS
5302 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005303 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5305 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005306#endif
5307
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005308 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005309 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005310
5311 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005313
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005314 /* named functions (matters for closures) */
5315 abort = abort || set_ref_in_functions(copyID);
5316
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005317 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005319
Bram Moolenaard812df62008-11-09 12:46:09 +00005320 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005321 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005322
Bram Moolenaar1dced572012-04-05 16:54:08 +02005323#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005324 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005325#endif
5326
Bram Moolenaardb913952012-06-29 12:54:53 +02005327#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005328 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005329#endif
5330
5331#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005332 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005333#endif
5334
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005336 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005337 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005338#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005339#ifdef FEAT_NETBEANS_INTG
5340 abort = abort || set_ref_in_nb_channel(copyID);
5341#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005342
Bram Moolenaare3188e22016-05-31 21:13:04 +02005343#ifdef FEAT_TIMERS
5344 abort = abort || set_ref_in_timer(copyID);
5345#endif
5346
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005347 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005348 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005349 /*
5350 * 2. Free lists and dictionaries that are not referenced.
5351 */
5352 did_free = free_unref_items(copyID);
5353
5354 /*
5355 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005356 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005359 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005360 else if (p_verbose > 0)
5361 {
5362 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
5363 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005364
5365 return did_free;
5366}
5367
5368/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005370 */
5371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005372free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005373{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005374 int did_free = FALSE;
5375
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 /* Let all "free" functions know that we are here. This means no
5377 * dictionaries, lists, channels or jobs are to be freed, because we will
5378 * do that here. */
5379 in_free_unref_items = TRUE;
5380
5381 /*
5382 * PASS 1: free the contents of the items. We don't free the items
5383 * themselves yet, so that it is possible to decrement refcount counters
5384 */
5385
Bram Moolenaarcd524592016-07-17 14:57:05 +02005386 /* Go through the list of dicts and free items without the copyID. */
5387 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005388
Bram Moolenaarda861d62016-07-17 15:46:27 +02005389 /* Go through the list of lists and free items without the copyID. */
5390 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005391
5392#ifdef FEAT_JOB_CHANNEL
5393 /* Go through the list of jobs and free items without the copyID. This
5394 * must happen before doing channels, because jobs refer to channels, but
5395 * the reference from the channel to the job isn't tracked. */
5396 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5397
5398 /* Go through the list of channels and free items without the copyID. */
5399 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5400#endif
5401
5402 /*
5403 * PASS 2: free the items themselves.
5404 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005405 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005406 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005407
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408#ifdef FEAT_JOB_CHANNEL
5409 /* Go through the list of jobs and free items without the copyID. This
5410 * must happen before doing channels, because jobs refer to channels, but
5411 * the reference from the channel to the job isn't tracked. */
5412 free_unused_jobs(copyID, COPYID_MASK);
5413
5414 /* Go through the list of channels and free items without the copyID. */
5415 free_unused_channels(copyID, COPYID_MASK);
5416#endif
5417
5418 in_free_unref_items = FALSE;
5419
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005420 return did_free;
5421}
5422
5423/*
5424 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425 * "list_stack" is used to add lists to be marked. Can be NULL.
5426 *
5427 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005428 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005430set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005431{
5432 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005434 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005435 hashtab_T *cur_ht;
5436 ht_stack_T *ht_stack = NULL;
5437 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005438
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005439 cur_ht = ht;
5440 for (;;)
5441 {
5442 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005443 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005444 /* Mark each item in the hashtab. If the item contains a hashtab
5445 * it is added to ht_stack, if it contains a list it is added to
5446 * list_stack. */
5447 todo = (int)cur_ht->ht_used;
5448 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5449 if (!HASHITEM_EMPTY(hi))
5450 {
5451 --todo;
5452 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5453 &ht_stack, list_stack);
5454 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005455 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005456
5457 if (ht_stack == NULL)
5458 break;
5459
5460 /* take an item from the stack */
5461 cur_ht = ht_stack->ht;
5462 tempitem = ht_stack;
5463 ht_stack = ht_stack->prev;
5464 free(tempitem);
5465 }
5466
5467 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005468}
5469
5470/*
5471 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 * "ht_stack" is used to add hashtabs 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_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005479 listitem_T *li;
5480 int abort = FALSE;
5481 list_T *cur_l;
5482 list_stack_T *list_stack = NULL;
5483 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005484
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005485 cur_l = l;
5486 for (;;)
5487 {
5488 if (!abort)
5489 /* Mark each item in the list. If the item contains a hashtab
5490 * it is added to ht_stack, if it contains a list it is added to
5491 * list_stack. */
5492 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5493 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5494 ht_stack, &list_stack);
5495 if (list_stack == NULL)
5496 break;
5497
5498 /* take an item from the stack */
5499 cur_l = list_stack->list;
5500 tempitem = list_stack;
5501 list_stack = list_stack->prev;
5502 free(tempitem);
5503 }
5504
5505 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005506}
5507
5508/*
5509 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005510 * "list_stack" is used to add lists to be marked. Can be NULL.
5511 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5512 *
5513 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005514 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005516set_ref_in_item(
5517 typval_T *tv,
5518 int copyID,
5519 ht_stack_T **ht_stack,
5520 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005521{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005522 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005523
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005524 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005525 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005526 dict_T *dd = tv->vval.v_dict;
5527
Bram Moolenaara03f2332016-02-06 18:09:59 +01005528 if (dd != NULL && dd->dv_copyID != copyID)
5529 {
5530 /* Didn't see this dict yet. */
5531 dd->dv_copyID = copyID;
5532 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005533 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005534 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5535 }
5536 else
5537 {
5538 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5539 if (newitem == NULL)
5540 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005541 else
5542 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005543 newitem->ht = &dd->dv_hashtab;
5544 newitem->prev = *ht_stack;
5545 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005546 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005547 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005548 }
5549 }
5550 else if (tv->v_type == VAR_LIST)
5551 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005552 list_T *ll = tv->vval.v_list;
5553
Bram Moolenaara03f2332016-02-06 18:09:59 +01005554 if (ll != NULL && ll->lv_copyID != copyID)
5555 {
5556 /* Didn't see this list yet. */
5557 ll->lv_copyID = copyID;
5558 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005559 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005560 abort = set_ref_in_list(ll, copyID, ht_stack);
5561 }
5562 else
5563 {
5564 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005565 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005566 if (newitem == NULL)
5567 abort = TRUE;
5568 else
5569 {
5570 newitem->list = ll;
5571 newitem->prev = *list_stack;
5572 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005573 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005574 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005575 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005576 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005577 else if (tv->v_type == VAR_FUNC)
5578 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005579 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005580 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005581 else if (tv->v_type == VAR_PARTIAL)
5582 {
5583 partial_T *pt = tv->vval.v_partial;
5584 int i;
5585
5586 /* A partial does not have a copyID, because it cannot contain itself.
5587 */
5588 if (pt != NULL)
5589 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005590 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005591
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005592 if (pt->pt_dict != NULL)
5593 {
5594 typval_T dtv;
5595
5596 dtv.v_type = VAR_DICT;
5597 dtv.vval.v_dict = pt->pt_dict;
5598 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5599 }
5600
5601 for (i = 0; i < pt->pt_argc; ++i)
5602 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5603 ht_stack, list_stack);
5604 }
5605 }
5606#ifdef FEAT_JOB_CHANNEL
5607 else if (tv->v_type == VAR_JOB)
5608 {
5609 job_T *job = tv->vval.v_job;
5610 typval_T dtv;
5611
5612 if (job != NULL && job->jv_copyID != copyID)
5613 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005614 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005615 if (job->jv_channel != NULL)
5616 {
5617 dtv.v_type = VAR_CHANNEL;
5618 dtv.vval.v_channel = job->jv_channel;
5619 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5620 }
5621 if (job->jv_exit_partial != NULL)
5622 {
5623 dtv.v_type = VAR_PARTIAL;
5624 dtv.vval.v_partial = job->jv_exit_partial;
5625 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5626 }
5627 }
5628 }
5629 else if (tv->v_type == VAR_CHANNEL)
5630 {
5631 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005632 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005633 typval_T dtv;
5634 jsonq_T *jq;
5635 cbq_T *cq;
5636
5637 if (ch != NULL && ch->ch_copyID != copyID)
5638 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005639 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005640 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005641 {
5642 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5643 jq = jq->jq_next)
5644 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5645 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5646 cq = cq->cq_next)
5647 if (cq->cq_partial != NULL)
5648 {
5649 dtv.v_type = VAR_PARTIAL;
5650 dtv.vval.v_partial = cq->cq_partial;
5651 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5652 }
5653 if (ch->ch_part[part].ch_partial != NULL)
5654 {
5655 dtv.v_type = VAR_PARTIAL;
5656 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5657 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5658 }
5659 }
5660 if (ch->ch_partial != NULL)
5661 {
5662 dtv.v_type = VAR_PARTIAL;
5663 dtv.vval.v_partial = ch->ch_partial;
5664 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5665 }
5666 if (ch->ch_close_partial != NULL)
5667 {
5668 dtv.v_type = VAR_PARTIAL;
5669 dtv.vval.v_partial = ch->ch_close_partial;
5670 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5671 }
5672 }
5673 }
5674#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005675 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005676}
5677
Bram Moolenaar17a13432016-01-24 14:22:10 +01005678 static char *
5679get_var_special_name(int nr)
5680{
5681 switch (nr)
5682 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005683 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005684 case VVAL_TRUE: return "v:true";
5685 case VVAL_NONE: return "v:none";
5686 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005687 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005688 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005689 return "42";
5690}
5691
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 * Return a string with the string representation of a variable.
5694 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005695 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005696 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005697 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
5698 * "string()", otherwise does not put quotes around strings, as ":echo"
5699 * displays values.
5700 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5701 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005702 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005704 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005705echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706 typval_T *tv,
5707 char_u **tofree,
5708 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005709 int copyID,
5710 int echo_style,
5711 int restore_copyID,
5712 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005714 static int recurse = 0;
5715 char_u *r = NULL;
5716
Bram Moolenaar33570922005-01-25 22:26:29 +00005717 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005719 if (!did_echo_string_emsg)
5720 {
5721 /* Only give this message once for a recursive call to avoid
5722 * flooding the user with errors. And stop iterating over lists
5723 * and dicts. */
5724 did_echo_string_emsg = TRUE;
5725 EMSG(_("E724: variable nested too deep for displaying"));
5726 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005727 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005728 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005729 }
5730 ++recurse;
5731
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 switch (tv->v_type)
5733 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005734 case VAR_STRING:
5735 if (echo_style && !dict_val)
5736 {
5737 *tofree = NULL;
5738 r = get_tv_string_buf(tv, numbuf);
5739 }
5740 else
5741 {
5742 *tofree = string_quote(tv->vval.v_string, FALSE);
5743 r = *tofree;
5744 }
5745 break;
5746
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005748 if (echo_style)
5749 {
5750 *tofree = NULL;
5751 r = tv->vval.v_string;
5752 }
5753 else
5754 {
5755 *tofree = string_quote(tv->vval.v_string, TRUE);
5756 r = *tofree;
5757 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005758 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005759
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005760 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005761 {
5762 partial_T *pt = tv->vval.v_partial;
5763 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005764 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005765 garray_T ga;
5766 int i;
5767 char_u *tf;
5768
5769 ga_init2(&ga, 1, 100);
5770 ga_concat(&ga, (char_u *)"function(");
5771 if (fname != NULL)
5772 {
5773 ga_concat(&ga, fname);
5774 vim_free(fname);
5775 }
5776 if (pt != NULL && pt->pt_argc > 0)
5777 {
5778 ga_concat(&ga, (char_u *)", [");
5779 for (i = 0; i < pt->pt_argc; ++i)
5780 {
5781 if (i > 0)
5782 ga_concat(&ga, (char_u *)", ");
5783 ga_concat(&ga,
5784 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5785 vim_free(tf);
5786 }
5787 ga_concat(&ga, (char_u *)"]");
5788 }
5789 if (pt != NULL && pt->pt_dict != NULL)
5790 {
5791 typval_T dtv;
5792
5793 ga_concat(&ga, (char_u *)", ");
5794 dtv.v_type = VAR_DICT;
5795 dtv.vval.v_dict = pt->pt_dict;
5796 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5797 vim_free(tf);
5798 }
5799 ga_concat(&ga, (char_u *)")");
5800
5801 *tofree = ga.ga_data;
5802 r = *tofree;
5803 break;
5804 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005807 if (tv->vval.v_list == NULL)
5808 {
5809 *tofree = NULL;
5810 r = NULL;
5811 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005812 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5813 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005814 {
5815 *tofree = NULL;
5816 r = (char_u *)"[...]";
5817 }
5818 else
5819 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005820 int old_copyID = tv->vval.v_list->lv_copyID;
5821
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005822 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005823 *tofree = list2string(tv, copyID, restore_copyID);
5824 if (restore_copyID)
5825 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005826 r = *tofree;
5827 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005828 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005831 if (tv->vval.v_dict == NULL)
5832 {
5833 *tofree = NULL;
5834 r = NULL;
5835 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005836 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5837 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005838 {
5839 *tofree = NULL;
5840 r = (char_u *)"{...}";
5841 }
5842 else
5843 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005844 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005845 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005846 *tofree = dict2string(tv, copyID, restore_copyID);
5847 if (restore_copyID)
5848 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005849 r = *tofree;
5850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005852
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005853 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005854 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005855 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005856 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 *tofree = NULL;
5858 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005861 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005862#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005863 *tofree = NULL;
5864 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5865 r = numbuf;
5866 break;
5867#endif
5868
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005869 case VAR_SPECIAL:
5870 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005871 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005872 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874
Bram Moolenaar8502c702014-06-17 12:51:16 +02005875 if (--recurse == 0)
5876 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878}
5879
5880/*
5881 * Return a string with the string representation of a variable.
5882 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5883 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005884 * Does not put quotes around strings, as ":echo" displays values.
5885 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5886 * May return NULL.
5887 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005888 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005889echo_string(
5890 typval_T *tv,
5891 char_u **tofree,
5892 char_u *numbuf,
5893 int copyID)
5894{
5895 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5896}
5897
5898/*
5899 * Return a string with the string representation of a variable.
5900 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5901 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005903 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02005905 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005906tv2string(
5907 typval_T *tv,
5908 char_u **tofree,
5909 char_u *numbuf,
5910 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005912 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913}
5914
5915/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 * Return string "str" in ' quotes, doubling ' characters.
5917 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005920 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922{
Bram Moolenaar33570922005-01-25 22:26:29 +00005923 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 char_u *p, *r, *s;
5925
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 len = (function ? 13 : 3);
5927 if (str != NULL)
5928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005929 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 for (p = str; *p != NUL; mb_ptr_adv(p))
5931 if (*p == '\'')
5932 ++len;
5933 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 s = r = alloc(len);
5935 if (r != NULL)
5936 {
5937 if (function)
5938 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 r += 10;
5941 }
5942 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 if (str != NULL)
5945 for (p = str; *p != NUL; )
5946 {
5947 if (*p == '\'')
5948 *r++ = '\'';
5949 MB_COPY_CHAR(p, r);
5950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 if (function)
5953 *r++ = ')';
5954 *r++ = NUL;
5955 }
5956 return s;
5957}
5958
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005959#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005960/*
5961 * Convert the string "text" to a floating point number.
5962 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5963 * this always uses a decimal point.
5964 * Returns the length of the text that was consumed.
5965 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967string2float(
5968 char_u *text,
5969 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970{
5971 char *s = (char *)text;
5972 float_T f;
5973
Bram Moolenaar62473612017-01-08 19:25:40 +01005974 /* MS-Windows does not deal with "inf" and "nan" properly. */
5975 if (STRNICMP(text, "inf", 3) == 0)
5976 {
5977 *value = INFINITY;
5978 return 3;
5979 }
5980 if (STRNICMP(text, "-inf", 3) == 0)
5981 {
5982 *value = -INFINITY;
5983 return 4;
5984 }
5985 if (STRNICMP(text, "nan", 3) == 0)
5986 {
5987 *value = NAN;
5988 return 3;
5989 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005990 f = strtod(s, &s);
5991 *value = f;
5992 return (int)((char_u *)s - text);
5993}
5994#endif
5995
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * Get the value of an environment variable.
5998 * "arg" is pointing to the '$'. It is advanced to after the name.
5999 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006000 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 */
6002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 char_u *string = NULL;
6006 int len;
6007 int cc;
6008 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006009 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
6011 ++*arg;
6012 name = *arg;
6013 len = get_env_len(arg);
6014 if (evaluate)
6015 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006016 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006017 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006018
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006019 cc = name[len];
6020 name[len] = NUL;
6021 /* first try vim_getenv(), fast for normal environment vars */
6022 string = vim_getenv(name, &mustfree);
6023 if (string != NULL && *string != NUL)
6024 {
6025 if (!mustfree)
6026 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006028 else
6029 {
6030 if (mustfree)
6031 vim_free(string);
6032
6033 /* next try expanding things like $VIM and ${HOME} */
6034 string = expand_env_save(name - 1);
6035 if (string != NULL && *string == '$')
6036 {
6037 vim_free(string);
6038 string = NULL;
6039 }
6040 }
6041 name[len] = cc;
6042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006043 rettv->v_type = VAR_STRING;
6044 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046
6047 return OK;
6048}
6049
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006050
6051
6052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006054 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057var2fpos(
6058 typval_T *varp,
6059 int dollar_lnum, /* TRUE when $ is last line */
6060 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006062 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006064 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaara5525202006-03-02 22:52:09 +00006066 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006067 if (varp->v_type == VAR_LIST)
6068 {
6069 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006070 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006071 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006072 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006073
6074 l = varp->vval.v_list;
6075 if (l == NULL)
6076 return NULL;
6077
6078 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006079 pos.lnum = list_find_nr(l, 0L, &error);
6080 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006081 return NULL; /* invalid line number */
6082
6083 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006084 pos.col = list_find_nr(l, 1L, &error);
6085 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006086 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006087 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006088
6089 /* We accept "$" for the column number: last column. */
6090 li = list_find(l, 1L);
6091 if (li != NULL && li->li_tv.v_type == VAR_STRING
6092 && li->li_tv.vval.v_string != NULL
6093 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6094 pos.col = len + 1;
6095
Bram Moolenaara5525202006-03-02 22:52:09 +00006096 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006097 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006098 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006099 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006100
Bram Moolenaara5525202006-03-02 22:52:09 +00006101#ifdef FEAT_VIRTUALEDIT
6102 /* Get the virtual offset. Defaults to zero. */
6103 pos.coladd = list_find_nr(l, 2L, &error);
6104 if (error)
6105 pos.coladd = 0;
6106#endif
6107
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006108 return &pos;
6109 }
6110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006111 name = get_tv_string_chk(varp);
6112 if (name == NULL)
6113 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006114 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006116 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6117 {
6118 if (VIsual_active)
6119 return &VIsual;
6120 return &curwin->w_cursor;
6121 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006122 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006124 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6126 return NULL;
6127 return pp;
6128 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006129
6130#ifdef FEAT_VIRTUALEDIT
6131 pos.coladd = 0;
6132#endif
6133
Bram Moolenaar477933c2007-07-17 14:32:23 +00006134 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006135 {
6136 pos.col = 0;
6137 if (name[1] == '0') /* "w0": first visible line */
6138 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006139 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006140 pos.lnum = curwin->w_topline;
6141 return &pos;
6142 }
6143 else if (name[1] == '$') /* "w$": last visible line */
6144 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006145 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006146 pos.lnum = curwin->w_botline - 1;
6147 return &pos;
6148 }
6149 }
6150 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006152 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
6154 pos.lnum = curbuf->b_ml.ml_line_count;
6155 pos.col = 0;
6156 }
6157 else
6158 {
6159 pos.lnum = curwin->w_cursor.lnum;
6160 pos.col = (colnr_T)STRLEN(ml_get_curline());
6161 }
6162 return &pos;
6163 }
6164 return NULL;
6165}
6166
6167/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006168 * Convert list in "arg" into a position and optional file number.
6169 * When "fnump" is NULL there is no file number, only 3 items.
6170 * Note that the column is passed on as-is, the caller may want to decrement
6171 * it to use 1 for the first column.
6172 * Return FAIL when conversion is not possible, doesn't check the position for
6173 * validity.
6174 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176list2fpos(
6177 typval_T *arg,
6178 pos_T *posp,
6179 int *fnump,
6180 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006181{
6182 list_T *l = arg->vval.v_list;
6183 long i = 0;
6184 long n;
6185
Bram Moolenaar493c1782014-05-28 14:34:46 +02006186 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6187 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006188 if (arg->v_type != VAR_LIST
6189 || l == NULL
6190 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006191 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006192 return FAIL;
6193
6194 if (fnump != NULL)
6195 {
6196 n = list_find_nr(l, i++, NULL); /* fnum */
6197 if (n < 0)
6198 return FAIL;
6199 if (n == 0)
6200 n = curbuf->b_fnum; /* current buffer */
6201 *fnump = n;
6202 }
6203
6204 n = list_find_nr(l, i++, NULL); /* lnum */
6205 if (n < 0)
6206 return FAIL;
6207 posp->lnum = n;
6208
6209 n = list_find_nr(l, i++, NULL); /* col */
6210 if (n < 0)
6211 return FAIL;
6212 posp->col = n;
6213
6214#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +02006215 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006216 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006217 posp->coladd = 0;
6218 else
6219 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006220#endif
6221
Bram Moolenaar493c1782014-05-28 14:34:46 +02006222 if (curswantp != NULL)
6223 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6224
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006225 return OK;
6226}
6227
6228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 * Get the length of an environment variable name.
6230 * Advance "arg" to the first character after the name.
6231 * Return 0 for error.
6232 */
6233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 char_u *p;
6237 int len;
6238
6239 for (p = *arg; vim_isIDc(*p); ++p)
6240 ;
6241 if (p == *arg) /* no name found */
6242 return 0;
6243
6244 len = (int)(p - *arg);
6245 *arg = p;
6246 return len;
6247}
6248
6249/*
6250 * Get the length of the name of a function or internal variable.
6251 * "arg" is advanced to the first non-white character after the name.
6252 * Return 0 if something is wrong.
6253 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 char_u *p;
6258 int len;
6259
6260 /* Find the end of the name. */
6261 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006262 {
6263 if (*p == ':')
6264 {
6265 /* "s:" is start of "s:var", but "n:" is not and can be used in
6266 * slice "[n:]". Also "xx:" is not a namespace. */
6267 len = (int)(p - *arg);
6268 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6269 || len > 1)
6270 break;
6271 }
6272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (p == *arg) /* no name found */
6274 return 0;
6275
6276 len = (int)(p - *arg);
6277 *arg = skipwhite(p);
6278
6279 return len;
6280}
6281
6282/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006283 * Get the length of the name of a variable or function.
6284 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006286 * Return -1 if curly braces expansion failed.
6287 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 * If the name contains 'magic' {}'s, expand them and return the
6289 * expanded name in an allocated string via 'alias' - caller must free.
6290 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006292get_name_len(
6293 char_u **arg,
6294 char_u **alias,
6295 int evaluate,
6296 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 char_u *p;
6300 char_u *expr_start;
6301 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 *alias = NULL; /* default to no alias */
6304
6305 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6306 && (*arg)[2] == (int)KE_SNR)
6307 {
6308 /* hard coded <SNR>, already translated */
6309 *arg += 3;
6310 return get_id_len(arg) + 3;
6311 }
6312 len = eval_fname_script(*arg);
6313 if (len > 0)
6314 {
6315 /* literal "<SID>", "s:" or "<SNR>" */
6316 *arg += len;
6317 }
6318
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006322 p = find_name_end(*arg, &expr_start, &expr_end,
6323 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (expr_start != NULL)
6325 {
6326 char_u *temp_string;
6327
6328 if (!evaluate)
6329 {
6330 len += (int)(p - *arg);
6331 *arg = skipwhite(p);
6332 return len;
6333 }
6334
6335 /*
6336 * Include any <SID> etc in the expanded string:
6337 * Thus the -len here.
6338 */
6339 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6340 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006341 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 *alias = temp_string;
6343 *arg = skipwhite(p);
6344 return (int)STRLEN(temp_string);
6345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006348 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 EMSG2(_(e_invexpr2), *arg);
6350
6351 return len;
6352}
6353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006354/*
6355 * Find the end of a variable or function name, taking care of magic braces.
6356 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6357 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006358 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006359 * Return a pointer to just after the name. Equal to "arg" if there is no
6360 * valid name.
6361 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006363find_name_end(
6364 char_u *arg,
6365 char_u **expr_start,
6366 char_u **expr_end,
6367 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006369 int mb_nest = 0;
6370 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006372 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 *expr_start = NULL;
6377 *expr_end = NULL;
6378 }
6379
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006380 /* Quick check for valid starting character. */
6381 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6382 return arg;
6383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 for (p = arg; *p != NUL
6385 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006387 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006388 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +00006389 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006390 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006391 if (*p == '\'')
6392 {
6393 /* skip over 'string' to avoid counting [ and ] inside it. */
6394 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
6395 ;
6396 if (*p == NUL)
6397 break;
6398 }
6399 else if (*p == '"')
6400 {
6401 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
6402 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
6403 if (*p == '\\' && p[1] != NUL)
6404 ++p;
6405 if (*p == NUL)
6406 break;
6407 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006408 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6409 {
6410 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006411 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006412 len = (int)(p - arg);
6413 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006414 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006415 break;
6416 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 if (*p == '[')
6421 ++br_nest;
6422 else if (*p == ']')
6423 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006426 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006428 if (*p == '{')
6429 {
6430 mb_nest++;
6431 if (expr_start != NULL && *expr_start == NULL)
6432 *expr_start = p;
6433 }
6434 else if (*p == '}')
6435 {
6436 mb_nest--;
6437 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6438 *expr_end = p;
6439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 }
6442
6443 return p;
6444}
6445
6446/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006447 * Expands out the 'magic' {}'s in a variable/function name.
6448 * Note that this can call itself recursively, to deal with
6449 * constructs like foo{bar}{baz}{bam}
6450 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6451 * "in_start" ^
6452 * "expr_start" ^
6453 * "expr_end" ^
6454 * "in_end" ^
6455 *
6456 * Returns a new allocated string, which the caller must free.
6457 * Returns NULL for failure.
6458 */
6459 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460make_expanded_name(
6461 char_u *in_start,
6462 char_u *expr_start,
6463 char_u *expr_end,
6464 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006465{
6466 char_u c1;
6467 char_u *retval = NULL;
6468 char_u *temp_result;
6469 char_u *nextcmd = NULL;
6470
6471 if (expr_end == NULL || in_end == NULL)
6472 return NULL;
6473 *expr_start = NUL;
6474 *expr_end = NUL;
6475 c1 = *in_end;
6476 *in_end = NUL;
6477
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006478 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006479 if (temp_result != NULL && nextcmd == NULL)
6480 {
6481 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6482 + (in_end - expr_end) + 1));
6483 if (retval != NULL)
6484 {
6485 STRCPY(retval, in_start);
6486 STRCAT(retval, temp_result);
6487 STRCAT(retval, expr_end + 1);
6488 }
6489 }
6490 vim_free(temp_result);
6491
6492 *in_end = c1; /* put char back for error messages */
6493 *expr_start = '{';
6494 *expr_end = '}';
6495
6496 if (retval != NULL)
6497 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006498 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006499 if (expr_start != NULL)
6500 {
6501 /* Further expansion! */
6502 temp_result = make_expanded_name(retval, expr_start,
6503 expr_end, temp_result);
6504 vim_free(retval);
6505 retval = temp_result;
6506 }
6507 }
6508
6509 return retval;
6510}
6511
6512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006514 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006517eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006519 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6520}
6521
6522/*
6523 * Return TRUE if character "c" can be used as the first character in a
6524 * variable or function name (excluding '{' and '}').
6525 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006526 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006527eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006528{
6529 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 * Set number v: variable to "val".
6534 */
6535 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006536set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006538 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539}
6540
6541/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006542 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006544 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006545get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548}
6549
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006550/*
6551 * Get string v: variable value. Uses a static buffer, can only be used once.
6552 */
6553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006555{
6556 return get_tv_string(&vimvars[idx].vv_tv);
6557}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006560 * Get List v: variable value. Caller must take care of reference count when
6561 * needed.
6562 */
6563 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006564get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006565{
6566 return vimvars[idx].vv_list;
6567}
6568
6569/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006570 * Set v:char to character "c".
6571 */
6572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006574{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006575 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006576
6577#ifdef FEAT_MBYTE
6578 if (has_mbyte)
6579 buf[(*mb_char2bytes)(c, buf)] = NUL;
6580 else
6581#endif
6582 {
6583 buf[0] = c;
6584 buf[1] = NUL;
6585 }
6586 set_vim_var_string(VV_CHAR, buf, -1);
6587}
6588
6589/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006590 * Set v:count to "count" and v:count1 to "count1".
6591 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 */
6593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006594set_vcount(
6595 long count,
6596 long count1,
6597 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006599 if (set_prevcount)
6600 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006601 vimvars[VV_COUNT].vv_nr = count;
6602 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
6605/*
6606 * Set string v: variable to a copy of "val".
6607 */
6608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609set_vim_var_string(
6610 int idx,
6611 char_u *val,
6612 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
Bram Moolenaara542c682016-01-31 16:28:04 +01006614 clear_tv(&vimvars[idx].vv_di.di_tv);
6615 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006617 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006619 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006621 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622}
6623
6624/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006625 * Set List v: variable to "val".
6626 */
6627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006628set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006629{
Bram Moolenaara542c682016-01-31 16:28:04 +01006630 clear_tv(&vimvars[idx].vv_di.di_tv);
6631 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006632 vimvars[idx].vv_list = val;
6633 if (val != NULL)
6634 ++val->lv_refcount;
6635}
6636
6637/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006638 * Set Dictionary v: variable to "val".
6639 */
6640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006641set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006642{
6643 int todo;
6644 hashitem_T *hi;
6645
Bram Moolenaara542c682016-01-31 16:28:04 +01006646 clear_tv(&vimvars[idx].vv_di.di_tv);
6647 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006648 vimvars[idx].vv_dict = val;
6649 if (val != NULL)
6650 {
6651 ++val->dv_refcount;
6652
6653 /* Set readonly */
6654 todo = (int)val->dv_hashtab.ht_used;
6655 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
6656 {
6657 if (HASHITEM_EMPTY(hi))
6658 continue;
6659 --todo;
6660 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
6661 }
6662 }
6663}
6664
6665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 * Set v:register if needed.
6667 */
6668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 char_u regname;
6672
6673 if (c == 0 || c == ' ')
6674 regname = '"';
6675 else
6676 regname = c;
6677 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006678 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 set_vim_var_string(VV_REG, &regname, 1);
6680}
6681
6682/*
6683 * Get or set v:exception. If "oldval" == NULL, return the current value.
6684 * Otherwise, restore the value to "oldval" and return NULL.
6685 * Must always be called in pairs to save and restore v:exception! Does not
6686 * take care of memory allocations.
6687 */
6688 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006689v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006692 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 return NULL;
6696}
6697
6698/*
6699 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6700 * Otherwise, restore the value to "oldval" and return NULL.
6701 * Must always be called in pairs to save and restore v:throwpoint! Does not
6702 * take care of memory allocations.
6703 */
6704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006705v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006708 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 return NULL;
6712}
6713
6714#if defined(FEAT_AUTOCMD) || defined(PROTO)
6715/*
6716 * Set v:cmdarg.
6717 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6718 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6719 * Must always be called in pairs!
6720 */
6721 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006722set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 char_u *oldval;
6725 char_u *newval;
6726 unsigned len;
6727
Bram Moolenaare9a41262005-01-15 22:18:47 +00006728 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006729 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006731 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006732 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006733 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 }
6735
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006736 if (eap->force_bin == FORCE_BIN)
6737 len = 6;
6738 else if (eap->force_bin == FORCE_NOBIN)
6739 len = 8;
6740 else
6741 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742
6743 if (eap->read_edit)
6744 len += 7;
6745
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006746 if (eap->force_ff != 0)
6747 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
6748# ifdef FEAT_MBYTE
6749 if (eap->force_enc != 0)
6750 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006751 if (eap->bad_char != 0)
6752 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006753# endif
6754
6755 newval = alloc(len + 1);
6756 if (newval == NULL)
6757 return NULL;
6758
6759 if (eap->force_bin == FORCE_BIN)
6760 sprintf((char *)newval, " ++bin");
6761 else if (eap->force_bin == FORCE_NOBIN)
6762 sprintf((char *)newval, " ++nobin");
6763 else
6764 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006765
6766 if (eap->read_edit)
6767 STRCAT(newval, " ++edit");
6768
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006769 if (eap->force_ff != 0)
6770 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
6771 eap->cmd + eap->force_ff);
6772# ifdef FEAT_MBYTE
6773 if (eap->force_enc != 0)
6774 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6775 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006776 if (eap->bad_char == BAD_KEEP)
6777 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6778 else if (eap->bad_char == BAD_DROP)
6779 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6780 else if (eap->bad_char != 0)
6781 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006782# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00006783 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006784 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785}
6786#endif
6787
6788/*
6789 * Get the value of internal variable "name".
6790 * Return OK or FAIL.
6791 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006793get_var_tv(
6794 char_u *name,
6795 int len, /* length of "name" */
6796 typval_T *rettv, /* NULL when only checking existence */
6797 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6798 int verbose, /* may give error message */
6799 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006802 typval_T *tv = NULL;
6803 typval_T atv;
6804 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 /* truncate the name, so that we can use strcmp() */
6808 cc = name[len];
6809 name[len] = NUL;
6810
6811 /*
6812 * Check for "b:changedtick".
6813 */
6814 if (STRCMP(name, "b:changedtick") == 0)
6815 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 atv.v_type = VAR_NUMBER;
6817 atv.vval.v_number = curbuf->b_changedtick;
6818 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820
6821 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 * Check for user-defined variables.
6823 */
6824 else
6825 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01006826 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006828 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006829 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02006830 if (dip != NULL)
6831 *dip = v;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006837 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006838 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 ret = FAIL;
6840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006842 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
6844 name[len] = cc;
6845
6846 return ret;
6847}
6848
6849/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006850 * Check if variable "name[len]" is a local variable or an argument.
6851 * If so, "*eval_lavars_used" is set to TRUE.
6852 */
6853 static void
6854check_vars(char_u *name, int len)
6855{
6856 int cc;
6857 char_u *varname;
6858 hashtab_T *ht;
6859
6860 if (eval_lavars_used == NULL)
6861 return;
6862
6863 /* truncate the name, so that we can use strcmp() */
6864 cc = name[len];
6865 name[len] = NUL;
6866
6867 ht = find_var_ht(name, &varname);
6868 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6869 {
6870 if (find_var(name, NULL, TRUE) != NULL)
6871 *eval_lavars_used = TRUE;
6872 }
6873
6874 name[len] = cc;
6875}
6876
6877/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006878 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
6879 * Also handle function call with Funcref variable: func(expr)
6880 * Can all be combined: dict.func(expr)[idx]['func'](expr)
6881 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006883handle_subscript(
6884 char_u **arg,
6885 typval_T *rettv,
6886 int evaluate, /* do more than finding the end */
6887 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006888{
6889 int ret = OK;
6890 dict_T *selfdict = NULL;
6891 char_u *s;
6892 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006893 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006894
6895 while (ret == OK
6896 && (**arg == '['
6897 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006898 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6899 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006900 && !vim_iswhite(*(*arg - 1)))
6901 {
6902 if (**arg == '(')
6903 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006904 partial_T *pt = NULL;
6905
Bram Moolenaard9fba312005-06-26 22:34:35 +00006906 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006907 if (evaluate)
6908 {
6909 functv = *rettv;
6910 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006911
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006912 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006913 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006914 {
6915 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006916 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006917 }
6918 else
6919 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006920 }
6921 else
6922 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006923 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006925 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006926
6927 /* Clear the funcref afterwards, so that deleting it while
6928 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01006929 if (evaluate)
6930 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006931
6932 /* Stop the expression evaluation when immediately aborting on
6933 * error, or when an interrupt occurred or an exception was thrown
6934 * but not caught. */
6935 if (aborting())
6936 {
6937 if (ret == OK)
6938 clear_tv(rettv);
6939 ret = FAIL;
6940 }
6941 dict_unref(selfdict);
6942 selfdict = NULL;
6943 }
6944 else /* **arg == '[' || **arg == '.' */
6945 {
6946 dict_unref(selfdict);
6947 if (rettv->v_type == VAR_DICT)
6948 {
6949 selfdict = rettv->vval.v_dict;
6950 if (selfdict != NULL)
6951 ++selfdict->dv_refcount;
6952 }
6953 else
6954 selfdict = NULL;
6955 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
6956 {
6957 clear_tv(rettv);
6958 ret = FAIL;
6959 }
6960 }
6961 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006962
Bram Moolenaar1d429612016-05-24 15:44:17 +02006963 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
6964 * Don't do this when "Func" is already a partial that was bound
6965 * explicitly (pt_auto is FALSE). */
6966 if (selfdict != NULL
6967 && (rettv->v_type == VAR_FUNC
6968 || (rettv->v_type == VAR_PARTIAL
6969 && (rettv->vval.v_partial->pt_auto
6970 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006971 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006972
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006973 dict_unref(selfdict);
6974 return ret;
6975}
6976
6977/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006978 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006979 * value).
6980 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01006981 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006983{
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006985}
6986
6987/*
6988 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 * The string "s" must have been allocated, it is consumed.
6990 * Return NULL for out of memory, the variable otherwise.
6991 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006992 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006993alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 rettv = alloc_tv();
6998 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007000 rettv->v_type = VAR_STRING;
7001 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003 else
7004 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007005 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006}
7007
7008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007009 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007012free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
7014 if (varp != NULL)
7015 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007016 switch (varp->v_type)
7017 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007018 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007019 func_unref(varp->vval.v_string);
7020 /*FALLTHROUGH*/
7021 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007022 vim_free(varp->vval.v_string);
7023 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007024 case VAR_PARTIAL:
7025 partial_unref(varp->vval.v_partial);
7026 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007027 case VAR_LIST:
7028 list_unref(varp->vval.v_list);
7029 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007030 case VAR_DICT:
7031 dict_unref(varp->vval.v_dict);
7032 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007033 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007034#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007035 job_unref(varp->vval.v_job);
7036 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007037#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007038 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007039#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007040 channel_unref(varp->vval.v_channel);
7041 break;
7042#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007043 case VAR_NUMBER:
7044 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007045 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007046 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007047 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 vim_free(varp);
7050 }
7051}
7052
7053/*
7054 * Free the memory for a variable value and set the value to NULL or 0.
7055 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007057clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 if (varp != NULL)
7060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007061 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007063 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007064 func_unref(varp->vval.v_string);
7065 /*FALLTHROUGH*/
7066 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007067 vim_free(varp->vval.v_string);
7068 varp->vval.v_string = NULL;
7069 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007070 case VAR_PARTIAL:
7071 partial_unref(varp->vval.v_partial);
7072 varp->vval.v_partial = NULL;
7073 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007074 case VAR_LIST:
7075 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007077 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 case VAR_DICT:
7079 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007082 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007083 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007084 varp->vval.v_number = 0;
7085 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007086 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007087#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007088 varp->vval.v_float = 0.0;
7089 break;
7090#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007091 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007092#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007093 job_unref(varp->vval.v_job);
7094 varp->vval.v_job = NULL;
7095#endif
7096 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007097 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007098#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007099 channel_unref(varp->vval.v_channel);
7100 varp->vval.v_channel = NULL;
7101#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007102 case VAR_UNKNOWN:
7103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007105 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 }
7107}
7108
7109/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007110 * Set the value of a variable to NULL without freeing items.
7111 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007113init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007114{
7115 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007117}
7118
7119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 * Get the number value of a variable.
7121 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007122 * For incompatible types, return 0.
7123 * get_tv_number_chk() is similar to get_tv_number(), but informs the
7124 * caller of incompatible types: it sets *denote to TRUE if "denote"
7125 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007127 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007128get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007130 int error = FALSE;
7131
7132 return get_tv_number_chk(varp, &error); /* return 0L on error */
7133}
7134
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007135 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007136get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007137{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007138 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007140 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007142 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007143 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007144 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007145#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +00007146 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007147 break;
7148#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007149 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007150 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007151 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007152 break;
7153 case VAR_STRING:
7154 if (varp->vval.v_string != NULL)
7155 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007156 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007158 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007159 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007160 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007161 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +00007162 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007163 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007164 case VAR_SPECIAL:
7165 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7166 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007167 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007169 EMSG(_("E910: Using a Job as a Number"));
7170 break;
7171#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007172 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007173#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007174 EMSG(_("E913: Using a Channel as a Number"));
7175 break;
7176#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01007177 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007178 internal_error("get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007181 if (denote == NULL) /* useful for values that must be unsigned */
7182 n = -1;
7183 else
7184 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007185 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186}
7187
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007188#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007191{
7192 switch (varp->v_type)
7193 {
7194 case VAR_NUMBER:
7195 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007196 case VAR_FLOAT:
7197 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007198 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007199 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007200 EMSG(_("E891: Using a Funcref as a Float"));
7201 break;
7202 case VAR_STRING:
7203 EMSG(_("E892: Using a String as a Float"));
7204 break;
7205 case VAR_LIST:
7206 EMSG(_("E893: Using a List as a Float"));
7207 break;
7208 case VAR_DICT:
7209 EMSG(_("E894: Using a Dictionary as a Float"));
7210 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007211 case VAR_SPECIAL:
7212 EMSG(_("E907: Using a special value as a Float"));
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(_("E911: Using a Job as a Float"));
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(_("E914: Using a Channel as a Float"));
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_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007226 break;
7227 }
7228 return 0;
7229}
7230#endif
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 * Get the string value of a variable.
7234 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +00007235 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7236 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 * If the String variable has never been set, return an empty string.
7238 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007239 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
7240 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007242 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 static char_u mybuf[NUMBUFLEN];
7246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007247 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248}
7249
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007250 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007251get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007253 char_u *res = get_tv_string_buf_chk(varp, buf);
7254
7255 return res != NULL ? res : (char_u *)"";
7256}
7257
Bram Moolenaar7d647822014-04-05 21:28:56 +02007258/*
7259 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7260 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007261 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007262get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007263{
7264 static char_u mybuf[NUMBUFLEN];
7265
7266 return get_tv_string_buf_chk(varp, mybuf);
7267}
7268
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007269 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007271{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007275 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
7276 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007277 return buf;
7278 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007279 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007280 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007281 break;
7282 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007283 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284 break;
7285 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007286 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007287 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007288 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007289#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +02007290 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007291 break;
7292#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007293 case VAR_STRING:
7294 if (varp->vval.v_string != NULL)
7295 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007296 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007297 case VAR_SPECIAL:
7298 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7299 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007300 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007302 {
7303 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007304 char *status;
7305
7306 if (job == NULL)
7307 return (char_u *)"no process";
7308 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007309 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007310 : "run";
7311# ifdef UNIX
7312 vim_snprintf((char *)buf, NUMBUFLEN,
7313 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007314# elif defined(WIN32)
7315 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007316 "process %ld %s",
7317 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007318 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007319# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007320 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007321 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7322# endif
7323 return buf;
7324 }
7325#endif
7326 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007327 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007328#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007329 {
7330 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007331 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007332
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007333 if (channel == NULL)
7334 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7335 else
7336 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007337 "channel %d %s", channel->ch_id, status);
7338 return buf;
7339 }
7340#endif
7341 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007342 case VAR_UNKNOWN:
7343 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007344 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007346 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347}
7348
7349/*
7350 * Find variable "name" in the list of variables.
7351 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007352 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007353 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007356 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007357find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007361 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaara7043832005-01-21 11:56:39 +00007363 ht = find_var_ht(name, &varname);
7364 if (htp != NULL)
7365 *htp = ht;
7366 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007368 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7369 if (ret != NULL)
7370 return ret;
7371
7372 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007373 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374}
7375
7376/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007377 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007378 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007381find_var_in_ht(
7382 hashtab_T *ht,
7383 int htname,
7384 char_u *varname,
7385 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007386{
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 hashitem_T *hi;
7388
7389 if (*varname == NUL)
7390 {
7391 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007392 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007394 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 case 'g': return &globvars_var;
7396 case 'v': return &vimvars_var;
7397 case 'b': return &curbuf->b_bufvar;
7398 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007399#ifdef FEAT_WINDOWS
7400 case 't': return &curtab->tp_winvar;
7401#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007402 case 'l': return get_funccal_local_var();
7403 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 }
7405 return NULL;
7406 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007407
7408 hi = hash_find(ht, varname);
7409 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007410 {
7411 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007412 * worked find the variable again. Don't auto-load a script if it was
7413 * loaded already, otherwise it would be loaded every time when
7414 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007415 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007416 {
7417 /* Note: script_autoload() may make "hi" invalid. It must either
7418 * be obtained again or not used. */
7419 if (!script_autoload(varname, FALSE) || aborting())
7420 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007421 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007422 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007423 if (HASHITEM_EMPTY(hi))
7424 return NULL;
7425 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007427}
7428
7429/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007431 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007432 * Set "varname" to the start of name without ':'.
7433 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007434 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007437 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007438 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007439
Bram Moolenaar73627d02015-08-11 15:46:09 +02007440 if (name[0] == NUL)
7441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (name[1] != ':')
7443 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007444 /* The name must not start with a colon or #. */
7445 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 return NULL;
7447 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007448
7449 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007450 hi = hash_find(&compat_hashtab, name);
7451 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007452 return &compat_hashtab;
7453
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007454 ht = get_funccal_local_ht();
7455 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007457 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007460 if (*name == 'g') /* global variable */
7461 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007462 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7463 */
7464 if (vim_strchr(name + 2, ':') != NULL
7465 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007466 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007468 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007470 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007471#ifdef FEAT_WINDOWS
7472 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007473 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 if (*name == 'v') /* v: variable */
7476 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007477 if (*name == 'a') /* a: function argument */
7478 return get_funccal_args_ht();
7479 if (*name == 'l') /* l: local function variable */
7480 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 if (*name == 's' /* script variable */
7482 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
7483 return &SCRIPT_VARS(current_SID);
7484 return NULL;
7485}
7486
7487/*
7488 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +02007489 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 * Returns NULL when it doesn't exist.
7491 */
7492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007493get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007497 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 if (v == NULL)
7499 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501}
7502
7503/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 * sourcing this script and when executing functions defined in the script.
7506 */
7507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007508new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
Bram Moolenaara7043832005-01-21 11:56:39 +00007510 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hashtab_T *ht;
7512 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7515 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007516 /* Re-allocating ga_data means that an ht_array pointing to
7517 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007519 for (i = 1; i <= ga_scripts.ga_len; ++i)
7520 {
7521 ht = &SCRIPT_VARS(i);
7522 if (ht->ht_mask == HT_INIT_SIZE - 1)
7523 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007524 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007526 }
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 while (ga_scripts.ga_len < id)
7529 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007530 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007531 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007532 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
7535 }
7536}
7537
7538/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7540 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 */
7542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007546 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007547 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007548 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007549 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 dict_var->di_tv.vval.v_dict = dict;
7551 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7554 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555}
7556
7557/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007558 * Unreference a dictionary initialized by init_var_dict().
7559 */
7560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007562{
7563 /* Now the dict needs to be freed if no one else is using it, go back to
7564 * normal reference counting. */
7565 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7566 dict_unref(dict);
7567}
7568
7569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 * Frees all allocated variables and the value they contain.
7572 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 */
7574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007575vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007576{
7577 vars_clear_ext(ht, TRUE);
7578}
7579
7580/*
7581 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7582 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
Bram Moolenaara7043832005-01-21 11:56:39 +00007586 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hashitem_T *hi;
7588 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007591 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007592 for (hi = ht->ht_array; todo > 0; ++hi)
7593 {
7594 if (!HASHITEM_EMPTY(hi))
7595 {
7596 --todo;
7597
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007599 * ht_array might change then. hash_clear() takes care of it
7600 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 v = HI2DI(hi);
7602 if (free_val)
7603 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007604 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007606 }
7607 }
7608 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007609 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
7611
Bram Moolenaara7043832005-01-21 11:56:39 +00007612/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007613 * Delete a variable from hashtab "ht" at item "hi".
7614 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007620
7621 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007622 clear_tv(&di->di_tv);
7623 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624}
7625
7626/*
7627 * List the value of one internal variable.
7628 */
7629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007630list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 char_u *tofree;
7633 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007634 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007636 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007638 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643list_one_var_a(
7644 char_u *prefix,
7645 char_u *name,
7646 int type,
7647 char_u *string,
7648 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
Bram Moolenaar31859182007-08-14 20:41:13 +00007650 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7651 msg_start();
7652 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (name != NULL) /* "a:" vars don't have a name stored */
7654 msg_puts(name);
7655 msg_putchar(' ');
7656 msg_advance(22);
7657 if (type == VAR_NUMBER)
7658 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007659 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 msg_putchar('*');
7661 else if (type == VAR_LIST)
7662 {
7663 msg_putchar('[');
7664 if (*string == '[')
7665 ++string;
7666 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 else if (type == VAR_DICT)
7668 {
7669 msg_putchar('{');
7670 if (*string == '{')
7671 ++string;
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 else
7674 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007677
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007678 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007679 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007680 if (*first)
7681 {
7682 msg_clr_eos();
7683 *first = FALSE;
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
7687/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 * If the variable already exists, the value is updated.
7690 * Otherwise the variable is created.
7691 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007693set_var(
7694 char_u *name,
7695 typval_T *tv,
7696 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007702 ht = find_var_ht(name, &varname);
7703 if (ht == NULL || *varname == NUL)
7704 {
7705 EMSG2(_(e_illvar), name);
7706 return;
7707 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007708 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007709
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007710 /* Search in parent scope which is possible to reference from lambda */
7711 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007712 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007713
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007714 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7715 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007716 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007720 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007721 if (var_check_ro(v->di_flags, name, FALSE)
7722 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007724
7725 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007726 * Handle setting internal v: variables separately where needed to
7727 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 */
7729 if (ht == &vimvarht)
7730 {
7731 if (v->di_tv.v_type == VAR_STRING)
7732 {
7733 vim_free(v->di_tv.vval.v_string);
7734 if (copy || tv->v_type != VAR_STRING)
7735 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
7736 else
7737 {
7738 /* Take over the string to avoid an extra alloc/free. */
7739 v->di_tv.vval.v_string = tv->vval.v_string;
7740 tv->vval.v_string = NULL;
7741 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007742 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007744 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007747 if (STRCMP(varname, "searchforward") == 0)
7748 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007749#ifdef FEAT_SEARCH_EXTRA
7750 else if (STRCMP(varname, "hlsearch") == 0)
7751 {
7752 no_hlsearch = !v->di_tv.vval.v_number;
7753 redraw_all_later(SOME_VALID);
7754 }
7755#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007756 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007757 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007758 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar95f09602016-11-10 20:01:45 +01007759 internal_error("set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 }
7761
7762 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764 else /* add a new variable */
7765 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007766 /* Can't add "v:" variable. */
7767 if (ht == &vimvarht)
7768 {
7769 EMSG2(_(e_illvar), name);
7770 return;
7771 }
7772
Bram Moolenaar92124a32005-06-17 22:03:40 +00007773 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007774 if (!valid_varname(varname))
7775 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007776
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007777 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7778 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007779 if (v == NULL)
7780 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007784 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 return;
7786 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007787 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007789
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007790 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007791 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007792 else
7793 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007795 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007801 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 * Also give an error message.
7803 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00007806{
7807 if (flags & DI_FLAGS_RO)
7808 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007809 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 return TRUE;
7811 }
7812 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
7813 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007814 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 return TRUE;
7816 }
7817 return FALSE;
7818}
7819
7820/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007821 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
7822 * Also give an error message.
7823 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007826{
7827 if (flags & DI_FLAGS_FIX)
7828 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02007829 EMSG2(_("E795: Cannot delete variable %s"),
7830 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007831 return TRUE;
7832 }
7833 return FALSE;
7834}
7835
7836/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007837 * Check if a funcref is assigned to a valid variable name.
7838 * Return TRUE and give an error if not.
7839 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841var_check_func_name(
7842 char_u *name, /* points to start of variable name */
7843 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007844{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02007845 /* Allow for w: b: s: and t:. */
7846 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007847 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
7848 ? name[2] : name[0]))
7849 {
7850 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
7851 name);
7852 return TRUE;
7853 }
7854 /* Don't allow hiding a function. When "v" is not NULL we might be
7855 * assigning another function to the same var, the type is checked
7856 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02007857 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007858 {
7859 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
7860 name);
7861 return TRUE;
7862 }
7863 return FALSE;
7864}
7865
7866/*
7867 * Check if a variable name is valid.
7868 * Return FALSE and give an error if not.
7869 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007871valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007872{
7873 char_u *p;
7874
7875 for (p = varname; *p != NUL; ++p)
7876 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
7877 && *p != AUTOLOAD_CHAR)
7878 {
7879 EMSG2(_(e_illvar), varname);
7880 return FALSE;
7881 }
7882 return TRUE;
7883}
7884
7885/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007886 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02007887 * Also give an error message, using "name" or _("name") when use_gettext is
7888 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007889 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007891tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007892{
7893 if (lock & VAR_LOCKED)
7894 {
7895 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007896 name == NULL ? (char_u *)_("Unknown")
7897 : use_gettext ? (char_u *)_(name)
7898 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007899 return TRUE;
7900 }
7901 if (lock & VAR_FIXED)
7902 {
7903 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02007904 name == NULL ? (char_u *)_("Unknown")
7905 : use_gettext ? (char_u *)_(name)
7906 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007907 return TRUE;
7908 }
7909 return FALSE;
7910}
7911
7912/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007916 * It is OK for "from" and "to" to point to the same item. This is used to
7917 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007918 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007920copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007923 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 switch (from->v_type)
7925 {
7926 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007927 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 to->vval.v_number = from->vval.v_number;
7929 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007930 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007931#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007932 to->vval.v_float = from->vval.v_float;
7933 break;
7934#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007935 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007936#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007937 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007938 if (to->vval.v_job != NULL)
7939 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007940 break;
7941#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007942 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007943#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007944 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007945 if (to->vval.v_channel != NULL)
7946 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01007947 break;
7948#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 case VAR_STRING:
7950 case VAR_FUNC:
7951 if (from->vval.v_string == NULL)
7952 to->vval.v_string = NULL;
7953 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007954 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007956 if (from->v_type == VAR_FUNC)
7957 func_ref(to->vval.v_string);
7958 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007959 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007960 case VAR_PARTIAL:
7961 if (from->vval.v_partial == NULL)
7962 to->vval.v_partial = NULL;
7963 else
7964 {
7965 to->vval.v_partial = from->vval.v_partial;
7966 ++to->vval.v_partial->pt_refcount;
7967 }
7968 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007969 case VAR_LIST:
7970 if (from->vval.v_list == NULL)
7971 to->vval.v_list = NULL;
7972 else
7973 {
7974 to->vval.v_list = from->vval.v_list;
7975 ++to->vval.v_list->lv_refcount;
7976 }
7977 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 case VAR_DICT:
7979 if (from->vval.v_dict == NULL)
7980 to->vval.v_dict = NULL;
7981 else
7982 {
7983 to->vval.v_dict = from->vval.v_dict;
7984 ++to->vval.v_dict->dv_refcount;
7985 }
7986 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007987 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01007988 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989 break;
7990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991}
7992
7993/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007994 * Make a copy of an item.
7995 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007996 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7997 * reference to an already copied list/dict can be used.
7998 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007999 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001item_copy(
8002 typval_T *from,
8003 typval_T *to,
8004 int deep,
8005 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008006{
8007 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008008 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 {
8012 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008013 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 }
8015 ++recurse;
8016
8017 switch (from->v_type)
8018 {
8019 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 case VAR_STRING:
8022 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008023 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008026 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 copy_tv(from, to);
8028 break;
8029 case VAR_LIST:
8030 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008031 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008032 if (from->vval.v_list == NULL)
8033 to->vval.v_list = NULL;
8034 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8035 {
8036 /* use the copy made earlier */
8037 to->vval.v_list = from->vval.v_list->lv_copylist;
8038 ++to->vval.v_list->lv_refcount;
8039 }
8040 else
8041 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8042 if (to->vval.v_list == NULL)
8043 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008044 break;
8045 case VAR_DICT:
8046 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008047 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008048 if (from->vval.v_dict == NULL)
8049 to->vval.v_dict = NULL;
8050 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8051 {
8052 /* use the copy made earlier */
8053 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8054 ++to->vval.v_dict->dv_refcount;
8055 }
8056 else
8057 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8058 if (to->vval.v_dict == NULL)
8059 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008060 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008061 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008062 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008063 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064 }
8065 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008066 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008067}
8068
8069/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 * This function is used by f_input() and f_inputdialog() functions. The third
8071 * argument to f_input() specifies the type of completion to use at the
8072 * prompt. The third argument to f_inputdialog() specifies the value to return
8073 * when the user cancels the prompt.
8074 */
8075 void
8076get_user_input(
8077 typval_T *argvars,
8078 typval_T *rettv,
8079 int inputdialog,
8080 int secret)
8081{
8082 char_u *prompt = get_tv_string_chk(&argvars[0]);
8083 char_u *p = NULL;
8084 int c;
8085 char_u buf[NUMBUFLEN];
8086 int cmd_silent_save = cmd_silent;
8087 char_u *defstr = (char_u *)"";
8088 int xp_type = EXPAND_NOTHING;
8089 char_u *xp_arg = NULL;
8090
8091 rettv->v_type = VAR_STRING;
8092 rettv->vval.v_string = NULL;
8093
8094#ifdef NO_CONSOLE_INPUT
8095 /* While starting up, there is no place to enter text. */
8096 if (no_console_input())
8097 return;
8098#endif
8099
8100 cmd_silent = FALSE; /* Want to see the prompt. */
8101 if (prompt != NULL)
8102 {
8103 /* Only the part of the message after the last NL is considered as
8104 * prompt for the command line */
8105 p = vim_strrchr(prompt, '\n');
8106 if (p == NULL)
8107 p = prompt;
8108 else
8109 {
8110 ++p;
8111 c = *p;
8112 *p = NUL;
8113 msg_start();
8114 msg_clr_eos();
8115 msg_puts_attr(prompt, echo_attr);
8116 msg_didout = FALSE;
8117 msg_starthere();
8118 *p = c;
8119 }
8120 cmdline_row = msg_row;
8121
8122 if (argvars[1].v_type != VAR_UNKNOWN)
8123 {
8124 defstr = get_tv_string_buf_chk(&argvars[1], buf);
8125 if (defstr != NULL)
8126 stuffReadbuffSpec(defstr);
8127
8128 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8129 {
8130 char_u *xp_name;
8131 int xp_namelen;
8132 long argt;
8133
8134 /* input() with a third argument: completion */
8135 rettv->vval.v_string = NULL;
8136
8137 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
8138 if (xp_name == NULL)
8139 return;
8140
8141 xp_namelen = (int)STRLEN(xp_name);
8142
8143 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8144 &xp_arg) == FAIL)
8145 return;
8146 }
8147 }
8148
8149 if (defstr != NULL)
8150 {
8151 int save_ex_normal_busy = ex_normal_busy;
8152 ex_normal_busy = 0;
8153 rettv->vval.v_string =
8154 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8155 xp_type, xp_arg);
8156 ex_normal_busy = save_ex_normal_busy;
8157 }
8158 if (inputdialog && rettv->vval.v_string == NULL
8159 && argvars[1].v_type != VAR_UNKNOWN
8160 && argvars[2].v_type != VAR_UNKNOWN)
8161 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
8162 &argvars[2], buf));
8163
8164 vim_free(xp_arg);
8165
8166 /* since the user typed this, no need to wait for return */
8167 need_wait_return = FALSE;
8168 msg_didout = FALSE;
8169 }
8170 cmd_silent = cmd_silent_save;
8171}
8172
8173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 * ":echo expr1 ..." print each argument separated with a space, add a
8175 * newline at the end.
8176 * ":echon expr1 ..." print each argument plain.
8177 */
8178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008179ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008183 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 char_u *p;
8185 int needclr = TRUE;
8186 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008187 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
8189 if (eap->skip)
8190 ++emsg_skip;
8191 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8192 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 /* If eval1() causes an error message the text from the command may
8194 * still need to be cleared. E.g., "echo 22,44". */
8195 need_clr_eos = needclr;
8196
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 /*
8201 * Report the invalid expression unless the expression evaluation
8202 * has been cancelled due to an aborting error, an interrupt, or an
8203 * exception.
8204 */
8205 if (!aborting())
8206 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008207 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 break;
8209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008210 need_clr_eos = FALSE;
8211
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 if (!eap->skip)
8213 {
8214 if (atstart)
8215 {
8216 atstart = FALSE;
8217 /* Call msg_start() after eval1(), evaluating the expression
8218 * may cause a message to appear. */
8219 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008220 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008221 /* Mark the saved text as finishing the line, so that what
8222 * follows is displayed on a new line when scrolling back
8223 * at the more prompt. */
8224 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
8228 else if (eap->cmdidx == CMD_echo)
8229 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008230 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008231 if (p != NULL)
8232 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008234 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008236 if (*p != TAB && needclr)
8237 {
8238 /* remove any text still there from the command */
8239 msg_clr_eos();
8240 needclr = FALSE;
8241 }
8242 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 }
8244 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008245 {
8246#ifdef FEAT_MBYTE
8247 if (has_mbyte)
8248 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008249 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008250
8251 (void)msg_outtrans_len_attr(p, i, echo_attr);
8252 p += i - 1;
8253 }
8254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008256 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008261 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 arg = skipwhite(arg);
8263 }
8264 eap->nextcmd = check_nextcmd(arg);
8265
8266 if (eap->skip)
8267 --emsg_skip;
8268 else
8269 {
8270 /* remove text that may still be there from the command */
8271 if (needclr)
8272 msg_clr_eos();
8273 if (eap->cmdidx == CMD_echo)
8274 msg_end();
8275 }
8276}
8277
8278/*
8279 * ":echohl {name}".
8280 */
8281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008282ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 int id;
8285
8286 id = syn_name2id(eap->arg);
8287 if (id == 0)
8288 echo_attr = 0;
8289 else
8290 echo_attr = syn_id2attr(id);
8291}
8292
8293/*
8294 * ":execute expr1 ..." execute the result of an expression.
8295 * ":echomsg expr1 ..." Print a message
8296 * ":echoerr expr1 ..." Print an error
8297 * Each gets spaces around each argument and a newline at the end for
8298 * echo commands
8299 */
8300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008301ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 int ret = OK;
8306 char_u *p;
8307 garray_T ga;
8308 int len;
8309 int save_did_emsg;
8310
8311 ga_init2(&ga, 1, 80);
8312
8313 if (eap->skip)
8314 ++emsg_skip;
8315 while (*arg != NUL && *arg != '|' && *arg != '\n')
8316 {
8317 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
8320 /*
8321 * Report the invalid expression unless the expression evaluation
8322 * has been cancelled due to an aborting error, an interrupt, or an
8323 * exception.
8324 */
8325 if (!aborting())
8326 EMSG2(_(e_invexpr2), p);
8327 ret = FAIL;
8328 break;
8329 }
8330
8331 if (!eap->skip)
8332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 len = (int)STRLEN(p);
8335 if (ga_grow(&ga, len + 2) == FAIL)
8336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 ret = FAIL;
8339 break;
8340 }
8341 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 ga.ga_len += len;
8345 }
8346
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 arg = skipwhite(arg);
8349 }
8350
8351 if (ret != FAIL && ga.ga_data != NULL)
8352 {
8353 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008356 out_flush();
8357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 else if (eap->cmdidx == CMD_echoerr)
8359 {
8360 /* We don't want to abort following commands, restore did_emsg. */
8361 save_did_emsg = did_emsg;
8362 EMSG((char_u *)ga.ga_data);
8363 if (!force_abort)
8364 did_emsg = save_did_emsg;
8365 }
8366 else if (eap->cmdidx == CMD_execute)
8367 do_cmdline((char_u *)ga.ga_data,
8368 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8369 }
8370
8371 ga_clear(&ga);
8372
8373 if (eap->skip)
8374 --emsg_skip;
8375
8376 eap->nextcmd = check_nextcmd(arg);
8377}
8378
8379/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 * Find window specified by "vp" in tabpage "tp".
8381 */
8382 win_T *
8383find_win_by_nr(
8384 typval_T *vp,
8385 tabpage_T *tp UNUSED) /* NULL for current tab page */
8386{
8387#ifdef FEAT_WINDOWS
8388 win_T *wp;
8389#endif
8390 int nr;
8391
8392 nr = (int)get_tv_number_chk(vp, NULL);
8393
8394#ifdef FEAT_WINDOWS
8395 if (nr < 0)
8396 return NULL;
8397 if (nr == 0)
8398 return curwin;
8399
Bram Moolenaar29323592016-07-24 22:04:11 +02008400 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8401 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 if (nr >= LOWEST_WIN_ID)
8403 {
8404 if (wp->w_id == nr)
8405 return wp;
8406 }
8407 else if (--nr <= 0)
8408 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008409 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 if (nr >= LOWEST_WIN_ID)
8411 return NULL;
8412 return wp;
8413#else
8414 if (nr == 0 || nr == 1 || nr == curwin->w_id)
8415 return curwin;
8416 return NULL;
8417#endif
8418}
8419
8420/*
8421 * Find window specified by "wvp" in tabpage "tvp".
8422 */
8423 win_T *
8424find_tabwin(
8425 typval_T *wvp, /* VAR_UNKNOWN for current window */
8426 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8427{
8428 win_T *wp = NULL;
8429 tabpage_T *tp = NULL;
8430 long n;
8431
8432 if (wvp->v_type != VAR_UNKNOWN)
8433 {
8434 if (tvp->v_type != VAR_UNKNOWN)
8435 {
8436 n = (long)get_tv_number(tvp);
8437 if (n >= 0)
8438 tp = find_tabpage(n);
8439 }
8440 else
8441 tp = curtab;
8442
8443 if (tp != NULL)
8444 wp = find_win_by_nr(wvp, tp);
8445 }
8446 else
8447 wp = curwin;
8448
8449 return wp;
8450}
8451
8452/*
8453 * getwinvar() and gettabwinvar()
8454 */
8455 void
8456getwinvar(
8457 typval_T *argvars,
8458 typval_T *rettv,
8459 int off) /* 1 for gettabwinvar() */
8460{
8461 win_T *win;
8462 char_u *varname;
8463 dictitem_T *v;
8464 tabpage_T *tp = NULL;
8465 int done = FALSE;
8466#ifdef FEAT_WINDOWS
8467 win_T *oldcurwin;
8468 tabpage_T *oldtabpage;
8469 int need_switch_win;
8470#endif
8471
8472#ifdef FEAT_WINDOWS
8473 if (off == 1)
8474 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8475 else
8476 tp = curtab;
8477#endif
8478 win = find_win_by_nr(&argvars[off], tp);
8479 varname = get_tv_string_chk(&argvars[off + 1]);
8480 ++emsg_off;
8481
8482 rettv->v_type = VAR_STRING;
8483 rettv->vval.v_string = NULL;
8484
8485 if (win != NULL && varname != NULL)
8486 {
8487#ifdef FEAT_WINDOWS
8488 /* Set curwin to be our win, temporarily. Also set the tabpage,
8489 * otherwise the window is not valid. Only do this when needed,
8490 * autocommands get blocked. */
8491 need_switch_win = !(tp == curtab && win == curwin);
8492 if (!need_switch_win
8493 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
8494#endif
8495 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008496 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008498 if (varname[1] == NUL)
8499 {
8500 /* get all window-local options in a dict */
8501 dict_T *opts = get_winbuf_options(FALSE);
8502
8503 if (opts != NULL)
8504 {
8505 rettv->v_type = VAR_DICT;
8506 rettv->vval.v_dict = opts;
8507 ++opts->dv_refcount;
8508 done = TRUE;
8509 }
8510 }
8511 else if (get_option_tv(&varname, rettv, 1) == OK)
8512 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 done = TRUE;
8514 }
8515 else
8516 {
8517 /* Look up the variable. */
8518 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8519 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8520 varname, FALSE);
8521 if (v != NULL)
8522 {
8523 copy_tv(&v->di_tv, rettv);
8524 done = TRUE;
8525 }
8526 }
8527 }
8528
8529#ifdef FEAT_WINDOWS
8530 if (need_switch_win)
8531 /* restore previous notion of curwin */
8532 restore_win(oldcurwin, oldtabpage, TRUE);
8533#endif
8534 }
8535
8536 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8537 /* use the default return value */
8538 copy_tv(&argvars[off + 2], rettv);
8539
8540 --emsg_off;
8541}
8542
8543/*
8544 * "setwinvar()" and "settabwinvar()" functions
8545 */
8546 void
8547setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8548{
8549 win_T *win;
8550#ifdef FEAT_WINDOWS
8551 win_T *save_curwin;
8552 tabpage_T *save_curtab;
8553 int need_switch_win;
8554#endif
8555 char_u *varname, *winvarname;
8556 typval_T *varp;
8557 char_u nbuf[NUMBUFLEN];
8558 tabpage_T *tp = NULL;
8559
8560 if (check_restricted() || check_secure())
8561 return;
8562
8563#ifdef FEAT_WINDOWS
8564 if (off == 1)
8565 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
8566 else
8567 tp = curtab;
8568#endif
8569 win = find_win_by_nr(&argvars[off], tp);
8570 varname = get_tv_string_chk(&argvars[off + 1]);
8571 varp = &argvars[off + 2];
8572
8573 if (win != NULL && varname != NULL && varp != NULL)
8574 {
8575#ifdef FEAT_WINDOWS
8576 need_switch_win = !(tp == curtab && win == curwin);
8577 if (!need_switch_win
8578 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
8579#endif
8580 {
8581 if (*varname == '&')
8582 {
8583 long numval;
8584 char_u *strval;
8585 int error = FALSE;
8586
8587 ++varname;
8588 numval = (long)get_tv_number_chk(varp, &error);
8589 strval = get_tv_string_buf_chk(varp, nbuf);
8590 if (!error && strval != NULL)
8591 set_option_value(varname, numval, strval, OPT_LOCAL);
8592 }
8593 else
8594 {
8595 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8596 if (winvarname != NULL)
8597 {
8598 STRCPY(winvarname, "w:");
8599 STRCPY(winvarname + 2, varname);
8600 set_var(winvarname, varp, TRUE);
8601 vim_free(winvarname);
8602 }
8603 }
8604 }
8605#ifdef FEAT_WINDOWS
8606 if (need_switch_win)
8607 restore_win(save_curwin, save_curtab, TRUE);
8608#endif
8609 }
8610}
8611
8612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8614 * "arg" points to the "&" or '+' when called, to "option" when returning.
8615 * Returns NULL when no option name found. Otherwise pointer to the char
8616 * after the option name.
8617 */
8618 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008619find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 char_u *p = *arg;
8622
8623 ++p;
8624 if (*p == 'g' && p[1] == ':')
8625 {
8626 *opt_flags = OPT_GLOBAL;
8627 p += 2;
8628 }
8629 else if (*p == 'l' && p[1] == ':')
8630 {
8631 *opt_flags = OPT_LOCAL;
8632 p += 2;
8633 }
8634 else
8635 *opt_flags = 0;
8636
8637 if (!ASCII_ISALPHA(*p))
8638 return NULL;
8639 *arg = p;
8640
8641 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8642 p += 4; /* termcap option */
8643 else
8644 while (ASCII_ISALPHA(*p))
8645 ++p;
8646 return p;
8647}
8648
8649/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008650 * Return the autoload script name for a function or variable name.
8651 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008653 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008654autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008655{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008656 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008657 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008658
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008659 /* Get the script file name: replace '#' with '/', append ".vim". */
8660 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8661 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008662 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008663 STRCPY(scriptname, "autoload/");
8664 STRCAT(scriptname, name);
8665 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8666 STRCAT(scriptname, ".vim");
8667 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8668 *p = '/';
8669 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008670}
8671
8672/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008673 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008674 * Return TRUE if a package was loaded.
8675 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008677script_autoload(
8678 char_u *name,
8679 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008680{
8681 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008682 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008683 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008684 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008685
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008686 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008687 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008688 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008689 return FALSE;
8690
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008691 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008692
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008693 /* Find the name in the list of previously loaded package names. Skip
8694 * "autoload/", it's always the same. */
8695 for (i = 0; i < ga_loaded.ga_len; ++i)
8696 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8697 break;
8698 if (!reload && i < ga_loaded.ga_len)
8699 ret = FALSE; /* was loaded already */
8700 else
8701 {
8702 /* Remember the name if it wasn't loaded already. */
8703 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8704 {
8705 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8706 tofree = NULL;
8707 }
8708
8709 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008710 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008711 ret = TRUE;
8712 }
8713
8714 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008715 return ret;
8716}
8717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8719typedef enum
8720{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008721 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8722 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8723 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724} var_flavour_T;
8725
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008726static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008729var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 char_u *p = varname;
8732
8733 if (ASCII_ISUPPER(*p))
8734 {
8735 while (*(++p))
8736 if (ASCII_ISLOWER(*p))
8737 return VAR_FLAVOUR_SESSION;
8738 return VAR_FLAVOUR_VIMINFO;
8739 }
8740 else
8741 return VAR_FLAVOUR_DEFAULT;
8742}
8743#endif
8744
8745#if defined(FEAT_VIMINFO) || defined(PROTO)
8746/*
8747 * Restore global vars that start with a capital from the viminfo file
8748 */
8749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008753 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T tv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008755 void *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
8757 if (!writing && (find_viminfo_parameter('!') != NULL))
8758 {
8759 tab = vim_strchr(virp->vir_line + 1, '\t');
8760 if (tab != NULL)
8761 {
8762 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008763 switch (*tab)
8764 {
8765 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008766#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008767 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008768#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008769 case 'D': type = VAR_DICT; break;
8770 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008771 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
8774 tab = vim_strchr(tab, '\t');
8775 if (tab != NULL)
8776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008777 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008778 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008779 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782 else if (type == VAR_FLOAT)
8783 (void)string2float(tab + 1, &tv.vval.v_float);
8784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008786 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008787 if (type == VAR_DICT || type == VAR_LIST)
8788 {
8789 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
8790
8791 if (etv == NULL)
8792 /* Failed to parse back the dict or list, use it as a
8793 * string. */
8794 tv.v_type = VAR_STRING;
8795 else
8796 {
8797 vim_free(tv.vval.v_string);
8798 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01008799 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008800 }
8801 }
8802
Bram Moolenaarb20e3342016-01-18 23:29:01 +01008803 /* when in a function use global variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008804 save_funccal = clear_current_funccal();
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008805 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008806 restore_current_funccal(save_funccal);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008807
8808 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008809 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008810 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
8811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
8813 }
8814 }
8815
8816 return viminfo_readline(virp);
8817}
8818
8819/*
8820 * Write global vars that start with a capital to the viminfo file
8821 */
8822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008823write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 hashitem_T *hi;
8826 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008827 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01008828 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008831 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 if (find_viminfo_parameter('!') == NULL)
8834 return;
8835
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008836 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00008837
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008838 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008841 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008843 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 this_var = HI2DI(hi);
8845 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00008848 {
8849 case VAR_STRING: s = "STR"; break;
8850 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008851 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008852 case VAR_DICT: s = "DIC"; break;
8853 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008854 case VAR_SPECIAL: s = "XPL"; break;
8855
8856 case VAR_UNKNOWN:
8857 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008858 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008859 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008860 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008861 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00008862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008864 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 if (p != NULL)
8866 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00008867 vim_free(tofree);
8868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870 }
8871}
8872#endif
8873
8874#if defined(FEAT_SESSION) || defined(PROTO)
8875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 hashitem_T *hi;
8879 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00008880 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 char_u *p, *t;
8882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008883 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008886 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008888 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008889 this_var = HI2DI(hi);
8890 if ((this_var->di_tv.v_type == VAR_NUMBER
8891 || this_var->di_tv.v_type == VAR_STRING)
8892 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00008893 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008894 /* Escape special characters with a backslash. Turn a LF and
8895 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00008897 (char_u *)"\\\"\n\r");
8898 if (p == NULL) /* out of memory */
8899 break;
8900 for (t = p; *t != NUL; ++t)
8901 if (*t == '\n')
8902 *t = 'n';
8903 else if (*t == '\r')
8904 *t = 'r';
8905 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00008906 this_var->di_key,
8907 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8908 : ' ',
8909 p,
8910 (this_var->di_tv.v_type == VAR_STRING) ? '"'
8911 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00008912 || put_eol(fd) == FAIL)
8913 {
8914 vim_free(p);
8915 return FAIL;
8916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 vim_free(p);
8918 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008919#ifdef FEAT_FLOAT
8920 else if (this_var->di_tv.v_type == VAR_FLOAT
8921 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
8922 {
8923 float_T f = this_var->di_tv.vval.v_float;
8924 int sign = ' ';
8925
8926 if (f < 0)
8927 {
8928 f = -f;
8929 sign = '-';
8930 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01008931 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008932 this_var->di_key, sign, f) < 0)
8933 || put_eol(fd) == FAIL)
8934 return FAIL;
8935 }
8936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 }
8938 }
8939 return OK;
8940}
8941#endif
8942
Bram Moolenaar661b1822005-07-28 22:36:45 +00008943/*
8944 * Display script name where an item was last set.
8945 * Should only be invoked when 'verbose' is non-zero.
8946 */
8947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008948last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +00008949{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008950 char_u *p;
8951
Bram Moolenaar661b1822005-07-28 22:36:45 +00008952 if (scriptID != 0)
8953 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00008954 p = home_replace_save(NULL, get_scriptname(scriptID));
8955 if (p != NULL)
8956 {
8957 verbose_enter();
8958 MSG_PUTS(_("\n\tLast set from "));
8959 MSG_PUTS(p);
8960 vim_free(p);
8961 verbose_leave();
8962 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00008963 }
8964}
8965
Bram Moolenaar53744302015-07-17 17:38:22 +02008966/* reset v:option_new, v:option_old and v:option_type */
8967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02008969{
8970 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
8971 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
8972 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
8973}
8974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975/*
8976 * Prepare "gap" for an assert error and add the sourcing position.
8977 */
8978 void
8979prepare_assert_error(garray_T *gap)
8980{
8981 char buf[NUMBUFLEN];
8982
8983 ga_init2(gap, 1, 100);
8984 if (sourcing_name != NULL)
8985 {
8986 ga_concat(gap, sourcing_name);
8987 if (sourcing_lnum > 0)
8988 ga_concat(gap, (char_u *)" ");
8989 }
8990 if (sourcing_lnum > 0)
8991 {
8992 sprintf(buf, "line %ld", (long)sourcing_lnum);
8993 ga_concat(gap, (char_u *)buf);
8994 }
8995 if (sourcing_name != NULL || sourcing_lnum > 0)
8996 ga_concat(gap, (char_u *)": ");
8997}
8998
8999/*
9000 * Add an assert error to v:errors.
9001 */
9002 void
9003assert_error(garray_T *gap)
9004{
9005 struct vimvar *vp = &vimvars[VV_ERRORS];
9006
9007 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9008 /* Make sure v:errors is a list. */
9009 set_vim_var_list(VV_ERRORS, list_alloc());
9010 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9011}
9012
9013 void
9014assert_equal_common(typval_T *argvars, assert_type_T atype)
9015{
9016 garray_T ga;
9017
9018 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9019 != (atype == ASSERT_EQUAL))
9020 {
9021 prepare_assert_error(&ga);
9022 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9023 atype);
9024 assert_error(&ga);
9025 ga_clear(&ga);
9026 }
9027}
9028
9029 void
9030assert_match_common(typval_T *argvars, assert_type_T atype)
9031{
9032 garray_T ga;
9033 char_u buf1[NUMBUFLEN];
9034 char_u buf2[NUMBUFLEN];
9035 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9036 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9037
9038 if (pat == NULL || text == NULL)
9039 EMSG(_(e_invarg));
9040 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9041 {
9042 prepare_assert_error(&ga);
9043 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9044 atype);
9045 assert_error(&ga);
9046 ga_clear(&ga);
9047 }
9048}
9049
Bram Moolenaar61c04492016-07-23 15:35:35 +02009050 void
9051assert_inrange(typval_T *argvars)
9052{
9053 garray_T ga;
9054 int error = FALSE;
9055 varnumber_T lower = get_tv_number_chk(&argvars[0], &error);
9056 varnumber_T upper = get_tv_number_chk(&argvars[1], &error);
9057 varnumber_T actual = get_tv_number_chk(&argvars[2], &error);
9058 char_u *tofree;
9059 char msg[200];
9060 char_u numbuf[NUMBUFLEN];
9061
9062 if (error)
9063 return;
9064 if (actual < lower || actual > upper)
9065 {
9066 prepare_assert_error(&ga);
9067 if (argvars[3].v_type != VAR_UNKNOWN)
9068 {
9069 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9070 vim_free(tofree);
9071 }
9072 else
9073 {
9074 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
9075 (long)lower, (long)upper, (long)actual);
9076 ga_concat(&ga, (char_u *)msg);
9077 }
9078 assert_error(&ga);
9079 ga_clear(&ga);
9080 }
9081}
9082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083/*
9084 * Common for assert_true() and assert_false().
9085 */
9086 void
9087assert_bool(typval_T *argvars, int isTrue)
9088{
9089 int error = FALSE;
9090 garray_T ga;
9091
9092 if (argvars[0].v_type == VAR_SPECIAL
9093 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
9094 return;
9095 if (argvars[0].v_type != VAR_NUMBER
9096 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9097 || error)
9098 {
9099 prepare_assert_error(&ga);
9100 fill_assert_error(&ga, &argvars[1],
9101 (char_u *)(isTrue ? "True" : "False"),
9102 NULL, &argvars[0], ASSERT_OTHER);
9103 assert_error(&ga);
9104 ga_clear(&ga);
9105 }
9106}
9107
9108 void
9109assert_exception(typval_T *argvars)
9110{
9111 garray_T ga;
9112 char_u *error = get_tv_string_chk(&argvars[0]);
9113
9114 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9115 {
9116 prepare_assert_error(&ga);
9117 ga_concat(&ga, (char_u *)"v:exception is not set");
9118 assert_error(&ga);
9119 ga_clear(&ga);
9120 }
9121 else if (error != NULL
9122 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9123 {
9124 prepare_assert_error(&ga);
9125 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9126 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9127 assert_error(&ga);
9128 ga_clear(&ga);
9129 }
9130}
9131
9132 void
9133assert_fails(typval_T *argvars)
9134{
9135 char_u *cmd = get_tv_string_chk(&argvars[0]);
9136 garray_T ga;
9137
9138 called_emsg = FALSE;
9139 suppress_errthrow = TRUE;
9140 emsg_silent = TRUE;
9141 do_cmdline_cmd(cmd);
9142 if (!called_emsg)
9143 {
9144 prepare_assert_error(&ga);
9145 ga_concat(&ga, (char_u *)"command did not fail: ");
9146 ga_concat(&ga, cmd);
9147 assert_error(&ga);
9148 ga_clear(&ga);
9149 }
9150 else if (argvars[1].v_type != VAR_UNKNOWN)
9151 {
9152 char_u buf[NUMBUFLEN];
9153 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9154
9155 if (error == NULL
9156 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9157 {
9158 prepare_assert_error(&ga);
9159 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9160 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9161 assert_error(&ga);
9162 ga_clear(&ga);
9163 }
9164 }
9165
9166 called_emsg = FALSE;
9167 suppress_errthrow = FALSE;
9168 emsg_silent = FALSE;
9169 emsg_on_display = FALSE;
9170 set_vim_var_string(VV_ERRMSG, NULL, 0);
9171}
9172
9173/*
9174 * Append "str" to "gap", escaping unprintable characters.
9175 * Changes NL to \n, CR to \r, etc.
9176 */
9177 static void
9178ga_concat_esc(garray_T *gap, char_u *str)
9179{
9180 char_u *p;
9181 char_u buf[NUMBUFLEN];
9182
9183 if (str == NULL)
9184 {
9185 ga_concat(gap, (char_u *)"NULL");
9186 return;
9187 }
9188
9189 for (p = str; *p != NUL; ++p)
9190 switch (*p)
9191 {
9192 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9193 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9194 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9195 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9196 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9197 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9198 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9199 default:
9200 if (*p < ' ')
9201 {
9202 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9203 ga_concat(gap, buf);
9204 }
9205 else
9206 ga_append(gap, *p);
9207 break;
9208 }
9209}
9210
9211/*
9212 * Fill "gap" with information about an assert error.
9213 */
9214 void
9215fill_assert_error(
9216 garray_T *gap,
9217 typval_T *opt_msg_tv,
9218 char_u *exp_str,
9219 typval_T *exp_tv,
9220 typval_T *got_tv,
9221 assert_type_T atype)
9222{
9223 char_u numbuf[NUMBUFLEN];
9224 char_u *tofree;
9225
9226 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9227 {
9228 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9229 vim_free(tofree);
9230 }
9231 else
9232 {
9233 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9234 ga_concat(gap, (char_u *)"Pattern ");
9235 else
9236 ga_concat(gap, (char_u *)"Expected ");
9237 if (exp_str == NULL)
9238 {
9239 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9240 vim_free(tofree);
9241 }
9242 else
9243 ga_concat_esc(gap, exp_str);
9244 if (atype == ASSERT_MATCH)
9245 ga_concat(gap, (char_u *)" does not match ");
9246 else if (atype == ASSERT_NOTMATCH)
9247 ga_concat(gap, (char_u *)" does match ");
9248 else if (atype == ASSERT_NOTEQUAL)
9249 ga_concat(gap, (char_u *)" differs from ");
9250 else
9251 ga_concat(gap, (char_u *)" but got ");
9252 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9253 vim_free(tofree);
9254 }
9255}
9256
Bram Moolenaar53744302015-07-17 17:38:22 +02009257
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258#endif /* FEAT_EVAL */
9259
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009261#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262
9263#ifdef WIN3264
9264/*
9265 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9266 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009267static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
9268static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
9269static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270
9271/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009272 * Get the short path (8.3) for the filename in "fnamep".
9273 * Only works for a valid file name.
9274 * When the path gets longer "fnamep" is changed and the allocated buffer
9275 * is put in "bufp".
9276 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9277 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 */
9279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009280get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009282 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 char_u *newbuf;
9284
9285 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009286 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 if (l > len - 1)
9288 {
9289 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009290 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 newbuf = vim_strnsave(*fnamep, l);
9292 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
9295 vim_free(*bufp);
9296 *fnamep = *bufp = newbuf;
9297
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009298 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009299 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 }
9301
9302 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009303 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304}
9305
9306/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009307 * Get the short path (8.3) for the filename in "fname". The converted
9308 * path is returned in "bufp".
9309 *
9310 * Some of the directories specified in "fname" may not exist. This function
9311 * will shorten the existing directories at the beginning of the path and then
9312 * append the remaining non-existing path.
9313 *
9314 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009315 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009316 * bufp - Pointer to an allocated buffer for the filename.
9317 * fnamelen - Length of the filename pointed to by fname
9318 *
9319 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 */
9321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009322shortpath_for_invalid_fname(
9323 char_u **fname,
9324 char_u **bufp,
9325 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009327 char_u *short_fname, *save_fname, *pbuf_unused;
9328 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009330 int old_len, len;
9331 int new_len, sfx_len;
9332 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
9334 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009335 old_len = *fnamelen;
9336 save_fname = vim_strnsave(*fname, old_len);
9337 pbuf_unused = NULL;
9338 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009340 endp = save_fname + old_len - 1; /* Find the end of the copy */
9341 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009343 /*
9344 * Try shortening the supplied path till it succeeds by removing one
9345 * directory at a time from the tail of the path.
9346 */
9347 len = 0;
9348 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009350 /* go back one path-separator */
9351 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9352 --endp;
9353 if (endp <= save_fname)
9354 break; /* processed the complete path */
9355
9356 /*
9357 * Replace the path separator with a NUL and try to shorten the
9358 * resulting path.
9359 */
9360 ch = *endp;
9361 *endp = 0;
9362 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009363 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009364 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9365 {
9366 retval = FAIL;
9367 goto theend;
9368 }
9369 *endp = ch; /* preserve the string */
9370
9371 if (len > 0)
9372 break; /* successfully shortened the path */
9373
9374 /* failed to shorten the path. Skip the path separator */
9375 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 }
9377
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009378 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009380 /*
9381 * Succeeded in shortening the path. Now concatenate the shortened
9382 * path with the remaining path at the tail.
9383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009385 /* Compute the length of the new path. */
9386 sfx_len = (int)(save_endp - endp) + 1;
9387 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009389 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009391 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009393 /* There is not enough space in the currently allocated string,
9394 * copy it to a buffer big enough. */
9395 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009397 {
9398 retval = FAIL;
9399 goto theend;
9400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 }
9402 else
9403 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009404 /* Transfer short_fname to the main buffer (it's big enough),
9405 * unless get_short_pathname() did its work in-place. */
9406 *fname = *bufp = save_fname;
9407 if (short_fname != save_fname)
9408 vim_strncpy(save_fname, short_fname, len);
9409 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009411
9412 /* concat the not-shortened part of the path */
9413 vim_strncpy(*fname + len, endp, sfx_len);
9414 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009416
9417theend:
9418 vim_free(pbuf_unused);
9419 vim_free(save_fname);
9420
9421 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
9424/*
9425 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009426 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 */
9428 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429shortpath_for_partial(
9430 char_u **fnamep,
9431 char_u **bufp,
9432 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 int sepcount, len, tflen;
9435 char_u *p;
9436 char_u *pbuf, *tfname;
9437 int hasTilde;
9438
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009439 /* Count up the path separators from the RHS.. so we know which part
9440 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009442 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (vim_ispathsep(*p))
9444 ++sepcount;
9445
9446 /* Need full path first (use expand_env() to remove a "~/") */
9447 hasTilde = (**fnamep == '~');
9448 if (hasTilde)
9449 pbuf = tfname = expand_env_save(*fnamep);
9450 else
9451 pbuf = tfname = FullName_save(*fnamep, FALSE);
9452
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009453 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009455 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
9456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457
9458 if (len == 0)
9459 {
9460 /* Don't have a valid filename, so shorten the rest of the
9461 * path if we can. This CAN give us invalid 8.3 filenames, but
9462 * there's not a lot of point in guessing what it might be.
9463 */
9464 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009465 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
9466 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 }
9468
9469 /* Count the paths backward to find the beginning of the desired string. */
9470 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009471 {
9472#ifdef FEAT_MBYTE
9473 if (has_mbyte)
9474 p -= mb_head_off(tfname, p);
9475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 if (vim_ispathsep(*p))
9477 {
9478 if (sepcount == 0 || (hasTilde && sepcount == 1))
9479 break;
9480 else
9481 sepcount --;
9482 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (hasTilde)
9485 {
9486 --p;
9487 if (p >= tfname)
9488 *p = '~';
9489 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009490 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 }
9492 else
9493 ++p;
9494
9495 /* Copy in the string - p indexes into tfname - allocated at pbuf */
9496 vim_free(*bufp);
9497 *fnamelen = (int)STRLEN(p);
9498 *bufp = pbuf;
9499 *fnamep = p;
9500
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009501 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503#endif /* WIN3264 */
9504
9505/*
9506 * Adjust a filename, according to a string of modifiers.
9507 * *fnamep must be NUL terminated when called. When returning, the length is
9508 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009509 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 * When there is an error, *fnamep is set to NULL.
9511 */
9512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513modify_fname(
9514 char_u *src, /* string with modifiers */
9515 int *usedlen, /* characters after src that are used */
9516 char_u **fnamep, /* file name so far */
9517 char_u **bufp, /* buffer for allocated file name or NULL */
9518 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
9520 int valid = 0;
9521 char_u *tail;
9522 char_u *s, *p, *pbuf;
9523 char_u dirname[MAXPATHL];
9524 int c;
9525 int has_fullname = 0;
9526#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009527 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 int has_shortname = 0;
9529#endif
9530
9531repeat:
9532 /* ":p" - full path/file_name */
9533 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
9534 {
9535 has_fullname = 1;
9536
9537 valid |= VALID_PATH;
9538 *usedlen += 2;
9539
9540 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
9541 if ((*fnamep)[0] == '~'
9542#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
9543 && ((*fnamep)[1] == '/'
9544# ifdef BACKSLASH_IN_FILENAME
9545 || (*fnamep)[1] == '\\'
9546# endif
9547 || (*fnamep)[1] == NUL)
9548
9549#endif
9550 )
9551 {
9552 *fnamep = expand_env_save(*fnamep);
9553 vim_free(*bufp); /* free any allocated file name */
9554 *bufp = *fnamep;
9555 if (*fnamep == NULL)
9556 return -1;
9557 }
9558
9559 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009560 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 {
9562 if (vim_ispathsep(*p)
9563 && p[1] == '.'
9564 && (p[2] == NUL
9565 || vim_ispathsep(p[2])
9566 || (p[2] == '.'
9567 && (p[3] == NUL || vim_ispathsep(p[3])))))
9568 break;
9569 }
9570
9571 /* FullName_save() is slow, don't use it when not needed. */
9572 if (*p != NUL || !vim_isAbsName(*fnamep))
9573 {
9574 *fnamep = FullName_save(*fnamep, *p != NUL);
9575 vim_free(*bufp); /* free any allocated file name */
9576 *bufp = *fnamep;
9577 if (*fnamep == NULL)
9578 return -1;
9579 }
9580
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009581#ifdef WIN3264
9582# if _WIN32_WINNT >= 0x0500
9583 if (vim_strchr(*fnamep, '~') != NULL)
9584 {
9585 /* Expand 8.3 filename to full path. Needed to make sure the same
9586 * file does not have two different names.
9587 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
9588 p = alloc(_MAX_PATH + 1);
9589 if (p != NULL)
9590 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009591 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02009592 {
9593 vim_free(*bufp);
9594 *bufp = *fnamep = p;
9595 }
9596 else
9597 vim_free(p);
9598 }
9599 }
9600# endif
9601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 /* Append a path separator to a directory. */
9603 if (mch_isdir(*fnamep))
9604 {
9605 /* Make room for one or two extra characters. */
9606 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
9607 vim_free(*bufp); /* free any allocated file name */
9608 *bufp = *fnamep;
9609 if (*fnamep == NULL)
9610 return -1;
9611 add_pathsep(*fnamep);
9612 }
9613 }
9614
9615 /* ":." - path relative to the current directory */
9616 /* ":~" - path relative to the home directory */
9617 /* ":8" - shortname path - postponed till after */
9618 while (src[*usedlen] == ':'
9619 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
9620 {
9621 *usedlen += 2;
9622 if (c == '8')
9623 {
9624#ifdef WIN3264
9625 has_shortname = 1; /* Postpone this. */
9626#endif
9627 continue;
9628 }
9629 pbuf = NULL;
9630 /* Need full path first (use expand_env() to remove a "~/") */
9631 if (!has_fullname)
9632 {
9633 if (c == '.' && **fnamep == '~')
9634 p = pbuf = expand_env_save(*fnamep);
9635 else
9636 p = pbuf = FullName_save(*fnamep, FALSE);
9637 }
9638 else
9639 p = *fnamep;
9640
9641 has_fullname = 0;
9642
9643 if (p != NULL)
9644 {
9645 if (c == '.')
9646 {
9647 mch_dirname(dirname, MAXPATHL);
9648 s = shorten_fname(p, dirname);
9649 if (s != NULL)
9650 {
9651 *fnamep = s;
9652 if (pbuf != NULL)
9653 {
9654 vim_free(*bufp); /* free any allocated file name */
9655 *bufp = pbuf;
9656 pbuf = NULL;
9657 }
9658 }
9659 }
9660 else
9661 {
9662 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
9663 /* Only replace it when it starts with '~' */
9664 if (*dirname == '~')
9665 {
9666 s = vim_strsave(dirname);
9667 if (s != NULL)
9668 {
9669 *fnamep = s;
9670 vim_free(*bufp);
9671 *bufp = s;
9672 }
9673 }
9674 }
9675 vim_free(pbuf);
9676 }
9677 }
9678
9679 tail = gettail(*fnamep);
9680 *fnamelen = (int)STRLEN(*fnamep);
9681
9682 /* ":h" - head, remove "/file_name", can be repeated */
9683 /* Don't remove the first "/" or "c:\" */
9684 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
9685 {
9686 valid |= VALID_HEAD;
9687 *usedlen += 2;
9688 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009689 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009690 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 *fnamelen = (int)(tail - *fnamep);
9692#ifdef VMS
9693 if (*fnamelen > 0)
9694 *fnamelen += 1; /* the path separator is part of the path */
9695#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00009696 if (*fnamelen == 0)
9697 {
9698 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
9699 p = vim_strsave((char_u *)".");
9700 if (p == NULL)
9701 return -1;
9702 vim_free(*bufp);
9703 *bufp = *fnamep = tail = p;
9704 *fnamelen = 1;
9705 }
9706 else
9707 {
9708 while (tail > s && !after_pathsep(s, tail))
9709 mb_ptr_back(*fnamep, tail);
9710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 }
9712
9713 /* ":8" - shortname */
9714 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
9715 {
9716 *usedlen += 2;
9717#ifdef WIN3264
9718 has_shortname = 1;
9719#endif
9720 }
9721
9722#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +02009723 /*
9724 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 */
9726 if (has_shortname)
9727 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009728 /* Copy the string if it is shortened by :h and when it wasn't copied
9729 * yet, because we are going to change it in place. Avoids changing
9730 * the buffer name for "%:8". */
9731 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 {
9733 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02009734 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 return -1;
9736 vim_free(*bufp);
9737 *bufp = *fnamep = p;
9738 }
9739
9740 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02009741 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (!has_fullname && !vim_isAbsName(*fnamep))
9743 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009744 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 return -1;
9746 }
9747 else
9748 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009749 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750
Bram Moolenaardc935552011-08-17 15:23:23 +02009751 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009753 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 return -1;
9755
9756 if (l == 0)
9757 {
Bram Moolenaardc935552011-08-17 15:23:23 +02009758 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009760 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 return -1;
9762 }
9763 *fnamelen = l;
9764 }
9765 }
9766#endif /* WIN3264 */
9767
9768 /* ":t" - tail, just the basename */
9769 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
9770 {
9771 *usedlen += 2;
9772 *fnamelen -= (int)(tail - *fnamep);
9773 *fnamep = tail;
9774 }
9775
9776 /* ":e" - extension, can be repeated */
9777 /* ":r" - root, without extension, can be repeated */
9778 while (src[*usedlen] == ':'
9779 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
9780 {
9781 /* find a '.' in the tail:
9782 * - for second :e: before the current fname
9783 * - otherwise: The last '.'
9784 */
9785 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
9786 s = *fnamep - 2;
9787 else
9788 s = *fnamep + *fnamelen - 1;
9789 for ( ; s > tail; --s)
9790 if (s[0] == '.')
9791 break;
9792 if (src[*usedlen + 1] == 'e') /* :e */
9793 {
9794 if (s > tail)
9795 {
9796 *fnamelen += (int)(*fnamep - (s + 1));
9797 *fnamep = s + 1;
9798#ifdef VMS
9799 /* cut version from the extension */
9800 s = *fnamep + *fnamelen - 1;
9801 for ( ; s > *fnamep; --s)
9802 if (s[0] == ';')
9803 break;
9804 if (s > *fnamep)
9805 *fnamelen = s - *fnamep;
9806#endif
9807 }
9808 else if (*fnamep <= tail)
9809 *fnamelen = 0;
9810 }
9811 else /* :r */
9812 {
9813 if (s > tail) /* remove one extension */
9814 *fnamelen = (int)(s - *fnamep);
9815 }
9816 *usedlen += 2;
9817 }
9818
9819 /* ":s?pat?foo?" - substitute */
9820 /* ":gs?pat?foo?" - global substitute */
9821 if (src[*usedlen] == ':'
9822 && (src[*usedlen + 1] == 's'
9823 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
9824 {
9825 char_u *str;
9826 char_u *pat;
9827 char_u *sub;
9828 int sep;
9829 char_u *flags;
9830 int didit = FALSE;
9831
9832 flags = (char_u *)"";
9833 s = src + *usedlen + 2;
9834 if (src[*usedlen + 1] == 'g')
9835 {
9836 flags = (char_u *)"g";
9837 ++s;
9838 }
9839
9840 sep = *s++;
9841 if (sep)
9842 {
9843 /* find end of pattern */
9844 p = vim_strchr(s, sep);
9845 if (p != NULL)
9846 {
9847 pat = vim_strnsave(s, (int)(p - s));
9848 if (pat != NULL)
9849 {
9850 s = p + 1;
9851 /* find end of substitution */
9852 p = vim_strchr(s, sep);
9853 if (p != NULL)
9854 {
9855 sub = vim_strnsave(s, (int)(p - s));
9856 str = vim_strnsave(*fnamep, *fnamelen);
9857 if (sub != NULL && str != NULL)
9858 {
9859 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009860 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 if (s != NULL)
9862 {
9863 *fnamep = s;
9864 *fnamelen = (int)STRLEN(s);
9865 vim_free(*bufp);
9866 *bufp = s;
9867 didit = TRUE;
9868 }
9869 }
9870 vim_free(sub);
9871 vim_free(str);
9872 }
9873 vim_free(pat);
9874 }
9875 }
9876 /* after using ":s", repeat all the modifiers */
9877 if (didit)
9878 goto repeat;
9879 }
9880 }
9881
Bram Moolenaar26df0922014-02-23 23:39:13 +01009882 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
9883 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01009884 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01009885 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009886 if (c != NUL)
9887 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009888 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01009889 if (c != NUL)
9890 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01009891 if (p == NULL)
9892 return -1;
9893 vim_free(*bufp);
9894 *bufp = *fnamep = p;
9895 *fnamelen = (int)STRLEN(p);
9896 *usedlen += 2;
9897 }
9898
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 return valid;
9900}
9901
9902/*
9903 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009904 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 * "flags" can be "g" to do a global substitute.
9906 * Returns an allocated string, NULL for error.
9907 */
9908 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909do_string_sub(
9910 char_u *str,
9911 char_u *pat,
9912 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009913 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 int sublen;
9917 regmatch_T regmatch;
9918 int i;
9919 int do_all;
9920 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009921 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 garray_T ga;
9923 char_u *ret;
9924 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009925 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
9927 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
9928 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009929 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930
9931 ga_init2(&ga, 1, 200);
9932
9933 do_all = (flags[0] == 'g');
9934
9935 regmatch.rm_ic = p_ic;
9936 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9937 if (regmatch.regprog != NULL)
9938 {
9939 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01009940 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
9942 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01009943 /* Skip empty match except for first match. */
9944 if (regmatch.startp[0] == regmatch.endp[0])
9945 {
9946 if (zero_width == regmatch.startp[0])
9947 {
9948 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02009949 i = MB_PTR2LEN(tail);
9950 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
9951 (size_t)i);
9952 ga.ga_len += i;
9953 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01009954 continue;
9955 }
9956 zero_width = regmatch.startp[0];
9957 }
9958
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 /*
9960 * Get some space for a temporary buffer to do the substitution
9961 * into. It will contain:
9962 * - The text up to where the match is.
9963 * - The substituted text.
9964 * - The text after the match.
9965 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009966 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01009967 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
9969 {
9970 ga_clear(&ga);
9971 break;
9972 }
9973
9974 /* copy the text up to where the match is */
9975 i = (int)(regmatch.startp[0] - tail);
9976 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
9977 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009978 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 + ga.ga_len + i, TRUE, TRUE, FALSE);
9980 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02009981 tail = regmatch.endp[0];
9982 if (*tail == NUL)
9983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 if (!do_all)
9985 break;
9986 }
9987
9988 if (ga.ga_data != NULL)
9989 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
9990
Bram Moolenaar473de612013-06-08 18:19:48 +02009991 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 }
9993
9994 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
9995 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00009996 if (p_cpo == empty_option)
9997 p_cpo = save_cpo;
9998 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009999 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010000 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001
10002 return ret;
10003}
10004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005 static int
10006filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10007{
10008 typval_T rettv;
10009 typval_T argv[3];
10010 char_u buf[NUMBUFLEN];
10011 char_u *s;
10012 int retval = FAIL;
10013 int dummy;
10014
10015 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10016 argv[0] = vimvars[VV_KEY].vv_tv;
10017 argv[1] = vimvars[VV_VAL].vv_tv;
10018 if (expr->v_type == VAR_FUNC)
10019 {
10020 s = expr->vval.v_string;
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010021 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10022 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010023 goto theend;
10024 }
10025 else if (expr->v_type == VAR_PARTIAL)
10026 {
10027 partial_T *partial = expr->vval.v_partial;
10028
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010029 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010030 if (call_func(s, (int)STRLEN(s), &rettv, 2, argv, NULL,
10031 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010032 goto theend;
10033 }
10034 else
10035 {
10036 s = get_tv_string_buf_chk(expr, buf);
10037 if (s == NULL)
10038 goto theend;
10039 s = skipwhite(s);
10040 if (eval1(&s, &rettv, TRUE) == FAIL)
10041 goto theend;
10042 if (*s != NUL) /* check for trailing chars after expr */
10043 {
10044 EMSG2(_(e_invexpr2), s);
10045 goto theend;
10046 }
10047 }
10048 if (map)
10049 {
10050 /* map(): replace the list item value */
10051 clear_tv(tv);
10052 rettv.v_lock = 0;
10053 *tv = rettv;
10054 }
10055 else
10056 {
10057 int error = FALSE;
10058
10059 /* filter(): when expr is zero remove the item */
10060 *remp = (get_tv_number_chk(&rettv, &error) == 0);
10061 clear_tv(&rettv);
10062 /* On type error, nothing has been removed; return FAIL to stop the
10063 * loop. The error message was given by get_tv_number_chk(). */
10064 if (error)
10065 goto theend;
10066 }
10067 retval = OK;
10068theend:
10069 clear_tv(&vimvars[VV_VAL].vv_tv);
10070 return retval;
10071}
10072
10073
10074/*
10075 * Implementation of map() and filter().
10076 */
10077 void
10078filter_map(typval_T *argvars, typval_T *rettv, int map)
10079{
10080 typval_T *expr;
10081 listitem_T *li, *nli;
10082 list_T *l = NULL;
10083 dictitem_T *di;
10084 hashtab_T *ht;
10085 hashitem_T *hi;
10086 dict_T *d = NULL;
10087 typval_T save_val;
10088 typval_T save_key;
10089 int rem;
10090 int todo;
10091 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10092 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10093 : N_("filter() argument"));
10094 int save_did_emsg;
10095 int idx = 0;
10096
10097 if (argvars[0].v_type == VAR_LIST)
10098 {
10099 if ((l = argvars[0].vval.v_list) == NULL
10100 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
10101 return;
10102 }
10103 else if (argvars[0].v_type == VAR_DICT)
10104 {
10105 if ((d = argvars[0].vval.v_dict) == NULL
10106 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
10107 return;
10108 }
10109 else
10110 {
10111 EMSG2(_(e_listdictarg), ermsg);
10112 return;
10113 }
10114
10115 expr = &argvars[1];
10116 /* On type errors, the preceding call has already displayed an error
10117 * message. Avoid a misleading error message for an empty string that
10118 * was not passed as argument. */
10119 if (expr->v_type != VAR_UNKNOWN)
10120 {
10121 prepare_vimvar(VV_VAL, &save_val);
10122
10123 /* We reset "did_emsg" to be able to detect whether an error
10124 * occurred during evaluation of the expression. */
10125 save_did_emsg = did_emsg;
10126 did_emsg = FALSE;
10127
10128 prepare_vimvar(VV_KEY, &save_key);
10129 if (argvars[0].v_type == VAR_DICT)
10130 {
10131 vimvars[VV_KEY].vv_type = VAR_STRING;
10132
10133 ht = &d->dv_hashtab;
10134 hash_lock(ht);
10135 todo = (int)ht->ht_used;
10136 for (hi = ht->ht_array; todo > 0; ++hi)
10137 {
10138 if (!HASHITEM_EMPTY(hi))
10139 {
10140 int r;
10141
10142 --todo;
10143 di = HI2DI(hi);
10144 if (map &&
10145 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10146 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
10147 break;
10148 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10149 r = filter_map_one(&di->di_tv, expr, map, &rem);
10150 clear_tv(&vimvars[VV_KEY].vv_tv);
10151 if (r == FAIL || did_emsg)
10152 break;
10153 if (!map && rem)
10154 {
10155 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10156 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10157 break;
10158 dictitem_remove(d, di);
10159 }
10160 }
10161 }
10162 hash_unlock(ht);
10163 }
10164 else
10165 {
10166 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10167
10168 for (li = l->lv_first; li != NULL; li = nli)
10169 {
10170 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
10171 break;
10172 nli = li->li_next;
10173 vimvars[VV_KEY].vv_nr = idx;
10174 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10175 || did_emsg)
10176 break;
10177 if (!map && rem)
10178 listitem_remove(l, li);
10179 ++idx;
10180 }
10181 }
10182
10183 restore_vimvar(VV_KEY, &save_key);
10184 restore_vimvar(VV_VAL, &save_val);
10185
10186 did_emsg |= save_did_emsg;
10187 }
10188
10189 copy_tv(&argvars[0], rettv);
10190}
10191
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */