blob: d633f4502e8eb4158c354e126d826ad085700187 [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 Moolenaar61343f02019-07-20 21:11:13 +02001489 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 * 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 Moolenaar61343f02019-07-20 21:11:13 +02001502 char_u *op)
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 Moolenaar61343f02019-07-20 21:11:13 +02001515 if (ex_let_one(arg, tv, copy, is_const, op, op) == 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,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001546 (char_u *)",;]", op);
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,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001571 (char_u *)"]", op);
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
Bram Moolenaar61343f02019-07-20 21:11:13 +02007358 // "." is ".name" lookup when we found a dict or when evaluating and
7359 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007360 while (ret == OK
7361 && (**arg == '['
Bram Moolenaar61343f02019-07-20 21:11:13 +02007362 || (**arg == '.' && (rettv->v_type == VAR_DICT
7363 || (!evaluate
7364 && (*arg)[1] != '.'
7365 && current_sctx.sc_version >= 2)))
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007366 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7367 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007368 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007369 {
7370 if (**arg == '(')
7371 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007372 partial_T *pt = NULL;
7373
Bram Moolenaard9fba312005-06-26 22:34:35 +00007374 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007375 if (evaluate)
7376 {
7377 functv = *rettv;
7378 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007379
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007380 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007381 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007382 {
7383 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007384 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007385 }
7386 else
7387 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007388 }
7389 else
7390 s = (char_u *)"";
Bram Moolenaar6ed88192019-05-11 18:37:44 +02007391 ret = get_func_tv(s, -1, rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007392 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007393 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007394
7395 /* Clear the funcref afterwards, so that deleting it while
7396 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007397 if (evaluate)
7398 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007399
7400 /* Stop the expression evaluation when immediately aborting on
7401 * error, or when an interrupt occurred or an exception was thrown
7402 * but not caught. */
7403 if (aborting())
7404 {
7405 if (ret == OK)
7406 clear_tv(rettv);
7407 ret = FAIL;
7408 }
7409 dict_unref(selfdict);
7410 selfdict = NULL;
7411 }
7412 else /* **arg == '[' || **arg == '.' */
7413 {
7414 dict_unref(selfdict);
7415 if (rettv->v_type == VAR_DICT)
7416 {
7417 selfdict = rettv->vval.v_dict;
7418 if (selfdict != NULL)
7419 ++selfdict->dv_refcount;
7420 }
7421 else
7422 selfdict = NULL;
7423 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7424 {
7425 clear_tv(rettv);
7426 ret = FAIL;
7427 }
7428 }
7429 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007430
Bram Moolenaar1d429612016-05-24 15:44:17 +02007431 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7432 * Don't do this when "Func" is already a partial that was bound
7433 * explicitly (pt_auto is FALSE). */
7434 if (selfdict != NULL
7435 && (rettv->v_type == VAR_FUNC
7436 || (rettv->v_type == VAR_PARTIAL
7437 && (rettv->vval.v_partial->pt_auto
7438 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007439 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007440
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007441 dict_unref(selfdict);
7442 return ret;
7443}
7444
7445/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007446 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447 * value).
7448 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007449 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007451{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007452 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007453}
7454
7455/*
7456 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 * The string "s" must have been allocated, it is consumed.
7458 * Return NULL for out of memory, the variable otherwise.
7459 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
Bram Moolenaar33570922005-01-25 22:26:29 +00007463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007465 rettv = alloc_tv();
7466 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007468 rettv->v_type = VAR_STRING;
7469 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 }
7471 else
7472 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007473 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474}
7475
7476/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007477 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 if (varp != NULL)
7483 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007484 switch (varp->v_type)
7485 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007486 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007487 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007488 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007489 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007490 vim_free(varp->vval.v_string);
7491 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007492 case VAR_PARTIAL:
7493 partial_unref(varp->vval.v_partial);
7494 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007495 case VAR_BLOB:
7496 blob_unref(varp->vval.v_blob);
7497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007498 case VAR_LIST:
7499 list_unref(varp->vval.v_list);
7500 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007501 case VAR_DICT:
7502 dict_unref(varp->vval.v_dict);
7503 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007504 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007506 job_unref(varp->vval.v_job);
7507 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007508#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007509 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007511 channel_unref(varp->vval.v_channel);
7512 break;
7513#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007514 case VAR_NUMBER:
7515 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007516 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007517 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007518 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 vim_free(varp);
7521 }
7522}
7523
7524/*
7525 * Free the memory for a variable value and set the value to NULL or 0.
7526 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007528clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 if (varp != NULL)
7531 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007532 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007535 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007536 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007537 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007538 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007539 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007540 case VAR_PARTIAL:
7541 partial_unref(varp->vval.v_partial);
7542 varp->vval.v_partial = NULL;
7543 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007544 case VAR_BLOB:
7545 blob_unref(varp->vval.v_blob);
7546 varp->vval.v_blob = NULL;
7547 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007548 case VAR_LIST:
7549 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007551 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 case VAR_DICT:
7553 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007557 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007558 varp->vval.v_number = 0;
7559 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007560 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007561#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007562 varp->vval.v_float = 0.0;
7563 break;
7564#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007565 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007566#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007567 job_unref(varp->vval.v_job);
7568 varp->vval.v_job = NULL;
7569#endif
7570 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007571 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007572#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007573 channel_unref(varp->vval.v_channel);
7574 varp->vval.v_channel = NULL;
7575#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 case VAR_UNKNOWN:
7577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007579 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
7581}
7582
7583/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 * Set the value of a variable to NULL without freeing items.
7585 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007588{
7589 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591}
7592
7593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 * Get the number value of a variable.
7595 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007596 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007598 * caller of incompatible types: it sets *denote to TRUE if "denote"
7599 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007601 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007602tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007604 int error = FALSE;
7605
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007606 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007607}
7608
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007609 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007610tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007611{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007612 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007617 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007618 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007619#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007620 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007621 break;
7622#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007624 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 break;
7627 case VAR_STRING:
7628 if (varp->vval.v_string != NULL)
7629 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007630 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007631 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007632 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007634 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007635 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007637 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007638 case VAR_SPECIAL:
7639 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7640 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007641 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007642#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007644 break;
7645#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007646 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007648 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007649 break;
7650#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007651 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007652 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007653 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007654 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007655 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007656 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007658 if (denote == NULL) /* useful for values that must be unsigned */
7659 n = -1;
7660 else
7661 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007662 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663}
7664
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007665#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007667tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007668{
7669 switch (varp->v_type)
7670 {
7671 case VAR_NUMBER:
7672 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007673 case VAR_FLOAT:
7674 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007675 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007676 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007677 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007678 break;
7679 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007681 break;
7682 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007684 break;
7685 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007686 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007687 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007688 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007689 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007690 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007691 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007692# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007693 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007694 break;
7695# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007696 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007697# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007698 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007699 break;
7700# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007701 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007702 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007703 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007704 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007705 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007706 break;
7707 }
7708 return 0;
7709}
7710#endif
7711
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 * Get the string value of a variable.
7714 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007715 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7716 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 * If the String variable has never been set, return an empty string.
7718 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007719 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007720 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007722 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007723tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
7725 static char_u mybuf[NUMBUFLEN];
7726
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007727 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007730 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007731tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007733 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007734
7735 return res != NULL ? res : (char_u *)"";
7736}
7737
Bram Moolenaar7d647822014-04-05 21:28:56 +02007738/*
7739 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7740 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007741 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007742tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007743{
7744 static char_u mybuf[NUMBUFLEN];
7745
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007746 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007747}
7748
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007749 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007750tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007751{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007755 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007756 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007757 return buf;
7758 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007759 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007760 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 break;
7762 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007763 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 break;
7765 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007766 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007767 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007768 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007769#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007770 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007771 break;
7772#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 case VAR_STRING:
7774 if (varp->vval.v_string != NULL)
7775 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007776 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007777 case VAR_SPECIAL:
7778 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7779 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007780 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007781 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007782 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007783 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007784#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007785 {
7786 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007787 char *status;
7788
7789 if (job == NULL)
7790 return (char_u *)"no process";
7791 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007792 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007793 : "run";
7794# ifdef UNIX
7795 vim_snprintf((char *)buf, NUMBUFLEN,
7796 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007797# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007798 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007799 "process %ld %s",
7800 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007801 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007802# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007803 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007804 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7805# endif
7806 return buf;
7807 }
7808#endif
7809 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007810 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007811#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007812 {
7813 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007814 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007815
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007816 if (channel == NULL)
7817 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7818 else
7819 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007820 "channel %d %s", channel->ch_id, status);
7821 return buf;
7822 }
7823#endif
7824 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007825 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007826 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007829 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
7832/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007833 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7834 * string() on Dict, List, etc.
7835 */
7836 char_u *
7837tv_stringify(typval_T *varp, char_u *buf)
7838{
7839 if (varp->v_type == VAR_LIST
7840 || varp->v_type == VAR_DICT
7841 || varp->v_type == VAR_FUNC
7842 || varp->v_type == VAR_PARTIAL
7843 || varp->v_type == VAR_FLOAT)
7844 {
7845 typval_T tmp;
7846
7847 f_string(varp, &tmp);
7848 tv_get_string_buf(&tmp, buf);
7849 clear_tv(varp);
7850 *varp = tmp;
7851 return tmp.vval.v_string;
7852 }
7853 return tv_get_string_buf(varp, buf);
7854}
7855
7856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 * Find variable "name" in the list of variables.
7858 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007860 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007863 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007864find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007868 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869
Bram Moolenaara7043832005-01-21 11:56:39 +00007870 ht = find_var_ht(name, &varname);
7871 if (htp != NULL)
7872 *htp = ht;
7873 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007875 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7876 if (ret != NULL)
7877 return ret;
7878
7879 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007880 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881}
7882
7883/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007884 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007885 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007888find_var_in_ht(
7889 hashtab_T *ht,
7890 int htname,
7891 char_u *varname,
7892 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007893{
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 hashitem_T *hi;
7895
7896 if (*varname == NUL)
7897 {
7898 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007899 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007901 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 case 'g': return &globvars_var;
7903 case 'v': return &vimvars_var;
7904 case 'b': return &curbuf->b_bufvar;
7905 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007906 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007907 case 'l': return get_funccal_local_var();
7908 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 }
7910 return NULL;
7911 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007912
7913 hi = hash_find(ht, varname);
7914 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007915 {
7916 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007917 * worked find the variable again. Don't auto-load a script if it was
7918 * loaded already, otherwise it would be loaded every time when
7919 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007920 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007921 {
7922 /* Note: script_autoload() may make "hi" invalid. It must either
7923 * be obtained again or not used. */
7924 if (!script_autoload(varname, FALSE) || aborting())
7925 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007926 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007927 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007928 if (HASHITEM_EMPTY(hi))
7929 return NULL;
7930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007932}
7933
7934/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007936 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007937 * Set "varname" to the start of name without ':'.
7938 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007939 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007940find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007942 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007943 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007944
Bram Moolenaar73627d02015-08-11 15:46:09 +02007945 if (name[0] == NUL)
7946 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 if (name[1] != ':')
7948 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007949 /* The name must not start with a colon or #. */
7950 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 return NULL;
7952 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007953
Bram Moolenaard2e716e2019-04-20 14:39:52 +02007954 // "version" is "v:version" in all scopes if scriptversion < 3.
7955 // Same for a few other variables marked with VV_COMPAT.
7956 if (current_sctx.sc_version < 3)
7957 {
7958 hi = hash_find(&compat_hashtab, name);
7959 if (!HASHITEM_EMPTY(hi))
7960 return &compat_hashtab;
7961 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00007962
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007963 ht = get_funccal_local_ht();
7964 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007966 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 }
7968 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007969 if (*name == 'g') /* global variable */
7970 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02007971 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007972 if (vim_strchr(name + 2, ':') != NULL
7973 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007974 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007976 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007978 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007979 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007980 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 if (*name == 'v') /* v: variable */
7982 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007983 if (*name == 'a') /* a: function argument */
7984 return get_funccal_args_ht();
7985 if (*name == 'l') /* l: local function variable */
7986 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007988 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7989 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 return NULL;
7991}
7992
7993/*
7994 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007995 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * Returns NULL when it doesn't exist.
7997 */
7998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007999get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008003 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 if (v == NULL)
8005 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008006 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007}
8008
8009/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 * sourcing this script and when executing functions defined in the script.
8012 */
8013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008014new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
Bram Moolenaara7043832005-01-21 11:56:39 +00008016 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 hashtab_T *ht;
8018 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008022 /* Re-allocating ga_data means that an ht_array pointing to
8023 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008025 for (i = 1; i <= ga_scripts.ga_len; ++i)
8026 {
8027 ht = &SCRIPT_VARS(i);
8028 if (ht->ht_mask == HT_INIT_SIZE - 1)
8029 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008030 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008032 }
8033
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 while (ga_scripts.ga_len < id)
8035 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008036 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008037 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008038 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
8041 }
8042}
8043
8044/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8046 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 */
8048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008049init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008052 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008053 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008054 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008055 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 dict_var->di_tv.vval.v_dict = dict;
8057 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008058 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8060 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061}
8062
8063/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008064 * Unreference a dictionary initialized by init_var_dict().
8065 */
8066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008067unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008068{
8069 /* Now the dict needs to be freed if no one else is using it, go back to
8070 * normal reference counting. */
8071 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8072 dict_unref(dict);
8073}
8074
8075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 * Frees all allocated variables and the value they contain.
8078 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 */
8080 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008081vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008082{
8083 vars_clear_ext(ht, TRUE);
8084}
8085
8086/*
8087 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8088 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008090vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
Bram Moolenaara7043832005-01-21 11:56:39 +00008092 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008093 hashitem_T *hi;
8094 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008097 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008098 for (hi = ht->ht_array; todo > 0; ++hi)
8099 {
8100 if (!HASHITEM_EMPTY(hi))
8101 {
8102 --todo;
8103
Bram Moolenaar33570922005-01-25 22:26:29 +00008104 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008105 * ht_array might change then. hash_clear() takes care of it
8106 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 v = HI2DI(hi);
8108 if (free_val)
8109 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008110 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008112 }
8113 }
8114 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008115 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116}
8117
Bram Moolenaara7043832005-01-21 11:56:39 +00008118/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 * Delete a variable from hashtab "ht" at item "hi".
8120 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008123delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008126
8127 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 clear_tv(&di->di_tv);
8129 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130}
8131
8132/*
8133 * List the value of one internal variable.
8134 */
8135 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008136list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 char_u *tofree;
8139 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008140 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008142 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008143 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008144 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146}
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008149list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008150 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008151 char_u *name,
8152 int type,
8153 char_u *string,
8154 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
Bram Moolenaar31859182007-08-14 20:41:13 +00008156 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8157 msg_start();
8158 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008160 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 msg_putchar(' ');
8162 msg_advance(22);
8163 if (type == VAR_NUMBER)
8164 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008165 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008166 msg_putchar('*');
8167 else if (type == VAR_LIST)
8168 {
8169 msg_putchar('[');
8170 if (*string == '[')
8171 ++string;
8172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008173 else if (type == VAR_DICT)
8174 {
8175 msg_putchar('{');
8176 if (*string == '{')
8177 ++string;
8178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 else
8180 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008181
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008183
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008184 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008185 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008186 if (*first)
8187 {
8188 msg_clr_eos();
8189 *first = FALSE;
8190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191}
8192
8193/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 * If the variable already exists, the value is updated.
8196 * Otherwise the variable is created.
8197 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008199set_var(
8200 char_u *name,
8201 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008202 int copy) // make copy of value in "tv"
8203{
8204 set_var_const(name, tv, copy, FALSE);
8205}
8206
8207/*
8208 * Set variable "name" to value in "tv".
8209 * If the variable already exists and "is_const" is FALSE the value is updated.
8210 * Otherwise the variable is created.
8211 */
8212 static void
8213set_var_const(
8214 char_u *name,
8215 typval_T *tv,
8216 int copy, // make copy of value in "tv"
8217 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008223 ht = find_var_ht(name, &varname);
8224 if (ht == NULL || *varname == NUL)
8225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008226 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008227 return;
8228 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008229 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008230
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008231 /* Search in parent scope which is possible to reference from lambda */
8232 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008233 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008234
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008235 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8236 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008237 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008241 if (is_const)
8242 {
8243 emsg(_(e_cannot_mod));
8244 return;
8245 }
8246
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008248 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008249 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008251
8252 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008253 * Handle setting internal v: variables separately where needed to
8254 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 */
8256 if (ht == &vimvarht)
8257 {
8258 if (v->di_tv.v_type == VAR_STRING)
8259 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008260 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008262 {
8263 char_u *val = tv_get_string(tv);
8264
8265 // Careful: when assigning to v:errmsg and tv_get_string()
8266 // causes an error message the variable will alrady be set.
8267 if (v->di_tv.vval.v_string == NULL)
8268 v->di_tv.vval.v_string = vim_strsave(val);
8269 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 else
8271 {
8272 /* Take over the string to avoid an extra alloc/free. */
8273 v->di_tv.vval.v_string = tv->vval.v_string;
8274 tv->vval.v_string = NULL;
8275 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008276 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008278 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008280 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008281 if (STRCMP(varname, "searchforward") == 0)
8282 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008283#ifdef FEAT_SEARCH_EXTRA
8284 else if (STRCMP(varname, "hlsearch") == 0)
8285 {
8286 no_hlsearch = !v->di_tv.vval.v_number;
8287 redraw_all_later(SOME_VALID);
8288 }
8289#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008290 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008291 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008292 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008294 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008295 return;
8296 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 }
8298
8299 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301 else /* add a new variable */
8302 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008303 // Can't add "v:" or "a:" variable.
8304 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008306 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008307 return;
8308 }
8309
Bram Moolenaar31b81602019-02-10 22:14:27 +01008310 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008311 if (!valid_varname(varname))
8312 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008313
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008314 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008315 if (v == NULL)
8316 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008320 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 return;
8322 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008323 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008324 if (is_const)
8325 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008327
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008328 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008330 else
8331 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008333 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008334 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008335 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008336
8337 if (is_const)
8338 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339}
8340
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008341/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008342 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 * Also give an error message.
8344 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008346var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008347{
8348 if (flags & DI_FLAGS_RO)
8349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008350 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 return TRUE;
8352 }
8353 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8354 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008355 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008356 return TRUE;
8357 }
8358 return FALSE;
8359}
8360
8361/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008362 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8363 * Also give an error message.
8364 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008366var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008367{
8368 if (flags & DI_FLAGS_FIX)
8369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008370 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008371 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008372 return TRUE;
8373 }
8374 return FALSE;
8375}
8376
8377/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008378 * Check if a funcref is assigned to a valid variable name.
8379 * Return TRUE and give an error if not.
8380 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008381 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008382var_check_func_name(
8383 char_u *name, /* points to start of variable name */
8384 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008385{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008386 /* Allow for w: b: s: and t:. */
8387 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008388 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8389 ? name[2] : name[0]))
8390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008391 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008392 name);
8393 return TRUE;
8394 }
8395 /* Don't allow hiding a function. When "v" is not NULL we might be
8396 * assigning another function to the same var, the type is checked
8397 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008398 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008400 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008401 name);
8402 return TRUE;
8403 }
8404 return FALSE;
8405}
8406
8407/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008408 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008409 * Also give an error message, using "name" or _("name") when use_gettext is
8410 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008411 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008412 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008413var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008414{
8415 if (lock & VAR_LOCKED)
8416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008417 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008418 name == NULL ? (char_u *)_("Unknown")
8419 : use_gettext ? (char_u *)_(name)
8420 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008421 return TRUE;
8422 }
8423 if (lock & VAR_FIXED)
8424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008425 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008426 name == NULL ? (char_u *)_("Unknown")
8427 : use_gettext ? (char_u *)_(name)
8428 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008429 return TRUE;
8430 }
8431 return FALSE;
8432}
8433
8434/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008435 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8436 * Also give an error message, using "name" or _("name") when use_gettext is
8437 * TRUE.
8438 */
8439 static int
8440tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8441{
8442 int lock = 0;
8443
8444 switch (tv->v_type)
8445 {
8446 case VAR_BLOB:
8447 if (tv->vval.v_blob != NULL)
8448 lock = tv->vval.v_blob->bv_lock;
8449 break;
8450 case VAR_LIST:
8451 if (tv->vval.v_list != NULL)
8452 lock = tv->vval.v_list->lv_lock;
8453 break;
8454 case VAR_DICT:
8455 if (tv->vval.v_dict != NULL)
8456 lock = tv->vval.v_dict->dv_lock;
8457 break;
8458 default:
8459 break;
8460 }
8461 return var_check_lock(tv->v_lock, name, use_gettext)
8462 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8463}
8464
8465/*
8466 * Check if a variable name is valid.
8467 * Return FALSE and give an error if not.
8468 */
8469 int
8470valid_varname(char_u *varname)
8471{
8472 char_u *p;
8473
8474 for (p = varname; *p != NUL; ++p)
8475 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8476 && *p != AUTOLOAD_CHAR)
8477 {
8478 semsg(_(e_illvar), varname);
8479 return FALSE;
8480 }
8481 return TRUE;
8482}
8483
8484/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008487 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008488 * It is OK for "from" and "to" to point to the same item. This is used to
8489 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008490 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008492copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008494 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008495 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008496 switch (from->v_type)
8497 {
8498 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008499 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008500 to->vval.v_number = from->vval.v_number;
8501 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008502 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008503#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008504 to->vval.v_float = from->vval.v_float;
8505 break;
8506#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008507 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008509 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008510 if (to->vval.v_job != NULL)
8511 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008512 break;
8513#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008514 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008516 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008517 if (to->vval.v_channel != NULL)
8518 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008519 break;
8520#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 case VAR_STRING:
8522 case VAR_FUNC:
8523 if (from->vval.v_string == NULL)
8524 to->vval.v_string = NULL;
8525 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008528 if (from->v_type == VAR_FUNC)
8529 func_ref(to->vval.v_string);
8530 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008532 case VAR_PARTIAL:
8533 if (from->vval.v_partial == NULL)
8534 to->vval.v_partial = NULL;
8535 else
8536 {
8537 to->vval.v_partial = from->vval.v_partial;
8538 ++to->vval.v_partial->pt_refcount;
8539 }
8540 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008541 case VAR_BLOB:
8542 if (from->vval.v_blob == NULL)
8543 to->vval.v_blob = NULL;
8544 else
8545 {
8546 to->vval.v_blob = from->vval.v_blob;
8547 ++to->vval.v_blob->bv_refcount;
8548 }
8549 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008550 case VAR_LIST:
8551 if (from->vval.v_list == NULL)
8552 to->vval.v_list = NULL;
8553 else
8554 {
8555 to->vval.v_list = from->vval.v_list;
8556 ++to->vval.v_list->lv_refcount;
8557 }
8558 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 case VAR_DICT:
8560 if (from->vval.v_dict == NULL)
8561 to->vval.v_dict = NULL;
8562 else
8563 {
8564 to->vval.v_dict = from->vval.v_dict;
8565 ++to->vval.v_dict->dv_refcount;
8566 }
8567 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008568 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008569 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 break;
8571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572}
8573
8574/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 * Make a copy of an item.
8576 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008577 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8578 * reference to an already copied list/dict can be used.
8579 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008580 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008582item_copy(
8583 typval_T *from,
8584 typval_T *to,
8585 int deep,
8586 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587{
8588 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008593 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 }
8596 ++recurse;
8597
8598 switch (from->v_type)
8599 {
8600 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008601 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 case VAR_STRING:
8603 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008604 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008605 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008606 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008607 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608 copy_tv(from, to);
8609 break;
8610 case VAR_LIST:
8611 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008612 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008613 if (from->vval.v_list == NULL)
8614 to->vval.v_list = NULL;
8615 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8616 {
8617 /* use the copy made earlier */
8618 to->vval.v_list = from->vval.v_list->lv_copylist;
8619 ++to->vval.v_list->lv_refcount;
8620 }
8621 else
8622 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8623 if (to->vval.v_list == NULL)
8624 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008625 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008626 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008627 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008628 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 case VAR_DICT:
8630 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008631 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008632 if (from->vval.v_dict == NULL)
8633 to->vval.v_dict = NULL;
8634 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8635 {
8636 /* use the copy made earlier */
8637 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8638 ++to->vval.v_dict->dv_refcount;
8639 }
8640 else
8641 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8642 if (to->vval.v_dict == NULL)
8643 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008644 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008645 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008646 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008647 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008648 }
8649 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008650 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008651}
8652
8653/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 * This function is used by f_input() and f_inputdialog() functions. The third
8655 * argument to f_input() specifies the type of completion to use at the
8656 * prompt. The third argument to f_inputdialog() specifies the value to return
8657 * when the user cancels the prompt.
8658 */
8659 void
8660get_user_input(
8661 typval_T *argvars,
8662 typval_T *rettv,
8663 int inputdialog,
8664 int secret)
8665{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008666 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 char_u *p = NULL;
8668 int c;
8669 char_u buf[NUMBUFLEN];
8670 int cmd_silent_save = cmd_silent;
8671 char_u *defstr = (char_u *)"";
8672 int xp_type = EXPAND_NOTHING;
8673 char_u *xp_arg = NULL;
8674
8675 rettv->v_type = VAR_STRING;
8676 rettv->vval.v_string = NULL;
8677
8678#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008679 /* While starting up, there is no place to enter text. When running tests
8680 * with --not-a-term we assume feedkeys() will be used. */
8681 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682 return;
8683#endif
8684
8685 cmd_silent = FALSE; /* Want to see the prompt. */
8686 if (prompt != NULL)
8687 {
8688 /* Only the part of the message after the last NL is considered as
8689 * prompt for the command line */
8690 p = vim_strrchr(prompt, '\n');
8691 if (p == NULL)
8692 p = prompt;
8693 else
8694 {
8695 ++p;
8696 c = *p;
8697 *p = NUL;
8698 msg_start();
8699 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008700 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 msg_didout = FALSE;
8702 msg_starthere();
8703 *p = c;
8704 }
8705 cmdline_row = msg_row;
8706
8707 if (argvars[1].v_type != VAR_UNKNOWN)
8708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008709 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 if (defstr != NULL)
8711 stuffReadbuffSpec(defstr);
8712
8713 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8714 {
8715 char_u *xp_name;
8716 int xp_namelen;
8717 long argt;
8718
8719 /* input() with a third argument: completion */
8720 rettv->vval.v_string = NULL;
8721
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 if (xp_name == NULL)
8724 return;
8725
8726 xp_namelen = (int)STRLEN(xp_name);
8727
8728 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8729 &xp_arg) == FAIL)
8730 return;
8731 }
8732 }
8733
8734 if (defstr != NULL)
8735 {
8736 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738 ex_normal_busy = 0;
8739 rettv->vval.v_string =
8740 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8741 xp_type, xp_arg);
8742 ex_normal_busy = save_ex_normal_busy;
8743 }
8744 if (inputdialog && rettv->vval.v_string == NULL
8745 && argvars[1].v_type != VAR_UNKNOWN
8746 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008747 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748 &argvars[2], buf));
8749
8750 vim_free(xp_arg);
8751
8752 /* since the user typed this, no need to wait for return */
8753 need_wait_return = FALSE;
8754 msg_didout = FALSE;
8755 }
8756 cmd_silent = cmd_silent_save;
8757}
8758
8759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 * ":echo expr1 ..." print each argument separated with a space, add a
8761 * newline at the end.
8762 * ":echon expr1 ..." print each argument plain.
8763 */
8764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008765ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008769 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 char_u *p;
8771 int needclr = TRUE;
8772 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008773 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008774 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008775 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776
8777 if (eap->skip)
8778 ++emsg_skip;
8779 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8780 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781 /* If eval1() causes an error message the text from the command may
8782 * still need to be cleared. E.g., "echo 22,44". */
8783 need_clr_eos = needclr;
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
8788 /*
8789 * Report the invalid expression unless the expression evaluation
8790 * has been cancelled due to an aborting error, an interrupt, or an
8791 * exception.
8792 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008793 if (!aborting() && did_emsg == did_emsg_before
8794 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008795 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008796 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 break;
8798 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008799 need_clr_eos = FALSE;
8800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 if (!eap->skip)
8802 {
8803 if (atstart)
8804 {
8805 atstart = FALSE;
8806 /* Call msg_start() after eval1(), evaluating the expression
8807 * may cause a message to appear. */
8808 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008809 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008810 /* Mark the saved text as finishing the line, so that what
8811 * follows is displayed on a new line when scrolling back
8812 * at the more prompt. */
8813 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008818 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008819 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008820 if (p != NULL)
8821 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008823 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008825 if (*p != TAB && needclr)
8826 {
8827 /* remove any text still there from the command */
8828 msg_clr_eos();
8829 needclr = FALSE;
8830 }
8831 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
8833 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008834 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008835 if (has_mbyte)
8836 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008837 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008838
8839 (void)msg_outtrans_len_attr(p, i, echo_attr);
8840 p += i - 1;
8841 }
8842 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008846 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 arg = skipwhite(arg);
8850 }
8851 eap->nextcmd = check_nextcmd(arg);
8852
8853 if (eap->skip)
8854 --emsg_skip;
8855 else
8856 {
8857 /* remove text that may still be there from the command */
8858 if (needclr)
8859 msg_clr_eos();
8860 if (eap->cmdidx == CMD_echo)
8861 msg_end();
8862 }
8863}
8864
8865/*
8866 * ":echohl {name}".
8867 */
8868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008869ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008871 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872}
8873
8874/*
8875 * ":execute expr1 ..." execute the result of an expression.
8876 * ":echomsg expr1 ..." Print a message
8877 * ":echoerr expr1 ..." Print an error
8878 * Each gets spaces around each argument and a newline at the end for
8879 * echo commands
8880 */
8881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008882ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008885 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 int ret = OK;
8887 char_u *p;
8888 garray_T ga;
8889 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008890 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
8892 ga_init2(&ga, 1, 80);
8893
8894 if (eap->skip)
8895 ++emsg_skip;
8896 while (*arg != NUL && *arg != '|' && *arg != '\n')
8897 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008898 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8899 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
8902 if (!eap->skip)
8903 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008904 char_u buf[NUMBUFLEN];
8905
8906 if (eap->cmdidx == CMD_execute)
8907 p = tv_get_string_buf(&rettv, buf);
8908 else
8909 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 len = (int)STRLEN(p);
8911 if (ga_grow(&ga, len + 2) == FAIL)
8912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 ret = FAIL;
8915 break;
8916 }
8917 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 ga.ga_len += len;
8921 }
8922
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 arg = skipwhite(arg);
8925 }
8926
8927 if (ret != FAIL && ga.ga_data != NULL)
8928 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008929 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8930 {
8931 /* Mark the already saved text as finishing the line, so that what
8932 * follows is displayed on a new line when scrolling back at the
8933 * more prompt. */
8934 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008935 }
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008938 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008939 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008940 out_flush();
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 else if (eap->cmdidx == CMD_echoerr)
8943 {
8944 /* We don't want to abort following commands, restore did_emsg. */
8945 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008946 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 if (!force_abort)
8948 did_emsg = save_did_emsg;
8949 }
8950 else if (eap->cmdidx == CMD_execute)
8951 do_cmdline((char_u *)ga.ga_data,
8952 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8953 }
8954
8955 ga_clear(&ga);
8956
8957 if (eap->skip)
8958 --emsg_skip;
8959
8960 eap->nextcmd = check_nextcmd(arg);
8961}
8962
8963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 * Find window specified by "vp" in tabpage "tp".
8965 */
8966 win_T *
8967find_win_by_nr(
8968 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008969 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008972 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 if (nr < 0)
8975 return NULL;
8976 if (nr == 0)
8977 return curwin;
8978
Bram Moolenaar29323592016-07-24 22:04:11 +02008979 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8980 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 if (nr >= LOWEST_WIN_ID)
8982 {
8983 if (wp->w_id == nr)
8984 return wp;
8985 }
8986 else if (--nr <= 0)
8987 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008988 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008990 {
8991#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008992 // check tab-local popup windows
8993 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008994 if (wp->w_id == nr)
8995 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02008996 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008997 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8998 if (wp->w_id == nr)
8999 return wp;
9000#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009002 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004}
9005
9006/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009007 * Find a window: When using a Window ID in any tab page, when using a number
9008 * in the current tab page.
9009 */
9010 win_T *
9011find_win_by_nr_or_id(typval_T *vp)
9012{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009013 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009014
9015 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009016 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009017 return find_win_by_nr(vp, NULL);
9018}
9019
9020/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009022 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 */
9024 win_T *
9025find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009026 typval_T *wvp, // VAR_UNKNOWN for current window
9027 typval_T *tvp, // VAR_UNKNOWN for current tab page
9028 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029{
9030 win_T *wp = NULL;
9031 tabpage_T *tp = NULL;
9032 long n;
9033
9034 if (wvp->v_type != VAR_UNKNOWN)
9035 {
9036 if (tvp->v_type != VAR_UNKNOWN)
9037 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009038 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039 if (n >= 0)
9040 tp = find_tabpage(n);
9041 }
9042 else
9043 tp = curtab;
9044
9045 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009046 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009048 if (wp == NULL && wvp->v_type == VAR_NUMBER
9049 && wvp->vval.v_number != -1)
9050 // A window with the specified number is not found
9051 tp = NULL;
9052 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 }
9054 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009055 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009057 tp = curtab;
9058 }
9059
9060 if (ptp != NULL)
9061 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062
9063 return wp;
9064}
9065
9066/*
9067 * getwinvar() and gettabwinvar()
9068 */
9069 void
9070getwinvar(
9071 typval_T *argvars,
9072 typval_T *rettv,
9073 int off) /* 1 for gettabwinvar() */
9074{
9075 win_T *win;
9076 char_u *varname;
9077 dictitem_T *v;
9078 tabpage_T *tp = NULL;
9079 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 win_T *oldcurwin;
9081 tabpage_T *oldtabpage;
9082 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009085 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 else
9087 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009089 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 ++emsg_off;
9091
9092 rettv->v_type = VAR_STRING;
9093 rettv->vval.v_string = NULL;
9094
9095 if (win != NULL && varname != NULL)
9096 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 /* Set curwin to be our win, temporarily. Also set the tabpage,
9098 * otherwise the window is not valid. Only do this when needed,
9099 * autocommands get blocked. */
9100 need_switch_win = !(tp == curtab && win == curwin);
9101 if (!need_switch_win
9102 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009104 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009106 if (varname[1] == NUL)
9107 {
9108 /* get all window-local options in a dict */
9109 dict_T *opts = get_winbuf_options(FALSE);
9110
9111 if (opts != NULL)
9112 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009113 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009114 done = TRUE;
9115 }
9116 }
9117 else if (get_option_tv(&varname, rettv, 1) == OK)
9118 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 done = TRUE;
9120 }
9121 else
9122 {
9123 /* Look up the variable. */
9124 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9125 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9126 varname, FALSE);
9127 if (v != NULL)
9128 {
9129 copy_tv(&v->di_tv, rettv);
9130 done = TRUE;
9131 }
9132 }
9133 }
9134
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 if (need_switch_win)
9136 /* restore previous notion of curwin */
9137 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 }
9139
9140 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9141 /* use the default return value */
9142 copy_tv(&argvars[off + 2], rettv);
9143
9144 --emsg_off;
9145}
9146
9147/*
9148 * "setwinvar()" and "settabwinvar()" functions
9149 */
9150 void
9151setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9152{
9153 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 win_T *save_curwin;
9155 tabpage_T *save_curtab;
9156 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 char_u *varname, *winvarname;
9158 typval_T *varp;
9159 char_u nbuf[NUMBUFLEN];
9160 tabpage_T *tp = NULL;
9161
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009162 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163 return;
9164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009166 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167 else
9168 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009169 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009170 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 varp = &argvars[off + 2];
9172
9173 if (win != NULL && varname != NULL && varp != NULL)
9174 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009175 need_switch_win = !(tp == curtab && win == curwin);
9176 if (!need_switch_win
9177 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 {
9179 if (*varname == '&')
9180 {
9181 long numval;
9182 char_u *strval;
9183 int error = FALSE;
9184
9185 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009186 numval = (long)tv_get_number_chk(varp, &error);
9187 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009188 if (!error && strval != NULL)
9189 set_option_value(varname, numval, strval, OPT_LOCAL);
9190 }
9191 else
9192 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009193 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009194 if (winvarname != NULL)
9195 {
9196 STRCPY(winvarname, "w:");
9197 STRCPY(winvarname + 2, varname);
9198 set_var(winvarname, varp, TRUE);
9199 vim_free(winvarname);
9200 }
9201 }
9202 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203 if (need_switch_win)
9204 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009205 }
9206}
9207
9208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9210 * "arg" points to the "&" or '+' when called, to "option" when returning.
9211 * Returns NULL when no option name found. Otherwise pointer to the char
9212 * after the option name.
9213 */
9214 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009215find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 char_u *p = *arg;
9218
9219 ++p;
9220 if (*p == 'g' && p[1] == ':')
9221 {
9222 *opt_flags = OPT_GLOBAL;
9223 p += 2;
9224 }
9225 else if (*p == 'l' && p[1] == ':')
9226 {
9227 *opt_flags = OPT_LOCAL;
9228 p += 2;
9229 }
9230 else
9231 *opt_flags = 0;
9232
9233 if (!ASCII_ISALPHA(*p))
9234 return NULL;
9235 *arg = p;
9236
9237 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9238 p += 4; /* termcap option */
9239 else
9240 while (ASCII_ISALPHA(*p))
9241 ++p;
9242 return p;
9243}
9244
9245/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009246 * Return the autoload script name for a function or variable name.
9247 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009248 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009250 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009251autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009252{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009253 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009254 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009255
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009256 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009257 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009258 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009259 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009260 STRCPY(scriptname, "autoload/");
9261 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009262 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9263 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009264 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009265 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009266 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009267}
9268
9269/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009270 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009271 * Return TRUE if a package was loaded.
9272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009274script_autoload(
9275 char_u *name,
9276 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009277{
9278 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009279 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009281 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009282
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009283 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009284 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009285 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009286 return FALSE;
9287
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009288 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009289
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009290 /* Find the name in the list of previously loaded package names. Skip
9291 * "autoload/", it's always the same. */
9292 for (i = 0; i < ga_loaded.ga_len; ++i)
9293 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9294 break;
9295 if (!reload && i < ga_loaded.ga_len)
9296 ret = FALSE; /* was loaded already */
9297 else
9298 {
9299 /* Remember the name if it wasn't loaded already. */
9300 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9301 {
9302 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9303 tofree = NULL;
9304 }
9305
9306 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009307 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009308 ret = TRUE;
9309 }
9310
9311 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009312 return ret;
9313}
9314
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9316typedef enum
9317{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009318 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9319 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9320 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321} var_flavour_T;
9322
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01009324var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 char_u *p = varname;
9327
9328 if (ASCII_ISUPPER(*p))
9329 {
9330 while (*(++p))
9331 if (ASCII_ISLOWER(*p))
9332 return VAR_FLAVOUR_SESSION;
9333 return VAR_FLAVOUR_VIMINFO;
9334 }
9335 else
9336 return VAR_FLAVOUR_DEFAULT;
9337}
9338#endif
9339
9340#if defined(FEAT_VIMINFO) || defined(PROTO)
9341/*
9342 * Restore global vars that start with a capital from the viminfo file
9343 */
9344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009345read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009348 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00009349 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009350 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
9352 if (!writing && (find_viminfo_parameter('!') != NULL))
9353 {
9354 tab = vim_strchr(virp->vir_line + 1, '\t');
9355 if (tab != NULL)
9356 {
9357 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009358 switch (*tab)
9359 {
9360 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009361#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009362 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009363#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009364 case 'D': type = VAR_DICT; break;
9365 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009366 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009367 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370 tab = vim_strchr(tab, '\t');
9371 if (tab != NULL)
9372 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009373 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009374 if (type == VAR_STRING || type == VAR_DICT
9375 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009376 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009378#ifdef FEAT_FLOAT
9379 else if (type == VAR_FLOAT)
9380 (void)string2float(tab + 1, &tv.vval.v_float);
9381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009383 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009384 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009385 {
9386 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9387
9388 if (etv == NULL)
9389 /* Failed to parse back the dict or list, use it as a
9390 * string. */
9391 tv.v_type = VAR_STRING;
9392 else
9393 {
9394 vim_free(tv.vval.v_string);
9395 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009396 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009397 }
9398 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009399 else if (type == VAR_BLOB)
9400 {
9401 blob_T *blob = string2blob(tv.vval.v_string);
9402
9403 if (blob == NULL)
9404 // Failed to parse back the blob, use it as a string.
9405 tv.v_type = VAR_STRING;
9406 else
9407 {
9408 vim_free(tv.vval.v_string);
9409 tv.v_type = VAR_BLOB;
9410 tv.vval.v_blob = blob;
9411 }
9412 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009413
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009414 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009415 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009416 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009417 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009418
9419 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009420 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009421 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9422 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009423 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 }
9425 }
9426 }
9427
9428 return viminfo_readline(virp);
9429}
9430
9431/*
9432 * Write global vars that start with a capital to the viminfo file
9433 */
9434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009435write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 hashitem_T *hi;
9438 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009439 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009440 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009441 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009443 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444
9445 if (find_viminfo_parameter('!') == NULL)
9446 return;
9447
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009448 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009449
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009450 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009453 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009455 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 this_var = HI2DI(hi);
9457 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009460 {
9461 case VAR_STRING: s = "STR"; break;
9462 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009463 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009464 case VAR_DICT: s = "DIC"; break;
9465 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009466 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009467 case VAR_SPECIAL: s = "XPL"; break;
9468
9469 case VAR_UNKNOWN:
9470 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009471 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009472 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009473 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009474 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009475 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009476 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009477 if (this_var->di_tv.v_type == VAR_SPECIAL)
9478 {
9479 sprintf((char *)numbuf, "%ld",
9480 (long)this_var->di_tv.vval.v_number);
9481 p = numbuf;
9482 tofree = NULL;
9483 }
9484 else
9485 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009486 if (p != NULL)
9487 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009488 vim_free(tofree);
9489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 }
9491 }
9492}
9493#endif
9494
9495#if defined(FEAT_SESSION) || defined(PROTO)
9496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
Bram Moolenaar33570922005-01-25 22:26:29 +00009499 hashitem_T *hi;
9500 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009501 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 char_u *p, *t;
9503
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009504 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009505 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009507 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009509 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009510 this_var = HI2DI(hi);
9511 if ((this_var->di_tv.v_type == VAR_NUMBER
9512 || this_var->di_tv.v_type == VAR_STRING)
9513 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009514 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009515 /* Escape special characters with a backslash. Turn a LF and
9516 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009517 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009518 (char_u *)"\\\"\n\r");
9519 if (p == NULL) /* out of memory */
9520 break;
9521 for (t = p; *t != NUL; ++t)
9522 if (*t == '\n')
9523 *t = 'n';
9524 else if (*t == '\r')
9525 *t = 'r';
9526 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 this_var->di_key,
9528 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9529 : ' ',
9530 p,
9531 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9532 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009533 || put_eol(fd) == FAIL)
9534 {
9535 vim_free(p);
9536 return FAIL;
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 vim_free(p);
9539 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009540#ifdef FEAT_FLOAT
9541 else if (this_var->di_tv.v_type == VAR_FLOAT
9542 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9543 {
9544 float_T f = this_var->di_tv.vval.v_float;
9545 int sign = ' ';
9546
9547 if (f < 0)
9548 {
9549 f = -f;
9550 sign = '-';
9551 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009552 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009553 this_var->di_key, sign, f) < 0)
9554 || put_eol(fd) == FAIL)
9555 return FAIL;
9556 }
9557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 }
9559 }
9560 return OK;
9561}
9562#endif
9563
Bram Moolenaar661b1822005-07-28 22:36:45 +00009564/*
9565 * Display script name where an item was last set.
9566 * Should only be invoked when 'verbose' is non-zero.
9567 */
9568 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009569last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009570{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009571 char_u *p;
9572
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009573 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009574 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009575 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009576 if (p != NULL)
9577 {
9578 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009579 msg_puts(_("\n\tLast set from "));
9580 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009581 if (script_ctx.sc_lnum > 0)
9582 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009583 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009584 msg_outnum((long)script_ctx.sc_lnum);
9585 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009586 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009587 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009588 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009589 }
9590}
9591
Bram Moolenaar61be3762019-03-19 23:04:17 +01009592/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009593 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9594 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009595 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009597reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009598{
9599 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9600 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009601 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9602 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009603 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009604 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009605}
9606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 * Add an assert error to v:errors.
9609 */
9610 void
9611assert_error(garray_T *gap)
9612{
9613 struct vimvar *vp = &vimvars[VV_ERRORS];
9614
9615 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9616 /* Make sure v:errors is a list. */
9617 set_vim_var_list(VV_ERRORS, list_alloc());
9618 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9619}
Bram Moolenaar31988702018-02-13 12:57:42 +01009620/*
9621 * Compare "typ1" and "typ2". Put the result in "typ1".
9622 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009623 int
9624typval_compare(
9625 typval_T *typ1, /* first operand */
9626 typval_T *typ2, /* second operand */
9627 exptype_T type, /* operator */
9628 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009629 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009630{
9631 int i;
9632 varnumber_T n1, n2;
9633 char_u *s1, *s2;
9634 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9635
Bram Moolenaar31988702018-02-13 12:57:42 +01009636 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009637 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009638 /* For "is" a different type always means FALSE, for "notis"
9639 * it means TRUE. */
9640 n1 = (type == TYPE_NEQUAL);
9641 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009642 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9643 {
9644 if (type_is)
9645 {
9646 n1 = (typ1->v_type == typ2->v_type
9647 && typ1->vval.v_blob == typ2->vval.v_blob);
9648 if (type == TYPE_NEQUAL)
9649 n1 = !n1;
9650 }
9651 else if (typ1->v_type != typ2->v_type
9652 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9653 {
9654 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009655 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009656 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009657 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009658 clear_tv(typ1);
9659 return FAIL;
9660 }
9661 else
9662 {
9663 // Compare two Blobs for being equal or unequal.
9664 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9665 if (type == TYPE_NEQUAL)
9666 n1 = !n1;
9667 }
9668 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009669 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9670 {
9671 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009672 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009673 n1 = (typ1->v_type == typ2->v_type
9674 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009675 if (type == TYPE_NEQUAL)
9676 n1 = !n1;
9677 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009678 else if (typ1->v_type != typ2->v_type
9679 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009680 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009681 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009682 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009683 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009684 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009685 clear_tv(typ1);
9686 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009687 }
9688 else
9689 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009690 /* Compare two Lists for being equal or unequal. */
9691 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9692 ic, FALSE);
9693 if (type == TYPE_NEQUAL)
9694 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009695 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009696 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009697
9698 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9699 {
9700 if (type_is)
9701 {
9702 n1 = (typ1->v_type == typ2->v_type
9703 && typ1->vval.v_dict == typ2->vval.v_dict);
9704 if (type == TYPE_NEQUAL)
9705 n1 = !n1;
9706 }
9707 else if (typ1->v_type != typ2->v_type
9708 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9709 {
9710 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009711 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009712 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009713 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009714 clear_tv(typ1);
9715 return FAIL;
9716 }
9717 else
9718 {
9719 /* Compare two Dictionaries for being equal or unequal. */
9720 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9721 ic, FALSE);
9722 if (type == TYPE_NEQUAL)
9723 n1 = !n1;
9724 }
9725 }
9726
9727 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9728 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9729 {
9730 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009732 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009733 clear_tv(typ1);
9734 return FAIL;
9735 }
9736 if ((typ1->v_type == VAR_PARTIAL
9737 && typ1->vval.v_partial == NULL)
9738 || (typ2->v_type == VAR_PARTIAL
9739 && typ2->vval.v_partial == NULL))
9740 /* when a partial is NULL assume not equal */
9741 n1 = FALSE;
9742 else if (type_is)
9743 {
9744 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9745 /* strings are considered the same if their value is
9746 * the same */
9747 n1 = tv_equal(typ1, typ2, ic, FALSE);
9748 else if (typ1->v_type == VAR_PARTIAL
9749 && typ2->v_type == VAR_PARTIAL)
9750 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9751 else
9752 n1 = FALSE;
9753 }
9754 else
9755 n1 = tv_equal(typ1, typ2, ic, FALSE);
9756 if (type == TYPE_NEQUAL)
9757 n1 = !n1;
9758 }
9759
9760#ifdef FEAT_FLOAT
9761 /*
9762 * If one of the two variables is a float, compare as a float.
9763 * When using "=~" or "!~", always compare as string.
9764 */
9765 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9766 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9767 {
9768 float_T f1, f2;
9769
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009770 f1 = tv_get_float(typ1);
9771 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009772 n1 = FALSE;
9773 switch (type)
9774 {
9775 case TYPE_EQUAL: n1 = (f1 == f2); break;
9776 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9777 case TYPE_GREATER: n1 = (f1 > f2); break;
9778 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9779 case TYPE_SMALLER: n1 = (f1 < f2); break;
9780 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9781 case TYPE_UNKNOWN:
9782 case TYPE_MATCH:
9783 case TYPE_NOMATCH: break; /* avoid gcc warning */
9784 }
9785 }
9786#endif
9787
9788 /*
9789 * If one of the two variables is a number, compare as a number.
9790 * When using "=~" or "!~", always compare as string.
9791 */
9792 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9793 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009795 n1 = tv_get_number(typ1);
9796 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009797 switch (type)
9798 {
9799 case TYPE_EQUAL: n1 = (n1 == n2); break;
9800 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9801 case TYPE_GREATER: n1 = (n1 > n2); break;
9802 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9803 case TYPE_SMALLER: n1 = (n1 < n2); break;
9804 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9805 case TYPE_UNKNOWN:
9806 case TYPE_MATCH:
9807 case TYPE_NOMATCH: break; /* avoid gcc warning */
9808 }
9809 }
9810 else
9811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009812 s1 = tv_get_string_buf(typ1, buf1);
9813 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009814 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9815 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9816 else
9817 i = 0;
9818 n1 = FALSE;
9819 switch (type)
9820 {
9821 case TYPE_EQUAL: n1 = (i == 0); break;
9822 case TYPE_NEQUAL: n1 = (i != 0); break;
9823 case TYPE_GREATER: n1 = (i > 0); break;
9824 case TYPE_GEQUAL: n1 = (i >= 0); break;
9825 case TYPE_SMALLER: n1 = (i < 0); break;
9826 case TYPE_SEQUAL: n1 = (i <= 0); break;
9827
9828 case TYPE_MATCH:
9829 case TYPE_NOMATCH:
9830 n1 = pattern_match(s2, s1, ic);
9831 if (type == TYPE_NOMATCH)
9832 n1 = !n1;
9833 break;
9834
9835 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9836 }
9837 }
9838 clear_tv(typ1);
9839 typ1->v_type = VAR_NUMBER;
9840 typ1->vval.v_number = n1;
9841
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009842 return OK;
9843}
9844
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009845 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009846typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009847{
9848 char_u *tofree;
9849 char_u numbuf[NUMBUFLEN];
9850 char_u *ret = NULL;
9851
9852 if (arg == NULL)
9853 return vim_strsave((char_u *)"(does not exist)");
9854 ret = tv2string(arg, &tofree, numbuf, 0);
9855 /* Make a copy if we have a value but it's not in allocated memory. */
9856 if (ret != NULL && tofree == NULL)
9857 ret = vim_strsave(ret);
9858 return ret;
9859}
9860
9861 int
9862var_exists(char_u *var)
9863{
9864 char_u *name;
9865 char_u *tofree;
9866 typval_T tv;
9867 int len = 0;
9868 int n = FALSE;
9869
9870 /* get_name_len() takes care of expanding curly braces */
9871 name = var;
9872 len = get_name_len(&var, &tofree, TRUE, FALSE);
9873 if (len > 0)
9874 {
9875 if (tofree != NULL)
9876 name = tofree;
9877 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9878 if (n)
9879 {
9880 /* handle d.key, l[idx], f(expr) */
9881 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9882 if (n)
9883 clear_tv(&tv);
9884 }
9885 }
9886 if (*var != NUL)
9887 n = FALSE;
9888
9889 vim_free(tofree);
9890 return n;
9891}
9892
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893#endif /* FEAT_EVAL */
9894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009896#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
Bram Moolenaar4f974752019-02-17 17:44:42 +01009898#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899/*
9900 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009904 * Get the short path (8.3) for the filename in "fnamep".
9905 * Only works for a valid file name.
9906 * When the path gets longer "fnamep" is changed and the allocated buffer
9907 * is put in "bufp".
9908 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9909 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 */
9911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009912get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009914 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 char_u *newbuf;
9916
9917 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009918 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (l > len - 1)
9920 {
9921 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009922 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 newbuf = vim_strnsave(*fnamep, l);
9924 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
9927 vim_free(*bufp);
9928 *fnamep = *bufp = newbuf;
9929
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009930 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009931 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 }
9933
9934 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009935 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009939 * Get the short path (8.3) for the filename in "fname". The converted
9940 * path is returned in "bufp".
9941 *
9942 * Some of the directories specified in "fname" may not exist. This function
9943 * will shorten the existing directories at the beginning of the path and then
9944 * append the remaining non-existing path.
9945 *
9946 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009947 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009948 * bufp - Pointer to an allocated buffer for the filename.
9949 * fnamelen - Length of the filename pointed to by fname
9950 *
9951 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 */
9953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009954shortpath_for_invalid_fname(
9955 char_u **fname,
9956 char_u **bufp,
9957 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009959 char_u *short_fname, *save_fname, *pbuf_unused;
9960 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009962 int old_len, len;
9963 int new_len, sfx_len;
9964 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009967 old_len = *fnamelen;
9968 save_fname = vim_strnsave(*fname, old_len);
9969 pbuf_unused = NULL;
9970 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009972 endp = save_fname + old_len - 1; /* Find the end of the copy */
9973 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009975 /*
9976 * Try shortening the supplied path till it succeeds by removing one
9977 * directory at a time from the tail of the path.
9978 */
9979 len = 0;
9980 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009982 /* go back one path-separator */
9983 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9984 --endp;
9985 if (endp <= save_fname)
9986 break; /* processed the complete path */
9987
9988 /*
9989 * Replace the path separator with a NUL and try to shorten the
9990 * resulting path.
9991 */
9992 ch = *endp;
9993 *endp = 0;
9994 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009995 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009996 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9997 {
9998 retval = FAIL;
9999 goto theend;
10000 }
10001 *endp = ch; /* preserve the string */
10002
10003 if (len > 0)
10004 break; /* successfully shortened the path */
10005
10006 /* failed to shorten the path. Skip the path separator */
10007 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 }
10009
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010010 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010012 /*
10013 * Succeeded in shortening the path. Now concatenate the shortened
10014 * path with the remaining path at the tail.
10015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010017 /* Compute the length of the new path. */
10018 sfx_len = (int)(save_endp - endp) + 1;
10019 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010021 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010023 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010025 /* There is not enough space in the currently allocated string,
10026 * copy it to a buffer big enough. */
10027 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010029 {
10030 retval = FAIL;
10031 goto theend;
10032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 }
10034 else
10035 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010036 /* Transfer short_fname to the main buffer (it's big enough),
10037 * unless get_short_pathname() did its work in-place. */
10038 *fname = *bufp = save_fname;
10039 if (short_fname != save_fname)
10040 vim_strncpy(save_fname, short_fname, len);
10041 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010043
10044 /* concat the not-shortened part of the path */
10045 vim_strncpy(*fname + len, endp, sfx_len);
10046 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010048
10049theend:
10050 vim_free(pbuf_unused);
10051 vim_free(save_fname);
10052
10053 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054}
10055
10056/*
10057 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010058 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 */
10060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010061shortpath_for_partial(
10062 char_u **fnamep,
10063 char_u **bufp,
10064 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065{
10066 int sepcount, len, tflen;
10067 char_u *p;
10068 char_u *pbuf, *tfname;
10069 int hasTilde;
10070
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010071 /* Count up the path separators from the RHS.. so we know which part
10072 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010074 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (vim_ispathsep(*p))
10076 ++sepcount;
10077
10078 /* Need full path first (use expand_env() to remove a "~/") */
10079 hasTilde = (**fnamep == '~');
10080 if (hasTilde)
10081 pbuf = tfname = expand_env_save(*fnamep);
10082 else
10083 pbuf = tfname = FullName_save(*fnamep, FALSE);
10084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010085 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010087 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
10090 if (len == 0)
10091 {
10092 /* Don't have a valid filename, so shorten the rest of the
10093 * path if we can. This CAN give us invalid 8.3 filenames, but
10094 * there's not a lot of point in guessing what it might be.
10095 */
10096 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010097 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
10100
10101 /* Count the paths backward to find the beginning of the desired string. */
10102 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010103 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010104 if (has_mbyte)
10105 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 if (vim_ispathsep(*p))
10107 {
10108 if (sepcount == 0 || (hasTilde && sepcount == 1))
10109 break;
10110 else
10111 sepcount --;
10112 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (hasTilde)
10115 {
10116 --p;
10117 if (p >= tfname)
10118 *p = '~';
10119 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010120 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 }
10122 else
10123 ++p;
10124
10125 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10126 vim_free(*bufp);
10127 *fnamelen = (int)STRLEN(p);
10128 *bufp = pbuf;
10129 *fnamep = p;
10130
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010131 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010133#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134
10135/*
10136 * Adjust a filename, according to a string of modifiers.
10137 * *fnamep must be NUL terminated when called. When returning, the length is
10138 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010139 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 * When there is an error, *fnamep is set to NULL.
10141 */
10142 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010143modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010144 char_u *src, // string with modifiers
10145 int tilde_file, // "~" is a file name, not $HOME
10146 int *usedlen, // characters after src that are used
10147 char_u **fnamep, // file name so far
10148 char_u **bufp, // buffer for allocated file name or NULL
10149 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 int valid = 0;
10152 char_u *tail;
10153 char_u *s, *p, *pbuf;
10154 char_u dirname[MAXPATHL];
10155 int c;
10156 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010157#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010158 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 int has_shortname = 0;
10160#endif
10161
10162repeat:
10163 /* ":p" - full path/file_name */
10164 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10165 {
10166 has_fullname = 1;
10167
10168 valid |= VALID_PATH;
10169 *usedlen += 2;
10170
10171 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10172 if ((*fnamep)[0] == '~'
10173#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10174 && ((*fnamep)[1] == '/'
10175# ifdef BACKSLASH_IN_FILENAME
10176 || (*fnamep)[1] == '\\'
10177# endif
10178 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010180 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 )
10182 {
10183 *fnamep = expand_env_save(*fnamep);
10184 vim_free(*bufp); /* free any allocated file name */
10185 *bufp = *fnamep;
10186 if (*fnamep == NULL)
10187 return -1;
10188 }
10189
10190 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010191 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 {
10193 if (vim_ispathsep(*p)
10194 && p[1] == '.'
10195 && (p[2] == NUL
10196 || vim_ispathsep(p[2])
10197 || (p[2] == '.'
10198 && (p[3] == NUL || vim_ispathsep(p[3])))))
10199 break;
10200 }
10201
10202 /* FullName_save() is slow, don't use it when not needed. */
10203 if (*p != NUL || !vim_isAbsName(*fnamep))
10204 {
10205 *fnamep = FullName_save(*fnamep, *p != NUL);
10206 vim_free(*bufp); /* free any allocated file name */
10207 *bufp = *fnamep;
10208 if (*fnamep == NULL)
10209 return -1;
10210 }
10211
Bram Moolenaar4f974752019-02-17 17:44:42 +010010212#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010213# if _WIN32_WINNT >= 0x0500
10214 if (vim_strchr(*fnamep, '~') != NULL)
10215 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010216 // Expand 8.3 filename to full path. Needed to make sure the same
10217 // file does not have two different names.
10218 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10219 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10220 WCHAR buf[_MAX_PATH];
10221
10222 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010223 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010224 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010225 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010226 char_u *p = utf16_to_enc(buf, NULL);
10227
10228 if (p != NULL)
10229 {
10230 vim_free(*bufp); // free any allocated file name
10231 *bufp = *fnamep = p;
10232 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010233 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010234 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010235 }
10236 }
10237# endif
10238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 /* Append a path separator to a directory. */
10240 if (mch_isdir(*fnamep))
10241 {
10242 /* Make room for one or two extra characters. */
10243 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10244 vim_free(*bufp); /* free any allocated file name */
10245 *bufp = *fnamep;
10246 if (*fnamep == NULL)
10247 return -1;
10248 add_pathsep(*fnamep);
10249 }
10250 }
10251
10252 /* ":." - path relative to the current directory */
10253 /* ":~" - path relative to the home directory */
10254 /* ":8" - shortname path - postponed till after */
10255 while (src[*usedlen] == ':'
10256 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10257 {
10258 *usedlen += 2;
10259 if (c == '8')
10260 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010261#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 has_shortname = 1; /* Postpone this. */
10263#endif
10264 continue;
10265 }
10266 pbuf = NULL;
10267 /* Need full path first (use expand_env() to remove a "~/") */
10268 if (!has_fullname)
10269 {
10270 if (c == '.' && **fnamep == '~')
10271 p = pbuf = expand_env_save(*fnamep);
10272 else
10273 p = pbuf = FullName_save(*fnamep, FALSE);
10274 }
10275 else
10276 p = *fnamep;
10277
10278 has_fullname = 0;
10279
10280 if (p != NULL)
10281 {
10282 if (c == '.')
10283 {
10284 mch_dirname(dirname, MAXPATHL);
10285 s = shorten_fname(p, dirname);
10286 if (s != NULL)
10287 {
10288 *fnamep = s;
10289 if (pbuf != NULL)
10290 {
10291 vim_free(*bufp); /* free any allocated file name */
10292 *bufp = pbuf;
10293 pbuf = NULL;
10294 }
10295 }
10296 }
10297 else
10298 {
10299 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10300 /* Only replace it when it starts with '~' */
10301 if (*dirname == '~')
10302 {
10303 s = vim_strsave(dirname);
10304 if (s != NULL)
10305 {
10306 *fnamep = s;
10307 vim_free(*bufp);
10308 *bufp = s;
10309 }
10310 }
10311 }
10312 vim_free(pbuf);
10313 }
10314 }
10315
10316 tail = gettail(*fnamep);
10317 *fnamelen = (int)STRLEN(*fnamep);
10318
10319 /* ":h" - head, remove "/file_name", can be repeated */
10320 /* Don't remove the first "/" or "c:\" */
10321 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10322 {
10323 valid |= VALID_HEAD;
10324 *usedlen += 2;
10325 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010326 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010327 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 *fnamelen = (int)(tail - *fnamep);
10329#ifdef VMS
10330 if (*fnamelen > 0)
10331 *fnamelen += 1; /* the path separator is part of the path */
10332#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010333 if (*fnamelen == 0)
10334 {
10335 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10336 p = vim_strsave((char_u *)".");
10337 if (p == NULL)
10338 return -1;
10339 vim_free(*bufp);
10340 *bufp = *fnamep = tail = p;
10341 *fnamelen = 1;
10342 }
10343 else
10344 {
10345 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010346 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 }
10349
10350 /* ":8" - shortname */
10351 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10352 {
10353 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010354#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 has_shortname = 1;
10356#endif
10357 }
10358
Bram Moolenaar4f974752019-02-17 17:44:42 +010010359#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010360 /*
10361 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 */
10363 if (has_shortname)
10364 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010365 /* Copy the string if it is shortened by :h and when it wasn't copied
10366 * yet, because we are going to change it in place. Avoids changing
10367 * the buffer name for "%:8". */
10368 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 {
10370 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010371 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 return -1;
10373 vim_free(*bufp);
10374 *bufp = *fnamep = p;
10375 }
10376
10377 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010378 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 if (!has_fullname && !vim_isAbsName(*fnamep))
10380 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010381 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 return -1;
10383 }
10384 else
10385 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010386 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaardc935552011-08-17 15:23:23 +020010388 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010390 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 return -1;
10392
10393 if (l == 0)
10394 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010395 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010397 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 return -1;
10399 }
10400 *fnamelen = l;
10401 }
10402 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010403#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404
10405 /* ":t" - tail, just the basename */
10406 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10407 {
10408 *usedlen += 2;
10409 *fnamelen -= (int)(tail - *fnamep);
10410 *fnamep = tail;
10411 }
10412
10413 /* ":e" - extension, can be repeated */
10414 /* ":r" - root, without extension, can be repeated */
10415 while (src[*usedlen] == ':'
10416 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10417 {
10418 /* find a '.' in the tail:
10419 * - for second :e: before the current fname
10420 * - otherwise: The last '.'
10421 */
10422 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10423 s = *fnamep - 2;
10424 else
10425 s = *fnamep + *fnamelen - 1;
10426 for ( ; s > tail; --s)
10427 if (s[0] == '.')
10428 break;
10429 if (src[*usedlen + 1] == 'e') /* :e */
10430 {
10431 if (s > tail)
10432 {
10433 *fnamelen += (int)(*fnamep - (s + 1));
10434 *fnamep = s + 1;
10435#ifdef VMS
10436 /* cut version from the extension */
10437 s = *fnamep + *fnamelen - 1;
10438 for ( ; s > *fnamep; --s)
10439 if (s[0] == ';')
10440 break;
10441 if (s > *fnamep)
10442 *fnamelen = s - *fnamep;
10443#endif
10444 }
10445 else if (*fnamep <= tail)
10446 *fnamelen = 0;
10447 }
10448 else /* :r */
10449 {
10450 if (s > tail) /* remove one extension */
10451 *fnamelen = (int)(s - *fnamep);
10452 }
10453 *usedlen += 2;
10454 }
10455
10456 /* ":s?pat?foo?" - substitute */
10457 /* ":gs?pat?foo?" - global substitute */
10458 if (src[*usedlen] == ':'
10459 && (src[*usedlen + 1] == 's'
10460 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10461 {
10462 char_u *str;
10463 char_u *pat;
10464 char_u *sub;
10465 int sep;
10466 char_u *flags;
10467 int didit = FALSE;
10468
10469 flags = (char_u *)"";
10470 s = src + *usedlen + 2;
10471 if (src[*usedlen + 1] == 'g')
10472 {
10473 flags = (char_u *)"g";
10474 ++s;
10475 }
10476
10477 sep = *s++;
10478 if (sep)
10479 {
10480 /* find end of pattern */
10481 p = vim_strchr(s, sep);
10482 if (p != NULL)
10483 {
10484 pat = vim_strnsave(s, (int)(p - s));
10485 if (pat != NULL)
10486 {
10487 s = p + 1;
10488 /* find end of substitution */
10489 p = vim_strchr(s, sep);
10490 if (p != NULL)
10491 {
10492 sub = vim_strnsave(s, (int)(p - s));
10493 str = vim_strnsave(*fnamep, *fnamelen);
10494 if (sub != NULL && str != NULL)
10495 {
10496 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010497 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 if (s != NULL)
10499 {
10500 *fnamep = s;
10501 *fnamelen = (int)STRLEN(s);
10502 vim_free(*bufp);
10503 *bufp = s;
10504 didit = TRUE;
10505 }
10506 }
10507 vim_free(sub);
10508 vim_free(str);
10509 }
10510 vim_free(pat);
10511 }
10512 }
10513 /* after using ":s", repeat all the modifiers */
10514 if (didit)
10515 goto repeat;
10516 }
10517 }
10518
Bram Moolenaar26df0922014-02-23 23:39:13 +010010519 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10520 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010521 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010522 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010523 if (c != NUL)
10524 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010525 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010526 if (c != NUL)
10527 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010528 if (p == NULL)
10529 return -1;
10530 vim_free(*bufp);
10531 *bufp = *fnamep = p;
10532 *fnamelen = (int)STRLEN(p);
10533 *usedlen += 2;
10534 }
10535
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 return valid;
10537}
10538
10539/*
10540 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010541 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 * "flags" can be "g" to do a global substitute.
10543 * Returns an allocated string, NULL for error.
10544 */
10545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010546do_string_sub(
10547 char_u *str,
10548 char_u *pat,
10549 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010550 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010551 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 int sublen;
10554 regmatch_T regmatch;
10555 int i;
10556 int do_all;
10557 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010558 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 garray_T ga;
10560 char_u *ret;
10561 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010562 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563
10564 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10565 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010566 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567
10568 ga_init2(&ga, 1, 200);
10569
10570 do_all = (flags[0] == 'g');
10571
10572 regmatch.rm_ic = p_ic;
10573 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10574 if (regmatch.regprog != NULL)
10575 {
10576 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010577 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10579 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010580 /* Skip empty match except for first match. */
10581 if (regmatch.startp[0] == regmatch.endp[0])
10582 {
10583 if (zero_width == regmatch.startp[0])
10584 {
10585 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010586 i = MB_PTR2LEN(tail);
10587 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10588 (size_t)i);
10589 ga.ga_len += i;
10590 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010591 continue;
10592 }
10593 zero_width = regmatch.startp[0];
10594 }
10595
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 /*
10597 * Get some space for a temporary buffer to do the substitution
10598 * into. It will contain:
10599 * - The text up to where the match is.
10600 * - The substituted text.
10601 * - The text after the match.
10602 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010603 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010604 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10606 {
10607 ga_clear(&ga);
10608 break;
10609 }
10610
10611 /* copy the text up to where the match is */
10612 i = (int)(regmatch.startp[0] - tail);
10613 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10614 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010615 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 + ga.ga_len + i, TRUE, TRUE, FALSE);
10617 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010618 tail = regmatch.endp[0];
10619 if (*tail == NUL)
10620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 if (!do_all)
10622 break;
10623 }
10624
10625 if (ga.ga_data != NULL)
10626 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10627
Bram Moolenaar473de612013-06-08 18:19:48 +020010628 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 }
10630
10631 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10632 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010633 if (p_cpo == empty_option)
10634 p_cpo = save_cpo;
10635 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010636 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010637 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638
10639 return ret;
10640}
10641
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642 static int
10643filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10644{
10645 typval_T rettv;
10646 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010647 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648
10649 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10650 argv[0] = vimvars[VV_KEY].vv_tv;
10651 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010652 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10653 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 if (map)
10655 {
10656 /* map(): replace the list item value */
10657 clear_tv(tv);
10658 rettv.v_lock = 0;
10659 *tv = rettv;
10660 }
10661 else
10662 {
10663 int error = FALSE;
10664
10665 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010666 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 clear_tv(&rettv);
10668 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010669 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010670 if (error)
10671 goto theend;
10672 }
10673 retval = OK;
10674theend:
10675 clear_tv(&vimvars[VV_VAL].vv_tv);
10676 return retval;
10677}
10678
10679
10680/*
10681 * Implementation of map() and filter().
10682 */
10683 void
10684filter_map(typval_T *argvars, typval_T *rettv, int map)
10685{
10686 typval_T *expr;
10687 listitem_T *li, *nli;
10688 list_T *l = NULL;
10689 dictitem_T *di;
10690 hashtab_T *ht;
10691 hashitem_T *hi;
10692 dict_T *d = NULL;
10693 typval_T save_val;
10694 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010695 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010696 int rem;
10697 int todo;
10698 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10699 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10700 : N_("filter() argument"));
10701 int save_did_emsg;
10702 int idx = 0;
10703
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010704 if (argvars[0].v_type == VAR_BLOB)
10705 {
10706 if ((b = argvars[0].vval.v_blob) == NULL)
10707 return;
10708 }
10709 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010710 {
10711 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010712 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010713 return;
10714 }
10715 else if (argvars[0].v_type == VAR_DICT)
10716 {
10717 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010718 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010719 return;
10720 }
10721 else
10722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010723 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010724 return;
10725 }
10726
10727 expr = &argvars[1];
10728 /* On type errors, the preceding call has already displayed an error
10729 * message. Avoid a misleading error message for an empty string that
10730 * was not passed as argument. */
10731 if (expr->v_type != VAR_UNKNOWN)
10732 {
10733 prepare_vimvar(VV_VAL, &save_val);
10734
10735 /* We reset "did_emsg" to be able to detect whether an error
10736 * occurred during evaluation of the expression. */
10737 save_did_emsg = did_emsg;
10738 did_emsg = FALSE;
10739
10740 prepare_vimvar(VV_KEY, &save_key);
10741 if (argvars[0].v_type == VAR_DICT)
10742 {
10743 vimvars[VV_KEY].vv_type = VAR_STRING;
10744
10745 ht = &d->dv_hashtab;
10746 hash_lock(ht);
10747 todo = (int)ht->ht_used;
10748 for (hi = ht->ht_array; todo > 0; ++hi)
10749 {
10750 if (!HASHITEM_EMPTY(hi))
10751 {
10752 int r;
10753
10754 --todo;
10755 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010756 if (map && (var_check_lock(di->di_tv.v_lock,
10757 arg_errmsg, TRUE)
10758 || var_check_ro(di->di_flags,
10759 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010760 break;
10761 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10762 r = filter_map_one(&di->di_tv, expr, map, &rem);
10763 clear_tv(&vimvars[VV_KEY].vv_tv);
10764 if (r == FAIL || did_emsg)
10765 break;
10766 if (!map && rem)
10767 {
10768 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10769 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10770 break;
10771 dictitem_remove(d, di);
10772 }
10773 }
10774 }
10775 hash_unlock(ht);
10776 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010777 else if (argvars[0].v_type == VAR_BLOB)
10778 {
10779 int i;
10780 typval_T tv;
10781
10782 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10783 for (i = 0; i < b->bv_ga.ga_len; i++)
10784 {
10785 tv.v_type = VAR_NUMBER;
10786 tv.vval.v_number = blob_get(b, i);
10787 vimvars[VV_KEY].vv_nr = idx;
10788 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10789 break;
10790 if (tv.v_type != VAR_NUMBER)
10791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010792 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010793 return;
10794 }
10795 tv.v_type = VAR_NUMBER;
10796 blob_set(b, i, tv.vval.v_number);
10797 if (!map && rem)
10798 {
10799 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10800
10801 mch_memmove(p + idx, p + i + 1,
10802 (size_t)b->bv_ga.ga_len - i - 1);
10803 --b->bv_ga.ga_len;
10804 --i;
10805 }
10806 }
10807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010808 else
10809 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010810 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010811 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10812
10813 for (li = l->lv_first; li != NULL; li = nli)
10814 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010815 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 break;
10817 nli = li->li_next;
10818 vimvars[VV_KEY].vv_nr = idx;
10819 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10820 || did_emsg)
10821 break;
10822 if (!map && rem)
10823 listitem_remove(l, li);
10824 ++idx;
10825 }
10826 }
10827
10828 restore_vimvar(VV_KEY, &save_key);
10829 restore_vimvar(VV_VAL, &save_val);
10830
10831 did_emsg |= save_did_emsg;
10832 }
10833
10834 copy_tv(&argvars[0], rettv);
10835}
10836
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */