blob: 8e9d01308e57d67d5bfd90fa11fa842dc3591bd0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000035
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010036#define NAMESPACE_CHAR (char_u *)"abglstvw"
37
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020038static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000039#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000042 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them.
44 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000045static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000046
47/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000048 * When recursively copying lists and dicts we need to remember which ones we
49 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000050 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000051 */
52static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020053
Bram Moolenaard9fba312005-06-26 22:34:35 +000054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000055 * Array to hold the hashtab with variables local to each sourced script.
56 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000058typedef struct
59{
60 dictitem_T sv_var;
61 dict_T sv_dict;
62} scriptvar_T;
63
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020064static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
65#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
66#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68static int echo_attr = 0; /* attributes used for ":echo" */
69
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000071static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000074 * Info used by a ":for" loop.
75 */
Bram Moolenaar33570922005-01-25 22:26:29 +000076typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000077{
78 int fi_semicolon; /* TRUE if ending in '; var]' */
79 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000080 listwatch_T fi_lw; /* keep an eye on the item used. */
81 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 int fi_bi; /* index of blob */
83 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000084} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085
Bram Moolenaara7043832005-01-21 11:56:39 +000086
87/*
Bram Moolenaar33570922005-01-25 22:26:29 +000088 * Array to hold the value of v: variables.
89 * The value is in a dictitem, so that it can also be used in the v: scope.
90 * The reason to use this table anyway is for very quick access to the
91 * variables with the VV_ defines.
92 */
Bram Moolenaar33570922005-01-25 22:26:29 +000093
94/* values for vv_flags: */
95#define VV_COMPAT 1 /* compatible, also used without "v:" */
96#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000097#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000098
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010099#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000100
101static struct vimvar
102{
103 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100104 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000105 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
106} vimvars[VV_LEN] =
107{
108 /*
109 * The order here must match the VV_ defines in vim.h!
110 * Initializing a union does not work, leave tv.vval empty to get zero's.
111 */
112 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
113 {VV_NAME("count1", VAR_NUMBER), VV_RO},
114 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
115 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
116 {VV_NAME("warningmsg", VAR_STRING), 0},
117 {VV_NAME("statusmsg", VAR_STRING), 0},
118 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
119 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
120 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
121 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
122 {VV_NAME("termresponse", VAR_STRING), VV_RO},
123 {VV_NAME("fname", VAR_STRING), VV_RO},
124 {VV_NAME("lang", VAR_STRING), VV_RO},
125 {VV_NAME("lc_time", VAR_STRING), VV_RO},
126 {VV_NAME("ctype", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
128 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
129 {VV_NAME("fname_in", VAR_STRING), VV_RO},
130 {VV_NAME("fname_out", VAR_STRING), VV_RO},
131 {VV_NAME("fname_new", VAR_STRING), VV_RO},
132 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
133 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
134 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
136 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
137 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
138 {VV_NAME("progname", VAR_STRING), VV_RO},
139 {VV_NAME("servername", VAR_STRING), VV_RO},
140 {VV_NAME("dying", VAR_NUMBER), VV_RO},
141 {VV_NAME("exception", VAR_STRING), VV_RO},
142 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
143 {VV_NAME("register", VAR_STRING), VV_RO},
144 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
145 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000146 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
147 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000148 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000149 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
150 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200153 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000154 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
156 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000157 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000158 {VV_NAME("swapname", VAR_STRING), VV_RO},
159 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000160 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200161 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000162 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200163 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000164 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
165 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000166 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100168 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000169 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200171 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200172 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200173 {VV_NAME("option_new", VAR_STRING), VV_RO},
174 {VV_NAME("option_old", VAR_STRING), VV_RO},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200175 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
176 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
177 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200178 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100179 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100180 {VV_NAME("false", VAR_SPECIAL), VV_RO},
181 {VV_NAME("true", VAR_SPECIAL), VV_RO},
182 {VV_NAME("null", VAR_SPECIAL), VV_RO},
183 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100184 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200185 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200186 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
195 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100196 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200197 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
198 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200199 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200200 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
201 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
202 {VV_NAME("event", VAR_DICT), VV_RO},
203 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000204};
205
206/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000207#define vv_type vv_di.di_tv.v_type
208#define vv_nr vv_di.di_tv.vval.v_number
209#define vv_float vv_di.di_tv.vval.v_float
210#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000211#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200212#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100213#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000214#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000215
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200216static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define vimvarht vimvardict.dv_hashtab
218
Bram Moolenaar9937a052019-06-15 15:45:06 +0200219static void ex_let_const(exarg_T *eap, int is_const);
220static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
222static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void list_glob_vars(int *first);
224static void list_buf_vars(int *first);
225static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void list_vim_vars(int *first);
228static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200230static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
231static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
234static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
235static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
236static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int eval2(char_u **arg, typval_T *rettv, int evaluate);
239static int eval3(char_u **arg, typval_T *rettv, int evaluate);
240static int eval4(char_u **arg, typval_T *rettv, int evaluate);
241static int eval5(char_u **arg, typval_T *rettv, int evaluate);
242static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
243static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000244
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100245static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
246static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200251static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100252static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100253static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100254static void list_one_var(dictitem_T *v, char *prefix, int *first);
255static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200256static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100257static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100258static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260/* for VIM_VERSION_ defines */
261#include "version.h"
262
Bram Moolenaare21c1582019-03-02 11:57:09 +0100263/*
264 * Return "n1" divided by "n2", taking care of dividing by zero.
265 */
266 static varnumber_T
267num_divide(varnumber_T n1, varnumber_T n2)
268{
269 varnumber_T result;
270
271 if (n2 == 0) // give an error message?
272 {
273 if (n1 == 0)
274 result = VARNUM_MIN; // similar to NaN
275 else if (n1 < 0)
276 result = -VARNUM_MAX;
277 else
278 result = VARNUM_MAX;
279 }
280 else
281 result = n1 / n2;
282
283 return result;
284}
285
286/*
287 * Return "n1" modulus "n2", taking care of dividing by zero.
288 */
289 static varnumber_T
290num_modulus(varnumber_T n1, varnumber_T n2)
291{
292 // Give an error when n2 is 0?
293 return (n2 == 0) ? 0 : (n1 % n2);
294}
295
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100296
297#if defined(EBCDIC) || defined(PROTO)
298/*
299 * Compare struct fst by function name.
300 */
301 static int
302compare_func_name(const void *s1, const void *s2)
303{
304 struct fst *p1 = (struct fst *)s1;
305 struct fst *p2 = (struct fst *)s2;
306
307 return STRCMP(p1->f_name, p2->f_name);
308}
309
310/*
311 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100312 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100313 * On machines using EBCDIC we have to sort it.
314 */
315 static void
316sortFunctions(void)
317{
318 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
319
320 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
321}
322#endif
323
324
Bram Moolenaar33570922005-01-25 22:26:29 +0000325/*
326 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000327 */
328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000330{
Bram Moolenaar33570922005-01-25 22:26:29 +0000331 int i;
332 struct vimvar *p;
333
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200334 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
335 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200336 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000337 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000339
340 for (i = 0; i < VV_LEN; ++i)
341 {
342 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200343 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200346 getout(1);
347 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 STRCPY(p->vv_di.di_key, p->vv_name);
349 if (p->vv_flags & VV_RO)
350 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
351 else if (p->vv_flags & VV_RO_SBX)
352 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
353 else
354 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000355
356 /* add to v: scope dict, unless the value is not always available */
357 if (p->vv_type != VAR_UNKNOWN)
358 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000359 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000360 /* add to compat scope dict */
361 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100363 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200364 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100365
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100368 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100369 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100370 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371
372 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
373 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
374 set_vim_var_nr(VV_NONE, VVAL_NONE);
375 set_vim_var_nr(VV_NULL, VVAL_NULL);
376
Bram Moolenaarf562e722016-07-19 17:25:25 +0200377 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
378 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
379 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
380 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
381 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
382 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
383 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
384 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
385 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
386 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100387 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200388
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200389 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200390
391#ifdef EBCDIC
392 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100393 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200394 */
395 sortFunctions();
396#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000397}
398
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000399#if defined(EXITFREE) || defined(PROTO)
400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100401eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000402{
403 int i;
404 struct vimvar *p;
405
406 for (i = 0; i < VV_LEN; ++i)
407 {
408 p = &vimvars[i];
409 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100410 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000411 else if (p->vv_di.di_tv.v_type == VAR_LIST)
412 {
413 list_unref(p->vv_list);
414 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000415 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000416 }
417 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000418 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000419 hash_clear(&compat_hashtab);
420
Bram Moolenaard9fba312005-06-26 22:34:35 +0000421 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100422# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200423 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100424# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000425
426 /* global variables */
427 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000428
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000429 /* autoloaded script names */
430 ga_clear_strings(&ga_loaded);
431
Bram Moolenaarcca74132013-09-25 21:00:28 +0200432 /* Script-local variables. First clear all the variables and in a second
433 * loop free the scriptvar_T, because a variable in one script might hold
434 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200436 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200437 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200438 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200439 ga_clear(&ga_scripts);
440
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200441 // unreferenced lists and dicts
442 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200443
444 // functions not garbage collected
445 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000446}
447#endif
448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 * Set an internal variable to a string value. Creates the variable if it does
452 * not already exist.
453 */
454 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100455set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000457 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000458 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
460 val = vim_strsave(value);
461 if (val != NULL)
462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000463 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000464 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000466 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000467 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 }
469 }
470}
471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000472static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200473#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000474static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000475static char_u *redir_endp = NULL;
476static char_u *redir_varname = NULL;
477
478/*
479 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100480 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000481 * Returns OK if successfully completed the setup. FAIL otherwise.
482 */
483 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100484var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000485{
486 int save_emsg;
487 int err;
488 typval_T tv;
489
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000490 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000491 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100493 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000494 return FAIL;
495 }
496
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000497 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 redir_varname = vim_strsave(name);
499 if (redir_varname == NULL)
500 return FAIL;
501
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200502 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000503 if (redir_lval == NULL)
504 {
505 var_redir_stop();
506 return FAIL;
507 }
508
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000509 /* The output is stored in growarray "redir_ga" until redirection ends. */
510 ga_init2(&redir_ga, (int)sizeof(char), 500);
511
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000512 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100513 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000514 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000515 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
516 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200517 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000518 if (redir_endp != NULL && *redir_endp != NUL)
519 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100520 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100522 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000523 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000524 var_redir_stop();
525 return FAIL;
526 }
527
528 /* check if we can write to the variable: set it to or append an empty
529 * string */
530 save_emsg = did_emsg;
531 did_emsg = FALSE;
532 tv.v_type = VAR_STRING;
533 tv.vval.v_string = (char_u *)"";
534 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200535 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200537 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200538 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000540 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 if (err)
542 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000543 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000544 var_redir_stop();
545 return FAIL;
546 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000547
548 return OK;
549}
550
551/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000552 * Append "value[value_len]" to the variable set by var_redir_start().
553 * The actual appending is postponed until redirection ends, because the value
554 * appended may in fact be the string we write to, changing it may cause freed
555 * memory to be used:
556 * :redir => foo
557 * :let foo
558 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000559 */
560 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100561var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000563 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564
565 if (redir_lval == NULL)
566 return;
567
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000568 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000569 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000571 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000573 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000574 {
575 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000576 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000577 }
578 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580}
581
582/*
583 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000584 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585 */
586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000588{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000589 typval_T tv;
590
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200591 if (EVALCMD_BUSY)
592 {
593 redir_lval = NULL;
594 return;
595 }
596
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000597 if (redir_lval != NULL)
598 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000599 /* If there was no error: assign the text to the variable. */
600 if (redir_endp != NULL)
601 {
602 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
603 tv.v_type = VAR_STRING;
604 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200605 /* Call get_lval() again, if it's inside a Dict or List it may
606 * have changed. */
607 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100608 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200609 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200610 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
611 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200612 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000613 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000614
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000615 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100616 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000617
Bram Moolenaard23a8232018-02-10 18:45:26 +0100618 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000619 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100624eval_charconvert(
625 char_u *enc_from,
626 char_u *enc_to,
627 char_u *fname_from,
628 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 int err = FALSE;
631
632 set_vim_var_string(VV_CC_FROM, enc_from, -1);
633 set_vim_var_string(VV_CC_TO, enc_to, -1);
634 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
635 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
636 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
637 err = TRUE;
638 set_vim_var_string(VV_CC_FROM, NULL, -1);
639 set_vim_var_string(VV_CC_TO, NULL, -1);
640 set_vim_var_string(VV_FNAME_IN, NULL, -1);
641 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
642
643 if (err)
644 return FAIL;
645 return OK;
646}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100650eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int err = FALSE;
653
654 set_vim_var_string(VV_FNAME_IN, fname, -1);
655 set_vim_var_string(VV_CMDARG, args, -1);
656 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
657 err = TRUE;
658 set_vim_var_string(VV_FNAME_IN, NULL, -1);
659 set_vim_var_string(VV_CMDARG, NULL, -1);
660
661 if (err)
662 {
663 mch_remove(fname);
664 return FAIL;
665 }
666 return OK;
667}
668# endif
669
670# if defined(FEAT_DIFF) || defined(PROTO)
671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100672eval_diff(
673 char_u *origfile,
674 char_u *newfile,
675 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 int err = FALSE;
678
679 set_vim_var_string(VV_FNAME_IN, origfile, -1);
680 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
681 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
682 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
683 set_vim_var_string(VV_FNAME_IN, NULL, -1);
684 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
685 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
686}
687
688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100689eval_patch(
690 char_u *origfile,
691 char_u *difffile,
692 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
694 int err;
695
696 set_vim_var_string(VV_FNAME_IN, origfile, -1);
697 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
698 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
699 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
700 set_vim_var_string(VV_FNAME_IN, NULL, -1);
701 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
702 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
703}
704# endif
705
706/*
707 * Top level evaluation function, returning a boolean.
708 * Sets "error" to TRUE if there was an error.
709 * Return TRUE or FALSE.
710 */
711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712eval_to_bool(
713 char_u *arg,
714 int *error,
715 char_u **nextcmd,
716 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
Bram Moolenaar33570922005-01-25 22:26:29 +0000718 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200719 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721 if (skip)
722 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 else
726 {
727 *error = FALSE;
728 if (!skip)
729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100730 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 }
734 if (skip)
735 --emsg_skip;
736
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200737 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100740/*
741 * Call eval1() and give an error message if not done at a lower level.
742 */
743 static int
744eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
745{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100746 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100747 int ret;
748 int did_emsg_before = did_emsg;
749 int called_emsg_before = called_emsg;
750
751 ret = eval1(arg, rettv, evaluate);
752 if (ret == FAIL)
753 {
754 // Report the invalid expression unless the expression evaluation has
755 // been cancelled due to an aborting error, an interrupt, or an
756 // exception, or we already gave a more specific error.
757 // Also check called_emsg for when using assert_fails().
758 if (!aborting() && did_emsg == did_emsg_before
759 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100760 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100761 }
762 return ret;
763}
764
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200765 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100766eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
767{
768 char_u *s;
769 int dummy;
770 char_u buf[NUMBUFLEN];
771
772 if (expr->v_type == VAR_FUNC)
773 {
774 s = expr->vval.v_string;
775 if (s == NULL || *s == NUL)
776 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200777 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100778 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
779 return FAIL;
780 }
781 else if (expr->v_type == VAR_PARTIAL)
782 {
783 partial_T *partial = expr->vval.v_partial;
784
785 s = partial_name(partial);
786 if (s == NULL || *s == NUL)
787 return FAIL;
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200788 if (call_func(s, -1, rettv, argc, argv, NULL,
Bram Moolenaar48570482017-10-30 21:48:41 +0100789 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
790 return FAIL;
791 }
792 else
793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100794 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100795 if (s == NULL)
796 return FAIL;
797 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100798 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100799 return FAIL;
800 if (*s != NUL) /* check for trailing chars after expr */
801 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200802 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100804 return FAIL;
805 }
806 }
807 return OK;
808}
809
810/*
811 * Like eval_to_bool() but using a typval_T instead of a string.
812 * Works for string, funcref and partial.
813 */
814 int
815eval_expr_to_bool(typval_T *expr, int *error)
816{
817 typval_T rettv;
818 int res;
819
820 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
821 {
822 *error = TRUE;
823 return FALSE;
824 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100825 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100826 clear_tv(&rettv);
827 return res;
828}
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830/*
831 * Top level evaluation function, returning a string. If "skip" is TRUE,
832 * only parsing to "nextcmd" is done, without reporting errors. Return
833 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
834 */
835 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100836eval_to_string_skip(
837 char_u *arg,
838 char_u **nextcmd,
839 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 char_u *retval;
843
844 if (skip)
845 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 retval = NULL;
848 else
849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100850 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 if (skip)
854 --emsg_skip;
855
856 return retval;
857}
858
859/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000860 * Skip over an expression at "*pp".
861 * Return FAIL for an error, OK otherwise.
862 */
863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100864skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000865{
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867
868 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000870}
871
872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000874 * When "convert" is TRUE convert a List into a sequence of lines and convert
875 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Return pointer to allocated memory, or NULL for failure.
877 */
878 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879eval_to_string(
880 char_u *arg,
881 char_u **nextcmd,
882 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000886 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000887#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000888 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = NULL;
893 else
894 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000895 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000896 {
897 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000898 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200899 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200900 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200901 if (tv.vval.v_list->lv_len > 0)
902 ga_append(&ga, NL);
903 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000904 ga_append(&ga, NUL);
905 retval = (char_u *)ga.ga_data;
906 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000907#ifdef FEAT_FLOAT
908 else if (convert && tv.v_type == VAR_FLOAT)
909 {
910 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
911 retval = vim_strsave(numbuf);
912 }
913#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000914 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100915 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 return retval;
920}
921
922/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000923 * Call eval_to_string() without using current local variables and using
924 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
926 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100927eval_to_string_safe(
928 char_u *arg,
929 char_u **nextcmd,
930 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200933 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200935 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000936 if (use_sandbox)
937 ++sandbox;
938 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000939 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000940 if (use_sandbox)
941 --sandbox;
942 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200943 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 return retval;
945}
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947/*
948 * Top level evaluation function, returning a number.
949 * Evaluates "expr" silently.
950 * Returns -1 for an error.
951 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200952 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100953eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200956 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000957 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
959 ++emsg_off;
960
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000961 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 retval = -1;
963 else
964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100965 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 --emsg_off;
969
970 return retval;
971}
972
Bram Moolenaara40058a2005-07-11 22:42:07 +0000973/*
974 * Prepare v: variable "idx" to be used.
975 * Save the current typeval in "save_tv".
976 * When not used yet add the variable to the v: hashtable.
977 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100979prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000980{
981 *save_tv = vimvars[idx].vv_tv;
982 if (vimvars[idx].vv_type == VAR_UNKNOWN)
983 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
984}
985
986/*
987 * Restore v: variable "idx" to typeval "save_tv".
988 * When no longer defined, remove the variable from the v: hashtable.
989 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100991restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000992{
993 hashitem_T *hi;
994
Bram Moolenaara40058a2005-07-11 22:42:07 +0000995 vimvars[idx].vv_tv = *save_tv;
996 if (vimvars[idx].vv_type == VAR_UNKNOWN)
997 {
998 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
999 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001000 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001001 else
1002 hash_remove(&vimvarht, hi);
1003 }
1004}
1005
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001006#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001007/*
1008 * Evaluate an expression to a list with suggestions.
1009 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001010 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001011 */
1012 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001014{
1015 typval_T save_val;
1016 typval_T rettv;
1017 list_T *list = NULL;
1018 char_u *p = skipwhite(expr);
1019
1020 /* Set "v:val" to the bad word. */
1021 prepare_vimvar(VV_VAL, &save_val);
1022 vimvars[VV_VAL].vv_type = VAR_STRING;
1023 vimvars[VV_VAL].vv_str = badword;
1024 if (p_verbose == 0)
1025 ++emsg_off;
1026
1027 if (eval1(&p, &rettv, TRUE) == OK)
1028 {
1029 if (rettv.v_type != VAR_LIST)
1030 clear_tv(&rettv);
1031 else
1032 list = rettv.vval.v_list;
1033 }
1034
1035 if (p_verbose == 0)
1036 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001037 restore_vimvar(VV_VAL, &save_val);
1038
1039 return list;
1040}
1041
1042/*
1043 * "list" is supposed to contain two items: a word and a number. Return the
1044 * word in "pp" and the number as the return value.
1045 * Return -1 if anything isn't right.
1046 * Used to get the good word and score from the eval_spell_expr() result.
1047 */
1048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001050{
1051 listitem_T *li;
1052
1053 li = list->lv_first;
1054 if (li == NULL)
1055 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001056 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001057
1058 li = li->li_next;
1059 if (li == NULL)
1060 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001062}
1063#endif
1064
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001065/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001066 * Top level evaluation function.
1067 * Returns an allocated typval_T with the result.
1068 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069 */
1070 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001071eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001072{
1073 typval_T *tv;
1074
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001075 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001076 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001077 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001078
1079 return tv;
1080}
1081
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001084 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001085 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1086 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001087 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090call_vim_function(
1091 char_u *func,
1092 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001093 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001094 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001097 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001100 ret = call_func(func, -1, rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001102 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001103 if (ret == FAIL)
1104 clear_tv(rettv);
1105
1106 return ret;
1107}
1108
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001109/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001110 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001111 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001112 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1113 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001115 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001116call_func_retnr(
1117 char_u *func,
1118 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001119 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120{
1121 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001122 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001123
Bram Moolenaarded27a12018-08-01 19:06:03 +02001124 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001125 return -1;
1126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001127 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001128 clear_tv(&rettv);
1129 return retval;
1130}
1131
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001132#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001133 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1134
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001135# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001136/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001137 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001138 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001139 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1140 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141 */
1142 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001143call_func_retstr(
1144 char_u *func,
1145 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001146 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147{
1148 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001149 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001150
Bram Moolenaarded27a12018-08-01 19:06:03 +02001151 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152 return NULL;
1153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001154 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return retval;
1157}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001158# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001159
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001160/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001161 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001162 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1163 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001164 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165 */
1166 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001167call_func_retlist(
1168 char_u *func,
1169 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001170 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001171{
1172 typval_T rettv;
1173
Bram Moolenaarded27a12018-08-01 19:06:03 +02001174 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001175 return NULL;
1176
1177 if (rettv.v_type != VAR_LIST)
1178 {
1179 clear_tv(&rettv);
1180 return NULL;
1181 }
1182
1183 return rettv.vval.v_list;
1184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
Bram Moolenaar05159a02005-02-26 23:04:13 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#ifdef FEAT_FOLDING
1189/*
1190 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1191 * it in "*cp". Doesn't give error messages.
1192 */
1193 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar33570922005-01-25 22:26:29 +00001196 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001197 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001199 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1200 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001203 if (use_sandbox)
1204 ++sandbox;
1205 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = 0;
1209 else
1210 {
1211 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 if (tv.v_type == VAR_NUMBER)
1213 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001214 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 retval = 0;
1216 else
1217 {
1218 /* If the result is a string, check if there is a non-digit before
1219 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (!VIM_ISDIGIT(*s) && *s != '-')
1222 *cp = *s++;
1223 retval = atol((char *)s);
1224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 if (use_sandbox)
1229 --sandbox;
1230 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001232 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233}
1234#endif
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001237 * Get a list of lines from a HERE document. The here document is a list of
1238 * lines surrounded by a marker.
1239 * cmd << {marker}
1240 * {line1}
1241 * {line2}
1242 * ....
1243 * {marker}
1244 *
1245 * The {marker} is a string. If the optional 'trim' word is supplied before the
1246 * marker, then the leading indentation before the lines (matching the
1247 * indentation in the 'cmd' line) is stripped.
1248 * Returns a List with {lines} or NULL.
1249 */
1250 static list_T *
1251heredoc_get(exarg_T *eap, char_u *cmd)
1252{
1253 char_u *theline;
1254 char_u *marker;
1255 list_T *l;
1256 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001257 int marker_indent_len = 0;
1258 int text_indent_len = 0;
1259 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001260
1261 if (eap->getline == NULL)
1262 {
1263 emsg(_("E991: cannot use =<< here"));
1264 return NULL;
1265 }
1266
1267 // Check for the optional 'trim' word before the marker
1268 cmd = skipwhite(cmd);
1269 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1270 {
1271 cmd = skipwhite(cmd + 4);
1272
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001273 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001274 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001275 // the first line after the :let command line. To find the end marker
1276 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001277 p = *eap->cmdlinep;
1278 while (VIM_ISWHITE(*p))
1279 {
1280 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001281 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001282 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001283 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001284 }
1285
1286 // The marker is the next word. Default marker is "."
1287 if (*cmd != NUL && *cmd != '"')
1288 {
1289 marker = skipwhite(cmd);
1290 p = skiptowhite(marker);
1291 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1292 {
1293 emsg(_(e_trailing));
1294 return NULL;
1295 }
1296 *p = NUL;
1297 }
1298 else
1299 marker = (char_u *)".";
1300
1301 l = list_alloc();
1302 if (l == NULL)
1303 return NULL;
1304
1305 for (;;)
1306 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001307 int mi = 0;
1308 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001309
Bram Moolenaare96a2492019-06-25 04:12:16 +02001310 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001311 if (theline == NULL)
1312 {
1313 semsg(_("E990: Missing end marker '%s'"), marker);
1314 break;
1315 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001316
1317 // with "trim": skip the indent matching the :let line to find the
1318 // marker
1319 if (marker_indent_len > 0
1320 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1321 mi = marker_indent_len;
1322 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001323 {
1324 vim_free(theline);
1325 break;
1326 }
1327
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001328 if (text_indent_len == -1 && *theline != NUL)
1329 {
1330 // set the text indent from the first line.
1331 p = theline;
1332 text_indent_len = 0;
1333 while (VIM_ISWHITE(*p))
1334 {
1335 p++;
1336 text_indent_len++;
1337 }
1338 text_indent = vim_strnsave(theline, text_indent_len);
1339 }
1340 // with "trim": skip the indent matching the first line
1341 if (text_indent != NULL)
1342 for (ti = 0; ti < text_indent_len; ++ti)
1343 if (theline[ti] != text_indent[ti])
1344 break;
1345
1346 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001347 break;
1348 vim_free(theline);
1349 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001350 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001351
1352 return l;
1353}
1354
1355/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 * ":let" list all variable values
1357 * ":let var1 var2" list variable values
1358 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 * ":let var += expr" assignment command.
1360 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001361 * ":let var *= expr" assignment command.
1362 * ":let var /= expr" assignment command.
1363 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001365 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
1368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001371 ex_let_const(eap, FALSE);
1372}
1373
1374/*
1375 * ":const" list all variable values
1376 * ":const var1 var2" list variable values
1377 * ":const var = expr" assignment command.
1378 * ":const [var1, var2] = expr" unpack list.
1379 */
1380 void
1381ex_const(exarg_T *eap)
1382{
1383 ex_let_const(eap, TRUE);
1384}
1385
1386 static void
1387ex_let_const(exarg_T *eap, int is_const)
1388{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 int var_count = 0;
1394 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001396 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001397 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001398 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaardb552d602006-03-23 22:59:57 +00001400 argend = skip_var_list(arg, &var_count, &semicolon);
1401 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001402 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001403 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001404 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001405 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001406 concat = expr[0] == '.'
1407 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1408 || (expr[1] == '.' && expr[2] == '='));
1409 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1410 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001412 /*
1413 * ":let" without "=": list variables
1414 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001416 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001417 else if (expr[0] == '.')
1418 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001421 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001425 list_glob_vars(&first);
1426 list_buf_vars(&first);
1427 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001428 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001429 list_script_vars(&first);
1430 list_func_vars(&first);
1431 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 eap->nextcmd = check_nextcmd(arg);
1434 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001435 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1436 {
1437 list_T *l;
1438
1439 // HERE document
1440 l = heredoc_get(eap, expr + 3);
1441 if (l != NULL)
1442 {
1443 rettv_list_set(&rettv, l);
1444 op[0] = '=';
1445 op[1] = NUL;
1446 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001447 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001448 clear_tv(&rettv);
1449 }
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 else
1452 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 op[0] = '=';
1454 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001455 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001457 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001458 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001459 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001460 if (expr[0] == '.' && expr[1] == '.') // ..=
1461 ++expr;
1462 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001463 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001465 else
1466 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (eap->skip)
1469 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (eap->skip)
1472 {
1473 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 --emsg_skip;
1476 }
1477 else if (i != FAIL)
1478 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001480 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 }
1484}
1485
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001486/*
1487 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1488 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 * When "nextchars" is not NULL it points to a string with characters that
1490 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1491 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 * Returns OK or FAIL;
1493 */
1494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495ex_let_vars(
1496 char_u *arg_start,
1497 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001498 int copy, // copy values from "tv", don't move
1499 int semicolon, // from skip_var_list()
1500 int var_count, // from skip_var_list()
1501 int is_const, // lock variables for const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503{
1504 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001505 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001507 listitem_T *item;
1508 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509
1510 if (*arg != '[')
1511 {
1512 /*
1513 * ":let var = expr" or ":for var in list"
1514 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001515 if (ex_let_one(arg, tv, copy, is_const, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001516 return FAIL;
1517 return OK;
1518 }
1519
1520 /*
1521 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1522 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001523 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001525 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001526 return FAIL;
1527 }
1528
1529 i = list_len(l);
1530 if (semicolon == 0 && var_count < i)
1531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001532 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533 return FAIL;
1534 }
1535 if (var_count - semicolon > i)
1536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001537 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 return FAIL;
1539 }
1540
1541 item = l->lv_first;
1542 while (*arg != ']')
1543 {
1544 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001545 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
1546 (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 item = item->li_next;
1548 if (arg == NULL)
1549 return FAIL;
1550
1551 arg = skipwhite(arg);
1552 if (*arg == ';')
1553 {
1554 /* Put the rest of the list (may be empty) in the var after ';'.
1555 * Create a new list for this. */
1556 l = list_alloc();
1557 if (l == NULL)
1558 return FAIL;
1559 while (item != NULL)
1560 {
1561 list_append_tv(l, &item->li_tv);
1562 item = item->li_next;
1563 }
1564
1565 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001566 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 ltv.vval.v_list = l;
1568 l->lv_refcount = 1;
1569
Bram Moolenaar9937a052019-06-15 15:45:06 +02001570 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
1571 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001572 clear_tv(&ltv);
1573 if (arg == NULL)
1574 return FAIL;
1575 break;
1576 }
1577 else if (*arg != ',' && *arg != ']')
1578 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001579 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 return FAIL;
1581 }
1582 }
1583
1584 return OK;
1585}
1586
1587/*
1588 * Skip over assignable variable "var" or list of variables "[var, var]".
1589 * Used for ":let varvar = expr" and ":for varvar in expr".
1590 * For "[var, var]" increment "*var_count" for each variable.
1591 * for "[var, var; var]" set "semicolon".
1592 * Return NULL for an error.
1593 */
1594 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595skip_var_list(
1596 char_u *arg,
1597 int *var_count,
1598 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599{
1600 char_u *p, *s;
1601
1602 if (*arg == '[')
1603 {
1604 /* "[var, var]": find the matching ']'. */
1605 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001606 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607 {
1608 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1609 s = skip_var_one(p);
1610 if (s == p)
1611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001612 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 return NULL;
1614 }
1615 ++*var_count;
1616
1617 p = skipwhite(s);
1618 if (*p == ']')
1619 break;
1620 else if (*p == ';')
1621 {
1622 if (*semicolon == 1)
1623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001624 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 return NULL;
1626 }
1627 *semicolon = 1;
1628 }
1629 else if (*p != ',')
1630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001631 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 return NULL;
1633 }
1634 }
1635 return p + 1;
1636 }
1637 else
1638 return skip_var_one(arg);
1639}
1640
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001641/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001642 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001643 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001644 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001648 if (*arg == '@' && arg[1] != NUL)
1649 return arg + 2;
1650 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1651 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652}
1653
Bram Moolenaara7043832005-01-21 11:56:39 +00001654/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001655 * List variables for hashtab "ht" with prefix "prefix".
1656 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001657 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659list_hashtable_vars(
1660 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001661 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662 int empty,
1663 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001664{
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 hashitem_T *hi;
1666 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001667 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001668 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001669
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001670 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001671 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1672 {
1673 if (!HASHITEM_EMPTY(hi))
1674 {
1675 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001677
1678 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001679 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1680 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001681 if (message_filtered(buf))
1682 continue;
1683
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 if (empty || di->di_tv.v_type != VAR_STRING
1685 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001686 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001687 }
1688 }
1689}
1690
1691/*
1692 * List global variables.
1693 */
1694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001696{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001697 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001698}
1699
1700/*
1701 * List buffer variables.
1702 */
1703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001705{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001706 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001707}
1708
1709/*
1710 * List window variables.
1711 */
1712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001713list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001714{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001715 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001716}
1717
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001718/*
1719 * List tab page variables.
1720 */
1721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001723{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001724 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001725}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001726
Bram Moolenaara7043832005-01-21 11:56:39 +00001727/*
1728 * List Vim variables.
1729 */
1730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001733 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734}
1735
1736/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001737 * List script-local variables, if there is a script.
1738 */
1739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001740list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001741{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001742 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1743 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001744 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001745}
1746
1747/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001748 * List variables in "arg".
1749 */
1750 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752{
1753 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001754 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001756 char_u *name_start;
1757 char_u *arg_subsc;
1758 char_u *tofree;
1759 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760
1761 while (!ends_excmd(*arg) && !got_int)
1762 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001763 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001765 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001766 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001767 {
1768 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001769 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001770 break;
1771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001775 /* get_name_len() takes care of expanding curly braces */
1776 name_start = name = arg;
1777 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1778 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 /* This is mainly to keep test 49 working: when expanding
1781 * curly braces fails overrule the exception error message. */
1782 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001784 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001785 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001786 break;
1787 }
1788 error = TRUE;
1789 }
1790 else
1791 {
1792 if (tofree != NULL)
1793 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001794 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 else
1797 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001798 /* handle d.key, l[idx], f(expr) */
1799 arg_subsc = arg;
1800 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001801 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001804 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001806 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001807 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001808 case 'g': list_glob_vars(first); break;
1809 case 'b': list_buf_vars(first); break;
1810 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001811 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001812 case 'v': list_vim_vars(first); break;
1813 case 's': list_script_vars(first); break;
1814 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001816 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 }
1819 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001820 {
1821 char_u numbuf[NUMBUFLEN];
1822 char_u *tf;
1823 int c;
1824 char_u *s;
1825
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 c = *arg;
1828 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001829 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001831 tv.v_type,
1832 s == NULL ? (char_u *)"" : s,
1833 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001834 *arg = c;
1835 vim_free(tf);
1836 }
1837 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 }
1840 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841
1842 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001844
1845 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 }
1847
1848 return arg;
1849}
1850
1851/*
1852 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1853 * Returns a pointer to the char just after the var name.
1854 * Returns NULL if there is an error.
1855 */
1856 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001858 char_u *arg, // points to variable name
1859 typval_T *tv, // value to assign to variable
1860 int copy, // copy value from "tv"
1861 int is_const, // lock variable for const
1862 char_u *endchars, // valid chars after variable name or NULL
1863 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864{
1865 int c1;
1866 char_u *name;
1867 char_u *p;
1868 char_u *arg_end = NULL;
1869 int len;
1870 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872
1873 /*
1874 * ":let $VAR = expr": Set environment variable.
1875 */
1876 if (*arg == '$')
1877 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001878 if (is_const)
1879 {
1880 emsg(_("E996: Cannot lock an environment variable"));
1881 return NULL;
1882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 /* Find the end of the name. */
1884 ++arg;
1885 name = arg;
1886 len = get_env_len(&arg);
1887 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 else
1890 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001891 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001896 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 {
1898 c1 = name[len];
1899 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001900 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001901 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 {
1903 int mustfree = FALSE;
1904 char_u *s = vim_getenv(name, &mustfree);
1905
1906 if (s != NULL)
1907 {
1908 p = tofree = concat_str(s, p);
1909 if (mustfree)
1910 vim_free(s);
1911 }
1912 }
1913 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 if (STRICMP(name, "HOME") == 0)
1917 init_homedir();
1918 else if (didset_vim && STRICMP(name, "VIM") == 0)
1919 didset_vim = FALSE;
1920 else if (didset_vimruntime
1921 && STRICMP(name, "VIMRUNTIME") == 0)
1922 didset_vimruntime = FALSE;
1923 arg_end = arg;
1924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 }
1928 }
1929 }
1930
1931 /*
1932 * ":let &option = expr": Set option value.
1933 * ":let &l:option = expr": Set local option value.
1934 * ":let &g:option = expr": Set global option value.
1935 */
1936 else if (*arg == '&')
1937 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001938 if (is_const)
1939 {
1940 emsg(_("E996: Cannot lock an option"));
1941 return NULL;
1942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 /* Find the end of the name. */
1944 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 if (p == NULL || (endchars != NULL
1946 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001947 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 else
1949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 long n;
1951 int opt_type;
1952 long numval;
1953 char_u *stringval = NULL;
1954 char_u *s;
1955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 c1 = *p;
1957 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001959 n = (long)tv_get_number(tv);
1960 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001961 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 {
1963 opt_type = get_option_value(arg, &numval,
1964 &stringval, opt_flags);
1965 if ((opt_type == 1 && *op == '.')
1966 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001968 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001969 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 else
1972 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001973 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001975 switch (*op)
1976 {
1977 case '+': n = numval + n; break;
1978 case '-': n = numval - n; break;
1979 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001980 case '/': n = (long)num_divide(numval, n); break;
1981 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001984 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 {
1986 s = concat_str(stringval, s);
1987 vim_free(stringval);
1988 stringval = s;
1989 }
1990 }
1991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001992 if (s != NULL)
1993 {
1994 set_option_value(arg, n, s, opt_flags);
1995 arg_end = p;
1996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
2000 }
2001
2002 /*
2003 * ":let @r = expr": Set register contents.
2004 */
2005 else if (*arg == '@')
2006 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002007 if (is_const)
2008 {
2009 emsg(_("E996: Cannot lock a register"));
2010 return NULL;
2011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002013 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002014 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002017 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 else
2019 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002020 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 char_u *s;
2022
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002023 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002024 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002026 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 if (s != NULL)
2028 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002029 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 vim_free(s);
2031 }
2032 }
2033 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 arg_end = arg + 1;
2037 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002038 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 }
2040 }
2041
2042 /*
2043 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002044 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002046 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002048 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002050 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002053 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002055 else
2056 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002057 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 arg_end = p;
2059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 }
2063
2064 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002065 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066
2067 return arg_end;
2068}
2069
2070/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 * Get an lval: variable, Dict item or List item that can be assigned a value
2072 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2073 * "name.key", "name.key[expr]" etc.
2074 * Indexing only works if "name" is an existing List or Dictionary.
2075 * "name" points to the start of the name.
2076 * If "rettv" is not NULL it points to the value to be assigned.
2077 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2078 * wrong; must end in space or cmd separator.
2079 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002080 * flags:
2081 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002082 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002083 * GLV_NO_AUTOLOAD: do not use script autoloading
2084 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002090get_lval(
2091 char_u *name,
2092 typval_T *rettv,
2093 lval_T *lp,
2094 int unlet,
2095 int skip,
2096 int flags, /* GLV_ values */
2097 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 char_u *expr_start, *expr_end;
2101 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 dictitem_T *v;
2103 typval_T var1;
2104 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002110 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002114
2115 if (skip)
2116 {
2117 /* When skipping just find the end of the name. */
2118 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002119 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 }
2121
2122 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002123 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 if (expr_start != NULL)
2125 {
2126 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002127 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 && *p != '[' && *p != '.')
2129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 return NULL;
2132 }
2133
2134 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2135 if (lp->ll_exp_name == NULL)
2136 {
2137 /* Report an invalid expression in braces, unless the
2138 * expression evaluation has been cancelled due to an
2139 * aborting error, an interrupt, or an exception. */
2140 if (!aborting() && !quiet)
2141 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002142 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002143 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 return NULL;
2145 }
2146 }
2147 lp->ll_name = lp->ll_exp_name;
2148 }
2149 else
2150 lp->ll_name = name;
2151
2152 /* Without [idx] or .key we are done. */
2153 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2154 return p;
2155
2156 cc = *p;
2157 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002158 /* Only pass &ht when we would write to the variable, it prevents autoload
2159 * as well. */
2160 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2161 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002164 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 if (v == NULL)
2166 return NULL;
2167
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 /*
2169 * Loop until no more [idx] or .key is following.
2170 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002171 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002172 var1.v_type = VAR_UNKNOWN;
2173 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2177 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002178 && lp->ll_tv->vval.v_dict != NULL)
2179 && !(lp->ll_tv->v_type == VAR_BLOB
2180 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002189 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002192
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 len = -1;
2194 if (*p == '.')
2195 {
2196 key = p + 1;
2197 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2198 ;
2199 if (len == 0)
2200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 }
2205 p = key + len;
2206 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002207 else
2208 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002209 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002210 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 if (*p == ':')
2212 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002213 else
2214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002215 empty1 = FALSE;
2216 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002218 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002219 {
2220 /* not a number or string */
2221 clear_tv(&var1);
2222 return NULL;
2223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 }
2225
2226 /* Optionally get the second index [ :expr]. */
2227 if (*p == ':')
2228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002232 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002233 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002234 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002235 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002236 if (rettv != NULL
2237 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002238 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002239 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002240 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002243 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002244 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002246 }
2247 p = skipwhite(p + 1);
2248 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 else
2251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2254 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002255 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002258 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 {
2260 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002261 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 clear_tv(&var2);
2263 return NULL;
2264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002270
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002275 clear_tv(&var1);
2276 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 }
2279
2280 /* Skip to past ']'. */
2281 ++p;
2282 }
2283
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002285 {
2286 if (len == -1)
2287 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002289 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002290 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002291 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002292 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002294 }
2295 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 lp->ll_list = NULL;
2297 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002298 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002299
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002300 /* When assigning to a scope dictionary check that a function and
2301 * variable name is valid (only variable name unless it is l: or
2302 * g: dictionary). Disallow overwriting a builtin function. */
2303 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002304 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002305 int prevval;
2306 int wrong;
2307
2308 if (len != -1)
2309 {
2310 prevval = key[len];
2311 key[len] = NUL;
2312 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002313 else
2314 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002315 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2316 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002317 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002318 || !valid_varname(key);
2319 if (len != -1)
2320 key[len] = prevval;
2321 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002322 return NULL;
2323 }
2324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002327 // Can't add "v:" or "a:" variable.
2328 if (lp->ll_dict == &vimvardict
2329 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002332 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002333 return NULL;
2334 }
2335
Bram Moolenaar31b81602019-02-10 22:14:27 +01002336 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002340 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002341 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 }
2344 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 p = NULL;
2351 break;
2352 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002353 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002354 else if ((flags & GLV_READ_ONLY) == 0
2355 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002356 {
2357 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002358 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002359 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002360
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002361 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002364 else if (lp->ll_tv->v_type == VAR_BLOB)
2365 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002366 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2367
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002368 /*
2369 * Get the number and item for the only or first index of the List.
2370 */
2371 if (empty1)
2372 lp->ll_n1 = 0;
2373 else
2374 // is number or string
2375 lp->ll_n1 = (long)tv_get_number(&var1);
2376 clear_tv(&var1);
2377
2378 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002379 || lp->ll_n1 > bloblen
2380 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002381 {
2382 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002384 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002385 return NULL;
2386 }
2387 if (lp->ll_range && !lp->ll_empty2)
2388 {
2389 lp->ll_n2 = (long)tv_get_number(&var2);
2390 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002391 if (lp->ll_n2 < 0
2392 || lp->ll_n2 >= bloblen
2393 || lp->ll_n2 < lp->ll_n1)
2394 {
2395 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002396 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002397 return NULL;
2398 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002399 }
2400 lp->ll_blob = lp->ll_tv->vval.v_blob;
2401 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002402 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 else
2405 {
2406 /*
2407 * Get the number and item for the only or first index of the List.
2408 */
2409 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002412 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002413 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002414 clear_tv(&var1);
2415
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_list = lp->ll_tv->vval.v_list;
2418 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2419 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002421 if (lp->ll_n1 < 0)
2422 {
2423 lp->ll_n1 = 0;
2424 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2425 }
2426 }
2427 if (lp->ll_li == NULL)
2428 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002429 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002430 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002431 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
2434
2435 /*
2436 * May need to find the item or absolute index for the second
2437 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 * When no index given: "lp->ll_empty2" is TRUE.
2439 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002443 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002444 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002450 {
2451 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002452 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002454 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 }
2457
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2459 if (lp->ll_n1 < 0)
2460 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2461 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002462 {
2463 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002464 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002466 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002467 }
2468
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
2472
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002473 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 return p;
2475}
2476
2477/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002478 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002481clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482{
2483 vim_free(lp->ll_exp_name);
2484 vim_free(lp->ll_newkey);
2485}
2486
2487/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002488 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002490 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2491 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 */
2493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494set_var_lval(
2495 lval_T *lp,
2496 char_u *endp,
2497 typval_T *rettv,
2498 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002499 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002500 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501{
2502 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 listitem_T *ri;
2504 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505
2506 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002508 cc = *endp;
2509 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002510 if (lp->ll_blob != NULL)
2511 {
2512 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002513
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002514 if (op != NULL && *op != '=')
2515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002516 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002517 return;
2518 }
2519
2520 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2521 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002522 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002523
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002524 if (lp->ll_empty2)
2525 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2526
2527 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002529 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002530 return;
2531 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002532 if (lp->ll_empty2)
2533 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002534
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002535 ir = 0;
2536 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2537 blob_set(lp->ll_blob, il,
2538 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002539 }
2540 else
2541 {
2542 val = (int)tv_get_number_chk(rettv, &error);
2543 if (!error)
2544 {
2545 garray_T *gap = &lp->ll_blob->bv_ga;
2546
2547 // Allow for appending a byte. Setting a byte beyond
2548 // the end is an error otherwise.
2549 if (lp->ll_n1 < gap->ga_len
2550 || (lp->ll_n1 == gap->ga_len
2551 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2552 {
2553 blob_set(lp->ll_blob, lp->ll_n1, val);
2554 if (lp->ll_n1 == gap->ga_len)
2555 ++gap->ga_len;
2556 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002557 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002558 }
2559 }
2560 }
2561 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002563 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564
Bram Moolenaar9937a052019-06-15 15:45:06 +02002565 if (is_const)
2566 {
2567 emsg(_(e_cannot_mod));
2568 *endp = cc;
2569 return;
2570 }
2571
Bram Moolenaarff697e62019-02-12 22:28:33 +01002572 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002573 di = NULL;
2574 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2575 &tv, &di, TRUE, FALSE) == OK)
2576 {
2577 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002578 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2579 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002580 && tv_op(&tv, rettv, op) == OK)
2581 set_var(lp->ll_name, &tv, FALSE);
2582 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002585 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002586 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002587 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002589 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002590 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002591 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002592 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 else if (lp->ll_range)
2594 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002595 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002596 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002597
Bram Moolenaar9937a052019-06-15 15:45:06 +02002598 if (is_const)
2599 {
2600 emsg(_("E996: Cannot lock a range"));
2601 return;
2602 }
2603
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002604 /*
2605 * Check whether any of the list items is locked
2606 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002607 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002608 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002609 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002610 return;
2611 ri = ri->li_next;
2612 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2613 break;
2614 ll_li = ll_li->li_next;
2615 ++ll_n1;
2616 }
2617
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /*
2619 * Assign the List values to the list items.
2620 */
2621 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 if (op != NULL && *op != '=')
2624 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2625 else
2626 {
2627 clear_tv(&lp->ll_li->li_tv);
2628 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2629 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 ri = ri->li_next;
2631 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2632 break;
2633 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002636 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 ri = NULL;
2639 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 lp->ll_li = lp->ll_li->li_next;
2643 ++lp->ll_n1;
2644 }
2645 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 else if (lp->ll_empty2
2648 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002650 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 }
2652 else
2653 {
2654 /*
2655 * Assign to a List or Dictionary item.
2656 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002657 if (is_const)
2658 {
2659 emsg(_("E996: Cannot lock a list or dict"));
2660 return;
2661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_newkey != NULL)
2663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 if (op != NULL && *op != '=')
2665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002666 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 return;
2668 }
2669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002671 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (di == NULL)
2673 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002674 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2675 {
2676 vim_free(di);
2677 return;
2678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002681 else if (op != NULL && *op != '=')
2682 {
2683 tv_op(lp->ll_tv, rettv, op);
2684 return;
2685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 /*
2690 * Assign the value to the variable or list item.
2691 */
2692 if (copy)
2693 copy_tv(rettv, lp->ll_tv);
2694 else
2695 {
2696 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002697 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 }
2700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701}
2702
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002703/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002704 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2705 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 * Returns OK or FAIL.
2707 */
2708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002711 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 char_u numbuf[NUMBUFLEN];
2713 char_u *s;
2714
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002715 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2716 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2717 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 {
2719 switch (tv1->v_type)
2720 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002721 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722 case VAR_DICT:
2723 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002724 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002725 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002726 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002727 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 break;
2729
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002730 case VAR_BLOB:
2731 if (*op != '+' || tv2->v_type != VAR_BLOB)
2732 break;
2733 // BLOB += BLOB
2734 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2735 {
2736 blob_T *b1 = tv1->vval.v_blob;
2737 blob_T *b2 = tv2->vval.v_blob;
2738 int i, len = blob_len(b2);
2739 for (i = 0; i < len; i++)
2740 ga_append(&b1->bv_ga, blob_get(b2, i));
2741 }
2742 return OK;
2743
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002744 case VAR_LIST:
2745 if (*op != '+' || tv2->v_type != VAR_LIST)
2746 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002747 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2749 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2750 return OK;
2751
2752 case VAR_NUMBER:
2753 case VAR_STRING:
2754 if (tv2->v_type == VAR_LIST)
2755 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002756 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002757 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002758 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002759 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002760#ifdef FEAT_FLOAT
2761 if (tv2->v_type == VAR_FLOAT)
2762 {
2763 float_T f = n;
2764
Bram Moolenaarff697e62019-02-12 22:28:33 +01002765 if (*op == '%')
2766 break;
2767 switch (*op)
2768 {
2769 case '+': f += tv2->vval.v_float; break;
2770 case '-': f -= tv2->vval.v_float; break;
2771 case '*': f *= tv2->vval.v_float; break;
2772 case '/': f /= tv2->vval.v_float; break;
2773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002774 clear_tv(tv1);
2775 tv1->v_type = VAR_FLOAT;
2776 tv1->vval.v_float = f;
2777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002779#endif
2780 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002781 switch (*op)
2782 {
2783 case '+': n += tv_get_number(tv2); break;
2784 case '-': n -= tv_get_number(tv2); break;
2785 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002786 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2787 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002789 clear_tv(tv1);
2790 tv1->v_type = VAR_NUMBER;
2791 tv1->vval.v_number = n;
2792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 }
2794 else
2795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002796 if (tv2->v_type == VAR_FLOAT)
2797 break;
2798
Bram Moolenaarff697e62019-02-12 22:28:33 +01002799 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002800 s = tv_get_string(tv1);
2801 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 clear_tv(tv1);
2803 tv1->v_type = VAR_STRING;
2804 tv1->vval.v_string = s;
2805 }
2806 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002807
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002809#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002810 {
2811 float_T f;
2812
Bram Moolenaarff697e62019-02-12 22:28:33 +01002813 if (*op == '%' || *op == '.'
2814 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002815 && tv2->v_type != VAR_NUMBER
2816 && tv2->v_type != VAR_STRING))
2817 break;
2818 if (tv2->v_type == VAR_FLOAT)
2819 f = tv2->vval.v_float;
2820 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002822 switch (*op)
2823 {
2824 case '+': tv1->vval.v_float += f; break;
2825 case '-': tv1->vval.v_float -= f; break;
2826 case '*': tv1->vval.v_float *= f; break;
2827 case '/': tv1->vval.v_float /= f; break;
2828 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002829 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002830#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002831 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002832 }
2833 }
2834
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002835 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 return FAIL;
2837}
2838
2839/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840 * Evaluate the expression used in a ":for var in expr" command.
2841 * "arg" points to "var".
2842 * Set "*errp" to TRUE for an error, FALSE otherwise;
2843 * Return a pointer that holds the info. Null when there is an error.
2844 */
2845 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002846eval_for_line(
2847 char_u *arg,
2848 int *errp,
2849 char_u **nextcmdp,
2850 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851{
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002854 typval_T tv;
2855 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856
2857 *errp = TRUE; /* default: there is an error */
2858
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002859 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 if (fi == NULL)
2861 return NULL;
2862
2863 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2864 if (expr == NULL)
2865 return fi;
2866
2867 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002868 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002870 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871 return fi;
2872 }
2873
2874 if (skip)
2875 ++emsg_skip;
2876 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2877 {
2878 *errp = FALSE;
2879 if (!skip)
2880 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002881 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002882 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002883 l = tv.vval.v_list;
2884 if (l == NULL)
2885 {
2886 // a null list is like an empty list: do nothing
2887 clear_tv(&tv);
2888 }
2889 else
2890 {
2891 // No need to increment the refcount, it's already set for
2892 // the list being used in "tv".
2893 fi->fi_list = l;
2894 list_add_watch(l, &fi->fi_lw);
2895 fi->fi_lw.lw_item = l->lv_first;
2896 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002898 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002899 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002900 fi->fi_bi = 0;
2901 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002902 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002903 typval_T btv;
2904
2905 // Make a copy, so that the iteration still works when the
2906 // blob is changed.
2907 blob_copy(&tv, &btv);
2908 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002909 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002910 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002911 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 else
2913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002915 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 }
2917 }
2918 }
2919 if (skip)
2920 --emsg_skip;
2921
2922 return fi;
2923}
2924
2925/*
2926 * Use the first item in a ":for" list. Advance to the next.
2927 * Assign the values to the variable (list). "arg" points to the first one.
2928 * Return TRUE when a valid item was found, FALSE when at end of list or
2929 * something wrong.
2930 */
2931 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002934 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002936 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002938 if (fi->fi_blob != NULL)
2939 {
2940 typval_T tv;
2941
2942 if (fi->fi_bi >= blob_len(fi->fi_blob))
2943 return FALSE;
2944 tv.v_type = VAR_NUMBER;
2945 tv.v_lock = VAR_FIXED;
2946 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2947 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002948 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2949 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002950 }
2951
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952 item = fi->fi_lw.lw_item;
2953 if (item == NULL)
2954 result = FALSE;
2955 else
2956 {
2957 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002958 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2959 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002960 }
2961 return result;
2962}
2963
2964/*
2965 * Free the structure used to store info used by ":for".
2966 */
2967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969{
Bram Moolenaar33570922005-01-25 22:26:29 +00002970 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002972 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002973 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002974 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002975 list_unref(fi->fi_list);
2976 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002977 if (fi != NULL && fi->fi_blob != NULL)
2978 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 vim_free(fi);
2980}
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2983
2984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002985set_context_for_expression(
2986 expand_T *xp,
2987 char_u *arg,
2988 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 int got_eq = FALSE;
2991 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 if (cmdidx == CMD_let)
2995 {
2996 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002997 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002998 {
2999 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003000 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 {
3002 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003003 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003004 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 break;
3006 }
3007 return;
3008 }
3009 }
3010 else
3011 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3012 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 while ((xp->xp_pattern = vim_strpbrk(arg,
3014 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3015 {
3016 c = *xp->xp_pattern;
3017 if (c == '&')
3018 {
3019 c = xp->xp_pattern[1];
3020 if (c == '&')
3021 {
3022 ++xp->xp_pattern;
3023 xp->xp_context = cmdidx != CMD_let || got_eq
3024 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3025 }
3026 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003029 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3030 xp->xp_pattern += 2;
3031
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034 else if (c == '$')
3035 {
3036 /* environment variable */
3037 xp->xp_context = EXPAND_ENV_VARS;
3038 }
3039 else if (c == '=')
3040 {
3041 got_eq = TRUE;
3042 xp->xp_context = EXPAND_EXPRESSION;
3043 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003044 else if (c == '#'
3045 && xp->xp_context == EXPAND_EXPRESSION)
3046 {
3047 /* Autoload function/variable contains '#'. */
3048 break;
3049 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003050 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 && xp->xp_context == EXPAND_FUNCTIONS
3052 && vim_strchr(xp->xp_pattern, '(') == NULL)
3053 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003054 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
3056 }
3057 else if (cmdidx != CMD_let || got_eq)
3058 {
3059 if (c == '"') /* string */
3060 {
3061 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3062 if (c == '\\' && xp->xp_pattern[1] != NUL)
3063 ++xp->xp_pattern;
3064 xp->xp_context = EXPAND_NOTHING;
3065 }
3066 else if (c == '\'') /* literal string */
3067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3070 /* skip */ ;
3071 xp->xp_context = EXPAND_NOTHING;
3072 }
3073 else if (c == '|')
3074 {
3075 if (xp->xp_pattern[1] == '|')
3076 {
3077 ++xp->xp_pattern;
3078 xp->xp_context = EXPAND_EXPRESSION;
3079 }
3080 else
3081 xp->xp_context = EXPAND_COMMANDS;
3082 }
3083 else
3084 xp->xp_context = EXPAND_EXPRESSION;
3085 }
3086 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087 /* Doesn't look like something valid, expand as an expression
3088 * anyway. */
3089 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 arg = xp->xp_pattern;
3091 if (*arg != NUL)
3092 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3093 /* skip */ ;
3094 }
3095 xp->xp_pattern = arg;
3096}
3097
3098#endif /* FEAT_CMDL_COMPL */
3099
3100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * ":unlet[!] var1 ... " command.
3102 */
3103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003106 ex_unletlock(eap, eap->arg, 0);
3107}
3108
3109/*
3110 * ":lockvar" and ":unlockvar" commands
3111 */
3112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116 int deep = 2;
3117
3118 if (eap->forceit)
3119 deep = -1;
3120 else if (vim_isdigit(*arg))
3121 {
3122 deep = getdigits(&arg);
3123 arg = skipwhite(arg);
3124 }
3125
3126 ex_unletlock(eap, arg, deep);
3127}
3128
3129/*
3130 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3131 */
3132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003133ex_unletlock(
3134 exarg_T *eap,
3135 char_u *argstart,
3136 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003137{
3138 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003141 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 do
3144 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003145 if (*arg == '$')
3146 {
3147 char_u *name = ++arg;
3148
3149 if (get_env_len(&arg) == 0)
3150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003151 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003152 return;
3153 }
3154 vim_unsetenv(name);
3155 arg = skipwhite(arg);
3156 continue;
3157 }
3158
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003159 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003160 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003161 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003162 if (lv.ll_name == NULL)
3163 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003164 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 if (name_end != NULL)
3168 {
3169 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003170 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003171 }
3172 if (!(eap->skip || error))
3173 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 break;
3175 }
3176
3177 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003178 {
3179 if (eap->cmdidx == CMD_unlet)
3180 {
3181 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3182 error = TRUE;
3183 }
3184 else
3185 {
3186 if (do_lock_var(&lv, name_end, deep,
3187 eap->cmdidx == CMD_lockvar) == FAIL)
3188 error = TRUE;
3189 }
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003192 if (!eap->skip)
3193 clear_lval(&lv);
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 arg = skipwhite(name_end);
3196 } while (!ends_excmd(*arg));
3197
3198 eap->nextcmd = check_nextcmd(arg);
3199}
3200
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202do_unlet_var(
3203 lval_T *lp,
3204 char_u *name_end,
3205 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003206{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 int ret = OK;
3208 int cc;
3209
3210 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003211 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 cc = *name_end;
3213 *name_end = NUL;
3214
3215 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003216 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003218 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003220 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003221 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003222 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003223 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003224 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 else if (lp->ll_range)
3226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003228 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003229 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003230
3231 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3232 {
3233 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003234 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003235 return FAIL;
3236 ll_li = li;
3237 ++ll_n1;
3238 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239
3240 /* Delete a range of List items. */
3241 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3242 {
3243 li = lp->ll_li->li_next;
3244 listitem_remove(lp->ll_list, lp->ll_li);
3245 lp->ll_li = li;
3246 ++lp->ll_n1;
3247 }
3248 }
3249 else
3250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 /* unlet a List item. */
3253 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003256 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 }
3258
3259 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262/*
3263 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003264 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
3266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 hashtab_T *ht;
3270 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003271 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003272 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003273 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 ht = find_var_ht(name, &varname);
3276 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003279 if (d == NULL)
3280 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281 if (ht == &globvarht)
3282 d = &globvardict;
3283 else if (ht == &compat_hashtab)
3284 d = &vimvardict;
3285 else
3286 {
3287 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3288 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3289 }
3290 if (d == NULL)
3291 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003292 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 return FAIL;
3294 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003295 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003297 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003298 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003299 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003300 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003301 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003302 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003303 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003304 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003305 return FAIL;
3306
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003307 delete_var(ht, hi);
3308 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003311 if (forceit)
3312 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return FAIL;
3315}
3316
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003317/*
3318 * Lock or unlock variable indicated by "lp".
3319 * "deep" is the levels to go (-1 for unlimited);
3320 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3321 */
3322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003323do_lock_var(
3324 lval_T *lp,
3325 char_u *name_end,
3326 int deep,
3327 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003328{
3329 int ret = OK;
3330 int cc;
3331 dictitem_T *di;
3332
3333 if (deep == 0) /* nothing to do */
3334 return OK;
3335
3336 if (lp->ll_tv == NULL)
3337 {
3338 cc = *name_end;
3339 *name_end = NUL;
3340
3341 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003342 di = find_var(lp->ll_name, NULL, TRUE);
3343 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003344 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003345 else if ((di->di_flags & DI_FLAGS_FIX)
3346 && di->di_tv.v_type != VAR_DICT
3347 && di->di_tv.v_type != VAR_LIST)
3348 /* For historic reasons this error is not given for a list or dict.
3349 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003350 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003351 else
3352 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003353 if (lock)
3354 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003356 di->di_flags &= ~DI_FLAGS_LOCK;
3357 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358 }
3359 *name_end = cc;
3360 }
3361 else if (lp->ll_range)
3362 {
3363 listitem_T *li = lp->ll_li;
3364
3365 /* (un)lock a range of List items. */
3366 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3367 {
3368 item_lock(&li->li_tv, deep, lock);
3369 li = li->li_next;
3370 ++lp->ll_n1;
3371 }
3372 }
3373 else if (lp->ll_list != NULL)
3374 /* (un)lock a List item. */
3375 item_lock(&lp->ll_li->li_tv, deep, lock);
3376 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003377 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003378 item_lock(&lp->ll_di->di_tv, deep, lock);
3379
3380 return ret;
3381}
3382
3383/*
3384 * Lock or unlock an item. "deep" is nr of levels to go.
3385 */
3386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003387item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003388{
3389 static int recurse = 0;
3390 list_T *l;
3391 listitem_T *li;
3392 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003393 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003394 hashitem_T *hi;
3395 int todo;
3396
3397 if (recurse >= DICT_MAXNEST)
3398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003399 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003400 return;
3401 }
3402 if (deep == 0)
3403 return;
3404 ++recurse;
3405
3406 /* lock/unlock the item itself */
3407 if (lock)
3408 tv->v_lock |= VAR_LOCKED;
3409 else
3410 tv->v_lock &= ~VAR_LOCKED;
3411
3412 switch (tv->v_type)
3413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003414 case VAR_UNKNOWN:
3415 case VAR_NUMBER:
3416 case VAR_STRING:
3417 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003418 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003419 case VAR_FLOAT:
3420 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003421 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003422 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003423 break;
3424
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003425 case VAR_BLOB:
3426 if ((b = tv->vval.v_blob) != NULL)
3427 {
3428 if (lock)
3429 b->bv_lock |= VAR_LOCKED;
3430 else
3431 b->bv_lock &= ~VAR_LOCKED;
3432 }
3433 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003434 case VAR_LIST:
3435 if ((l = tv->vval.v_list) != NULL)
3436 {
3437 if (lock)
3438 l->lv_lock |= VAR_LOCKED;
3439 else
3440 l->lv_lock &= ~VAR_LOCKED;
3441 if (deep < 0 || deep > 1)
3442 /* recursive: lock/unlock the items the List contains */
3443 for (li = l->lv_first; li != NULL; li = li->li_next)
3444 item_lock(&li->li_tv, deep - 1, lock);
3445 }
3446 break;
3447 case VAR_DICT:
3448 if ((d = tv->vval.v_dict) != NULL)
3449 {
3450 if (lock)
3451 d->dv_lock |= VAR_LOCKED;
3452 else
3453 d->dv_lock &= ~VAR_LOCKED;
3454 if (deep < 0 || deep > 1)
3455 {
3456 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003458 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3459 {
3460 if (!HASHITEM_EMPTY(hi))
3461 {
3462 --todo;
3463 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3464 }
3465 }
3466 }
3467 }
3468 }
3469 --recurse;
3470}
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3473/*
3474 * Delete all "menutrans_" variables.
3475 */
3476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003477del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
Bram Moolenaar33570922005-01-25 22:26:29 +00003479 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003480 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003485 {
3486 if (!HASHITEM_EMPTY(hi))
3487 {
3488 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003489 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3490 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003491 }
3492 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003493 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494}
3495#endif
3496
3497#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3498
3499/*
3500 * Local string buffer for the next two functions to store a variable name
3501 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3502 * get_user_var_name().
3503 */
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505static char_u *varnamebuf = NULL;
3506static int varnamebuflen = 0;
3507
3508/*
3509 * Function to concatenate a prefix and a variable name.
3510 */
3511 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 int len;
3515
3516 len = (int)STRLEN(name) + 3;
3517 if (len > varnamebuflen)
3518 {
3519 vim_free(varnamebuf);
3520 len += 10; /* some additional space */
3521 varnamebuf = alloc(len);
3522 if (varnamebuf == NULL)
3523 {
3524 varnamebuflen = 0;
3525 return NULL;
3526 }
3527 varnamebuflen = len;
3528 }
3529 *varnamebuf = prefix;
3530 varnamebuf[1] = ':';
3531 STRCPY(varnamebuf + 2, name);
3532 return varnamebuf;
3533}
3534
3535/*
3536 * Function given to ExpandGeneric() to obtain the list of user defined
3537 * (global/buffer/window/built-in) variable names.
3538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003542 static long_u gdone;
3543 static long_u bdone;
3544 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003545 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003546 static int vidx;
3547 static hashitem_T *hi;
3548 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003551 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003552 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003553 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003554 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003555
3556 /* Global variables */
3557 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003559 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003560 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003561 else
3562 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003563 while (HASHITEM_EMPTY(hi))
3564 ++hi;
3565 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3566 return cat_prefix_varname('g', hi->hi_key);
3567 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003569
3570 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003571 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003576 else
3577 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003578 while (HASHITEM_EMPTY(hi))
3579 ++hi;
3580 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003582
3583 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003584 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003587 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003588 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003589 else
3590 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003591 while (HASHITEM_EMPTY(hi))
3592 ++hi;
3593 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003595
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003596 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003597 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003598 if (tdone < ht->ht_used)
3599 {
3600 if (tdone++ == 0)
3601 hi = ht->ht_array;
3602 else
3603 ++hi;
3604 while (HASHITEM_EMPTY(hi))
3605 ++hi;
3606 return cat_prefix_varname('t', hi->hi_key);
3607 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003608
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 /* v: variables */
3610 if (vidx < VV_LEN)
3611 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaard23a8232018-02-10 18:45:26 +01003613 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 varnamebuflen = 0;
3615 return NULL;
3616}
3617
3618#endif /* FEAT_CMDL_COMPL */
3619
3620/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003621 * Return TRUE if "pat" matches "text".
3622 * Does not use 'cpo' and always uses 'magic'.
3623 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003624 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003625pattern_match(char_u *pat, char_u *text, int ic)
3626{
3627 int matches = FALSE;
3628 char_u *save_cpo;
3629 regmatch_T regmatch;
3630
3631 /* avoid 'l' flag in 'cpoptions' */
3632 save_cpo = p_cpo;
3633 p_cpo = (char_u *)"";
3634 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3635 if (regmatch.regprog != NULL)
3636 {
3637 regmatch.rm_ic = ic;
3638 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3639 vim_regfree(regmatch.regprog);
3640 }
3641 p_cpo = save_cpo;
3642 return matches;
3643}
3644
3645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003647 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3649 */
3650
3651/*
3652 * Handle zero level expression.
3653 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003655 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 * Return OK or FAIL.
3657 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003659eval0(
3660 char_u *arg,
3661 typval_T *rettv,
3662 char_u **nextcmd,
3663 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 int ret;
3666 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003667 int did_emsg_before = did_emsg;
3668 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 if (ret == FAIL || !ends_excmd(*p))
3673 {
3674 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 /*
3677 * Report the invalid expression unless the expression evaluation has
3678 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003679 * exception, or we already gave a more specific error.
3680 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003682 if (!aborting() && did_emsg == did_emsg_before
3683 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003684 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 ret = FAIL;
3686 }
3687 if (nextcmd != NULL)
3688 *nextcmd = check_nextcmd(p);
3689
3690 return ret;
3691}
3692
3693/*
3694 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003695 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 *
3697 * "arg" must point to the first non-white of the expression.
3698 * "arg" is advanced to the next non-white after the recognized expression.
3699 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003700 * Note: "rettv.v_lock" is not set.
3701 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 * Return OK or FAIL.
3703 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003708 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 /*
3711 * Get the first variable.
3712 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FAIL;
3715
3716 if ((*arg)[0] == '?')
3717 {
3718 result = FALSE;
3719 if (evaluate)
3720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003721 int error = FALSE;
3722
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003723 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 if (error)
3727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
3730 /*
3731 * Get the second variable.
3732 */
3733 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 return FAIL;
3736
3737 /*
3738 * Check for the ":".
3739 */
3740 if ((*arg)[0] != ':')
3741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003742 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 return FAIL;
3746 }
3747
3748 /*
3749 * Get the third variable.
3750 */
3751 *arg = skipwhite(*arg + 1);
3752 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3753 {
3754 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 return FAIL;
3757 }
3758 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003759 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761
3762 return OK;
3763}
3764
3765/*
3766 * Handle first level expression:
3767 * expr2 || expr2 || expr2 logical OR
3768 *
3769 * "arg" must point to the first non-white of the expression.
3770 * "arg" is advanced to the next non-white after the recognized expression.
3771 *
3772 * Return OK or FAIL.
3773 */
3774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003775eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
Bram Moolenaar33570922005-01-25 22:26:29 +00003777 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 long result;
3779 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * Get the first variable.
3784 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 /*
3789 * Repeat until there is no following "||".
3790 */
3791 first = TRUE;
3792 result = FALSE;
3793 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3794 {
3795 if (evaluate && first)
3796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003797 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 if (error)
3801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 first = FALSE;
3803 }
3804
3805 /*
3806 * Get the second variable.
3807 */
3808 *arg = skipwhite(*arg + 2);
3809 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3810 return FAIL;
3811
3812 /*
3813 * Compute the result.
3814 */
3815 if (evaluate && !result)
3816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003817 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003819 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003820 if (error)
3821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 if (evaluate)
3824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825 rettv->v_type = VAR_NUMBER;
3826 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829
3830 return OK;
3831}
3832
3833/*
3834 * Handle second level expression:
3835 * expr3 && expr3 && expr3 logical AND
3836 *
3837 * "arg" must point to the first non-white of the expression.
3838 * "arg" is advanced to the next non-white after the recognized expression.
3839 *
3840 * Return OK or FAIL.
3841 */
3842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 long result;
3847 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
3850 /*
3851 * Get the first variable.
3852 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return FAIL;
3855
3856 /*
3857 * Repeat until there is no following "&&".
3858 */
3859 first = TRUE;
3860 result = TRUE;
3861 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3862 {
3863 if (evaluate && first)
3864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003865 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868 if (error)
3869 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 first = FALSE;
3871 }
3872
3873 /*
3874 * Get the second variable.
3875 */
3876 *arg = skipwhite(*arg + 2);
3877 if (eval4(arg, &var2, evaluate && result) == FAIL)
3878 return FAIL;
3879
3880 /*
3881 * Compute the result.
3882 */
3883 if (evaluate && result)
3884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 if (error)
3889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891 if (evaluate)
3892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893 rettv->v_type = VAR_NUMBER;
3894 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 }
3897
3898 return OK;
3899}
3900
3901/*
3902 * Handle third level expression:
3903 * var1 == var2
3904 * var1 =~ var2
3905 * var1 != var2
3906 * var1 !~ var2
3907 * var1 > var2
3908 * var1 >= var2
3909 * var1 < var2
3910 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003911 * var1 is var2
3912 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 *
3914 * "arg" must point to the first non-white of the expression.
3915 * "arg" is advanced to the next non-white after the recognized expression.
3916 *
3917 * Return OK or FAIL.
3918 */
3919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *p;
3924 int i;
3925 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003926 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 /*
3931 * Get the first variable.
3932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 return FAIL;
3935
3936 p = *arg;
3937 switch (p[0])
3938 {
3939 case '=': if (p[1] == '=')
3940 type = TYPE_EQUAL;
3941 else if (p[1] == '~')
3942 type = TYPE_MATCH;
3943 break;
3944 case '!': if (p[1] == '=')
3945 type = TYPE_NEQUAL;
3946 else if (p[1] == '~')
3947 type = TYPE_NOMATCH;
3948 break;
3949 case '>': if (p[1] != '=')
3950 {
3951 type = TYPE_GREATER;
3952 len = 1;
3953 }
3954 else
3955 type = TYPE_GEQUAL;
3956 break;
3957 case '<': if (p[1] != '=')
3958 {
3959 type = TYPE_SMALLER;
3960 len = 1;
3961 }
3962 else
3963 type = TYPE_SEQUAL;
3964 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003965 case 'i': if (p[1] == 's')
3966 {
3967 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3968 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003969 i = p[len];
3970 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003971 {
3972 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3973 type_is = TRUE;
3974 }
3975 }
3976 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978
3979 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003980 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 if (type != TYPE_UNKNOWN)
3983 {
3984 /* extra question mark appended: ignore case */
3985 if (p[len] == '?')
3986 {
3987 ic = TRUE;
3988 ++len;
3989 }
3990 /* extra '#' appended: match case */
3991 else if (p[len] == '#')
3992 {
3993 ic = FALSE;
3994 ++len;
3995 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003996 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else
3998 ic = p_ic;
3999
4000 /*
4001 * Get the second variable.
4002 */
4003 *arg = skipwhite(p + len);
4004 if (eval5(arg, &var2, evaluate) == FAIL)
4005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FAIL;
4008 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004009 if (evaluate)
4010 {
4011 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4012
4013 clear_tv(&var2);
4014 return ret;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017
4018 return OK;
4019}
4020
4021/*
4022 * Handle fourth level expression:
4023 * + number addition
4024 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004025 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004026 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 *
4028 * "arg" must point to the first non-white of the expression.
4029 * "arg" is advanced to the next non-white after the recognized expression.
4030 *
4031 * Return OK or FAIL.
4032 */
4033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004034eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 typval_T var2;
4037 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004039 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004040#ifdef FEAT_FLOAT
4041 float_T f1 = 0, f2 = 0;
4042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 char_u *s1, *s2;
4044 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4045 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004046 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 /*
4049 * Get the first variable.
4050 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004051 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return FAIL;
4053
4054 /*
4055 * Repeat computing, until no '+', '-' or '.' is following.
4056 */
4057 for (;;)
4058 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004059 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004061 concat = op == '.'
4062 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4063 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 break;
4065
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004066 if ((op != '+' || (rettv->v_type != VAR_LIST
4067 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004068#ifdef FEAT_FLOAT
4069 && (op == '.' || rettv->v_type != VAR_FLOAT)
4070#endif
4071 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 {
4073 /* For "list + ...", an illegal use of the first operand as
4074 * a number cannot be determined before evaluating the 2nd
4075 * operand: if this is also a list, all is ok.
4076 * For "something . ...", "something - ..." or "non-list + ...",
4077 * we know that the first operand needs to be a string or number
4078 * without evaluating the 2nd operand. So check before to avoid
4079 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004080 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 {
4082 clear_tv(rettv);
4083 return FAIL;
4084 }
4085 }
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 /*
4088 * Get the second variable.
4089 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004090 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4091 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004093 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return FAIL;
4097 }
4098
4099 if (evaluate)
4100 {
4101 /*
4102 * Compute the result.
4103 */
4104 if (op == '.')
4105 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004106 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4107 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108 if (s2 == NULL) /* type error ? */
4109 {
4110 clear_tv(rettv);
4111 clear_tv(&var2);
4112 return FAIL;
4113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004114 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
4116 rettv->v_type = VAR_STRING;
4117 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004119 else if (op == '+' && rettv->v_type == VAR_BLOB
4120 && var2.v_type == VAR_BLOB)
4121 {
4122 blob_T *b1 = rettv->vval.v_blob;
4123 blob_T *b2 = var2.vval.v_blob;
4124 blob_T *b = blob_alloc();
4125 int i;
4126
4127 if (b != NULL)
4128 {
4129 for (i = 0; i < blob_len(b1); i++)
4130 ga_append(&b->bv_ga, blob_get(b1, i));
4131 for (i = 0; i < blob_len(b2); i++)
4132 ga_append(&b->bv_ga, blob_get(b2, i));
4133
4134 clear_tv(rettv);
4135 rettv_blob_set(rettv, b);
4136 }
4137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004138 else if (op == '+' && rettv->v_type == VAR_LIST
4139 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004140 {
4141 /* concatenate Lists */
4142 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4143 &var3) == FAIL)
4144 {
4145 clear_tv(rettv);
4146 clear_tv(&var2);
4147 return FAIL;
4148 }
4149 clear_tv(rettv);
4150 *rettv = var3;
4151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
4153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 int error = FALSE;
4155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004156#ifdef FEAT_FLOAT
4157 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004158 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004159 f1 = rettv->vval.v_float;
4160 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004162 else
4163#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004165 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004166 if (error)
4167 {
4168 /* This can only happen for "list + non-list". For
4169 * "non-list + ..." or "something - ...", we returned
4170 * before evaluating the 2nd operand. */
4171 clear_tv(rettv);
4172 return FAIL;
4173 }
4174#ifdef FEAT_FLOAT
4175 if (var2.v_type == VAR_FLOAT)
4176 f1 = n1;
4177#endif
4178 }
4179#ifdef FEAT_FLOAT
4180 if (var2.v_type == VAR_FLOAT)
4181 {
4182 f2 = var2.vval.v_float;
4183 n2 = 0;
4184 }
4185 else
4186#endif
4187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004188 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004189 if (error)
4190 {
4191 clear_tv(rettv);
4192 clear_tv(&var2);
4193 return FAIL;
4194 }
4195#ifdef FEAT_FLOAT
4196 if (rettv->v_type == VAR_FLOAT)
4197 f2 = n2;
4198#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004201
4202#ifdef FEAT_FLOAT
4203 /* If there is a float on either side the result is a float. */
4204 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4205 {
4206 if (op == '+')
4207 f1 = f1 + f2;
4208 else
4209 f1 = f1 - f2;
4210 rettv->v_type = VAR_FLOAT;
4211 rettv->vval.v_float = f1;
4212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004214#endif
4215 {
4216 if (op == '+')
4217 n1 = n1 + n2;
4218 else
4219 n1 = n1 - n2;
4220 rettv->v_type = VAR_NUMBER;
4221 rettv->vval.v_number = n1;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227 return OK;
4228}
4229
4230/*
4231 * Handle fifth level expression:
4232 * * number multiplication
4233 * / number division
4234 * % number modulo
4235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
4239 * Return OK or FAIL.
4240 */
4241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004242eval6(
4243 char_u **arg,
4244 typval_T *rettv,
4245 int evaluate,
4246 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
Bram Moolenaar33570922005-01-25 22:26:29 +00004248 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004250 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004251#ifdef FEAT_FLOAT
4252 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004253 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004254#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /*
4258 * Get the first variable.
4259 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004260 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 return FAIL;
4262
4263 /*
4264 * Repeat computing, until no '*', '/' or '%' is following.
4265 */
4266 for (;;)
4267 {
4268 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004269 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 break;
4271
4272 if (evaluate)
4273 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004274#ifdef FEAT_FLOAT
4275 if (rettv->v_type == VAR_FLOAT)
4276 {
4277 f1 = rettv->vval.v_float;
4278 use_float = TRUE;
4279 n1 = 0;
4280 }
4281 else
4282#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004283 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 if (error)
4286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 else
4289 n1 = 0;
4290
4291 /*
4292 * Get the second variable.
4293 */
4294 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004295 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return FAIL;
4297
4298 if (evaluate)
4299 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004300#ifdef FEAT_FLOAT
4301 if (var2.v_type == VAR_FLOAT)
4302 {
4303 if (!use_float)
4304 {
4305 f1 = n1;
4306 use_float = TRUE;
4307 }
4308 f2 = var2.vval.v_float;
4309 n2 = 0;
4310 }
4311 else
4312#endif
4313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004314 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004315 clear_tv(&var2);
4316 if (error)
4317 return FAIL;
4318#ifdef FEAT_FLOAT
4319 if (use_float)
4320 f2 = n2;
4321#endif
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004326 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004328#ifdef FEAT_FLOAT
4329 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331 if (op == '*')
4332 f1 = f1 * f2;
4333 else if (op == '/')
4334 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004335# ifdef VMS
4336 /* VMS crashes on divide by zero, work around it */
4337 if (f2 == 0.0)
4338 {
4339 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004340 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004341 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004342 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004343 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004344 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004345 }
4346 else
4347 f1 = f1 / f2;
4348# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004349 /* We rely on the floating point library to handle divide
4350 * by zero to result in "inf" and not a crash. */
4351 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004352# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004356 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 return FAIL;
4358 }
4359 rettv->v_type = VAR_FLOAT;
4360 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 if (op == '*')
4366 n1 = n1 * n2;
4367 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004368 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004370 n1 = num_modulus(n1, n2);
4371
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 rettv->v_type = VAR_NUMBER;
4373 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377
4378 return OK;
4379}
4380
4381/*
4382 * Handle sixth level expression:
4383 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004384 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004385 * "string" string constant
4386 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 * &option-name option value
4388 * @r register contents
4389 * identifier variable value
4390 * function() function call
4391 * $VAR environment variable
4392 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004393 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004394 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004395 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 *
4397 * Also handle:
4398 * ! in front logical NOT
4399 * - in front unary minus
4400 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004401 * trailing [] subscript in String or List
4402 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 *
4404 * "arg" must point to the first non-white of the expression.
4405 * "arg" is advanced to the next non-white after the recognized expression.
4406 *
4407 * Return OK or FAIL.
4408 */
4409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004410eval7(
4411 char_u **arg,
4412 typval_T *rettv,
4413 int evaluate,
4414 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004416 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 int len;
4418 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 char_u *start_leader, *end_leader;
4420 int ret = OK;
4421 char_u *alias;
4422
4423 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004425 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004430 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 */
4432 start_leader = *arg;
4433 while (**arg == '!' || **arg == '-' || **arg == '+')
4434 *arg = skipwhite(*arg + 1);
4435 end_leader = *arg;
4436
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004437 if (**arg == '.' && (!isdigit(*(*arg + 1))
4438#ifdef FEAT_FLOAT
4439 || current_sctx.sc_version < 2
4440#endif
4441 ))
4442 {
4443 semsg(_(e_invexpr2), *arg);
4444 ++*arg;
4445 return FAIL;
4446 }
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 switch (**arg)
4449 {
4450 /*
4451 * Number constant.
4452 */
4453 case '0':
4454 case '1':
4455 case '2':
4456 case '3':
4457 case '4':
4458 case '5':
4459 case '6':
4460 case '7':
4461 case '8':
4462 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004463 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 {
4465#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004466 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004467 int get_float = FALSE;
4468
4469 /* We accept a float when the format matches
4470 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004471 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004472 * With script version 2 and later the leading digit can be
4473 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004474 * Don't look for a float after the "." operator, so that
4475 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004476 if (**arg == '.')
4477 p = *arg;
4478 else
4479 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004480 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482 get_float = TRUE;
4483 p = skipdigits(p + 2);
4484 if (*p == 'e' || *p == 'E')
4485 {
4486 ++p;
4487 if (*p == '-' || *p == '+')
4488 ++p;
4489 if (!vim_isdigit(*p))
4490 get_float = FALSE;
4491 else
4492 p = skipdigits(p + 1);
4493 }
4494 if (ASCII_ISALPHA(*p) || *p == '.')
4495 get_float = FALSE;
4496 }
4497 if (get_float)
4498 {
4499 float_T f;
4500
4501 *arg += string2float(*arg, &f);
4502 if (evaluate)
4503 {
4504 rettv->v_type = VAR_FLOAT;
4505 rettv->vval.v_float = f;
4506 }
4507 }
4508 else
4509#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004510 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004511 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004512 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004513 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004514
4515 // Blob constant: 0z0123456789abcdef
4516 if (evaluate)
4517 blob = blob_alloc();
4518 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4519 {
4520 if (!vim_isxdigit(bp[1]))
4521 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004522 if (blob != NULL)
4523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004524 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004525 ga_clear(&blob->bv_ga);
4526 VIM_CLEAR(blob);
4527 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004528 ret = FAIL;
4529 break;
4530 }
4531 if (blob != NULL)
4532 ga_append(&blob->bv_ga,
4533 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004534 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4535 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004536 }
4537 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004538 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004539 *arg = bp;
4540 }
4541 else
4542 {
4543 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004544 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4545 if (len == 0)
4546 {
4547 semsg(_(e_invexpr2), *arg);
4548 ret = FAIL;
4549 break;
4550 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004551 *arg += len;
4552 if (evaluate)
4553 {
4554 rettv->v_type = VAR_NUMBER;
4555 rettv->vval.v_number = n;
4556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 /*
4562 * String constant: "string".
4563 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 break;
4566
4567 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004570 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 break;
4572
4573 /*
4574 * List: [expr, expr]
4575 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 break;
4578
4579 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004580 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004581 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004582 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004583 {
4584 ++*arg;
4585 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4586 }
4587 else
4588 ret = NOTDONE;
4589 break;
4590
4591 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004592 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004593 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004595 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4596 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004597 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 break;
4599
4600 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004601 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004603 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 break;
4605
4606 /*
4607 * Environment variable: $VAR.
4608 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 break;
4611
4612 /*
4613 * Register contents: @r.
4614 */
4615 case '@': ++*arg;
4616 if (evaluate)
4617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004619 rettv->vval.v_string = get_reg_contents(**arg,
4620 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 if (**arg != NUL)
4623 ++*arg;
4624 break;
4625
4626 /*
4627 * nested expression: (expression).
4628 */
4629 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (**arg == ')')
4632 ++*arg;
4633 else if (ret == OK)
4634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004635 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 ret = FAIL;
4638 }
4639 break;
4640
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 break;
4643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644
4645 if (ret == NOTDONE)
4646 {
4647 /*
4648 * Must be a variable or function name.
4649 * Can also be a curly-braces kind of name: {expr}.
4650 */
4651 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004652 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 if (alias != NULL)
4654 s = alias;
4655
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004656 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 ret = FAIL;
4658 else
4659 {
4660 if (**arg == '(') /* recursive! */
4661 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004662 partial_T *partial;
4663
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004664 if (!evaluate)
4665 check_vars(s, len);
4666
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 /* If "s" is the name of a variable of type VAR_FUNC
4668 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004669 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004671 /* Need to make a copy, in case evaluating the arguments makes
4672 * the name invalid. */
4673 s = vim_strsave(s);
4674 if (s == NULL)
4675 ret = FAIL;
4676 else
4677 /* Invoke the function. */
4678 ret = get_func_tv(s, len, rettv, arg,
4679 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4680 &len, evaluate, partial, NULL);
4681 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004682
4683 /* If evaluate is FALSE rettv->v_type was not set in
4684 * get_func_tv, but it's needed in handle_subscript() to parse
4685 * what follows. So set it here. */
4686 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4687 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004688 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004689 rettv->v_type = VAR_FUNC;
4690 }
4691
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 /* Stop the expression evaluation when immediately
4693 * aborting on error, or when an interrupt occurred or
4694 * an exception was thrown but not caught. */
Bram Moolenaar60640732019-06-07 23:15:22 +02004695 if (evaluate && aborting())
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 {
4697 if (ret == OK)
4698 clear_tv(rettv);
4699 ret = FAIL;
4700 }
4701 }
4702 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004703 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004704 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004705 {
4706 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004707 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004710 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004711 }
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 *arg = skipwhite(*arg);
4714
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004715 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4716 * expr(expr). */
4717 if (ret == OK)
4718 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 /*
4721 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4722 */
4723 if (ret == OK && evaluate && end_leader > start_leader)
4724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004726 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727#ifdef FEAT_FLOAT
4728 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004730 if (rettv->v_type == VAR_FLOAT)
4731 f = rettv->vval.v_float;
4732 else
4733#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004734 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004735 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 clear_tv(rettv);
4738 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 else
4741 {
4742 while (end_leader > start_leader)
4743 {
4744 --end_leader;
4745 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746 {
4747#ifdef FEAT_FLOAT
4748 if (rettv->v_type == VAR_FLOAT)
4749 f = !f;
4750 else
4751#endif
4752 val = !val;
4753 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 {
4756#ifdef FEAT_FLOAT
4757 if (rettv->v_type == VAR_FLOAT)
4758 f = -f;
4759 else
4760#endif
4761 val = -val;
4762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764#ifdef FEAT_FLOAT
4765 if (rettv->v_type == VAR_FLOAT)
4766 {
4767 clear_tv(rettv);
4768 rettv->vval.v_float = f;
4769 }
4770 else
4771#endif
4772 {
4773 clear_tv(rettv);
4774 rettv->v_type = VAR_NUMBER;
4775 rettv->vval.v_number = val;
4776 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
4779
4780 return ret;
4781}
4782
4783/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004784 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4785 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4787 */
4788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004789eval_index(
4790 char_u **arg,
4791 typval_T *rettv,
4792 int evaluate,
4793 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004794{
4795 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004797 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 long len = -1;
4800 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803
Bram Moolenaara03f2332016-02-06 18:09:59 +01004804 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004806 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004807 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004808 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004809 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004810 return FAIL;
4811 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004812#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004813 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004814 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004815 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004816#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004817 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004818 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004819 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004820 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004821 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004822 return FAIL;
4823 case VAR_UNKNOWN:
4824 if (evaluate)
4825 return FAIL;
4826 /* FALLTHROUGH */
4827
4828 case VAR_STRING:
4829 case VAR_NUMBER:
4830 case VAR_LIST:
4831 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004832 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004833 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004834 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004836 init_tv(&var1);
4837 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004838 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 /*
4841 * dict.name
4842 */
4843 key = *arg + 1;
4844 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4845 ;
4846 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004848 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 }
4850 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852 /*
4853 * something[idx]
4854 *
4855 * Get the (first) variable from inside the [].
4856 */
4857 *arg = skipwhite(*arg + 1);
4858 if (**arg == ':')
4859 empty1 = TRUE;
4860 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4861 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004862 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004863 {
4864 /* not a number or string */
4865 clear_tv(&var1);
4866 return FAIL;
4867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004868
4869 /*
4870 * Get the second variable from inside the [:].
4871 */
4872 if (**arg == ':')
4873 {
4874 range = TRUE;
4875 *arg = skipwhite(*arg + 1);
4876 if (**arg == ']')
4877 empty2 = TRUE;
4878 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 if (!empty1)
4881 clear_tv(&var1);
4882 return FAIL;
4883 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004884 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 {
4886 /* not a number or string */
4887 if (!empty1)
4888 clear_tv(&var1);
4889 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004890 return FAIL;
4891 }
4892 }
4893
4894 /* Check for the ']'. */
4895 if (**arg != ']')
4896 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004897 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004898 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004899 clear_tv(&var1);
4900 if (range)
4901 clear_tv(&var2);
4902 return FAIL;
4903 }
4904 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004905 }
4906
4907 if (evaluate)
4908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909 n1 = 0;
4910 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004912 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 }
4915 if (range)
4916 {
4917 if (empty2)
4918 n2 = -1;
4919 else
4920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004921 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004922 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004923 }
4924 }
4925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004927 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004928 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004929 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004930 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004931 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004932 case VAR_SPECIAL:
4933 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004934 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004935 break; /* not evaluating, skipping over subscript */
4936
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 case VAR_NUMBER:
4938 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 len = (long)STRLEN(s);
4941 if (range)
4942 {
4943 /* The resulting variable is a substring. If the indexes
4944 * are out of range the result is empty. */
4945 if (n1 < 0)
4946 {
4947 n1 = len + n1;
4948 if (n1 < 0)
4949 n1 = 0;
4950 }
4951 if (n2 < 0)
4952 n2 = len + n2;
4953 else if (n2 >= len)
4954 n2 = len;
4955 if (n1 >= len || n2 < 0 || n1 > n2)
4956 s = NULL;
4957 else
4958 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4959 }
4960 else
4961 {
4962 /* The resulting variable is a string of a single
4963 * character. If the index is too big or negative the
4964 * result is empty. */
4965 if (n1 >= len || n1 < 0)
4966 s = NULL;
4967 else
4968 s = vim_strnsave(s + n1, 1);
4969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970 clear_tv(rettv);
4971 rettv->v_type = VAR_STRING;
4972 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 break;
4974
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004975 case VAR_BLOB:
4976 len = blob_len(rettv->vval.v_blob);
4977 if (range)
4978 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004979 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004980 // are out of range the result is empty.
4981 if (n1 < 0)
4982 {
4983 n1 = len + n1;
4984 if (n1 < 0)
4985 n1 = 0;
4986 }
4987 if (n2 < 0)
4988 n2 = len + n2;
4989 else if (n2 >= len)
4990 n2 = len - 1;
4991 if (n1 >= len || n2 < 0 || n1 > n2)
4992 {
4993 clear_tv(rettv);
4994 rettv->v_type = VAR_BLOB;
4995 rettv->vval.v_blob = NULL;
4996 }
4997 else
4998 {
4999 blob_T *blob = blob_alloc();
5000
5001 if (blob != NULL)
5002 {
5003 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5004 {
5005 blob_free(blob);
5006 return FAIL;
5007 }
5008 blob->bv_ga.ga_len = n2 - n1 + 1;
5009 for (i = n1; i <= n2; i++)
5010 blob_set(blob, i - n1,
5011 blob_get(rettv->vval.v_blob, i));
5012
5013 clear_tv(rettv);
5014 rettv_blob_set(rettv, blob);
5015 }
5016 }
5017 }
5018 else
5019 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005020 // The resulting variable is a byte value.
5021 // If the index is too big or negative that is an error.
5022 if (n1 < 0)
5023 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005024 if (n1 < len && n1 >= 0)
5025 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005026 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005027
5028 clear_tv(rettv);
5029 rettv->v_type = VAR_NUMBER;
5030 rettv->vval.v_number = v;
5031 }
5032 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005033 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005034 }
5035 break;
5036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005039 if (n1 < 0)
5040 n1 = len + n1;
5041 if (!empty1 && (n1 < 0 || n1 >= len))
5042 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005043 /* For a range we allow invalid values and return an empty
5044 * list. A list index out of range is an error. */
5045 if (!range)
5046 {
5047 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005048 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005049 return FAIL;
5050 }
5051 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005052 }
5053 if (range)
5054 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 list_T *l;
5056 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005057
5058 if (n2 < 0)
5059 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005060 else if (n2 >= len)
5061 n2 = len - 1;
5062 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005063 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005064 l = list_alloc();
5065 if (l == NULL)
5066 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 n1 <= n2; ++n1)
5069 {
5070 if (list_append_tv(l, &item->li_tv) == FAIL)
5071 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005072 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 return FAIL;
5074 }
5075 item = item->li_next;
5076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005078 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 }
5080 else
5081 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005082 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 clear_tv(rettv);
5084 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 }
5086 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087
5088 case VAR_DICT:
5089 if (range)
5090 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005091 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005092 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 if (len == -1)
5094 clear_tv(&var1);
5095 return FAIL;
5096 }
5097 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005098 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099
5100 if (len == -1)
5101 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005102 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005103 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 clear_tv(&var1);
5106 return FAIL;
5107 }
5108 }
5109
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005110 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005112 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005113 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 if (len == -1)
5115 clear_tv(&var1);
5116 if (item == NULL)
5117 return FAIL;
5118
5119 copy_tv(&item->di_tv, &var1);
5120 clear_tv(rettv);
5121 *rettv = var1;
5122 }
5123 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 }
5125 }
5126
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 return OK;
5128}
5129
5130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 * Get an option value.
5132 * "arg" points to the '&' or '+' before the option name.
5133 * "arg" is advanced to character after the option name.
5134 * Return OK or FAIL.
5135 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005137get_option_tv(
5138 char_u **arg,
5139 typval_T *rettv, /* when NULL, only check if option exists */
5140 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 char_u *option_end;
5143 long numval;
5144 char_u *stringval;
5145 int opt_type;
5146 int c;
5147 int working = (**arg == '+'); /* has("+option") */
5148 int ret = OK;
5149 int opt_flags;
5150
5151 /*
5152 * Isolate the option name and find its value.
5153 */
5154 option_end = find_option_end(arg, &opt_flags);
5155 if (option_end == NULL)
5156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005158 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 return FAIL;
5160 }
5161
5162 if (!evaluate)
5163 {
5164 *arg = option_end;
5165 return OK;
5166 }
5167
5168 c = *option_end;
5169 *option_end = NUL;
5170 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 if (opt_type == -3) /* invalid name */
5174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005176 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 ret = FAIL;
5178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
5181 if (opt_type == -2) /* hidden string option */
5182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 rettv->v_type = VAR_STRING;
5184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 else if (opt_type == -1) /* hidden number option */
5187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 rettv->v_type = VAR_NUMBER;
5189 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 else if (opt_type == 1) /* number option */
5192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 rettv->v_type = VAR_NUMBER;
5194 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196 else /* string option */
5197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 rettv->v_type = VAR_STRING;
5199 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 }
5202 else if (working && (opt_type == -2 || opt_type == -1))
5203 ret = FAIL;
5204
5205 *option_end = c; /* put back for error messages */
5206 *arg = option_end;
5207
5208 return ret;
5209}
5210
5211/*
5212 * Allocate a variable for a string constant.
5213 * Return OK or FAIL.
5214 */
5215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005216get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 char_u *p;
5219 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 int extra = 0;
5221
5222 /*
5223 * Find the end of the string, skipping backslashed characters.
5224 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005225 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (*p == '\\' && p[1] != NUL)
5228 {
5229 ++p;
5230 /* A "\<x>" form occupies at least 4 characters, and produces up
5231 * to 6 characters: reserve space for 2 extra */
5232 if (*p == '<')
5233 extra += 2;
5234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236
5237 if (*p != '"')
5238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005239 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return FAIL;
5241 }
5242
5243 /* If only parsing, set *arg and return here */
5244 if (!evaluate)
5245 {
5246 *arg = p + 1;
5247 return OK;
5248 }
5249
5250 /*
5251 * Copy the string into allocated memory, handling backslashed
5252 * characters.
5253 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005254 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (name == NULL)
5256 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 rettv->v_type = VAR_STRING;
5258 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 if (*p == '\\')
5263 {
5264 switch (*++p)
5265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 case 'b': *name++ = BS; ++p; break;
5267 case 'e': *name++ = ESC; ++p; break;
5268 case 'f': *name++ = FF; ++p; break;
5269 case 'n': *name++ = NL; ++p; break;
5270 case 'r': *name++ = CAR; ++p; break;
5271 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 case 'X': /* hex: "\x1", "\x12" */
5274 case 'x':
5275 case 'u': /* Unicode: "\u0023" */
5276 case 'U':
5277 if (vim_isxdigit(p[1]))
5278 {
5279 int n, nr;
5280 int c = toupper(*p);
5281
5282 if (c == 'X')
5283 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005284 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005286 else
5287 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 nr = 0;
5289 while (--n >= 0 && vim_isxdigit(p[1]))
5290 {
5291 ++p;
5292 nr = (nr << 4) + hex2nr(*p);
5293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 /* For "\u" store the number according to
5296 * 'encoding'. */
5297 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 break;
5303
5304 /* octal: "\1", "\12", "\123" */
5305 case '0':
5306 case '1':
5307 case '2':
5308 case '3':
5309 case '4':
5310 case '5':
5311 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 case '7': *name = *p++ - '0';
5313 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 *name = (*name << 3) + *p++ - '0';
5316 if (*p >= '0' && *p <= '7')
5317 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 break;
5321
5322 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005323 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (extra != 0)
5325 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 break;
5328 }
5329 /* FALLTHROUGH */
5330
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 break;
5333 }
5334 }
5335 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005340 if (*p != NUL) /* just in case */
5341 ++p;
5342 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 return OK;
5345}
5346
5347/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 * Return OK or FAIL.
5350 */
5351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005355 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005356 int reduce = 0;
5357
5358 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005360 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005361 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005362 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005366 break;
5367 ++reduce;
5368 ++p;
5369 }
5370 }
5371
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005374 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005375 return FAIL;
5376 }
5377
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005379 if (!evaluate)
5380 {
5381 *arg = p + 1;
5382 return OK;
5383 }
5384
5385 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005387 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005388 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005389 if (str == NULL)
5390 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 rettv->v_type = VAR_STRING;
5392 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005393
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005397 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005399 break;
5400 ++p;
5401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005405 *arg = p + 1;
5406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005407 return OK;
5408}
5409
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005410/*
5411 * Return the function name of the partial.
5412 */
5413 char_u *
5414partial_name(partial_T *pt)
5415{
5416 if (pt->pt_name != NULL)
5417 return pt->pt_name;
5418 return pt->pt_func->uf_name;
5419}
5420
Bram Moolenaarddecc252016-04-06 22:59:37 +02005421 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005422partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005423{
5424 int i;
5425
5426 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005427 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005428 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005429 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005430 if (pt->pt_name != NULL)
5431 {
5432 func_unref(pt->pt_name);
5433 vim_free(pt->pt_name);
5434 }
5435 else
5436 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005437 vim_free(pt);
5438}
5439
5440/*
5441 * Unreference a closure: decrement the reference count and free it when it
5442 * becomes zero.
5443 */
5444 void
5445partial_unref(partial_T *pt)
5446{
5447 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005448 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005449}
5450
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005451static int tv_equal_recurse_limit;
5452
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005453 static int
5454func_equal(
5455 typval_T *tv1,
5456 typval_T *tv2,
5457 int ic) /* ignore case */
5458{
5459 char_u *s1, *s2;
5460 dict_T *d1, *d2;
5461 int a1, a2;
5462 int i;
5463
5464 /* empty and NULL function name considered the same */
5465 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005466 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005467 if (s1 != NULL && *s1 == NUL)
5468 s1 = NULL;
5469 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005470 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005471 if (s2 != NULL && *s2 == NUL)
5472 s2 = NULL;
5473 if (s1 == NULL || s2 == NULL)
5474 {
5475 if (s1 != s2)
5476 return FALSE;
5477 }
5478 else if (STRCMP(s1, s2) != 0)
5479 return FALSE;
5480
5481 /* empty dict and NULL dict is different */
5482 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5483 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5484 if (d1 == NULL || d2 == NULL)
5485 {
5486 if (d1 != d2)
5487 return FALSE;
5488 }
5489 else if (!dict_equal(d1, d2, ic, TRUE))
5490 return FALSE;
5491
5492 /* empty list and no list considered the same */
5493 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5494 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5495 if (a1 != a2)
5496 return FALSE;
5497 for (i = 0; i < a1; ++i)
5498 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5499 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5500 return FALSE;
5501
5502 return TRUE;
5503}
5504
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005505/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005506 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005507 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005508 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005509 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005511tv_equal(
5512 typval_T *tv1,
5513 typval_T *tv2,
5514 int ic, /* ignore case */
5515 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005516{
5517 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005518 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005519 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005520 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005521
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005522 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005523 * recursiveness to a limit. We guess they are equal then.
5524 * A fixed limit has the problem of still taking an awful long time.
5525 * Reduce the limit every time running into it. That should work fine for
5526 * deeply linked structures that are not recursively linked and catch
5527 * recursiveness quickly. */
5528 if (!recursive)
5529 tv_equal_recurse_limit = 1000;
5530 if (recursive_cnt >= tv_equal_recurse_limit)
5531 {
5532 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005533 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005534 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005535
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005536 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5537 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005538 if ((tv1->v_type == VAR_FUNC
5539 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5540 && (tv2->v_type == VAR_FUNC
5541 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5542 {
5543 ++recursive_cnt;
5544 r = func_equal(tv1, tv2, ic);
5545 --recursive_cnt;
5546 return r;
5547 }
5548
5549 if (tv1->v_type != tv2->v_type)
5550 return FALSE;
5551
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005552 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005553 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005554 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005555 ++recursive_cnt;
5556 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5557 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005558 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005559
5560 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005561 ++recursive_cnt;
5562 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5563 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005564 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005565
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005566 case VAR_BLOB:
5567 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5568
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005569 case VAR_NUMBER:
5570 return tv1->vval.v_number == tv2->vval.v_number;
5571
5572 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005573 s1 = tv_get_string_buf(tv1, buf1);
5574 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005575 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005576
5577 case VAR_SPECIAL:
5578 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005579
5580 case VAR_FLOAT:
5581#ifdef FEAT_FLOAT
5582 return tv1->vval.v_float == tv2->vval.v_float;
5583#endif
5584 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005586 return tv1->vval.v_job == tv2->vval.v_job;
5587#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005588 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005589#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005590 return tv1->vval.v_channel == tv2->vval.v_channel;
5591#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005592 case VAR_FUNC:
5593 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005594 case VAR_UNKNOWN:
5595 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005596 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005597
Bram Moolenaara03f2332016-02-06 18:09:59 +01005598 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5599 * does not equal anything, not even itself. */
5600 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005601}
5602
5603/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005604 * Return the next (unique) copy ID.
5605 * Used for serializing nested structures.
5606 */
5607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005608get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005609{
5610 current_copyID += COPYID_INC;
5611 return current_copyID;
5612}
5613
5614/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005615 * Garbage collection for lists and dictionaries.
5616 *
5617 * We use reference counts to be able to free most items right away when they
5618 * are no longer used. But for composite items it's possible that it becomes
5619 * unused while the reference count is > 0: When there is a recursive
5620 * reference. Example:
5621 * :let l = [1, 2, 3]
5622 * :let d = {9: l}
5623 * :let l[1] = d
5624 *
5625 * Since this is quite unusual we handle this with garbage collection: every
5626 * once in a while find out which lists and dicts are not referenced from any
5627 * variable.
5628 *
5629 * Here is a good reference text about garbage collection (refers to Python
5630 * but it applies to all reference-counting mechanisms):
5631 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005632 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005633
5634/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005635 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005636 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005637 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005638 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005639 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005640garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005641{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005642 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005643 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005644 buf_T *buf;
5645 win_T *wp;
5646 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005647 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005648 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005649
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005650 if (!testing)
5651 {
5652 /* Only do this once. */
5653 want_garbage_collect = FALSE;
5654 may_garbage_collect = FALSE;
5655 garbage_collect_at_exit = FALSE;
5656 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005657
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005658 /* We advance by two because we add one for items referenced through
5659 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005660 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005661
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005662 /*
5663 * 1. Go through all accessible variables and mark all lists and dicts
5664 * with copyID.
5665 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005666
5667 /* Don't free variables in the previous_funccal list unless they are only
5668 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005669 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005671
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005672 /* script-local variables */
5673 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005674 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005675
5676 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005677 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005678 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5679 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005680
5681 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005682 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005683 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5684 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005685 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005686 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5687 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005688#ifdef FEAT_TEXT_PROP
5689 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5690 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5691 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005692 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005693 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005694 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5695 NULL, NULL);
5696#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005697
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005698 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005699 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005700 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5701 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005702 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005703 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005704
5705 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005707
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005708 /* named functions (matters for closures) */
5709 abort = abort || set_ref_in_functions(copyID);
5710
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005711 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005713
Bram Moolenaard812df62008-11-09 12:46:09 +00005714 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005715 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005716
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005717 // callbacks in buffers
5718 abort = abort || set_ref_in_buffers(copyID);
5719
Bram Moolenaar1dced572012-04-05 16:54:08 +02005720#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005721 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005722#endif
5723
Bram Moolenaardb913952012-06-29 12:54:53 +02005724#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005725 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005726#endif
5727
5728#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005730#endif
5731
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005732#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005733 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005734 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005735#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005736#ifdef FEAT_NETBEANS_INTG
5737 abort = abort || set_ref_in_nb_channel(copyID);
5738#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005739
Bram Moolenaare3188e22016-05-31 21:13:04 +02005740#ifdef FEAT_TIMERS
5741 abort = abort || set_ref_in_timer(copyID);
5742#endif
5743
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005744#ifdef FEAT_QUICKFIX
5745 abort = abort || set_ref_in_quickfix(copyID);
5746#endif
5747
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005748#ifdef FEAT_TERMINAL
5749 abort = abort || set_ref_in_term(copyID);
5750#endif
5751
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005752#ifdef FEAT_TEXT_PROP
5753 abort = abort || set_ref_in_popups(copyID);
5754#endif
5755
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005756 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005757 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005758 /*
5759 * 2. Free lists and dictionaries that are not referenced.
5760 */
5761 did_free = free_unref_items(copyID);
5762
5763 /*
5764 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005765 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005766 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005767 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005768 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005769 else if (p_verbose > 0)
5770 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005771 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005772 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005773
5774 return did_free;
5775}
5776
5777/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005778 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005779 */
5780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005781free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005782{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005783 int did_free = FALSE;
5784
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005785 /* Let all "free" functions know that we are here. This means no
5786 * dictionaries, lists, channels or jobs are to be freed, because we will
5787 * do that here. */
5788 in_free_unref_items = TRUE;
5789
5790 /*
5791 * PASS 1: free the contents of the items. We don't free the items
5792 * themselves yet, so that it is possible to decrement refcount counters
5793 */
5794
Bram Moolenaarcd524592016-07-17 14:57:05 +02005795 /* Go through the list of dicts and free items without the copyID. */
5796 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005797
Bram Moolenaarda861d62016-07-17 15:46:27 +02005798 /* Go through the list of lists and free items without the copyID. */
5799 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005800
5801#ifdef FEAT_JOB_CHANNEL
5802 /* Go through the list of jobs and free items without the copyID. This
5803 * must happen before doing channels, because jobs refer to channels, but
5804 * the reference from the channel to the job isn't tracked. */
5805 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5806
5807 /* Go through the list of channels and free items without the copyID. */
5808 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5809#endif
5810
5811 /*
5812 * PASS 2: free the items themselves.
5813 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005814 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005815 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005816
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005817#ifdef FEAT_JOB_CHANNEL
5818 /* Go through the list of jobs and free items without the copyID. This
5819 * must happen before doing channels, because jobs refer to channels, but
5820 * the reference from the channel to the job isn't tracked. */
5821 free_unused_jobs(copyID, COPYID_MASK);
5822
5823 /* Go through the list of channels and free items without the copyID. */
5824 free_unused_channels(copyID, COPYID_MASK);
5825#endif
5826
5827 in_free_unref_items = FALSE;
5828
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005829 return did_free;
5830}
5831
5832/*
5833 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005834 * "list_stack" is used to add lists to be marked. Can be NULL.
5835 *
5836 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005837 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005839set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005840{
5841 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005842 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005843 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005844 hashtab_T *cur_ht;
5845 ht_stack_T *ht_stack = NULL;
5846 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005847
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005848 cur_ht = ht;
5849 for (;;)
5850 {
5851 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005852 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005853 /* Mark each item in the hashtab. If the item contains a hashtab
5854 * it is added to ht_stack, if it contains a list it is added to
5855 * list_stack. */
5856 todo = (int)cur_ht->ht_used;
5857 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5858 if (!HASHITEM_EMPTY(hi))
5859 {
5860 --todo;
5861 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5862 &ht_stack, list_stack);
5863 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005864 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005865
5866 if (ht_stack == NULL)
5867 break;
5868
5869 /* take an item from the stack */
5870 cur_ht = ht_stack->ht;
5871 tempitem = ht_stack;
5872 ht_stack = ht_stack->prev;
5873 free(tempitem);
5874 }
5875
5876 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005877}
5878
5879/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005880 * Mark a dict and its items with "copyID".
5881 * Returns TRUE if setting references failed somehow.
5882 */
5883 int
5884set_ref_in_dict(dict_T *d, int copyID)
5885{
5886 if (d != NULL && d->dv_copyID != copyID)
5887 {
5888 d->dv_copyID = copyID;
5889 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5890 }
5891 return FALSE;
5892}
5893
5894/*
5895 * Mark a list and its items with "copyID".
5896 * Returns TRUE if setting references failed somehow.
5897 */
5898 int
5899set_ref_in_list(list_T *ll, int copyID)
5900{
5901 if (ll != NULL && ll->lv_copyID != copyID)
5902 {
5903 ll->lv_copyID = copyID;
5904 return set_ref_in_list_items(ll, copyID, NULL);
5905 }
5906 return FALSE;
5907}
5908
5909/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005910 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005911 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5912 *
5913 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005914 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005915 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005916set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005918 listitem_T *li;
5919 int abort = FALSE;
5920 list_T *cur_l;
5921 list_stack_T *list_stack = NULL;
5922 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005923
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005924 cur_l = l;
5925 for (;;)
5926 {
5927 if (!abort)
5928 /* Mark each item in the list. If the item contains a hashtab
5929 * it is added to ht_stack, if it contains a list it is added to
5930 * list_stack. */
5931 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5932 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5933 ht_stack, &list_stack);
5934 if (list_stack == NULL)
5935 break;
5936
5937 /* take an item from the stack */
5938 cur_l = list_stack->list;
5939 tempitem = list_stack;
5940 list_stack = list_stack->prev;
5941 free(tempitem);
5942 }
5943
5944 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005945}
5946
5947/*
5948 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005949 * "list_stack" is used to add lists to be marked. Can be NULL.
5950 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5951 *
5952 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005955set_ref_in_item(
5956 typval_T *tv,
5957 int copyID,
5958 ht_stack_T **ht_stack,
5959 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005961 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005962
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005964 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 dict_T *dd = tv->vval.v_dict;
5966
Bram Moolenaara03f2332016-02-06 18:09:59 +01005967 if (dd != NULL && dd->dv_copyID != copyID)
5968 {
5969 /* Didn't see this dict yet. */
5970 dd->dv_copyID = copyID;
5971 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005972 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005973 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5974 }
5975 else
5976 {
5977 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5978 if (newitem == NULL)
5979 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005980 else
5981 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005982 newitem->ht = &dd->dv_hashtab;
5983 newitem->prev = *ht_stack;
5984 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005985 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005987 }
5988 }
5989 else if (tv->v_type == VAR_LIST)
5990 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005991 list_T *ll = tv->vval.v_list;
5992
Bram Moolenaara03f2332016-02-06 18:09:59 +01005993 if (ll != NULL && ll->lv_copyID != copyID)
5994 {
5995 /* Didn't see this list yet. */
5996 ll->lv_copyID = copyID;
5997 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005998 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005999 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006000 }
6001 else
6002 {
6003 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006004 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006005 if (newitem == NULL)
6006 abort = TRUE;
6007 else
6008 {
6009 newitem->list = ll;
6010 newitem->prev = *list_stack;
6011 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006012 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006013 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006014 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006015 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006016 else if (tv->v_type == VAR_FUNC)
6017 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006018 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006019 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006020 else if (tv->v_type == VAR_PARTIAL)
6021 {
6022 partial_T *pt = tv->vval.v_partial;
6023 int i;
6024
6025 /* A partial does not have a copyID, because it cannot contain itself.
6026 */
6027 if (pt != NULL)
6028 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006029 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006030
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006031 if (pt->pt_dict != NULL)
6032 {
6033 typval_T dtv;
6034
6035 dtv.v_type = VAR_DICT;
6036 dtv.vval.v_dict = pt->pt_dict;
6037 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6038 }
6039
6040 for (i = 0; i < pt->pt_argc; ++i)
6041 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6042 ht_stack, list_stack);
6043 }
6044 }
6045#ifdef FEAT_JOB_CHANNEL
6046 else if (tv->v_type == VAR_JOB)
6047 {
6048 job_T *job = tv->vval.v_job;
6049 typval_T dtv;
6050
6051 if (job != NULL && job->jv_copyID != copyID)
6052 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006053 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006054 if (job->jv_channel != NULL)
6055 {
6056 dtv.v_type = VAR_CHANNEL;
6057 dtv.vval.v_channel = job->jv_channel;
6058 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6059 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006060 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006061 {
6062 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006063 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006064 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6065 }
6066 }
6067 }
6068 else if (tv->v_type == VAR_CHANNEL)
6069 {
6070 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006071 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006072 typval_T dtv;
6073 jsonq_T *jq;
6074 cbq_T *cq;
6075
6076 if (ch != NULL && ch->ch_copyID != copyID)
6077 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006078 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006079 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 {
6081 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6082 jq = jq->jq_next)
6083 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6084 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6085 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006086 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006087 {
6088 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006089 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006090 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6091 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006092 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006093 {
6094 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006095 dtv.vval.v_partial =
6096 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6098 }
6099 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006100 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006101 {
6102 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006103 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6105 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006106 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006107 {
6108 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006109 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006110 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6111 }
6112 }
6113 }
6114#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006115 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006116}
6117
Bram Moolenaar17a13432016-01-24 14:22:10 +01006118 static char *
6119get_var_special_name(int nr)
6120{
6121 switch (nr)
6122 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006123 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006124 case VVAL_TRUE: return "v:true";
6125 case VVAL_NONE: return "v:none";
6126 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006127 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006128 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006129 return "42";
6130}
6131
Bram Moolenaar8c711452005-01-14 21:53:12 +00006132/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 * Return a string with the string representation of a variable.
6134 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006136 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006137 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6138 * stings as "string()", otherwise does not put quotes around strings, as
6139 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006140 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6141 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006142 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006144 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006145echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146 typval_T *tv,
6147 char_u **tofree,
6148 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006149 int copyID,
6150 int echo_style,
6151 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006152 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006154 static int recurse = 0;
6155 char_u *r = NULL;
6156
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006158 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006159 if (!did_echo_string_emsg)
6160 {
6161 /* Only give this message once for a recursive call to avoid
6162 * flooding the user with errors. And stop iterating over lists
6163 * and dicts. */
6164 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006165 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006167 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006168 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006169 }
6170 ++recurse;
6171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 switch (tv->v_type)
6173 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006174 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006175 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006176 {
6177 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006178 r = tv->vval.v_string;
6179 if (r == NULL)
6180 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006181 }
6182 else
6183 {
6184 *tofree = string_quote(tv->vval.v_string, FALSE);
6185 r = *tofree;
6186 }
6187 break;
6188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006190 if (echo_style)
6191 {
6192 *tofree = NULL;
6193 r = tv->vval.v_string;
6194 }
6195 else
6196 {
6197 *tofree = string_quote(tv->vval.v_string, TRUE);
6198 r = *tofree;
6199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006200 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006202 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006203 {
6204 partial_T *pt = tv->vval.v_partial;
6205 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006206 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006207 garray_T ga;
6208 int i;
6209 char_u *tf;
6210
6211 ga_init2(&ga, 1, 100);
6212 ga_concat(&ga, (char_u *)"function(");
6213 if (fname != NULL)
6214 {
6215 ga_concat(&ga, fname);
6216 vim_free(fname);
6217 }
6218 if (pt != NULL && pt->pt_argc > 0)
6219 {
6220 ga_concat(&ga, (char_u *)", [");
6221 for (i = 0; i < pt->pt_argc; ++i)
6222 {
6223 if (i > 0)
6224 ga_concat(&ga, (char_u *)", ");
6225 ga_concat(&ga,
6226 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6227 vim_free(tf);
6228 }
6229 ga_concat(&ga, (char_u *)"]");
6230 }
6231 if (pt != NULL && pt->pt_dict != NULL)
6232 {
6233 typval_T dtv;
6234
6235 ga_concat(&ga, (char_u *)", ");
6236 dtv.v_type = VAR_DICT;
6237 dtv.vval.v_dict = pt->pt_dict;
6238 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6239 vim_free(tf);
6240 }
6241 ga_concat(&ga, (char_u *)")");
6242
6243 *tofree = ga.ga_data;
6244 r = *tofree;
6245 break;
6246 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006247
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006248 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006249 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006250 break;
6251
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006253 if (tv->vval.v_list == NULL)
6254 {
6255 *tofree = NULL;
6256 r = NULL;
6257 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006258 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6259 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006260 {
6261 *tofree = NULL;
6262 r = (char_u *)"[...]";
6263 }
6264 else
6265 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006266 int old_copyID = tv->vval.v_list->lv_copyID;
6267
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006268 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006269 *tofree = list2string(tv, copyID, restore_copyID);
6270 if (restore_copyID)
6271 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006272 r = *tofree;
6273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006275
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006277 if (tv->vval.v_dict == NULL)
6278 {
6279 *tofree = NULL;
6280 r = NULL;
6281 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006282 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6283 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006284 {
6285 *tofree = NULL;
6286 r = (char_u *)"{...}";
6287 }
6288 else
6289 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006290 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006291 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006292 *tofree = dict2string(tv, copyID, restore_copyID);
6293 if (restore_copyID)
6294 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006295 r = *tofree;
6296 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006297 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006299 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006300 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006301 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006302 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006303 break;
6304
Bram Moolenaar835dc632016-02-07 14:27:38 +01006305 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006306 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006308 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006309 if (composite_val)
6310 {
6311 *tofree = string_quote(r, FALSE);
6312 r = *tofree;
6313 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006315
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006316 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006317#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006318 *tofree = NULL;
6319 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6320 r = numbuf;
6321 break;
6322#endif
6323
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006324 case VAR_SPECIAL:
6325 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006326 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006327 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329
Bram Moolenaar8502c702014-06-17 12:51:16 +02006330 if (--recurse == 0)
6331 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006333}
6334
6335/*
6336 * Return a string with the string representation of a variable.
6337 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6338 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006339 * Does not put quotes around strings, as ":echo" displays values.
6340 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6341 * May return NULL.
6342 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006343 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006344echo_string(
6345 typval_T *tv,
6346 char_u **tofree,
6347 char_u *numbuf,
6348 int copyID)
6349{
6350 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6351}
6352
6353/*
6354 * Return a string with the string representation of a variable.
6355 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6356 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006357 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006358 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006359 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361tv2string(
6362 typval_T *tv,
6363 char_u **tofree,
6364 char_u *numbuf,
6365 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006366{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006367 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368}
6369
6370/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 * Return string "str" in ' quotes, doubling ' characters.
6372 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006374 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006375 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006376string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006377{
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006379 char_u *p, *r, *s;
6380
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 len = (function ? 13 : 3);
6382 if (str != NULL)
6383 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006384 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006385 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 if (*p == '\'')
6387 ++len;
6388 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006389 s = r = alloc(len);
6390 if (r != NULL)
6391 {
6392 if (function)
6393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006394 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006395 r += 10;
6396 }
6397 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 if (str != NULL)
6400 for (p = str; *p != NUL; )
6401 {
6402 if (*p == '\'')
6403 *r++ = '\'';
6404 MB_COPY_CHAR(p, r);
6405 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006407 if (function)
6408 *r++ = ')';
6409 *r++ = NUL;
6410 }
6411 return s;
6412}
6413
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006414#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006415/*
6416 * Convert the string "text" to a floating point number.
6417 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6418 * this always uses a decimal point.
6419 * Returns the length of the text that was consumed.
6420 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006422string2float(
6423 char_u *text,
6424 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006425{
6426 char *s = (char *)text;
6427 float_T f;
6428
Bram Moolenaar62473612017-01-08 19:25:40 +01006429 /* MS-Windows does not deal with "inf" and "nan" properly. */
6430 if (STRNICMP(text, "inf", 3) == 0)
6431 {
6432 *value = INFINITY;
6433 return 3;
6434 }
6435 if (STRNICMP(text, "-inf", 3) == 0)
6436 {
6437 *value = -INFINITY;
6438 return 4;
6439 }
6440 if (STRNICMP(text, "nan", 3) == 0)
6441 {
6442 *value = NAN;
6443 return 3;
6444 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006445 f = strtod(s, &s);
6446 *value = f;
6447 return (int)((char_u *)s - text);
6448}
6449#endif
6450
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 * Get the value of an environment variable.
6453 * "arg" is pointing to the '$'. It is advanced to after the name.
6454 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006455 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 */
6457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 char_u *string = NULL;
6461 int len;
6462 int cc;
6463 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006464 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
6466 ++*arg;
6467 name = *arg;
6468 len = get_env_len(arg);
6469 if (evaluate)
6470 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006471 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006472 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006473
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006474 cc = name[len];
6475 name[len] = NUL;
6476 /* first try vim_getenv(), fast for normal environment vars */
6477 string = vim_getenv(name, &mustfree);
6478 if (string != NULL && *string != NUL)
6479 {
6480 if (!mustfree)
6481 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006483 else
6484 {
6485 if (mustfree)
6486 vim_free(string);
6487
6488 /* next try expanding things like $VIM and ${HOME} */
6489 string = expand_env_save(name - 1);
6490 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006491 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006492 }
6493 name[len] = cc;
6494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495 rettv->v_type = VAR_STRING;
6496 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 }
6498
6499 return OK;
6500}
6501
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006502
6503
6504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006506 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006509var2fpos(
6510 typval_T *varp,
6511 int dollar_lnum, /* TRUE when $ is last line */
6512 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006514 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006516 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaara5525202006-03-02 22:52:09 +00006518 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006519 if (varp->v_type == VAR_LIST)
6520 {
6521 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006522 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006523 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006524 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006525
6526 l = varp->vval.v_list;
6527 if (l == NULL)
6528 return NULL;
6529
6530 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006531 pos.lnum = list_find_nr(l, 0L, &error);
6532 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006533 return NULL; /* invalid line number */
6534
6535 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006536 pos.col = list_find_nr(l, 1L, &error);
6537 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006538 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006539 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006540
6541 /* We accept "$" for the column number: last column. */
6542 li = list_find(l, 1L);
6543 if (li != NULL && li->li_tv.v_type == VAR_STRING
6544 && li->li_tv.vval.v_string != NULL
6545 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6546 pos.col = len + 1;
6547
Bram Moolenaara5525202006-03-02 22:52:09 +00006548 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006549 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006550 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006551 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006552
Bram Moolenaara5525202006-03-02 22:52:09 +00006553 /* Get the virtual offset. Defaults to zero. */
6554 pos.coladd = list_find_nr(l, 2L, &error);
6555 if (error)
6556 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006557
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006558 return &pos;
6559 }
6560
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006561 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006562 if (name == NULL)
6563 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006564 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006566 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6567 {
6568 if (VIsual_active)
6569 return &VIsual;
6570 return &curwin->w_cursor;
6571 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006572 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006574 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6576 return NULL;
6577 return pp;
6578 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006579
Bram Moolenaara5525202006-03-02 22:52:09 +00006580 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006581
Bram Moolenaar477933c2007-07-17 14:32:23 +00006582 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006583 {
6584 pos.col = 0;
6585 if (name[1] == '0') /* "w0": first visible line */
6586 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006587 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006588 /* In silent Ex mode topline is zero, but that's not a valid line
6589 * number; use one instead. */
6590 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006591 return &pos;
6592 }
6593 else if (name[1] == '$') /* "w$": last visible line */
6594 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006595 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006596 /* In silent Ex mode botline is zero, return zero then. */
6597 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006598 return &pos;
6599 }
6600 }
6601 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006603 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
6605 pos.lnum = curbuf->b_ml.ml_line_count;
6606 pos.col = 0;
6607 }
6608 else
6609 {
6610 pos.lnum = curwin->w_cursor.lnum;
6611 pos.col = (colnr_T)STRLEN(ml_get_curline());
6612 }
6613 return &pos;
6614 }
6615 return NULL;
6616}
6617
6618/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006619 * Convert list in "arg" into a position and optional file number.
6620 * When "fnump" is NULL there is no file number, only 3 items.
6621 * Note that the column is passed on as-is, the caller may want to decrement
6622 * it to use 1 for the first column.
6623 * Return FAIL when conversion is not possible, doesn't check the position for
6624 * validity.
6625 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006627list2fpos(
6628 typval_T *arg,
6629 pos_T *posp,
6630 int *fnump,
6631 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006632{
6633 list_T *l = arg->vval.v_list;
6634 long i = 0;
6635 long n;
6636
Bram Moolenaar493c1782014-05-28 14:34:46 +02006637 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6638 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006639 if (arg->v_type != VAR_LIST
6640 || l == NULL
6641 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006642 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006643 return FAIL;
6644
6645 if (fnump != NULL)
6646 {
6647 n = list_find_nr(l, i++, NULL); /* fnum */
6648 if (n < 0)
6649 return FAIL;
6650 if (n == 0)
6651 n = curbuf->b_fnum; /* current buffer */
6652 *fnump = n;
6653 }
6654
6655 n = list_find_nr(l, i++, NULL); /* lnum */
6656 if (n < 0)
6657 return FAIL;
6658 posp->lnum = n;
6659
6660 n = list_find_nr(l, i++, NULL); /* col */
6661 if (n < 0)
6662 return FAIL;
6663 posp->col = n;
6664
Bram Moolenaar493c1782014-05-28 14:34:46 +02006665 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006666 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006667 posp->coladd = 0;
6668 else
6669 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006670
Bram Moolenaar493c1782014-05-28 14:34:46 +02006671 if (curswantp != NULL)
6672 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6673
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006674 return OK;
6675}
6676
6677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 * Get the length of an environment variable name.
6679 * Advance "arg" to the first character after the name.
6680 * Return 0 for error.
6681 */
6682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 char_u *p;
6686 int len;
6687
6688 for (p = *arg; vim_isIDc(*p); ++p)
6689 ;
6690 if (p == *arg) /* no name found */
6691 return 0;
6692
6693 len = (int)(p - *arg);
6694 *arg = p;
6695 return len;
6696}
6697
6698/*
6699 * Get the length of the name of a function or internal variable.
6700 * "arg" is advanced to the first non-white character after the name.
6701 * Return 0 if something is wrong.
6702 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 char_u *p;
6707 int len;
6708
6709 /* Find the end of the name. */
6710 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006711 {
6712 if (*p == ':')
6713 {
6714 /* "s:" is start of "s:var", but "n:" is not and can be used in
6715 * slice "[n:]". Also "xx:" is not a namespace. */
6716 len = (int)(p - *arg);
6717 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6718 || len > 1)
6719 break;
6720 }
6721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (p == *arg) /* no name found */
6723 return 0;
6724
6725 len = (int)(p - *arg);
6726 *arg = skipwhite(p);
6727
6728 return len;
6729}
6730
6731/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006732 * Get the length of the name of a variable or function.
6733 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006735 * Return -1 if curly braces expansion failed.
6736 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 * If the name contains 'magic' {}'s, expand them and return the
6738 * expanded name in an allocated string via 'alias' - caller must free.
6739 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741get_name_len(
6742 char_u **arg,
6743 char_u **alias,
6744 int evaluate,
6745 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 char_u *p;
6749 char_u *expr_start;
6750 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752 *alias = NULL; /* default to no alias */
6753
6754 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6755 && (*arg)[2] == (int)KE_SNR)
6756 {
6757 /* hard coded <SNR>, already translated */
6758 *arg += 3;
6759 return get_id_len(arg) + 3;
6760 }
6761 len = eval_fname_script(*arg);
6762 if (len > 0)
6763 {
6764 /* literal "<SID>", "s:" or "<SNR>" */
6765 *arg += len;
6766 }
6767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006769 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006771 p = find_name_end(*arg, &expr_start, &expr_end,
6772 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (expr_start != NULL)
6774 {
6775 char_u *temp_string;
6776
6777 if (!evaluate)
6778 {
6779 len += (int)(p - *arg);
6780 *arg = skipwhite(p);
6781 return len;
6782 }
6783
6784 /*
6785 * Include any <SID> etc in the expanded string:
6786 * Thus the -len here.
6787 */
6788 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6789 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006790 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 *alias = temp_string;
6792 *arg = skipwhite(p);
6793 return (int)STRLEN(temp_string);
6794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006797 // Only give an error when there is something, otherwise it will be
6798 // reported at a higher level.
6799 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006800 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802 return len;
6803}
6804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805/*
6806 * Find the end of a variable or function name, taking care of magic braces.
6807 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6808 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006809 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 * Return a pointer to just after the name. Equal to "arg" if there is no
6811 * valid name.
6812 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006813 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814find_name_end(
6815 char_u *arg,
6816 char_u **expr_start,
6817 char_u **expr_end,
6818 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006820 int mb_nest = 0;
6821 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006823 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006825 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006827 *expr_start = NULL;
6828 *expr_end = NULL;
6829 }
6830
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006831 /* Quick check for valid starting character. */
6832 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6833 return arg;
6834
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006835 for (p = arg; *p != NUL
6836 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006838 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006839 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006840 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006842 if (*p == '\'')
6843 {
6844 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006845 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006846 ;
6847 if (*p == NUL)
6848 break;
6849 }
6850 else if (*p == '"')
6851 {
6852 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006853 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006854 if (*p == '\\' && p[1] != NUL)
6855 ++p;
6856 if (*p == NUL)
6857 break;
6858 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006859 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6860 {
6861 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006862 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006863 len = (int)(p - arg);
6864 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006865 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006866 break;
6867 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006868
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006871 if (*p == '[')
6872 ++br_nest;
6873 else if (*p == ']')
6874 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006877 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879 if (*p == '{')
6880 {
6881 mb_nest++;
6882 if (expr_start != NULL && *expr_start == NULL)
6883 *expr_start = p;
6884 }
6885 else if (*p == '}')
6886 {
6887 mb_nest--;
6888 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6889 *expr_end = p;
6890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893
6894 return p;
6895}
6896
6897/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006898 * Expands out the 'magic' {}'s in a variable/function name.
6899 * Note that this can call itself recursively, to deal with
6900 * constructs like foo{bar}{baz}{bam}
6901 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6902 * "in_start" ^
6903 * "expr_start" ^
6904 * "expr_end" ^
6905 * "in_end" ^
6906 *
6907 * Returns a new allocated string, which the caller must free.
6908 * Returns NULL for failure.
6909 */
6910 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006911make_expanded_name(
6912 char_u *in_start,
6913 char_u *expr_start,
6914 char_u *expr_end,
6915 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006916{
6917 char_u c1;
6918 char_u *retval = NULL;
6919 char_u *temp_result;
6920 char_u *nextcmd = NULL;
6921
6922 if (expr_end == NULL || in_end == NULL)
6923 return NULL;
6924 *expr_start = NUL;
6925 *expr_end = NUL;
6926 c1 = *in_end;
6927 *in_end = NUL;
6928
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006929 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006930 if (temp_result != NULL && nextcmd == NULL)
6931 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006932 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6933 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006934 if (retval != NULL)
6935 {
6936 STRCPY(retval, in_start);
6937 STRCAT(retval, temp_result);
6938 STRCAT(retval, expr_end + 1);
6939 }
6940 }
6941 vim_free(temp_result);
6942
6943 *in_end = c1; /* put char back for error messages */
6944 *expr_start = '{';
6945 *expr_end = '}';
6946
6947 if (retval != NULL)
6948 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006949 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006950 if (expr_start != NULL)
6951 {
6952 /* Further expansion! */
6953 temp_result = make_expanded_name(retval, expr_start,
6954 expr_end, temp_result);
6955 vim_free(retval);
6956 retval = temp_result;
6957 }
6958 }
6959
6960 return retval;
6961}
6962
6963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006967 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006968eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006970 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6971}
6972
6973/*
6974 * Return TRUE if character "c" can be used as the first character in a
6975 * variable or function name (excluding '{' and '}').
6976 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006978eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006979{
6980 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981}
6982
6983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 * Set number v: variable to "val".
6985 */
6986 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006987set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990}
6991
6992/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02006993 * Get typval_T v: variable value.
6994 */
6995 typval_T *
6996get_vim_var_tv(int idx)
6997{
6998 return &vimvars[idx].vv_tv;
6999}
7000
7001/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007002 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007004 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007007 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008}
7009
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007010/*
7011 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007012 * If the String variable has never been set, return an empty string.
7013 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007014 */
7015 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007016get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007017{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007018 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007019}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007022 * Get List v: variable value. Caller must take care of reference count when
7023 * needed.
7024 */
7025 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007027{
7028 return vimvars[idx].vv_list;
7029}
7030
7031/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007032 * Get Dict v: variable value. Caller must take care of reference count when
7033 * needed.
7034 */
7035 dict_T *
7036get_vim_var_dict(int idx)
7037{
7038 return vimvars[idx].vv_dict;
7039}
7040
7041/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007042 * Set v:char to character "c".
7043 */
7044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007045set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007046{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007047 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007048
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007049 if (has_mbyte)
7050 buf[(*mb_char2bytes)(c, buf)] = NUL;
7051 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007052 {
7053 buf[0] = c;
7054 buf[1] = NUL;
7055 }
7056 set_vim_var_string(VV_CHAR, buf, -1);
7057}
7058
7059/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007060 * Set v:count to "count" and v:count1 to "count1".
7061 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 */
7063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007064set_vcount(
7065 long count,
7066 long count1,
7067 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007069 if (set_prevcount)
7070 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071 vimvars[VV_COUNT].vv_nr = count;
7072 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073}
7074
7075/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007076 * Save variables that might be changed as a side effect. Used when executing
7077 * a timer callback.
7078 */
7079 void
7080save_vimvars(vimvars_save_T *vvsave)
7081{
7082 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7083 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7084 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7085}
7086
7087/*
7088 * Restore variables saved by save_vimvars().
7089 */
7090 void
7091restore_vimvars(vimvars_save_T *vvsave)
7092{
7093 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7094 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7095 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7096}
7097
7098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 * Set string v: variable to a copy of "val".
7100 */
7101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102set_vim_var_string(
7103 int idx,
7104 char_u *val,
7105 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaara542c682016-01-31 16:28:04 +01007107 clear_tv(&vimvars[idx].vv_di.di_tv);
7108 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115}
7116
7117/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007118 * Set List v: variable to "val".
7119 */
7120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007121set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007122{
Bram Moolenaara542c682016-01-31 16:28:04 +01007123 clear_tv(&vimvars[idx].vv_di.di_tv);
7124 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007125 vimvars[idx].vv_list = val;
7126 if (val != NULL)
7127 ++val->lv_refcount;
7128}
7129
7130/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007131 * Set Dictionary v: variable to "val".
7132 */
7133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007134set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007135{
Bram Moolenaara542c682016-01-31 16:28:04 +01007136 clear_tv(&vimvars[idx].vv_di.di_tv);
7137 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007138 vimvars[idx].vv_dict = val;
7139 if (val != NULL)
7140 {
7141 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007142 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007143 }
7144}
7145
7146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 * Set v:register if needed.
7148 */
7149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007150set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 char_u regname;
7153
7154 if (c == 0 || c == ' ')
7155 regname = '"';
7156 else
7157 regname = c;
7158 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 set_vim_var_string(VV_REG, &regname, 1);
7161}
7162
7163/*
7164 * Get or set v:exception. If "oldval" == NULL, return the current value.
7165 * Otherwise, restore the value to "oldval" and return NULL.
7166 * Must always be called in pairs to save and restore v:exception! Does not
7167 * take care of memory allocations.
7168 */
7169 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
7172 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 return NULL;
7177}
7178
7179/*
7180 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7181 * Otherwise, restore the value to "oldval" and return NULL.
7182 * Must always be called in pairs to save and restore v:throwpoint! Does not
7183 * take care of memory allocations.
7184 */
7185 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007186v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007189 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 return NULL;
7193}
7194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195/*
7196 * Set v:cmdarg.
7197 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7198 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7199 * Must always be called in pairs!
7200 */
7201 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 char_u *oldval;
7205 char_u *newval;
7206 unsigned len;
7207
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007209 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007211 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007213 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007216 if (eap->force_bin == FORCE_BIN)
7217 len = 6;
7218 else if (eap->force_bin == FORCE_NOBIN)
7219 len = 8;
7220 else
7221 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007222
7223 if (eap->read_edit)
7224 len += 7;
7225
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007226 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007227 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007228 if (eap->force_enc != 0)
7229 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007230 if (eap->bad_char != 0)
7231 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007232
7233 newval = alloc(len + 1);
7234 if (newval == NULL)
7235 return NULL;
7236
7237 if (eap->force_bin == FORCE_BIN)
7238 sprintf((char *)newval, " ++bin");
7239 else if (eap->force_bin == FORCE_NOBIN)
7240 sprintf((char *)newval, " ++nobin");
7241 else
7242 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007243
7244 if (eap->read_edit)
7245 STRCAT(newval, " ++edit");
7246
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007247 if (eap->force_ff != 0)
7248 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007249 eap->force_ff == 'u' ? "unix"
7250 : eap->force_ff == 'd' ? "dos"
7251 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007252 if (eap->force_enc != 0)
7253 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7254 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007255 if (eap->bad_char == BAD_KEEP)
7256 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7257 else if (eap->bad_char == BAD_DROP)
7258 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7259 else if (eap->bad_char != 0)
7260 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007262 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265/*
7266 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007267 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270get_var_tv(
7271 char_u *name,
7272 int len, /* length of "name" */
7273 typval_T *rettv, /* NULL when only checking existence */
7274 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7275 int verbose, /* may give error message */
7276 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
7278 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 /* truncate the name, so that we can use strcmp() */
7284 cc = name[len];
7285 name[len] = NUL;
7286
7287 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 * Check for user-defined variables.
7289 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007290 v = find_var(name, NULL, no_autoload);
7291 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007293 tv = &v->di_tv;
7294 if (dip != NULL)
7295 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007300 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 ret = FAIL;
7303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007304 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306
7307 name[len] = cc;
7308
7309 return ret;
7310}
7311
7312/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007313 * Check if variable "name[len]" is a local variable or an argument.
7314 * If so, "*eval_lavars_used" is set to TRUE.
7315 */
7316 static void
7317check_vars(char_u *name, int len)
7318{
7319 int cc;
7320 char_u *varname;
7321 hashtab_T *ht;
7322
7323 if (eval_lavars_used == NULL)
7324 return;
7325
7326 /* truncate the name, so that we can use strcmp() */
7327 cc = name[len];
7328 name[len] = NUL;
7329
7330 ht = find_var_ht(name, &varname);
7331 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7332 {
7333 if (find_var(name, NULL, TRUE) != NULL)
7334 *eval_lavars_used = TRUE;
7335 }
7336
7337 name[len] = cc;
7338}
7339
7340/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007341 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7342 * Also handle function call with Funcref variable: func(expr)
7343 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7344 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007346handle_subscript(
7347 char_u **arg,
7348 typval_T *rettv,
7349 int evaluate, /* do more than finding the end */
7350 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007351{
7352 int ret = OK;
7353 dict_T *selfdict = NULL;
7354 char_u *s;
7355 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007356 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007357
7358 while (ret == OK
7359 && (**arg == '['
7360 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007361 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7362 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007363 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007364 {
7365 if (**arg == '(')
7366 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007367 partial_T *pt = NULL;
7368
Bram Moolenaard9fba312005-06-26 22:34:35 +00007369 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007370 if (evaluate)
7371 {
7372 functv = *rettv;
7373 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007374
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007375 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007376 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007377 {
7378 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007379 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007380 }
7381 else
7382 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007383 }
7384 else
7385 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007386 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007387 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007388 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007389
7390 /* Clear the funcref afterwards, so that deleting it while
7391 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007392 if (evaluate)
7393 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007394
7395 /* Stop the expression evaluation when immediately aborting on
7396 * error, or when an interrupt occurred or an exception was thrown
7397 * but not caught. */
7398 if (aborting())
7399 {
7400 if (ret == OK)
7401 clear_tv(rettv);
7402 ret = FAIL;
7403 }
7404 dict_unref(selfdict);
7405 selfdict = NULL;
7406 }
7407 else /* **arg == '[' || **arg == '.' */
7408 {
7409 dict_unref(selfdict);
7410 if (rettv->v_type == VAR_DICT)
7411 {
7412 selfdict = rettv->vval.v_dict;
7413 if (selfdict != NULL)
7414 ++selfdict->dv_refcount;
7415 }
7416 else
7417 selfdict = NULL;
7418 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7419 {
7420 clear_tv(rettv);
7421 ret = FAIL;
7422 }
7423 }
7424 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007425
Bram Moolenaar1d429612016-05-24 15:44:17 +02007426 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7427 * Don't do this when "Func" is already a partial that was bound
7428 * explicitly (pt_auto is FALSE). */
7429 if (selfdict != NULL
7430 && (rettv->v_type == VAR_FUNC
7431 || (rettv->v_type == VAR_PARTIAL
7432 && (rettv->vval.v_partial->pt_auto
7433 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007434 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007435
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007436 dict_unref(selfdict);
7437 return ret;
7438}
7439
7440/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007441 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007442 * value).
7443 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007444 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007445alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007446{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007447 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007448}
7449
7450/*
7451 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 * The string "s" must have been allocated, it is consumed.
7453 * Return NULL for out of memory, the variable otherwise.
7454 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007456alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460 rettv = alloc_tv();
7461 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463 rettv->v_type = VAR_STRING;
7464 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466 else
7467 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007468 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469}
7470
7471/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
7477 if (varp != NULL)
7478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007479 switch (varp->v_type)
7480 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007481 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007482 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007483 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007484 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485 vim_free(varp->vval.v_string);
7486 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007487 case VAR_PARTIAL:
7488 partial_unref(varp->vval.v_partial);
7489 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007490 case VAR_BLOB:
7491 blob_unref(varp->vval.v_blob);
7492 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007493 case VAR_LIST:
7494 list_unref(varp->vval.v_list);
7495 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007496 case VAR_DICT:
7497 dict_unref(varp->vval.v_dict);
7498 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007499 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007501 job_unref(varp->vval.v_job);
7502 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007503#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007504 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007506 channel_unref(varp->vval.v_channel);
7507 break;
7508#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007509 case VAR_NUMBER:
7510 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007511 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007512 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007513 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 vim_free(varp);
7516 }
7517}
7518
7519/*
7520 * Free the memory for a variable value and set the value to NULL or 0.
7521 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525 if (varp != NULL)
7526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007527 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007529 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007530 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007531 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007532 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007533 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007535 case VAR_PARTIAL:
7536 partial_unref(varp->vval.v_partial);
7537 varp->vval.v_partial = NULL;
7538 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007539 case VAR_BLOB:
7540 blob_unref(varp->vval.v_blob);
7541 varp->vval.v_blob = NULL;
7542 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007543 case VAR_LIST:
7544 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007546 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 case VAR_DICT:
7548 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007552 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007553 varp->vval.v_number = 0;
7554 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007555 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007556#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007557 varp->vval.v_float = 0.0;
7558 break;
7559#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007560 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007561#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007562 job_unref(varp->vval.v_job);
7563 varp->vval.v_job = NULL;
7564#endif
7565 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007566 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007567#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007568 channel_unref(varp->vval.v_channel);
7569 varp->vval.v_channel = NULL;
7570#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007571 case VAR_UNKNOWN:
7572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007574 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 }
7576}
7577
7578/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579 * Set the value of a variable to NULL without freeing items.
7580 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007582init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583{
7584 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586}
7587
7588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 * Get the number value of a variable.
7590 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007591 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007592 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007593 * caller of incompatible types: it sets *denote to TRUE if "denote"
7594 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007596 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 int error = FALSE;
7600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007601 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007602}
7603
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007604 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007605tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007606{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007607 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007612 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007613 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007614#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616 break;
7617#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007618 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007619 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007620 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 break;
7622 case VAR_STRING:
7623 if (varp->vval.v_string != NULL)
7624 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007625 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007626 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007627 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007628 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007629 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007630 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007631 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007632 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007633 case VAR_SPECIAL:
7634 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7635 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007636 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007637#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007638 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007639 break;
7640#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007641 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007642#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007644 break;
7645#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007646 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007647 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007648 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007649 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007650 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007653 if (denote == NULL) /* useful for values that must be unsigned */
7654 n = -1;
7655 else
7656 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007657 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658}
7659
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007660#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007662tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007663{
7664 switch (varp->v_type)
7665 {
7666 case VAR_NUMBER:
7667 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007668 case VAR_FLOAT:
7669 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007670 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007671 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007673 break;
7674 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007675 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007676 break;
7677 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007679 break;
7680 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007681 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007682 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007683 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007684 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007685 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007686 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007687# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007688 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007689 break;
7690# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007691 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007692# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007693 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007694 break;
7695# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007696 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007697 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007698 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007699 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007701 break;
7702 }
7703 return 0;
7704}
7705#endif
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 * Get the string value of a variable.
7709 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007710 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7711 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 * If the String variable has never been set, return an empty string.
7713 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007714 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007715 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007717 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007718tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 static char_u mybuf[NUMBUFLEN];
7721
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007722 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007725 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007726tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007728 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007729
7730 return res != NULL ? res : (char_u *)"";
7731}
7732
Bram Moolenaar7d647822014-04-05 21:28:56 +02007733/*
7734 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7735 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007736 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007737tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738{
7739 static char_u mybuf[NUMBUFLEN];
7740
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007741 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742}
7743
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007744 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007745tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007746{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007747 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007750 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007751 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 return buf;
7753 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007754 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007755 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 break;
7757 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007758 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 break;
7760 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007761 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007763 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007764#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007765 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007766 break;
7767#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 case VAR_STRING:
7769 if (varp->vval.v_string != NULL)
7770 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007771 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007772 case VAR_SPECIAL:
7773 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7774 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007775 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007776 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007777 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007778 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007779#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007780 {
7781 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007782 char *status;
7783
7784 if (job == NULL)
7785 return (char_u *)"no process";
7786 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007787 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007788 : "run";
7789# ifdef UNIX
7790 vim_snprintf((char *)buf, NUMBUFLEN,
7791 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007792# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007793 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007794 "process %ld %s",
7795 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007796 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007797# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007798 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007799 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7800# endif
7801 return buf;
7802 }
7803#endif
7804 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007805 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007806#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007807 {
7808 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007809 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007810
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007811 if (channel == NULL)
7812 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7813 else
7814 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007815 "channel %d %s", channel->ch_id, status);
7816 return buf;
7817 }
7818#endif
7819 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007820 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007821 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007824 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825}
7826
7827/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007828 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7829 * string() on Dict, List, etc.
7830 */
7831 char_u *
7832tv_stringify(typval_T *varp, char_u *buf)
7833{
7834 if (varp->v_type == VAR_LIST
7835 || varp->v_type == VAR_DICT
7836 || varp->v_type == VAR_FUNC
7837 || varp->v_type == VAR_PARTIAL
7838 || varp->v_type == VAR_FLOAT)
7839 {
7840 typval_T tmp;
7841
7842 f_string(varp, &tmp);
7843 tv_get_string_buf(&tmp, buf);
7844 clear_tv(varp);
7845 *varp = tmp;
7846 return tmp.vval.v_string;
7847 }
7848 return tv_get_string_buf(varp, buf);
7849}
7850
7851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 * Find variable "name" in the list of variables.
7853 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007855 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007858 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007859find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007863 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
Bram Moolenaara7043832005-01-21 11:56:39 +00007865 ht = find_var_ht(name, &varname);
7866 if (htp != NULL)
7867 *htp = ht;
7868 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007870 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7871 if (ret != NULL)
7872 return ret;
7873
7874 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007875 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876}
7877
7878/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007879 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007880 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007883find_var_in_ht(
7884 hashtab_T *ht,
7885 int htname,
7886 char_u *varname,
7887 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007888{
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 hashitem_T *hi;
7890
7891 if (*varname == NUL)
7892 {
7893 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007894 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007896 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 case 'g': return &globvars_var;
7898 case 'v': return &vimvars_var;
7899 case 'b': return &curbuf->b_bufvar;
7900 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007901 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007902 case 'l': return get_funccal_local_var();
7903 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 }
7905 return NULL;
7906 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007907
7908 hi = hash_find(ht, varname);
7909 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007910 {
7911 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007912 * worked find the variable again. Don't auto-load a script if it was
7913 * loaded already, otherwise it would be loaded every time when
7914 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007915 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007916 {
7917 /* Note: script_autoload() may make "hi" invalid. It must either
7918 * be obtained again or not used. */
7919 if (!script_autoload(varname, FALSE) || aborting())
7920 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007921 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007922 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007923 if (HASHITEM_EMPTY(hi))
7924 return NULL;
7925 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007927}
7928
7929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007931 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007932 * Set "varname" to the start of name without ':'.
7933 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007934 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007935find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007937 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007938 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007939
Bram Moolenaar73627d02015-08-11 15:46:09 +02007940 if (name[0] == NUL)
7941 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 if (name[1] != ':')
7943 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007944 /* The name must not start with a colon or #. */
7945 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 return NULL;
7947 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007948
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007949 // "version" is "v:version" in all scopes if scriptversion < 3.
7950 // Same for a few other variables marked with VV_COMPAT.
7951 if (current_sctx.sc_version < 3)
7952 {
7953 hi = hash_find(&compat_hashtab, name);
7954 if (!HASHITEM_EMPTY(hi))
7955 return &compat_hashtab;
7956 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007957
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007958 ht = get_funccal_local_ht();
7959 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007961 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007964 if (*name == 'g') /* global variable */
7965 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02007966 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007967 if (vim_strchr(name + 2, ':') != NULL
7968 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007969 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007971 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007973 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007974 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007975 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 if (*name == 'v') /* v: variable */
7977 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007978 if (*name == 'a') /* a: function argument */
7979 return get_funccal_args_ht();
7980 if (*name == 'l') /* l: local function variable */
7981 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007983 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7984 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 return NULL;
7986}
7987
7988/*
7989 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007990 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 * Returns NULL when it doesn't exist.
7992 */
7993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007994get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
Bram Moolenaar33570922005-01-25 22:26:29 +00007996 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007998 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (v == NULL)
8000 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008001 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002}
8003
8004/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 * sourcing this script and when executing functions defined in the script.
8007 */
8008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008009new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
Bram Moolenaara7043832005-01-21 11:56:39 +00008011 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008012 hashtab_T *ht;
8013 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008014
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8016 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008017 /* Re-allocating ga_data means that an ht_array pointing to
8018 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008020 for (i = 1; i <= ga_scripts.ga_len; ++i)
8021 {
8022 ht = &SCRIPT_VARS(i);
8023 if (ht->ht_mask == HT_INIT_SIZE - 1)
8024 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008025 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008027 }
8028
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 while (ga_scripts.ga_len < id)
8030 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008031 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008032 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008033 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 }
8037}
8038
8039/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8041 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 */
8043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008044init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008047 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008048 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008049 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008050 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 dict_var->di_tv.vval.v_dict = dict;
8052 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008053 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8055 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056}
8057
8058/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008059 * Unreference a dictionary initialized by init_var_dict().
8060 */
8061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008062unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008063{
8064 /* Now the dict needs to be freed if no one else is using it, go back to
8065 * normal reference counting. */
8066 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8067 dict_unref(dict);
8068}
8069
8070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 * Frees all allocated variables and the value they contain.
8073 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 */
8075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008076vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008077{
8078 vars_clear_ext(ht, TRUE);
8079}
8080
8081/*
8082 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8083 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
Bram Moolenaara7043832005-01-21 11:56:39 +00008087 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008088 hashitem_T *hi;
8089 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008092 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008093 for (hi = ht->ht_array; todo > 0; ++hi)
8094 {
8095 if (!HASHITEM_EMPTY(hi))
8096 {
8097 --todo;
8098
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008100 * ht_array might change then. hash_clear() takes care of it
8101 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008102 v = HI2DI(hi);
8103 if (free_val)
8104 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008105 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008106 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008107 }
8108 }
8109 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008110 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111}
8112
Bram Moolenaara7043832005-01-21 11:56:39 +00008113/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 * Delete a variable from hashtab "ht" at item "hi".
8115 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008118delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
Bram Moolenaar33570922005-01-25 22:26:29 +00008120 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008121
8122 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 clear_tv(&di->di_tv);
8124 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125}
8126
8127/*
8128 * List the value of one internal variable.
8129 */
8130 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008131list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 char_u *tofree;
8134 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008135 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008137 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008139 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141}
8142
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008144list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008145 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008146 char_u *name,
8147 int type,
8148 char_u *string,
8149 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
Bram Moolenaar31859182007-08-14 20:41:13 +00008151 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8152 msg_start();
8153 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008155 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 msg_putchar(' ');
8157 msg_advance(22);
8158 if (type == VAR_NUMBER)
8159 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008160 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008161 msg_putchar('*');
8162 else if (type == VAR_LIST)
8163 {
8164 msg_putchar('[');
8165 if (*string == '[')
8166 ++string;
8167 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008168 else if (type == VAR_DICT)
8169 {
8170 msg_putchar('{');
8171 if (*string == '{')
8172 ++string;
8173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 else
8175 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008176
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008179 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008180 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008181 if (*first)
8182 {
8183 msg_clr_eos();
8184 *first = FALSE;
8185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186}
8187
8188/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * If the variable already exists, the value is updated.
8191 * Otherwise the variable is created.
8192 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008194set_var(
8195 char_u *name,
8196 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008197 int copy) // make copy of value in "tv"
8198{
8199 set_var_const(name, tv, copy, FALSE);
8200}
8201
8202/*
8203 * Set variable "name" to value in "tv".
8204 * If the variable already exists and "is_const" is FALSE the value is updated.
8205 * Otherwise the variable is created.
8206 */
8207 static void
8208set_var_const(
8209 char_u *name,
8210 typval_T *tv,
8211 int copy, // make copy of value in "tv"
8212 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213{
Bram Moolenaar33570922005-01-25 22:26:29 +00008214 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008216 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008218 ht = find_var_ht(name, &varname);
8219 if (ht == NULL || *varname == NUL)
8220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008221 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008222 return;
8223 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008224 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008225
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008226 /* Search in parent scope which is possible to reference from lambda */
8227 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008228 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008229
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008230 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8231 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008232 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008233
Bram Moolenaar33570922005-01-25 22:26:29 +00008234 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008236 if (is_const)
8237 {
8238 emsg(_(e_cannot_mod));
8239 return;
8240 }
8241
Bram Moolenaar33570922005-01-25 22:26:29 +00008242 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008243 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008244 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008245 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008246
8247 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008248 * Handle setting internal v: variables separately where needed to
8249 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 */
8251 if (ht == &vimvarht)
8252 {
8253 if (v->di_tv.v_type == VAR_STRING)
8254 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008255 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008257 {
8258 char_u *val = tv_get_string(tv);
8259
8260 // Careful: when assigning to v:errmsg and tv_get_string()
8261 // causes an error message the variable will alrady be set.
8262 if (v->di_tv.vval.v_string == NULL)
8263 v->di_tv.vval.v_string = vim_strsave(val);
8264 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 else
8266 {
8267 /* Take over the string to avoid an extra alloc/free. */
8268 v->di_tv.vval.v_string = tv->vval.v_string;
8269 tv->vval.v_string = NULL;
8270 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008271 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008273 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008275 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008276 if (STRCMP(varname, "searchforward") == 0)
8277 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008278#ifdef FEAT_SEARCH_EXTRA
8279 else if (STRCMP(varname, "hlsearch") == 0)
8280 {
8281 no_hlsearch = !v->di_tv.vval.v_number;
8282 redraw_all_later(SOME_VALID);
8283 }
8284#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008285 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008286 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008287 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008288 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008289 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008290 return;
8291 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 }
8293
8294 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296 else /* add a new variable */
8297 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008298 // Can't add "v:" or "a:" variable.
8299 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008301 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008302 return;
8303 }
8304
Bram Moolenaar31b81602019-02-10 22:14:27 +01008305 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008306 if (!valid_varname(varname))
8307 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008308
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008309 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008310 if (v == NULL)
8311 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008315 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 return;
8317 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008318 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008319 if (is_const)
8320 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008322
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008323 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008325 else
8326 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008328 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008330 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008331
8332 if (is_const)
8333 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334}
8335
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008336/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008337 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 * Also give an error message.
8339 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008341var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008342{
8343 if (flags & DI_FLAGS_RO)
8344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008345 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 return TRUE;
8347 }
8348 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008350 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 return TRUE;
8352 }
8353 return FALSE;
8354}
8355
8356/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008357 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8358 * Also give an error message.
8359 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008361var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008362{
8363 if (flags & DI_FLAGS_FIX)
8364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008365 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008366 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008367 return TRUE;
8368 }
8369 return FALSE;
8370}
8371
8372/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008373 * Check if a funcref is assigned to a valid variable name.
8374 * Return TRUE and give an error if not.
8375 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008376 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008377var_check_func_name(
8378 char_u *name, /* points to start of variable name */
8379 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008380{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008381 /* Allow for w: b: s: and t:. */
8382 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008383 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8384 ? name[2] : name[0]))
8385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008387 name);
8388 return TRUE;
8389 }
8390 /* Don't allow hiding a function. When "v" is not NULL we might be
8391 * assigning another function to the same var, the type is checked
8392 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008393 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008395 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008396 name);
8397 return TRUE;
8398 }
8399 return FALSE;
8400}
8401
8402/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008403 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008404 * Also give an error message, using "name" or _("name") when use_gettext is
8405 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008406 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008407 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008408var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008409{
8410 if (lock & VAR_LOCKED)
8411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008412 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008413 name == NULL ? (char_u *)_("Unknown")
8414 : use_gettext ? (char_u *)_(name)
8415 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008416 return TRUE;
8417 }
8418 if (lock & VAR_FIXED)
8419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008420 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008421 name == NULL ? (char_u *)_("Unknown")
8422 : use_gettext ? (char_u *)_(name)
8423 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008424 return TRUE;
8425 }
8426 return FALSE;
8427}
8428
8429/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008430 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8431 * Also give an error message, using "name" or _("name") when use_gettext is
8432 * TRUE.
8433 */
8434 static int
8435tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8436{
8437 int lock = 0;
8438
8439 switch (tv->v_type)
8440 {
8441 case VAR_BLOB:
8442 if (tv->vval.v_blob != NULL)
8443 lock = tv->vval.v_blob->bv_lock;
8444 break;
8445 case VAR_LIST:
8446 if (tv->vval.v_list != NULL)
8447 lock = tv->vval.v_list->lv_lock;
8448 break;
8449 case VAR_DICT:
8450 if (tv->vval.v_dict != NULL)
8451 lock = tv->vval.v_dict->dv_lock;
8452 break;
8453 default:
8454 break;
8455 }
8456 return var_check_lock(tv->v_lock, name, use_gettext)
8457 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8458}
8459
8460/*
8461 * Check if a variable name is valid.
8462 * Return FALSE and give an error if not.
8463 */
8464 int
8465valid_varname(char_u *varname)
8466{
8467 char_u *p;
8468
8469 for (p = varname; *p != NUL; ++p)
8470 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8471 && *p != AUTOLOAD_CHAR)
8472 {
8473 semsg(_(e_illvar), varname);
8474 return FALSE;
8475 }
8476 return TRUE;
8477}
8478
8479/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008480 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008482 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008483 * It is OK for "from" and "to" to point to the same item. This is used to
8484 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008485 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008487copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008489 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008490 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008491 switch (from->v_type)
8492 {
8493 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008494 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008495 to->vval.v_number = from->vval.v_number;
8496 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008497 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008498#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008499 to->vval.v_float = from->vval.v_float;
8500 break;
8501#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008502 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008504 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008505 if (to->vval.v_job != NULL)
8506 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008507 break;
8508#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008509 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008511 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008512 if (to->vval.v_channel != NULL)
8513 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008514 break;
8515#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 case VAR_STRING:
8517 case VAR_FUNC:
8518 if (from->vval.v_string == NULL)
8519 to->vval.v_string = NULL;
8520 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008521 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008523 if (from->v_type == VAR_FUNC)
8524 func_ref(to->vval.v_string);
8525 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008527 case VAR_PARTIAL:
8528 if (from->vval.v_partial == NULL)
8529 to->vval.v_partial = NULL;
8530 else
8531 {
8532 to->vval.v_partial = from->vval.v_partial;
8533 ++to->vval.v_partial->pt_refcount;
8534 }
8535 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008536 case VAR_BLOB:
8537 if (from->vval.v_blob == NULL)
8538 to->vval.v_blob = NULL;
8539 else
8540 {
8541 to->vval.v_blob = from->vval.v_blob;
8542 ++to->vval.v_blob->bv_refcount;
8543 }
8544 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 case VAR_LIST:
8546 if (from->vval.v_list == NULL)
8547 to->vval.v_list = NULL;
8548 else
8549 {
8550 to->vval.v_list = from->vval.v_list;
8551 ++to->vval.v_list->lv_refcount;
8552 }
8553 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 case VAR_DICT:
8555 if (from->vval.v_dict == NULL)
8556 to->vval.v_dict = NULL;
8557 else
8558 {
8559 to->vval.v_dict = from->vval.v_dict;
8560 ++to->vval.v_dict->dv_refcount;
8561 }
8562 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008563 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008564 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565 break;
8566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567}
8568
8569/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570 * Make a copy of an item.
8571 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008572 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8573 * reference to an already copied list/dict can be used.
8574 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008577item_copy(
8578 typval_T *from,
8579 typval_T *to,
8580 int deep,
8581 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008582{
8583 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008588 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590 }
8591 ++recurse;
8592
8593 switch (from->v_type)
8594 {
8595 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008596 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 case VAR_STRING:
8598 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008599 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008600 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008601 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008602 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 copy_tv(from, to);
8604 break;
8605 case VAR_LIST:
8606 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008607 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008608 if (from->vval.v_list == NULL)
8609 to->vval.v_list = NULL;
8610 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8611 {
8612 /* use the copy made earlier */
8613 to->vval.v_list = from->vval.v_list->lv_copylist;
8614 ++to->vval.v_list->lv_refcount;
8615 }
8616 else
8617 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8618 if (to->vval.v_list == NULL)
8619 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008621 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008622 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008623 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008624 case VAR_DICT:
8625 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008626 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008627 if (from->vval.v_dict == NULL)
8628 to->vval.v_dict = NULL;
8629 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8630 {
8631 /* use the copy made earlier */
8632 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8633 ++to->vval.v_dict->dv_refcount;
8634 }
8635 else
8636 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8637 if (to->vval.v_dict == NULL)
8638 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008639 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008640 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008641 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008642 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 }
8644 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008645 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646}
8647
8648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 * This function is used by f_input() and f_inputdialog() functions. The third
8650 * argument to f_input() specifies the type of completion to use at the
8651 * prompt. The third argument to f_inputdialog() specifies the value to return
8652 * when the user cancels the prompt.
8653 */
8654 void
8655get_user_input(
8656 typval_T *argvars,
8657 typval_T *rettv,
8658 int inputdialog,
8659 int secret)
8660{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 char_u *p = NULL;
8663 int c;
8664 char_u buf[NUMBUFLEN];
8665 int cmd_silent_save = cmd_silent;
8666 char_u *defstr = (char_u *)"";
8667 int xp_type = EXPAND_NOTHING;
8668 char_u *xp_arg = NULL;
8669
8670 rettv->v_type = VAR_STRING;
8671 rettv->vval.v_string = NULL;
8672
8673#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008674 /* While starting up, there is no place to enter text. When running tests
8675 * with --not-a-term we assume feedkeys() will be used. */
8676 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 return;
8678#endif
8679
8680 cmd_silent = FALSE; /* Want to see the prompt. */
8681 if (prompt != NULL)
8682 {
8683 /* Only the part of the message after the last NL is considered as
8684 * prompt for the command line */
8685 p = vim_strrchr(prompt, '\n');
8686 if (p == NULL)
8687 p = prompt;
8688 else
8689 {
8690 ++p;
8691 c = *p;
8692 *p = NUL;
8693 msg_start();
8694 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008695 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 msg_didout = FALSE;
8697 msg_starthere();
8698 *p = c;
8699 }
8700 cmdline_row = msg_row;
8701
8702 if (argvars[1].v_type != VAR_UNKNOWN)
8703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008704 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 if (defstr != NULL)
8706 stuffReadbuffSpec(defstr);
8707
8708 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8709 {
8710 char_u *xp_name;
8711 int xp_namelen;
8712 long argt;
8713
8714 /* input() with a third argument: completion */
8715 rettv->vval.v_string = NULL;
8716
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008717 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 if (xp_name == NULL)
8719 return;
8720
8721 xp_namelen = (int)STRLEN(xp_name);
8722
8723 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8724 &xp_arg) == FAIL)
8725 return;
8726 }
8727 }
8728
8729 if (defstr != NULL)
8730 {
8731 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008732
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 ex_normal_busy = 0;
8734 rettv->vval.v_string =
8735 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8736 xp_type, xp_arg);
8737 ex_normal_busy = save_ex_normal_busy;
8738 }
8739 if (inputdialog && rettv->vval.v_string == NULL
8740 && argvars[1].v_type != VAR_UNKNOWN
8741 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008742 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 &argvars[2], buf));
8744
8745 vim_free(xp_arg);
8746
8747 /* since the user typed this, no need to wait for return */
8748 need_wait_return = FALSE;
8749 msg_didout = FALSE;
8750 }
8751 cmd_silent = cmd_silent_save;
8752}
8753
8754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 * ":echo expr1 ..." print each argument separated with a space, add a
8756 * newline at the end.
8757 * ":echon expr1 ..." print each argument plain.
8758 */
8759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008760ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008764 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 char_u *p;
8766 int needclr = TRUE;
8767 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008768 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008769 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008770 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 if (eap->skip)
8773 ++emsg_skip;
8774 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8775 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008776 /* If eval1() causes an error message the text from the command may
8777 * still need to be cleared. E.g., "echo 22,44". */
8778 need_clr_eos = needclr;
8779
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 {
8783 /*
8784 * Report the invalid expression unless the expression evaluation
8785 * has been cancelled due to an aborting error, an interrupt, or an
8786 * exception.
8787 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008788 if (!aborting() && did_emsg == did_emsg_before
8789 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008790 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 break;
8793 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008794 need_clr_eos = FALSE;
8795
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 if (!eap->skip)
8797 {
8798 if (atstart)
8799 {
8800 atstart = FALSE;
8801 /* Call msg_start() after eval1(), evaluating the expression
8802 * may cause a message to appear. */
8803 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008804 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008805 /* Mark the saved text as finishing the line, so that what
8806 * follows is displayed on a new line when scrolling back
8807 * at the more prompt. */
8808 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
8812 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008813 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008814 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008815 if (p != NULL)
8816 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008818 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008820 if (*p != TAB && needclr)
8821 {
8822 /* remove any text still there from the command */
8823 msg_clr_eos();
8824 needclr = FALSE;
8825 }
8826 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 }
8828 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008830 if (has_mbyte)
8831 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008832 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833
8834 (void)msg_outtrans_len_attr(p, i, echo_attr);
8835 p += i - 1;
8836 }
8837 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008838 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008841 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 arg = skipwhite(arg);
8845 }
8846 eap->nextcmd = check_nextcmd(arg);
8847
8848 if (eap->skip)
8849 --emsg_skip;
8850 else
8851 {
8852 /* remove text that may still be there from the command */
8853 if (needclr)
8854 msg_clr_eos();
8855 if (eap->cmdidx == CMD_echo)
8856 msg_end();
8857 }
8858}
8859
8860/*
8861 * ":echohl {name}".
8862 */
8863 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008864ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008866 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867}
8868
8869/*
8870 * ":execute expr1 ..." execute the result of an expression.
8871 * ":echomsg expr1 ..." Print a message
8872 * ":echoerr expr1 ..." Print an error
8873 * Each gets spaces around each argument and a newline at the end for
8874 * echo commands
8875 */
8876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 int ret = OK;
8882 char_u *p;
8883 garray_T ga;
8884 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008885 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887 ga_init2(&ga, 1, 80);
8888
8889 if (eap->skip)
8890 ++emsg_skip;
8891 while (*arg != NUL && *arg != '|' && *arg != '\n')
8892 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008893 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8894 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
8897 if (!eap->skip)
8898 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008899 char_u buf[NUMBUFLEN];
8900
8901 if (eap->cmdidx == CMD_execute)
8902 p = tv_get_string_buf(&rettv, buf);
8903 else
8904 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 len = (int)STRLEN(p);
8906 if (ga_grow(&ga, len + 2) == FAIL)
8907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 ret = FAIL;
8910 break;
8911 }
8912 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 ga.ga_len += len;
8916 }
8917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 arg = skipwhite(arg);
8920 }
8921
8922 if (ret != FAIL && ga.ga_data != NULL)
8923 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008924 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8925 {
8926 /* Mark the already saved text as finishing the line, so that what
8927 * follows is displayed on a new line when scrolling back at the
8928 * more prompt. */
8929 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008930 }
8931
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008933 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008934 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008935 out_flush();
8936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 else if (eap->cmdidx == CMD_echoerr)
8938 {
8939 /* We don't want to abort following commands, restore did_emsg. */
8940 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008941 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 if (!force_abort)
8943 did_emsg = save_did_emsg;
8944 }
8945 else if (eap->cmdidx == CMD_execute)
8946 do_cmdline((char_u *)ga.ga_data,
8947 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8948 }
8949
8950 ga_clear(&ga);
8951
8952 if (eap->skip)
8953 --emsg_skip;
8954
8955 eap->nextcmd = check_nextcmd(arg);
8956}
8957
8958/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 * Find window specified by "vp" in tabpage "tp".
8960 */
8961 win_T *
8962find_win_by_nr(
8963 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008964 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008967 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 if (nr < 0)
8970 return NULL;
8971 if (nr == 0)
8972 return curwin;
8973
Bram Moolenaar29323592016-07-24 22:04:11 +02008974 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8975 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976 if (nr >= LOWEST_WIN_ID)
8977 {
8978 if (wp->w_id == nr)
8979 return wp;
8980 }
8981 else if (--nr <= 0)
8982 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008983 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008985 {
8986#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008987 // check tab-local popup windows
8988 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008989 if (wp->w_id == nr)
8990 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008991 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008992 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8993 if (wp->w_id == nr)
8994 return wp;
8995#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008997 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999}
9000
9001/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009002 * Find a window: When using a Window ID in any tab page, when using a number
9003 * in the current tab page.
9004 */
9005 win_T *
9006find_win_by_nr_or_id(typval_T *vp)
9007{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009008 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009009
9010 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009011 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009012 return find_win_by_nr(vp, NULL);
9013}
9014
9015/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009017 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 */
9019 win_T *
9020find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009021 typval_T *wvp, // VAR_UNKNOWN for current window
9022 typval_T *tvp, // VAR_UNKNOWN for current tab page
9023 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024{
9025 win_T *wp = NULL;
9026 tabpage_T *tp = NULL;
9027 long n;
9028
9029 if (wvp->v_type != VAR_UNKNOWN)
9030 {
9031 if (tvp->v_type != VAR_UNKNOWN)
9032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009033 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 if (n >= 0)
9035 tp = find_tabpage(n);
9036 }
9037 else
9038 tp = curtab;
9039
9040 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009041 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009042 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009043 if (wp == NULL && wvp->v_type == VAR_NUMBER
9044 && wvp->vval.v_number != -1)
9045 // A window with the specified number is not found
9046 tp = NULL;
9047 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048 }
9049 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009050 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009052 tp = curtab;
9053 }
9054
9055 if (ptp != NULL)
9056 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057
9058 return wp;
9059}
9060
9061/*
9062 * getwinvar() and gettabwinvar()
9063 */
9064 void
9065getwinvar(
9066 typval_T *argvars,
9067 typval_T *rettv,
9068 int off) /* 1 for gettabwinvar() */
9069{
9070 win_T *win;
9071 char_u *varname;
9072 dictitem_T *v;
9073 tabpage_T *tp = NULL;
9074 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 win_T *oldcurwin;
9076 tabpage_T *oldtabpage;
9077 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009080 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 else
9082 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009084 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009085 ++emsg_off;
9086
9087 rettv->v_type = VAR_STRING;
9088 rettv->vval.v_string = NULL;
9089
9090 if (win != NULL && varname != NULL)
9091 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092 /* Set curwin to be our win, temporarily. Also set the tabpage,
9093 * otherwise the window is not valid. Only do this when needed,
9094 * autocommands get blocked. */
9095 need_switch_win = !(tp == curtab && win == curwin);
9096 if (!need_switch_win
9097 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009099 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009100 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009101 if (varname[1] == NUL)
9102 {
9103 /* get all window-local options in a dict */
9104 dict_T *opts = get_winbuf_options(FALSE);
9105
9106 if (opts != NULL)
9107 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009108 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009109 done = TRUE;
9110 }
9111 }
9112 else if (get_option_tv(&varname, rettv, 1) == OK)
9113 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 done = TRUE;
9115 }
9116 else
9117 {
9118 /* Look up the variable. */
9119 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9120 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9121 varname, FALSE);
9122 if (v != NULL)
9123 {
9124 copy_tv(&v->di_tv, rettv);
9125 done = TRUE;
9126 }
9127 }
9128 }
9129
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009130 if (need_switch_win)
9131 /* restore previous notion of curwin */
9132 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133 }
9134
9135 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9136 /* use the default return value */
9137 copy_tv(&argvars[off + 2], rettv);
9138
9139 --emsg_off;
9140}
9141
9142/*
9143 * "setwinvar()" and "settabwinvar()" functions
9144 */
9145 void
9146setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9147{
9148 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 win_T *save_curwin;
9150 tabpage_T *save_curtab;
9151 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152 char_u *varname, *winvarname;
9153 typval_T *varp;
9154 char_u nbuf[NUMBUFLEN];
9155 tabpage_T *tp = NULL;
9156
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009157 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 return;
9159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009160 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009161 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 else
9163 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009164 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009165 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009166 varp = &argvars[off + 2];
9167
9168 if (win != NULL && varname != NULL && varp != NULL)
9169 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 need_switch_win = !(tp == curtab && win == curwin);
9171 if (!need_switch_win
9172 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009173 {
9174 if (*varname == '&')
9175 {
9176 long numval;
9177 char_u *strval;
9178 int error = FALSE;
9179
9180 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009181 numval = (long)tv_get_number_chk(varp, &error);
9182 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183 if (!error && strval != NULL)
9184 set_option_value(varname, numval, strval, OPT_LOCAL);
9185 }
9186 else
9187 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009188 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189 if (winvarname != NULL)
9190 {
9191 STRCPY(winvarname, "w:");
9192 STRCPY(winvarname + 2, varname);
9193 set_var(winvarname, varp, TRUE);
9194 vim_free(winvarname);
9195 }
9196 }
9197 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 if (need_switch_win)
9199 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 }
9201}
9202
9203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9205 * "arg" points to the "&" or '+' when called, to "option" when returning.
9206 * Returns NULL when no option name found. Otherwise pointer to the char
9207 * after the option name.
9208 */
9209 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009210find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 char_u *p = *arg;
9213
9214 ++p;
9215 if (*p == 'g' && p[1] == ':')
9216 {
9217 *opt_flags = OPT_GLOBAL;
9218 p += 2;
9219 }
9220 else if (*p == 'l' && p[1] == ':')
9221 {
9222 *opt_flags = OPT_LOCAL;
9223 p += 2;
9224 }
9225 else
9226 *opt_flags = 0;
9227
9228 if (!ASCII_ISALPHA(*p))
9229 return NULL;
9230 *arg = p;
9231
9232 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9233 p += 4; /* termcap option */
9234 else
9235 while (ASCII_ISALPHA(*p))
9236 ++p;
9237 return p;
9238}
9239
9240/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009241 * Return the autoload script name for a function or variable name.
9242 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009243 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009245 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009246autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009247{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009248 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009249 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009250
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009251 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009252 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009253 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009254 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009255 STRCPY(scriptname, "autoload/");
9256 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009257 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9258 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009259 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009260 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009261 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009262}
9263
9264/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009265 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009266 * Return TRUE if a package was loaded.
9267 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009269script_autoload(
9270 char_u *name,
9271 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009272{
9273 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009274 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009275 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009276 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009277
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009278 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009279 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009280 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009281 return FALSE;
9282
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009283 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009284
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009285 /* Find the name in the list of previously loaded package names. Skip
9286 * "autoload/", it's always the same. */
9287 for (i = 0; i < ga_loaded.ga_len; ++i)
9288 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9289 break;
9290 if (!reload && i < ga_loaded.ga_len)
9291 ret = FALSE; /* was loaded already */
9292 else
9293 {
9294 /* Remember the name if it wasn't loaded already. */
9295 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9296 {
9297 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9298 tofree = NULL;
9299 }
9300
9301 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009302 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009303 ret = TRUE;
9304 }
9305
9306 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009307 return ret;
9308}
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9311typedef enum
9312{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009313 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9314 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9315 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316} var_flavour_T;
9317
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 char_u *p = varname;
9322
9323 if (ASCII_ISUPPER(*p))
9324 {
9325 while (*(++p))
9326 if (ASCII_ISLOWER(*p))
9327 return VAR_FLAVOUR_SESSION;
9328 return VAR_FLAVOUR_VIMINFO;
9329 }
9330 else
9331 return VAR_FLAVOUR_DEFAULT;
9332}
9333#endif
9334
9335#if defined(FEAT_VIMINFO) || defined(PROTO)
9336/*
9337 * Restore global vars that start with a capital from the viminfo file
9338 */
9339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009340read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009343 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009345 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
9347 if (!writing && (find_viminfo_parameter('!') != NULL))
9348 {
9349 tab = vim_strchr(virp->vir_line + 1, '\t');
9350 if (tab != NULL)
9351 {
9352 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009353 switch (*tab)
9354 {
9355 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009356#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009357 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009358#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009359 case 'D': type = VAR_DICT; break;
9360 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009361 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009362 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
9365 tab = vim_strchr(tab, '\t');
9366 if (tab != NULL)
9367 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009368 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009369 if (type == VAR_STRING || type == VAR_DICT
9370 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009371 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009373#ifdef FEAT_FLOAT
9374 else if (type == VAR_FLOAT)
9375 (void)string2float(tab + 1, &tv.vval.v_float);
9376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009378 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009379 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009380 {
9381 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9382
9383 if (etv == NULL)
9384 /* Failed to parse back the dict or list, use it as a
9385 * string. */
9386 tv.v_type = VAR_STRING;
9387 else
9388 {
9389 vim_free(tv.vval.v_string);
9390 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009391 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009392 }
9393 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009394 else if (type == VAR_BLOB)
9395 {
9396 blob_T *blob = string2blob(tv.vval.v_string);
9397
9398 if (blob == NULL)
9399 // Failed to parse back the blob, use it as a string.
9400 tv.v_type = VAR_STRING;
9401 else
9402 {
9403 vim_free(tv.vval.v_string);
9404 tv.v_type = VAR_BLOB;
9405 tv.vval.v_blob = blob;
9406 }
9407 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009408
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009409 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009410 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009411 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009412 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009413
9414 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009415 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009416 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9417 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009418 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 }
9420 }
9421 }
9422
9423 return viminfo_readline(virp);
9424}
9425
9426/*
9427 * Write global vars that start with a capital to the viminfo file
9428 */
9429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 hashitem_T *hi;
9433 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009434 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009435 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009436 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009438 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
9440 if (find_viminfo_parameter('!') == NULL)
9441 return;
9442
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009443 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009444
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009445 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009448 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009450 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 this_var = HI2DI(hi);
9452 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009455 {
9456 case VAR_STRING: s = "STR"; break;
9457 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009458 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009459 case VAR_DICT: s = "DIC"; break;
9460 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009461 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009462 case VAR_SPECIAL: s = "XPL"; break;
9463
9464 case VAR_UNKNOWN:
9465 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009466 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009467 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009468 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009469 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009470 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009472 if (this_var->di_tv.v_type == VAR_SPECIAL)
9473 {
9474 sprintf((char *)numbuf, "%ld",
9475 (long)this_var->di_tv.vval.v_number);
9476 p = numbuf;
9477 tofree = NULL;
9478 }
9479 else
9480 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009481 if (p != NULL)
9482 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009483 vim_free(tofree);
9484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 }
9486 }
9487}
9488#endif
9489
9490#if defined(FEAT_SESSION) || defined(PROTO)
9491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009492store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
Bram Moolenaar33570922005-01-25 22:26:29 +00009494 hashitem_T *hi;
9495 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009496 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 char_u *p, *t;
9498
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009499 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009502 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009504 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009505 this_var = HI2DI(hi);
9506 if ((this_var->di_tv.v_type == VAR_NUMBER
9507 || this_var->di_tv.v_type == VAR_STRING)
9508 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009509 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009510 /* Escape special characters with a backslash. Turn a LF and
9511 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009512 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009513 (char_u *)"\\\"\n\r");
9514 if (p == NULL) /* out of memory */
9515 break;
9516 for (t = p; *t != NUL; ++t)
9517 if (*t == '\n')
9518 *t = 'n';
9519 else if (*t == '\r')
9520 *t = 'r';
9521 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009522 this_var->di_key,
9523 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9524 : ' ',
9525 p,
9526 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9527 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009528 || put_eol(fd) == FAIL)
9529 {
9530 vim_free(p);
9531 return FAIL;
9532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 vim_free(p);
9534 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009535#ifdef FEAT_FLOAT
9536 else if (this_var->di_tv.v_type == VAR_FLOAT
9537 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9538 {
9539 float_T f = this_var->di_tv.vval.v_float;
9540 int sign = ' ';
9541
9542 if (f < 0)
9543 {
9544 f = -f;
9545 sign = '-';
9546 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009547 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009548 this_var->di_key, sign, f) < 0)
9549 || put_eol(fd) == FAIL)
9550 return FAIL;
9551 }
9552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 }
9554 }
9555 return OK;
9556}
9557#endif
9558
Bram Moolenaar661b1822005-07-28 22:36:45 +00009559/*
9560 * Display script name where an item was last set.
9561 * Should only be invoked when 'verbose' is non-zero.
9562 */
9563 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009564last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009565{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009566 char_u *p;
9567
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009568 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009569 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009570 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009571 if (p != NULL)
9572 {
9573 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009574 msg_puts(_("\n\tLast set from "));
9575 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009576 if (script_ctx.sc_lnum > 0)
9577 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009578 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009579 msg_outnum((long)script_ctx.sc_lnum);
9580 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009581 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009582 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009583 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009584 }
9585}
9586
Bram Moolenaar61be3762019-03-19 23:04:17 +01009587/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009588 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9589 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009590 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009593{
9594 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9595 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009596 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9597 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009598 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009599 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009600}
9601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 * Add an assert error to v:errors.
9604 */
9605 void
9606assert_error(garray_T *gap)
9607{
9608 struct vimvar *vp = &vimvars[VV_ERRORS];
9609
9610 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9611 /* Make sure v:errors is a list. */
9612 set_vim_var_list(VV_ERRORS, list_alloc());
9613 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9614}
Bram Moolenaar31988702018-02-13 12:57:42 +01009615/*
9616 * Compare "typ1" and "typ2". Put the result in "typ1".
9617 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009618 int
9619typval_compare(
9620 typval_T *typ1, /* first operand */
9621 typval_T *typ2, /* second operand */
9622 exptype_T type, /* operator */
9623 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009624 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009625{
9626 int i;
9627 varnumber_T n1, n2;
9628 char_u *s1, *s2;
9629 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9630
Bram Moolenaar31988702018-02-13 12:57:42 +01009631 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009632 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009633 /* For "is" a different type always means FALSE, for "notis"
9634 * it means TRUE. */
9635 n1 = (type == TYPE_NEQUAL);
9636 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009637 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9638 {
9639 if (type_is)
9640 {
9641 n1 = (typ1->v_type == typ2->v_type
9642 && typ1->vval.v_blob == typ2->vval.v_blob);
9643 if (type == TYPE_NEQUAL)
9644 n1 = !n1;
9645 }
9646 else if (typ1->v_type != typ2->v_type
9647 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9648 {
9649 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009650 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009651 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009652 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009653 clear_tv(typ1);
9654 return FAIL;
9655 }
9656 else
9657 {
9658 // Compare two Blobs for being equal or unequal.
9659 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9660 if (type == TYPE_NEQUAL)
9661 n1 = !n1;
9662 }
9663 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009664 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9665 {
9666 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009667 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009668 n1 = (typ1->v_type == typ2->v_type
9669 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009670 if (type == TYPE_NEQUAL)
9671 n1 = !n1;
9672 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009673 else if (typ1->v_type != typ2->v_type
9674 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009675 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009676 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009677 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009678 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009679 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009680 clear_tv(typ1);
9681 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009682 }
9683 else
9684 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009685 /* Compare two Lists for being equal or unequal. */
9686 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9687 ic, FALSE);
9688 if (type == TYPE_NEQUAL)
9689 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009690 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009691 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009692
9693 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9694 {
9695 if (type_is)
9696 {
9697 n1 = (typ1->v_type == typ2->v_type
9698 && typ1->vval.v_dict == typ2->vval.v_dict);
9699 if (type == TYPE_NEQUAL)
9700 n1 = !n1;
9701 }
9702 else if (typ1->v_type != typ2->v_type
9703 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9704 {
9705 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009706 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009707 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009708 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009709 clear_tv(typ1);
9710 return FAIL;
9711 }
9712 else
9713 {
9714 /* Compare two Dictionaries for being equal or unequal. */
9715 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9716 ic, FALSE);
9717 if (type == TYPE_NEQUAL)
9718 n1 = !n1;
9719 }
9720 }
9721
9722 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9723 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9724 {
9725 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009727 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009728 clear_tv(typ1);
9729 return FAIL;
9730 }
9731 if ((typ1->v_type == VAR_PARTIAL
9732 && typ1->vval.v_partial == NULL)
9733 || (typ2->v_type == VAR_PARTIAL
9734 && typ2->vval.v_partial == NULL))
9735 /* when a partial is NULL assume not equal */
9736 n1 = FALSE;
9737 else if (type_is)
9738 {
9739 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9740 /* strings are considered the same if their value is
9741 * the same */
9742 n1 = tv_equal(typ1, typ2, ic, FALSE);
9743 else if (typ1->v_type == VAR_PARTIAL
9744 && typ2->v_type == VAR_PARTIAL)
9745 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9746 else
9747 n1 = FALSE;
9748 }
9749 else
9750 n1 = tv_equal(typ1, typ2, ic, FALSE);
9751 if (type == TYPE_NEQUAL)
9752 n1 = !n1;
9753 }
9754
9755#ifdef FEAT_FLOAT
9756 /*
9757 * If one of the two variables is a float, compare as a float.
9758 * When using "=~" or "!~", always compare as string.
9759 */
9760 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9761 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9762 {
9763 float_T f1, f2;
9764
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009765 f1 = tv_get_float(typ1);
9766 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009767 n1 = FALSE;
9768 switch (type)
9769 {
9770 case TYPE_EQUAL: n1 = (f1 == f2); break;
9771 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9772 case TYPE_GREATER: n1 = (f1 > f2); break;
9773 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9774 case TYPE_SMALLER: n1 = (f1 < f2); break;
9775 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9776 case TYPE_UNKNOWN:
9777 case TYPE_MATCH:
9778 case TYPE_NOMATCH: break; /* avoid gcc warning */
9779 }
9780 }
9781#endif
9782
9783 /*
9784 * If one of the two variables is a number, compare as a number.
9785 * When using "=~" or "!~", always compare as string.
9786 */
9787 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9788 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009790 n1 = tv_get_number(typ1);
9791 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009792 switch (type)
9793 {
9794 case TYPE_EQUAL: n1 = (n1 == n2); break;
9795 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9796 case TYPE_GREATER: n1 = (n1 > n2); break;
9797 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9798 case TYPE_SMALLER: n1 = (n1 < n2); break;
9799 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9800 case TYPE_UNKNOWN:
9801 case TYPE_MATCH:
9802 case TYPE_NOMATCH: break; /* avoid gcc warning */
9803 }
9804 }
9805 else
9806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009807 s1 = tv_get_string_buf(typ1, buf1);
9808 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009809 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9810 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9811 else
9812 i = 0;
9813 n1 = FALSE;
9814 switch (type)
9815 {
9816 case TYPE_EQUAL: n1 = (i == 0); break;
9817 case TYPE_NEQUAL: n1 = (i != 0); break;
9818 case TYPE_GREATER: n1 = (i > 0); break;
9819 case TYPE_GEQUAL: n1 = (i >= 0); break;
9820 case TYPE_SMALLER: n1 = (i < 0); break;
9821 case TYPE_SEQUAL: n1 = (i <= 0); break;
9822
9823 case TYPE_MATCH:
9824 case TYPE_NOMATCH:
9825 n1 = pattern_match(s2, s1, ic);
9826 if (type == TYPE_NOMATCH)
9827 n1 = !n1;
9828 break;
9829
9830 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9831 }
9832 }
9833 clear_tv(typ1);
9834 typ1->v_type = VAR_NUMBER;
9835 typ1->vval.v_number = n1;
9836
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009837 return OK;
9838}
9839
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009840 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009841typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009842{
9843 char_u *tofree;
9844 char_u numbuf[NUMBUFLEN];
9845 char_u *ret = NULL;
9846
9847 if (arg == NULL)
9848 return vim_strsave((char_u *)"(does not exist)");
9849 ret = tv2string(arg, &tofree, numbuf, 0);
9850 /* Make a copy if we have a value but it's not in allocated memory. */
9851 if (ret != NULL && tofree == NULL)
9852 ret = vim_strsave(ret);
9853 return ret;
9854}
9855
9856 int
9857var_exists(char_u *var)
9858{
9859 char_u *name;
9860 char_u *tofree;
9861 typval_T tv;
9862 int len = 0;
9863 int n = FALSE;
9864
9865 /* get_name_len() takes care of expanding curly braces */
9866 name = var;
9867 len = get_name_len(&var, &tofree, TRUE, FALSE);
9868 if (len > 0)
9869 {
9870 if (tofree != NULL)
9871 name = tofree;
9872 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9873 if (n)
9874 {
9875 /* handle d.key, l[idx], f(expr) */
9876 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9877 if (n)
9878 clear_tv(&tv);
9879 }
9880 }
9881 if (*var != NUL)
9882 n = FALSE;
9883
9884 vim_free(tofree);
9885 return n;
9886}
9887
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888#endif /* FEAT_EVAL */
9889
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009891#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
Bram Moolenaar4f974752019-02-17 17:44:42 +01009893#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894/*
9895 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009899 * Get the short path (8.3) for the filename in "fnamep".
9900 * Only works for a valid file name.
9901 * When the path gets longer "fnamep" is changed and the allocated buffer
9902 * is put in "bufp".
9903 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9904 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 */
9906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009907get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009909 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 char_u *newbuf;
9911
9912 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009913 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (l > len - 1)
9915 {
9916 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009917 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 newbuf = vim_strnsave(*fnamep, l);
9919 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
9922 vim_free(*bufp);
9923 *fnamep = *bufp = newbuf;
9924
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009925 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009926 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 }
9928
9929 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009930 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931}
9932
9933/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009934 * Get the short path (8.3) for the filename in "fname". The converted
9935 * path is returned in "bufp".
9936 *
9937 * Some of the directories specified in "fname" may not exist. This function
9938 * will shorten the existing directories at the beginning of the path and then
9939 * append the remaining non-existing path.
9940 *
9941 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009942 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009943 * bufp - Pointer to an allocated buffer for the filename.
9944 * fnamelen - Length of the filename pointed to by fname
9945 *
9946 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 */
9948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009949shortpath_for_invalid_fname(
9950 char_u **fname,
9951 char_u **bufp,
9952 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009954 char_u *short_fname, *save_fname, *pbuf_unused;
9955 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009957 int old_len, len;
9958 int new_len, sfx_len;
9959 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
9961 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009962 old_len = *fnamelen;
9963 save_fname = vim_strnsave(*fname, old_len);
9964 pbuf_unused = NULL;
9965 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009967 endp = save_fname + old_len - 1; /* Find the end of the copy */
9968 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009970 /*
9971 * Try shortening the supplied path till it succeeds by removing one
9972 * directory at a time from the tail of the path.
9973 */
9974 len = 0;
9975 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009977 /* go back one path-separator */
9978 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9979 --endp;
9980 if (endp <= save_fname)
9981 break; /* processed the complete path */
9982
9983 /*
9984 * Replace the path separator with a NUL and try to shorten the
9985 * resulting path.
9986 */
9987 ch = *endp;
9988 *endp = 0;
9989 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009990 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009991 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9992 {
9993 retval = FAIL;
9994 goto theend;
9995 }
9996 *endp = ch; /* preserve the string */
9997
9998 if (len > 0)
9999 break; /* successfully shortened the path */
10000
10001 /* failed to shorten the path. Skip the path separator */
10002 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 }
10004
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010005 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010007 /*
10008 * Succeeded in shortening the path. Now concatenate the shortened
10009 * path with the remaining path at the tail.
10010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010012 /* Compute the length of the new path. */
10013 sfx_len = (int)(save_endp - endp) + 1;
10014 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010016 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010018 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010020 /* There is not enough space in the currently allocated string,
10021 * copy it to a buffer big enough. */
10022 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010024 {
10025 retval = FAIL;
10026 goto theend;
10027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 }
10029 else
10030 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010031 /* Transfer short_fname to the main buffer (it's big enough),
10032 * unless get_short_pathname() did its work in-place. */
10033 *fname = *bufp = save_fname;
10034 if (short_fname != save_fname)
10035 vim_strncpy(save_fname, short_fname, len);
10036 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010038
10039 /* concat the not-shortened part of the path */
10040 vim_strncpy(*fname + len, endp, sfx_len);
10041 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010043
10044theend:
10045 vim_free(pbuf_unused);
10046 vim_free(save_fname);
10047
10048 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
10051/*
10052 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010053 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 */
10055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010056shortpath_for_partial(
10057 char_u **fnamep,
10058 char_u **bufp,
10059 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060{
10061 int sepcount, len, tflen;
10062 char_u *p;
10063 char_u *pbuf, *tfname;
10064 int hasTilde;
10065
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010066 /* Count up the path separators from the RHS.. so we know which part
10067 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010069 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (vim_ispathsep(*p))
10071 ++sepcount;
10072
10073 /* Need full path first (use expand_env() to remove a "~/") */
10074 hasTilde = (**fnamep == '~');
10075 if (hasTilde)
10076 pbuf = tfname = expand_env_save(*fnamep);
10077 else
10078 pbuf = tfname = FullName_save(*fnamep, FALSE);
10079
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010080 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010082 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10083 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084
10085 if (len == 0)
10086 {
10087 /* Don't have a valid filename, so shorten the rest of the
10088 * path if we can. This CAN give us invalid 8.3 filenames, but
10089 * there's not a lot of point in guessing what it might be.
10090 */
10091 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010092 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10093 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 }
10095
10096 /* Count the paths backward to find the beginning of the desired string. */
10097 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010098 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010099 if (has_mbyte)
10100 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 if (vim_ispathsep(*p))
10102 {
10103 if (sepcount == 0 || (hasTilde && sepcount == 1))
10104 break;
10105 else
10106 sepcount --;
10107 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 if (hasTilde)
10110 {
10111 --p;
10112 if (p >= tfname)
10113 *p = '~';
10114 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 }
10117 else
10118 ++p;
10119
10120 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10121 vim_free(*bufp);
10122 *fnamelen = (int)STRLEN(p);
10123 *bufp = pbuf;
10124 *fnamep = p;
10125
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010126 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010128#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
10130/*
10131 * Adjust a filename, according to a string of modifiers.
10132 * *fnamep must be NUL terminated when called. When returning, the length is
10133 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010134 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 * When there is an error, *fnamep is set to NULL.
10136 */
10137 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010138modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010139 char_u *src, // string with modifiers
10140 int tilde_file, // "~" is a file name, not $HOME
10141 int *usedlen, // characters after src that are used
10142 char_u **fnamep, // file name so far
10143 char_u **bufp, // buffer for allocated file name or NULL
10144 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145{
10146 int valid = 0;
10147 char_u *tail;
10148 char_u *s, *p, *pbuf;
10149 char_u dirname[MAXPATHL];
10150 int c;
10151 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010152#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010153 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 int has_shortname = 0;
10155#endif
10156
10157repeat:
10158 /* ":p" - full path/file_name */
10159 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10160 {
10161 has_fullname = 1;
10162
10163 valid |= VALID_PATH;
10164 *usedlen += 2;
10165
10166 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10167 if ((*fnamep)[0] == '~'
10168#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10169 && ((*fnamep)[1] == '/'
10170# ifdef BACKSLASH_IN_FILENAME
10171 || (*fnamep)[1] == '\\'
10172# endif
10173 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010175 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 )
10177 {
10178 *fnamep = expand_env_save(*fnamep);
10179 vim_free(*bufp); /* free any allocated file name */
10180 *bufp = *fnamep;
10181 if (*fnamep == NULL)
10182 return -1;
10183 }
10184
10185 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010186 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 {
10188 if (vim_ispathsep(*p)
10189 && p[1] == '.'
10190 && (p[2] == NUL
10191 || vim_ispathsep(p[2])
10192 || (p[2] == '.'
10193 && (p[3] == NUL || vim_ispathsep(p[3])))))
10194 break;
10195 }
10196
10197 /* FullName_save() is slow, don't use it when not needed. */
10198 if (*p != NUL || !vim_isAbsName(*fnamep))
10199 {
10200 *fnamep = FullName_save(*fnamep, *p != NUL);
10201 vim_free(*bufp); /* free any allocated file name */
10202 *bufp = *fnamep;
10203 if (*fnamep == NULL)
10204 return -1;
10205 }
10206
Bram Moolenaar4f974752019-02-17 17:44:42 +010010207#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010208# if _WIN32_WINNT >= 0x0500
10209 if (vim_strchr(*fnamep, '~') != NULL)
10210 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010211 // Expand 8.3 filename to full path. Needed to make sure the same
10212 // file does not have two different names.
10213 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10214 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10215 WCHAR buf[_MAX_PATH];
10216
10217 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010218 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010219 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010220 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010221 char_u *p = utf16_to_enc(buf, NULL);
10222
10223 if (p != NULL)
10224 {
10225 vim_free(*bufp); // free any allocated file name
10226 *bufp = *fnamep = p;
10227 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010228 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010229 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010230 }
10231 }
10232# endif
10233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 /* Append a path separator to a directory. */
10235 if (mch_isdir(*fnamep))
10236 {
10237 /* Make room for one or two extra characters. */
10238 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10239 vim_free(*bufp); /* free any allocated file name */
10240 *bufp = *fnamep;
10241 if (*fnamep == NULL)
10242 return -1;
10243 add_pathsep(*fnamep);
10244 }
10245 }
10246
10247 /* ":." - path relative to the current directory */
10248 /* ":~" - path relative to the home directory */
10249 /* ":8" - shortname path - postponed till after */
10250 while (src[*usedlen] == ':'
10251 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10252 {
10253 *usedlen += 2;
10254 if (c == '8')
10255 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010256#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 has_shortname = 1; /* Postpone this. */
10258#endif
10259 continue;
10260 }
10261 pbuf = NULL;
10262 /* Need full path first (use expand_env() to remove a "~/") */
10263 if (!has_fullname)
10264 {
10265 if (c == '.' && **fnamep == '~')
10266 p = pbuf = expand_env_save(*fnamep);
10267 else
10268 p = pbuf = FullName_save(*fnamep, FALSE);
10269 }
10270 else
10271 p = *fnamep;
10272
10273 has_fullname = 0;
10274
10275 if (p != NULL)
10276 {
10277 if (c == '.')
10278 {
10279 mch_dirname(dirname, MAXPATHL);
10280 s = shorten_fname(p, dirname);
10281 if (s != NULL)
10282 {
10283 *fnamep = s;
10284 if (pbuf != NULL)
10285 {
10286 vim_free(*bufp); /* free any allocated file name */
10287 *bufp = pbuf;
10288 pbuf = NULL;
10289 }
10290 }
10291 }
10292 else
10293 {
10294 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10295 /* Only replace it when it starts with '~' */
10296 if (*dirname == '~')
10297 {
10298 s = vim_strsave(dirname);
10299 if (s != NULL)
10300 {
10301 *fnamep = s;
10302 vim_free(*bufp);
10303 *bufp = s;
10304 }
10305 }
10306 }
10307 vim_free(pbuf);
10308 }
10309 }
10310
10311 tail = gettail(*fnamep);
10312 *fnamelen = (int)STRLEN(*fnamep);
10313
10314 /* ":h" - head, remove "/file_name", can be repeated */
10315 /* Don't remove the first "/" or "c:\" */
10316 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10317 {
10318 valid |= VALID_HEAD;
10319 *usedlen += 2;
10320 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010321 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010322 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 *fnamelen = (int)(tail - *fnamep);
10324#ifdef VMS
10325 if (*fnamelen > 0)
10326 *fnamelen += 1; /* the path separator is part of the path */
10327#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010328 if (*fnamelen == 0)
10329 {
10330 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10331 p = vim_strsave((char_u *)".");
10332 if (p == NULL)
10333 return -1;
10334 vim_free(*bufp);
10335 *bufp = *fnamep = tail = p;
10336 *fnamelen = 1;
10337 }
10338 else
10339 {
10340 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010341 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 }
10344
10345 /* ":8" - shortname */
10346 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10347 {
10348 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010349#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 has_shortname = 1;
10351#endif
10352 }
10353
Bram Moolenaar4f974752019-02-17 17:44:42 +010010354#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010355 /*
10356 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 */
10358 if (has_shortname)
10359 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010360 /* Copy the string if it is shortened by :h and when it wasn't copied
10361 * yet, because we are going to change it in place. Avoids changing
10362 * the buffer name for "%:8". */
10363 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
10365 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010366 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 return -1;
10368 vim_free(*bufp);
10369 *bufp = *fnamep = p;
10370 }
10371
10372 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010373 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 if (!has_fullname && !vim_isAbsName(*fnamep))
10375 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010376 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 return -1;
10378 }
10379 else
10380 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010381 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382
Bram Moolenaardc935552011-08-17 15:23:23 +020010383 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010385 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 return -1;
10387
10388 if (l == 0)
10389 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010390 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010392 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 return -1;
10394 }
10395 *fnamelen = l;
10396 }
10397 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010398#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
10400 /* ":t" - tail, just the basename */
10401 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10402 {
10403 *usedlen += 2;
10404 *fnamelen -= (int)(tail - *fnamep);
10405 *fnamep = tail;
10406 }
10407
10408 /* ":e" - extension, can be repeated */
10409 /* ":r" - root, without extension, can be repeated */
10410 while (src[*usedlen] == ':'
10411 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10412 {
10413 /* find a '.' in the tail:
10414 * - for second :e: before the current fname
10415 * - otherwise: The last '.'
10416 */
10417 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10418 s = *fnamep - 2;
10419 else
10420 s = *fnamep + *fnamelen - 1;
10421 for ( ; s > tail; --s)
10422 if (s[0] == '.')
10423 break;
10424 if (src[*usedlen + 1] == 'e') /* :e */
10425 {
10426 if (s > tail)
10427 {
10428 *fnamelen += (int)(*fnamep - (s + 1));
10429 *fnamep = s + 1;
10430#ifdef VMS
10431 /* cut version from the extension */
10432 s = *fnamep + *fnamelen - 1;
10433 for ( ; s > *fnamep; --s)
10434 if (s[0] == ';')
10435 break;
10436 if (s > *fnamep)
10437 *fnamelen = s - *fnamep;
10438#endif
10439 }
10440 else if (*fnamep <= tail)
10441 *fnamelen = 0;
10442 }
10443 else /* :r */
10444 {
10445 if (s > tail) /* remove one extension */
10446 *fnamelen = (int)(s - *fnamep);
10447 }
10448 *usedlen += 2;
10449 }
10450
10451 /* ":s?pat?foo?" - substitute */
10452 /* ":gs?pat?foo?" - global substitute */
10453 if (src[*usedlen] == ':'
10454 && (src[*usedlen + 1] == 's'
10455 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10456 {
10457 char_u *str;
10458 char_u *pat;
10459 char_u *sub;
10460 int sep;
10461 char_u *flags;
10462 int didit = FALSE;
10463
10464 flags = (char_u *)"";
10465 s = src + *usedlen + 2;
10466 if (src[*usedlen + 1] == 'g')
10467 {
10468 flags = (char_u *)"g";
10469 ++s;
10470 }
10471
10472 sep = *s++;
10473 if (sep)
10474 {
10475 /* find end of pattern */
10476 p = vim_strchr(s, sep);
10477 if (p != NULL)
10478 {
10479 pat = vim_strnsave(s, (int)(p - s));
10480 if (pat != NULL)
10481 {
10482 s = p + 1;
10483 /* find end of substitution */
10484 p = vim_strchr(s, sep);
10485 if (p != NULL)
10486 {
10487 sub = vim_strnsave(s, (int)(p - s));
10488 str = vim_strnsave(*fnamep, *fnamelen);
10489 if (sub != NULL && str != NULL)
10490 {
10491 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010492 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 if (s != NULL)
10494 {
10495 *fnamep = s;
10496 *fnamelen = (int)STRLEN(s);
10497 vim_free(*bufp);
10498 *bufp = s;
10499 didit = TRUE;
10500 }
10501 }
10502 vim_free(sub);
10503 vim_free(str);
10504 }
10505 vim_free(pat);
10506 }
10507 }
10508 /* after using ":s", repeat all the modifiers */
10509 if (didit)
10510 goto repeat;
10511 }
10512 }
10513
Bram Moolenaar26df0922014-02-23 23:39:13 +010010514 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10515 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010516 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010517 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010518 if (c != NUL)
10519 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010520 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010521 if (c != NUL)
10522 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010523 if (p == NULL)
10524 return -1;
10525 vim_free(*bufp);
10526 *bufp = *fnamep = p;
10527 *fnamelen = (int)STRLEN(p);
10528 *usedlen += 2;
10529 }
10530
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 return valid;
10532}
10533
10534/*
10535 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010536 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 * "flags" can be "g" to do a global substitute.
10538 * Returns an allocated string, NULL for error.
10539 */
10540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010541do_string_sub(
10542 char_u *str,
10543 char_u *pat,
10544 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010545 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548 int sublen;
10549 regmatch_T regmatch;
10550 int i;
10551 int do_all;
10552 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010553 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 garray_T ga;
10555 char_u *ret;
10556 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010557 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558
10559 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10560 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010561 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562
10563 ga_init2(&ga, 1, 200);
10564
10565 do_all = (flags[0] == 'g');
10566
10567 regmatch.rm_ic = p_ic;
10568 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10569 if (regmatch.regprog != NULL)
10570 {
10571 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010572 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10574 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010575 /* Skip empty match except for first match. */
10576 if (regmatch.startp[0] == regmatch.endp[0])
10577 {
10578 if (zero_width == regmatch.startp[0])
10579 {
10580 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010581 i = MB_PTR2LEN(tail);
10582 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10583 (size_t)i);
10584 ga.ga_len += i;
10585 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010586 continue;
10587 }
10588 zero_width = regmatch.startp[0];
10589 }
10590
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 /*
10592 * Get some space for a temporary buffer to do the substitution
10593 * into. It will contain:
10594 * - The text up to where the match is.
10595 * - The substituted text.
10596 * - The text after the match.
10597 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010598 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010599 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10601 {
10602 ga_clear(&ga);
10603 break;
10604 }
10605
10606 /* copy the text up to where the match is */
10607 i = (int)(regmatch.startp[0] - tail);
10608 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10609 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010610 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 + ga.ga_len + i, TRUE, TRUE, FALSE);
10612 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010613 tail = regmatch.endp[0];
10614 if (*tail == NUL)
10615 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 if (!do_all)
10617 break;
10618 }
10619
10620 if (ga.ga_data != NULL)
10621 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10622
Bram Moolenaar473de612013-06-08 18:19:48 +020010623 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 }
10625
10626 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10627 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010628 if (p_cpo == empty_option)
10629 p_cpo = save_cpo;
10630 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010631 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010632 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633
10634 return ret;
10635}
10636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 static int
10638filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10639{
10640 typval_T rettv;
10641 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010643
10644 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10645 argv[0] = vimvars[VV_KEY].vv_tv;
10646 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010647 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10648 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 if (map)
10650 {
10651 /* map(): replace the list item value */
10652 clear_tv(tv);
10653 rettv.v_lock = 0;
10654 *tv = rettv;
10655 }
10656 else
10657 {
10658 int error = FALSE;
10659
10660 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010661 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010662 clear_tv(&rettv);
10663 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010664 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010665 if (error)
10666 goto theend;
10667 }
10668 retval = OK;
10669theend:
10670 clear_tv(&vimvars[VV_VAL].vv_tv);
10671 return retval;
10672}
10673
10674
10675/*
10676 * Implementation of map() and filter().
10677 */
10678 void
10679filter_map(typval_T *argvars, typval_T *rettv, int map)
10680{
10681 typval_T *expr;
10682 listitem_T *li, *nli;
10683 list_T *l = NULL;
10684 dictitem_T *di;
10685 hashtab_T *ht;
10686 hashitem_T *hi;
10687 dict_T *d = NULL;
10688 typval_T save_val;
10689 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010690 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 int rem;
10692 int todo;
10693 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10694 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10695 : N_("filter() argument"));
10696 int save_did_emsg;
10697 int idx = 0;
10698
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010699 if (argvars[0].v_type == VAR_BLOB)
10700 {
10701 if ((b = argvars[0].vval.v_blob) == NULL)
10702 return;
10703 }
10704 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010705 {
10706 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010707 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 return;
10709 }
10710 else if (argvars[0].v_type == VAR_DICT)
10711 {
10712 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010713 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010714 return;
10715 }
10716 else
10717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010718 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010719 return;
10720 }
10721
10722 expr = &argvars[1];
10723 /* On type errors, the preceding call has already displayed an error
10724 * message. Avoid a misleading error message for an empty string that
10725 * was not passed as argument. */
10726 if (expr->v_type != VAR_UNKNOWN)
10727 {
10728 prepare_vimvar(VV_VAL, &save_val);
10729
10730 /* We reset "did_emsg" to be able to detect whether an error
10731 * occurred during evaluation of the expression. */
10732 save_did_emsg = did_emsg;
10733 did_emsg = FALSE;
10734
10735 prepare_vimvar(VV_KEY, &save_key);
10736 if (argvars[0].v_type == VAR_DICT)
10737 {
10738 vimvars[VV_KEY].vv_type = VAR_STRING;
10739
10740 ht = &d->dv_hashtab;
10741 hash_lock(ht);
10742 todo = (int)ht->ht_used;
10743 for (hi = ht->ht_array; todo > 0; ++hi)
10744 {
10745 if (!HASHITEM_EMPTY(hi))
10746 {
10747 int r;
10748
10749 --todo;
10750 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010751 if (map && (var_check_lock(di->di_tv.v_lock,
10752 arg_errmsg, TRUE)
10753 || var_check_ro(di->di_flags,
10754 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755 break;
10756 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10757 r = filter_map_one(&di->di_tv, expr, map, &rem);
10758 clear_tv(&vimvars[VV_KEY].vv_tv);
10759 if (r == FAIL || did_emsg)
10760 break;
10761 if (!map && rem)
10762 {
10763 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10764 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10765 break;
10766 dictitem_remove(d, di);
10767 }
10768 }
10769 }
10770 hash_unlock(ht);
10771 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010772 else if (argvars[0].v_type == VAR_BLOB)
10773 {
10774 int i;
10775 typval_T tv;
10776
10777 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10778 for (i = 0; i < b->bv_ga.ga_len; i++)
10779 {
10780 tv.v_type = VAR_NUMBER;
10781 tv.vval.v_number = blob_get(b, i);
10782 vimvars[VV_KEY].vv_nr = idx;
10783 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10784 break;
10785 if (tv.v_type != VAR_NUMBER)
10786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010787 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010788 return;
10789 }
10790 tv.v_type = VAR_NUMBER;
10791 blob_set(b, i, tv.vval.v_number);
10792 if (!map && rem)
10793 {
10794 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10795
10796 mch_memmove(p + idx, p + i + 1,
10797 (size_t)b->bv_ga.ga_len - i - 1);
10798 --b->bv_ga.ga_len;
10799 --i;
10800 }
10801 }
10802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010803 else
10804 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010805 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010806 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10807
10808 for (li = l->lv_first; li != NULL; li = nli)
10809 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010810 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010811 break;
10812 nli = li->li_next;
10813 vimvars[VV_KEY].vv_nr = idx;
10814 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10815 || did_emsg)
10816 break;
10817 if (!map && rem)
10818 listitem_remove(l, li);
10819 ++idx;
10820 }
10821 }
10822
10823 restore_vimvar(VV_KEY, &save_key);
10824 restore_vimvar(VV_VAL, &save_val);
10825
10826 did_emsg |= save_did_emsg;
10827 }
10828
10829 copy_tv(&argvars[0], rettv);
10830}
10831
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */