blob: 1967f9401c8cac86164c47894e298c661bf6ea7a [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 Moolenaar22a0c0c2019-08-09 23:25:08 +020035static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000036
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010037#define NAMESPACE_CHAR (char_u *)"abglstvw"
38
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020039static dictitem_T globvars_var; /* variable used for g: */
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;
Bram Moolenaar48570482017-10-30 21:48:41 +0100769 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200770 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100771
772 if (expr->v_type == VAR_FUNC)
773 {
774 s = expr->vval.v_string;
775 if (s == NULL || *s == NUL)
776 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200777 vim_memset(&funcexe, 0, sizeof(funcexe));
778 funcexe.evaluate = TRUE;
779 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100780 return FAIL;
781 }
782 else if (expr->v_type == VAR_PARTIAL)
783 {
784 partial_T *partial = expr->vval.v_partial;
785
786 s = partial_name(partial);
787 if (s == NULL || *s == NUL)
788 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200789 vim_memset(&funcexe, 0, sizeof(funcexe));
790 funcexe.evaluate = TRUE;
791 funcexe.partial = partial;
792 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100793 return FAIL;
794 }
795 else
796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100797 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100798 if (s == NULL)
799 return FAIL;
800 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100801 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100802 return FAIL;
803 if (*s != NUL) /* check for trailing chars after expr */
804 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200805 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100807 return FAIL;
808 }
809 }
810 return OK;
811}
812
813/*
814 * Like eval_to_bool() but using a typval_T instead of a string.
815 * Works for string, funcref and partial.
816 */
817 int
818eval_expr_to_bool(typval_T *expr, int *error)
819{
820 typval_T rettv;
821 int res;
822
823 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
824 {
825 *error = TRUE;
826 return FALSE;
827 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100828 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100829 clear_tv(&rettv);
830 return res;
831}
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833/*
834 * Top level evaluation function, returning a string. If "skip" is TRUE,
835 * only parsing to "nextcmd" is done, without reporting errors. Return
836 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
837 */
838 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839eval_to_string_skip(
840 char_u *arg,
841 char_u **nextcmd,
842 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 char_u *retval;
846
847 if (skip)
848 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 retval = NULL;
851 else
852 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100853 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000854 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 if (skip)
857 --emsg_skip;
858
859 return retval;
860}
861
862/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000863 * Skip over an expression at "*pp".
864 * Return FAIL for an error, OK otherwise.
865 */
866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100867skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000868{
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000870
871 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000873}
874
875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000877 * When "convert" is TRUE convert a List into a sequence of lines and convert
878 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * Return pointer to allocated memory, or NULL for failure.
880 */
881 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100882eval_to_string(
883 char_u *arg,
884 char_u **nextcmd,
885 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000889 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000890#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000891 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 retval = NULL;
896 else
897 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000898 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000899 {
900 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000901 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200902 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200903 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200904 if (tv.vval.v_list->lv_len > 0)
905 ga_append(&ga, NL);
906 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000907 ga_append(&ga, NUL);
908 retval = (char_u *)ga.ga_data;
909 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000910#ifdef FEAT_FLOAT
911 else if (convert && tv.v_type == VAR_FLOAT)
912 {
913 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
914 retval = vim_strsave(numbuf);
915 }
916#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000917 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100918 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000919 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921
922 return retval;
923}
924
925/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000926 * Call eval_to_string() without using current local variables and using
927 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 */
929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100930eval_to_string_safe(
931 char_u *arg,
932 char_u **nextcmd,
933 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
935 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200936 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200938 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000939 if (use_sandbox)
940 ++sandbox;
941 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000942 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000943 if (use_sandbox)
944 --sandbox;
945 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200946 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 return retval;
948}
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950/*
951 * Top level evaluation function, returning a number.
952 * Evaluates "expr" silently.
953 * Returns -1 for an error.
954 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200955 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100956eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
Bram Moolenaar33570922005-01-25 22:26:29 +0000958 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200959 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000960 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
962 ++emsg_off;
963
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = -1;
966 else
967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100968 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000969 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 --emsg_off;
972
973 return retval;
974}
975
Bram Moolenaara40058a2005-07-11 22:42:07 +0000976/*
977 * Prepare v: variable "idx" to be used.
978 * Save the current typeval in "save_tv".
979 * When not used yet add the variable to the v: hashtable.
980 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000983{
984 *save_tv = vimvars[idx].vv_tv;
985 if (vimvars[idx].vv_type == VAR_UNKNOWN)
986 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
987}
988
989/*
990 * Restore v: variable "idx" to typeval "save_tv".
991 * When no longer defined, remove the variable from the v: hashtable.
992 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100994restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000995{
996 hashitem_T *hi;
997
Bram Moolenaara40058a2005-07-11 22:42:07 +0000998 vimvars[idx].vv_tv = *save_tv;
999 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1000 {
1001 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1002 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001003 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001004 else
1005 hash_remove(&vimvarht, hi);
1006 }
1007}
1008
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001009#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001010/*
1011 * Evaluate an expression to a list with suggestions.
1012 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001013 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001014 */
1015 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001016eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001017{
1018 typval_T save_val;
1019 typval_T rettv;
1020 list_T *list = NULL;
1021 char_u *p = skipwhite(expr);
1022
1023 /* Set "v:val" to the bad word. */
1024 prepare_vimvar(VV_VAL, &save_val);
1025 vimvars[VV_VAL].vv_type = VAR_STRING;
1026 vimvars[VV_VAL].vv_str = badword;
1027 if (p_verbose == 0)
1028 ++emsg_off;
1029
1030 if (eval1(&p, &rettv, TRUE) == OK)
1031 {
1032 if (rettv.v_type != VAR_LIST)
1033 clear_tv(&rettv);
1034 else
1035 list = rettv.vval.v_list;
1036 }
1037
1038 if (p_verbose == 0)
1039 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001040 restore_vimvar(VV_VAL, &save_val);
1041
1042 return list;
1043}
1044
1045/*
1046 * "list" is supposed to contain two items: a word and a number. Return the
1047 * word in "pp" and the number as the return value.
1048 * Return -1 if anything isn't right.
1049 * Used to get the good word and score from the eval_spell_expr() result.
1050 */
1051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001052get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001053{
1054 listitem_T *li;
1055
1056 li = list->lv_first;
1057 if (li == NULL)
1058 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001059 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001060
1061 li = li->li_next;
1062 if (li == NULL)
1063 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001064 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001065}
1066#endif
1067
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001068/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001069 * Top level evaluation function.
1070 * Returns an allocated typval_T with the result.
1071 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001072 */
1073 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001075{
1076 typval_T *tv;
1077
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001078 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001079 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001080 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001081
1082 return tv;
1083}
1084
1085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001087 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001088 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1089 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001090 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001093call_vim_function(
1094 char_u *func,
1095 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001096 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001097 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001099 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001100 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001102 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001103 vim_memset(&funcexe, 0, sizeof(funcexe));
1104 funcexe.firstline = curwin->w_cursor.lnum;
1105 funcexe.lastline = curwin->w_cursor.lnum;
1106 funcexe.evaluate = TRUE;
1107 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001108 if (ret == FAIL)
1109 clear_tv(rettv);
1110
1111 return ret;
1112}
1113
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001115 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001116 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001117 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1118 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001119 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001120 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001121call_func_retnr(
1122 char_u *func,
1123 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001124 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001125{
1126 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001127 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001128
Bram Moolenaarded27a12018-08-01 19:06:03 +02001129 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001130 return -1;
1131
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001132 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001133 clear_tv(&rettv);
1134 return retval;
1135}
1136
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001137#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001138 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1139
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001140# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001142 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001143 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001144 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1145 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146 */
1147 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001148call_func_retstr(
1149 char_u *func,
1150 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001151 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152{
1153 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001154 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001155
Bram Moolenaarded27a12018-08-01 19:06:03 +02001156 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001157 return NULL;
1158
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001159 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001160 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 return retval;
1162}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001163# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001164
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001165/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001166 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001167 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1168 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001169 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001170 */
1171 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001172call_func_retlist(
1173 char_u *func,
1174 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001175 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001176{
1177 typval_T rettv;
1178
Bram Moolenaarded27a12018-08-01 19:06:03 +02001179 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001180 return NULL;
1181
1182 if (rettv.v_type != VAR_LIST)
1183 {
1184 clear_tv(&rettv);
1185 return NULL;
1186 }
1187
1188 return rettv.vval.v_list;
1189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#endif
1191
Bram Moolenaar05159a02005-02-26 23:04:13 +00001192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef FEAT_FOLDING
1194/*
1195 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1196 * it in "*cp". Doesn't give error messages.
1197 */
1198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001199eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaar33570922005-01-25 22:26:29 +00001201 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001202 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001204 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1205 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
1207 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208 if (use_sandbox)
1209 ++sandbox;
1210 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 retval = 0;
1214 else
1215 {
1216 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 if (tv.v_type == VAR_NUMBER)
1218 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001219 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 retval = 0;
1221 else
1222 {
1223 /* If the result is a string, check if there is a non-digit before
1224 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (!VIM_ISDIGIT(*s) && *s != '-')
1227 *cp = *s++;
1228 retval = atol((char *)s);
1229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
1232 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001233 if (use_sandbox)
1234 --sandbox;
1235 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001237 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238}
1239#endif
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001242 * Get a list of lines from a HERE document. The here document is a list of
1243 * lines surrounded by a marker.
1244 * cmd << {marker}
1245 * {line1}
1246 * {line2}
1247 * ....
1248 * {marker}
1249 *
1250 * The {marker} is a string. If the optional 'trim' word is supplied before the
1251 * marker, then the leading indentation before the lines (matching the
1252 * indentation in the 'cmd' line) is stripped.
1253 * Returns a List with {lines} or NULL.
1254 */
1255 static list_T *
1256heredoc_get(exarg_T *eap, char_u *cmd)
1257{
1258 char_u *theline;
1259 char_u *marker;
1260 list_T *l;
1261 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001262 int marker_indent_len = 0;
1263 int text_indent_len = 0;
1264 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001265
1266 if (eap->getline == NULL)
1267 {
1268 emsg(_("E991: cannot use =<< here"));
1269 return NULL;
1270 }
1271
1272 // Check for the optional 'trim' word before the marker
1273 cmd = skipwhite(cmd);
1274 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1275 {
1276 cmd = skipwhite(cmd + 4);
1277
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001278 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001279 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001280 // the first line after the :let command line. To find the end marker
1281 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001282 p = *eap->cmdlinep;
1283 while (VIM_ISWHITE(*p))
1284 {
1285 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001286 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001287 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001288 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001289 }
1290
Bram Moolenaar24582002019-07-21 14:14:26 +02001291 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001292 if (*cmd != NUL && *cmd != '"')
1293 {
1294 marker = skipwhite(cmd);
1295 p = skiptowhite(marker);
1296 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1297 {
1298 emsg(_(e_trailing));
1299 return NULL;
1300 }
1301 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001302 if (vim_islower(*marker))
1303 {
1304 emsg(_("E221: Marker cannot start with lower case letter"));
1305 return NULL;
1306 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001307 }
1308 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001309 {
1310 emsg(_("E172: Missing marker"));
1311 return NULL;
1312 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001313
1314 l = list_alloc();
1315 if (l == NULL)
1316 return NULL;
1317
1318 for (;;)
1319 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001320 int mi = 0;
1321 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001322
Bram Moolenaare96a2492019-06-25 04:12:16 +02001323 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001324 if (theline == NULL)
1325 {
1326 semsg(_("E990: Missing end marker '%s'"), marker);
1327 break;
1328 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001329
1330 // with "trim": skip the indent matching the :let line to find the
1331 // marker
1332 if (marker_indent_len > 0
1333 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1334 mi = marker_indent_len;
1335 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001336 {
1337 vim_free(theline);
1338 break;
1339 }
1340
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001341 if (text_indent_len == -1 && *theline != NUL)
1342 {
1343 // set the text indent from the first line.
1344 p = theline;
1345 text_indent_len = 0;
1346 while (VIM_ISWHITE(*p))
1347 {
1348 p++;
1349 text_indent_len++;
1350 }
1351 text_indent = vim_strnsave(theline, text_indent_len);
1352 }
1353 // with "trim": skip the indent matching the first line
1354 if (text_indent != NULL)
1355 for (ti = 0; ti < text_indent_len; ++ti)
1356 if (theline[ti] != text_indent[ti])
1357 break;
1358
1359 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001360 break;
1361 vim_free(theline);
1362 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001363 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001364
1365 return l;
1366}
1367
1368/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 * ":let" list all variable values
1370 * ":let var1 var2" list variable values
1371 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 * ":let var += expr" assignment command.
1373 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001374 * ":let var *= expr" assignment command.
1375 * ":let var /= expr" assignment command.
1376 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001378 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001382ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001384 ex_let_const(eap, FALSE);
1385}
1386
1387/*
1388 * ":const" list all variable values
1389 * ":const var1 var2" list variable values
1390 * ":const var = expr" assignment command.
1391 * ":const [var1, var2] = expr" unpack list.
1392 */
1393 void
1394ex_const(exarg_T *eap)
1395{
1396 ex_let_const(eap, TRUE);
1397}
1398
1399 static void
1400ex_let_const(exarg_T *eap, int is_const)
1401{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001404 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 int var_count = 0;
1407 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001409 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001410 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001411 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
Bram Moolenaardb552d602006-03-23 22:59:57 +00001413 argend = skip_var_list(arg, &var_count, &semicolon);
1414 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001416 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001417 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001418 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001419 concat = expr[0] == '.'
1420 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1421 || (expr[1] == '.' && expr[2] == '='));
1422 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1423 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001425 /*
1426 * ":let" without "=": list variables
1427 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001428 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001429 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001430 else if (expr[0] == '.')
1431 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001432 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001434 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001438 list_glob_vars(&first);
1439 list_buf_vars(&first);
1440 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001441 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001442 list_script_vars(&first);
1443 list_func_vars(&first);
1444 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 eap->nextcmd = check_nextcmd(arg);
1447 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001448 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1449 {
1450 list_T *l;
1451
1452 // HERE document
1453 l = heredoc_get(eap, expr + 3);
1454 if (l != NULL)
1455 {
1456 rettv_list_set(&rettv, l);
1457 op[0] = '=';
1458 op[1] = NUL;
1459 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001460 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001461 clear_tv(&rettv);
1462 }
1463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 else
1465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 op[0] = '=';
1467 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001468 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001469 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001471 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001472 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001473 if (expr[0] == '.' && expr[1] == '.') // ..=
1474 ++expr;
1475 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001476 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001478 else
1479 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (eap->skip)
1482 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (eap->skip)
1485 {
1486 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 --emsg_skip;
1489 }
1490 else if (i != FAIL)
1491 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001493 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496 }
1497}
1498
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499/*
1500 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1501 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001502 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1504 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505 * Returns OK or FAIL;
1506 */
1507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508ex_let_vars(
1509 char_u *arg_start,
1510 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001511 int copy, // copy values from "tv", don't move
1512 int semicolon, // from skip_var_list()
1513 int var_count, // from skip_var_list()
1514 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001515 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001516{
1517 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001518 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001519 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 listitem_T *item;
1521 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522
1523 if (*arg != '[')
1524 {
1525 /*
1526 * ":let var = expr" or ":for var in list"
1527 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001528 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 return FAIL;
1530 return OK;
1531 }
1532
1533 /*
1534 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1535 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001536 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001538 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 return FAIL;
1540 }
1541
1542 i = list_len(l);
1543 if (semicolon == 0 && var_count < i)
1544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546 return FAIL;
1547 }
1548 if (var_count - semicolon > i)
1549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001550 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001551 return FAIL;
1552 }
1553
1554 item = l->lv_first;
1555 while (*arg != ']')
1556 {
1557 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001558 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001559 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 item = item->li_next;
1561 if (arg == NULL)
1562 return FAIL;
1563
1564 arg = skipwhite(arg);
1565 if (*arg == ';')
1566 {
1567 /* Put the rest of the list (may be empty) in the var after ';'.
1568 * Create a new list for this. */
1569 l = list_alloc();
1570 if (l == NULL)
1571 return FAIL;
1572 while (item != NULL)
1573 {
1574 list_append_tv(l, &item->li_tv);
1575 item = item->li_next;
1576 }
1577
1578 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001579 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 ltv.vval.v_list = l;
1581 l->lv_refcount = 1;
1582
Bram Moolenaar9937a052019-06-15 15:45:06 +02001583 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001584 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 clear_tv(&ltv);
1586 if (arg == NULL)
1587 return FAIL;
1588 break;
1589 }
1590 else if (*arg != ',' && *arg != ']')
1591 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001592 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 return FAIL;
1594 }
1595 }
1596
1597 return OK;
1598}
1599
1600/*
1601 * Skip over assignable variable "var" or list of variables "[var, var]".
1602 * Used for ":let varvar = expr" and ":for varvar in expr".
1603 * For "[var, var]" increment "*var_count" for each variable.
1604 * for "[var, var; var]" set "semicolon".
1605 * Return NULL for an error.
1606 */
1607 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608skip_var_list(
1609 char_u *arg,
1610 int *var_count,
1611 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612{
1613 char_u *p, *s;
1614
1615 if (*arg == '[')
1616 {
1617 /* "[var, var]": find the matching ']'. */
1618 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 {
1621 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1622 s = skip_var_one(p);
1623 if (s == p)
1624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001625 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626 return NULL;
1627 }
1628 ++*var_count;
1629
1630 p = skipwhite(s);
1631 if (*p == ']')
1632 break;
1633 else if (*p == ';')
1634 {
1635 if (*semicolon == 1)
1636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001637 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 return NULL;
1639 }
1640 *semicolon = 1;
1641 }
1642 else if (*p != ',')
1643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001644 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 return NULL;
1646 }
1647 }
1648 return p + 1;
1649 }
1650 else
1651 return skip_var_one(arg);
1652}
1653
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001654/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001655 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001656 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001657 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001661 if (*arg == '@' && arg[1] != NUL)
1662 return arg + 2;
1663 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1664 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665}
1666
Bram Moolenaara7043832005-01-21 11:56:39 +00001667/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 * List variables for hashtab "ht" with prefix "prefix".
1669 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001670 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672list_hashtable_vars(
1673 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001674 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675 int empty,
1676 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001677{
Bram Moolenaar33570922005-01-25 22:26:29 +00001678 hashitem_T *hi;
1679 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001680 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001681 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001682
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001683 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001684 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1685 {
1686 if (!HASHITEM_EMPTY(hi))
1687 {
1688 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001689 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001690
1691 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001692 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1693 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001694 if (message_filtered(buf))
1695 continue;
1696
Bram Moolenaar33570922005-01-25 22:26:29 +00001697 if (empty || di->di_tv.v_type != VAR_STRING
1698 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001699 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001700 }
1701 }
1702}
1703
1704/*
1705 * List global variables.
1706 */
1707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001709{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001710 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001711}
1712
1713/*
1714 * List buffer variables.
1715 */
1716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001717list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001718{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001719 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001720}
1721
1722/*
1723 * List window variables.
1724 */
1725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001726list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001727{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001728 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001729}
1730
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001731/*
1732 * List tab page variables.
1733 */
1734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001736{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001737 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001738}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001739
Bram Moolenaara7043832005-01-21 11:56:39 +00001740/*
1741 * List Vim variables.
1742 */
1743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001744list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001746 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747}
1748
1749/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001750 * List script-local variables, if there is a script.
1751 */
1752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001754{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001755 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1756 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001757 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001758}
1759
1760/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 * List variables in "arg".
1762 */
1763 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765{
1766 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001767 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001769 char_u *name_start;
1770 char_u *arg_subsc;
1771 char_u *tofree;
1772 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773
1774 while (!ends_excmd(*arg) && !got_int)
1775 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001776 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001778 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001779 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 {
1781 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001782 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001783 break;
1784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001788 /* get_name_len() takes care of expanding curly braces */
1789 name_start = name = arg;
1790 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1791 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001793 /* This is mainly to keep test 49 working: when expanding
1794 * curly braces fails overrule the exception error message. */
1795 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001797 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001798 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001799 break;
1800 }
1801 error = TRUE;
1802 }
1803 else
1804 {
1805 if (tofree != NULL)
1806 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001807 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 else
1810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001811 /* handle d.key, l[idx], f(expr) */
1812 arg_subsc = arg;
1813 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001814 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001816 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001817 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001819 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001820 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001821 case 'g': list_glob_vars(first); break;
1822 case 'b': list_buf_vars(first); break;
1823 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001824 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001825 case 'v': list_vim_vars(first); break;
1826 case 's': list_script_vars(first); break;
1827 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001828 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001829 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001830 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001831 }
1832 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001833 {
1834 char_u numbuf[NUMBUFLEN];
1835 char_u *tf;
1836 int c;
1837 char_u *s;
1838
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001839 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001840 c = *arg;
1841 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001842 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 tv.v_type,
1845 s == NULL ? (char_u *)"" : s,
1846 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847 *arg = c;
1848 vim_free(tf);
1849 }
1850 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 }
1853 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001854
1855 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001857
1858 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 }
1860
1861 return arg;
1862}
1863
1864/*
1865 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1866 * Returns a pointer to the char just after the var name.
1867 * Returns NULL if there is an error.
1868 */
1869 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001870ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001871 char_u *arg, // points to variable name
1872 typval_T *tv, // value to assign to variable
1873 int copy, // copy value from "tv"
1874 int is_const, // lock variable for const
1875 char_u *endchars, // valid chars after variable name or NULL
1876 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877{
1878 int c1;
1879 char_u *name;
1880 char_u *p;
1881 char_u *arg_end = NULL;
1882 int len;
1883 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885
1886 /*
1887 * ":let $VAR = expr": Set environment variable.
1888 */
1889 if (*arg == '$')
1890 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001891 if (is_const)
1892 {
1893 emsg(_("E996: Cannot lock an environment variable"));
1894 return NULL;
1895 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* Find the end of the name. */
1897 ++arg;
1898 name = arg;
1899 len = get_env_len(&arg);
1900 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001901 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 else
1903 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001904 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001908 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001909 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 {
1911 c1 = name[len];
1912 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001913 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001914 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 {
1916 int mustfree = FALSE;
1917 char_u *s = vim_getenv(name, &mustfree);
1918
1919 if (s != NULL)
1920 {
1921 p = tofree = concat_str(s, p);
1922 if (mustfree)
1923 vim_free(s);
1924 }
1925 }
1926 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001927 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001929 if (STRICMP(name, "HOME") == 0)
1930 init_homedir();
1931 else if (didset_vim && STRICMP(name, "VIM") == 0)
1932 didset_vim = FALSE;
1933 else if (didset_vimruntime
1934 && STRICMP(name, "VIMRUNTIME") == 0)
1935 didset_vimruntime = FALSE;
1936 arg_end = arg;
1937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 }
1941 }
1942 }
1943
1944 /*
1945 * ":let &option = expr": Set option value.
1946 * ":let &l:option = expr": Set local option value.
1947 * ":let &g:option = expr": Set global option value.
1948 */
1949 else if (*arg == '&')
1950 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001951 if (is_const)
1952 {
1953 emsg(_("E996: Cannot lock an option"));
1954 return NULL;
1955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 /* Find the end of the name. */
1957 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 if (p == NULL || (endchars != NULL
1959 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001960 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 else
1962 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 long n;
1964 int opt_type;
1965 long numval;
1966 char_u *stringval = NULL;
1967 char_u *s;
1968
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 c1 = *p;
1970 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001972 n = (long)tv_get_number(tv);
1973 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001974 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 {
1976 opt_type = get_option_value(arg, &numval,
1977 &stringval, opt_flags);
1978 if ((opt_type == 1 && *op == '.')
1979 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001981 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001982 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 else
1985 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001986 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001988 switch (*op)
1989 {
1990 case '+': n = numval + n; break;
1991 case '-': n = numval - n; break;
1992 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001993 case '/': n = (long)num_divide(numval, n); break;
1994 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001997 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 {
1999 s = concat_str(stringval, s);
2000 vim_free(stringval);
2001 stringval = s;
2002 }
2003 }
2004 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002005 if (s != NULL)
2006 {
2007 set_option_value(arg, n, s, opt_flags);
2008 arg_end = p;
2009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 }
2013 }
2014
2015 /*
2016 * ":let @r = expr": Set register contents.
2017 */
2018 else if (*arg == '@')
2019 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002020 if (is_const)
2021 {
2022 emsg(_("E996: Cannot lock a register"));
2023 return NULL;
2024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002026 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002027 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002030 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 else
2032 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002033 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034 char_u *s;
2035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002036 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002037 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002039 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 if (s != NULL)
2041 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002042 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 vim_free(s);
2044 }
2045 }
2046 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002049 arg_end = arg + 1;
2050 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002051 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 }
2053 }
2054
2055 /*
2056 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002057 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002059 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002061 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002063 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002067 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 else
2069 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002070 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 arg_end = p;
2072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 }
2076
2077 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002078 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079
2080 return arg_end;
2081}
2082
2083/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 * Get an lval: variable, Dict item or List item that can be assigned a value
2085 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2086 * "name.key", "name.key[expr]" etc.
2087 * Indexing only works if "name" is an existing List or Dictionary.
2088 * "name" points to the start of the name.
2089 * If "rettv" is not NULL it points to the value to be assigned.
2090 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2091 * wrong; must end in space or cmd separator.
2092 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002093 * flags:
2094 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002095 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002096 * GLV_NO_AUTOLOAD: do not use script autoloading
2097 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002102 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002103get_lval(
2104 char_u *name,
2105 typval_T *rettv,
2106 lval_T *lp,
2107 int unlet,
2108 int skip,
2109 int flags, /* GLV_ values */
2110 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002113 char_u *expr_start, *expr_end;
2114 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 dictitem_T *v;
2116 typval_T var1;
2117 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002119 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002121 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002122 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002123 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127
2128 if (skip)
2129 {
2130 /* When skipping just find the end of the name. */
2131 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002132 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002133 }
2134
2135 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002136 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 if (expr_start != NULL)
2138 {
2139 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002140 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 && *p != '[' && *p != '.')
2142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002143 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 return NULL;
2145 }
2146
2147 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2148 if (lp->ll_exp_name == NULL)
2149 {
2150 /* Report an invalid expression in braces, unless the
2151 * expression evaluation has been cancelled due to an
2152 * aborting error, an interrupt, or an exception. */
2153 if (!aborting() && !quiet)
2154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002155 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 return NULL;
2158 }
2159 }
2160 lp->ll_name = lp->ll_exp_name;
2161 }
2162 else
2163 lp->ll_name = name;
2164
2165 /* Without [idx] or .key we are done. */
2166 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2167 return p;
2168
2169 cc = *p;
2170 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002171 /* Only pass &ht when we would write to the variable, it prevents autoload
2172 * as well. */
2173 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2174 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002176 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 if (v == NULL)
2179 return NULL;
2180
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 /*
2182 * Loop until no more [idx] or .key is following.
2183 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002185 var1.v_type = VAR_UNKNOWN;
2186 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2190 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002191 && lp->ll_tv->vval.v_dict != NULL)
2192 && !(lp->ll_tv->v_type == VAR_BLOB
2193 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002205
Bram Moolenaar8c711452005-01-14 21:53:12 +00002206 len = -1;
2207 if (*p == '.')
2208 {
2209 key = p + 1;
2210 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2211 ;
2212 if (len == 0)
2213 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002215 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 }
2218 p = key + len;
2219 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002220 else
2221 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002222 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002223 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 if (*p == ':')
2225 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002226 else
2227 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002228 empty1 = FALSE;
2229 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002231 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 {
2233 /* not a number or string */
2234 clear_tv(&var1);
2235 return NULL;
2236 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002237 }
2238
2239 /* Optionally get the second index [ :expr]. */
2240 if (*p == ':')
2241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002245 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002246 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002248 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002249 if (rettv != NULL
2250 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002251 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002252 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002253 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002256 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002257 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002259 }
2260 p = skipwhite(p + 1);
2261 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002263 else
2264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002266 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2267 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002268 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002270 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002271 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 {
2273 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002274 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 clear_tv(&var2);
2276 return NULL;
2277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002281 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002283
Bram Moolenaar8c711452005-01-14 21:53:12 +00002284 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002285 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002288 clear_tv(&var1);
2289 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002291 }
2292
2293 /* Skip to past ']'. */
2294 ++p;
2295 }
2296
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002298 {
2299 if (len == -1)
2300 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002302 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002303 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002304 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002305 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002307 }
2308 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 lp->ll_list = NULL;
2310 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002311 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002312
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002313 /* When assigning to a scope dictionary check that a function and
2314 * variable name is valid (only variable name unless it is l: or
2315 * g: dictionary). Disallow overwriting a builtin function. */
2316 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002317 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002318 int prevval;
2319 int wrong;
2320
2321 if (len != -1)
2322 {
2323 prevval = key[len];
2324 key[len] = NUL;
2325 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002326 else
2327 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002328 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2329 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002330 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002331 || !valid_varname(key);
2332 if (len != -1)
2333 key[len] = prevval;
2334 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002335 return NULL;
2336 }
2337
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002339 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002340 // Can't add "v:" or "a:" variable.
2341 if (lp->ll_dict == &vimvardict
2342 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002344 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002345 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002346 return NULL;
2347 }
2348
Bram Moolenaar31b81602019-02-10 22:14:27 +01002349 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002354 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 }
2357 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002361 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 p = NULL;
2364 break;
2365 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002366 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002367 else if ((flags & GLV_READ_ONLY) == 0
2368 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002369 {
2370 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002371 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002372 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002373
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002374 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002377 else if (lp->ll_tv->v_type == VAR_BLOB)
2378 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002379 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2380
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002381 /*
2382 * Get the number and item for the only or first index of the List.
2383 */
2384 if (empty1)
2385 lp->ll_n1 = 0;
2386 else
2387 // is number or string
2388 lp->ll_n1 = (long)tv_get_number(&var1);
2389 clear_tv(&var1);
2390
2391 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002392 || lp->ll_n1 > bloblen
2393 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002394 {
2395 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002396 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002397 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002398 return NULL;
2399 }
2400 if (lp->ll_range && !lp->ll_empty2)
2401 {
2402 lp->ll_n2 = (long)tv_get_number(&var2);
2403 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002404 if (lp->ll_n2 < 0
2405 || lp->ll_n2 >= bloblen
2406 || lp->ll_n2 < lp->ll_n1)
2407 {
2408 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002409 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002410 return NULL;
2411 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002412 }
2413 lp->ll_blob = lp->ll_tv->vval.v_blob;
2414 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002415 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 else
2418 {
2419 /*
2420 * Get the number and item for the only or first index of the List.
2421 */
2422 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002425 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002426 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002427 clear_tv(&var1);
2428
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 lp->ll_list = lp->ll_tv->vval.v_list;
2431 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2432 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002434 if (lp->ll_n1 < 0)
2435 {
2436 lp->ll_n1 = 0;
2437 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2438 }
2439 }
2440 if (lp->ll_li == NULL)
2441 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002442 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002443 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002444 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 }
2447
2448 /*
2449 * May need to find the item or absolute index for the second
2450 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 * When no index given: "lp->ll_empty2" is TRUE.
2452 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002456 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002457 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002463 {
2464 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002465 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002467 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2472 if (lp->ll_n1 < 0)
2473 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2474 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002475 {
2476 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002477 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002479 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002480 }
2481
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002486 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 return p;
2488}
2489
2490/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002491 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495{
2496 vim_free(lp->ll_exp_name);
2497 vim_free(lp->ll_newkey);
2498}
2499
2500/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002501 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002503 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2504 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 */
2506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002507set_var_lval(
2508 lval_T *lp,
2509 char_u *endp,
2510 typval_T *rettv,
2511 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002512 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002513 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514{
2515 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002516 listitem_T *ri;
2517 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518
2519 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002521 cc = *endp;
2522 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002523 if (lp->ll_blob != NULL)
2524 {
2525 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002526
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002527 if (op != NULL && *op != '=')
2528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002529 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002530 return;
2531 }
2532
2533 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2534 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002535 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002536
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002537 if (lp->ll_empty2)
2538 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2539
2540 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002542 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002543 return;
2544 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002545 if (lp->ll_empty2)
2546 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002547
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002548 ir = 0;
2549 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2550 blob_set(lp->ll_blob, il,
2551 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002552 }
2553 else
2554 {
2555 val = (int)tv_get_number_chk(rettv, &error);
2556 if (!error)
2557 {
2558 garray_T *gap = &lp->ll_blob->bv_ga;
2559
2560 // Allow for appending a byte. Setting a byte beyond
2561 // the end is an error otherwise.
2562 if (lp->ll_n1 < gap->ga_len
2563 || (lp->ll_n1 == gap->ga_len
2564 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2565 {
2566 blob_set(lp->ll_blob, lp->ll_n1, val);
2567 if (lp->ll_n1 == gap->ga_len)
2568 ++gap->ga_len;
2569 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002570 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002571 }
2572 }
2573 }
2574 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002576 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577
Bram Moolenaar9937a052019-06-15 15:45:06 +02002578 if (is_const)
2579 {
2580 emsg(_(e_cannot_mod));
2581 *endp = cc;
2582 return;
2583 }
2584
Bram Moolenaarff697e62019-02-12 22:28:33 +01002585 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002586 di = NULL;
2587 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2588 &tv, &di, TRUE, FALSE) == OK)
2589 {
2590 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002591 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2592 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002593 && tv_op(&tv, rettv, op) == OK)
2594 set_var(lp->ll_name, &tv, FALSE);
2595 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002598 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002599 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002600 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002602 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002603 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002604 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002605 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 else if (lp->ll_range)
2607 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002608 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002609 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002610
Bram Moolenaar9937a052019-06-15 15:45:06 +02002611 if (is_const)
2612 {
2613 emsg(_("E996: Cannot lock a range"));
2614 return;
2615 }
2616
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002617 /*
2618 * Check whether any of the list items is locked
2619 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002620 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002621 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002622 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002623 return;
2624 ri = ri->li_next;
2625 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2626 break;
2627 ll_li = ll_li->li_next;
2628 ++ll_n1;
2629 }
2630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 /*
2632 * Assign the List values to the list items.
2633 */
2634 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636 if (op != NULL && *op != '=')
2637 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2638 else
2639 {
2640 clear_tv(&lp->ll_li->li_tv);
2641 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2642 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 ri = ri->li_next;
2644 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2645 break;
2646 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002649 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 ri = NULL;
2652 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 lp->ll_li = lp->ll_li->li_next;
2656 ++lp->ll_n1;
2657 }
2658 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002659 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 else if (lp->ll_empty2
2661 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002663 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 }
2665 else
2666 {
2667 /*
2668 * Assign to a List or Dictionary item.
2669 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002670 if (is_const)
2671 {
2672 emsg(_("E996: Cannot lock a list or dict"));
2673 return;
2674 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 if (lp->ll_newkey != NULL)
2676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 if (op != NULL && *op != '=')
2678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 return;
2681 }
2682
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002684 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (di == NULL)
2686 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002687 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2688 {
2689 vim_free(di);
2690 return;
2691 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002694 else if (op != NULL && *op != '=')
2695 {
2696 tv_op(lp->ll_tv, rettv, op);
2697 return;
2698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 /*
2703 * Assign the value to the variable or list item.
2704 */
2705 if (copy)
2706 copy_tv(rettv, lp->ll_tv);
2707 else
2708 {
2709 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002710 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002712 }
2713 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714}
2715
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002716/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002717 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2718 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 * Returns OK or FAIL.
2720 */
2721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002722tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002724 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 char_u numbuf[NUMBUFLEN];
2726 char_u *s;
2727
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002728 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2729 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2730 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002731 {
2732 switch (tv1->v_type)
2733 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002734 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 case VAR_DICT:
2736 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002737 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002738 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002739 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002740 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002741 break;
2742
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002743 case VAR_BLOB:
2744 if (*op != '+' || tv2->v_type != VAR_BLOB)
2745 break;
2746 // BLOB += BLOB
2747 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2748 {
2749 blob_T *b1 = tv1->vval.v_blob;
2750 blob_T *b2 = tv2->vval.v_blob;
2751 int i, len = blob_len(b2);
2752 for (i = 0; i < len; i++)
2753 ga_append(&b1->bv_ga, blob_get(b2, i));
2754 }
2755 return OK;
2756
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002757 case VAR_LIST:
2758 if (*op != '+' || tv2->v_type != VAR_LIST)
2759 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002760 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002761 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2762 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2763 return OK;
2764
2765 case VAR_NUMBER:
2766 case VAR_STRING:
2767 if (tv2->v_type == VAR_LIST)
2768 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002769 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002771 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002772 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002773#ifdef FEAT_FLOAT
2774 if (tv2->v_type == VAR_FLOAT)
2775 {
2776 float_T f = n;
2777
Bram Moolenaarff697e62019-02-12 22:28:33 +01002778 if (*op == '%')
2779 break;
2780 switch (*op)
2781 {
2782 case '+': f += tv2->vval.v_float; break;
2783 case '-': f -= tv2->vval.v_float; break;
2784 case '*': f *= tv2->vval.v_float; break;
2785 case '/': f /= tv2->vval.v_float; break;
2786 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002787 clear_tv(tv1);
2788 tv1->v_type = VAR_FLOAT;
2789 tv1->vval.v_float = f;
2790 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002791 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002792#endif
2793 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002794 switch (*op)
2795 {
2796 case '+': n += tv_get_number(tv2); break;
2797 case '-': n -= tv_get_number(tv2); break;
2798 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002799 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2800 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002801 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002802 clear_tv(tv1);
2803 tv1->v_type = VAR_NUMBER;
2804 tv1->vval.v_number = n;
2805 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002806 }
2807 else
2808 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002809 if (tv2->v_type == VAR_FLOAT)
2810 break;
2811
Bram Moolenaarff697e62019-02-12 22:28:33 +01002812 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002813 s = tv_get_string(tv1);
2814 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002815 clear_tv(tv1);
2816 tv1->v_type = VAR_STRING;
2817 tv1->vval.v_string = s;
2818 }
2819 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002820
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002821 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002822#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002823 {
2824 float_T f;
2825
Bram Moolenaarff697e62019-02-12 22:28:33 +01002826 if (*op == '%' || *op == '.'
2827 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002828 && tv2->v_type != VAR_NUMBER
2829 && tv2->v_type != VAR_STRING))
2830 break;
2831 if (tv2->v_type == VAR_FLOAT)
2832 f = tv2->vval.v_float;
2833 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002834 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002835 switch (*op)
2836 {
2837 case '+': tv1->vval.v_float += f; break;
2838 case '-': tv1->vval.v_float -= f; break;
2839 case '*': tv1->vval.v_float *= f; break;
2840 case '/': tv1->vval.v_float /= f; break;
2841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002843#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002844 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 }
2846 }
2847
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002848 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 return FAIL;
2850}
2851
2852/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 * Evaluate the expression used in a ":for var in expr" command.
2854 * "arg" points to "var".
2855 * Set "*errp" to TRUE for an error, FALSE otherwise;
2856 * Return a pointer that holds the info. Null when there is an error.
2857 */
2858 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002859eval_for_line(
2860 char_u *arg,
2861 int *errp,
2862 char_u **nextcmdp,
2863 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864{
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002866 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T tv;
2868 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869
2870 *errp = TRUE; /* default: there is an error */
2871
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002872 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002873 if (fi == NULL)
2874 return NULL;
2875
2876 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2877 if (expr == NULL)
2878 return fi;
2879
2880 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002881 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002883 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884 return fi;
2885 }
2886
2887 if (skip)
2888 ++emsg_skip;
2889 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2890 {
2891 *errp = FALSE;
2892 if (!skip)
2893 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002894 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002895 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002896 l = tv.vval.v_list;
2897 if (l == NULL)
2898 {
2899 // a null list is like an empty list: do nothing
2900 clear_tv(&tv);
2901 }
2902 else
2903 {
2904 // No need to increment the refcount, it's already set for
2905 // the list being used in "tv".
2906 fi->fi_list = l;
2907 list_add_watch(l, &fi->fi_lw);
2908 fi->fi_lw.lw_item = l->lv_first;
2909 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002910 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002911 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002912 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002913 fi->fi_bi = 0;
2914 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002915 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002916 typval_T btv;
2917
2918 // Make a copy, so that the iteration still works when the
2919 // blob is changed.
2920 blob_copy(&tv, &btv);
2921 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002922 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002923 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002924 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002925 else
2926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002927 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002928 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929 }
2930 }
2931 }
2932 if (skip)
2933 --emsg_skip;
2934
2935 return fi;
2936}
2937
2938/*
2939 * Use the first item in a ":for" list. Advance to the next.
2940 * Assign the values to the variable (list). "arg" points to the first one.
2941 * Return TRUE when a valid item was found, FALSE when at end of list or
2942 * something wrong.
2943 */
2944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002947 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002949 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002950
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 if (fi->fi_blob != NULL)
2952 {
2953 typval_T tv;
2954
2955 if (fi->fi_bi >= blob_len(fi->fi_blob))
2956 return FALSE;
2957 tv.v_type = VAR_NUMBER;
2958 tv.v_lock = VAR_FIXED;
2959 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2960 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002961 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2962 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002963 }
2964
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002965 item = fi->fi_lw.lw_item;
2966 if (item == NULL)
2967 result = FALSE;
2968 else
2969 {
2970 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002971 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2972 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002973 }
2974 return result;
2975}
2976
2977/*
2978 * Free the structure used to store info used by ":for".
2979 */
2980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002981free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982{
Bram Moolenaar33570922005-01-25 22:26:29 +00002983 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002985 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002986 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002988 list_unref(fi->fi_list);
2989 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002990 if (fi != NULL && fi->fi_blob != NULL)
2991 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 vim_free(fi);
2993}
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2996
2997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002998set_context_for_expression(
2999 expand_T *xp,
3000 char_u *arg,
3001 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 int got_eq = FALSE;
3004 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 if (cmdidx == CMD_let)
3008 {
3009 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003010 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 {
3012 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003013 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003014 {
3015 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003016 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003017 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018 break;
3019 }
3020 return;
3021 }
3022 }
3023 else
3024 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3025 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 while ((xp->xp_pattern = vim_strpbrk(arg,
3027 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3028 {
3029 c = *xp->xp_pattern;
3030 if (c == '&')
3031 {
3032 c = xp->xp_pattern[1];
3033 if (c == '&')
3034 {
3035 ++xp->xp_pattern;
3036 xp->xp_context = cmdidx != CMD_let || got_eq
3037 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3038 }
3039 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003042 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3043 xp->xp_pattern += 2;
3044
3045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047 else if (c == '$')
3048 {
3049 /* environment variable */
3050 xp->xp_context = EXPAND_ENV_VARS;
3051 }
3052 else if (c == '=')
3053 {
3054 got_eq = TRUE;
3055 xp->xp_context = EXPAND_EXPRESSION;
3056 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003057 else if (c == '#'
3058 && xp->xp_context == EXPAND_EXPRESSION)
3059 {
3060 /* Autoload function/variable contains '#'. */
3061 break;
3062 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003063 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 && xp->xp_context == EXPAND_FUNCTIONS
3065 && vim_strchr(xp->xp_pattern, '(') == NULL)
3066 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003067 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 break;
3069 }
3070 else if (cmdidx != CMD_let || got_eq)
3071 {
3072 if (c == '"') /* string */
3073 {
3074 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3075 if (c == '\\' && xp->xp_pattern[1] != NUL)
3076 ++xp->xp_pattern;
3077 xp->xp_context = EXPAND_NOTHING;
3078 }
3079 else if (c == '\'') /* literal string */
3080 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003081 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3083 /* skip */ ;
3084 xp->xp_context = EXPAND_NOTHING;
3085 }
3086 else if (c == '|')
3087 {
3088 if (xp->xp_pattern[1] == '|')
3089 {
3090 ++xp->xp_pattern;
3091 xp->xp_context = EXPAND_EXPRESSION;
3092 }
3093 else
3094 xp->xp_context = EXPAND_COMMANDS;
3095 }
3096 else
3097 xp->xp_context = EXPAND_EXPRESSION;
3098 }
3099 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003100 /* Doesn't look like something valid, expand as an expression
3101 * anyway. */
3102 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 arg = xp->xp_pattern;
3104 if (*arg != NUL)
3105 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3106 /* skip */ ;
3107 }
3108 xp->xp_pattern = arg;
3109}
3110
3111#endif /* FEAT_CMDL_COMPL */
3112
3113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 * ":unlet[!] var1 ... " command.
3115 */
3116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003119 ex_unletlock(eap, eap->arg, 0);
3120}
3121
3122/*
3123 * ":lockvar" and ":unlockvar" commands
3124 */
3125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003126ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003127{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003129 int deep = 2;
3130
3131 if (eap->forceit)
3132 deep = -1;
3133 else if (vim_isdigit(*arg))
3134 {
3135 deep = getdigits(&arg);
3136 arg = skipwhite(arg);
3137 }
3138
3139 ex_unletlock(eap, arg, deep);
3140}
3141
3142/*
3143 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3144 */
3145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003146ex_unletlock(
3147 exarg_T *eap,
3148 char_u *argstart,
3149 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003150{
3151 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003154 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 do
3157 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003158 if (*arg == '$')
3159 {
3160 char_u *name = ++arg;
3161
3162 if (get_env_len(&arg) == 0)
3163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003164 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003165 return;
3166 }
3167 vim_unsetenv(name);
3168 arg = skipwhite(arg);
3169 continue;
3170 }
3171
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003173 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003174 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003175 if (lv.ll_name == NULL)
3176 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003177 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003178 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 if (name_end != NULL)
3181 {
3182 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003183 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003184 }
3185 if (!(eap->skip || error))
3186 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 break;
3188 }
3189
3190 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003191 {
3192 if (eap->cmdidx == CMD_unlet)
3193 {
3194 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3195 error = TRUE;
3196 }
3197 else
3198 {
3199 if (do_lock_var(&lv, name_end, deep,
3200 eap->cmdidx == CMD_lockvar) == FAIL)
3201 error = TRUE;
3202 }
3203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 if (!eap->skip)
3206 clear_lval(&lv);
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 arg = skipwhite(name_end);
3209 } while (!ends_excmd(*arg));
3210
3211 eap->nextcmd = check_nextcmd(arg);
3212}
3213
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003215do_unlet_var(
3216 lval_T *lp,
3217 char_u *name_end,
3218 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 int ret = OK;
3221 int cc;
3222
3223 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 cc = *name_end;
3226 *name_end = NUL;
3227
3228 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003229 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003230 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003233 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003234 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003235 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003236 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003237 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 else if (lp->ll_range)
3239 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003241 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003242 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003243
3244 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3245 {
3246 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003247 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003248 return FAIL;
3249 ll_li = li;
3250 ++ll_n1;
3251 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252
3253 /* Delete a range of List items. */
3254 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3255 {
3256 li = lp->ll_li->li_next;
3257 listitem_remove(lp->ll_list, lp->ll_li);
3258 lp->ll_li = li;
3259 ++lp->ll_n1;
3260 }
3261 }
3262 else
3263 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 /* unlet a List item. */
3266 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003267 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003269 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003270 }
3271
3272 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275/*
3276 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 */
3279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 hashtab_T *ht;
3283 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003284 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003285 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003286 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 ht = find_var_ht(name, &varname);
3289 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003292 if (d == NULL)
3293 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294 if (ht == &globvarht)
3295 d = &globvardict;
3296 else if (ht == &compat_hashtab)
3297 d = &vimvardict;
3298 else
3299 {
3300 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3301 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3302 }
3303 if (d == NULL)
3304 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003305 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 return FAIL;
3307 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003308 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003310 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003311 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003312 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003313 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003314 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003315 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003316 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003317 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003318 return FAIL;
3319
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003320 delete_var(ht, hi);
3321 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003324 if (forceit)
3325 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003326 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return FAIL;
3328}
3329
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003330/*
3331 * Lock or unlock variable indicated by "lp".
3332 * "deep" is the levels to go (-1 for unlimited);
3333 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3334 */
3335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336do_lock_var(
3337 lval_T *lp,
3338 char_u *name_end,
3339 int deep,
3340 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003341{
3342 int ret = OK;
3343 int cc;
3344 dictitem_T *di;
3345
3346 if (deep == 0) /* nothing to do */
3347 return OK;
3348
3349 if (lp->ll_tv == NULL)
3350 {
3351 cc = *name_end;
3352 *name_end = NUL;
3353
3354 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003355 di = find_var(lp->ll_name, NULL, TRUE);
3356 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003357 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003358 else if ((di->di_flags & DI_FLAGS_FIX)
3359 && di->di_tv.v_type != VAR_DICT
3360 && di->di_tv.v_type != VAR_LIST)
3361 /* For historic reasons this error is not given for a list or dict.
3362 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003363 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003364 else
3365 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003366 if (lock)
3367 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003368 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003369 di->di_flags &= ~DI_FLAGS_LOCK;
3370 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003371 }
3372 *name_end = cc;
3373 }
3374 else if (lp->ll_range)
3375 {
3376 listitem_T *li = lp->ll_li;
3377
3378 /* (un)lock a range of List items. */
3379 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3380 {
3381 item_lock(&li->li_tv, deep, lock);
3382 li = li->li_next;
3383 ++lp->ll_n1;
3384 }
3385 }
3386 else if (lp->ll_list != NULL)
3387 /* (un)lock a List item. */
3388 item_lock(&lp->ll_li->li_tv, deep, lock);
3389 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003390 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003391 item_lock(&lp->ll_di->di_tv, deep, lock);
3392
3393 return ret;
3394}
3395
3396/*
3397 * Lock or unlock an item. "deep" is nr of levels to go.
3398 */
3399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003400item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401{
3402 static int recurse = 0;
3403 list_T *l;
3404 listitem_T *li;
3405 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003406 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407 hashitem_T *hi;
3408 int todo;
3409
3410 if (recurse >= DICT_MAXNEST)
3411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003412 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003413 return;
3414 }
3415 if (deep == 0)
3416 return;
3417 ++recurse;
3418
3419 /* lock/unlock the item itself */
3420 if (lock)
3421 tv->v_lock |= VAR_LOCKED;
3422 else
3423 tv->v_lock &= ~VAR_LOCKED;
3424
3425 switch (tv->v_type)
3426 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003427 case VAR_UNKNOWN:
3428 case VAR_NUMBER:
3429 case VAR_STRING:
3430 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003431 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003432 case VAR_FLOAT:
3433 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003434 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003435 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003436 break;
3437
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003438 case VAR_BLOB:
3439 if ((b = tv->vval.v_blob) != NULL)
3440 {
3441 if (lock)
3442 b->bv_lock |= VAR_LOCKED;
3443 else
3444 b->bv_lock &= ~VAR_LOCKED;
3445 }
3446 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003447 case VAR_LIST:
3448 if ((l = tv->vval.v_list) != NULL)
3449 {
3450 if (lock)
3451 l->lv_lock |= VAR_LOCKED;
3452 else
3453 l->lv_lock &= ~VAR_LOCKED;
3454 if (deep < 0 || deep > 1)
3455 /* recursive: lock/unlock the items the List contains */
3456 for (li = l->lv_first; li != NULL; li = li->li_next)
3457 item_lock(&li->li_tv, deep - 1, lock);
3458 }
3459 break;
3460 case VAR_DICT:
3461 if ((d = tv->vval.v_dict) != NULL)
3462 {
3463 if (lock)
3464 d->dv_lock |= VAR_LOCKED;
3465 else
3466 d->dv_lock &= ~VAR_LOCKED;
3467 if (deep < 0 || deep > 1)
3468 {
3469 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003470 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003471 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3472 {
3473 if (!HASHITEM_EMPTY(hi))
3474 {
3475 --todo;
3476 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3477 }
3478 }
3479 }
3480 }
3481 }
3482 --recurse;
3483}
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3486/*
3487 * Delete all "menutrans_" variables.
3488 */
3489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003490del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
Bram Moolenaar33570922005-01-25 22:26:29 +00003492 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003493 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaar33570922005-01-25 22:26:29 +00003495 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003498 {
3499 if (!HASHITEM_EMPTY(hi))
3500 {
3501 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3503 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 }
3505 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003506 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507}
3508#endif
3509
3510#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3511
3512/*
3513 * Local string buffer for the next two functions to store a variable name
3514 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3515 * get_user_var_name().
3516 */
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518static char_u *varnamebuf = NULL;
3519static int varnamebuflen = 0;
3520
3521/*
3522 * Function to concatenate a prefix and a variable name.
3523 */
3524 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003525cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 int len;
3528
3529 len = (int)STRLEN(name) + 3;
3530 if (len > varnamebuflen)
3531 {
3532 vim_free(varnamebuf);
3533 len += 10; /* some additional space */
3534 varnamebuf = alloc(len);
3535 if (varnamebuf == NULL)
3536 {
3537 varnamebuflen = 0;
3538 return NULL;
3539 }
3540 varnamebuflen = len;
3541 }
3542 *varnamebuf = prefix;
3543 varnamebuf[1] = ':';
3544 STRCPY(varnamebuf + 2, name);
3545 return varnamebuf;
3546}
3547
3548/*
3549 * Function given to ExpandGeneric() to obtain the list of user defined
3550 * (global/buffer/window/built-in) variable names.
3551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003555 static long_u gdone;
3556 static long_u bdone;
3557 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003558 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003559 static int vidx;
3560 static hashitem_T *hi;
3561 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003564 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003565 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003566 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003567 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003568
3569 /* Global variables */
3570 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003572 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003573 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003574 else
3575 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003576 while (HASHITEM_EMPTY(hi))
3577 ++hi;
3578 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3579 return cat_prefix_varname('g', hi->hi_key);
3580 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003582
3583 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003584 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003587 if (bdone++ == 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('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003595
3596 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003597 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003600 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003601 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003602 else
3603 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003604 while (HASHITEM_EMPTY(hi))
3605 ++hi;
3606 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003608
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003609 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003610 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003611 if (tdone < ht->ht_used)
3612 {
3613 if (tdone++ == 0)
3614 hi = ht->ht_array;
3615 else
3616 ++hi;
3617 while (HASHITEM_EMPTY(hi))
3618 ++hi;
3619 return cat_prefix_varname('t', hi->hi_key);
3620 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003621
Bram Moolenaar33570922005-01-25 22:26:29 +00003622 /* v: variables */
3623 if (vidx < VV_LEN)
3624 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaard23a8232018-02-10 18:45:26 +01003626 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 varnamebuflen = 0;
3628 return NULL;
3629}
3630
3631#endif /* FEAT_CMDL_COMPL */
3632
3633/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003634 * Return TRUE if "pat" matches "text".
3635 * Does not use 'cpo' and always uses 'magic'.
3636 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003637 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003638pattern_match(char_u *pat, char_u *text, int ic)
3639{
3640 int matches = FALSE;
3641 char_u *save_cpo;
3642 regmatch_T regmatch;
3643
3644 /* avoid 'l' flag in 'cpoptions' */
3645 save_cpo = p_cpo;
3646 p_cpo = (char_u *)"";
3647 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3648 if (regmatch.regprog != NULL)
3649 {
3650 regmatch.rm_ic = ic;
3651 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3652 vim_regfree(regmatch.regprog);
3653 }
3654 p_cpo = save_cpo;
3655 return matches;
3656}
3657
3658/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003659 * Handle a name followed by "(". Both for just "name(arg)" and for
3660 * "expr->name(arg)".
3661 * Returns OK or FAIL.
3662 */
3663 static int
3664eval_func(
3665 char_u **arg, // points to "(", will be advanced
3666 char_u *name,
3667 int name_len,
3668 typval_T *rettv,
3669 int evaluate,
3670 typval_T *basetv) // "expr" for "expr->name(arg)"
3671{
3672 char_u *s = name;
3673 int len = name_len;
3674 partial_T *partial;
3675 int ret = OK;
3676
3677 if (!evaluate)
3678 check_vars(s, len);
3679
3680 /* If "s" is the name of a variable of type VAR_FUNC
3681 * use its contents. */
3682 s = deref_func_name(s, &len, &partial, !evaluate);
3683
3684 /* Need to make a copy, in case evaluating the arguments makes
3685 * the name invalid. */
3686 s = vim_strsave(s);
3687 if (s == NULL)
3688 ret = FAIL;
3689 else
3690 {
3691 funcexe_T funcexe;
3692
3693 // Invoke the function.
3694 vim_memset(&funcexe, 0, sizeof(funcexe));
3695 funcexe.firstline = curwin->w_cursor.lnum;
3696 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003697 funcexe.evaluate = evaluate;
3698 funcexe.partial = partial;
3699 funcexe.basetv = basetv;
3700 ret = get_func_tv(s, len, rettv, arg, &funcexe);
3701 }
3702 vim_free(s);
3703
3704 /* If evaluate is FALSE rettv->v_type was not set in
3705 * get_func_tv, but it's needed in handle_subscript() to parse
3706 * what follows. So set it here. */
3707 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3708 {
3709 rettv->vval.v_string = NULL;
3710 rettv->v_type = VAR_FUNC;
3711 }
3712
3713 /* Stop the expression evaluation when immediately
3714 * aborting on error, or when an interrupt occurred or
3715 * an exception was thrown but not caught. */
3716 if (evaluate && aborting())
3717 {
3718 if (ret == OK)
3719 clear_tv(rettv);
3720 ret = FAIL;
3721 }
3722 return ret;
3723}
3724
3725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3729 */
3730
3731/*
3732 * Handle zero level expression.
3733 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003735 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Return OK or FAIL.
3737 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739eval0(
3740 char_u *arg,
3741 typval_T *rettv,
3742 char_u **nextcmd,
3743 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 int ret;
3746 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003747 int did_emsg_before = did_emsg;
3748 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003751 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (ret == FAIL || !ends_excmd(*p))
3753 {
3754 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 /*
3757 * Report the invalid expression unless the expression evaluation has
3758 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003759 * exception, or we already gave a more specific error.
3760 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003762 if (!aborting() && did_emsg == did_emsg_before
3763 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003764 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 ret = FAIL;
3766 }
3767 if (nextcmd != NULL)
3768 *nextcmd = check_nextcmd(p);
3769
3770 return ret;
3771}
3772
3773/*
3774 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003775 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 *
3777 * "arg" must point to the first non-white of the expression.
3778 * "arg" is advanced to the next non-white after the recognized expression.
3779 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003780 * Note: "rettv.v_lock" is not set.
3781 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * Return OK or FAIL.
3783 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003785eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 /*
3791 * Get the first variable.
3792 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 if ((*arg)[0] == '?')
3797 {
3798 result = FALSE;
3799 if (evaluate)
3800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 int error = FALSE;
3802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003803 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003806 if (error)
3807 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809
3810 /*
3811 * Get the second variable.
3812 */
3813 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return FAIL;
3816
3817 /*
3818 * Check for the ":".
3819 */
3820 if ((*arg)[0] != ':')
3821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003822 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826 }
3827
3828 /*
3829 * Get the third variable.
3830 */
3831 *arg = skipwhite(*arg + 1);
3832 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3833 {
3834 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837 }
3838 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
3842 return OK;
3843}
3844
3845/*
3846 * Handle first level expression:
3847 * expr2 || expr2 || expr2 logical OR
3848 *
3849 * "arg" must point to the first non-white of the expression.
3850 * "arg" is advanced to the next non-white after the recognized expression.
3851 *
3852 * Return OK or FAIL.
3853 */
3854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 long result;
3859 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003860 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 /*
3863 * Get the first variable.
3864 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return FAIL;
3867
3868 /*
3869 * Repeat until there is no following "||".
3870 */
3871 first = TRUE;
3872 result = FALSE;
3873 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3874 {
3875 if (evaluate && first)
3876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003877 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 if (error)
3881 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 first = FALSE;
3883 }
3884
3885 /*
3886 * Get the second variable.
3887 */
3888 *arg = skipwhite(*arg + 2);
3889 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3890 return FAIL;
3891
3892 /*
3893 * Compute the result.
3894 */
3895 if (evaluate && !result)
3896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (error)
3901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 if (evaluate)
3904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 rettv->v_type = VAR_NUMBER;
3906 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909
3910 return OK;
3911}
3912
3913/*
3914 * Handle second level expression:
3915 * expr3 && expr3 && expr3 logical AND
3916 *
3917 * "arg" must point to the first non-white of the expression.
3918 * "arg" is advanced to the next non-white after the recognized expression.
3919 *
3920 * Return OK or FAIL.
3921 */
3922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 long result;
3927 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 /*
3931 * Get the first variable.
3932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 return FAIL;
3935
3936 /*
3937 * Repeat until there is no following "&&".
3938 */
3939 first = TRUE;
3940 result = TRUE;
3941 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3942 {
3943 if (evaluate && first)
3944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003948 if (error)
3949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 first = FALSE;
3951 }
3952
3953 /*
3954 * Get the second variable.
3955 */
3956 *arg = skipwhite(*arg + 2);
3957 if (eval4(arg, &var2, evaluate && result) == FAIL)
3958 return FAIL;
3959
3960 /*
3961 * Compute the result.
3962 */
3963 if (evaluate && result)
3964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003965 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003968 if (error)
3969 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 if (evaluate)
3972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 rettv->v_type = VAR_NUMBER;
3974 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 }
3977
3978 return OK;
3979}
3980
3981/*
3982 * Handle third level expression:
3983 * var1 == var2
3984 * var1 =~ var2
3985 * var1 != var2
3986 * var1 !~ var2
3987 * var1 > var2
3988 * var1 >= var2
3989 * var1 < var2
3990 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003991 * var1 is var2
3992 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 *
3994 * "arg" must point to the first non-white of the expression.
3995 * "arg" is advanced to the next non-white after the recognized expression.
3996 *
3997 * Return OK or FAIL.
3998 */
3999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004000eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *p;
4004 int i;
4005 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004006 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 /*
4011 * Get the first variable.
4012 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return FAIL;
4015
4016 p = *arg;
4017 switch (p[0])
4018 {
4019 case '=': if (p[1] == '=')
4020 type = TYPE_EQUAL;
4021 else if (p[1] == '~')
4022 type = TYPE_MATCH;
4023 break;
4024 case '!': if (p[1] == '=')
4025 type = TYPE_NEQUAL;
4026 else if (p[1] == '~')
4027 type = TYPE_NOMATCH;
4028 break;
4029 case '>': if (p[1] != '=')
4030 {
4031 type = TYPE_GREATER;
4032 len = 1;
4033 }
4034 else
4035 type = TYPE_GEQUAL;
4036 break;
4037 case '<': if (p[1] != '=')
4038 {
4039 type = TYPE_SMALLER;
4040 len = 1;
4041 }
4042 else
4043 type = TYPE_SEQUAL;
4044 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004045 case 'i': if (p[1] == 's')
4046 {
4047 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4048 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004049 i = p[len];
4050 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004051 {
4052 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4053 type_is = TRUE;
4054 }
4055 }
4056 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058
4059 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004060 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 */
4062 if (type != TYPE_UNKNOWN)
4063 {
4064 /* extra question mark appended: ignore case */
4065 if (p[len] == '?')
4066 {
4067 ic = TRUE;
4068 ++len;
4069 }
4070 /* extra '#' appended: match case */
4071 else if (p[len] == '#')
4072 {
4073 ic = FALSE;
4074 ++len;
4075 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004076 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 else
4078 ic = p_ic;
4079
4080 /*
4081 * Get the second variable.
4082 */
4083 *arg = skipwhite(p + len);
4084 if (eval5(arg, &var2, evaluate) == FAIL)
4085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004089 if (evaluate)
4090 {
4091 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4092
4093 clear_tv(&var2);
4094 return ret;
4095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 return OK;
4099}
4100
4101/*
4102 * Handle fourth level expression:
4103 * + number addition
4104 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004105 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004106 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 *
4108 * "arg" must point to the first non-white of the expression.
4109 * "arg" is advanced to the next non-white after the recognized expression.
4110 *
4111 * Return OK or FAIL.
4112 */
4113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
Bram Moolenaar33570922005-01-25 22:26:29 +00004116 typval_T var2;
4117 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004119 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004120#ifdef FEAT_FLOAT
4121 float_T f1 = 0, f2 = 0;
4122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 char_u *s1, *s2;
4124 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4125 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004126 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
4128 /*
4129 * Get the first variable.
4130 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004131 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return FAIL;
4133
4134 /*
4135 * Repeat computing, until no '+', '-' or '.' is following.
4136 */
4137 for (;;)
4138 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004139 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004141 concat = op == '.'
4142 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4143 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 break;
4145
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004146 if ((op != '+' || (rettv->v_type != VAR_LIST
4147 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004148#ifdef FEAT_FLOAT
4149 && (op == '.' || rettv->v_type != VAR_FLOAT)
4150#endif
4151 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 {
4153 /* For "list + ...", an illegal use of the first operand as
4154 * a number cannot be determined before evaluating the 2nd
4155 * operand: if this is also a list, all is ok.
4156 * For "something . ...", "something - ..." or "non-list + ...",
4157 * we know that the first operand needs to be a string or number
4158 * without evaluating the 2nd operand. So check before to avoid
4159 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004160 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 {
4162 clear_tv(rettv);
4163 return FAIL;
4164 }
4165 }
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /*
4168 * Get the second variable.
4169 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004170 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4171 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004173 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 return FAIL;
4177 }
4178
4179 if (evaluate)
4180 {
4181 /*
4182 * Compute the result.
4183 */
4184 if (op == '.')
4185 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004186 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4187 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (s2 == NULL) /* type error ? */
4189 {
4190 clear_tv(rettv);
4191 clear_tv(&var2);
4192 return FAIL;
4193 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004194 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
4196 rettv->v_type = VAR_STRING;
4197 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004199 else if (op == '+' && rettv->v_type == VAR_BLOB
4200 && var2.v_type == VAR_BLOB)
4201 {
4202 blob_T *b1 = rettv->vval.v_blob;
4203 blob_T *b2 = var2.vval.v_blob;
4204 blob_T *b = blob_alloc();
4205 int i;
4206
4207 if (b != NULL)
4208 {
4209 for (i = 0; i < blob_len(b1); i++)
4210 ga_append(&b->bv_ga, blob_get(b1, i));
4211 for (i = 0; i < blob_len(b2); i++)
4212 ga_append(&b->bv_ga, blob_get(b2, i));
4213
4214 clear_tv(rettv);
4215 rettv_blob_set(rettv, b);
4216 }
4217 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004218 else if (op == '+' && rettv->v_type == VAR_LIST
4219 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 {
4221 /* concatenate Lists */
4222 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4223 &var3) == FAIL)
4224 {
4225 clear_tv(rettv);
4226 clear_tv(&var2);
4227 return FAIL;
4228 }
4229 clear_tv(rettv);
4230 *rettv = var3;
4231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 else
4233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
4235
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004236#ifdef FEAT_FLOAT
4237 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004239 f1 = rettv->vval.v_float;
4240 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004242 else
4243#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004245 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004246 if (error)
4247 {
4248 /* This can only happen for "list + non-list". For
4249 * "non-list + ..." or "something - ...", we returned
4250 * before evaluating the 2nd operand. */
4251 clear_tv(rettv);
4252 return FAIL;
4253 }
4254#ifdef FEAT_FLOAT
4255 if (var2.v_type == VAR_FLOAT)
4256 f1 = n1;
4257#endif
4258 }
4259#ifdef FEAT_FLOAT
4260 if (var2.v_type == VAR_FLOAT)
4261 {
4262 f2 = var2.vval.v_float;
4263 n2 = 0;
4264 }
4265 else
4266#endif
4267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004268 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004269 if (error)
4270 {
4271 clear_tv(rettv);
4272 clear_tv(&var2);
4273 return FAIL;
4274 }
4275#ifdef FEAT_FLOAT
4276 if (rettv->v_type == VAR_FLOAT)
4277 f2 = n2;
4278#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004281
4282#ifdef FEAT_FLOAT
4283 /* If there is a float on either side the result is a float. */
4284 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4285 {
4286 if (op == '+')
4287 f1 = f1 + f2;
4288 else
4289 f1 = f1 - f2;
4290 rettv->v_type = VAR_FLOAT;
4291 rettv->vval.v_float = f1;
4292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004294#endif
4295 {
4296 if (op == '+')
4297 n1 = n1 + n2;
4298 else
4299 n1 = n1 - n2;
4300 rettv->v_type = VAR_NUMBER;
4301 rettv->vval.v_number = n1;
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 }
4307 return OK;
4308}
4309
4310/*
4311 * Handle fifth level expression:
4312 * * number multiplication
4313 * / number division
4314 * % number modulo
4315 *
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4318 *
4319 * Return OK or FAIL.
4320 */
4321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004322eval6(
4323 char_u **arg,
4324 typval_T *rettv,
4325 int evaluate,
4326 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
Bram Moolenaar33570922005-01-25 22:26:29 +00004328 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004330 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331#ifdef FEAT_FLOAT
4332 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004333 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 /*
4338 * Get the first variable.
4339 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004340 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 return FAIL;
4342
4343 /*
4344 * Repeat computing, until no '*', '/' or '%' is following.
4345 */
4346 for (;;)
4347 {
4348 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004349 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
4351
4352 if (evaluate)
4353 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004354#ifdef FEAT_FLOAT
4355 if (rettv->v_type == VAR_FLOAT)
4356 {
4357 f1 = rettv->vval.v_float;
4358 use_float = TRUE;
4359 n1 = 0;
4360 }
4361 else
4362#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004363 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 if (error)
4366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 else
4369 n1 = 0;
4370
4371 /*
4372 * Get the second variable.
4373 */
4374 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004375 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 return FAIL;
4377
4378 if (evaluate)
4379 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004380#ifdef FEAT_FLOAT
4381 if (var2.v_type == VAR_FLOAT)
4382 {
4383 if (!use_float)
4384 {
4385 f1 = n1;
4386 use_float = TRUE;
4387 }
4388 f2 = var2.vval.v_float;
4389 n2 = 0;
4390 }
4391 else
4392#endif
4393 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004394 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004395 clear_tv(&var2);
4396 if (error)
4397 return FAIL;
4398#ifdef FEAT_FLOAT
4399 if (use_float)
4400 f2 = n2;
4401#endif
4402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 /*
4405 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004406 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408#ifdef FEAT_FLOAT
4409 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004411 if (op == '*')
4412 f1 = f1 * f2;
4413 else if (op == '/')
4414 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004415# ifdef VMS
4416 /* VMS crashes on divide by zero, work around it */
4417 if (f2 == 0.0)
4418 {
4419 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004420 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004421 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004422 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004423 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004424 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004425 }
4426 else
4427 f1 = f1 / f2;
4428# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429 /* We rely on the floating point library to handle divide
4430 * by zero to result in "inf" and not a crash. */
4431 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004432# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004436 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 return FAIL;
4438 }
4439 rettv->v_type = VAR_FLOAT;
4440 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 if (op == '*')
4446 n1 = n1 * n2;
4447 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004448 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004450 n1 = num_modulus(n1, n2);
4451
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 rettv->v_type = VAR_NUMBER;
4453 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 }
4457
4458 return OK;
4459}
4460
4461/*
4462 * Handle sixth level expression:
4463 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004464 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004465 * "string" string constant
4466 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 * &option-name option value
4468 * @r register contents
4469 * identifier variable value
4470 * function() function call
4471 * $VAR environment variable
4472 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004473 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004474 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004475 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 *
4477 * Also handle:
4478 * ! in front logical NOT
4479 * - in front unary minus
4480 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 * trailing [] subscript in String or List
4482 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004483 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 *
4485 * "arg" must point to the first non-white of the expression.
4486 * "arg" is advanced to the next non-white after the recognized expression.
4487 *
4488 * Return OK or FAIL.
4489 */
4490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004491eval7(
4492 char_u **arg,
4493 typval_T *rettv,
4494 int evaluate,
4495 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004497 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 int len;
4499 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 char_u *start_leader, *end_leader;
4501 int ret = OK;
4502 char_u *alias;
4503
4504 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004511 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 */
4513 start_leader = *arg;
4514 while (**arg == '!' || **arg == '-' || **arg == '+')
4515 *arg = skipwhite(*arg + 1);
4516 end_leader = *arg;
4517
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004518 if (**arg == '.' && (!isdigit(*(*arg + 1))
4519#ifdef FEAT_FLOAT
4520 || current_sctx.sc_version < 2
4521#endif
4522 ))
4523 {
4524 semsg(_(e_invexpr2), *arg);
4525 ++*arg;
4526 return FAIL;
4527 }
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 switch (**arg)
4530 {
4531 /*
4532 * Number constant.
4533 */
4534 case '0':
4535 case '1':
4536 case '2':
4537 case '3':
4538 case '4':
4539 case '5':
4540 case '6':
4541 case '7':
4542 case '8':
4543 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004544 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004545 {
4546#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004547 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004548 int get_float = FALSE;
4549
4550 /* We accept a float when the format matches
4551 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004552 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004553 * With script version 2 and later the leading digit can be
4554 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004555 * Don't look for a float after the "." operator, so that
4556 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004557 if (**arg == '.')
4558 p = *arg;
4559 else
4560 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004561 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004563 get_float = TRUE;
4564 p = skipdigits(p + 2);
4565 if (*p == 'e' || *p == 'E')
4566 {
4567 ++p;
4568 if (*p == '-' || *p == '+')
4569 ++p;
4570 if (!vim_isdigit(*p))
4571 get_float = FALSE;
4572 else
4573 p = skipdigits(p + 1);
4574 }
4575 if (ASCII_ISALPHA(*p) || *p == '.')
4576 get_float = FALSE;
4577 }
4578 if (get_float)
4579 {
4580 float_T f;
4581
4582 *arg += string2float(*arg, &f);
4583 if (evaluate)
4584 {
4585 rettv->v_type = VAR_FLOAT;
4586 rettv->vval.v_float = f;
4587 }
4588 }
4589 else
4590#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004591 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004592 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004593 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004594 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004595
4596 // Blob constant: 0z0123456789abcdef
4597 if (evaluate)
4598 blob = blob_alloc();
4599 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4600 {
4601 if (!vim_isxdigit(bp[1]))
4602 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004603 if (blob != NULL)
4604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004605 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004606 ga_clear(&blob->bv_ga);
4607 VIM_CLEAR(blob);
4608 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004609 ret = FAIL;
4610 break;
4611 }
4612 if (blob != NULL)
4613 ga_append(&blob->bv_ga,
4614 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004615 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4616 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004617 }
4618 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004619 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004620 *arg = bp;
4621 }
4622 else
4623 {
4624 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004625 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4626 if (len == 0)
4627 {
4628 semsg(_(e_invexpr2), *arg);
4629 ret = FAIL;
4630 break;
4631 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 *arg += len;
4633 if (evaluate)
4634 {
4635 rettv->v_type = VAR_NUMBER;
4636 rettv->vval.v_number = n;
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /*
4643 * String constant: "string".
4644 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004645 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 break;
4647
4648 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004649 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 break;
4653
4654 /*
4655 * List: [expr, expr]
4656 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 break;
4659
4660 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004661 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004662 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004663 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004664 {
4665 ++*arg;
4666 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4667 }
4668 else
4669 ret = NOTDONE;
4670 break;
4671
4672 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004673 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004674 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004676 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4677 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004678 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 break;
4680
4681 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004682 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 break;
4686
4687 /*
4688 * Environment variable: $VAR.
4689 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 break;
4692
4693 /*
4694 * Register contents: @r.
4695 */
4696 case '@': ++*arg;
4697 if (evaluate)
4698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004700 rettv->vval.v_string = get_reg_contents(**arg,
4701 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 if (**arg != NUL)
4704 ++*arg;
4705 break;
4706
4707 /*
4708 * nested expression: (expression).
4709 */
4710 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004711 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (**arg == ')')
4713 ++*arg;
4714 else if (ret == OK)
4715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004717 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 ret = FAIL;
4719 }
4720 break;
4721
Bram Moolenaar8c711452005-01-14 21:53:12 +00004722 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 break;
4724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725
4726 if (ret == NOTDONE)
4727 {
4728 /*
4729 * Must be a variable or function name.
4730 * Can also be a curly-braces kind of name: {expr}.
4731 */
4732 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 if (alias != NULL)
4735 s = alias;
4736
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004737 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 ret = FAIL;
4739 else
4740 {
4741 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004742 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004744 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004745 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004746 {
4747 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004748 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004750 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004751 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 *arg = skipwhite(*arg);
4755
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004756 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004757 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 if (ret == OK)
4759 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 /*
4762 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4763 */
4764 if (ret == OK && evaluate && end_leader > start_leader)
4765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004766 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004767 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768#ifdef FEAT_FLOAT
4769 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771 if (rettv->v_type == VAR_FLOAT)
4772 f = rettv->vval.v_float;
4773 else
4774#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004775 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 clear_tv(rettv);
4779 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 else
4782 {
4783 while (end_leader > start_leader)
4784 {
4785 --end_leader;
4786 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 {
4788#ifdef FEAT_FLOAT
4789 if (rettv->v_type == VAR_FLOAT)
4790 f = !f;
4791 else
4792#endif
4793 val = !val;
4794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 {
4797#ifdef FEAT_FLOAT
4798 if (rettv->v_type == VAR_FLOAT)
4799 f = -f;
4800 else
4801#endif
4802 val = -val;
4803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (rettv->v_type == VAR_FLOAT)
4807 {
4808 clear_tv(rettv);
4809 rettv->vval.v_float = f;
4810 }
4811 else
4812#endif
4813 {
4814 clear_tv(rettv);
4815 rettv->v_type = VAR_NUMBER;
4816 rettv->vval.v_number = val;
4817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820
4821 return ret;
4822}
4823
4824/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004825 * Call the function referred to in "rettv".
4826 */
4827 static int
4828call_func_rettv(
4829 char_u **arg,
4830 typval_T *rettv,
4831 int evaluate,
4832 dict_T *selfdict,
4833 typval_T *basetv)
4834{
4835 partial_T *pt = NULL;
4836 funcexe_T funcexe;
4837 typval_T functv;
4838 char_u *s;
4839 int ret;
4840
4841 // need to copy the funcref so that we can clear rettv
4842 if (evaluate)
4843 {
4844 functv = *rettv;
4845 rettv->v_type = VAR_UNKNOWN;
4846
4847 /* Invoke the function. Recursive! */
4848 if (functv.v_type == VAR_PARTIAL)
4849 {
4850 pt = functv.vval.v_partial;
4851 s = partial_name(pt);
4852 }
4853 else
4854 s = functv.vval.v_string;
4855 }
4856 else
4857 s = (char_u *)"";
4858
4859 vim_memset(&funcexe, 0, sizeof(funcexe));
4860 funcexe.firstline = curwin->w_cursor.lnum;
4861 funcexe.lastline = curwin->w_cursor.lnum;
4862 funcexe.evaluate = evaluate;
4863 funcexe.partial = pt;
4864 funcexe.selfdict = selfdict;
4865 funcexe.basetv = basetv;
4866 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
4867
4868 /* Clear the funcref afterwards, so that deleting it while
4869 * evaluating the arguments is possible (see test55). */
4870 if (evaluate)
4871 clear_tv(&functv);
4872
4873 return ret;
4874}
4875
4876/*
4877 * Evaluate "->method()".
4878 * "*arg" points to the '-'.
4879 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4880 */
4881 static int
4882eval_lambda(
4883 char_u **arg,
4884 typval_T *rettv,
4885 int evaluate,
4886 int verbose) /* give error messages */
4887{
4888 typval_T base = *rettv;
4889 int ret;
4890
4891 // Skip over the ->.
4892 *arg += 2;
4893 rettv->v_type = VAR_UNKNOWN;
4894
4895 ret = get_lambda_tv(arg, rettv, evaluate);
4896 if (ret == NOTDONE)
4897 return FAIL;
4898 else if (**arg != '(')
4899 {
4900 if (verbose)
4901 {
4902 if (*skipwhite(*arg) == '(')
4903 semsg(_(e_nowhitespace));
4904 else
4905 semsg(_(e_missingparen), "lambda");
4906 }
4907 clear_tv(rettv);
4908 return FAIL;
4909 }
4910 return call_func_rettv(arg, rettv, evaluate, NULL, &base);
4911}
4912
4913/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004914 * Evaluate "->method()".
4915 * "*arg" points to the '-'.
4916 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4917 */
4918 static int
4919eval_method(
4920 char_u **arg,
4921 typval_T *rettv,
4922 int evaluate,
4923 int verbose) /* give error messages */
4924{
4925 char_u *name;
4926 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004927 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004928 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004929 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004930
4931 // Skip over the ->.
4932 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004933 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004934
Bram Moolenaarac92e252019-08-03 21:58:38 +02004935 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004936 len = get_name_len(arg, &alias, evaluate, TRUE);
4937 if (alias != NULL)
4938 name = alias;
4939
4940 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004941 {
4942 if (verbose)
4943 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004944 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004945 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004946 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004947 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004948 if (**arg != '(')
4949 {
4950 if (verbose)
4951 semsg(_(e_missingparen), name);
4952 ret = FAIL;
4953 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004954 else if (VIM_ISWHITE((*arg)[-1]))
4955 {
4956 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004957 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02004958 ret = FAIL;
4959 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004960 else
4961 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004962 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004963
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004964 // Clear the funcref afterwards, so that deleting it while
4965 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004966 if (evaluate)
4967 clear_tv(&base);
4968
Bram Moolenaarac92e252019-08-03 21:58:38 +02004969 return ret;
4970}
4971
4972/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004973 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4974 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4976 */
4977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004978eval_index(
4979 char_u **arg,
4980 typval_T *rettv,
4981 int evaluate,
4982 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983{
4984 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004985 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004986 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 long len = -1;
4989 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004992
Bram Moolenaara03f2332016-02-06 18:09:59 +01004993 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004995 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004996 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004997 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004998 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004999 return FAIL;
5000 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005001#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005002 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005003 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005004 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005005#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005006 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005007 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005008 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005009 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005010 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005011 return FAIL;
5012 case VAR_UNKNOWN:
5013 if (evaluate)
5014 return FAIL;
5015 /* FALLTHROUGH */
5016
5017 case VAR_STRING:
5018 case VAR_NUMBER:
5019 case VAR_LIST:
5020 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005021 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005022 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005023 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005024
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005025 init_tv(&var1);
5026 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 /*
5030 * dict.name
5031 */
5032 key = *arg + 1;
5033 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5034 ;
5035 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005038 }
5039 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005040 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 /*
5042 * something[idx]
5043 *
5044 * Get the (first) variable from inside the [].
5045 */
5046 *arg = skipwhite(*arg + 1);
5047 if (**arg == ':')
5048 empty1 = TRUE;
5049 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5050 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005051 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005052 {
5053 /* not a number or string */
5054 clear_tv(&var1);
5055 return FAIL;
5056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057
5058 /*
5059 * Get the second variable from inside the [:].
5060 */
5061 if (**arg == ':')
5062 {
5063 range = TRUE;
5064 *arg = skipwhite(*arg + 1);
5065 if (**arg == ']')
5066 empty2 = TRUE;
5067 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005069 if (!empty1)
5070 clear_tv(&var1);
5071 return FAIL;
5072 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005073 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005074 {
5075 /* not a number or string */
5076 if (!empty1)
5077 clear_tv(&var1);
5078 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 return FAIL;
5080 }
5081 }
5082
5083 /* Check for the ']'. */
5084 if (**arg != ']')
5085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005086 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005087 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 clear_tv(&var1);
5089 if (range)
5090 clear_tv(&var2);
5091 return FAIL;
5092 }
5093 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 }
5095
5096 if (evaluate)
5097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 n1 = 0;
5099 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005101 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 }
5104 if (range)
5105 {
5106 if (empty2)
5107 n2 = -1;
5108 else
5109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005110 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112 }
5113 }
5114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005117 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005118 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005119 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005120 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005121 case VAR_SPECIAL:
5122 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005123 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005124 break; /* not evaluating, skipping over subscript */
5125
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126 case VAR_NUMBER:
5127 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005128 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 len = (long)STRLEN(s);
5130 if (range)
5131 {
5132 /* The resulting variable is a substring. If the indexes
5133 * are out of range the result is empty. */
5134 if (n1 < 0)
5135 {
5136 n1 = len + n1;
5137 if (n1 < 0)
5138 n1 = 0;
5139 }
5140 if (n2 < 0)
5141 n2 = len + n2;
5142 else if (n2 >= len)
5143 n2 = len;
5144 if (n1 >= len || n2 < 0 || n1 > n2)
5145 s = NULL;
5146 else
5147 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5148 }
5149 else
5150 {
5151 /* The resulting variable is a string of a single
5152 * character. If the index is too big or negative the
5153 * result is empty. */
5154 if (n1 >= len || n1 < 0)
5155 s = NULL;
5156 else
5157 s = vim_strnsave(s + n1, 1);
5158 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 clear_tv(rettv);
5160 rettv->v_type = VAR_STRING;
5161 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 break;
5163
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005164 case VAR_BLOB:
5165 len = blob_len(rettv->vval.v_blob);
5166 if (range)
5167 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005168 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005169 // are out of range the result is empty.
5170 if (n1 < 0)
5171 {
5172 n1 = len + n1;
5173 if (n1 < 0)
5174 n1 = 0;
5175 }
5176 if (n2 < 0)
5177 n2 = len + n2;
5178 else if (n2 >= len)
5179 n2 = len - 1;
5180 if (n1 >= len || n2 < 0 || n1 > n2)
5181 {
5182 clear_tv(rettv);
5183 rettv->v_type = VAR_BLOB;
5184 rettv->vval.v_blob = NULL;
5185 }
5186 else
5187 {
5188 blob_T *blob = blob_alloc();
5189
5190 if (blob != NULL)
5191 {
5192 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5193 {
5194 blob_free(blob);
5195 return FAIL;
5196 }
5197 blob->bv_ga.ga_len = n2 - n1 + 1;
5198 for (i = n1; i <= n2; i++)
5199 blob_set(blob, i - n1,
5200 blob_get(rettv->vval.v_blob, i));
5201
5202 clear_tv(rettv);
5203 rettv_blob_set(rettv, blob);
5204 }
5205 }
5206 }
5207 else
5208 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005209 // The resulting variable is a byte value.
5210 // If the index is too big or negative that is an error.
5211 if (n1 < 0)
5212 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005213 if (n1 < len && n1 >= 0)
5214 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005215 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005216
5217 clear_tv(rettv);
5218 rettv->v_type = VAR_NUMBER;
5219 rettv->vval.v_number = v;
5220 }
5221 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005222 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005223 }
5224 break;
5225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 if (n1 < 0)
5229 n1 = len + n1;
5230 if (!empty1 && (n1 < 0 || n1 >= len))
5231 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005232 /* For a range we allow invalid values and return an empty
5233 * list. A list index out of range is an error. */
5234 if (!range)
5235 {
5236 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005237 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005238 return FAIL;
5239 }
5240 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 }
5242 if (range)
5243 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 list_T *l;
5245 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246
5247 if (n2 < 0)
5248 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005249 else if (n2 >= len)
5250 n2 = len - 1;
5251 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005252 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 l = list_alloc();
5254 if (l == NULL)
5255 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 n1 <= n2; ++n1)
5258 {
5259 if (list_append_tv(l, &item->li_tv) == FAIL)
5260 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005261 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 return FAIL;
5263 }
5264 item = item->li_next;
5265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005267 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 }
5269 else
5270 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005271 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005272 clear_tv(rettv);
5273 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 }
5275 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276
5277 case VAR_DICT:
5278 if (range)
5279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005281 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (len == -1)
5283 clear_tv(&var1);
5284 return FAIL;
5285 }
5286 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005287 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288
5289 if (len == -1)
5290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005291 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005292 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 clear_tv(&var1);
5295 return FAIL;
5296 }
5297 }
5298
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005299 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005302 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 if (len == -1)
5304 clear_tv(&var1);
5305 if (item == NULL)
5306 return FAIL;
5307
5308 copy_tv(&item->di_tv, &var1);
5309 clear_tv(rettv);
5310 *rettv = var1;
5311 }
5312 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 }
5314 }
5315
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 return OK;
5317}
5318
5319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 * Get an option value.
5321 * "arg" points to the '&' or '+' before the option name.
5322 * "arg" is advanced to character after the option name.
5323 * Return OK or FAIL.
5324 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005326get_option_tv(
5327 char_u **arg,
5328 typval_T *rettv, /* when NULL, only check if option exists */
5329 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
5331 char_u *option_end;
5332 long numval;
5333 char_u *stringval;
5334 int opt_type;
5335 int c;
5336 int working = (**arg == '+'); /* has("+option") */
5337 int ret = OK;
5338 int opt_flags;
5339
5340 /*
5341 * Isolate the option name and find its value.
5342 */
5343 option_end = find_option_end(arg, &opt_flags);
5344 if (option_end == NULL)
5345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005346 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005347 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 return FAIL;
5349 }
5350
5351 if (!evaluate)
5352 {
5353 *arg = option_end;
5354 return OK;
5355 }
5356
5357 c = *option_end;
5358 *option_end = NUL;
5359 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
5362 if (opt_type == -3) /* invalid name */
5363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005365 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 ret = FAIL;
5367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
5370 if (opt_type == -2) /* hidden string option */
5371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 rettv->v_type = VAR_STRING;
5373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 else if (opt_type == -1) /* hidden number option */
5376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 rettv->v_type = VAR_NUMBER;
5378 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
5380 else if (opt_type == 1) /* number option */
5381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 rettv->v_type = VAR_NUMBER;
5383 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 else /* string option */
5386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 rettv->v_type = VAR_STRING;
5388 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390 }
5391 else if (working && (opt_type == -2 || opt_type == -1))
5392 ret = FAIL;
5393
5394 *option_end = c; /* put back for error messages */
5395 *arg = option_end;
5396
5397 return ret;
5398}
5399
5400/*
5401 * Allocate a variable for a string constant.
5402 * Return OK or FAIL.
5403 */
5404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005405get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 char_u *p;
5408 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 int extra = 0;
5410
5411 /*
5412 * Find the end of the string, skipping backslashed characters.
5413 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005414 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 if (*p == '\\' && p[1] != NUL)
5417 {
5418 ++p;
5419 /* A "\<x>" form occupies at least 4 characters, and produces up
5420 * to 6 characters: reserve space for 2 extra */
5421 if (*p == '<')
5422 extra += 2;
5423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425
5426 if (*p != '"')
5427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005428 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 return FAIL;
5430 }
5431
5432 /* If only parsing, set *arg and return here */
5433 if (!evaluate)
5434 {
5435 *arg = p + 1;
5436 return OK;
5437 }
5438
5439 /*
5440 * Copy the string into allocated memory, handling backslashed
5441 * characters.
5442 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005443 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (name == NULL)
5445 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 rettv->v_type = VAR_STRING;
5447 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaar8c711452005-01-14 21:53:12 +00005449 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 if (*p == '\\')
5452 {
5453 switch (*++p)
5454 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 case 'b': *name++ = BS; ++p; break;
5456 case 'e': *name++ = ESC; ++p; break;
5457 case 'f': *name++ = FF; ++p; break;
5458 case 'n': *name++ = NL; ++p; break;
5459 case 'r': *name++ = CAR; ++p; break;
5460 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 case 'X': /* hex: "\x1", "\x12" */
5463 case 'x':
5464 case 'u': /* Unicode: "\u0023" */
5465 case 'U':
5466 if (vim_isxdigit(p[1]))
5467 {
5468 int n, nr;
5469 int c = toupper(*p);
5470
5471 if (c == 'X')
5472 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005473 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005475 else
5476 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 nr = 0;
5478 while (--n >= 0 && vim_isxdigit(p[1]))
5479 {
5480 ++p;
5481 nr = (nr << 4) + hex2nr(*p);
5482 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005483 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 /* For "\u" store the number according to
5485 * 'encoding'. */
5486 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 break;
5492
5493 /* octal: "\1", "\12", "\123" */
5494 case '0':
5495 case '1':
5496 case '2':
5497 case '3':
5498 case '4':
5499 case '5':
5500 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 case '7': *name = *p++ - '0';
5502 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504 *name = (*name << 3) + *p++ - '0';
5505 if (*p >= '0' && *p <= '7')
5506 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005508 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 break;
5510
5511 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005512 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if (extra != 0)
5514 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 break;
5517 }
5518 /* FALLTHROUGH */
5519
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 break;
5522 }
5523 }
5524 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005525 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005529 if (*p != NUL) /* just in case */
5530 ++p;
5531 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 return OK;
5534}
5535
5536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 * Return OK or FAIL.
5539 */
5540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005541get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005544 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005545 int reduce = 0;
5546
5547 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005548 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005549 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005550 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005551 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005553 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005554 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005555 break;
5556 ++reduce;
5557 ++p;
5558 }
5559 }
5560
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005563 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005564 return FAIL;
5565 }
5566
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005568 if (!evaluate)
5569 {
5570 *arg = p + 1;
5571 return OK;
5572 }
5573
5574 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005577 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005578 if (str == NULL)
5579 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 rettv->v_type = VAR_STRING;
5581 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005582
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005586 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005588 break;
5589 ++p;
5590 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005594 *arg = p + 1;
5595
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 return OK;
5597}
5598
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005599/*
5600 * Return the function name of the partial.
5601 */
5602 char_u *
5603partial_name(partial_T *pt)
5604{
5605 if (pt->pt_name != NULL)
5606 return pt->pt_name;
5607 return pt->pt_func->uf_name;
5608}
5609
Bram Moolenaarddecc252016-04-06 22:59:37 +02005610 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005611partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005612{
5613 int i;
5614
5615 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005616 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005617 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005619 if (pt->pt_name != NULL)
5620 {
5621 func_unref(pt->pt_name);
5622 vim_free(pt->pt_name);
5623 }
5624 else
5625 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005626 vim_free(pt);
5627}
5628
5629/*
5630 * Unreference a closure: decrement the reference count and free it when it
5631 * becomes zero.
5632 */
5633 void
5634partial_unref(partial_T *pt)
5635{
5636 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005637 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005638}
5639
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005640static int tv_equal_recurse_limit;
5641
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005642 static int
5643func_equal(
5644 typval_T *tv1,
5645 typval_T *tv2,
5646 int ic) /* ignore case */
5647{
5648 char_u *s1, *s2;
5649 dict_T *d1, *d2;
5650 int a1, a2;
5651 int i;
5652
5653 /* empty and NULL function name considered the same */
5654 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005655 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005656 if (s1 != NULL && *s1 == NUL)
5657 s1 = NULL;
5658 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005659 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005660 if (s2 != NULL && *s2 == NUL)
5661 s2 = NULL;
5662 if (s1 == NULL || s2 == NULL)
5663 {
5664 if (s1 != s2)
5665 return FALSE;
5666 }
5667 else if (STRCMP(s1, s2) != 0)
5668 return FALSE;
5669
5670 /* empty dict and NULL dict is different */
5671 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5672 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5673 if (d1 == NULL || d2 == NULL)
5674 {
5675 if (d1 != d2)
5676 return FALSE;
5677 }
5678 else if (!dict_equal(d1, d2, ic, TRUE))
5679 return FALSE;
5680
5681 /* empty list and no list considered the same */
5682 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5683 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5684 if (a1 != a2)
5685 return FALSE;
5686 for (i = 0; i < a1; ++i)
5687 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5688 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5689 return FALSE;
5690
5691 return TRUE;
5692}
5693
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005694/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005695 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005696 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005697 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005698 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005700tv_equal(
5701 typval_T *tv1,
5702 typval_T *tv2,
5703 int ic, /* ignore case */
5704 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005705{
5706 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005707 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005708 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005709 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005710
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005711 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005712 * recursiveness to a limit. We guess they are equal then.
5713 * A fixed limit has the problem of still taking an awful long time.
5714 * Reduce the limit every time running into it. That should work fine for
5715 * deeply linked structures that are not recursively linked and catch
5716 * recursiveness quickly. */
5717 if (!recursive)
5718 tv_equal_recurse_limit = 1000;
5719 if (recursive_cnt >= tv_equal_recurse_limit)
5720 {
5721 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005722 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005723 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005724
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005725 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5726 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005727 if ((tv1->v_type == VAR_FUNC
5728 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5729 && (tv2->v_type == VAR_FUNC
5730 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5731 {
5732 ++recursive_cnt;
5733 r = func_equal(tv1, tv2, ic);
5734 --recursive_cnt;
5735 return r;
5736 }
5737
5738 if (tv1->v_type != tv2->v_type)
5739 return FALSE;
5740
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005741 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005742 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005743 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005744 ++recursive_cnt;
5745 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5746 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005747 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005748
5749 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005750 ++recursive_cnt;
5751 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5752 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005753 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005754
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005755 case VAR_BLOB:
5756 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5757
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005758 case VAR_NUMBER:
5759 return tv1->vval.v_number == tv2->vval.v_number;
5760
5761 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005762 s1 = tv_get_string_buf(tv1, buf1);
5763 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005764 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005765
5766 case VAR_SPECIAL:
5767 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005768
5769 case VAR_FLOAT:
5770#ifdef FEAT_FLOAT
5771 return tv1->vval.v_float == tv2->vval.v_float;
5772#endif
5773 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005774#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005775 return tv1->vval.v_job == tv2->vval.v_job;
5776#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005777 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005778#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005779 return tv1->vval.v_channel == tv2->vval.v_channel;
5780#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005781 case VAR_FUNC:
5782 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005783 case VAR_UNKNOWN:
5784 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005785 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005786
Bram Moolenaara03f2332016-02-06 18:09:59 +01005787 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5788 * does not equal anything, not even itself. */
5789 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005790}
5791
5792/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005793 * Return the next (unique) copy ID.
5794 * Used for serializing nested structures.
5795 */
5796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005797get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005798{
5799 current_copyID += COPYID_INC;
5800 return current_copyID;
5801}
5802
5803/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005804 * Garbage collection for lists and dictionaries.
5805 *
5806 * We use reference counts to be able to free most items right away when they
5807 * are no longer used. But for composite items it's possible that it becomes
5808 * unused while the reference count is > 0: When there is a recursive
5809 * reference. Example:
5810 * :let l = [1, 2, 3]
5811 * :let d = {9: l}
5812 * :let l[1] = d
5813 *
5814 * Since this is quite unusual we handle this with garbage collection: every
5815 * once in a while find out which lists and dicts are not referenced from any
5816 * variable.
5817 *
5818 * Here is a good reference text about garbage collection (refers to Python
5819 * but it applies to all reference-counting mechanisms):
5820 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005821 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005822
5823/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005825 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005826 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005827 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005828 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005829garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005830{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005831 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005832 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005833 buf_T *buf;
5834 win_T *wp;
5835 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005836 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005837 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005838
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005839 if (!testing)
5840 {
5841 /* Only do this once. */
5842 want_garbage_collect = FALSE;
5843 may_garbage_collect = FALSE;
5844 garbage_collect_at_exit = FALSE;
5845 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005846
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005847 /* We advance by two because we add one for items referenced through
5848 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005849 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005850
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005851 /*
5852 * 1. Go through all accessible variables and mark all lists and dicts
5853 * with copyID.
5854 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005855
5856 /* Don't free variables in the previous_funccal list unless they are only
5857 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005858 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005859 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005861 /* script-local variables */
5862 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005864
5865 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005866 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005867 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5868 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005869
5870 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005871 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005872 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5873 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005874 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5876 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005877#ifdef FEAT_TEXT_PROP
5878 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5879 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5880 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005881 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005882 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005883 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5884 NULL, NULL);
5885#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005887 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005888 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005889 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5890 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005892 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005893
5894 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005897 /* named functions (matters for closures) */
5898 abort = abort || set_ref_in_functions(copyID);
5899
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005900 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005901 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005902
Bram Moolenaard812df62008-11-09 12:46:09 +00005903 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005904 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005905
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005906 // callbacks in buffers
5907 abort = abort || set_ref_in_buffers(copyID);
5908
Bram Moolenaar1dced572012-04-05 16:54:08 +02005909#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005910 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005911#endif
5912
Bram Moolenaardb913952012-06-29 12:54:53 +02005913#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005914 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005915#endif
5916
5917#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005918 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005919#endif
5920
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005921#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005922 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005923 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005924#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005925#ifdef FEAT_NETBEANS_INTG
5926 abort = abort || set_ref_in_nb_channel(copyID);
5927#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005928
Bram Moolenaare3188e22016-05-31 21:13:04 +02005929#ifdef FEAT_TIMERS
5930 abort = abort || set_ref_in_timer(copyID);
5931#endif
5932
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005933#ifdef FEAT_QUICKFIX
5934 abort = abort || set_ref_in_quickfix(copyID);
5935#endif
5936
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005937#ifdef FEAT_TERMINAL
5938 abort = abort || set_ref_in_term(copyID);
5939#endif
5940
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005941#ifdef FEAT_TEXT_PROP
5942 abort = abort || set_ref_in_popups(copyID);
5943#endif
5944
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005945 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005946 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005947 /*
5948 * 2. Free lists and dictionaries that are not referenced.
5949 */
5950 did_free = free_unref_items(copyID);
5951
5952 /*
5953 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005954 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005955 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005957 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005958 else if (p_verbose > 0)
5959 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005960 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005961 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005962
5963 return did_free;
5964}
5965
5966/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005967 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005968 */
5969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005971{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005972 int did_free = FALSE;
5973
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005974 /* Let all "free" functions know that we are here. This means no
5975 * dictionaries, lists, channels or jobs are to be freed, because we will
5976 * do that here. */
5977 in_free_unref_items = TRUE;
5978
5979 /*
5980 * PASS 1: free the contents of the items. We don't free the items
5981 * themselves yet, so that it is possible to decrement refcount counters
5982 */
5983
Bram Moolenaarcd524592016-07-17 14:57:05 +02005984 /* Go through the list of dicts and free items without the copyID. */
5985 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005986
Bram Moolenaarda861d62016-07-17 15:46:27 +02005987 /* Go through the list of lists and free items without the copyID. */
5988 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005989
5990#ifdef FEAT_JOB_CHANNEL
5991 /* Go through the list of jobs and free items without the copyID. This
5992 * must happen before doing channels, because jobs refer to channels, but
5993 * the reference from the channel to the job isn't tracked. */
5994 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5995
5996 /* Go through the list of channels and free items without the copyID. */
5997 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5998#endif
5999
6000 /*
6001 * PASS 2: free the items themselves.
6002 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006003 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02006004 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006005
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006006#ifdef FEAT_JOB_CHANNEL
6007 /* Go through the list of jobs and free items without the copyID. This
6008 * must happen before doing channels, because jobs refer to channels, but
6009 * the reference from the channel to the job isn't tracked. */
6010 free_unused_jobs(copyID, COPYID_MASK);
6011
6012 /* Go through the list of channels and free items without the copyID. */
6013 free_unused_channels(copyID, COPYID_MASK);
6014#endif
6015
6016 in_free_unref_items = FALSE;
6017
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 return did_free;
6019}
6020
6021/*
6022 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006023 * "list_stack" is used to add lists to be marked. Can be NULL.
6024 *
6025 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006029{
6030 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006031 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006033 hashtab_T *cur_ht;
6034 ht_stack_T *ht_stack = NULL;
6035 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006037 cur_ht = ht;
6038 for (;;)
6039 {
6040 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006042 /* Mark each item in the hashtab. If the item contains a hashtab
6043 * it is added to ht_stack, if it contains a list it is added to
6044 * list_stack. */
6045 todo = (int)cur_ht->ht_used;
6046 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6047 if (!HASHITEM_EMPTY(hi))
6048 {
6049 --todo;
6050 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6051 &ht_stack, list_stack);
6052 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006053 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006054
6055 if (ht_stack == NULL)
6056 break;
6057
6058 /* take an item from the stack */
6059 cur_ht = ht_stack->ht;
6060 tempitem = ht_stack;
6061 ht_stack = ht_stack->prev;
6062 free(tempitem);
6063 }
6064
6065 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006066}
6067
6068/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006069 * Mark a dict and its items with "copyID".
6070 * Returns TRUE if setting references failed somehow.
6071 */
6072 int
6073set_ref_in_dict(dict_T *d, int copyID)
6074{
6075 if (d != NULL && d->dv_copyID != copyID)
6076 {
6077 d->dv_copyID = copyID;
6078 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6079 }
6080 return FALSE;
6081}
6082
6083/*
6084 * Mark a list and its items with "copyID".
6085 * Returns TRUE if setting references failed somehow.
6086 */
6087 int
6088set_ref_in_list(list_T *ll, int copyID)
6089{
6090 if (ll != NULL && ll->lv_copyID != copyID)
6091 {
6092 ll->lv_copyID = copyID;
6093 return set_ref_in_list_items(ll, copyID, NULL);
6094 }
6095 return FALSE;
6096}
6097
6098/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006099 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006100 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6101 *
6102 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006103 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006104 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006105set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006107 listitem_T *li;
6108 int abort = FALSE;
6109 list_T *cur_l;
6110 list_stack_T *list_stack = NULL;
6111 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006113 cur_l = l;
6114 for (;;)
6115 {
6116 if (!abort)
6117 /* Mark each item in the list. If the item contains a hashtab
6118 * it is added to ht_stack, if it contains a list it is added to
6119 * list_stack. */
6120 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6121 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6122 ht_stack, &list_stack);
6123 if (list_stack == NULL)
6124 break;
6125
6126 /* take an item from the stack */
6127 cur_l = list_stack->list;
6128 tempitem = list_stack;
6129 list_stack = list_stack->prev;
6130 free(tempitem);
6131 }
6132
6133 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006134}
6135
6136/*
6137 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006138 * "list_stack" is used to add lists to be marked. Can be NULL.
6139 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6140 *
6141 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006142 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006143 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006144set_ref_in_item(
6145 typval_T *tv,
6146 int copyID,
6147 ht_stack_T **ht_stack,
6148 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006149{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006150 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006151
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006152 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006153 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006154 dict_T *dd = tv->vval.v_dict;
6155
Bram Moolenaara03f2332016-02-06 18:09:59 +01006156 if (dd != NULL && dd->dv_copyID != copyID)
6157 {
6158 /* Didn't see this dict yet. */
6159 dd->dv_copyID = copyID;
6160 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006161 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006162 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6163 }
6164 else
6165 {
6166 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
6167 if (newitem == NULL)
6168 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006169 else
6170 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006171 newitem->ht = &dd->dv_hashtab;
6172 newitem->prev = *ht_stack;
6173 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006174 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006175 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006176 }
6177 }
6178 else if (tv->v_type == VAR_LIST)
6179 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006180 list_T *ll = tv->vval.v_list;
6181
Bram Moolenaara03f2332016-02-06 18:09:59 +01006182 if (ll != NULL && ll->lv_copyID != copyID)
6183 {
6184 /* Didn't see this list yet. */
6185 ll->lv_copyID = copyID;
6186 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006187 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006188 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006189 }
6190 else
6191 {
6192 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006193 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006194 if (newitem == NULL)
6195 abort = TRUE;
6196 else
6197 {
6198 newitem->list = ll;
6199 newitem->prev = *list_stack;
6200 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006201 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006202 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006203 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006204 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006205 else if (tv->v_type == VAR_FUNC)
6206 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006207 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006208 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006209 else if (tv->v_type == VAR_PARTIAL)
6210 {
6211 partial_T *pt = tv->vval.v_partial;
6212 int i;
6213
6214 /* A partial does not have a copyID, because it cannot contain itself.
6215 */
6216 if (pt != NULL)
6217 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006218 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006219
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006220 if (pt->pt_dict != NULL)
6221 {
6222 typval_T dtv;
6223
6224 dtv.v_type = VAR_DICT;
6225 dtv.vval.v_dict = pt->pt_dict;
6226 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6227 }
6228
6229 for (i = 0; i < pt->pt_argc; ++i)
6230 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6231 ht_stack, list_stack);
6232 }
6233 }
6234#ifdef FEAT_JOB_CHANNEL
6235 else if (tv->v_type == VAR_JOB)
6236 {
6237 job_T *job = tv->vval.v_job;
6238 typval_T dtv;
6239
6240 if (job != NULL && job->jv_copyID != copyID)
6241 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006242 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006243 if (job->jv_channel != NULL)
6244 {
6245 dtv.v_type = VAR_CHANNEL;
6246 dtv.vval.v_channel = job->jv_channel;
6247 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6248 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006249 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006250 {
6251 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006252 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006253 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6254 }
6255 }
6256 }
6257 else if (tv->v_type == VAR_CHANNEL)
6258 {
6259 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006260 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006261 typval_T dtv;
6262 jsonq_T *jq;
6263 cbq_T *cq;
6264
6265 if (ch != NULL && ch->ch_copyID != copyID)
6266 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006267 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006268 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006269 {
6270 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6271 jq = jq->jq_next)
6272 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6273 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6274 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006275 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006276 {
6277 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006278 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006279 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6280 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006281 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006282 {
6283 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006284 dtv.vval.v_partial =
6285 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006286 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6287 }
6288 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006289 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006290 {
6291 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006292 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006293 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6294 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006295 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006296 {
6297 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006298 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006299 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6300 }
6301 }
6302 }
6303#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006304 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006305}
6306
Bram Moolenaar17a13432016-01-24 14:22:10 +01006307 static char *
6308get_var_special_name(int nr)
6309{
6310 switch (nr)
6311 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006312 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006313 case VVAL_TRUE: return "v:true";
6314 case VVAL_NONE: return "v:none";
6315 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006316 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006317 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006318 return "42";
6319}
6320
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 * Return a string with the string representation of a variable.
6323 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006324 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006325 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006326 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6327 * stings as "string()", otherwise does not put quotes around strings, as
6328 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006329 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6330 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006331 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006333 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006334echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335 typval_T *tv,
6336 char_u **tofree,
6337 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006338 int copyID,
6339 int echo_style,
6340 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006341 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006343 static int recurse = 0;
6344 char_u *r = NULL;
6345
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006347 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006348 if (!did_echo_string_emsg)
6349 {
6350 /* Only give this message once for a recursive call to avoid
6351 * flooding the user with errors. And stop iterating over lists
6352 * and dicts. */
6353 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006354 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006355 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006357 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006358 }
6359 ++recurse;
6360
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 switch (tv->v_type)
6362 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006363 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006364 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006365 {
6366 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006367 r = tv->vval.v_string;
6368 if (r == NULL)
6369 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006370 }
6371 else
6372 {
6373 *tofree = string_quote(tv->vval.v_string, FALSE);
6374 r = *tofree;
6375 }
6376 break;
6377
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006379 if (echo_style)
6380 {
6381 *tofree = NULL;
6382 r = tv->vval.v_string;
6383 }
6384 else
6385 {
6386 *tofree = string_quote(tv->vval.v_string, TRUE);
6387 r = *tofree;
6388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006390
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006391 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006392 {
6393 partial_T *pt = tv->vval.v_partial;
6394 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006395 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006396 garray_T ga;
6397 int i;
6398 char_u *tf;
6399
6400 ga_init2(&ga, 1, 100);
6401 ga_concat(&ga, (char_u *)"function(");
6402 if (fname != NULL)
6403 {
6404 ga_concat(&ga, fname);
6405 vim_free(fname);
6406 }
6407 if (pt != NULL && pt->pt_argc > 0)
6408 {
6409 ga_concat(&ga, (char_u *)", [");
6410 for (i = 0; i < pt->pt_argc; ++i)
6411 {
6412 if (i > 0)
6413 ga_concat(&ga, (char_u *)", ");
6414 ga_concat(&ga,
6415 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6416 vim_free(tf);
6417 }
6418 ga_concat(&ga, (char_u *)"]");
6419 }
6420 if (pt != NULL && pt->pt_dict != NULL)
6421 {
6422 typval_T dtv;
6423
6424 ga_concat(&ga, (char_u *)", ");
6425 dtv.v_type = VAR_DICT;
6426 dtv.vval.v_dict = pt->pt_dict;
6427 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6428 vim_free(tf);
6429 }
6430 ga_concat(&ga, (char_u *)")");
6431
6432 *tofree = ga.ga_data;
6433 r = *tofree;
6434 break;
6435 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006436
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006437 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006438 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006439 break;
6440
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006442 if (tv->vval.v_list == NULL)
6443 {
6444 *tofree = NULL;
6445 r = NULL;
6446 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006447 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6448 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006449 {
6450 *tofree = NULL;
6451 r = (char_u *)"[...]";
6452 }
6453 else
6454 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006455 int old_copyID = tv->vval.v_list->lv_copyID;
6456
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006457 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006458 *tofree = list2string(tv, copyID, restore_copyID);
6459 if (restore_copyID)
6460 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006461 r = *tofree;
6462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006463 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006464
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006466 if (tv->vval.v_dict == NULL)
6467 {
6468 *tofree = NULL;
6469 r = NULL;
6470 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006471 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6472 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006473 {
6474 *tofree = NULL;
6475 r = (char_u *)"{...}";
6476 }
6477 else
6478 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006479 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006480 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006481 *tofree = dict2string(tv, copyID, restore_copyID);
6482 if (restore_copyID)
6483 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006484 r = *tofree;
6485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006488 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006489 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006490 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006491 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006492 break;
6493
Bram Moolenaar835dc632016-02-07 14:27:38 +01006494 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006495 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006497 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006498 if (composite_val)
6499 {
6500 *tofree = string_quote(r, FALSE);
6501 r = *tofree;
6502 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006504
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006505 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006506#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006507 *tofree = NULL;
6508 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6509 r = numbuf;
6510 break;
6511#endif
6512
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006513 case VAR_SPECIAL:
6514 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006515 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006516 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518
Bram Moolenaar8502c702014-06-17 12:51:16 +02006519 if (--recurse == 0)
6520 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006522}
6523
6524/*
6525 * Return a string with the string representation of a variable.
6526 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6527 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006528 * Does not put quotes around strings, as ":echo" displays values.
6529 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6530 * May return NULL.
6531 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006532 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006533echo_string(
6534 typval_T *tv,
6535 char_u **tofree,
6536 char_u *numbuf,
6537 int copyID)
6538{
6539 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6540}
6541
6542/*
6543 * Return a string with the string representation of a variable.
6544 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6545 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006546 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006547 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006548 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550tv2string(
6551 typval_T *tv,
6552 char_u **tofree,
6553 char_u *numbuf,
6554 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006555{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006556 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557}
6558
6559/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 * Return string "str" in ' quotes, doubling ' characters.
6561 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006563 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006568 char_u *p, *r, *s;
6569
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 len = (function ? 13 : 3);
6571 if (str != NULL)
6572 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006573 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006574 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 if (*p == '\'')
6576 ++len;
6577 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006578 s = r = alloc(len);
6579 if (r != NULL)
6580 {
6581 if (function)
6582 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006583 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006584 r += 10;
6585 }
6586 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 if (str != NULL)
6589 for (p = str; *p != NUL; )
6590 {
6591 if (*p == '\'')
6592 *r++ = '\'';
6593 MB_COPY_CHAR(p, r);
6594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006596 if (function)
6597 *r++ = ')';
6598 *r++ = NUL;
6599 }
6600 return s;
6601}
6602
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006603#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006604/*
6605 * Convert the string "text" to a floating point number.
6606 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6607 * this always uses a decimal point.
6608 * Returns the length of the text that was consumed.
6609 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006610 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006611string2float(
6612 char_u *text,
6613 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006614{
6615 char *s = (char *)text;
6616 float_T f;
6617
Bram Moolenaar62473612017-01-08 19:25:40 +01006618 /* MS-Windows does not deal with "inf" and "nan" properly. */
6619 if (STRNICMP(text, "inf", 3) == 0)
6620 {
6621 *value = INFINITY;
6622 return 3;
6623 }
6624 if (STRNICMP(text, "-inf", 3) == 0)
6625 {
6626 *value = -INFINITY;
6627 return 4;
6628 }
6629 if (STRNICMP(text, "nan", 3) == 0)
6630 {
6631 *value = NAN;
6632 return 3;
6633 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006634 f = strtod(s, &s);
6635 *value = f;
6636 return (int)((char_u *)s - text);
6637}
6638#endif
6639
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 * Get the value of an environment variable.
6642 * "arg" is pointing to the '$'. It is advanced to after the name.
6643 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006644 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 */
6646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 char_u *string = NULL;
6650 int len;
6651 int cc;
6652 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006653 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 ++*arg;
6656 name = *arg;
6657 len = get_env_len(arg);
6658 if (evaluate)
6659 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006660 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006661 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006662
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006663 cc = name[len];
6664 name[len] = NUL;
6665 /* first try vim_getenv(), fast for normal environment vars */
6666 string = vim_getenv(name, &mustfree);
6667 if (string != NULL && *string != NUL)
6668 {
6669 if (!mustfree)
6670 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006672 else
6673 {
6674 if (mustfree)
6675 vim_free(string);
6676
6677 /* next try expanding things like $VIM and ${HOME} */
6678 string = expand_env_save(name - 1);
6679 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006680 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006681 }
6682 name[len] = cc;
6683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006684 rettv->v_type = VAR_STRING;
6685 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 }
6687
6688 return OK;
6689}
6690
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006691
6692
6693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006695 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698var2fpos(
6699 typval_T *varp,
6700 int dollar_lnum, /* TRUE when $ is last line */
6701 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006703 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006705 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
Bram Moolenaara5525202006-03-02 22:52:09 +00006707 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006708 if (varp->v_type == VAR_LIST)
6709 {
6710 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006711 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006712 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006713 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006714
6715 l = varp->vval.v_list;
6716 if (l == NULL)
6717 return NULL;
6718
6719 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006720 pos.lnum = list_find_nr(l, 0L, &error);
6721 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006722 return NULL; /* invalid line number */
6723
6724 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006725 pos.col = list_find_nr(l, 1L, &error);
6726 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006727 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006728 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006729
6730 /* We accept "$" for the column number: last column. */
6731 li = list_find(l, 1L);
6732 if (li != NULL && li->li_tv.v_type == VAR_STRING
6733 && li->li_tv.vval.v_string != NULL
6734 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6735 pos.col = len + 1;
6736
Bram Moolenaara5525202006-03-02 22:52:09 +00006737 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006738 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006739 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006740 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006741
Bram Moolenaara5525202006-03-02 22:52:09 +00006742 /* Get the virtual offset. Defaults to zero. */
6743 pos.coladd = list_find_nr(l, 2L, &error);
6744 if (error)
6745 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006746
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006747 return &pos;
6748 }
6749
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006750 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006751 if (name == NULL)
6752 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006753 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006755 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6756 {
6757 if (VIsual_active)
6758 return &VIsual;
6759 return &curwin->w_cursor;
6760 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006761 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006763 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6765 return NULL;
6766 return pp;
6767 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006768
Bram Moolenaara5525202006-03-02 22:52:09 +00006769 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006770
Bram Moolenaar477933c2007-07-17 14:32:23 +00006771 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006772 {
6773 pos.col = 0;
6774 if (name[1] == '0') /* "w0": first visible line */
6775 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006776 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006777 /* In silent Ex mode topline is zero, but that's not a valid line
6778 * number; use one instead. */
6779 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006780 return &pos;
6781 }
6782 else if (name[1] == '$') /* "w$": last visible line */
6783 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006784 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006785 /* In silent Ex mode botline is zero, return zero then. */
6786 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006787 return &pos;
6788 }
6789 }
6790 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006792 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 pos.lnum = curbuf->b_ml.ml_line_count;
6795 pos.col = 0;
6796 }
6797 else
6798 {
6799 pos.lnum = curwin->w_cursor.lnum;
6800 pos.col = (colnr_T)STRLEN(ml_get_curline());
6801 }
6802 return &pos;
6803 }
6804 return NULL;
6805}
6806
6807/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006808 * Convert list in "arg" into a position and optional file number.
6809 * When "fnump" is NULL there is no file number, only 3 items.
6810 * Note that the column is passed on as-is, the caller may want to decrement
6811 * it to use 1 for the first column.
6812 * Return FAIL when conversion is not possible, doesn't check the position for
6813 * validity.
6814 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006816list2fpos(
6817 typval_T *arg,
6818 pos_T *posp,
6819 int *fnump,
6820 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006821{
6822 list_T *l = arg->vval.v_list;
6823 long i = 0;
6824 long n;
6825
Bram Moolenaar493c1782014-05-28 14:34:46 +02006826 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6827 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006828 if (arg->v_type != VAR_LIST
6829 || l == NULL
6830 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006831 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006832 return FAIL;
6833
6834 if (fnump != NULL)
6835 {
6836 n = list_find_nr(l, i++, NULL); /* fnum */
6837 if (n < 0)
6838 return FAIL;
6839 if (n == 0)
6840 n = curbuf->b_fnum; /* current buffer */
6841 *fnump = n;
6842 }
6843
6844 n = list_find_nr(l, i++, NULL); /* lnum */
6845 if (n < 0)
6846 return FAIL;
6847 posp->lnum = n;
6848
6849 n = list_find_nr(l, i++, NULL); /* col */
6850 if (n < 0)
6851 return FAIL;
6852 posp->col = n;
6853
Bram Moolenaar493c1782014-05-28 14:34:46 +02006854 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006855 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006856 posp->coladd = 0;
6857 else
6858 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006859
Bram Moolenaar493c1782014-05-28 14:34:46 +02006860 if (curswantp != NULL)
6861 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6862
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006863 return OK;
6864}
6865
6866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 * Get the length of an environment variable name.
6868 * Advance "arg" to the first character after the name.
6869 * Return 0 for error.
6870 */
6871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 char_u *p;
6875 int len;
6876
6877 for (p = *arg; vim_isIDc(*p); ++p)
6878 ;
6879 if (p == *arg) /* no name found */
6880 return 0;
6881
6882 len = (int)(p - *arg);
6883 *arg = p;
6884 return len;
6885}
6886
6887/*
6888 * Get the length of the name of a function or internal variable.
6889 * "arg" is advanced to the first non-white character after the name.
6890 * Return 0 if something is wrong.
6891 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006893get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 char_u *p;
6896 int len;
6897
6898 /* Find the end of the name. */
6899 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006900 {
6901 if (*p == ':')
6902 {
6903 /* "s:" is start of "s:var", but "n:" is not and can be used in
6904 * slice "[n:]". Also "xx:" is not a namespace. */
6905 len = (int)(p - *arg);
6906 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6907 || len > 1)
6908 break;
6909 }
6910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 if (p == *arg) /* no name found */
6912 return 0;
6913
6914 len = (int)(p - *arg);
6915 *arg = skipwhite(p);
6916
6917 return len;
6918}
6919
6920/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006921 * Get the length of the name of a variable or function.
6922 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006924 * Return -1 if curly braces expansion failed.
6925 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 * If the name contains 'magic' {}'s, expand them and return the
6927 * expanded name in an allocated string via 'alias' - caller must free.
6928 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006930get_name_len(
6931 char_u **arg,
6932 char_u **alias,
6933 int evaluate,
6934 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 char_u *p;
6938 char_u *expr_start;
6939 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 *alias = NULL; /* default to no alias */
6942
6943 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6944 && (*arg)[2] == (int)KE_SNR)
6945 {
6946 /* hard coded <SNR>, already translated */
6947 *arg += 3;
6948 return get_id_len(arg) + 3;
6949 }
6950 len = eval_fname_script(*arg);
6951 if (len > 0)
6952 {
6953 /* literal "<SID>", "s:" or "<SNR>" */
6954 *arg += len;
6955 }
6956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006958 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006960 p = find_name_end(*arg, &expr_start, &expr_end,
6961 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (expr_start != NULL)
6963 {
6964 char_u *temp_string;
6965
6966 if (!evaluate)
6967 {
6968 len += (int)(p - *arg);
6969 *arg = skipwhite(p);
6970 return len;
6971 }
6972
6973 /*
6974 * Include any <SID> etc in the expanded string:
6975 * Thus the -len here.
6976 */
6977 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6978 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006979 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 *alias = temp_string;
6981 *arg = skipwhite(p);
6982 return (int)STRLEN(temp_string);
6983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006986 // Only give an error when there is something, otherwise it will be
6987 // reported at a higher level.
6988 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006989 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 return len;
6992}
6993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994/*
6995 * Find the end of a variable or function name, taking care of magic braces.
6996 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6997 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006998 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006999 * Return a pointer to just after the name. Equal to "arg" if there is no
7000 * valid name.
7001 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007002 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007003find_name_end(
7004 char_u *arg,
7005 char_u **expr_start,
7006 char_u **expr_end,
7007 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009 int mb_nest = 0;
7010 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007012 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007016 *expr_start = NULL;
7017 *expr_end = NULL;
7018 }
7019
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007020 /* Quick check for valid starting character. */
7021 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
7022 return arg;
7023
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007024 for (p = arg; *p != NUL
7025 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007026 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007027 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007028 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007029 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007031 if (*p == '\'')
7032 {
7033 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007034 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007035 ;
7036 if (*p == NUL)
7037 break;
7038 }
7039 else if (*p == '"')
7040 {
7041 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007042 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007043 if (*p == '\\' && p[1] != NUL)
7044 ++p;
7045 if (*p == NUL)
7046 break;
7047 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007048 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7049 {
7050 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007051 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007052 len = (int)(p - arg);
7053 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007054 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007055 break;
7056 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007057
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007058 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007060 if (*p == '[')
7061 ++br_nest;
7062 else if (*p == ']')
7063 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007065
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007068 if (*p == '{')
7069 {
7070 mb_nest++;
7071 if (expr_start != NULL && *expr_start == NULL)
7072 *expr_start = p;
7073 }
7074 else if (*p == '}')
7075 {
7076 mb_nest--;
7077 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7078 *expr_end = p;
7079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 }
7082
7083 return p;
7084}
7085
7086/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007087 * Expands out the 'magic' {}'s in a variable/function name.
7088 * Note that this can call itself recursively, to deal with
7089 * constructs like foo{bar}{baz}{bam}
7090 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7091 * "in_start" ^
7092 * "expr_start" ^
7093 * "expr_end" ^
7094 * "in_end" ^
7095 *
7096 * Returns a new allocated string, which the caller must free.
7097 * Returns NULL for failure.
7098 */
7099 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007100make_expanded_name(
7101 char_u *in_start,
7102 char_u *expr_start,
7103 char_u *expr_end,
7104 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007105{
7106 char_u c1;
7107 char_u *retval = NULL;
7108 char_u *temp_result;
7109 char_u *nextcmd = NULL;
7110
7111 if (expr_end == NULL || in_end == NULL)
7112 return NULL;
7113 *expr_start = NUL;
7114 *expr_end = NUL;
7115 c1 = *in_end;
7116 *in_end = NUL;
7117
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007118 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007119 if (temp_result != NULL && nextcmd == NULL)
7120 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007121 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7122 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007123 if (retval != NULL)
7124 {
7125 STRCPY(retval, in_start);
7126 STRCAT(retval, temp_result);
7127 STRCAT(retval, expr_end + 1);
7128 }
7129 }
7130 vim_free(temp_result);
7131
7132 *in_end = c1; /* put char back for error messages */
7133 *expr_start = '{';
7134 *expr_end = '}';
7135
7136 if (retval != NULL)
7137 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007138 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007139 if (expr_start != NULL)
7140 {
7141 /* Further expansion! */
7142 temp_result = make_expanded_name(retval, expr_start,
7143 expr_end, temp_result);
7144 vim_free(retval);
7145 retval = temp_result;
7146 }
7147 }
7148
7149 return retval;
7150}
7151
7152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007157eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007159 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
7160}
7161
7162/*
7163 * Return TRUE if character "c" can be used as the first character in a
7164 * variable or function name (excluding '{' and '}').
7165 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007168{
7169 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170}
7171
7172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 * Set number v: variable to "val".
7174 */
7175 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007176set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
7181/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007182 * Get typval_T v: variable value.
7183 */
7184 typval_T *
7185get_vim_var_tv(int idx)
7186{
7187 return &vimvars[idx].vv_tv;
7188}
7189
7190/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007191 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007193 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197}
7198
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007199/*
7200 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007201 * If the String variable has never been set, return an empty string.
7202 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007203 */
7204 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007206{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007207 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007208}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007211 * Get List v: variable value. Caller must take care of reference count when
7212 * needed.
7213 */
7214 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007216{
7217 return vimvars[idx].vv_list;
7218}
7219
7220/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007221 * Get Dict v: variable value. Caller must take care of reference count when
7222 * needed.
7223 */
7224 dict_T *
7225get_vim_var_dict(int idx)
7226{
7227 return vimvars[idx].vv_dict;
7228}
7229
7230/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007231 * Set v:char to character "c".
7232 */
7233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007235{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007236 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007237
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007238 if (has_mbyte)
7239 buf[(*mb_char2bytes)(c, buf)] = NUL;
7240 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007241 {
7242 buf[0] = c;
7243 buf[1] = NUL;
7244 }
7245 set_vim_var_string(VV_CHAR, buf, -1);
7246}
7247
7248/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007249 * Set v:count to "count" and v:count1 to "count1".
7250 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 */
7252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007253set_vcount(
7254 long count,
7255 long count1,
7256 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007258 if (set_prevcount)
7259 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260 vimvars[VV_COUNT].vv_nr = count;
7261 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262}
7263
7264/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007265 * Save variables that might be changed as a side effect. Used when executing
7266 * a timer callback.
7267 */
7268 void
7269save_vimvars(vimvars_save_T *vvsave)
7270{
7271 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7272 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7273 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7274}
7275
7276/*
7277 * Restore variables saved by save_vimvars().
7278 */
7279 void
7280restore_vimvars(vimvars_save_T *vvsave)
7281{
7282 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7283 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7284 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7285}
7286
7287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 * Set string v: variable to a copy of "val".
7289 */
7290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007291set_vim_var_string(
7292 int idx,
7293 char_u *val,
7294 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
Bram Moolenaara542c682016-01-31 16:28:04 +01007296 clear_tv(&vimvars[idx].vv_di.di_tv);
7297 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304}
7305
7306/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007307 * Set List v: variable to "val".
7308 */
7309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007310set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007311{
Bram Moolenaara542c682016-01-31 16:28:04 +01007312 clear_tv(&vimvars[idx].vv_di.di_tv);
7313 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007314 vimvars[idx].vv_list = val;
7315 if (val != NULL)
7316 ++val->lv_refcount;
7317}
7318
7319/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007320 * Set Dictionary v: variable to "val".
7321 */
7322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007323set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007324{
Bram Moolenaara542c682016-01-31 16:28:04 +01007325 clear_tv(&vimvars[idx].vv_di.di_tv);
7326 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007327 vimvars[idx].vv_dict = val;
7328 if (val != NULL)
7329 {
7330 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007331 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007332 }
7333}
7334
7335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 * Set v:register if needed.
7337 */
7338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007339set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341 char_u regname;
7342
7343 if (c == 0 || c == ' ')
7344 regname = '"';
7345 else
7346 regname = c;
7347 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 set_vim_var_string(VV_REG, &regname, 1);
7350}
7351
7352/*
7353 * Get or set v:exception. If "oldval" == NULL, return the current value.
7354 * Otherwise, restore the value to "oldval" and return NULL.
7355 * Must always be called in pairs to save and restore v:exception! Does not
7356 * take care of memory allocations.
7357 */
7358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007359v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
7361 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaare9a41262005-01-15 22:18:47 +00007364 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 return NULL;
7366}
7367
7368/*
7369 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7370 * Otherwise, restore the value to "oldval" and return NULL.
7371 * Must always be called in pairs to save and restore v:throwpoint! Does not
7372 * take care of memory allocations.
7373 */
7374 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007375v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
7377 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 return NULL;
7382}
7383
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384/*
7385 * Set v:cmdarg.
7386 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7387 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7388 * Must always be called in pairs!
7389 */
7390 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007391set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393 char_u *oldval;
7394 char_u *newval;
7395 unsigned len;
7396
Bram Moolenaare9a41262005-01-15 22:18:47 +00007397 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007398 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007400 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 }
7404
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007405 if (eap->force_bin == FORCE_BIN)
7406 len = 6;
7407 else if (eap->force_bin == FORCE_NOBIN)
7408 len = 8;
7409 else
7410 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007411
7412 if (eap->read_edit)
7413 len += 7;
7414
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007415 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007416 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007417 if (eap->force_enc != 0)
7418 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007419 if (eap->bad_char != 0)
7420 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007421
7422 newval = alloc(len + 1);
7423 if (newval == NULL)
7424 return NULL;
7425
7426 if (eap->force_bin == FORCE_BIN)
7427 sprintf((char *)newval, " ++bin");
7428 else if (eap->force_bin == FORCE_NOBIN)
7429 sprintf((char *)newval, " ++nobin");
7430 else
7431 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007432
7433 if (eap->read_edit)
7434 STRCAT(newval, " ++edit");
7435
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007436 if (eap->force_ff != 0)
7437 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007438 eap->force_ff == 'u' ? "unix"
7439 : eap->force_ff == 'd' ? "dos"
7440 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007441 if (eap->force_enc != 0)
7442 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7443 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007444 if (eap->bad_char == BAD_KEEP)
7445 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7446 else if (eap->bad_char == BAD_DROP)
7447 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7448 else if (eap->bad_char != 0)
7449 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007450 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007451 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454/*
7455 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007456 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007459get_var_tv(
7460 char_u *name,
7461 int len, /* length of "name" */
7462 typval_T *rettv, /* NULL when only checking existence */
7463 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7464 int verbose, /* may give error message */
7465 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
7467 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007469 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472 /* truncate the name, so that we can use strcmp() */
7473 cc = name[len];
7474 name[len] = NUL;
7475
7476 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 * Check for user-defined variables.
7478 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007479 v = find_var(name, NULL, no_autoload);
7480 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007482 tv = &v->di_tv;
7483 if (dip != NULL)
7484 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007489 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007490 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 ret = FAIL;
7492 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007493 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
7496 name[len] = cc;
7497
7498 return ret;
7499}
7500
7501/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007502 * Check if variable "name[len]" is a local variable or an argument.
7503 * If so, "*eval_lavars_used" is set to TRUE.
7504 */
7505 static void
7506check_vars(char_u *name, int len)
7507{
7508 int cc;
7509 char_u *varname;
7510 hashtab_T *ht;
7511
7512 if (eval_lavars_used == NULL)
7513 return;
7514
7515 /* truncate the name, so that we can use strcmp() */
7516 cc = name[len];
7517 name[len] = NUL;
7518
7519 ht = find_var_ht(name, &varname);
7520 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7521 {
7522 if (find_var(name, NULL, TRUE) != NULL)
7523 *eval_lavars_used = TRUE;
7524 }
7525
7526 name[len] = cc;
7527}
7528
7529/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007530 * Handle:
7531 * - expr[expr], expr[expr:expr] subscript
7532 * - ".name" lookup
7533 * - function call with Funcref variable: func(expr)
7534 * - method call: var->method()
7535 *
7536 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007537 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007539handle_subscript(
7540 char_u **arg,
7541 typval_T *rettv,
7542 int evaluate, /* do more than finding the end */
7543 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007544{
7545 int ret = OK;
7546 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007547
Bram Moolenaar61343f02019-07-20 21:11:13 +02007548 // "." is ".name" lookup when we found a dict or when evaluating and
7549 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007550 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02007551 && (((**arg == '['
7552 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02007553 || (!evaluate
7554 && (*arg)[1] != '.'
7555 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007556 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007557 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007558 && !VIM_ISWHITE(*(*arg - 1)))
7559 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007560 {
7561 if (**arg == '(')
7562 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007563 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007564
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007565 // Stop the expression evaluation when immediately aborting on
7566 // error, or when an interrupt occurred or an exception was thrown
7567 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007568 if (aborting())
7569 {
7570 if (ret == OK)
7571 clear_tv(rettv);
7572 ret = FAIL;
7573 }
7574 dict_unref(selfdict);
7575 selfdict = NULL;
7576 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007577 else if (**arg == '-')
7578 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007579 if ((*arg)[2] == '{')
7580 // expr->{lambda}()
7581 ret = eval_lambda(arg, rettv, evaluate, verbose);
7582 else
7583 // expr->name()
7584 ret = eval_method(arg, rettv, evaluate, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02007585 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007586 else /* **arg == '[' || **arg == '.' */
7587 {
7588 dict_unref(selfdict);
7589 if (rettv->v_type == VAR_DICT)
7590 {
7591 selfdict = rettv->vval.v_dict;
7592 if (selfdict != NULL)
7593 ++selfdict->dv_refcount;
7594 }
7595 else
7596 selfdict = NULL;
7597 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7598 {
7599 clear_tv(rettv);
7600 ret = FAIL;
7601 }
7602 }
7603 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007604
Bram Moolenaar1d429612016-05-24 15:44:17 +02007605 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7606 * Don't do this when "Func" is already a partial that was bound
7607 * explicitly (pt_auto is FALSE). */
7608 if (selfdict != NULL
7609 && (rettv->v_type == VAR_FUNC
7610 || (rettv->v_type == VAR_PARTIAL
7611 && (rettv->vval.v_partial->pt_auto
7612 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007613 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007614
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007615 dict_unref(selfdict);
7616 return ret;
7617}
7618
7619/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007620 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 * value).
7622 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007623 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007625{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007626 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007627}
7628
7629/*
7630 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 * The string "s" must have been allocated, it is consumed.
7632 * Return NULL for out of memory, the variable otherwise.
7633 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007634 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007635alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv = alloc_tv();
7640 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 rettv->v_type = VAR_STRING;
7643 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645 else
7646 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656 if (varp != NULL)
7657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 switch (varp->v_type)
7659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007661 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007662 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007663 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007664 vim_free(varp->vval.v_string);
7665 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007666 case VAR_PARTIAL:
7667 partial_unref(varp->vval.v_partial);
7668 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007669 case VAR_BLOB:
7670 blob_unref(varp->vval.v_blob);
7671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 case VAR_LIST:
7673 list_unref(varp->vval.v_list);
7674 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007675 case VAR_DICT:
7676 dict_unref(varp->vval.v_dict);
7677 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007678 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007680 job_unref(varp->vval.v_job);
7681 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007682#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007683 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007684#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007685 channel_unref(varp->vval.v_channel);
7686 break;
7687#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007688 case VAR_NUMBER:
7689 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007690 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007691 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007692 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 vim_free(varp);
7695 }
7696}
7697
7698/*
7699 * Free the memory for a variable value and set the value to NULL or 0.
7700 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (varp != NULL)
7705 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007706 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007708 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007709 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007710 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007711 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007712 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007714 case VAR_PARTIAL:
7715 partial_unref(varp->vval.v_partial);
7716 varp->vval.v_partial = NULL;
7717 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007718 case VAR_BLOB:
7719 blob_unref(varp->vval.v_blob);
7720 varp->vval.v_blob = NULL;
7721 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 case VAR_LIST:
7723 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 case VAR_DICT:
7727 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007728 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007730 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007731 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007732 varp->vval.v_number = 0;
7733 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007735#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007736 varp->vval.v_float = 0.0;
7737 break;
7738#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007739 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007741 job_unref(varp->vval.v_job);
7742 varp->vval.v_job = NULL;
7743#endif
7744 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007745 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007746#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007747 channel_unref(varp->vval.v_channel);
7748 varp->vval.v_channel = NULL;
7749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750 case VAR_UNKNOWN:
7751 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007753 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755}
7756
7757/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758 * Set the value of a variable to NULL without freeing items.
7759 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007761init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762{
7763 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765}
7766
7767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 * Get the number value of a variable.
7769 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007771 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007772 * caller of incompatible types: it sets *denote to TRUE if "denote"
7773 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007775 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007776tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007778 int error = FALSE;
7779
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007780 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007781}
7782
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007783 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007784tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007785{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007786 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007790 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007791 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007792 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007793#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007794 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795 break;
7796#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007798 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007799 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 break;
7801 case VAR_STRING:
7802 if (varp->vval.v_string != NULL)
7803 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007804 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007805 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007807 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007808 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007809 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007810 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007811 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007812 case VAR_SPECIAL:
7813 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7814 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007815 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007816#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007817 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007818 break;
7819#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007820 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007821#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007822 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007823 break;
7824#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007825 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007826 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007827 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007828 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832 if (denote == NULL) /* useful for values that must be unsigned */
7833 n = -1;
7834 else
7835 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007836 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007839#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007840 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007841tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007842{
7843 switch (varp->v_type)
7844 {
7845 case VAR_NUMBER:
7846 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007847 case VAR_FLOAT:
7848 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007849 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007850 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007851 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007852 break;
7853 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007854 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007855 break;
7856 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007857 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007858 break;
7859 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007860 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007861 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007862 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007863 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007864 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007865 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007866# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007867 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007868 break;
7869# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007870 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007871# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007872 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007873 break;
7874# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007875 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007876 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007877 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007878 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007879 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007880 break;
7881 }
7882 return 0;
7883}
7884#endif
7885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 * Get the string value of a variable.
7888 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007889 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7890 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 * If the String variable has never been set, return an empty string.
7892 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007893 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007894 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007896 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007897tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 static char_u mybuf[NUMBUFLEN];
7900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007901 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007904 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007907 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007908
7909 return res != NULL ? res : (char_u *)"";
7910}
7911
Bram Moolenaar7d647822014-04-05 21:28:56 +02007912/*
7913 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7914 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007915 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007916tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007917{
7918 static char_u mybuf[NUMBUFLEN];
7919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007920 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007921}
7922
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007923 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007924tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007925{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007929 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007930 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007931 return buf;
7932 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007933 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007934 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 break;
7936 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007937 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 break;
7939 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007940 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007941 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007943#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007944 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945 break;
7946#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 case VAR_STRING:
7948 if (varp->vval.v_string != NULL)
7949 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007950 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007951 case VAR_SPECIAL:
7952 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7953 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007954 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007955 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007956 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007957 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007958#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007959 {
7960 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007961 char *status;
7962
7963 if (job == NULL)
7964 return (char_u *)"no process";
7965 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007966 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007967 : "run";
7968# ifdef UNIX
7969 vim_snprintf((char *)buf, NUMBUFLEN,
7970 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007971# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007972 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007973 "process %ld %s",
7974 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007975 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007976# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007977 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007978 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7979# endif
7980 return buf;
7981 }
7982#endif
7983 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007984 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007985#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007986 {
7987 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007988 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007989
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007990 if (channel == NULL)
7991 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7992 else
7993 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007994 "channel %d %s", channel->ch_id, status);
7995 return buf;
7996 }
7997#endif
7998 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007999 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008000 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008003 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
8005
8006/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008007 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
8008 * string() on Dict, List, etc.
8009 */
8010 char_u *
8011tv_stringify(typval_T *varp, char_u *buf)
8012{
8013 if (varp->v_type == VAR_LIST
8014 || varp->v_type == VAR_DICT
8015 || varp->v_type == VAR_FUNC
8016 || varp->v_type == VAR_PARTIAL
8017 || varp->v_type == VAR_FLOAT)
8018 {
8019 typval_T tmp;
8020
8021 f_string(varp, &tmp);
8022 tv_get_string_buf(&tmp, buf);
8023 clear_tv(varp);
8024 *varp = tmp;
8025 return tmp.vval.v_string;
8026 }
8027 return tv_get_string_buf(varp, buf);
8028}
8029
8030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 * Find variable "name" in the list of variables.
8032 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008033 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00008034 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008037 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008038find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008042 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043
Bram Moolenaara7043832005-01-21 11:56:39 +00008044 ht = find_var_ht(name, &varname);
8045 if (htp != NULL)
8046 *htp = ht;
8047 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008049 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
8050 if (ret != NULL)
8051 return ret;
8052
8053 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008054 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02008058 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00008059 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008062find_var_in_ht(
8063 hashtab_T *ht,
8064 int htname,
8065 char_u *varname,
8066 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00008067{
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 hashitem_T *hi;
8069
8070 if (*varname == NUL)
8071 {
8072 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02008073 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008075 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 case 'g': return &globvars_var;
8077 case 'v': return &vimvars_var;
8078 case 'b': return &curbuf->b_bufvar;
8079 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008080 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008081 case 'l': return get_funccal_local_var();
8082 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 }
8084 return NULL;
8085 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008086
8087 hi = hash_find(ht, varname);
8088 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008089 {
8090 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008091 * worked find the variable again. Don't auto-load a script if it was
8092 * loaded already, otherwise it would be loaded every time when
8093 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008094 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008095 {
8096 /* Note: script_autoload() may make "hi" invalid. It must either
8097 * be obtained again or not used. */
8098 if (!script_autoload(varname, FALSE) || aborting())
8099 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008100 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008101 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008102 if (HASHITEM_EMPTY(hi))
8103 return NULL;
8104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008106}
8107
8108/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008109 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02008110 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00008111 * Set "varname" to the start of name without ':'.
8112 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008113 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008114find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008116 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008117 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008118
Bram Moolenaar73627d02015-08-11 15:46:09 +02008119 if (name[0] == NUL)
8120 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (name[1] != ':')
8122 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008123 /* The name must not start with a colon or #. */
8124 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 return NULL;
8126 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00008127
Bram Moolenaard2e716e2019-04-20 14:39:52 +02008128 // "version" is "v:version" in all scopes if scriptversion < 3.
8129 // Same for a few other variables marked with VV_COMPAT.
8130 if (current_sctx.sc_version < 3)
8131 {
8132 hi = hash_find(&compat_hashtab, name);
8133 if (!HASHITEM_EMPTY(hi))
8134 return &compat_hashtab;
8135 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00008136
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008137 ht = get_funccal_local_ht();
8138 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008140 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008143 if (*name == 'g') /* global variable */
8144 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02008145 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008146 if (vim_strchr(name + 2, ':') != NULL
8147 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008148 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008150 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008152 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008153 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008154 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 if (*name == 'v') /* v: variable */
8156 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008157 if (*name == 'a') /* a: function argument */
8158 return get_funccal_args_ht();
8159 if (*name == 'l') /* l: local function variable */
8160 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008162 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
8163 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 return NULL;
8165}
8166
8167/*
8168 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008169 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 * Returns NULL when it doesn't exist.
8171 */
8172 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008173get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
Bram Moolenaar33570922005-01-25 22:26:29 +00008175 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008177 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (v == NULL)
8179 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008180 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181}
8182
8183/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 * sourcing this script and when executing functions defined in the script.
8186 */
8187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008188new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
Bram Moolenaara7043832005-01-21 11:56:39 +00008190 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 hashtab_T *ht;
8192 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008193
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8195 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008196 /* Re-allocating ga_data means that an ht_array pointing to
8197 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008199 for (i = 1; i <= ga_scripts.ga_len; ++i)
8200 {
8201 ht = &SCRIPT_VARS(i);
8202 if (ht->ht_mask == HT_INIT_SIZE - 1)
8203 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008204 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008206 }
8207
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 while (ga_scripts.ga_len < id)
8209 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008210 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008211 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008212 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 }
8215 }
8216}
8217
8218/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8220 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 */
8222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008223init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008226 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008227 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008228 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008229 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 dict_var->di_tv.vval.v_dict = dict;
8231 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008232 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008233 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8234 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235}
8236
8237/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008238 * Unreference a dictionary initialized by init_var_dict().
8239 */
8240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008241unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008242{
8243 /* Now the dict needs to be freed if no one else is using it, go back to
8244 * normal reference counting. */
8245 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8246 dict_unref(dict);
8247}
8248
8249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008251 * Frees all allocated variables and the value they contain.
8252 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 */
8254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008255vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008256{
8257 vars_clear_ext(ht, TRUE);
8258}
8259
8260/*
8261 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8262 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008264vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
Bram Moolenaara7043832005-01-21 11:56:39 +00008266 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 hashitem_T *hi;
8268 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008271 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008272 for (hi = ht->ht_array; todo > 0; ++hi)
8273 {
8274 if (!HASHITEM_EMPTY(hi))
8275 {
8276 --todo;
8277
Bram Moolenaar33570922005-01-25 22:26:29 +00008278 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008279 * ht_array might change then. hash_clear() takes care of it
8280 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 v = HI2DI(hi);
8282 if (free_val)
8283 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008284 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008286 }
8287 }
8288 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008289 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290}
8291
Bram Moolenaara7043832005-01-21 11:56:39 +00008292/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 * Delete a variable from hashtab "ht" at item "hi".
8294 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008297delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008300
8301 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 clear_tv(&di->di_tv);
8303 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304}
8305
8306/*
8307 * List the value of one internal variable.
8308 */
8309 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008310list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008312 char_u *tofree;
8313 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008314 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008316 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008318 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320}
8321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008323list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008324 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008325 char_u *name,
8326 int type,
8327 char_u *string,
8328 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
Bram Moolenaar31859182007-08-14 20:41:13 +00008330 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8331 msg_start();
8332 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008334 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 msg_putchar(' ');
8336 msg_advance(22);
8337 if (type == VAR_NUMBER)
8338 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008339 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 msg_putchar('*');
8341 else if (type == VAR_LIST)
8342 {
8343 msg_putchar('[');
8344 if (*string == '[')
8345 ++string;
8346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 else if (type == VAR_DICT)
8348 {
8349 msg_putchar('{');
8350 if (*string == '{')
8351 ++string;
8352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 else
8354 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008357
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008358 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008359 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008360 if (*first)
8361 {
8362 msg_clr_eos();
8363 *first = FALSE;
8364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365}
8366
8367/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 * If the variable already exists, the value is updated.
8370 * Otherwise the variable is created.
8371 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008373set_var(
8374 char_u *name,
8375 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008376 int copy) // make copy of value in "tv"
8377{
8378 set_var_const(name, tv, copy, FALSE);
8379}
8380
8381/*
8382 * Set variable "name" to value in "tv".
8383 * If the variable already exists and "is_const" is FALSE the value is updated.
8384 * Otherwise the variable is created.
8385 */
8386 static void
8387set_var_const(
8388 char_u *name,
8389 typval_T *tv,
8390 int copy, // make copy of value in "tv"
8391 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
Bram Moolenaar33570922005-01-25 22:26:29 +00008393 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008395 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008397 ht = find_var_ht(name, &varname);
8398 if (ht == NULL || *varname == NUL)
8399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008400 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008401 return;
8402 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008403 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008404
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008405 /* Search in parent scope which is possible to reference from lambda */
8406 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008407 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008408
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008409 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8410 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008411 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008412
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008415 if (is_const)
8416 {
8417 emsg(_(e_cannot_mod));
8418 return;
8419 }
8420
Bram Moolenaar33570922005-01-25 22:26:29 +00008421 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008422 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008423 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008424 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008425
8426 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008427 * Handle setting internal v: variables separately where needed to
8428 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 */
8430 if (ht == &vimvarht)
8431 {
8432 if (v->di_tv.v_type == VAR_STRING)
8433 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008434 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008435 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008436 {
8437 char_u *val = tv_get_string(tv);
8438
8439 // Careful: when assigning to v:errmsg and tv_get_string()
8440 // causes an error message the variable will alrady be set.
8441 if (v->di_tv.vval.v_string == NULL)
8442 v->di_tv.vval.v_string = vim_strsave(val);
8443 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008444 else
8445 {
8446 /* Take over the string to avoid an extra alloc/free. */
8447 v->di_tv.vval.v_string = tv->vval.v_string;
8448 tv->vval.v_string = NULL;
8449 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008450 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008451 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008452 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008454 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455 if (STRCMP(varname, "searchforward") == 0)
8456 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008457#ifdef FEAT_SEARCH_EXTRA
8458 else if (STRCMP(varname, "hlsearch") == 0)
8459 {
8460 no_hlsearch = !v->di_tv.vval.v_number;
8461 redraw_all_later(SOME_VALID);
8462 }
8463#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008464 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008465 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008466 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008468 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008469 return;
8470 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 }
8472
8473 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 }
8475 else /* add a new variable */
8476 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008477 // Can't add "v:" or "a:" variable.
8478 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008480 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008481 return;
8482 }
8483
Bram Moolenaar31b81602019-02-10 22:14:27 +01008484 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008485 if (!valid_varname(varname))
8486 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008487
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008488 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008489 if (v == NULL)
8490 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008494 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 return;
8496 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008497 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008498 if (is_const)
8499 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008501
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008502 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008504 else
8505 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008507 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008509 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008510
8511 if (is_const)
8512 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513}
8514
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008516 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 * Also give an error message.
8518 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008520var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008521{
8522 if (flags & DI_FLAGS_RO)
8523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008524 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008525 return TRUE;
8526 }
8527 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008529 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 return TRUE;
8531 }
8532 return FALSE;
8533}
8534
8535/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008536 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8537 * Also give an error message.
8538 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008540var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008541{
8542 if (flags & DI_FLAGS_FIX)
8543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008544 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008545 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008546 return TRUE;
8547 }
8548 return FALSE;
8549}
8550
8551/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008552 * Check if a funcref is assigned to a valid variable name.
8553 * Return TRUE and give an error if not.
8554 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008556var_check_func_name(
8557 char_u *name, /* points to start of variable name */
8558 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008559{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008560 /* Allow for w: b: s: and t:. */
8561 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008562 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8563 ? name[2] : name[0]))
8564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008565 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008566 name);
8567 return TRUE;
8568 }
8569 /* Don't allow hiding a function. When "v" is not NULL we might be
8570 * assigning another function to the same var, the type is checked
8571 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008572 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008574 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008575 name);
8576 return TRUE;
8577 }
8578 return FALSE;
8579}
8580
8581/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008582 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008583 * Also give an error message, using "name" or _("name") when use_gettext is
8584 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008585 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008586 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008587var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008588{
8589 if (lock & VAR_LOCKED)
8590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008591 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008592 name == NULL ? (char_u *)_("Unknown")
8593 : use_gettext ? (char_u *)_(name)
8594 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008595 return TRUE;
8596 }
8597 if (lock & VAR_FIXED)
8598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008599 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008600 name == NULL ? (char_u *)_("Unknown")
8601 : use_gettext ? (char_u *)_(name)
8602 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008603 return TRUE;
8604 }
8605 return FALSE;
8606}
8607
8608/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008609 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8610 * Also give an error message, using "name" or _("name") when use_gettext is
8611 * TRUE.
8612 */
8613 static int
8614tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8615{
8616 int lock = 0;
8617
8618 switch (tv->v_type)
8619 {
8620 case VAR_BLOB:
8621 if (tv->vval.v_blob != NULL)
8622 lock = tv->vval.v_blob->bv_lock;
8623 break;
8624 case VAR_LIST:
8625 if (tv->vval.v_list != NULL)
8626 lock = tv->vval.v_list->lv_lock;
8627 break;
8628 case VAR_DICT:
8629 if (tv->vval.v_dict != NULL)
8630 lock = tv->vval.v_dict->dv_lock;
8631 break;
8632 default:
8633 break;
8634 }
8635 return var_check_lock(tv->v_lock, name, use_gettext)
8636 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8637}
8638
8639/*
8640 * Check if a variable name is valid.
8641 * Return FALSE and give an error if not.
8642 */
8643 int
8644valid_varname(char_u *varname)
8645{
8646 char_u *p;
8647
8648 for (p = varname; *p != NUL; ++p)
8649 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8650 && *p != AUTOLOAD_CHAR)
8651 {
8652 semsg(_(e_illvar), varname);
8653 return FALSE;
8654 }
8655 return TRUE;
8656}
8657
8658/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008661 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008662 * It is OK for "from" and "to" to point to the same item. This is used to
8663 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008664 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008666copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008669 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 switch (from->v_type)
8671 {
8672 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008673 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674 to->vval.v_number = from->vval.v_number;
8675 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008676 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008677#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008678 to->vval.v_float = from->vval.v_float;
8679 break;
8680#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008681 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008682#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008683 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008684 if (to->vval.v_job != NULL)
8685 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008686 break;
8687#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008688 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008689#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008690 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008691 if (to->vval.v_channel != NULL)
8692 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008693 break;
8694#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008695 case VAR_STRING:
8696 case VAR_FUNC:
8697 if (from->vval.v_string == NULL)
8698 to->vval.v_string = NULL;
8699 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008700 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008701 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008702 if (from->v_type == VAR_FUNC)
8703 func_ref(to->vval.v_string);
8704 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008705 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008706 case VAR_PARTIAL:
8707 if (from->vval.v_partial == NULL)
8708 to->vval.v_partial = NULL;
8709 else
8710 {
8711 to->vval.v_partial = from->vval.v_partial;
8712 ++to->vval.v_partial->pt_refcount;
8713 }
8714 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008715 case VAR_BLOB:
8716 if (from->vval.v_blob == NULL)
8717 to->vval.v_blob = NULL;
8718 else
8719 {
8720 to->vval.v_blob = from->vval.v_blob;
8721 ++to->vval.v_blob->bv_refcount;
8722 }
8723 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008724 case VAR_LIST:
8725 if (from->vval.v_list == NULL)
8726 to->vval.v_list = NULL;
8727 else
8728 {
8729 to->vval.v_list = from->vval.v_list;
8730 ++to->vval.v_list->lv_refcount;
8731 }
8732 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008733 case VAR_DICT:
8734 if (from->vval.v_dict == NULL)
8735 to->vval.v_dict = NULL;
8736 else
8737 {
8738 to->vval.v_dict = from->vval.v_dict;
8739 ++to->vval.v_dict->dv_refcount;
8740 }
8741 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008742 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008743 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008744 break;
8745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746}
8747
8748/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008749 * Make a copy of an item.
8750 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008751 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8752 * reference to an already copied list/dict can be used.
8753 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008756item_copy(
8757 typval_T *from,
8758 typval_T *to,
8759 int deep,
8760 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761{
8762 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008763 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008764
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008767 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008768 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008769 }
8770 ++recurse;
8771
8772 switch (from->v_type)
8773 {
8774 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008775 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008776 case VAR_STRING:
8777 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008778 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008779 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008780 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008781 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008782 copy_tv(from, to);
8783 break;
8784 case VAR_LIST:
8785 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008786 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008787 if (from->vval.v_list == NULL)
8788 to->vval.v_list = NULL;
8789 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8790 {
8791 /* use the copy made earlier */
8792 to->vval.v_list = from->vval.v_list->lv_copylist;
8793 ++to->vval.v_list->lv_refcount;
8794 }
8795 else
8796 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8797 if (to->vval.v_list == NULL)
8798 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008799 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008800 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008801 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008802 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 case VAR_DICT:
8804 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008805 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008806 if (from->vval.v_dict == NULL)
8807 to->vval.v_dict = NULL;
8808 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8809 {
8810 /* use the copy made earlier */
8811 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8812 ++to->vval.v_dict->dv_refcount;
8813 }
8814 else
8815 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8816 if (to->vval.v_dict == NULL)
8817 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008818 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008819 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008820 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008821 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008822 }
8823 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008824 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825}
8826
8827/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 * This function is used by f_input() and f_inputdialog() functions. The third
8829 * argument to f_input() specifies the type of completion to use at the
8830 * prompt. The third argument to f_inputdialog() specifies the value to return
8831 * when the user cancels the prompt.
8832 */
8833 void
8834get_user_input(
8835 typval_T *argvars,
8836 typval_T *rettv,
8837 int inputdialog,
8838 int secret)
8839{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008840 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 char_u *p = NULL;
8842 int c;
8843 char_u buf[NUMBUFLEN];
8844 int cmd_silent_save = cmd_silent;
8845 char_u *defstr = (char_u *)"";
8846 int xp_type = EXPAND_NOTHING;
8847 char_u *xp_arg = NULL;
8848
8849 rettv->v_type = VAR_STRING;
8850 rettv->vval.v_string = NULL;
8851
8852#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008853 /* While starting up, there is no place to enter text. When running tests
8854 * with --not-a-term we assume feedkeys() will be used. */
8855 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 return;
8857#endif
8858
8859 cmd_silent = FALSE; /* Want to see the prompt. */
8860 if (prompt != NULL)
8861 {
8862 /* Only the part of the message after the last NL is considered as
8863 * prompt for the command line */
8864 p = vim_strrchr(prompt, '\n');
8865 if (p == NULL)
8866 p = prompt;
8867 else
8868 {
8869 ++p;
8870 c = *p;
8871 *p = NUL;
8872 msg_start();
8873 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008874 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 msg_didout = FALSE;
8876 msg_starthere();
8877 *p = c;
8878 }
8879 cmdline_row = msg_row;
8880
8881 if (argvars[1].v_type != VAR_UNKNOWN)
8882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008883 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884 if (defstr != NULL)
8885 stuffReadbuffSpec(defstr);
8886
8887 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8888 {
8889 char_u *xp_name;
8890 int xp_namelen;
8891 long argt;
8892
8893 /* input() with a third argument: completion */
8894 rettv->vval.v_string = NULL;
8895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008896 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897 if (xp_name == NULL)
8898 return;
8899
8900 xp_namelen = (int)STRLEN(xp_name);
8901
8902 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8903 &xp_arg) == FAIL)
8904 return;
8905 }
8906 }
8907
8908 if (defstr != NULL)
8909 {
8910 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008911
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 ex_normal_busy = 0;
8913 rettv->vval.v_string =
8914 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8915 xp_type, xp_arg);
8916 ex_normal_busy = save_ex_normal_busy;
8917 }
8918 if (inputdialog && rettv->vval.v_string == NULL
8919 && argvars[1].v_type != VAR_UNKNOWN
8920 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008921 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 &argvars[2], buf));
8923
8924 vim_free(xp_arg);
8925
8926 /* since the user typed this, no need to wait for return */
8927 need_wait_return = FALSE;
8928 msg_didout = FALSE;
8929 }
8930 cmd_silent = cmd_silent_save;
8931}
8932
8933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 * ":echo expr1 ..." print each argument separated with a space, add a
8935 * newline at the end.
8936 * ":echon expr1 ..." print each argument plain.
8937 */
8938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008939ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
8941 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008943 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 char_u *p;
8945 int needclr = TRUE;
8946 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008947 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008948 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008949 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 if (eap->skip)
8952 ++emsg_skip;
8953 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008955 /* If eval1() causes an error message the text from the command may
8956 * still need to be cleared. E.g., "echo 22,44". */
8957 need_clr_eos = needclr;
8958
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 {
8962 /*
8963 * Report the invalid expression unless the expression evaluation
8964 * has been cancelled due to an aborting error, an interrupt, or an
8965 * exception.
8966 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008967 if (!aborting() && did_emsg == did_emsg_before
8968 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008969 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008970 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 break;
8972 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008973 need_clr_eos = FALSE;
8974
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (!eap->skip)
8976 {
8977 if (atstart)
8978 {
8979 atstart = FALSE;
8980 /* Call msg_start() after eval1(), evaluating the expression
8981 * may cause a message to appear. */
8982 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008983 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008984 /* Mark the saved text as finishing the line, so that what
8985 * follows is displayed on a new line when scrolling back
8986 * at the more prompt. */
8987 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 }
8991 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008992 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008993 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008994 if (p != NULL)
8995 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008997 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008999 if (*p != TAB && needclr)
9000 {
9001 /* remove any text still there from the command */
9002 msg_clr_eos();
9003 needclr = FALSE;
9004 }
9005 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 }
9007 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009008 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009009 if (has_mbyte)
9010 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009011 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009012
9013 (void)msg_outtrans_len_attr(p, i, echo_attr);
9014 p += i - 1;
9015 }
9016 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009017 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 arg = skipwhite(arg);
9024 }
9025 eap->nextcmd = check_nextcmd(arg);
9026
9027 if (eap->skip)
9028 --emsg_skip;
9029 else
9030 {
9031 /* remove text that may still be there from the command */
9032 if (needclr)
9033 msg_clr_eos();
9034 if (eap->cmdidx == CMD_echo)
9035 msg_end();
9036 }
9037}
9038
9039/*
9040 * ":echohl {name}".
9041 */
9042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009043ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02009045 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046}
9047
9048/*
9049 * ":execute expr1 ..." execute the result of an expression.
9050 * ":echomsg expr1 ..." Print a message
9051 * ":echoerr expr1 ..." Print an error
9052 * Each gets spaces around each argument and a newline at the end for
9053 * echo commands
9054 */
9055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009056ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 int ret = OK;
9061 char_u *p;
9062 garray_T ga;
9063 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01009064 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 ga_init2(&ga, 1, 80);
9067
9068 if (eap->skip)
9069 ++emsg_skip;
9070 while (*arg != NUL && *arg != '|' && *arg != '\n')
9071 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01009072 ret = eval1_emsg(&arg, &rettv, !eap->skip);
9073 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
9076 if (!eap->skip)
9077 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009078 char_u buf[NUMBUFLEN];
9079
9080 if (eap->cmdidx == CMD_execute)
9081 p = tv_get_string_buf(&rettv, buf);
9082 else
9083 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 len = (int)STRLEN(p);
9085 if (ga_grow(&ga, len + 2) == FAIL)
9086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 ret = FAIL;
9089 break;
9090 }
9091 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 ga.ga_len += len;
9095 }
9096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 arg = skipwhite(arg);
9099 }
9100
9101 if (ret != FAIL && ga.ga_data != NULL)
9102 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009103 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
9104 {
9105 /* Mark the already saved text as finishing the line, so that what
9106 * follows is displayed on a new line when scrolling back at the
9107 * more prompt. */
9108 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009109 }
9110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009112 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009113 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009114 out_flush();
9115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 else if (eap->cmdidx == CMD_echoerr)
9117 {
9118 /* We don't want to abort following commands, restore did_emsg. */
9119 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009120 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (!force_abort)
9122 did_emsg = save_did_emsg;
9123 }
9124 else if (eap->cmdidx == CMD_execute)
9125 do_cmdline((char_u *)ga.ga_data,
9126 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9127 }
9128
9129 ga_clear(&ga);
9130
9131 if (eap->skip)
9132 --emsg_skip;
9133
9134 eap->nextcmd = check_nextcmd(arg);
9135}
9136
9137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 * Find window specified by "vp" in tabpage "tp".
9139 */
9140 win_T *
9141find_win_by_nr(
9142 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009143 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009146 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 if (nr < 0)
9149 return NULL;
9150 if (nr == 0)
9151 return curwin;
9152
Bram Moolenaar29323592016-07-24 22:04:11 +02009153 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
9154 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155 if (nr >= LOWEST_WIN_ID)
9156 {
9157 if (wp->w_id == nr)
9158 return wp;
9159 }
9160 else if (--nr <= 0)
9161 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02009162 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009164 {
9165#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009166 // check tab-local popup windows
9167 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009168 if (wp->w_id == nr)
9169 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009170 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009171 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9172 if (wp->w_id == nr)
9173 return wp;
9174#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009175 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009176 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178}
9179
9180/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009181 * Find a window: When using a Window ID in any tab page, when using a number
9182 * in the current tab page.
9183 */
9184 win_T *
9185find_win_by_nr_or_id(typval_T *vp)
9186{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009187 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009188
9189 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009190 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009191 return find_win_by_nr(vp, NULL);
9192}
9193
9194/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009196 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 */
9198 win_T *
9199find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009200 typval_T *wvp, // VAR_UNKNOWN for current window
9201 typval_T *tvp, // VAR_UNKNOWN for current tab page
9202 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203{
9204 win_T *wp = NULL;
9205 tabpage_T *tp = NULL;
9206 long n;
9207
9208 if (wvp->v_type != VAR_UNKNOWN)
9209 {
9210 if (tvp->v_type != VAR_UNKNOWN)
9211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009212 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 if (n >= 0)
9214 tp = find_tabpage(n);
9215 }
9216 else
9217 tp = curtab;
9218
9219 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009220 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009222 if (wp == NULL && wvp->v_type == VAR_NUMBER
9223 && wvp->vval.v_number != -1)
9224 // A window with the specified number is not found
9225 tp = NULL;
9226 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009227 }
9228 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009229 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009231 tp = curtab;
9232 }
9233
9234 if (ptp != NULL)
9235 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236
9237 return wp;
9238}
9239
9240/*
9241 * getwinvar() and gettabwinvar()
9242 */
9243 void
9244getwinvar(
9245 typval_T *argvars,
9246 typval_T *rettv,
9247 int off) /* 1 for gettabwinvar() */
9248{
9249 win_T *win;
9250 char_u *varname;
9251 dictitem_T *v;
9252 tabpage_T *tp = NULL;
9253 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009254 win_T *oldcurwin;
9255 tabpage_T *oldtabpage;
9256 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009259 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260 else
9261 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009262 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009263 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 ++emsg_off;
9265
9266 rettv->v_type = VAR_STRING;
9267 rettv->vval.v_string = NULL;
9268
9269 if (win != NULL && varname != NULL)
9270 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009271 /* Set curwin to be our win, temporarily. Also set the tabpage,
9272 * otherwise the window is not valid. Only do this when needed,
9273 * autocommands get blocked. */
9274 need_switch_win = !(tp == curtab && win == curwin);
9275 if (!need_switch_win
9276 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009278 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009279 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009280 if (varname[1] == NUL)
9281 {
9282 /* get all window-local options in a dict */
9283 dict_T *opts = get_winbuf_options(FALSE);
9284
9285 if (opts != NULL)
9286 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009287 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009288 done = TRUE;
9289 }
9290 }
9291 else if (get_option_tv(&varname, rettv, 1) == OK)
9292 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 done = TRUE;
9294 }
9295 else
9296 {
9297 /* Look up the variable. */
9298 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9299 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9300 varname, FALSE);
9301 if (v != NULL)
9302 {
9303 copy_tv(&v->di_tv, rettv);
9304 done = TRUE;
9305 }
9306 }
9307 }
9308
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309 if (need_switch_win)
9310 /* restore previous notion of curwin */
9311 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312 }
9313
9314 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9315 /* use the default return value */
9316 copy_tv(&argvars[off + 2], rettv);
9317
9318 --emsg_off;
9319}
9320
9321/*
9322 * "setwinvar()" and "settabwinvar()" functions
9323 */
9324 void
9325setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9326{
9327 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 win_T *save_curwin;
9329 tabpage_T *save_curtab;
9330 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 char_u *varname, *winvarname;
9332 typval_T *varp;
9333 char_u nbuf[NUMBUFLEN];
9334 tabpage_T *tp = NULL;
9335
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009336 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337 return;
9338
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009340 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 else
9342 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009344 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 varp = &argvars[off + 2];
9346
9347 if (win != NULL && varname != NULL && varp != NULL)
9348 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009349 need_switch_win = !(tp == curtab && win == curwin);
9350 if (!need_switch_win
9351 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 {
9353 if (*varname == '&')
9354 {
9355 long numval;
9356 char_u *strval;
9357 int error = FALSE;
9358
9359 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009360 numval = (long)tv_get_number_chk(varp, &error);
9361 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 if (!error && strval != NULL)
9363 set_option_value(varname, numval, strval, OPT_LOCAL);
9364 }
9365 else
9366 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009367 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 if (winvarname != NULL)
9369 {
9370 STRCPY(winvarname, "w:");
9371 STRCPY(winvarname + 2, varname);
9372 set_var(winvarname, varp, TRUE);
9373 vim_free(winvarname);
9374 }
9375 }
9376 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 if (need_switch_win)
9378 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 }
9380}
9381
9382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9384 * "arg" points to the "&" or '+' when called, to "option" when returning.
9385 * Returns NULL when no option name found. Otherwise pointer to the char
9386 * after the option name.
9387 */
9388 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009389find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 char_u *p = *arg;
9392
9393 ++p;
9394 if (*p == 'g' && p[1] == ':')
9395 {
9396 *opt_flags = OPT_GLOBAL;
9397 p += 2;
9398 }
9399 else if (*p == 'l' && p[1] == ':')
9400 {
9401 *opt_flags = OPT_LOCAL;
9402 p += 2;
9403 }
9404 else
9405 *opt_flags = 0;
9406
9407 if (!ASCII_ISALPHA(*p))
9408 return NULL;
9409 *arg = p;
9410
9411 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9412 p += 4; /* termcap option */
9413 else
9414 while (ASCII_ISALPHA(*p))
9415 ++p;
9416 return p;
9417}
9418
9419/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009420 * Return the autoload script name for a function or variable name.
9421 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009422 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009424 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009425autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009426{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009427 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009428 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009429
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009430 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009431 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009432 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009433 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009434 STRCPY(scriptname, "autoload/");
9435 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009436 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9437 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009438 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009439 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009440 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009441}
9442
9443/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009444 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009445 * Return TRUE if a package was loaded.
9446 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009448script_autoload(
9449 char_u *name,
9450 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009451{
9452 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009453 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009454 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009455 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009456
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009457 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009458 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009459 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009460 return FALSE;
9461
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009462 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009463
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009464 /* Find the name in the list of previously loaded package names. Skip
9465 * "autoload/", it's always the same. */
9466 for (i = 0; i < ga_loaded.ga_len; ++i)
9467 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9468 break;
9469 if (!reload && i < ga_loaded.ga_len)
9470 ret = FALSE; /* was loaded already */
9471 else
9472 {
9473 /* Remember the name if it wasn't loaded already. */
9474 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9475 {
9476 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9477 tofree = NULL;
9478 }
9479
9480 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009481 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009482 ret = TRUE;
9483 }
9484
9485 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009486 return ret;
9487}
9488
Bram Moolenaar661b1822005-07-28 22:36:45 +00009489/*
9490 * Display script name where an item was last set.
9491 * Should only be invoked when 'verbose' is non-zero.
9492 */
9493 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009494last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009495{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009496 char_u *p;
9497
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009498 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009499 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009500 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009501 if (p != NULL)
9502 {
9503 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009504 msg_puts(_("\n\tLast set from "));
9505 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009506 if (script_ctx.sc_lnum > 0)
9507 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009508 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009509 msg_outnum((long)script_ctx.sc_lnum);
9510 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009511 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009512 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009513 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009514 }
9515}
9516
Bram Moolenaar61be3762019-03-19 23:04:17 +01009517/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009518 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9519 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009520 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009522reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009523{
9524 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9525 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009526 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9527 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009528 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009529 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009530}
9531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009533 * Add an assert error to v:errors.
9534 */
9535 void
9536assert_error(garray_T *gap)
9537{
9538 struct vimvar *vp = &vimvars[VV_ERRORS];
9539
9540 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9541 /* Make sure v:errors is a list. */
9542 set_vim_var_list(VV_ERRORS, list_alloc());
9543 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9544}
Bram Moolenaar31988702018-02-13 12:57:42 +01009545/*
9546 * Compare "typ1" and "typ2". Put the result in "typ1".
9547 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009548 int
9549typval_compare(
9550 typval_T *typ1, /* first operand */
9551 typval_T *typ2, /* second operand */
9552 exptype_T type, /* operator */
9553 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009554 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009555{
9556 int i;
9557 varnumber_T n1, n2;
9558 char_u *s1, *s2;
9559 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9560
Bram Moolenaar31988702018-02-13 12:57:42 +01009561 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009562 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009563 /* For "is" a different type always means FALSE, for "notis"
9564 * it means TRUE. */
9565 n1 = (type == TYPE_NEQUAL);
9566 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009567 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9568 {
9569 if (type_is)
9570 {
9571 n1 = (typ1->v_type == typ2->v_type
9572 && typ1->vval.v_blob == typ2->vval.v_blob);
9573 if (type == TYPE_NEQUAL)
9574 n1 = !n1;
9575 }
9576 else if (typ1->v_type != typ2->v_type
9577 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9578 {
9579 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009580 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009581 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009582 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009583 clear_tv(typ1);
9584 return FAIL;
9585 }
9586 else
9587 {
9588 // Compare two Blobs for being equal or unequal.
9589 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9590 if (type == TYPE_NEQUAL)
9591 n1 = !n1;
9592 }
9593 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009594 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9595 {
9596 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009597 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009598 n1 = (typ1->v_type == typ2->v_type
9599 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009600 if (type == TYPE_NEQUAL)
9601 n1 = !n1;
9602 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009603 else if (typ1->v_type != typ2->v_type
9604 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009605 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009606 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009607 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009608 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009609 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009610 clear_tv(typ1);
9611 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009612 }
9613 else
9614 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009615 /* Compare two Lists for being equal or unequal. */
9616 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9617 ic, FALSE);
9618 if (type == TYPE_NEQUAL)
9619 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009620 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009621 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009622
9623 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9624 {
9625 if (type_is)
9626 {
9627 n1 = (typ1->v_type == typ2->v_type
9628 && typ1->vval.v_dict == typ2->vval.v_dict);
9629 if (type == TYPE_NEQUAL)
9630 n1 = !n1;
9631 }
9632 else if (typ1->v_type != typ2->v_type
9633 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9634 {
9635 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009636 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009637 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009638 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009639 clear_tv(typ1);
9640 return FAIL;
9641 }
9642 else
9643 {
9644 /* Compare two Dictionaries for being equal or unequal. */
9645 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9646 ic, FALSE);
9647 if (type == TYPE_NEQUAL)
9648 n1 = !n1;
9649 }
9650 }
9651
9652 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9653 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9654 {
9655 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009657 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009658 clear_tv(typ1);
9659 return FAIL;
9660 }
9661 if ((typ1->v_type == VAR_PARTIAL
9662 && typ1->vval.v_partial == NULL)
9663 || (typ2->v_type == VAR_PARTIAL
9664 && typ2->vval.v_partial == NULL))
9665 /* when a partial is NULL assume not equal */
9666 n1 = FALSE;
9667 else if (type_is)
9668 {
9669 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9670 /* strings are considered the same if their value is
9671 * the same */
9672 n1 = tv_equal(typ1, typ2, ic, FALSE);
9673 else if (typ1->v_type == VAR_PARTIAL
9674 && typ2->v_type == VAR_PARTIAL)
9675 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9676 else
9677 n1 = FALSE;
9678 }
9679 else
9680 n1 = tv_equal(typ1, typ2, ic, FALSE);
9681 if (type == TYPE_NEQUAL)
9682 n1 = !n1;
9683 }
9684
9685#ifdef FEAT_FLOAT
9686 /*
9687 * If one of the two variables is a float, compare as a float.
9688 * When using "=~" or "!~", always compare as string.
9689 */
9690 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9691 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9692 {
9693 float_T f1, f2;
9694
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009695 f1 = tv_get_float(typ1);
9696 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009697 n1 = FALSE;
9698 switch (type)
9699 {
9700 case TYPE_EQUAL: n1 = (f1 == f2); break;
9701 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9702 case TYPE_GREATER: n1 = (f1 > f2); break;
9703 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9704 case TYPE_SMALLER: n1 = (f1 < f2); break;
9705 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9706 case TYPE_UNKNOWN:
9707 case TYPE_MATCH:
9708 case TYPE_NOMATCH: break; /* avoid gcc warning */
9709 }
9710 }
9711#endif
9712
9713 /*
9714 * If one of the two variables is a number, compare as a number.
9715 * When using "=~" or "!~", always compare as string.
9716 */
9717 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9718 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9719 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009720 n1 = tv_get_number(typ1);
9721 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009722 switch (type)
9723 {
9724 case TYPE_EQUAL: n1 = (n1 == n2); break;
9725 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9726 case TYPE_GREATER: n1 = (n1 > n2); break;
9727 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9728 case TYPE_SMALLER: n1 = (n1 < n2); break;
9729 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9730 case TYPE_UNKNOWN:
9731 case TYPE_MATCH:
9732 case TYPE_NOMATCH: break; /* avoid gcc warning */
9733 }
9734 }
9735 else
9736 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009737 s1 = tv_get_string_buf(typ1, buf1);
9738 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009739 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9740 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9741 else
9742 i = 0;
9743 n1 = FALSE;
9744 switch (type)
9745 {
9746 case TYPE_EQUAL: n1 = (i == 0); break;
9747 case TYPE_NEQUAL: n1 = (i != 0); break;
9748 case TYPE_GREATER: n1 = (i > 0); break;
9749 case TYPE_GEQUAL: n1 = (i >= 0); break;
9750 case TYPE_SMALLER: n1 = (i < 0); break;
9751 case TYPE_SEQUAL: n1 = (i <= 0); break;
9752
9753 case TYPE_MATCH:
9754 case TYPE_NOMATCH:
9755 n1 = pattern_match(s2, s1, ic);
9756 if (type == TYPE_NOMATCH)
9757 n1 = !n1;
9758 break;
9759
9760 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9761 }
9762 }
9763 clear_tv(typ1);
9764 typ1->v_type = VAR_NUMBER;
9765 typ1->vval.v_number = n1;
9766
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009767 return OK;
9768}
9769
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009770 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009771typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009772{
9773 char_u *tofree;
9774 char_u numbuf[NUMBUFLEN];
9775 char_u *ret = NULL;
9776
9777 if (arg == NULL)
9778 return vim_strsave((char_u *)"(does not exist)");
9779 ret = tv2string(arg, &tofree, numbuf, 0);
9780 /* Make a copy if we have a value but it's not in allocated memory. */
9781 if (ret != NULL && tofree == NULL)
9782 ret = vim_strsave(ret);
9783 return ret;
9784}
9785
9786 int
9787var_exists(char_u *var)
9788{
9789 char_u *name;
9790 char_u *tofree;
9791 typval_T tv;
9792 int len = 0;
9793 int n = FALSE;
9794
9795 /* get_name_len() takes care of expanding curly braces */
9796 name = var;
9797 len = get_name_len(&var, &tofree, TRUE, FALSE);
9798 if (len > 0)
9799 {
9800 if (tofree != NULL)
9801 name = tofree;
9802 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9803 if (n)
9804 {
9805 /* handle d.key, l[idx], f(expr) */
9806 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9807 if (n)
9808 clear_tv(&tv);
9809 }
9810 }
9811 if (*var != NUL)
9812 n = FALSE;
9813
9814 vim_free(tofree);
9815 return n;
9816}
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#endif /* FEAT_EVAL */
9819
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009821#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822
Bram Moolenaar4f974752019-02-17 17:44:42 +01009823#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824/*
9825 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
9828/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009829 * Get the short path (8.3) for the filename in "fnamep".
9830 * Only works for a valid file name.
9831 * When the path gets longer "fnamep" is changed and the allocated buffer
9832 * is put in "bufp".
9833 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9834 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 */
9836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009839 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 char_u *newbuf;
9841
9842 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009843 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 if (l > len - 1)
9845 {
9846 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009847 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 newbuf = vim_strnsave(*fnamep, l);
9849 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009850 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
9852 vim_free(*bufp);
9853 *fnamep = *bufp = newbuf;
9854
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009855 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009856 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 }
9858
9859 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009860 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861}
9862
9863/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009864 * Get the short path (8.3) for the filename in "fname". The converted
9865 * path is returned in "bufp".
9866 *
9867 * Some of the directories specified in "fname" may not exist. This function
9868 * will shorten the existing directories at the beginning of the path and then
9869 * append the remaining non-existing path.
9870 *
9871 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009872 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009873 * bufp - Pointer to an allocated buffer for the filename.
9874 * fnamelen - Length of the filename pointed to by fname
9875 *
9876 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 */
9878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009879shortpath_for_invalid_fname(
9880 char_u **fname,
9881 char_u **bufp,
9882 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009884 char_u *short_fname, *save_fname, *pbuf_unused;
9885 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009887 int old_len, len;
9888 int new_len, sfx_len;
9889 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009892 old_len = *fnamelen;
9893 save_fname = vim_strnsave(*fname, old_len);
9894 pbuf_unused = NULL;
9895 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009897 endp = save_fname + old_len - 1; /* Find the end of the copy */
9898 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009900 /*
9901 * Try shortening the supplied path till it succeeds by removing one
9902 * directory at a time from the tail of the path.
9903 */
9904 len = 0;
9905 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009907 /* go back one path-separator */
9908 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9909 --endp;
9910 if (endp <= save_fname)
9911 break; /* processed the complete path */
9912
9913 /*
9914 * Replace the path separator with a NUL and try to shorten the
9915 * resulting path.
9916 */
9917 ch = *endp;
9918 *endp = 0;
9919 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009920 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009921 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9922 {
9923 retval = FAIL;
9924 goto theend;
9925 }
9926 *endp = ch; /* preserve the string */
9927
9928 if (len > 0)
9929 break; /* successfully shortened the path */
9930
9931 /* failed to shorten the path. Skip the path separator */
9932 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 }
9934
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009935 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009937 /*
9938 * Succeeded in shortening the path. Now concatenate the shortened
9939 * path with the remaining path at the tail.
9940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009942 /* Compute the length of the new path. */
9943 sfx_len = (int)(save_endp - endp) + 1;
9944 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009946 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009948 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009950 /* There is not enough space in the currently allocated string,
9951 * copy it to a buffer big enough. */
9952 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009954 {
9955 retval = FAIL;
9956 goto theend;
9957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 }
9959 else
9960 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009961 /* Transfer short_fname to the main buffer (it's big enough),
9962 * unless get_short_pathname() did its work in-place. */
9963 *fname = *bufp = save_fname;
9964 if (short_fname != save_fname)
9965 vim_strncpy(save_fname, short_fname, len);
9966 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009968
9969 /* concat the not-shortened part of the path */
9970 vim_strncpy(*fname + len, endp, sfx_len);
9971 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009973
9974theend:
9975 vim_free(pbuf_unused);
9976 vim_free(save_fname);
9977
9978 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009983 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 */
9985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986shortpath_for_partial(
9987 char_u **fnamep,
9988 char_u **bufp,
9989 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991 int sepcount, len, tflen;
9992 char_u *p;
9993 char_u *pbuf, *tfname;
9994 int hasTilde;
9995
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009996 /* Count up the path separators from the RHS.. so we know which part
9997 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009999 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 if (vim_ispathsep(*p))
10001 ++sepcount;
10002
10003 /* Need full path first (use expand_env() to remove a "~/") */
10004 hasTilde = (**fnamep == '~');
10005 if (hasTilde)
10006 pbuf = tfname = expand_env_save(*fnamep);
10007 else
10008 pbuf = tfname = FullName_save(*fnamep, FALSE);
10009
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010010 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010012 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
10015 if (len == 0)
10016 {
10017 /* Don't have a valid filename, so shorten the rest of the
10018 * path if we can. This CAN give us invalid 8.3 filenames, but
10019 * there's not a lot of point in guessing what it might be.
10020 */
10021 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010022 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10023 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025
10026 /* Count the paths backward to find the beginning of the desired string. */
10027 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010028 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010029 if (has_mbyte)
10030 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 if (vim_ispathsep(*p))
10032 {
10033 if (sepcount == 0 || (hasTilde && sepcount == 1))
10034 break;
10035 else
10036 sepcount --;
10037 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 if (hasTilde)
10040 {
10041 --p;
10042 if (p >= tfname)
10043 *p = '~';
10044 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010045 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
10047 else
10048 ++p;
10049
10050 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10051 vim_free(*bufp);
10052 *fnamelen = (int)STRLEN(p);
10053 *bufp = pbuf;
10054 *fnamep = p;
10055
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010056 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010058#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059
10060/*
10061 * Adjust a filename, according to a string of modifiers.
10062 * *fnamep must be NUL terminated when called. When returning, the length is
10063 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010064 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 * When there is an error, *fnamep is set to NULL.
10066 */
10067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010068modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010069 char_u *src, // string with modifiers
10070 int tilde_file, // "~" is a file name, not $HOME
10071 int *usedlen, // characters after src that are used
10072 char_u **fnamep, // file name so far
10073 char_u **bufp, // buffer for allocated file name or NULL
10074 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 int valid = 0;
10077 char_u *tail;
10078 char_u *s, *p, *pbuf;
10079 char_u dirname[MAXPATHL];
10080 int c;
10081 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010082#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010083 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 int has_shortname = 0;
10085#endif
10086
10087repeat:
10088 /* ":p" - full path/file_name */
10089 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10090 {
10091 has_fullname = 1;
10092
10093 valid |= VALID_PATH;
10094 *usedlen += 2;
10095
10096 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10097 if ((*fnamep)[0] == '~'
10098#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10099 && ((*fnamep)[1] == '/'
10100# ifdef BACKSLASH_IN_FILENAME
10101 || (*fnamep)[1] == '\\'
10102# endif
10103 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010105 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 )
10107 {
10108 *fnamep = expand_env_save(*fnamep);
10109 vim_free(*bufp); /* free any allocated file name */
10110 *bufp = *fnamep;
10111 if (*fnamep == NULL)
10112 return -1;
10113 }
10114
10115 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010116 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 {
10118 if (vim_ispathsep(*p)
10119 && p[1] == '.'
10120 && (p[2] == NUL
10121 || vim_ispathsep(p[2])
10122 || (p[2] == '.'
10123 && (p[3] == NUL || vim_ispathsep(p[3])))))
10124 break;
10125 }
10126
10127 /* FullName_save() is slow, don't use it when not needed. */
10128 if (*p != NUL || !vim_isAbsName(*fnamep))
10129 {
10130 *fnamep = FullName_save(*fnamep, *p != NUL);
10131 vim_free(*bufp); /* free any allocated file name */
10132 *bufp = *fnamep;
10133 if (*fnamep == NULL)
10134 return -1;
10135 }
10136
Bram Moolenaar4f974752019-02-17 17:44:42 +010010137#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010138# if _WIN32_WINNT >= 0x0500
10139 if (vim_strchr(*fnamep, '~') != NULL)
10140 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010141 // Expand 8.3 filename to full path. Needed to make sure the same
10142 // file does not have two different names.
10143 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10144 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10145 WCHAR buf[_MAX_PATH];
10146
10147 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010148 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010149 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010150 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010151 char_u *p = utf16_to_enc(buf, NULL);
10152
10153 if (p != NULL)
10154 {
10155 vim_free(*bufp); // free any allocated file name
10156 *bufp = *fnamep = p;
10157 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010158 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010159 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010160 }
10161 }
10162# endif
10163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 /* Append a path separator to a directory. */
10165 if (mch_isdir(*fnamep))
10166 {
10167 /* Make room for one or two extra characters. */
10168 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10169 vim_free(*bufp); /* free any allocated file name */
10170 *bufp = *fnamep;
10171 if (*fnamep == NULL)
10172 return -1;
10173 add_pathsep(*fnamep);
10174 }
10175 }
10176
10177 /* ":." - path relative to the current directory */
10178 /* ":~" - path relative to the home directory */
10179 /* ":8" - shortname path - postponed till after */
10180 while (src[*usedlen] == ':'
10181 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10182 {
10183 *usedlen += 2;
10184 if (c == '8')
10185 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010186#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 has_shortname = 1; /* Postpone this. */
10188#endif
10189 continue;
10190 }
10191 pbuf = NULL;
10192 /* Need full path first (use expand_env() to remove a "~/") */
10193 if (!has_fullname)
10194 {
10195 if (c == '.' && **fnamep == '~')
10196 p = pbuf = expand_env_save(*fnamep);
10197 else
10198 p = pbuf = FullName_save(*fnamep, FALSE);
10199 }
10200 else
10201 p = *fnamep;
10202
10203 has_fullname = 0;
10204
10205 if (p != NULL)
10206 {
10207 if (c == '.')
10208 {
10209 mch_dirname(dirname, MAXPATHL);
10210 s = shorten_fname(p, dirname);
10211 if (s != NULL)
10212 {
10213 *fnamep = s;
10214 if (pbuf != NULL)
10215 {
10216 vim_free(*bufp); /* free any allocated file name */
10217 *bufp = pbuf;
10218 pbuf = NULL;
10219 }
10220 }
10221 }
10222 else
10223 {
10224 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10225 /* Only replace it when it starts with '~' */
10226 if (*dirname == '~')
10227 {
10228 s = vim_strsave(dirname);
10229 if (s != NULL)
10230 {
10231 *fnamep = s;
10232 vim_free(*bufp);
10233 *bufp = s;
10234 }
10235 }
10236 }
10237 vim_free(pbuf);
10238 }
10239 }
10240
10241 tail = gettail(*fnamep);
10242 *fnamelen = (int)STRLEN(*fnamep);
10243
10244 /* ":h" - head, remove "/file_name", can be repeated */
10245 /* Don't remove the first "/" or "c:\" */
10246 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10247 {
10248 valid |= VALID_HEAD;
10249 *usedlen += 2;
10250 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010251 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010252 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 *fnamelen = (int)(tail - *fnamep);
10254#ifdef VMS
10255 if (*fnamelen > 0)
10256 *fnamelen += 1; /* the path separator is part of the path */
10257#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010258 if (*fnamelen == 0)
10259 {
10260 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10261 p = vim_strsave((char_u *)".");
10262 if (p == NULL)
10263 return -1;
10264 vim_free(*bufp);
10265 *bufp = *fnamep = tail = p;
10266 *fnamelen = 1;
10267 }
10268 else
10269 {
10270 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010271 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 }
10274
10275 /* ":8" - shortname */
10276 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10277 {
10278 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010279#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 has_shortname = 1;
10281#endif
10282 }
10283
Bram Moolenaar4f974752019-02-17 17:44:42 +010010284#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010285 /*
10286 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 */
10288 if (has_shortname)
10289 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010290 /* Copy the string if it is shortened by :h and when it wasn't copied
10291 * yet, because we are going to change it in place. Avoids changing
10292 * the buffer name for "%:8". */
10293 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 {
10295 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010296 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 return -1;
10298 vim_free(*bufp);
10299 *bufp = *fnamep = p;
10300 }
10301
10302 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010303 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 if (!has_fullname && !vim_isAbsName(*fnamep))
10305 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010306 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 return -1;
10308 }
10309 else
10310 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010311 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312
Bram Moolenaardc935552011-08-17 15:23:23 +020010313 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010315 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 return -1;
10317
10318 if (l == 0)
10319 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010320 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010322 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 return -1;
10324 }
10325 *fnamelen = l;
10326 }
10327 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010328#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329
10330 /* ":t" - tail, just the basename */
10331 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10332 {
10333 *usedlen += 2;
10334 *fnamelen -= (int)(tail - *fnamep);
10335 *fnamep = tail;
10336 }
10337
10338 /* ":e" - extension, can be repeated */
10339 /* ":r" - root, without extension, can be repeated */
10340 while (src[*usedlen] == ':'
10341 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10342 {
10343 /* find a '.' in the tail:
10344 * - for second :e: before the current fname
10345 * - otherwise: The last '.'
10346 */
10347 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10348 s = *fnamep - 2;
10349 else
10350 s = *fnamep + *fnamelen - 1;
10351 for ( ; s > tail; --s)
10352 if (s[0] == '.')
10353 break;
10354 if (src[*usedlen + 1] == 'e') /* :e */
10355 {
10356 if (s > tail)
10357 {
10358 *fnamelen += (int)(*fnamep - (s + 1));
10359 *fnamep = s + 1;
10360#ifdef VMS
10361 /* cut version from the extension */
10362 s = *fnamep + *fnamelen - 1;
10363 for ( ; s > *fnamep; --s)
10364 if (s[0] == ';')
10365 break;
10366 if (s > *fnamep)
10367 *fnamelen = s - *fnamep;
10368#endif
10369 }
10370 else if (*fnamep <= tail)
10371 *fnamelen = 0;
10372 }
10373 else /* :r */
10374 {
10375 if (s > tail) /* remove one extension */
10376 *fnamelen = (int)(s - *fnamep);
10377 }
10378 *usedlen += 2;
10379 }
10380
10381 /* ":s?pat?foo?" - substitute */
10382 /* ":gs?pat?foo?" - global substitute */
10383 if (src[*usedlen] == ':'
10384 && (src[*usedlen + 1] == 's'
10385 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10386 {
10387 char_u *str;
10388 char_u *pat;
10389 char_u *sub;
10390 int sep;
10391 char_u *flags;
10392 int didit = FALSE;
10393
10394 flags = (char_u *)"";
10395 s = src + *usedlen + 2;
10396 if (src[*usedlen + 1] == 'g')
10397 {
10398 flags = (char_u *)"g";
10399 ++s;
10400 }
10401
10402 sep = *s++;
10403 if (sep)
10404 {
10405 /* find end of pattern */
10406 p = vim_strchr(s, sep);
10407 if (p != NULL)
10408 {
10409 pat = vim_strnsave(s, (int)(p - s));
10410 if (pat != NULL)
10411 {
10412 s = p + 1;
10413 /* find end of substitution */
10414 p = vim_strchr(s, sep);
10415 if (p != NULL)
10416 {
10417 sub = vim_strnsave(s, (int)(p - s));
10418 str = vim_strnsave(*fnamep, *fnamelen);
10419 if (sub != NULL && str != NULL)
10420 {
10421 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010422 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 if (s != NULL)
10424 {
10425 *fnamep = s;
10426 *fnamelen = (int)STRLEN(s);
10427 vim_free(*bufp);
10428 *bufp = s;
10429 didit = TRUE;
10430 }
10431 }
10432 vim_free(sub);
10433 vim_free(str);
10434 }
10435 vim_free(pat);
10436 }
10437 }
10438 /* after using ":s", repeat all the modifiers */
10439 if (didit)
10440 goto repeat;
10441 }
10442 }
10443
Bram Moolenaar26df0922014-02-23 23:39:13 +010010444 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10445 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010446 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010447 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010448 if (c != NUL)
10449 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010450 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010451 if (c != NUL)
10452 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010453 if (p == NULL)
10454 return -1;
10455 vim_free(*bufp);
10456 *bufp = *fnamep = p;
10457 *fnamelen = (int)STRLEN(p);
10458 *usedlen += 2;
10459 }
10460
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 return valid;
10462}
10463
10464/*
10465 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010466 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * "flags" can be "g" to do a global substitute.
10468 * Returns an allocated string, NULL for error.
10469 */
10470 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010471do_string_sub(
10472 char_u *str,
10473 char_u *pat,
10474 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010475 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010476 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477{
10478 int sublen;
10479 regmatch_T regmatch;
10480 int i;
10481 int do_all;
10482 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010483 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 garray_T ga;
10485 char_u *ret;
10486 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010487 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488
10489 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10490 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010491 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492
10493 ga_init2(&ga, 1, 200);
10494
10495 do_all = (flags[0] == 'g');
10496
10497 regmatch.rm_ic = p_ic;
10498 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10499 if (regmatch.regprog != NULL)
10500 {
10501 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010502 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10504 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010505 /* Skip empty match except for first match. */
10506 if (regmatch.startp[0] == regmatch.endp[0])
10507 {
10508 if (zero_width == regmatch.startp[0])
10509 {
10510 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010511 i = MB_PTR2LEN(tail);
10512 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10513 (size_t)i);
10514 ga.ga_len += i;
10515 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010516 continue;
10517 }
10518 zero_width = regmatch.startp[0];
10519 }
10520
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 /*
10522 * Get some space for a temporary buffer to do the substitution
10523 * into. It will contain:
10524 * - The text up to where the match is.
10525 * - The substituted text.
10526 * - The text after the match.
10527 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010528 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010529 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10531 {
10532 ga_clear(&ga);
10533 break;
10534 }
10535
10536 /* copy the text up to where the match is */
10537 i = (int)(regmatch.startp[0] - tail);
10538 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10539 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010540 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 + ga.ga_len + i, TRUE, TRUE, FALSE);
10542 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010543 tail = regmatch.endp[0];
10544 if (*tail == NUL)
10545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (!do_all)
10547 break;
10548 }
10549
10550 if (ga.ga_data != NULL)
10551 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10552
Bram Moolenaar473de612013-06-08 18:19:48 +020010553 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 }
10555
10556 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10557 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010558 if (p_cpo == empty_option)
10559 p_cpo = save_cpo;
10560 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010561 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010562 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563
10564 return ret;
10565}
10566
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010567 static int
10568filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10569{
10570 typval_T rettv;
10571 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010572 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573
10574 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10575 argv[0] = vimvars[VV_KEY].vv_tv;
10576 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010577 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10578 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010579 if (map)
10580 {
10581 /* map(): replace the list item value */
10582 clear_tv(tv);
10583 rettv.v_lock = 0;
10584 *tv = rettv;
10585 }
10586 else
10587 {
10588 int error = FALSE;
10589
10590 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010591 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592 clear_tv(&rettv);
10593 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010594 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010595 if (error)
10596 goto theend;
10597 }
10598 retval = OK;
10599theend:
10600 clear_tv(&vimvars[VV_VAL].vv_tv);
10601 return retval;
10602}
10603
10604
10605/*
10606 * Implementation of map() and filter().
10607 */
10608 void
10609filter_map(typval_T *argvars, typval_T *rettv, int map)
10610{
10611 typval_T *expr;
10612 listitem_T *li, *nli;
10613 list_T *l = NULL;
10614 dictitem_T *di;
10615 hashtab_T *ht;
10616 hashitem_T *hi;
10617 dict_T *d = NULL;
10618 typval_T save_val;
10619 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010620 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 int rem;
10622 int todo;
10623 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10624 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10625 : N_("filter() argument"));
10626 int save_did_emsg;
10627 int idx = 0;
10628
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010629 if (argvars[0].v_type == VAR_BLOB)
10630 {
10631 if ((b = argvars[0].vval.v_blob) == NULL)
10632 return;
10633 }
10634 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 {
10636 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010637 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638 return;
10639 }
10640 else if (argvars[0].v_type == VAR_DICT)
10641 {
10642 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010643 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010644 return;
10645 }
10646 else
10647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010648 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 return;
10650 }
10651
10652 expr = &argvars[1];
10653 /* On type errors, the preceding call has already displayed an error
10654 * message. Avoid a misleading error message for an empty string that
10655 * was not passed as argument. */
10656 if (expr->v_type != VAR_UNKNOWN)
10657 {
10658 prepare_vimvar(VV_VAL, &save_val);
10659
10660 /* We reset "did_emsg" to be able to detect whether an error
10661 * occurred during evaluation of the expression. */
10662 save_did_emsg = did_emsg;
10663 did_emsg = FALSE;
10664
10665 prepare_vimvar(VV_KEY, &save_key);
10666 if (argvars[0].v_type == VAR_DICT)
10667 {
10668 vimvars[VV_KEY].vv_type = VAR_STRING;
10669
10670 ht = &d->dv_hashtab;
10671 hash_lock(ht);
10672 todo = (int)ht->ht_used;
10673 for (hi = ht->ht_array; todo > 0; ++hi)
10674 {
10675 if (!HASHITEM_EMPTY(hi))
10676 {
10677 int r;
10678
10679 --todo;
10680 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010681 if (map && (var_check_lock(di->di_tv.v_lock,
10682 arg_errmsg, TRUE)
10683 || var_check_ro(di->di_flags,
10684 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010685 break;
10686 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10687 r = filter_map_one(&di->di_tv, expr, map, &rem);
10688 clear_tv(&vimvars[VV_KEY].vv_tv);
10689 if (r == FAIL || did_emsg)
10690 break;
10691 if (!map && rem)
10692 {
10693 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10694 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10695 break;
10696 dictitem_remove(d, di);
10697 }
10698 }
10699 }
10700 hash_unlock(ht);
10701 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010702 else if (argvars[0].v_type == VAR_BLOB)
10703 {
10704 int i;
10705 typval_T tv;
10706
10707 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10708 for (i = 0; i < b->bv_ga.ga_len; i++)
10709 {
10710 tv.v_type = VAR_NUMBER;
10711 tv.vval.v_number = blob_get(b, i);
10712 vimvars[VV_KEY].vv_nr = idx;
10713 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10714 break;
10715 if (tv.v_type != VAR_NUMBER)
10716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010717 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010718 return;
10719 }
10720 tv.v_type = VAR_NUMBER;
10721 blob_set(b, i, tv.vval.v_number);
10722 if (!map && rem)
10723 {
10724 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10725
10726 mch_memmove(p + idx, p + i + 1,
10727 (size_t)b->bv_ga.ga_len - i - 1);
10728 --b->bv_ga.ga_len;
10729 --i;
10730 }
10731 }
10732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010733 else
10734 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010735 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10737
10738 for (li = l->lv_first; li != NULL; li = nli)
10739 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010740 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 break;
10742 nli = li->li_next;
10743 vimvars[VV_KEY].vv_nr = idx;
10744 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10745 || did_emsg)
10746 break;
10747 if (!map && rem)
10748 listitem_remove(l, li);
10749 ++idx;
10750 }
10751 }
10752
10753 restore_vimvar(VV_KEY, &save_key);
10754 restore_vimvar(VV_VAL, &save_val);
10755
10756 did_emsg |= save_did_emsg;
10757 }
10758
10759 copy_tv(&argvars[0], rettv);
10760}
10761
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */