blob: 9a2642cbe81b812e0beb671d486f082df4cf56ec [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 Moolenaar37f4cbd2019-08-23 20:58:45 +0200204 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000205};
206
207/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000208#define vv_type vv_di.di_tv.v_type
209#define vv_nr vv_di.di_tv.vval.v_number
210#define vv_float vv_di.di_tv.vval.v_float
211#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000212#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200213#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100214#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000215#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000216
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200217static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000218#define vimvarht vimvardict.dv_hashtab
219
Bram Moolenaar9937a052019-06-15 15:45:06 +0200220static void ex_let_const(exarg_T *eap, int is_const);
221static 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 +0100222static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
223static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100224static void list_glob_vars(int *first);
225static void list_buf_vars(int *first);
226static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100228static void list_vim_vars(int *first);
229static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100230static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200231static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
232static 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 +0100233static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100234static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
235static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
236static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
237static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int eval2(char_u **arg, typval_T *rettv, int evaluate);
240static int eval3(char_u **arg, typval_T *rettv, int evaluate);
241static int eval4(char_u **arg, typval_T *rettv, int evaluate);
242static int eval5(char_u **arg, typval_T *rettv, int evaluate);
243static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
244static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +0200245static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000246
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
248static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static int get_env_len(char_u **arg);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200252static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
253static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
254static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200255static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100256static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100257static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100258static void list_one_var(dictitem_T *v, char *prefix, int *first);
259static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200260static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100261static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100262static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200263
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264/* for VIM_VERSION_ defines */
265#include "version.h"
266
Bram Moolenaare21c1582019-03-02 11:57:09 +0100267/*
268 * Return "n1" divided by "n2", taking care of dividing by zero.
269 */
270 static varnumber_T
271num_divide(varnumber_T n1, varnumber_T n2)
272{
273 varnumber_T result;
274
275 if (n2 == 0) // give an error message?
276 {
277 if (n1 == 0)
278 result = VARNUM_MIN; // similar to NaN
279 else if (n1 < 0)
280 result = -VARNUM_MAX;
281 else
282 result = VARNUM_MAX;
283 }
284 else
285 result = n1 / n2;
286
287 return result;
288}
289
290/*
291 * Return "n1" modulus "n2", taking care of dividing by zero.
292 */
293 static varnumber_T
294num_modulus(varnumber_T n1, varnumber_T n2)
295{
296 // Give an error when n2 is 0?
297 return (n2 == 0) ? 0 : (n1 % n2);
298}
299
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100300
301#if defined(EBCDIC) || defined(PROTO)
302/*
303 * Compare struct fst by function name.
304 */
305 static int
306compare_func_name(const void *s1, const void *s2)
307{
308 struct fst *p1 = (struct fst *)s1;
309 struct fst *p2 = (struct fst *)s2;
310
311 return STRCMP(p1->f_name, p2->f_name);
312}
313
314/*
315 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100316 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100317 * On machines using EBCDIC we have to sort it.
318 */
319 static void
320sortFunctions(void)
321{
322 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
323
324 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
325}
326#endif
327
328
Bram Moolenaar33570922005-01-25 22:26:29 +0000329/*
330 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000331 */
332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100333eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000334{
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 int i;
336 struct vimvar *p;
337
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200338 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
339 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200340 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000341 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200342 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000343
344 for (i = 0; i < VV_LEN; ++i)
345 {
346 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200347 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100349 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200350 getout(1);
351 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000352 STRCPY(p->vv_di.di_key, p->vv_name);
353 if (p->vv_flags & VV_RO)
354 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
355 else if (p->vv_flags & VV_RO_SBX)
356 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
357 else
358 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000359
360 /* add to v: scope dict, unless the value is not always available */
361 if (p->vv_type != VAR_UNKNOWN)
362 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000363 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000364 /* add to compat scope dict */
365 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000366 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100367 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200368 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100369
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100371 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100372 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100373 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100374 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100375
376 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
377 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
378 set_vim_var_nr(VV_NONE, VVAL_NONE);
379 set_vim_var_nr(VV_NULL, VVAL_NULL);
380
Bram Moolenaarf562e722016-07-19 17:25:25 +0200381 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
382 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
383 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
384 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
385 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
386 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
387 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
388 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
389 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
390 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100391 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200392
Bram Moolenaar37f4cbd2019-08-23 20:58:45 +0200393 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
394
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200395 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200396
397#ifdef EBCDIC
398 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100399 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200400 */
401 sortFunctions();
402#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000403}
404
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000405#if defined(EXITFREE) || defined(PROTO)
406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100407eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000408{
409 int i;
410 struct vimvar *p;
411
412 for (i = 0; i < VV_LEN; ++i)
413 {
414 p = &vimvars[i];
415 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100416 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000417 else if (p->vv_di.di_tv.v_type == VAR_LIST)
418 {
419 list_unref(p->vv_list);
420 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000421 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000422 }
423 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000424 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000425 hash_clear(&compat_hashtab);
426
Bram Moolenaard9fba312005-06-26 22:34:35 +0000427 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200428 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000429
430 /* global variables */
431 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000432
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000433 /* autoloaded script names */
434 ga_clear_strings(&ga_loaded);
435
Bram Moolenaarcca74132013-09-25 21:00:28 +0200436 /* Script-local variables. First clear all the variables and in a second
437 * loop free the scriptvar_T, because a variable in one script might hold
438 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200439 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200440 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200441 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200442 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200443 ga_clear(&ga_scripts);
444
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200445 // unreferenced lists and dicts
446 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200447
448 // functions not garbage collected
449 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000450}
451#endif
452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 * Set an internal variable to a string value. Creates the variable if it does
456 * not already exist.
457 */
458 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100459set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000461 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000462 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464 val = vim_strsave(value);
465 if (val != NULL)
466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000467 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000468 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000470 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000471 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 }
473 }
474}
475
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200477#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000478static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000479static char_u *redir_endp = NULL;
480static char_u *redir_varname = NULL;
481
482/*
483 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100484 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000485 * Returns OK if successfully completed the setup. FAIL otherwise.
486 */
487 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100488var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489{
490 int save_emsg;
491 int err;
492 typval_T tv;
493
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000494 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000495 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100497 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000498 return FAIL;
499 }
500
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000501 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 redir_varname = vim_strsave(name);
503 if (redir_varname == NULL)
504 return FAIL;
505
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200506 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000507 if (redir_lval == NULL)
508 {
509 var_redir_stop();
510 return FAIL;
511 }
512
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000513 /* The output is stored in growarray "redir_ga" until redirection ends. */
514 ga_init2(&redir_ga, (int)sizeof(char), 500);
515
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100517 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000518 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000519 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
520 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200521 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522 if (redir_endp != NULL && *redir_endp != NUL)
523 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100524 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100526 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000527 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000528 var_redir_stop();
529 return FAIL;
530 }
531
532 /* check if we can write to the variable: set it to or append an empty
533 * string */
534 save_emsg = did_emsg;
535 did_emsg = FALSE;
536 tv.v_type = VAR_STRING;
537 tv.vval.v_string = (char_u *)"";
538 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200539 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200541 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200542 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000544 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000545 if (err)
546 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000547 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000548 var_redir_stop();
549 return FAIL;
550 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551
552 return OK;
553}
554
555/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000556 * Append "value[value_len]" to the variable set by var_redir_start().
557 * The actual appending is postponed until redirection ends, because the value
558 * appended may in fact be the string we write to, changing it may cause freed
559 * memory to be used:
560 * :redir => foo
561 * :let foo
562 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563 */
564 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100565var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000566{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000567 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568
569 if (redir_lval == NULL)
570 return;
571
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000572 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000573 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000574 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000575 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000576
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000577 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000578 {
579 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000580 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000581 }
582 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000583 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000584}
585
586/*
587 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000588 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000589 */
590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100591var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000592{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000593 typval_T tv;
594
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200595 if (EVALCMD_BUSY)
596 {
597 redir_lval = NULL;
598 return;
599 }
600
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000601 if (redir_lval != NULL)
602 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000603 /* If there was no error: assign the text to the variable. */
604 if (redir_endp != NULL)
605 {
606 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
607 tv.v_type = VAR_STRING;
608 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200609 /* Call get_lval() again, if it's inside a Dict or List it may
610 * have changed. */
611 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100612 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200613 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200614 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
615 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200616 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000617 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000618
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000619 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100620 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000621
Bram Moolenaard23a8232018-02-10 18:45:26 +0100622 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000623 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100624 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000625}
626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100628eval_charconvert(
629 char_u *enc_from,
630 char_u *enc_to,
631 char_u *fname_from,
632 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int err = FALSE;
635
636 set_vim_var_string(VV_CC_FROM, enc_from, -1);
637 set_vim_var_string(VV_CC_TO, enc_to, -1);
638 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
639 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
640 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
641 err = TRUE;
642 set_vim_var_string(VV_CC_FROM, NULL, -1);
643 set_vim_var_string(VV_CC_TO, NULL, -1);
644 set_vim_var_string(VV_FNAME_IN, NULL, -1);
645 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
646
647 if (err)
648 return FAIL;
649 return OK;
650}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100654eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
656 int err = FALSE;
657
658 set_vim_var_string(VV_FNAME_IN, fname, -1);
659 set_vim_var_string(VV_CMDARG, args, -1);
660 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
661 err = TRUE;
662 set_vim_var_string(VV_FNAME_IN, NULL, -1);
663 set_vim_var_string(VV_CMDARG, NULL, -1);
664
665 if (err)
666 {
667 mch_remove(fname);
668 return FAIL;
669 }
670 return OK;
671}
672# endif
673
674# if defined(FEAT_DIFF) || defined(PROTO)
675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100676eval_diff(
677 char_u *origfile,
678 char_u *newfile,
679 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
681 int err = FALSE;
682
683 set_vim_var_string(VV_FNAME_IN, origfile, -1);
684 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
685 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
686 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
687 set_vim_var_string(VV_FNAME_IN, NULL, -1);
688 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
689 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
690}
691
692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100693eval_patch(
694 char_u *origfile,
695 char_u *difffile,
696 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
698 int err;
699
700 set_vim_var_string(VV_FNAME_IN, origfile, -1);
701 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
702 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
703 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
704 set_vim_var_string(VV_FNAME_IN, NULL, -1);
705 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
706 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
707}
708# endif
709
710/*
711 * Top level evaluation function, returning a boolean.
712 * Sets "error" to TRUE if there was an error.
713 * Return TRUE or FALSE.
714 */
715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100716eval_to_bool(
717 char_u *arg,
718 int *error,
719 char_u **nextcmd,
720 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
Bram Moolenaar33570922005-01-25 22:26:29 +0000722 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200723 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725 if (skip)
726 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 else
730 {
731 *error = FALSE;
732 if (!skip)
733 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100734 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737 }
738 if (skip)
739 --emsg_skip;
740
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200741 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742}
743
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100744/*
745 * Call eval1() and give an error message if not done at a lower level.
746 */
747 static int
748eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
749{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100750 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100751 int ret;
752 int did_emsg_before = did_emsg;
753 int called_emsg_before = called_emsg;
754
755 ret = eval1(arg, rettv, evaluate);
756 if (ret == FAIL)
757 {
758 // Report the invalid expression unless the expression evaluation has
759 // been cancelled due to an aborting error, an interrupt, or an
760 // exception, or we already gave a more specific error.
761 // Also check called_emsg for when using assert_fails().
762 if (!aborting() && did_emsg == did_emsg_before
763 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100764 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100765 }
766 return ret;
767}
768
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200769 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100770eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
771{
772 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100773 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200774 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100775
776 if (expr->v_type == VAR_FUNC)
777 {
778 s = expr->vval.v_string;
779 if (s == NULL || *s == NUL)
780 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200781 vim_memset(&funcexe, 0, sizeof(funcexe));
782 funcexe.evaluate = TRUE;
783 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100784 return FAIL;
785 }
786 else if (expr->v_type == VAR_PARTIAL)
787 {
788 partial_T *partial = expr->vval.v_partial;
789
790 s = partial_name(partial);
791 if (s == NULL || *s == NUL)
792 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200793 vim_memset(&funcexe, 0, sizeof(funcexe));
794 funcexe.evaluate = TRUE;
795 funcexe.partial = partial;
796 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100797 return FAIL;
798 }
799 else
800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100801 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100802 if (s == NULL)
803 return FAIL;
804 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100805 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100806 return FAIL;
807 if (*s != NUL) /* check for trailing chars after expr */
808 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200809 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100810 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100811 return FAIL;
812 }
813 }
814 return OK;
815}
816
817/*
818 * Like eval_to_bool() but using a typval_T instead of a string.
819 * Works for string, funcref and partial.
820 */
821 int
822eval_expr_to_bool(typval_T *expr, int *error)
823{
824 typval_T rettv;
825 int res;
826
827 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
828 {
829 *error = TRUE;
830 return FALSE;
831 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100832 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100833 clear_tv(&rettv);
834 return res;
835}
836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837/*
838 * Top level evaluation function, returning a string. If "skip" is TRUE,
839 * only parsing to "nextcmd" is done, without reporting errors. Return
840 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
841 */
842 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100843eval_to_string_skip(
844 char_u *arg,
845 char_u **nextcmd,
846 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 char_u *retval;
850
851 if (skip)
852 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 retval = NULL;
855 else
856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100857 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860 if (skip)
861 --emsg_skip;
862
863 return retval;
864}
865
866/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867 * Skip over an expression at "*pp".
868 * Return FAIL for an error, OK otherwise.
869 */
870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100871skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000872{
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000874
875 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000877}
878
879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000881 * When "convert" is TRUE convert a List into a sequence of lines and convert
882 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Return pointer to allocated memory, or NULL for failure.
884 */
885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886eval_to_string(
887 char_u *arg,
888 char_u **nextcmd,
889 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
Bram Moolenaar33570922005-01-25 22:26:29 +0000891 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000893 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000894#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000895 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 retval = NULL;
900 else
901 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000902 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000903 {
904 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000905 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200906 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200907 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200908 if (tv.vval.v_list->lv_len > 0)
909 ga_append(&ga, NL);
910 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000911 ga_append(&ga, NUL);
912 retval = (char_u *)ga.ga_data;
913 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000914#ifdef FEAT_FLOAT
915 else if (convert && tv.v_type == VAR_FLOAT)
916 {
917 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
918 retval = vim_strsave(numbuf);
919 }
920#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000921 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100922 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925
926 return retval;
927}
928
929/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000930 * Call eval_to_string() without using current local variables and using
931 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 */
933 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100934eval_to_string_safe(
935 char_u *arg,
936 char_u **nextcmd,
937 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200940 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200942 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000943 if (use_sandbox)
944 ++sandbox;
945 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000946 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000947 if (use_sandbox)
948 --sandbox;
949 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200950 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return retval;
952}
953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954/*
955 * Top level evaluation function, returning a number.
956 * Evaluates "expr" silently.
957 * Returns -1 for an error.
958 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200959 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100960eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200963 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000964 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 ++emsg_off;
967
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 retval = -1;
970 else
971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100972 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 --emsg_off;
976
977 return retval;
978}
979
Bram Moolenaara40058a2005-07-11 22:42:07 +0000980/*
981 * Prepare v: variable "idx" to be used.
982 * Save the current typeval in "save_tv".
983 * When not used yet add the variable to the v: hashtable.
984 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100986prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000987{
988 *save_tv = vimvars[idx].vv_tv;
989 if (vimvars[idx].vv_type == VAR_UNKNOWN)
990 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
991}
992
993/*
994 * Restore v: variable "idx" to typeval "save_tv".
995 * When no longer defined, remove the variable from the v: hashtable.
996 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100998restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000999{
1000 hashitem_T *hi;
1001
Bram Moolenaara40058a2005-07-11 22:42:07 +00001002 vimvars[idx].vv_tv = *save_tv;
1003 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1004 {
1005 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1006 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001007 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001008 else
1009 hash_remove(&vimvarht, hi);
1010 }
1011}
1012
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001013#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001014/*
1015 * Evaluate an expression to a list with suggestions.
1016 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001017 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001018 */
1019 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001020eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001021{
1022 typval_T save_val;
1023 typval_T rettv;
1024 list_T *list = NULL;
1025 char_u *p = skipwhite(expr);
1026
1027 /* Set "v:val" to the bad word. */
1028 prepare_vimvar(VV_VAL, &save_val);
1029 vimvars[VV_VAL].vv_type = VAR_STRING;
1030 vimvars[VV_VAL].vv_str = badword;
1031 if (p_verbose == 0)
1032 ++emsg_off;
1033
1034 if (eval1(&p, &rettv, TRUE) == OK)
1035 {
1036 if (rettv.v_type != VAR_LIST)
1037 clear_tv(&rettv);
1038 else
1039 list = rettv.vval.v_list;
1040 }
1041
1042 if (p_verbose == 0)
1043 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001044 restore_vimvar(VV_VAL, &save_val);
1045
1046 return list;
1047}
1048
1049/*
1050 * "list" is supposed to contain two items: a word and a number. Return the
1051 * word in "pp" and the number as the return value.
1052 * Return -1 if anything isn't right.
1053 * Used to get the good word and score from the eval_spell_expr() result.
1054 */
1055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001056get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001057{
1058 listitem_T *li;
1059
1060 li = list->lv_first;
1061 if (li == NULL)
1062 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001063 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001064
1065 li = li->li_next;
1066 if (li == NULL)
1067 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001068 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001069}
1070#endif
1071
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001072/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001073 * Top level evaluation function.
1074 * Returns an allocated typval_T with the result.
1075 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001076 */
1077 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001078eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001079{
1080 typval_T *tv;
1081
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001082 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001083 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001084 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001085
1086 return tv;
1087}
1088
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001091 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001092 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1093 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001096 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001097call_vim_function(
1098 char_u *func,
1099 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001100 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001101 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001103 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001104 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001106 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001107 vim_memset(&funcexe, 0, sizeof(funcexe));
1108 funcexe.firstline = curwin->w_cursor.lnum;
1109 funcexe.lastline = curwin->w_cursor.lnum;
1110 funcexe.evaluate = TRUE;
1111 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001112 if (ret == FAIL)
1113 clear_tv(rettv);
1114
1115 return ret;
1116}
1117
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001118/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001119 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001121 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1122 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001123 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001124 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001125call_func_retnr(
1126 char_u *func,
1127 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001128 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001129{
1130 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001131 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001132
Bram Moolenaarded27a12018-08-01 19:06:03 +02001133 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001134 return -1;
1135
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001136 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001137 clear_tv(&rettv);
1138 return retval;
1139}
1140
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 Moolenaard8e9bb22005-07-09 21:14:46 +00001163
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001164/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001165 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001166 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1167 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001168 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001169 */
1170 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171call_func_retlist(
1172 char_u *func,
1173 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001174 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001175{
1176 typval_T rettv;
1177
Bram Moolenaarded27a12018-08-01 19:06:03 +02001178 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001179 return NULL;
1180
1181 if (rettv.v_type != VAR_LIST)
1182 {
1183 clear_tv(&rettv);
1184 return NULL;
1185 }
1186
1187 return rettv.vval.v_list;
1188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaar05159a02005-02-26 23:04:13 +00001190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#ifdef FEAT_FOLDING
1192/*
1193 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1194 * it in "*cp". Doesn't give error messages.
1195 */
1196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001197eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
Bram Moolenaar33570922005-01-25 22:26:29 +00001199 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001200 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001202 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1203 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001206 if (use_sandbox)
1207 ++sandbox;
1208 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 retval = 0;
1212 else
1213 {
1214 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 if (tv.v_type == VAR_NUMBER)
1216 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001217 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 retval = 0;
1219 else
1220 {
1221 /* If the result is a string, check if there is a non-digit before
1222 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001223 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (!VIM_ISDIGIT(*s) && *s != '-')
1225 *cp = *s++;
1226 retval = atol((char *)s);
1227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001231 if (use_sandbox)
1232 --sandbox;
1233 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001235 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236}
1237#endif
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001240 * Get a list of lines from a HERE document. The here document is a list of
1241 * lines surrounded by a marker.
1242 * cmd << {marker}
1243 * {line1}
1244 * {line2}
1245 * ....
1246 * {marker}
1247 *
1248 * The {marker} is a string. If the optional 'trim' word is supplied before the
1249 * marker, then the leading indentation before the lines (matching the
1250 * indentation in the 'cmd' line) is stripped.
1251 * Returns a List with {lines} or NULL.
1252 */
1253 static list_T *
1254heredoc_get(exarg_T *eap, char_u *cmd)
1255{
1256 char_u *theline;
1257 char_u *marker;
1258 list_T *l;
1259 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001260 int marker_indent_len = 0;
1261 int text_indent_len = 0;
1262 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001263
1264 if (eap->getline == NULL)
1265 {
1266 emsg(_("E991: cannot use =<< here"));
1267 return NULL;
1268 }
1269
1270 // Check for the optional 'trim' word before the marker
1271 cmd = skipwhite(cmd);
1272 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1273 {
1274 cmd = skipwhite(cmd + 4);
1275
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001276 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001277 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001278 // the first line after the :let command line. To find the end marker
1279 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001280 p = *eap->cmdlinep;
1281 while (VIM_ISWHITE(*p))
1282 {
1283 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001284 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001285 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001286 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001287 }
1288
Bram Moolenaar24582002019-07-21 14:14:26 +02001289 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001290 if (*cmd != NUL && *cmd != '"')
1291 {
1292 marker = skipwhite(cmd);
1293 p = skiptowhite(marker);
1294 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1295 {
1296 emsg(_(e_trailing));
1297 return NULL;
1298 }
1299 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001300 if (vim_islower(*marker))
1301 {
1302 emsg(_("E221: Marker cannot start with lower case letter"));
1303 return NULL;
1304 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001305 }
1306 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001307 {
1308 emsg(_("E172: Missing marker"));
1309 return NULL;
1310 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001311
1312 l = list_alloc();
1313 if (l == NULL)
1314 return NULL;
1315
1316 for (;;)
1317 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001318 int mi = 0;
1319 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001320
Bram Moolenaare96a2492019-06-25 04:12:16 +02001321 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001322 if (theline == NULL)
1323 {
1324 semsg(_("E990: Missing end marker '%s'"), marker);
1325 break;
1326 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001327
1328 // with "trim": skip the indent matching the :let line to find the
1329 // marker
1330 if (marker_indent_len > 0
1331 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1332 mi = marker_indent_len;
1333 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001334 {
1335 vim_free(theline);
1336 break;
1337 }
1338
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001339 if (text_indent_len == -1 && *theline != NUL)
1340 {
1341 // set the text indent from the first line.
1342 p = theline;
1343 text_indent_len = 0;
1344 while (VIM_ISWHITE(*p))
1345 {
1346 p++;
1347 text_indent_len++;
1348 }
1349 text_indent = vim_strnsave(theline, text_indent_len);
1350 }
1351 // with "trim": skip the indent matching the first line
1352 if (text_indent != NULL)
1353 for (ti = 0; ti < text_indent_len; ++ti)
1354 if (theline[ti] != text_indent[ti])
1355 break;
1356
1357 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001358 break;
1359 vim_free(theline);
1360 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001361 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001362
1363 return l;
1364}
1365
1366/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 * ":let" list all variable values
1368 * ":let var1 var2" list variable values
1369 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001370 * ":let var += expr" assignment command.
1371 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001372 * ":let var *= expr" assignment command.
1373 * ":let var /= expr" assignment command.
1374 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001375 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001376 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 */
1379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001382 ex_let_const(eap, FALSE);
1383}
1384
1385/*
1386 * ":const" list all variable values
1387 * ":const var1 var2" list variable values
1388 * ":const var = expr" assignment command.
1389 * ":const [var1, var2] = expr" unpack list.
1390 */
1391 void
1392ex_const(exarg_T *eap)
1393{
1394 ex_let_const(eap, TRUE);
1395}
1396
1397 static void
1398ex_let_const(exarg_T *eap, int is_const)
1399{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 int var_count = 0;
1405 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001407 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001408 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001409 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaardb552d602006-03-23 22:59:57 +00001411 argend = skip_var_list(arg, &var_count, &semicolon);
1412 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001414 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001415 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001416 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001417 concat = expr[0] == '.'
1418 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1419 || (expr[1] == '.' && expr[2] == '='));
1420 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1421 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001423 /*
1424 * ":let" without "=": list variables
1425 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001426 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001427 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001428 else if (expr[0] == '.')
1429 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001432 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001436 list_glob_vars(&first);
1437 list_buf_vars(&first);
1438 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001439 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001440 list_script_vars(&first);
1441 list_func_vars(&first);
1442 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 eap->nextcmd = check_nextcmd(arg);
1445 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001446 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1447 {
1448 list_T *l;
1449
1450 // HERE document
1451 l = heredoc_get(eap, expr + 3);
1452 if (l != NULL)
1453 {
1454 rettv_list_set(&rettv, l);
1455 op[0] = '=';
1456 op[1] = NUL;
1457 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001458 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001459 clear_tv(&rettv);
1460 }
1461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 else
1463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 op[0] = '=';
1465 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001466 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001468 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001469 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001471 if (expr[0] == '.' && expr[1] == '.') // ..=
1472 ++expr;
1473 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001474 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001475 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001476 else
1477 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (eap->skip)
1480 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (eap->skip)
1483 {
1484 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 --emsg_skip;
1487 }
1488 else if (i != FAIL)
1489 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001490 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001491 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494 }
1495}
1496
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497/*
1498 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1499 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001500 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001501 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1502 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503 * Returns OK or FAIL;
1504 */
1505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506ex_let_vars(
1507 char_u *arg_start,
1508 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001509 int copy, // copy values from "tv", don't move
1510 int semicolon, // from skip_var_list()
1511 int var_count, // from skip_var_list()
1512 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001513 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514{
1515 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001516 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001518 listitem_T *item;
1519 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520
1521 if (*arg != '[')
1522 {
1523 /*
1524 * ":let var = expr" or ":for var in list"
1525 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001526 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 return FAIL;
1528 return OK;
1529 }
1530
1531 /*
1532 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1533 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001534 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001536 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537 return FAIL;
1538 }
1539
1540 i = list_len(l);
1541 if (semicolon == 0 && var_count < i)
1542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001544 return FAIL;
1545 }
1546 if (var_count - semicolon > i)
1547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001548 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549 return FAIL;
1550 }
1551
1552 item = l->lv_first;
1553 while (*arg != ']')
1554 {
1555 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001556 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001557 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 item = item->li_next;
1559 if (arg == NULL)
1560 return FAIL;
1561
1562 arg = skipwhite(arg);
1563 if (*arg == ';')
1564 {
1565 /* Put the rest of the list (may be empty) in the var after ';'.
1566 * Create a new list for this. */
1567 l = list_alloc();
1568 if (l == NULL)
1569 return FAIL;
1570 while (item != NULL)
1571 {
1572 list_append_tv(l, &item->li_tv);
1573 item = item->li_next;
1574 }
1575
1576 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001577 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 ltv.vval.v_list = l;
1579 l->lv_refcount = 1;
1580
Bram Moolenaar9937a052019-06-15 15:45:06 +02001581 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001582 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001583 clear_tv(&ltv);
1584 if (arg == NULL)
1585 return FAIL;
1586 break;
1587 }
1588 else if (*arg != ',' && *arg != ']')
1589 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001590 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001591 return FAIL;
1592 }
1593 }
1594
1595 return OK;
1596}
1597
1598/*
1599 * Skip over assignable variable "var" or list of variables "[var, var]".
1600 * Used for ":let varvar = expr" and ":for varvar in expr".
1601 * For "[var, var]" increment "*var_count" for each variable.
1602 * for "[var, var; var]" set "semicolon".
1603 * Return NULL for an error.
1604 */
1605 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001606skip_var_list(
1607 char_u *arg,
1608 int *var_count,
1609 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610{
1611 char_u *p, *s;
1612
1613 if (*arg == '[')
1614 {
1615 /* "[var, var]": find the matching ']'. */
1616 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618 {
1619 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1620 s = skip_var_one(p);
1621 if (s == p)
1622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001623 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001624 return NULL;
1625 }
1626 ++*var_count;
1627
1628 p = skipwhite(s);
1629 if (*p == ']')
1630 break;
1631 else if (*p == ';')
1632 {
1633 if (*semicolon == 1)
1634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 return NULL;
1637 }
1638 *semicolon = 1;
1639 }
1640 else if (*p != ',')
1641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 return NULL;
1644 }
1645 }
1646 return p + 1;
1647 }
1648 else
1649 return skip_var_one(arg);
1650}
1651
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001652/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001653 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001654 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001659 if (*arg == '@' && arg[1] != NUL)
1660 return arg + 2;
1661 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1662 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663}
1664
Bram Moolenaara7043832005-01-21 11:56:39 +00001665/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 * List variables for hashtab "ht" with prefix "prefix".
1667 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001668 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001670list_hashtable_vars(
1671 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001672 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001673 int empty,
1674 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001675{
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 hashitem_T *hi;
1677 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001678 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001679 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001680
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001681 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001682 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1683 {
1684 if (!HASHITEM_EMPTY(hi))
1685 {
1686 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001688
1689 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001690 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1691 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001692 if (message_filtered(buf))
1693 continue;
1694
Bram Moolenaar33570922005-01-25 22:26:29 +00001695 if (empty || di->di_tv.v_type != VAR_STRING
1696 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001697 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001698 }
1699 }
1700}
1701
1702/*
1703 * List global variables.
1704 */
1705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001706list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001707{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001708 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001709}
1710
1711/*
1712 * List buffer variables.
1713 */
1714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001715list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001716{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001717 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001718}
1719
1720/*
1721 * List window variables.
1722 */
1723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001725{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001726 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001727}
1728
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001729/*
1730 * List tab page variables.
1731 */
1732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001733list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001734{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001735 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001736}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001737
Bram Moolenaara7043832005-01-21 11:56:39 +00001738/*
1739 * List Vim variables.
1740 */
1741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001742list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001744 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745}
1746
1747/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001748 * List script-local variables, if there is a script.
1749 */
1750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001752{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001753 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1754 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001755 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001756}
1757
1758/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 * List variables in "arg".
1760 */
1761 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763{
1764 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001765 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001767 char_u *name_start;
1768 char_u *arg_subsc;
1769 char_u *tofree;
1770 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771
1772 while (!ends_excmd(*arg) && !got_int)
1773 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001774 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001776 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001777 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001778 {
1779 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001780 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001781 break;
1782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001786 /* get_name_len() takes care of expanding curly braces */
1787 name_start = name = arg;
1788 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1789 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001791 /* This is mainly to keep test 49 working: when expanding
1792 * curly braces fails overrule the exception error message. */
1793 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001795 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001796 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001797 break;
1798 }
1799 error = TRUE;
1800 }
1801 else
1802 {
1803 if (tofree != NULL)
1804 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001805 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 else
1808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001809 /* handle d.key, l[idx], f(expr) */
1810 arg_subsc = arg;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02001811 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1812 name, &name) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001813 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001815 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001816 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001817 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001819 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001820 case 'g': list_glob_vars(first); break;
1821 case 'b': list_buf_vars(first); break;
1822 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001823 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001824 case 'v': list_vim_vars(first); break;
1825 case 's': list_script_vars(first); break;
1826 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001828 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001829 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001830 }
1831 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001832 {
1833 char_u numbuf[NUMBUFLEN];
1834 char_u *tf;
1835 int c;
1836 char_u *s;
1837
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001838 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001839 c = *arg;
1840 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001841 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001842 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 tv.v_type,
1844 s == NULL ? (char_u *)"" : s,
1845 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 *arg = c;
1847 vim_free(tf);
1848 }
1849 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 }
1852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001853
1854 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001856
1857 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 }
1859
1860 return arg;
1861}
1862
1863/*
1864 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1865 * Returns a pointer to the char just after the var name.
1866 * Returns NULL if there is an error.
1867 */
1868 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001870 char_u *arg, // points to variable name
1871 typval_T *tv, // value to assign to variable
1872 int copy, // copy value from "tv"
1873 int is_const, // lock variable for const
1874 char_u *endchars, // valid chars after variable name or NULL
1875 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876{
1877 int c1;
1878 char_u *name;
1879 char_u *p;
1880 char_u *arg_end = NULL;
1881 int len;
1882 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884
1885 /*
1886 * ":let $VAR = expr": Set environment variable.
1887 */
1888 if (*arg == '$')
1889 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001890 if (is_const)
1891 {
1892 emsg(_("E996: Cannot lock an environment variable"));
1893 return NULL;
1894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 /* Find the end of the name. */
1896 ++arg;
1897 name = arg;
1898 len = get_env_len(&arg);
1899 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001900 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 else
1902 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001903 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001907 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001908 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 {
1910 c1 = name[len];
1911 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001912 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001913 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 {
1915 int mustfree = FALSE;
1916 char_u *s = vim_getenv(name, &mustfree);
1917
1918 if (s != NULL)
1919 {
1920 p = tofree = concat_str(s, p);
1921 if (mustfree)
1922 vim_free(s);
1923 }
1924 }
1925 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001926 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001928 if (STRICMP(name, "HOME") == 0)
1929 init_homedir();
1930 else if (didset_vim && STRICMP(name, "VIM") == 0)
1931 didset_vim = FALSE;
1932 else if (didset_vimruntime
1933 && STRICMP(name, "VIMRUNTIME") == 0)
1934 didset_vimruntime = FALSE;
1935 arg_end = arg;
1936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
1940 }
1941 }
1942
1943 /*
1944 * ":let &option = expr": Set option value.
1945 * ":let &l:option = expr": Set local option value.
1946 * ":let &g:option = expr": Set global option value.
1947 */
1948 else if (*arg == '&')
1949 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001950 if (is_const)
1951 {
1952 emsg(_("E996: Cannot lock an option"));
1953 return NULL;
1954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 /* Find the end of the name. */
1956 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 if (p == NULL || (endchars != NULL
1958 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 else
1961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 long n;
1963 int opt_type;
1964 long numval;
1965 char_u *stringval = NULL;
1966 char_u *s;
1967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 c1 = *p;
1969 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001971 n = (long)tv_get_number(tv);
1972 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001973 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 {
1975 opt_type = get_option_value(arg, &numval,
1976 &stringval, opt_flags);
1977 if ((opt_type == 1 && *op == '.')
1978 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001981 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 else
1984 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001985 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001987 switch (*op)
1988 {
1989 case '+': n = numval + n; break;
1990 case '-': n = numval - n; break;
1991 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001992 case '/': n = (long)num_divide(numval, n); break;
1993 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001994 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001996 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 {
1998 s = concat_str(stringval, s);
1999 vim_free(stringval);
2000 stringval = s;
2001 }
2002 }
2003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002004 if (s != NULL)
2005 {
2006 set_option_value(arg, n, s, opt_flags);
2007 arg_end = p;
2008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002011 }
2012 }
2013
2014 /*
2015 * ":let @r = expr": Set register contents.
2016 */
2017 else if (*arg == '@')
2018 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002019 if (is_const)
2020 {
2021 emsg(_("E996: Cannot lock a register"));
2022 return NULL;
2023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002025 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 else
2031 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002032 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 char_u *s;
2034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002035 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002038 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002039 if (s != NULL)
2040 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002041 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 vim_free(s);
2043 }
2044 }
2045 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002048 arg_end = arg + 1;
2049 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002050 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 }
2052 }
2053
2054 /*
2055 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002058 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002060 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002062 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002066 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 else
2068 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002069 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 arg_end = p;
2071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 }
2075
2076 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078
2079 return arg_end;
2080}
2081
2082/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 * Get an lval: variable, Dict item or List item that can be assigned a value
2084 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2085 * "name.key", "name.key[expr]" etc.
2086 * Indexing only works if "name" is an existing List or Dictionary.
2087 * "name" points to the start of the name.
2088 * If "rettv" is not NULL it points to the value to be assigned.
2089 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2090 * wrong; must end in space or cmd separator.
2091 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002092 * flags:
2093 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002094 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002095 * GLV_NO_AUTOLOAD: do not use script autoloading
2096 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002102get_lval(
2103 char_u *name,
2104 typval_T *rettv,
2105 lval_T *lp,
2106 int unlet,
2107 int skip,
2108 int flags, /* GLV_ values */
2109 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 char_u *expr_start, *expr_end;
2113 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 dictitem_T *v;
2115 typval_T var1;
2116 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002122 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126
2127 if (skip)
2128 {
2129 /* When skipping just find the end of the name. */
2130 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002131 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 }
2133
2134 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002135 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (expr_start != NULL)
2137 {
2138 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002139 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 && *p != '[' && *p != '.')
2141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002142 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 return NULL;
2144 }
2145
2146 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2147 if (lp->ll_exp_name == NULL)
2148 {
2149 /* Report an invalid expression in braces, unless the
2150 * expression evaluation has been cancelled due to an
2151 * aborting error, an interrupt, or an exception. */
2152 if (!aborting() && !quiet)
2153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002154 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002155 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 return NULL;
2157 }
2158 }
2159 lp->ll_name = lp->ll_exp_name;
2160 }
2161 else
2162 lp->ll_name = name;
2163
2164 /* Without [idx] or .key we are done. */
2165 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2166 return p;
2167
2168 cc = *p;
2169 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002170 /* Only pass &ht when we would write to the variable, it prevents autoload
2171 * as well. */
2172 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2173 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002175 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 if (v == NULL)
2178 return NULL;
2179
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 /*
2181 * Loop until no more [idx] or .key is following.
2182 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002184 var1.v_type = VAR_UNKNOWN;
2185 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2189 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002190 && lp->ll_tv->vval.v_dict != NULL)
2191 && !(lp->ll_tv->v_type == VAR_BLOB
2192 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002195 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002204
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 len = -1;
2206 if (*p == '.')
2207 {
2208 key = p + 1;
2209 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2210 ;
2211 if (len == 0)
2212 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002214 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 }
2217 p = key + len;
2218 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002219 else
2220 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002221 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002222 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002223 if (*p == ':')
2224 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002225 else
2226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 empty1 = FALSE;
2228 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002230 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002231 {
2232 /* not a number or string */
2233 clear_tv(&var1);
2234 return NULL;
2235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002236 }
2237
2238 /* Optionally get the second index [ :expr]. */
2239 if (*p == ':')
2240 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002244 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002245 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002247 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002248 if (rettv != NULL
2249 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002250 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002251 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002252 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002255 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002256 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258 }
2259 p = skipwhite(p + 1);
2260 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002262 else
2263 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2266 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002267 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002269 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002270 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002271 {
2272 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002273 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 clear_tv(&var2);
2275 return NULL;
2276 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002277 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002282
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002284 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002286 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002287 clear_tv(&var1);
2288 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002290 }
2291
2292 /* Skip to past ']'. */
2293 ++p;
2294 }
2295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002297 {
2298 if (len == -1)
2299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002301 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002302 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002303 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002304 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306 }
2307 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 lp->ll_list = NULL;
2309 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002310 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002311
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002312 /* When assigning to a scope dictionary check that a function and
2313 * variable name is valid (only variable name unless it is l: or
2314 * g: dictionary). Disallow overwriting a builtin function. */
2315 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002316 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002317 int prevval;
2318 int wrong;
2319
2320 if (len != -1)
2321 {
2322 prevval = key[len];
2323 key[len] = NUL;
2324 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002325 else
2326 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002327 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2328 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002329 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002330 || !valid_varname(key);
2331 if (len != -1)
2332 key[len] = prevval;
2333 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002334 return NULL;
2335 }
2336
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002339 // Can't add "v:" or "a:" variable.
2340 if (lp->ll_dict == &vimvardict
2341 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002343 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002344 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002345 return NULL;
2346 }
2347
Bram Moolenaar31b81602019-02-10 22:14:27 +01002348 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002352 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002353 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 }
2356 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002360 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 p = NULL;
2363 break;
2364 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002365 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002366 else if ((flags & GLV_READ_ONLY) == 0
2367 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002368 {
2369 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002370 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002371 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002372
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002373 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002376 else if (lp->ll_tv->v_type == VAR_BLOB)
2377 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002378 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2379
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002380 /*
2381 * Get the number and item for the only or first index of the List.
2382 */
2383 if (empty1)
2384 lp->ll_n1 = 0;
2385 else
2386 // is number or string
2387 lp->ll_n1 = (long)tv_get_number(&var1);
2388 clear_tv(&var1);
2389
2390 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002391 || lp->ll_n1 > bloblen
2392 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002393 {
2394 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002395 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002396 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002397 return NULL;
2398 }
2399 if (lp->ll_range && !lp->ll_empty2)
2400 {
2401 lp->ll_n2 = (long)tv_get_number(&var2);
2402 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002403 if (lp->ll_n2 < 0
2404 || lp->ll_n2 >= bloblen
2405 || lp->ll_n2 < lp->ll_n1)
2406 {
2407 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002409 return NULL;
2410 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002411 }
2412 lp->ll_blob = lp->ll_tv->vval.v_blob;
2413 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002414 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 else
2417 {
2418 /*
2419 * Get the number and item for the only or first index of the List.
2420 */
2421 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002424 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002425 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002426 clear_tv(&var1);
2427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_list = lp->ll_tv->vval.v_list;
2430 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2431 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002433 if (lp->ll_n1 < 0)
2434 {
2435 lp->ll_n1 = 0;
2436 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2437 }
2438 }
2439 if (lp->ll_li == NULL)
2440 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002441 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002442 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002443 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 }
2446
2447 /*
2448 * May need to find the item or absolute index for the second
2449 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 * When no index given: "lp->ll_empty2" is TRUE.
2451 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002455 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002456 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002462 {
2463 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002464 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 }
2469
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2471 if (lp->ll_n1 < 0)
2472 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2473 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002474 {
2475 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002478 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 }
2480
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002485 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 return p;
2487}
2488
2489/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002490 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002493clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494{
2495 vim_free(lp->ll_exp_name);
2496 vim_free(lp->ll_newkey);
2497}
2498
2499/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002500 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002502 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2503 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 */
2505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002506set_var_lval(
2507 lval_T *lp,
2508 char_u *endp,
2509 typval_T *rettv,
2510 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002511 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002512 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513{
2514 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 listitem_T *ri;
2516 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517
2518 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002520 cc = *endp;
2521 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002522 if (lp->ll_blob != NULL)
2523 {
2524 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002525
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002526 if (op != NULL && *op != '=')
2527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002528 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002529 return;
2530 }
2531
2532 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2533 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002534 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002535
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002536 if (lp->ll_empty2)
2537 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2538
2539 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002541 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002542 return;
2543 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002544 if (lp->ll_empty2)
2545 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002546
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002547 ir = 0;
2548 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2549 blob_set(lp->ll_blob, il,
2550 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002551 }
2552 else
2553 {
2554 val = (int)tv_get_number_chk(rettv, &error);
2555 if (!error)
2556 {
2557 garray_T *gap = &lp->ll_blob->bv_ga;
2558
2559 // Allow for appending a byte. Setting a byte beyond
2560 // the end is an error otherwise.
2561 if (lp->ll_n1 < gap->ga_len
2562 || (lp->ll_n1 == gap->ga_len
2563 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2564 {
2565 blob_set(lp->ll_blob, lp->ll_n1, val);
2566 if (lp->ll_n1 == gap->ga_len)
2567 ++gap->ga_len;
2568 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002569 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002570 }
2571 }
2572 }
2573 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002575 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002576
Bram Moolenaar9937a052019-06-15 15:45:06 +02002577 if (is_const)
2578 {
2579 emsg(_(e_cannot_mod));
2580 *endp = cc;
2581 return;
2582 }
2583
Bram Moolenaarff697e62019-02-12 22:28:33 +01002584 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002585 di = NULL;
2586 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2587 &tv, &di, TRUE, FALSE) == OK)
2588 {
2589 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002590 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2591 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002592 && tv_op(&tv, rettv, op) == OK)
2593 set_var(lp->ll_name, &tv, FALSE);
2594 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002597 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002598 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002599 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002601 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002602 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002603 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002604 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 else if (lp->ll_range)
2606 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002607 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002608 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002609
Bram Moolenaar9937a052019-06-15 15:45:06 +02002610 if (is_const)
2611 {
2612 emsg(_("E996: Cannot lock a range"));
2613 return;
2614 }
2615
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002616 /*
2617 * Check whether any of the list items is locked
2618 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002619 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002620 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002621 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002622 return;
2623 ri = ri->li_next;
2624 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2625 break;
2626 ll_li = ll_li->li_next;
2627 ++ll_n1;
2628 }
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 /*
2631 * Assign the List values to the list items.
2632 */
2633 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635 if (op != NULL && *op != '=')
2636 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2637 else
2638 {
2639 clear_tv(&lp->ll_li->li_tv);
2640 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 ri = ri->li_next;
2643 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2644 break;
2645 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002648 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 ri = NULL;
2651 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_li = lp->ll_li->li_next;
2655 ++lp->ll_n1;
2656 }
2657 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002658 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 else if (lp->ll_empty2
2660 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002662 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 }
2664 else
2665 {
2666 /*
2667 * Assign to a List or Dictionary item.
2668 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002669 if (is_const)
2670 {
2671 emsg(_("E996: Cannot lock a list or dict"));
2672 return;
2673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_newkey != NULL)
2675 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002676 if (op != NULL && *op != '=')
2677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 return;
2680 }
2681
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002683 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (di == NULL)
2685 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002686 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2687 {
2688 vim_free(di);
2689 return;
2690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 else if (op != NULL && *op != '=')
2694 {
2695 tv_op(lp->ll_tv, rettv, op);
2696 return;
2697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 /*
2702 * Assign the value to the variable or list item.
2703 */
2704 if (copy)
2705 copy_tv(rettv, lp->ll_tv);
2706 else
2707 {
2708 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002709 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 }
2712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713}
2714
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002715/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002716 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2717 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 * Returns OK or FAIL.
2719 */
2720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002721tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002723 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 char_u numbuf[NUMBUFLEN];
2725 char_u *s;
2726
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002727 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2728 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2729 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 {
2731 switch (tv1->v_type)
2732 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002733 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 case VAR_DICT:
2735 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002736 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002737 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002738 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002739 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002740 break;
2741
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002742 case VAR_BLOB:
2743 if (*op != '+' || tv2->v_type != VAR_BLOB)
2744 break;
2745 // BLOB += BLOB
2746 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2747 {
2748 blob_T *b1 = tv1->vval.v_blob;
2749 blob_T *b2 = tv2->vval.v_blob;
2750 int i, len = blob_len(b2);
2751 for (i = 0; i < len; i++)
2752 ga_append(&b1->bv_ga, blob_get(b2, i));
2753 }
2754 return OK;
2755
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 case VAR_LIST:
2757 if (*op != '+' || tv2->v_type != VAR_LIST)
2758 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002759 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2761 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2762 return OK;
2763
2764 case VAR_NUMBER:
2765 case VAR_STRING:
2766 if (tv2->v_type == VAR_LIST)
2767 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002768 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002770 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002771 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002772#ifdef FEAT_FLOAT
2773 if (tv2->v_type == VAR_FLOAT)
2774 {
2775 float_T f = n;
2776
Bram Moolenaarff697e62019-02-12 22:28:33 +01002777 if (*op == '%')
2778 break;
2779 switch (*op)
2780 {
2781 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002786 clear_tv(tv1);
2787 tv1->v_type = VAR_FLOAT;
2788 tv1->vval.v_float = f;
2789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002790 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002791#endif
2792 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002793 switch (*op)
2794 {
2795 case '+': n += tv_get_number(tv2); break;
2796 case '-': n -= tv_get_number(tv2); break;
2797 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002798 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2799 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002800 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002801 clear_tv(tv1);
2802 tv1->v_type = VAR_NUMBER;
2803 tv1->vval.v_number = n;
2804 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002805 }
2806 else
2807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808 if (tv2->v_type == VAR_FLOAT)
2809 break;
2810
Bram Moolenaarff697e62019-02-12 22:28:33 +01002811 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002812 s = tv_get_string(tv1);
2813 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 clear_tv(tv1);
2815 tv1->v_type = VAR_STRING;
2816 tv1->vval.v_string = s;
2817 }
2818 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002819
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002820 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002821#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002822 {
2823 float_T f;
2824
Bram Moolenaarff697e62019-02-12 22:28:33 +01002825 if (*op == '%' || *op == '.'
2826 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002827 && tv2->v_type != VAR_NUMBER
2828 && tv2->v_type != VAR_STRING))
2829 break;
2830 if (tv2->v_type == VAR_FLOAT)
2831 f = tv2->vval.v_float;
2832 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002833 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002834 switch (*op)
2835 {
2836 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002842#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002843 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 }
2845 }
2846
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002847 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002848 return FAIL;
2849}
2850
2851/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 * Evaluate the expression used in a ":for var in expr" command.
2853 * "arg" points to "var".
2854 * Set "*errp" to TRUE for an error, FALSE otherwise;
2855 * Return a pointer that holds the info. Null when there is an error.
2856 */
2857 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002858eval_for_line(
2859 char_u *arg,
2860 int *errp,
2861 char_u **nextcmdp,
2862 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863{
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 typval_T tv;
2867 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002868
2869 *errp = TRUE; /* default: there is an error */
2870
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002871 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 if (fi == NULL)
2873 return NULL;
2874
2875 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2876 if (expr == NULL)
2877 return fi;
2878
2879 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002880 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002882 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883 return fi;
2884 }
2885
2886 if (skip)
2887 ++emsg_skip;
2888 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2889 {
2890 *errp = FALSE;
2891 if (!skip)
2892 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002893 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002894 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002895 l = tv.vval.v_list;
2896 if (l == NULL)
2897 {
2898 // a null list is like an empty list: do nothing
2899 clear_tv(&tv);
2900 }
2901 else
2902 {
2903 // No need to increment the refcount, it's already set for
2904 // the list being used in "tv".
2905 fi->fi_list = l;
2906 list_add_watch(l, &fi->fi_lw);
2907 fi->fi_lw.lw_item = l->lv_first;
2908 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002909 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002910 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002911 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002912 fi->fi_bi = 0;
2913 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002914 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002915 typval_T btv;
2916
2917 // Make a copy, so that the iteration still works when the
2918 // blob is changed.
2919 blob_copy(&tv, &btv);
2920 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002921 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002922 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002923 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 else
2925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002926 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002927 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 }
2929 }
2930 }
2931 if (skip)
2932 --emsg_skip;
2933
2934 return fi;
2935}
2936
2937/*
2938 * Use the first item in a ":for" list. Advance to the next.
2939 * Assign the values to the variable (list). "arg" points to the first one.
2940 * Return TRUE when a valid item was found, FALSE when at end of list or
2941 * something wrong.
2942 */
2943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002945{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002946 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002949
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002950 if (fi->fi_blob != NULL)
2951 {
2952 typval_T tv;
2953
2954 if (fi->fi_bi >= blob_len(fi->fi_blob))
2955 return FALSE;
2956 tv.v_type = VAR_NUMBER;
2957 tv.v_lock = VAR_FIXED;
2958 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2959 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002960 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2961 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002962 }
2963
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002964 item = fi->fi_lw.lw_item;
2965 if (item == NULL)
2966 result = FALSE;
2967 else
2968 {
2969 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002970 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2971 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 }
2973 return result;
2974}
2975
2976/*
2977 * Free the structure used to store info used by ":for".
2978 */
2979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002980free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981{
Bram Moolenaar33570922005-01-25 22:26:29 +00002982 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002983
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002984 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002985 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002987 list_unref(fi->fi_list);
2988 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002989 if (fi != NULL && fi->fi_blob != NULL)
2990 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 vim_free(fi);
2992}
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002995set_context_for_expression(
2996 expand_T *xp,
2997 char_u *arg,
2998 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 int got_eq = FALSE;
3001 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003002 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004 if (cmdidx == CMD_let)
3005 {
3006 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003007 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008 {
3009 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003010 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 {
3012 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003013 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003014 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015 break;
3016 }
3017 return;
3018 }
3019 }
3020 else
3021 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3022 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 while ((xp->xp_pattern = vim_strpbrk(arg,
3024 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3025 {
3026 c = *xp->xp_pattern;
3027 if (c == '&')
3028 {
3029 c = xp->xp_pattern[1];
3030 if (c == '&')
3031 {
3032 ++xp->xp_pattern;
3033 xp->xp_context = cmdidx != CMD_let || got_eq
3034 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3035 }
3036 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003039 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3040 xp->xp_pattern += 2;
3041
3042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 }
3044 else if (c == '$')
3045 {
3046 /* environment variable */
3047 xp->xp_context = EXPAND_ENV_VARS;
3048 }
3049 else if (c == '=')
3050 {
3051 got_eq = TRUE;
3052 xp->xp_context = EXPAND_EXPRESSION;
3053 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003054 else if (c == '#'
3055 && xp->xp_context == EXPAND_EXPRESSION)
3056 {
3057 /* Autoload function/variable contains '#'. */
3058 break;
3059 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003060 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 && xp->xp_context == EXPAND_FUNCTIONS
3062 && vim_strchr(xp->xp_pattern, '(') == NULL)
3063 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003064 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 break;
3066 }
3067 else if (cmdidx != CMD_let || got_eq)
3068 {
3069 if (c == '"') /* string */
3070 {
3071 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3072 if (c == '\\' && xp->xp_pattern[1] != NUL)
3073 ++xp->xp_pattern;
3074 xp->xp_context = EXPAND_NOTHING;
3075 }
3076 else if (c == '\'') /* literal string */
3077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003078 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3080 /* skip */ ;
3081 xp->xp_context = EXPAND_NOTHING;
3082 }
3083 else if (c == '|')
3084 {
3085 if (xp->xp_pattern[1] == '|')
3086 {
3087 ++xp->xp_pattern;
3088 xp->xp_context = EXPAND_EXPRESSION;
3089 }
3090 else
3091 xp->xp_context = EXPAND_COMMANDS;
3092 }
3093 else
3094 xp->xp_context = EXPAND_EXPRESSION;
3095 }
3096 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097 /* Doesn't look like something valid, expand as an expression
3098 * anyway. */
3099 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 arg = xp->xp_pattern;
3101 if (*arg != NUL)
3102 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3103 /* skip */ ;
3104 }
3105 xp->xp_pattern = arg;
3106}
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * ":unlet[!] var1 ... " command.
3110 */
3111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114 ex_unletlock(eap, eap->arg, 0);
3115}
3116
3117/*
3118 * ":lockvar" and ":unlockvar" commands
3119 */
3120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003121ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003124 int deep = 2;
3125
3126 if (eap->forceit)
3127 deep = -1;
3128 else if (vim_isdigit(*arg))
3129 {
3130 deep = getdigits(&arg);
3131 arg = skipwhite(arg);
3132 }
3133
3134 ex_unletlock(eap, arg, deep);
3135}
3136
3137/*
3138 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3139 */
3140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003141ex_unletlock(
3142 exarg_T *eap,
3143 char_u *argstart,
3144 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145{
3146 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003149 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 do
3152 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003153 if (*arg == '$')
3154 {
3155 char_u *name = ++arg;
3156
3157 if (get_env_len(&arg) == 0)
3158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003159 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003160 return;
3161 }
3162 vim_unsetenv(name);
3163 arg = skipwhite(arg);
3164 continue;
3165 }
3166
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003167 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003168 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003169 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 if (lv.ll_name == NULL)
3171 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003172 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003173 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003175 if (name_end != NULL)
3176 {
3177 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003178 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003179 }
3180 if (!(eap->skip || error))
3181 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 break;
3183 }
3184
3185 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003186 {
3187 if (eap->cmdidx == CMD_unlet)
3188 {
3189 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3190 error = TRUE;
3191 }
3192 else
3193 {
3194 if (do_lock_var(&lv, name_end, deep,
3195 eap->cmdidx == CMD_lockvar) == FAIL)
3196 error = TRUE;
3197 }
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 if (!eap->skip)
3201 clear_lval(&lv);
3202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 arg = skipwhite(name_end);
3204 } while (!ends_excmd(*arg));
3205
3206 eap->nextcmd = check_nextcmd(arg);
3207}
3208
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210do_unlet_var(
3211 lval_T *lp,
3212 char_u *name_end,
3213 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 int ret = OK;
3216 int cc;
3217
3218 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 cc = *name_end;
3221 *name_end = NUL;
3222
3223 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003224 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003226 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003227 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003228 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003229 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003230 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003231 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003232 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 else if (lp->ll_range)
3234 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003236 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003237 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003238
3239 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3240 {
3241 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003242 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003243 return FAIL;
3244 ll_li = li;
3245 ++ll_n1;
3246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247
3248 /* Delete a range of List items. */
3249 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3250 {
3251 li = lp->ll_li->li_next;
3252 listitem_remove(lp->ll_list, lp->ll_li);
3253 lp->ll_li = li;
3254 ++lp->ll_n1;
3255 }
3256 }
3257 else
3258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 /* unlet a List item. */
3261 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003263 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003264 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 }
3266
3267 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003268}
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270/*
3271 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
3274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003275do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 hashtab_T *ht;
3278 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003279 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003280 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003281 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 ht = find_var_ht(name, &varname);
3284 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003287 if (d == NULL)
3288 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 if (ht == &globvarht)
3290 d = &globvardict;
3291 else if (ht == &compat_hashtab)
3292 d = &vimvardict;
3293 else
3294 {
3295 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3296 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3297 }
3298 if (d == NULL)
3299 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003300 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301 return FAIL;
3302 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003303 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003305 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003306 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003307 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003308 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003309 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003310 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003311 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003312 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003313 return FAIL;
3314
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003315 delete_var(ht, hi);
3316 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003319 if (forceit)
3320 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003321 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return FAIL;
3323}
3324
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325/*
3326 * Lock or unlock variable indicated by "lp".
3327 * "deep" is the levels to go (-1 for unlimited);
3328 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3329 */
3330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003331do_lock_var(
3332 lval_T *lp,
3333 char_u *name_end,
3334 int deep,
3335 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003336{
3337 int ret = OK;
3338 int cc;
3339 dictitem_T *di;
3340
3341 if (deep == 0) /* nothing to do */
3342 return OK;
3343
3344 if (lp->ll_tv == NULL)
3345 {
3346 cc = *name_end;
3347 *name_end = NUL;
3348
3349 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003350 di = find_var(lp->ll_name, NULL, TRUE);
3351 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003352 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003353 else if ((di->di_flags & DI_FLAGS_FIX)
3354 && di->di_tv.v_type != VAR_DICT
3355 && di->di_tv.v_type != VAR_LIST)
3356 /* For historic reasons this error is not given for a list or dict.
3357 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003358 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003359 else
3360 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003361 if (lock)
3362 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003363 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003364 di->di_flags &= ~DI_FLAGS_LOCK;
3365 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 }
3367 *name_end = cc;
3368 }
3369 else if (lp->ll_range)
3370 {
3371 listitem_T *li = lp->ll_li;
3372
3373 /* (un)lock a range of List items. */
3374 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3375 {
3376 item_lock(&li->li_tv, deep, lock);
3377 li = li->li_next;
3378 ++lp->ll_n1;
3379 }
3380 }
3381 else if (lp->ll_list != NULL)
3382 /* (un)lock a List item. */
3383 item_lock(&lp->ll_li->li_tv, deep, lock);
3384 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003385 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003386 item_lock(&lp->ll_di->di_tv, deep, lock);
3387
3388 return ret;
3389}
3390
3391/*
3392 * Lock or unlock an item. "deep" is nr of levels to go.
3393 */
3394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003395item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003396{
3397 static int recurse = 0;
3398 list_T *l;
3399 listitem_T *li;
3400 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003401 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003402 hashitem_T *hi;
3403 int todo;
3404
3405 if (recurse >= DICT_MAXNEST)
3406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003407 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003408 return;
3409 }
3410 if (deep == 0)
3411 return;
3412 ++recurse;
3413
3414 /* lock/unlock the item itself */
3415 if (lock)
3416 tv->v_lock |= VAR_LOCKED;
3417 else
3418 tv->v_lock &= ~VAR_LOCKED;
3419
3420 switch (tv->v_type)
3421 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003422 case VAR_UNKNOWN:
3423 case VAR_NUMBER:
3424 case VAR_STRING:
3425 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003426 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003427 case VAR_FLOAT:
3428 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003429 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003430 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003431 break;
3432
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003433 case VAR_BLOB:
3434 if ((b = tv->vval.v_blob) != NULL)
3435 {
3436 if (lock)
3437 b->bv_lock |= VAR_LOCKED;
3438 else
3439 b->bv_lock &= ~VAR_LOCKED;
3440 }
3441 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003442 case VAR_LIST:
3443 if ((l = tv->vval.v_list) != NULL)
3444 {
3445 if (lock)
3446 l->lv_lock |= VAR_LOCKED;
3447 else
3448 l->lv_lock &= ~VAR_LOCKED;
3449 if (deep < 0 || deep > 1)
3450 /* recursive: lock/unlock the items the List contains */
3451 for (li = l->lv_first; li != NULL; li = li->li_next)
3452 item_lock(&li->li_tv, deep - 1, lock);
3453 }
3454 break;
3455 case VAR_DICT:
3456 if ((d = tv->vval.v_dict) != NULL)
3457 {
3458 if (lock)
3459 d->dv_lock |= VAR_LOCKED;
3460 else
3461 d->dv_lock &= ~VAR_LOCKED;
3462 if (deep < 0 || deep > 1)
3463 {
3464 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003465 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003466 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3467 {
3468 if (!HASHITEM_EMPTY(hi))
3469 {
3470 --todo;
3471 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3472 }
3473 }
3474 }
3475 }
3476 }
3477 --recurse;
3478}
3479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3481/*
3482 * Delete all "menutrans_" variables.
3483 */
3484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003485del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003488 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar33570922005-01-25 22:26:29 +00003490 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003491 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003492 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003493 {
3494 if (!HASHITEM_EMPTY(hi))
3495 {
3496 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3498 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003499 }
3500 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503#endif
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505/*
3506 * Local string buffer for the next two functions to store a variable name
3507 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3508 * get_user_var_name().
3509 */
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511static char_u *varnamebuf = NULL;
3512static int varnamebuflen = 0;
3513
3514/*
3515 * Function to concatenate a prefix and a variable name.
3516 */
3517 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003518cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 int len;
3521
3522 len = (int)STRLEN(name) + 3;
3523 if (len > varnamebuflen)
3524 {
3525 vim_free(varnamebuf);
3526 len += 10; /* some additional space */
3527 varnamebuf = alloc(len);
3528 if (varnamebuf == NULL)
3529 {
3530 varnamebuflen = 0;
3531 return NULL;
3532 }
3533 varnamebuflen = len;
3534 }
3535 *varnamebuf = prefix;
3536 varnamebuf[1] = ':';
3537 STRCPY(varnamebuf + 2, name);
3538 return varnamebuf;
3539}
3540
3541/*
3542 * Function given to ExpandGeneric() to obtain the list of user defined
3543 * (global/buffer/window/built-in) variable names.
3544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003546get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003548 static long_u gdone;
3549 static long_u bdone;
3550 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003551 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003552 static int vidx;
3553 static hashitem_T *hi;
3554 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003557 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003559 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003561
3562 /* Global variables */
3563 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003565 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003567 else
3568 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003569 while (HASHITEM_EMPTY(hi))
3570 ++hi;
3571 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3572 return cat_prefix_varname('g', hi->hi_key);
3573 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003575
3576 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003577 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003581 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003582 else
3583 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003584 while (HASHITEM_EMPTY(hi))
3585 ++hi;
3586 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003588
3589 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003590 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003593 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003595 else
3596 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003597 while (HASHITEM_EMPTY(hi))
3598 ++hi;
3599 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003601
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003602 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003603 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003604 if (tdone < ht->ht_used)
3605 {
3606 if (tdone++ == 0)
3607 hi = ht->ht_array;
3608 else
3609 ++hi;
3610 while (HASHITEM_EMPTY(hi))
3611 ++hi;
3612 return cat_prefix_varname('t', hi->hi_key);
3613 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003614
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 /* v: variables */
3616 if (vidx < VV_LEN)
3617 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
Bram Moolenaard23a8232018-02-10 18:45:26 +01003619 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 varnamebuflen = 0;
3621 return NULL;
3622}
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003625 * Return TRUE if "pat" matches "text".
3626 * Does not use 'cpo' and always uses 'magic'.
3627 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003628 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003629pattern_match(char_u *pat, char_u *text, int ic)
3630{
3631 int matches = FALSE;
3632 char_u *save_cpo;
3633 regmatch_T regmatch;
3634
3635 /* avoid 'l' flag in 'cpoptions' */
3636 save_cpo = p_cpo;
3637 p_cpo = (char_u *)"";
3638 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3639 if (regmatch.regprog != NULL)
3640 {
3641 regmatch.rm_ic = ic;
3642 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3643 vim_regfree(regmatch.regprog);
3644 }
3645 p_cpo = save_cpo;
3646 return matches;
3647}
3648
3649/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003650 * Handle a name followed by "(". Both for just "name(arg)" and for
3651 * "expr->name(arg)".
3652 * Returns OK or FAIL.
3653 */
3654 static int
3655eval_func(
3656 char_u **arg, // points to "(", will be advanced
3657 char_u *name,
3658 int name_len,
3659 typval_T *rettv,
3660 int evaluate,
3661 typval_T *basetv) // "expr" for "expr->name(arg)"
3662{
3663 char_u *s = name;
3664 int len = name_len;
3665 partial_T *partial;
3666 int ret = OK;
3667
3668 if (!evaluate)
3669 check_vars(s, len);
3670
3671 /* If "s" is the name of a variable of type VAR_FUNC
3672 * use its contents. */
3673 s = deref_func_name(s, &len, &partial, !evaluate);
3674
3675 /* Need to make a copy, in case evaluating the arguments makes
3676 * the name invalid. */
3677 s = vim_strsave(s);
3678 if (s == NULL)
3679 ret = FAIL;
3680 else
3681 {
3682 funcexe_T funcexe;
3683
3684 // Invoke the function.
3685 vim_memset(&funcexe, 0, sizeof(funcexe));
3686 funcexe.firstline = curwin->w_cursor.lnum;
3687 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003688 funcexe.evaluate = evaluate;
3689 funcexe.partial = partial;
3690 funcexe.basetv = basetv;
3691 ret = get_func_tv(s, len, rettv, arg, &funcexe);
3692 }
3693 vim_free(s);
3694
3695 /* If evaluate is FALSE rettv->v_type was not set in
3696 * get_func_tv, but it's needed in handle_subscript() to parse
3697 * what follows. So set it here. */
3698 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3699 {
3700 rettv->vval.v_string = NULL;
3701 rettv->v_type = VAR_FUNC;
3702 }
3703
3704 /* Stop the expression evaluation when immediately
3705 * aborting on error, or when an interrupt occurred or
3706 * an exception was thrown but not caught. */
3707 if (evaluate && aborting())
3708 {
3709 if (ret == OK)
3710 clear_tv(rettv);
3711 ret = FAIL;
3712 }
3713 return ret;
3714}
3715
3716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3720 */
3721
3722/*
3723 * Handle zero level expression.
3724 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003726 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 * Return OK or FAIL.
3728 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003730eval0(
3731 char_u *arg,
3732 typval_T *rettv,
3733 char_u **nextcmd,
3734 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
3736 int ret;
3737 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003738 int did_emsg_before = did_emsg;
3739 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (ret == FAIL || !ends_excmd(*p))
3744 {
3745 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /*
3748 * Report the invalid expression unless the expression evaluation has
3749 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003750 * exception, or we already gave a more specific error.
3751 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003753 if (!aborting() && did_emsg == did_emsg_before
3754 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003755 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 ret = FAIL;
3757 }
3758 if (nextcmd != NULL)
3759 *nextcmd = check_nextcmd(p);
3760
3761 return ret;
3762}
3763
3764/*
3765 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003766 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 *
3768 * "arg" must point to the first non-white of the expression.
3769 * "arg" is advanced to the next non-white after the recognized expression.
3770 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003771 * Note: "rettv.v_lock" is not set.
3772 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 * Return OK or FAIL.
3774 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003776eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
3778 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
3781 /*
3782 * Get the first variable.
3783 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return FAIL;
3786
3787 if ((*arg)[0] == '?')
3788 {
3789 result = FALSE;
3790 if (evaluate)
3791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 int error = FALSE;
3793
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003794 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 if (error)
3798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800
3801 /*
3802 * Get the second variable.
3803 */
3804 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 return FAIL;
3807
3808 /*
3809 * Check for the ":".
3810 */
3811 if ((*arg)[0] != ':')
3812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003813 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 return FAIL;
3817 }
3818
3819 /*
3820 * Get the third variable.
3821 */
3822 *arg = skipwhite(*arg + 1);
3823 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3824 {
3825 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003826 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 return FAIL;
3828 }
3829 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
3833 return OK;
3834}
3835
3836/*
3837 * Handle first level expression:
3838 * expr2 || expr2 || expr2 logical OR
3839 *
3840 * "arg" must point to the first non-white of the expression.
3841 * "arg" is advanced to the next non-white after the recognized expression.
3842 *
3843 * Return OK or FAIL.
3844 */
3845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003846eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 long result;
3850 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003851 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 /*
3854 * Get the first variable.
3855 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return FAIL;
3858
3859 /*
3860 * Repeat until there is no following "||".
3861 */
3862 first = TRUE;
3863 result = FALSE;
3864 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3865 {
3866 if (evaluate && first)
3867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003868 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003870 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003871 if (error)
3872 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 first = FALSE;
3874 }
3875
3876 /*
3877 * Get the second variable.
3878 */
3879 *arg = skipwhite(*arg + 2);
3880 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3881 return FAIL;
3882
3883 /*
3884 * Compute the result.
3885 */
3886 if (evaluate && !result)
3887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003888 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003890 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 if (error)
3892 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 if (evaluate)
3895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003896 rettv->v_type = VAR_NUMBER;
3897 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 }
3900
3901 return OK;
3902}
3903
3904/*
3905 * Handle second level expression:
3906 * expr3 && expr3 && expr3 logical AND
3907 *
3908 * "arg" must point to the first non-white of the expression.
3909 * "arg" is advanced to the next non-white after the recognized expression.
3910 *
3911 * Return OK or FAIL.
3912 */
3913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003914eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
Bram Moolenaar33570922005-01-25 22:26:29 +00003916 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 long result;
3918 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003919 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 /*
3922 * Get the first variable.
3923 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003924 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 return FAIL;
3926
3927 /*
3928 * Repeat until there is no following "&&".
3929 */
3930 first = TRUE;
3931 result = TRUE;
3932 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3933 {
3934 if (evaluate && first)
3935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003936 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003938 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003939 if (error)
3940 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 first = FALSE;
3942 }
3943
3944 /*
3945 * Get the second variable.
3946 */
3947 *arg = skipwhite(*arg + 2);
3948 if (eval4(arg, &var2, evaluate && result) == FAIL)
3949 return FAIL;
3950
3951 /*
3952 * Compute the result.
3953 */
3954 if (evaluate && result)
3955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003956 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003958 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003959 if (error)
3960 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 if (evaluate)
3963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 rettv->v_type = VAR_NUMBER;
3965 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 }
3968
3969 return OK;
3970}
3971
3972/*
3973 * Handle third level expression:
3974 * var1 == var2
3975 * var1 =~ var2
3976 * var1 != var2
3977 * var1 !~ var2
3978 * var1 > var2
3979 * var1 >= var2
3980 * var1 < var2
3981 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003982 * var1 is var2
3983 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *
3985 * "arg" must point to the first non-white of the expression.
3986 * "arg" is advanced to the next non-white after the recognized expression.
3987 *
3988 * Return OK or FAIL.
3989 */
3990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003991eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 char_u *p;
3995 int i;
3996 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003997 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 /*
4002 * Get the first variable.
4003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return FAIL;
4006
4007 p = *arg;
4008 switch (p[0])
4009 {
4010 case '=': if (p[1] == '=')
4011 type = TYPE_EQUAL;
4012 else if (p[1] == '~')
4013 type = TYPE_MATCH;
4014 break;
4015 case '!': if (p[1] == '=')
4016 type = TYPE_NEQUAL;
4017 else if (p[1] == '~')
4018 type = TYPE_NOMATCH;
4019 break;
4020 case '>': if (p[1] != '=')
4021 {
4022 type = TYPE_GREATER;
4023 len = 1;
4024 }
4025 else
4026 type = TYPE_GEQUAL;
4027 break;
4028 case '<': if (p[1] != '=')
4029 {
4030 type = TYPE_SMALLER;
4031 len = 1;
4032 }
4033 else
4034 type = TYPE_SEQUAL;
4035 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004036 case 'i': if (p[1] == 's')
4037 {
4038 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4039 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004040 i = p[len];
4041 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 {
4043 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4044 type_is = TRUE;
4045 }
4046 }
4047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049
4050 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004051 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 */
4053 if (type != TYPE_UNKNOWN)
4054 {
4055 /* extra question mark appended: ignore case */
4056 if (p[len] == '?')
4057 {
4058 ic = TRUE;
4059 ++len;
4060 }
4061 /* extra '#' appended: match case */
4062 else if (p[len] == '#')
4063 {
4064 ic = FALSE;
4065 ++len;
4066 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004067 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else
4069 ic = p_ic;
4070
4071 /*
4072 * Get the second variable.
4073 */
4074 *arg = skipwhite(p + len);
4075 if (eval5(arg, &var2, evaluate) == FAIL)
4076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004080 if (evaluate)
4081 {
4082 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4083
4084 clear_tv(&var2);
4085 return ret;
4086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088
4089 return OK;
4090}
4091
4092/*
4093 * Handle fourth level expression:
4094 * + number addition
4095 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004096 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004097 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 *
4099 * "arg" must point to the first non-white of the expression.
4100 * "arg" is advanced to the next non-white after the recognized expression.
4101 *
4102 * Return OK or FAIL.
4103 */
4104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004105eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaar33570922005-01-25 22:26:29 +00004107 typval_T var2;
4108 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004110 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004111#ifdef FEAT_FLOAT
4112 float_T f1 = 0, f2 = 0;
4113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 char_u *s1, *s2;
4115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4116 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004117 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 /*
4120 * Get the first variable.
4121 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004122 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Repeat computing, until no '+', '-' or '.' is following.
4127 */
4128 for (;;)
4129 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004130 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004132 concat = op == '.'
4133 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4134 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 break;
4136
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004137 if ((op != '+' || (rettv->v_type != VAR_LIST
4138 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004139#ifdef FEAT_FLOAT
4140 && (op == '.' || rettv->v_type != VAR_FLOAT)
4141#endif
4142 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 {
4144 /* For "list + ...", an illegal use of the first operand as
4145 * a number cannot be determined before evaluating the 2nd
4146 * operand: if this is also a list, all is ok.
4147 * For "something . ...", "something - ..." or "non-list + ...",
4148 * we know that the first operand needs to be a string or number
4149 * without evaluating the 2nd operand. So check before to avoid
4150 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004151 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 {
4153 clear_tv(rettv);
4154 return FAIL;
4155 }
4156 }
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 /*
4159 * Get the second variable.
4160 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004161 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4162 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004164 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168 }
4169
4170 if (evaluate)
4171 {
4172 /*
4173 * Compute the result.
4174 */
4175 if (op == '.')
4176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004177 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4178 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (s2 == NULL) /* type error ? */
4180 {
4181 clear_tv(rettv);
4182 clear_tv(&var2);
4183 return FAIL;
4184 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004185 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
4187 rettv->v_type = VAR_STRING;
4188 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004190 else if (op == '+' && rettv->v_type == VAR_BLOB
4191 && var2.v_type == VAR_BLOB)
4192 {
4193 blob_T *b1 = rettv->vval.v_blob;
4194 blob_T *b2 = var2.vval.v_blob;
4195 blob_T *b = blob_alloc();
4196 int i;
4197
4198 if (b != NULL)
4199 {
4200 for (i = 0; i < blob_len(b1); i++)
4201 ga_append(&b->bv_ga, blob_get(b1, i));
4202 for (i = 0; i < blob_len(b2); i++)
4203 ga_append(&b->bv_ga, blob_get(b2, i));
4204
4205 clear_tv(rettv);
4206 rettv_blob_set(rettv, b);
4207 }
4208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004209 else if (op == '+' && rettv->v_type == VAR_LIST
4210 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004211 {
4212 /* concatenate Lists */
4213 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4214 &var3) == FAIL)
4215 {
4216 clear_tv(rettv);
4217 clear_tv(&var2);
4218 return FAIL;
4219 }
4220 clear_tv(rettv);
4221 *rettv = var3;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 else
4224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004225 int error = FALSE;
4226
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004227#ifdef FEAT_FLOAT
4228 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004230 f1 = rettv->vval.v_float;
4231 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004233 else
4234#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004236 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004237 if (error)
4238 {
4239 /* This can only happen for "list + non-list". For
4240 * "non-list + ..." or "something - ...", we returned
4241 * before evaluating the 2nd operand. */
4242 clear_tv(rettv);
4243 return FAIL;
4244 }
4245#ifdef FEAT_FLOAT
4246 if (var2.v_type == VAR_FLOAT)
4247 f1 = n1;
4248#endif
4249 }
4250#ifdef FEAT_FLOAT
4251 if (var2.v_type == VAR_FLOAT)
4252 {
4253 f2 = var2.vval.v_float;
4254 n2 = 0;
4255 }
4256 else
4257#endif
4258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004259 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004260 if (error)
4261 {
4262 clear_tv(rettv);
4263 clear_tv(&var2);
4264 return FAIL;
4265 }
4266#ifdef FEAT_FLOAT
4267 if (rettv->v_type == VAR_FLOAT)
4268 f2 = n2;
4269#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004272
4273#ifdef FEAT_FLOAT
4274 /* If there is a float on either side the result is a float. */
4275 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4276 {
4277 if (op == '+')
4278 f1 = f1 + f2;
4279 else
4280 f1 = f1 - f2;
4281 rettv->v_type = VAR_FLOAT;
4282 rettv->vval.v_float = f1;
4283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004285#endif
4286 {
4287 if (op == '+')
4288 n1 = n1 + n2;
4289 else
4290 n1 = n1 - n2;
4291 rettv->v_type = VAR_NUMBER;
4292 rettv->vval.v_number = n1;
4293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298 return OK;
4299}
4300
4301/*
4302 * Handle fifth level expression:
4303 * * number multiplication
4304 * / number division
4305 * % number modulo
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval6(
4314 char_u **arg,
4315 typval_T *rettv,
4316 int evaluate,
4317 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004321 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004322#ifdef FEAT_FLOAT
4323 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004324 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004325#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 /*
4329 * Get the first variable.
4330 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004331 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 return FAIL;
4333
4334 /*
4335 * Repeat computing, until no '*', '/' or '%' is following.
4336 */
4337 for (;;)
4338 {
4339 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004340 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 break;
4342
4343 if (evaluate)
4344 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004345#ifdef FEAT_FLOAT
4346 if (rettv->v_type == VAR_FLOAT)
4347 {
4348 f1 = rettv->vval.v_float;
4349 use_float = TRUE;
4350 n1 = 0;
4351 }
4352 else
4353#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004354 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 if (error)
4357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 else
4360 n1 = 0;
4361
4362 /*
4363 * Get the second variable.
4364 */
4365 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004366 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return FAIL;
4368
4369 if (evaluate)
4370 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371#ifdef FEAT_FLOAT
4372 if (var2.v_type == VAR_FLOAT)
4373 {
4374 if (!use_float)
4375 {
4376 f1 = n1;
4377 use_float = TRUE;
4378 }
4379 f2 = var2.vval.v_float;
4380 n2 = 0;
4381 }
4382 else
4383#endif
4384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004385 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004386 clear_tv(&var2);
4387 if (error)
4388 return FAIL;
4389#ifdef FEAT_FLOAT
4390 if (use_float)
4391 f2 = n2;
4392#endif
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 /*
4396 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004399#ifdef FEAT_FLOAT
4400 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004402 if (op == '*')
4403 f1 = f1 * f2;
4404 else if (op == '/')
4405 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004406# ifdef VMS
4407 /* VMS crashes on divide by zero, work around it */
4408 if (f2 == 0.0)
4409 {
4410 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004411 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004412 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004413 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004414 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004415 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004416 }
4417 else
4418 f1 = f1 / f2;
4419# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004420 /* We rely on the floating point library to handle divide
4421 * by zero to result in "inf" and not a crash. */
4422 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004423# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004427 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428 return FAIL;
4429 }
4430 rettv->v_type = VAR_FLOAT;
4431 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004436 if (op == '*')
4437 n1 = n1 * n2;
4438 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004439 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004441 n1 = num_modulus(n1, n2);
4442
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 rettv->v_type = VAR_NUMBER;
4444 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 }
4448
4449 return OK;
4450}
4451
4452/*
4453 * Handle sixth level expression:
4454 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004455 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004456 * "string" string constant
4457 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 * &option-name option value
4459 * @r register contents
4460 * identifier variable value
4461 * function() function call
4462 * $VAR environment variable
4463 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004464 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004465 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004466 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 *
4468 * Also handle:
4469 * ! in front logical NOT
4470 * - in front unary minus
4471 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004472 * trailing [] subscript in String or List
4473 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004474 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 *
4476 * "arg" must point to the first non-white of the expression.
4477 * "arg" is advanced to the next non-white after the recognized expression.
4478 *
4479 * Return OK or FAIL.
4480 */
4481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004482eval7(
4483 char_u **arg,
4484 typval_T *rettv,
4485 int evaluate,
4486 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004488 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 int len;
4490 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 char_u *start_leader, *end_leader;
4492 int ret = OK;
4493 char_u *alias;
4494
4495 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004502 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 */
4504 start_leader = *arg;
4505 while (**arg == '!' || **arg == '-' || **arg == '+')
4506 *arg = skipwhite(*arg + 1);
4507 end_leader = *arg;
4508
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004509 if (**arg == '.' && (!isdigit(*(*arg + 1))
4510#ifdef FEAT_FLOAT
4511 || current_sctx.sc_version < 2
4512#endif
4513 ))
4514 {
4515 semsg(_(e_invexpr2), *arg);
4516 ++*arg;
4517 return FAIL;
4518 }
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 switch (**arg)
4521 {
4522 /*
4523 * Number constant.
4524 */
4525 case '0':
4526 case '1':
4527 case '2':
4528 case '3':
4529 case '4':
4530 case '5':
4531 case '6':
4532 case '7':
4533 case '8':
4534 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004535 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536 {
4537#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004538 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004539 int get_float = FALSE;
4540
4541 /* We accept a float when the format matches
4542 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004543 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004544 * With script version 2 and later the leading digit can be
4545 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004546 * Don't look for a float after the "." operator, so that
4547 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004548 if (**arg == '.')
4549 p = *arg;
4550 else
4551 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004552 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004554 get_float = TRUE;
4555 p = skipdigits(p + 2);
4556 if (*p == 'e' || *p == 'E')
4557 {
4558 ++p;
4559 if (*p == '-' || *p == '+')
4560 ++p;
4561 if (!vim_isdigit(*p))
4562 get_float = FALSE;
4563 else
4564 p = skipdigits(p + 1);
4565 }
4566 if (ASCII_ISALPHA(*p) || *p == '.')
4567 get_float = FALSE;
4568 }
4569 if (get_float)
4570 {
4571 float_T f;
4572
4573 *arg += string2float(*arg, &f);
4574 if (evaluate)
4575 {
4576 rettv->v_type = VAR_FLOAT;
4577 rettv->vval.v_float = f;
4578 }
4579 }
4580 else
4581#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004582 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004583 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004584 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004585 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004586
4587 // Blob constant: 0z0123456789abcdef
4588 if (evaluate)
4589 blob = blob_alloc();
4590 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4591 {
4592 if (!vim_isxdigit(bp[1]))
4593 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004594 if (blob != NULL)
4595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004596 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004597 ga_clear(&blob->bv_ga);
4598 VIM_CLEAR(blob);
4599 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004600 ret = FAIL;
4601 break;
4602 }
4603 if (blob != NULL)
4604 ga_append(&blob->bv_ga,
4605 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004606 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4607 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004608 }
4609 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004610 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004611 *arg = bp;
4612 }
4613 else
4614 {
4615 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004616 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4617 if (len == 0)
4618 {
4619 semsg(_(e_invexpr2), *arg);
4620 ret = FAIL;
4621 break;
4622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623 *arg += len;
4624 if (evaluate)
4625 {
4626 rettv->v_type = VAR_NUMBER;
4627 rettv->vval.v_number = n;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633 /*
4634 * String constant: "string".
4635 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638
4639 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 break;
4644
4645 /*
4646 * List: [expr, expr]
4647 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004648 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650
4651 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004652 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004653 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004654 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004655 {
4656 ++*arg;
4657 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4658 }
4659 else
4660 ret = NOTDONE;
4661 break;
4662
4663 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004664 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004665 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004667 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4668 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004669 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 break;
4671
4672 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004673 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004675 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 break;
4677
4678 /*
4679 * Environment variable: $VAR.
4680 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 break;
4683
4684 /*
4685 * Register contents: @r.
4686 */
4687 case '@': ++*arg;
4688 if (evaluate)
4689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004691 rettv->vval.v_string = get_reg_contents(**arg,
4692 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 if (**arg != NUL)
4695 ++*arg;
4696 break;
4697
4698 /*
4699 * nested expression: (expression).
4700 */
4701 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (**arg == ')')
4704 ++*arg;
4705 else if (ret == OK)
4706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004707 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004708 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 ret = FAIL;
4710 }
4711 break;
4712
Bram Moolenaar8c711452005-01-14 21:53:12 +00004713 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 break;
4715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716
4717 if (ret == NOTDONE)
4718 {
4719 /*
4720 * Must be a variable or function name.
4721 * Can also be a curly-braces kind of name: {expr}.
4722 */
4723 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725 if (alias != NULL)
4726 s = alias;
4727
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004728 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 ret = FAIL;
4730 else
4731 {
4732 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004733 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004735 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004736 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004737 {
4738 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004739 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004742 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 }
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 *arg = skipwhite(*arg);
4746
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004747 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004748 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004749 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004750 ret = handle_subscript(arg, rettv, evaluate, TRUE,
4751 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 /*
4754 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4755 */
4756 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004757 ret = eval7_leader(rettv, start_leader, &end_leader);
4758 return ret;
4759}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004760
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004761/*
4762 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
4763 * Adjusts "end_leaderp" until it is at "start_leader".
4764 */
4765 static int
4766eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
4767{
4768 char_u *end_leader = *end_leaderp;
4769 int ret = OK;
4770 int error = FALSE;
4771 varnumber_T val = 0;
4772#ifdef FEAT_FLOAT
4773 float_T f = 0.0;
4774
4775 if (rettv->v_type == VAR_FLOAT)
4776 f = rettv->vval.v_float;
4777 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004779 val = tv_get_number_chk(rettv, &error);
4780 if (error)
4781 {
4782 clear_tv(rettv);
4783 ret = FAIL;
4784 }
4785 else
4786 {
4787 while (end_leader > start_leader)
4788 {
4789 --end_leader;
4790 if (*end_leader == '!')
4791 {
4792#ifdef FEAT_FLOAT
4793 if (rettv->v_type == VAR_FLOAT)
4794 f = !f;
4795 else
4796#endif
4797 val = !val;
4798 }
4799 else if (*end_leader == '-')
4800 {
4801#ifdef FEAT_FLOAT
4802 if (rettv->v_type == VAR_FLOAT)
4803 f = -f;
4804 else
4805#endif
4806 val = -val;
4807 }
4808 }
4809#ifdef FEAT_FLOAT
4810 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004813 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004816#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004818 clear_tv(rettv);
4819 rettv->v_type = VAR_NUMBER;
4820 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004823 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return ret;
4825}
4826
4827/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004828 * Call the function referred to in "rettv".
4829 */
4830 static int
4831call_func_rettv(
4832 char_u **arg,
4833 typval_T *rettv,
4834 int evaluate,
4835 dict_T *selfdict,
4836 typval_T *basetv)
4837{
4838 partial_T *pt = NULL;
4839 funcexe_T funcexe;
4840 typval_T functv;
4841 char_u *s;
4842 int ret;
4843
4844 // need to copy the funcref so that we can clear rettv
4845 if (evaluate)
4846 {
4847 functv = *rettv;
4848 rettv->v_type = VAR_UNKNOWN;
4849
4850 /* Invoke the function. Recursive! */
4851 if (functv.v_type == VAR_PARTIAL)
4852 {
4853 pt = functv.vval.v_partial;
4854 s = partial_name(pt);
4855 }
4856 else
4857 s = functv.vval.v_string;
4858 }
4859 else
4860 s = (char_u *)"";
4861
4862 vim_memset(&funcexe, 0, sizeof(funcexe));
4863 funcexe.firstline = curwin->w_cursor.lnum;
4864 funcexe.lastline = curwin->w_cursor.lnum;
4865 funcexe.evaluate = evaluate;
4866 funcexe.partial = pt;
4867 funcexe.selfdict = selfdict;
4868 funcexe.basetv = basetv;
4869 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
4870
4871 /* Clear the funcref afterwards, so that deleting it while
4872 * evaluating the arguments is possible (see test55). */
4873 if (evaluate)
4874 clear_tv(&functv);
4875
4876 return ret;
4877}
4878
4879/*
4880 * Evaluate "->method()".
4881 * "*arg" points to the '-'.
4882 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4883 */
4884 static int
4885eval_lambda(
4886 char_u **arg,
4887 typval_T *rettv,
4888 int evaluate,
4889 int verbose) /* give error messages */
4890{
4891 typval_T base = *rettv;
4892 int ret;
4893
4894 // Skip over the ->.
4895 *arg += 2;
4896 rettv->v_type = VAR_UNKNOWN;
4897
4898 ret = get_lambda_tv(arg, rettv, evaluate);
4899 if (ret == NOTDONE)
4900 return FAIL;
4901 else if (**arg != '(')
4902 {
4903 if (verbose)
4904 {
4905 if (*skipwhite(*arg) == '(')
4906 semsg(_(e_nowhitespace));
4907 else
4908 semsg(_(e_missingparen), "lambda");
4909 }
4910 clear_tv(rettv);
4911 return FAIL;
4912 }
4913 return call_func_rettv(arg, rettv, evaluate, NULL, &base);
4914}
4915
4916/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004917 * Evaluate "->method()".
4918 * "*arg" points to the '-'.
4919 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4920 */
4921 static int
4922eval_method(
4923 char_u **arg,
4924 typval_T *rettv,
4925 int evaluate,
4926 int verbose) /* give error messages */
4927{
4928 char_u *name;
4929 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004930 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004931 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004932 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004933
4934 // Skip over the ->.
4935 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004936 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004937
Bram Moolenaarac92e252019-08-03 21:58:38 +02004938 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004939 len = get_name_len(arg, &alias, evaluate, TRUE);
4940 if (alias != NULL)
4941 name = alias;
4942
4943 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004944 {
4945 if (verbose)
4946 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004947 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004948 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004949 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004950 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004951 if (**arg != '(')
4952 {
4953 if (verbose)
4954 semsg(_(e_missingparen), name);
4955 ret = FAIL;
4956 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004957 else if (VIM_ISWHITE((*arg)[-1]))
4958 {
4959 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004960 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02004961 ret = FAIL;
4962 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004963 else
4964 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004965 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004966
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004967 // Clear the funcref afterwards, so that deleting it while
4968 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004969 if (evaluate)
4970 clear_tv(&base);
4971
Bram Moolenaarac92e252019-08-03 21:58:38 +02004972 return ret;
4973}
4974
4975/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004976 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4977 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4979 */
4980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004981eval_index(
4982 char_u **arg,
4983 typval_T *rettv,
4984 int evaluate,
4985 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986{
4987 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004988 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004989 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 long len = -1;
4992 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995
Bram Moolenaara03f2332016-02-06 18:09:59 +01004996 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004997 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004998 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004999 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005000 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005001 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005002 return FAIL;
5003 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005004#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005005 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005006 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005007 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005008#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005009 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005010 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005011 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005012 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005013 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005014 return FAIL;
5015 case VAR_UNKNOWN:
5016 if (evaluate)
5017 return FAIL;
5018 /* FALLTHROUGH */
5019
5020 case VAR_STRING:
5021 case VAR_NUMBER:
5022 case VAR_LIST:
5023 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005024 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005025 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005026 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005027
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005028 init_tv(&var1);
5029 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005031 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 /*
5033 * dict.name
5034 */
5035 key = *arg + 1;
5036 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5037 ;
5038 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005039 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005041 }
5042 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005043 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 /*
5045 * something[idx]
5046 *
5047 * Get the (first) variable from inside the [].
5048 */
5049 *arg = skipwhite(*arg + 1);
5050 if (**arg == ':')
5051 empty1 = TRUE;
5052 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5053 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005054 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005055 {
5056 /* not a number or string */
5057 clear_tv(&var1);
5058 return FAIL;
5059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060
5061 /*
5062 * Get the second variable from inside the [:].
5063 */
5064 if (**arg == ':')
5065 {
5066 range = TRUE;
5067 *arg = skipwhite(*arg + 1);
5068 if (**arg == ']')
5069 empty2 = TRUE;
5070 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072 if (!empty1)
5073 clear_tv(&var1);
5074 return FAIL;
5075 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005076 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005077 {
5078 /* not a number or string */
5079 if (!empty1)
5080 clear_tv(&var1);
5081 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 return FAIL;
5083 }
5084 }
5085
5086 /* Check for the ']'. */
5087 if (**arg != ']')
5088 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005089 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005090 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 clear_tv(&var1);
5092 if (range)
5093 clear_tv(&var2);
5094 return FAIL;
5095 }
5096 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 }
5098
5099 if (evaluate)
5100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 n1 = 0;
5102 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005104 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005106 }
5107 if (range)
5108 {
5109 if (empty2)
5110 n2 = -1;
5111 else
5112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005113 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 }
5116 }
5117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005120 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005121 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005122 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005123 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005124 case VAR_SPECIAL:
5125 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005126 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005127 break; /* not evaluating, skipping over subscript */
5128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 case VAR_NUMBER:
5130 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005131 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005132 len = (long)STRLEN(s);
5133 if (range)
5134 {
5135 /* The resulting variable is a substring. If the indexes
5136 * are out of range the result is empty. */
5137 if (n1 < 0)
5138 {
5139 n1 = len + n1;
5140 if (n1 < 0)
5141 n1 = 0;
5142 }
5143 if (n2 < 0)
5144 n2 = len + n2;
5145 else if (n2 >= len)
5146 n2 = len;
5147 if (n1 >= len || n2 < 0 || n1 > n2)
5148 s = NULL;
5149 else
5150 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5151 }
5152 else
5153 {
5154 /* The resulting variable is a string of a single
5155 * character. If the index is too big or negative the
5156 * result is empty. */
5157 if (n1 >= len || n1 < 0)
5158 s = NULL;
5159 else
5160 s = vim_strnsave(s + n1, 1);
5161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 clear_tv(rettv);
5163 rettv->v_type = VAR_STRING;
5164 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 break;
5166
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005167 case VAR_BLOB:
5168 len = blob_len(rettv->vval.v_blob);
5169 if (range)
5170 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005171 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005172 // are out of range the result is empty.
5173 if (n1 < 0)
5174 {
5175 n1 = len + n1;
5176 if (n1 < 0)
5177 n1 = 0;
5178 }
5179 if (n2 < 0)
5180 n2 = len + n2;
5181 else if (n2 >= len)
5182 n2 = len - 1;
5183 if (n1 >= len || n2 < 0 || n1 > n2)
5184 {
5185 clear_tv(rettv);
5186 rettv->v_type = VAR_BLOB;
5187 rettv->vval.v_blob = NULL;
5188 }
5189 else
5190 {
5191 blob_T *blob = blob_alloc();
5192
5193 if (blob != NULL)
5194 {
5195 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5196 {
5197 blob_free(blob);
5198 return FAIL;
5199 }
5200 blob->bv_ga.ga_len = n2 - n1 + 1;
5201 for (i = n1; i <= n2; i++)
5202 blob_set(blob, i - n1,
5203 blob_get(rettv->vval.v_blob, i));
5204
5205 clear_tv(rettv);
5206 rettv_blob_set(rettv, blob);
5207 }
5208 }
5209 }
5210 else
5211 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005212 // The resulting variable is a byte value.
5213 // If the index is too big or negative that is an error.
5214 if (n1 < 0)
5215 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005216 if (n1 < len && n1 >= 0)
5217 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005218 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005219
5220 clear_tv(rettv);
5221 rettv->v_type = VAR_NUMBER;
5222 rettv->vval.v_number = v;
5223 }
5224 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005225 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005226 }
5227 break;
5228
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005230 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 if (n1 < 0)
5232 n1 = len + n1;
5233 if (!empty1 && (n1 < 0 || n1 >= len))
5234 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005235 /* For a range we allow invalid values and return an empty
5236 * list. A list index out of range is an error. */
5237 if (!range)
5238 {
5239 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005240 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005241 return FAIL;
5242 }
5243 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 }
5245 if (range)
5246 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005247 list_T *l;
5248 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249
5250 if (n2 < 0)
5251 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005252 else if (n2 >= len)
5253 n2 = len - 1;
5254 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005255 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 l = list_alloc();
5257 if (l == NULL)
5258 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 n1 <= n2; ++n1)
5261 {
5262 if (list_append_tv(l, &item->li_tv) == FAIL)
5263 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005264 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 return FAIL;
5266 }
5267 item = item->li_next;
5268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005270 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 }
5272 else
5273 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005274 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005275 clear_tv(rettv);
5276 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 }
5278 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279
5280 case VAR_DICT:
5281 if (range)
5282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005284 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (len == -1)
5286 clear_tv(&var1);
5287 return FAIL;
5288 }
5289 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291
5292 if (len == -1)
5293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005294 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005295 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 clear_tv(&var1);
5298 return FAIL;
5299 }
5300 }
5301
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005302 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005304 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005305 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 if (len == -1)
5307 clear_tv(&var1);
5308 if (item == NULL)
5309 return FAIL;
5310
5311 copy_tv(&item->di_tv, &var1);
5312 clear_tv(rettv);
5313 *rettv = var1;
5314 }
5315 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 }
5317 }
5318
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 return OK;
5320}
5321
5322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 * Get an option value.
5324 * "arg" points to the '&' or '+' before the option name.
5325 * "arg" is advanced to character after the option name.
5326 * Return OK or FAIL.
5327 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005329get_option_tv(
5330 char_u **arg,
5331 typval_T *rettv, /* when NULL, only check if option exists */
5332 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 char_u *option_end;
5335 long numval;
5336 char_u *stringval;
5337 int opt_type;
5338 int c;
5339 int working = (**arg == '+'); /* has("+option") */
5340 int ret = OK;
5341 int opt_flags;
5342
5343 /*
5344 * Isolate the option name and find its value.
5345 */
5346 option_end = find_option_end(arg, &opt_flags);
5347 if (option_end == NULL)
5348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005350 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 return FAIL;
5352 }
5353
5354 if (!evaluate)
5355 {
5356 *arg = option_end;
5357 return OK;
5358 }
5359
5360 c = *option_end;
5361 *option_end = NUL;
5362 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 if (opt_type == -3) /* invalid name */
5366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005368 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 ret = FAIL;
5370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005371 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
5373 if (opt_type == -2) /* hidden string option */
5374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 rettv->v_type = VAR_STRING;
5376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 else if (opt_type == -1) /* hidden number option */
5379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 rettv->v_type = VAR_NUMBER;
5381 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 else if (opt_type == 1) /* number option */
5384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 rettv->v_type = VAR_NUMBER;
5386 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 else /* string option */
5389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 rettv->v_type = VAR_STRING;
5391 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 }
5394 else if (working && (opt_type == -2 || opt_type == -1))
5395 ret = FAIL;
5396
5397 *option_end = c; /* put back for error messages */
5398 *arg = option_end;
5399
5400 return ret;
5401}
5402
5403/*
5404 * Allocate a variable for a string constant.
5405 * Return OK or FAIL.
5406 */
5407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005408get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 char_u *p;
5411 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 int extra = 0;
5413
5414 /*
5415 * Find the end of the string, skipping backslashed characters.
5416 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005417 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 if (*p == '\\' && p[1] != NUL)
5420 {
5421 ++p;
5422 /* A "\<x>" form occupies at least 4 characters, and produces up
5423 * to 6 characters: reserve space for 2 extra */
5424 if (*p == '<')
5425 extra += 2;
5426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428
5429 if (*p != '"')
5430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005431 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 return FAIL;
5433 }
5434
5435 /* If only parsing, set *arg and return here */
5436 if (!evaluate)
5437 {
5438 *arg = p + 1;
5439 return OK;
5440 }
5441
5442 /*
5443 * Copy the string into allocated memory, handling backslashed
5444 * characters.
5445 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005446 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (name == NULL)
5448 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005449 rettv->v_type = VAR_STRING;
5450 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 if (*p == '\\')
5455 {
5456 switch (*++p)
5457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 case 'b': *name++ = BS; ++p; break;
5459 case 'e': *name++ = ESC; ++p; break;
5460 case 'f': *name++ = FF; ++p; break;
5461 case 'n': *name++ = NL; ++p; break;
5462 case 'r': *name++ = CAR; ++p; break;
5463 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 case 'X': /* hex: "\x1", "\x12" */
5466 case 'x':
5467 case 'u': /* Unicode: "\u0023" */
5468 case 'U':
5469 if (vim_isxdigit(p[1]))
5470 {
5471 int n, nr;
5472 int c = toupper(*p);
5473
5474 if (c == 'X')
5475 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005476 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005478 else
5479 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 nr = 0;
5481 while (--n >= 0 && vim_isxdigit(p[1]))
5482 {
5483 ++p;
5484 nr = (nr << 4) + hex2nr(*p);
5485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 /* For "\u" store the number according to
5488 * 'encoding'. */
5489 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 break;
5495
5496 /* octal: "\1", "\12", "\123" */
5497 case '0':
5498 case '1':
5499 case '2':
5500 case '3':
5501 case '4':
5502 case '5':
5503 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504 case '7': *name = *p++ - '0';
5505 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 *name = (*name << 3) + *p++ - '0';
5508 if (*p >= '0' && *p <= '7')
5509 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 break;
5513
5514 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005515 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (extra != 0)
5517 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 break;
5520 }
5521 /* FALLTHROUGH */
5522
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 break;
5525 }
5526 }
5527 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005532 if (*p != NUL) /* just in case */
5533 ++p;
5534 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return OK;
5537}
5538
5539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 * Return OK or FAIL.
5542 */
5543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005544get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005547 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005548 int reduce = 0;
5549
5550 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005552 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005553 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005554 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005558 break;
5559 ++reduce;
5560 ++p;
5561 }
5562 }
5563
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005566 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005567 return FAIL;
5568 }
5569
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005571 if (!evaluate)
5572 {
5573 *arg = p + 1;
5574 return OK;
5575 }
5576
5577 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005579 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005580 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005581 if (str == NULL)
5582 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 rettv->v_type = VAR_STRING;
5584 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005585
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005587 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005589 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005591 break;
5592 ++p;
5593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005597 *arg = p + 1;
5598
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005599 return OK;
5600}
5601
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005602/*
5603 * Return the function name of the partial.
5604 */
5605 char_u *
5606partial_name(partial_T *pt)
5607{
5608 if (pt->pt_name != NULL)
5609 return pt->pt_name;
5610 return pt->pt_func->uf_name;
5611}
5612
Bram Moolenaarddecc252016-04-06 22:59:37 +02005613 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005614partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005615{
5616 int i;
5617
5618 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005619 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005620 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005621 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005622 if (pt->pt_name != NULL)
5623 {
5624 func_unref(pt->pt_name);
5625 vim_free(pt->pt_name);
5626 }
5627 else
5628 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005629 vim_free(pt);
5630}
5631
5632/*
5633 * Unreference a closure: decrement the reference count and free it when it
5634 * becomes zero.
5635 */
5636 void
5637partial_unref(partial_T *pt)
5638{
5639 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005640 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005641}
5642
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005643static int tv_equal_recurse_limit;
5644
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005645 static int
5646func_equal(
5647 typval_T *tv1,
5648 typval_T *tv2,
5649 int ic) /* ignore case */
5650{
5651 char_u *s1, *s2;
5652 dict_T *d1, *d2;
5653 int a1, a2;
5654 int i;
5655
5656 /* empty and NULL function name considered the same */
5657 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005658 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005659 if (s1 != NULL && *s1 == NUL)
5660 s1 = NULL;
5661 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005662 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005663 if (s2 != NULL && *s2 == NUL)
5664 s2 = NULL;
5665 if (s1 == NULL || s2 == NULL)
5666 {
5667 if (s1 != s2)
5668 return FALSE;
5669 }
5670 else if (STRCMP(s1, s2) != 0)
5671 return FALSE;
5672
5673 /* empty dict and NULL dict is different */
5674 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5675 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5676 if (d1 == NULL || d2 == NULL)
5677 {
5678 if (d1 != d2)
5679 return FALSE;
5680 }
5681 else if (!dict_equal(d1, d2, ic, TRUE))
5682 return FALSE;
5683
5684 /* empty list and no list considered the same */
5685 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5686 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5687 if (a1 != a2)
5688 return FALSE;
5689 for (i = 0; i < a1; ++i)
5690 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5691 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5692 return FALSE;
5693
5694 return TRUE;
5695}
5696
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005697/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005698 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005699 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005700 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005703tv_equal(
5704 typval_T *tv1,
5705 typval_T *tv2,
5706 int ic, /* ignore case */
5707 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005708{
5709 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005710 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005711 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005712 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005713
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005714 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005715 * recursiveness to a limit. We guess they are equal then.
5716 * A fixed limit has the problem of still taking an awful long time.
5717 * Reduce the limit every time running into it. That should work fine for
5718 * deeply linked structures that are not recursively linked and catch
5719 * recursiveness quickly. */
5720 if (!recursive)
5721 tv_equal_recurse_limit = 1000;
5722 if (recursive_cnt >= tv_equal_recurse_limit)
5723 {
5724 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005725 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005726 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005727
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005728 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5729 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005730 if ((tv1->v_type == VAR_FUNC
5731 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5732 && (tv2->v_type == VAR_FUNC
5733 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5734 {
5735 ++recursive_cnt;
5736 r = func_equal(tv1, tv2, ic);
5737 --recursive_cnt;
5738 return r;
5739 }
5740
5741 if (tv1->v_type != tv2->v_type)
5742 return FALSE;
5743
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005744 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005745 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005746 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005747 ++recursive_cnt;
5748 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5749 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005750 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005751
5752 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005753 ++recursive_cnt;
5754 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5755 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005756 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005757
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005758 case VAR_BLOB:
5759 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5760
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005761 case VAR_NUMBER:
5762 return tv1->vval.v_number == tv2->vval.v_number;
5763
5764 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005765 s1 = tv_get_string_buf(tv1, buf1);
5766 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005767 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005768
5769 case VAR_SPECIAL:
5770 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005771
5772 case VAR_FLOAT:
5773#ifdef FEAT_FLOAT
5774 return tv1->vval.v_float == tv2->vval.v_float;
5775#endif
5776 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005777#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005778 return tv1->vval.v_job == tv2->vval.v_job;
5779#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005780 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005781#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005782 return tv1->vval.v_channel == tv2->vval.v_channel;
5783#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005784 case VAR_FUNC:
5785 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005786 case VAR_UNKNOWN:
5787 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005788 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005789
Bram Moolenaara03f2332016-02-06 18:09:59 +01005790 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5791 * does not equal anything, not even itself. */
5792 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005793}
5794
5795/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005796 * Return the next (unique) copy ID.
5797 * Used for serializing nested structures.
5798 */
5799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005800get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005801{
5802 current_copyID += COPYID_INC;
5803 return current_copyID;
5804}
5805
5806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005807 * Garbage collection for lists and dictionaries.
5808 *
5809 * We use reference counts to be able to free most items right away when they
5810 * are no longer used. But for composite items it's possible that it becomes
5811 * unused while the reference count is > 0: When there is a recursive
5812 * reference. Example:
5813 * :let l = [1, 2, 3]
5814 * :let d = {9: l}
5815 * :let l[1] = d
5816 *
5817 * Since this is quite unusual we handle this with garbage collection: every
5818 * once in a while find out which lists and dicts are not referenced from any
5819 * variable.
5820 *
5821 * Here is a good reference text about garbage collection (refers to Python
5822 * but it applies to all reference-counting mechanisms):
5823 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005824 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005825
5826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005827 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005828 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005829 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005830 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005831 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005832garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005833{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005834 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005835 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005836 buf_T *buf;
5837 win_T *wp;
5838 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005839 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005840 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005841
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005842 if (!testing)
5843 {
5844 /* Only do this once. */
5845 want_garbage_collect = FALSE;
5846 may_garbage_collect = FALSE;
5847 garbage_collect_at_exit = FALSE;
5848 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005849
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005850 /* We advance by two because we add one for items referenced through
5851 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005852 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005853
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005854 /*
5855 * 1. Go through all accessible variables and mark all lists and dicts
5856 * with copyID.
5857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005858
5859 /* Don't free variables in the previous_funccal list unless they are only
5860 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005861 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005863
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005864 /* script-local variables */
5865 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005866 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005867
5868 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005869 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005870 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5871 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005872
5873 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005874 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005875 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5876 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005877 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005878 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5879 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005880#ifdef FEAT_TEXT_PROP
5881 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5882 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5883 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005884 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005885 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005886 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5887 NULL, NULL);
5888#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005889
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005890 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005891 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005892 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5893 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005895 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896
5897 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005898 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005899
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005900 /* named functions (matters for closures) */
5901 abort = abort || set_ref_in_functions(copyID);
5902
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005903 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005904 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005905
Bram Moolenaard812df62008-11-09 12:46:09 +00005906 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005907 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005908
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005909 // callbacks in buffers
5910 abort = abort || set_ref_in_buffers(copyID);
5911
Bram Moolenaar1dced572012-04-05 16:54:08 +02005912#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005913 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005914#endif
5915
Bram Moolenaardb913952012-06-29 12:54:53 +02005916#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005917 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005918#endif
5919
5920#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005921 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005922#endif
5923
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005925 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005926 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005927#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005928#ifdef FEAT_NETBEANS_INTG
5929 abort = abort || set_ref_in_nb_channel(copyID);
5930#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005931
Bram Moolenaare3188e22016-05-31 21:13:04 +02005932#ifdef FEAT_TIMERS
5933 abort = abort || set_ref_in_timer(copyID);
5934#endif
5935
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005936#ifdef FEAT_QUICKFIX
5937 abort = abort || set_ref_in_quickfix(copyID);
5938#endif
5939
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005940#ifdef FEAT_TERMINAL
5941 abort = abort || set_ref_in_term(copyID);
5942#endif
5943
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005944#ifdef FEAT_TEXT_PROP
5945 abort = abort || set_ref_in_popups(copyID);
5946#endif
5947
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005948 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005949 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005950 /*
5951 * 2. Free lists and dictionaries that are not referenced.
5952 */
5953 did_free = free_unref_items(copyID);
5954
5955 /*
5956 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005957 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005958 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005959 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005961 else if (p_verbose > 0)
5962 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005963 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005964 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005965
5966 return did_free;
5967}
5968
5969/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005970 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005971 */
5972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005974{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005975 int did_free = FALSE;
5976
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005977 /* Let all "free" functions know that we are here. This means no
5978 * dictionaries, lists, channels or jobs are to be freed, because we will
5979 * do that here. */
5980 in_free_unref_items = TRUE;
5981
5982 /*
5983 * PASS 1: free the contents of the items. We don't free the items
5984 * themselves yet, so that it is possible to decrement refcount counters
5985 */
5986
Bram Moolenaarcd524592016-07-17 14:57:05 +02005987 /* Go through the list of dicts and free items without the copyID. */
5988 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005989
Bram Moolenaarda861d62016-07-17 15:46:27 +02005990 /* Go through the list of lists and free items without the copyID. */
5991 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005992
5993#ifdef FEAT_JOB_CHANNEL
5994 /* Go through the list of jobs and free items without the copyID. This
5995 * must happen before doing channels, because jobs refer to channels, but
5996 * the reference from the channel to the job isn't tracked. */
5997 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5998
5999 /* Go through the list of channels and free items without the copyID. */
6000 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
6001#endif
6002
6003 /*
6004 * PASS 2: free the items themselves.
6005 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006006 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02006007 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006008
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006009#ifdef FEAT_JOB_CHANNEL
6010 /* Go through the list of jobs and free items without the copyID. This
6011 * must happen before doing channels, because jobs refer to channels, but
6012 * the reference from the channel to the job isn't tracked. */
6013 free_unused_jobs(copyID, COPYID_MASK);
6014
6015 /* Go through the list of channels and free items without the copyID. */
6016 free_unused_channels(copyID, COPYID_MASK);
6017#endif
6018
6019 in_free_unref_items = FALSE;
6020
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006021 return did_free;
6022}
6023
6024/*
6025 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006026 * "list_stack" is used to add lists to be marked. Can be NULL.
6027 *
6028 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006029 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032{
6033 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006034 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006035 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006036 hashtab_T *cur_ht;
6037 ht_stack_T *ht_stack = NULL;
6038 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006040 cur_ht = ht;
6041 for (;;)
6042 {
6043 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006044 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006045 /* Mark each item in the hashtab. If the item contains a hashtab
6046 * it is added to ht_stack, if it contains a list it is added to
6047 * list_stack. */
6048 todo = (int)cur_ht->ht_used;
6049 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6050 if (!HASHITEM_EMPTY(hi))
6051 {
6052 --todo;
6053 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6054 &ht_stack, list_stack);
6055 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006057
6058 if (ht_stack == NULL)
6059 break;
6060
6061 /* take an item from the stack */
6062 cur_ht = ht_stack->ht;
6063 tempitem = ht_stack;
6064 ht_stack = ht_stack->prev;
6065 free(tempitem);
6066 }
6067
6068 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069}
6070
6071/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006072 * Mark a dict and its items with "copyID".
6073 * Returns TRUE if setting references failed somehow.
6074 */
6075 int
6076set_ref_in_dict(dict_T *d, int copyID)
6077{
6078 if (d != NULL && d->dv_copyID != copyID)
6079 {
6080 d->dv_copyID = copyID;
6081 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6082 }
6083 return FALSE;
6084}
6085
6086/*
6087 * Mark a list and its items with "copyID".
6088 * Returns TRUE if setting references failed somehow.
6089 */
6090 int
6091set_ref_in_list(list_T *ll, int copyID)
6092{
6093 if (ll != NULL && ll->lv_copyID != copyID)
6094 {
6095 ll->lv_copyID = copyID;
6096 return set_ref_in_list_items(ll, copyID, NULL);
6097 }
6098 return FALSE;
6099}
6100
6101/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006103 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6104 *
6105 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006107 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006108set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006110 listitem_T *li;
6111 int abort = FALSE;
6112 list_T *cur_l;
6113 list_stack_T *list_stack = NULL;
6114 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006116 cur_l = l;
6117 for (;;)
6118 {
6119 if (!abort)
6120 /* Mark each item in the list. If the item contains a hashtab
6121 * it is added to ht_stack, if it contains a list it is added to
6122 * list_stack. */
6123 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6124 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6125 ht_stack, &list_stack);
6126 if (list_stack == NULL)
6127 break;
6128
6129 /* take an item from the stack */
6130 cur_l = list_stack->list;
6131 tempitem = list_stack;
6132 list_stack = list_stack->prev;
6133 free(tempitem);
6134 }
6135
6136 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006137}
6138
6139/*
6140 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006141 * "list_stack" is used to add lists to be marked. Can be NULL.
6142 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6143 *
6144 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006145 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006146 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147set_ref_in_item(
6148 typval_T *tv,
6149 int copyID,
6150 ht_stack_T **ht_stack,
6151 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006152{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006153 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006154
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006155 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006156 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006157 dict_T *dd = tv->vval.v_dict;
6158
Bram Moolenaara03f2332016-02-06 18:09:59 +01006159 if (dd != NULL && dd->dv_copyID != copyID)
6160 {
6161 /* Didn't see this dict yet. */
6162 dd->dv_copyID = copyID;
6163 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006164 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006165 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6166 }
6167 else
6168 {
6169 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
6170 if (newitem == NULL)
6171 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006172 else
6173 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006174 newitem->ht = &dd->dv_hashtab;
6175 newitem->prev = *ht_stack;
6176 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006177 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006178 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006179 }
6180 }
6181 else if (tv->v_type == VAR_LIST)
6182 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006183 list_T *ll = tv->vval.v_list;
6184
Bram Moolenaara03f2332016-02-06 18:09:59 +01006185 if (ll != NULL && ll->lv_copyID != copyID)
6186 {
6187 /* Didn't see this list yet. */
6188 ll->lv_copyID = copyID;
6189 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006190 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006191 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006192 }
6193 else
6194 {
6195 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006196 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006197 if (newitem == NULL)
6198 abort = TRUE;
6199 else
6200 {
6201 newitem->list = ll;
6202 newitem->prev = *list_stack;
6203 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006204 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006205 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006206 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006207 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006208 else if (tv->v_type == VAR_FUNC)
6209 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006210 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006211 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006212 else if (tv->v_type == VAR_PARTIAL)
6213 {
6214 partial_T *pt = tv->vval.v_partial;
6215 int i;
6216
6217 /* A partial does not have a copyID, because it cannot contain itself.
6218 */
6219 if (pt != NULL)
6220 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006221 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006222
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006223 if (pt->pt_dict != NULL)
6224 {
6225 typval_T dtv;
6226
6227 dtv.v_type = VAR_DICT;
6228 dtv.vval.v_dict = pt->pt_dict;
6229 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6230 }
6231
6232 for (i = 0; i < pt->pt_argc; ++i)
6233 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6234 ht_stack, list_stack);
6235 }
6236 }
6237#ifdef FEAT_JOB_CHANNEL
6238 else if (tv->v_type == VAR_JOB)
6239 {
6240 job_T *job = tv->vval.v_job;
6241 typval_T dtv;
6242
6243 if (job != NULL && job->jv_copyID != copyID)
6244 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006245 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006246 if (job->jv_channel != NULL)
6247 {
6248 dtv.v_type = VAR_CHANNEL;
6249 dtv.vval.v_channel = job->jv_channel;
6250 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6251 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006252 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006253 {
6254 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006255 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006256 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6257 }
6258 }
6259 }
6260 else if (tv->v_type == VAR_CHANNEL)
6261 {
6262 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006263 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006264 typval_T dtv;
6265 jsonq_T *jq;
6266 cbq_T *cq;
6267
6268 if (ch != NULL && ch->ch_copyID != copyID)
6269 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006270 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006271 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006272 {
6273 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6274 jq = jq->jq_next)
6275 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6276 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6277 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006278 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006279 {
6280 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006281 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006282 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6283 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006284 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006285 {
6286 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006287 dtv.vval.v_partial =
6288 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006289 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6290 }
6291 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006292 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006293 {
6294 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006295 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006296 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6297 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006298 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006299 {
6300 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006301 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006302 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6303 }
6304 }
6305 }
6306#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006307 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006308}
6309
Bram Moolenaar17a13432016-01-24 14:22:10 +01006310 static char *
6311get_var_special_name(int nr)
6312{
6313 switch (nr)
6314 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006315 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006316 case VVAL_TRUE: return "v:true";
6317 case VVAL_NONE: return "v:none";
6318 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006319 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006320 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006321 return "42";
6322}
6323
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 * Return a string with the string representation of a variable.
6326 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006327 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006328 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006329 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6330 * stings as "string()", otherwise does not put quotes around strings, as
6331 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006332 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6333 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006334 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006336 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006337echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338 typval_T *tv,
6339 char_u **tofree,
6340 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006341 int copyID,
6342 int echo_style,
6343 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006344 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 static int recurse = 0;
6347 char_u *r = NULL;
6348
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006351 if (!did_echo_string_emsg)
6352 {
6353 /* Only give this message once for a recursive call to avoid
6354 * flooding the user with errors. And stop iterating over lists
6355 * and dicts. */
6356 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006357 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006360 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006361 }
6362 ++recurse;
6363
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 switch (tv->v_type)
6365 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006366 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006367 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006368 {
6369 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006370 r = tv->vval.v_string;
6371 if (r == NULL)
6372 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006373 }
6374 else
6375 {
6376 *tofree = string_quote(tv->vval.v_string, FALSE);
6377 r = *tofree;
6378 }
6379 break;
6380
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006382 if (echo_style)
6383 {
6384 *tofree = NULL;
6385 r = tv->vval.v_string;
6386 }
6387 else
6388 {
6389 *tofree = string_quote(tv->vval.v_string, TRUE);
6390 r = *tofree;
6391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006393
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006394 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006395 {
6396 partial_T *pt = tv->vval.v_partial;
6397 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006398 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006399 garray_T ga;
6400 int i;
6401 char_u *tf;
6402
6403 ga_init2(&ga, 1, 100);
6404 ga_concat(&ga, (char_u *)"function(");
6405 if (fname != NULL)
6406 {
6407 ga_concat(&ga, fname);
6408 vim_free(fname);
6409 }
6410 if (pt != NULL && pt->pt_argc > 0)
6411 {
6412 ga_concat(&ga, (char_u *)", [");
6413 for (i = 0; i < pt->pt_argc; ++i)
6414 {
6415 if (i > 0)
6416 ga_concat(&ga, (char_u *)", ");
6417 ga_concat(&ga,
6418 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6419 vim_free(tf);
6420 }
6421 ga_concat(&ga, (char_u *)"]");
6422 }
6423 if (pt != NULL && pt->pt_dict != NULL)
6424 {
6425 typval_T dtv;
6426
6427 ga_concat(&ga, (char_u *)", ");
6428 dtv.v_type = VAR_DICT;
6429 dtv.vval.v_dict = pt->pt_dict;
6430 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6431 vim_free(tf);
6432 }
6433 ga_concat(&ga, (char_u *)")");
6434
6435 *tofree = ga.ga_data;
6436 r = *tofree;
6437 break;
6438 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006439
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006440 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006441 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006442 break;
6443
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006445 if (tv->vval.v_list == NULL)
6446 {
6447 *tofree = NULL;
6448 r = NULL;
6449 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006450 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6451 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006452 {
6453 *tofree = NULL;
6454 r = (char_u *)"[...]";
6455 }
6456 else
6457 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006458 int old_copyID = tv->vval.v_list->lv_copyID;
6459
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006460 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006461 *tofree = list2string(tv, copyID, restore_copyID);
6462 if (restore_copyID)
6463 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006464 r = *tofree;
6465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006467
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006469 if (tv->vval.v_dict == NULL)
6470 {
6471 *tofree = NULL;
6472 r = NULL;
6473 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006474 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6475 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006476 {
6477 *tofree = NULL;
6478 r = (char_u *)"{...}";
6479 }
6480 else
6481 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006482 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006483 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006484 *tofree = dict2string(tv, copyID, restore_copyID);
6485 if (restore_copyID)
6486 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 r = *tofree;
6488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006491 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006492 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006493 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006494 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006495 break;
6496
Bram Moolenaar835dc632016-02-07 14:27:38 +01006497 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006498 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006500 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006501 if (composite_val)
6502 {
6503 *tofree = string_quote(r, FALSE);
6504 r = *tofree;
6505 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006507
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006508 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006509#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006510 *tofree = NULL;
6511 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6512 r = numbuf;
6513 break;
6514#endif
6515
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006516 case VAR_SPECIAL:
6517 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006518 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006519 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521
Bram Moolenaar8502c702014-06-17 12:51:16 +02006522 if (--recurse == 0)
6523 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006525}
6526
6527/*
6528 * Return a string with the string representation of a variable.
6529 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6530 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006531 * Does not put quotes around strings, as ":echo" displays values.
6532 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6533 * May return NULL.
6534 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006535 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006536echo_string(
6537 typval_T *tv,
6538 char_u **tofree,
6539 char_u *numbuf,
6540 int copyID)
6541{
6542 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6543}
6544
6545/*
6546 * Return a string with the string representation of a variable.
6547 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6548 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006549 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006550 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006551 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553tv2string(
6554 typval_T *tv,
6555 char_u **tofree,
6556 char_u *numbuf,
6557 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006558{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006559 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560}
6561
6562/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 * Return string "str" in ' quotes, doubling ' characters.
6564 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006566 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006567 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006569{
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 char_u *p, *r, *s;
6572
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 len = (function ? 13 : 3);
6574 if (str != NULL)
6575 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006576 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006577 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 if (*p == '\'')
6579 ++len;
6580 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 s = r = alloc(len);
6582 if (r != NULL)
6583 {
6584 if (function)
6585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006586 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006587 r += 10;
6588 }
6589 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006590 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 if (str != NULL)
6592 for (p = str; *p != NUL; )
6593 {
6594 if (*p == '\'')
6595 *r++ = '\'';
6596 MB_COPY_CHAR(p, r);
6597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 if (function)
6600 *r++ = ')';
6601 *r++ = NUL;
6602 }
6603 return s;
6604}
6605
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006606#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006607/*
6608 * Convert the string "text" to a floating point number.
6609 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6610 * this always uses a decimal point.
6611 * Returns the length of the text that was consumed.
6612 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614string2float(
6615 char_u *text,
6616 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006617{
6618 char *s = (char *)text;
6619 float_T f;
6620
Bram Moolenaar62473612017-01-08 19:25:40 +01006621 /* MS-Windows does not deal with "inf" and "nan" properly. */
6622 if (STRNICMP(text, "inf", 3) == 0)
6623 {
6624 *value = INFINITY;
6625 return 3;
6626 }
6627 if (STRNICMP(text, "-inf", 3) == 0)
6628 {
6629 *value = -INFINITY;
6630 return 4;
6631 }
6632 if (STRNICMP(text, "nan", 3) == 0)
6633 {
6634 *value = NAN;
6635 return 3;
6636 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006637 f = strtod(s, &s);
6638 *value = f;
6639 return (int)((char_u *)s - text);
6640}
6641#endif
6642
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 * Get the value of an environment variable.
6645 * "arg" is pointing to the '$'. It is advanced to after the name.
6646 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006647 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 */
6649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 char_u *string = NULL;
6653 int len;
6654 int cc;
6655 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006656 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658 ++*arg;
6659 name = *arg;
6660 len = get_env_len(arg);
6661 if (evaluate)
6662 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006663 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006664 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006665
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006666 cc = name[len];
6667 name[len] = NUL;
6668 /* first try vim_getenv(), fast for normal environment vars */
6669 string = vim_getenv(name, &mustfree);
6670 if (string != NULL && *string != NUL)
6671 {
6672 if (!mustfree)
6673 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006675 else
6676 {
6677 if (mustfree)
6678 vim_free(string);
6679
6680 /* next try expanding things like $VIM and ${HOME} */
6681 string = expand_env_save(name - 1);
6682 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006683 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006684 }
6685 name[len] = cc;
6686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006687 rettv->v_type = VAR_STRING;
6688 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690
6691 return OK;
6692}
6693
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006694
6695
6696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006698 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006701var2fpos(
6702 typval_T *varp,
6703 int dollar_lnum, /* TRUE when $ is last line */
6704 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006706 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006708 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
Bram Moolenaara5525202006-03-02 22:52:09 +00006710 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006711 if (varp->v_type == VAR_LIST)
6712 {
6713 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006714 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006715 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006716 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006717
6718 l = varp->vval.v_list;
6719 if (l == NULL)
6720 return NULL;
6721
6722 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006723 pos.lnum = list_find_nr(l, 0L, &error);
6724 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006725 return NULL; /* invalid line number */
6726
6727 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006728 pos.col = list_find_nr(l, 1L, &error);
6729 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006730 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006731 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006732
6733 /* We accept "$" for the column number: last column. */
6734 li = list_find(l, 1L);
6735 if (li != NULL && li->li_tv.v_type == VAR_STRING
6736 && li->li_tv.vval.v_string != NULL
6737 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6738 pos.col = len + 1;
6739
Bram Moolenaara5525202006-03-02 22:52:09 +00006740 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006741 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006742 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006743 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006744
Bram Moolenaara5525202006-03-02 22:52:09 +00006745 /* Get the virtual offset. Defaults to zero. */
6746 pos.coladd = list_find_nr(l, 2L, &error);
6747 if (error)
6748 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006749
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006750 return &pos;
6751 }
6752
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006753 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006754 if (name == NULL)
6755 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006756 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006758 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6759 {
6760 if (VIsual_active)
6761 return &VIsual;
6762 return &curwin->w_cursor;
6763 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006764 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006766 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6768 return NULL;
6769 return pp;
6770 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006771
Bram Moolenaara5525202006-03-02 22:52:09 +00006772 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006773
Bram Moolenaar477933c2007-07-17 14:32:23 +00006774 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006775 {
6776 pos.col = 0;
6777 if (name[1] == '0') /* "w0": first visible line */
6778 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006779 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006780 /* In silent Ex mode topline is zero, but that's not a valid line
6781 * number; use one instead. */
6782 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006783 return &pos;
6784 }
6785 else if (name[1] == '$') /* "w$": last visible line */
6786 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006787 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006788 /* In silent Ex mode botline is zero, return zero then. */
6789 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006790 return &pos;
6791 }
6792 }
6793 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006795 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
6797 pos.lnum = curbuf->b_ml.ml_line_count;
6798 pos.col = 0;
6799 }
6800 else
6801 {
6802 pos.lnum = curwin->w_cursor.lnum;
6803 pos.col = (colnr_T)STRLEN(ml_get_curline());
6804 }
6805 return &pos;
6806 }
6807 return NULL;
6808}
6809
6810/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006811 * Convert list in "arg" into a position and optional file number.
6812 * When "fnump" is NULL there is no file number, only 3 items.
6813 * Note that the column is passed on as-is, the caller may want to decrement
6814 * it to use 1 for the first column.
6815 * Return FAIL when conversion is not possible, doesn't check the position for
6816 * validity.
6817 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819list2fpos(
6820 typval_T *arg,
6821 pos_T *posp,
6822 int *fnump,
6823 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006824{
6825 list_T *l = arg->vval.v_list;
6826 long i = 0;
6827 long n;
6828
Bram Moolenaar493c1782014-05-28 14:34:46 +02006829 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6830 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006831 if (arg->v_type != VAR_LIST
6832 || l == NULL
6833 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006834 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006835 return FAIL;
6836
6837 if (fnump != NULL)
6838 {
6839 n = list_find_nr(l, i++, NULL); /* fnum */
6840 if (n < 0)
6841 return FAIL;
6842 if (n == 0)
6843 n = curbuf->b_fnum; /* current buffer */
6844 *fnump = n;
6845 }
6846
6847 n = list_find_nr(l, i++, NULL); /* lnum */
6848 if (n < 0)
6849 return FAIL;
6850 posp->lnum = n;
6851
6852 n = list_find_nr(l, i++, NULL); /* col */
6853 if (n < 0)
6854 return FAIL;
6855 posp->col = n;
6856
Bram Moolenaar493c1782014-05-28 14:34:46 +02006857 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006858 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006859 posp->coladd = 0;
6860 else
6861 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006862
Bram Moolenaar493c1782014-05-28 14:34:46 +02006863 if (curswantp != NULL)
6864 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6865
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006866 return OK;
6867}
6868
6869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * Get the length of an environment variable name.
6871 * Advance "arg" to the first character after the name.
6872 * Return 0 for error.
6873 */
6874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006875get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876{
6877 char_u *p;
6878 int len;
6879
6880 for (p = *arg; vim_isIDc(*p); ++p)
6881 ;
6882 if (p == *arg) /* no name found */
6883 return 0;
6884
6885 len = (int)(p - *arg);
6886 *arg = p;
6887 return len;
6888}
6889
6890/*
6891 * Get the length of the name of a function or internal variable.
6892 * "arg" is advanced to the first non-white character after the name.
6893 * Return 0 if something is wrong.
6894 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006896get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 char_u *p;
6899 int len;
6900
6901 /* Find the end of the name. */
6902 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006903 {
6904 if (*p == ':')
6905 {
6906 /* "s:" is start of "s:var", but "n:" is not and can be used in
6907 * slice "[n:]". Also "xx:" is not a namespace. */
6908 len = (int)(p - *arg);
6909 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6910 || len > 1)
6911 break;
6912 }
6913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (p == *arg) /* no name found */
6915 return 0;
6916
6917 len = (int)(p - *arg);
6918 *arg = skipwhite(p);
6919
6920 return len;
6921}
6922
6923/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006924 * Get the length of the name of a variable or function.
6925 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006927 * Return -1 if curly braces expansion failed.
6928 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 * If the name contains 'magic' {}'s, expand them and return the
6930 * expanded name in an allocated string via 'alias' - caller must free.
6931 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933get_name_len(
6934 char_u **arg,
6935 char_u **alias,
6936 int evaluate,
6937 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 char_u *p;
6941 char_u *expr_start;
6942 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
6944 *alias = NULL; /* default to no alias */
6945
6946 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6947 && (*arg)[2] == (int)KE_SNR)
6948 {
6949 /* hard coded <SNR>, already translated */
6950 *arg += 3;
6951 return get_id_len(arg) + 3;
6952 }
6953 len = eval_fname_script(*arg);
6954 if (len > 0)
6955 {
6956 /* literal "<SID>", "s:" or "<SNR>" */
6957 *arg += len;
6958 }
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006961 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006963 p = find_name_end(*arg, &expr_start, &expr_end,
6964 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (expr_start != NULL)
6966 {
6967 char_u *temp_string;
6968
6969 if (!evaluate)
6970 {
6971 len += (int)(p - *arg);
6972 *arg = skipwhite(p);
6973 return len;
6974 }
6975
6976 /*
6977 * Include any <SID> etc in the expanded string:
6978 * Thus the -len here.
6979 */
6980 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6981 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006982 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 *alias = temp_string;
6984 *arg = skipwhite(p);
6985 return (int)STRLEN(temp_string);
6986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
6988 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006989 // Only give an error when there is something, otherwise it will be
6990 // reported at a higher level.
6991 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006992 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994 return len;
6995}
6996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997/*
6998 * Find the end of a variable or function name, taking care of magic braces.
6999 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7000 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007001 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002 * Return a pointer to just after the name. Equal to "arg" if there is no
7003 * valid name.
7004 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007005 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007006find_name_end(
7007 char_u *arg,
7008 char_u **expr_start,
7009 char_u **expr_end,
7010 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007012 int mb_nest = 0;
7013 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007015 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007017 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007019 *expr_start = NULL;
7020 *expr_end = NULL;
7021 }
7022
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007023 /* Quick check for valid starting character. */
7024 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
7025 return arg;
7026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007027 for (p = arg; *p != NUL
7028 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007029 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007030 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007031 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007032 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007033 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007034 if (*p == '\'')
7035 {
7036 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007037 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007038 ;
7039 if (*p == NUL)
7040 break;
7041 }
7042 else if (*p == '"')
7043 {
7044 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007045 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007046 if (*p == '\\' && p[1] != NUL)
7047 ++p;
7048 if (*p == NUL)
7049 break;
7050 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007051 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7052 {
7053 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007054 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007055 len = (int)(p - arg);
7056 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007057 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007058 break;
7059 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007060
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007061 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007063 if (*p == '[')
7064 ++br_nest;
7065 else if (*p == ']')
7066 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007068
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007069 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007071 if (*p == '{')
7072 {
7073 mb_nest++;
7074 if (expr_start != NULL && *expr_start == NULL)
7075 *expr_start = p;
7076 }
7077 else if (*p == '}')
7078 {
7079 mb_nest--;
7080 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7081 *expr_end = p;
7082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085
7086 return p;
7087}
7088
7089/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007090 * Expands out the 'magic' {}'s in a variable/function name.
7091 * Note that this can call itself recursively, to deal with
7092 * constructs like foo{bar}{baz}{bam}
7093 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7094 * "in_start" ^
7095 * "expr_start" ^
7096 * "expr_end" ^
7097 * "in_end" ^
7098 *
7099 * Returns a new allocated string, which the caller must free.
7100 * Returns NULL for failure.
7101 */
7102 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007103make_expanded_name(
7104 char_u *in_start,
7105 char_u *expr_start,
7106 char_u *expr_end,
7107 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007108{
7109 char_u c1;
7110 char_u *retval = NULL;
7111 char_u *temp_result;
7112 char_u *nextcmd = NULL;
7113
7114 if (expr_end == NULL || in_end == NULL)
7115 return NULL;
7116 *expr_start = NUL;
7117 *expr_end = NUL;
7118 c1 = *in_end;
7119 *in_end = NUL;
7120
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007121 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007122 if (temp_result != NULL && nextcmd == NULL)
7123 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007124 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7125 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007126 if (retval != NULL)
7127 {
7128 STRCPY(retval, in_start);
7129 STRCAT(retval, temp_result);
7130 STRCAT(retval, expr_end + 1);
7131 }
7132 }
7133 vim_free(temp_result);
7134
7135 *in_end = c1; /* put char back for error messages */
7136 *expr_start = '{';
7137 *expr_end = '}';
7138
7139 if (retval != NULL)
7140 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007141 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007142 if (expr_start != NULL)
7143 {
7144 /* Further expansion! */
7145 temp_result = make_expanded_name(retval, expr_start,
7146 expr_end, temp_result);
7147 vim_free(retval);
7148 retval = temp_result;
7149 }
7150 }
7151
7152 return retval;
7153}
7154
7155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007160eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007162 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
7163}
7164
7165/*
7166 * Return TRUE if character "c" can be used as the first character in a
7167 * variable or function name (excluding '{' and '}').
7168 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007169 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007171{
7172 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
7175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 * Set number v: variable to "val".
7177 */
7178 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007179set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182}
7183
7184/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007185 * Get typval_T v: variable value.
7186 */
7187 typval_T *
7188get_vim_var_tv(int idx)
7189{
7190 return &vimvars[idx].vv_tv;
7191}
7192
7193/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007194 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007196 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007197get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200}
7201
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007202/*
7203 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007204 * If the String variable has never been set, return an empty string.
7205 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007206 */
7207 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007209{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007210 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007211}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007214 * Get List v: variable value. Caller must take care of reference count when
7215 * needed.
7216 */
7217 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007218get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007219{
7220 return vimvars[idx].vv_list;
7221}
7222
7223/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007224 * Get Dict v: variable value. Caller must take care of reference count when
7225 * needed.
7226 */
7227 dict_T *
7228get_vim_var_dict(int idx)
7229{
7230 return vimvars[idx].vv_dict;
7231}
7232
7233/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007234 * Set v:char to character "c".
7235 */
7236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007238{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007239 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007240
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007241 if (has_mbyte)
7242 buf[(*mb_char2bytes)(c, buf)] = NUL;
7243 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007244 {
7245 buf[0] = c;
7246 buf[1] = NUL;
7247 }
7248 set_vim_var_string(VV_CHAR, buf, -1);
7249}
7250
7251/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007252 * Set v:count to "count" and v:count1 to "count1".
7253 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 */
7255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007256set_vcount(
7257 long count,
7258 long count1,
7259 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007261 if (set_prevcount)
7262 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007263 vimvars[VV_COUNT].vv_nr = count;
7264 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265}
7266
7267/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007268 * Save variables that might be changed as a side effect. Used when executing
7269 * a timer callback.
7270 */
7271 void
7272save_vimvars(vimvars_save_T *vvsave)
7273{
7274 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7275 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7276 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7277}
7278
7279/*
7280 * Restore variables saved by save_vimvars().
7281 */
7282 void
7283restore_vimvars(vimvars_save_T *vvsave)
7284{
7285 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7286 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7287 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7288}
7289
7290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 * Set string v: variable to a copy of "val".
7292 */
7293 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294set_vim_var_string(
7295 int idx,
7296 char_u *val,
7297 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
Bram Moolenaara542c682016-01-31 16:28:04 +01007299 clear_tv(&vimvars[idx].vv_di.di_tv);
7300 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
7308
7309/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007310 * Set List v: variable to "val".
7311 */
7312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007314{
Bram Moolenaara542c682016-01-31 16:28:04 +01007315 clear_tv(&vimvars[idx].vv_di.di_tv);
7316 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007317 vimvars[idx].vv_list = val;
7318 if (val != NULL)
7319 ++val->lv_refcount;
7320}
7321
7322/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007323 * Set Dictionary v: variable to "val".
7324 */
7325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007326set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007327{
Bram Moolenaara542c682016-01-31 16:28:04 +01007328 clear_tv(&vimvars[idx].vv_di.di_tv);
7329 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007330 vimvars[idx].vv_dict = val;
7331 if (val != NULL)
7332 {
7333 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007334 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007335 }
7336}
7337
7338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 * Set v:register if needed.
7340 */
7341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 char_u regname;
7345
7346 if (c == 0 || c == ' ')
7347 regname = '"';
7348 else
7349 regname = c;
7350 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 set_vim_var_string(VV_REG, &regname, 1);
7353}
7354
7355/*
7356 * Get or set v:exception. If "oldval" == NULL, return the current value.
7357 * Otherwise, restore the value to "oldval" and return NULL.
7358 * Must always be called in pairs to save and restore v:exception! Does not
7359 * take care of memory allocations.
7360 */
7361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007362v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 return NULL;
7369}
7370
7371/*
7372 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7373 * Otherwise, restore the value to "oldval" and return NULL.
7374 * Must always be called in pairs to save and restore v:throwpoint! Does not
7375 * take care of memory allocations.
7376 */
7377 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007378v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 return NULL;
7385}
7386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387/*
7388 * Set v:cmdarg.
7389 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7390 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7391 * Must always be called in pairs!
7392 */
7393 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 char_u *oldval;
7397 char_u *newval;
7398 unsigned len;
7399
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007401 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007403 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
7407
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007408 if (eap->force_bin == FORCE_BIN)
7409 len = 6;
7410 else if (eap->force_bin == FORCE_NOBIN)
7411 len = 8;
7412 else
7413 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007414
7415 if (eap->read_edit)
7416 len += 7;
7417
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007418 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007419 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007420 if (eap->force_enc != 0)
7421 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007422 if (eap->bad_char != 0)
7423 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007424
7425 newval = alloc(len + 1);
7426 if (newval == NULL)
7427 return NULL;
7428
7429 if (eap->force_bin == FORCE_BIN)
7430 sprintf((char *)newval, " ++bin");
7431 else if (eap->force_bin == FORCE_NOBIN)
7432 sprintf((char *)newval, " ++nobin");
7433 else
7434 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007435
7436 if (eap->read_edit)
7437 STRCAT(newval, " ++edit");
7438
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007439 if (eap->force_ff != 0)
7440 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007441 eap->force_ff == 'u' ? "unix"
7442 : eap->force_ff == 'd' ? "dos"
7443 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007444 if (eap->force_enc != 0)
7445 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7446 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007447 if (eap->bad_char == BAD_KEEP)
7448 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7449 else if (eap->bad_char == BAD_DROP)
7450 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7451 else if (eap->bad_char != 0)
7452 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007454 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457/*
7458 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007459 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02007461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007462get_var_tv(
7463 char_u *name,
7464 int len, /* length of "name" */
7465 typval_T *rettv, /* NULL when only checking existence */
7466 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7467 int verbose, /* may give error message */
7468 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469{
7470 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 /* truncate the name, so that we can use strcmp() */
7476 cc = name[len];
7477 name[len] = NUL;
7478
7479 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 * Check for user-defined variables.
7481 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007482 v = find_var(name, NULL, no_autoload);
7483 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007485 tv = &v->di_tv;
7486 if (dip != NULL)
7487 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489
Bram Moolenaare9a41262005-01-15 22:18:47 +00007490 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007492 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007493 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 ret = FAIL;
7495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
7499 name[len] = cc;
7500
7501 return ret;
7502}
7503
7504/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007505 * Check if variable "name[len]" is a local variable or an argument.
7506 * If so, "*eval_lavars_used" is set to TRUE.
7507 */
7508 static void
7509check_vars(char_u *name, int len)
7510{
7511 int cc;
7512 char_u *varname;
7513 hashtab_T *ht;
7514
7515 if (eval_lavars_used == NULL)
7516 return;
7517
7518 /* truncate the name, so that we can use strcmp() */
7519 cc = name[len];
7520 name[len] = NUL;
7521
7522 ht = find_var_ht(name, &varname);
7523 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7524 {
7525 if (find_var(name, NULL, TRUE) != NULL)
7526 *eval_lavars_used = TRUE;
7527 }
7528
7529 name[len] = cc;
7530}
7531
7532/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007533 * Handle:
7534 * - expr[expr], expr[expr:expr] subscript
7535 * - ".name" lookup
7536 * - function call with Funcref variable: func(expr)
7537 * - method call: var->method()
7538 *
7539 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007540 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542handle_subscript(
7543 char_u **arg,
7544 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007545 int evaluate, // do more than finding the end
7546 int verbose, // give error messages
7547 char_u *start_leader, // start of '!' and '-' prefixes
7548 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007549{
7550 int ret = OK;
7551 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007552
Bram Moolenaar61343f02019-07-20 21:11:13 +02007553 // "." is ".name" lookup when we found a dict or when evaluating and
7554 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007555 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02007556 && (((**arg == '['
7557 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02007558 || (!evaluate
7559 && (*arg)[1] != '.'
7560 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007561 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007562 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007563 && !VIM_ISWHITE(*(*arg - 1)))
7564 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007565 {
7566 if (**arg == '(')
7567 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007568 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007569
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007570 // Stop the expression evaluation when immediately aborting on
7571 // error, or when an interrupt occurred or an exception was thrown
7572 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007573 if (aborting())
7574 {
7575 if (ret == OK)
7576 clear_tv(rettv);
7577 ret = FAIL;
7578 }
7579 dict_unref(selfdict);
7580 selfdict = NULL;
7581 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007582 else if (**arg == '-')
7583 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007584 // Expression "-1.0->method()" applies the leader "-" before
7585 // applying ->.
7586 if (evaluate && *end_leaderp > start_leader)
7587 ret = eval7_leader(rettv, start_leader, end_leaderp);
7588 if (ret == OK)
7589 {
7590 if ((*arg)[2] == '{')
7591 // expr->{lambda}()
7592 ret = eval_lambda(arg, rettv, evaluate, verbose);
7593 else
7594 // expr->name()
7595 ret = eval_method(arg, rettv, evaluate, verbose);
7596 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007597 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007598 else /* **arg == '[' || **arg == '.' */
7599 {
7600 dict_unref(selfdict);
7601 if (rettv->v_type == VAR_DICT)
7602 {
7603 selfdict = rettv->vval.v_dict;
7604 if (selfdict != NULL)
7605 ++selfdict->dv_refcount;
7606 }
7607 else
7608 selfdict = NULL;
7609 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7610 {
7611 clear_tv(rettv);
7612 ret = FAIL;
7613 }
7614 }
7615 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007616
Bram Moolenaar1d429612016-05-24 15:44:17 +02007617 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7618 * Don't do this when "Func" is already a partial that was bound
7619 * explicitly (pt_auto is FALSE). */
7620 if (selfdict != NULL
7621 && (rettv->v_type == VAR_FUNC
7622 || (rettv->v_type == VAR_PARTIAL
7623 && (rettv->vval.v_partial->pt_auto
7624 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007625 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007626
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007627 dict_unref(selfdict);
7628 return ret;
7629}
7630
7631/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007632 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 * value).
7634 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007635 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007636alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007637{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007638 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007639}
7640
7641/*
7642 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 * The string "s" must have been allocated, it is consumed.
7644 * Return NULL for out of memory, the variable otherwise.
7645 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007647alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv = alloc_tv();
7652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 rettv->v_type = VAR_STRING;
7655 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657 else
7658 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007666free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
7668 if (varp != NULL)
7669 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 switch (varp->v_type)
7671 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007673 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007674 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007675 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676 vim_free(varp->vval.v_string);
7677 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007678 case VAR_PARTIAL:
7679 partial_unref(varp->vval.v_partial);
7680 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007681 case VAR_BLOB:
7682 blob_unref(varp->vval.v_blob);
7683 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007684 case VAR_LIST:
7685 list_unref(varp->vval.v_list);
7686 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007687 case VAR_DICT:
7688 dict_unref(varp->vval.v_dict);
7689 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007690 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007691#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007692 job_unref(varp->vval.v_job);
7693 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007694#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007695 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007697 channel_unref(varp->vval.v_channel);
7698 break;
7699#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007700 case VAR_NUMBER:
7701 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007702 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007703 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007704 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 vim_free(varp);
7707 }
7708}
7709
7710/*
7711 * Free the memory for a variable value and set the value to NULL or 0.
7712 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007714clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 if (varp != NULL)
7717 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007720 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007721 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007722 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007723 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007724 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007726 case VAR_PARTIAL:
7727 partial_unref(varp->vval.v_partial);
7728 varp->vval.v_partial = NULL;
7729 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007730 case VAR_BLOB:
7731 blob_unref(varp->vval.v_blob);
7732 varp->vval.v_blob = NULL;
7733 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007734 case VAR_LIST:
7735 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007736 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007737 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 case VAR_DICT:
7739 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007743 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007744 varp->vval.v_number = 0;
7745 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007746 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007747#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007748 varp->vval.v_float = 0.0;
7749 break;
7750#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007751 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007752#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007753 job_unref(varp->vval.v_job);
7754 varp->vval.v_job = NULL;
7755#endif
7756 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007757 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007758#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007759 channel_unref(varp->vval.v_channel);
7760 varp->vval.v_channel = NULL;
7761#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 case VAR_UNKNOWN:
7763 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007765 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
7767}
7768
7769/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 * Set the value of a variable to NULL without freeing items.
7771 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007773init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774{
7775 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777}
7778
7779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 * Get the number value of a variable.
7781 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007782 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007783 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007784 * caller of incompatible types: it sets *denote to TRUE if "denote"
7785 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007787 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007788tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 int error = FALSE;
7791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007792 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007793}
7794
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007795 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007796tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007797{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007798 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007803 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007805#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007806 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007807 break;
7808#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007810 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007811 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 break;
7813 case VAR_STRING:
7814 if (varp->vval.v_string != NULL)
7815 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007816 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007817 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007818 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007819 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007820 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007821 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007822 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007823 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007824 case VAR_SPECIAL:
7825 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7826 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007827 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007828#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007829 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007830 break;
7831#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007832 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007833#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007834 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007835 break;
7836#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007837 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007838 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007839 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007840 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007841 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007844 if (denote == NULL) /* useful for values that must be unsigned */
7845 n = -1;
7846 else
7847 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007851#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007853tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007854{
7855 switch (varp->v_type)
7856 {
7857 case VAR_NUMBER:
7858 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007859 case VAR_FLOAT:
7860 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007861 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007862 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007863 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007864 break;
7865 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007866 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007867 break;
7868 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007869 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007870 break;
7871 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007872 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007873 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007874 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007875 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007876 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007877 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007878# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007879 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007880 break;
7881# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007882 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007883# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007884 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007885 break;
7886# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007887 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007889 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007890 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007891 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007892 break;
7893 }
7894 return 0;
7895}
7896#endif
7897
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 * Get the string value of a variable.
7900 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007901 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7902 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 * If the String variable has never been set, return an empty string.
7904 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007906 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007908 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007909tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 static char_u mybuf[NUMBUFLEN];
7912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007913 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914}
7915
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007916 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007917tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007919 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007920
7921 return res != NULL ? res : (char_u *)"";
7922}
7923
Bram Moolenaar7d647822014-04-05 21:28:56 +02007924/*
7925 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7926 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007927 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007928tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007929{
7930 static char_u mybuf[NUMBUFLEN];
7931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007932 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933}
7934
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007935 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007936tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007937{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007940 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007941 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007942 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007943 return buf;
7944 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007945 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007946 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 break;
7948 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007949 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 break;
7951 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007952 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007953 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007954 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007955#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007956 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007957 break;
7958#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007959 case VAR_STRING:
7960 if (varp->vval.v_string != NULL)
7961 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007962 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007963 case VAR_SPECIAL:
7964 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7965 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007966 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007967 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007968 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007969 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007970#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007971 {
7972 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007973 char *status;
7974
7975 if (job == NULL)
7976 return (char_u *)"no process";
7977 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007978 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007979 : "run";
7980# ifdef UNIX
7981 vim_snprintf((char *)buf, NUMBUFLEN,
7982 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007983# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007984 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007985 "process %ld %s",
7986 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007987 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007988# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007989 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007990 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7991# endif
7992 return buf;
7993 }
7994#endif
7995 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007996 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007997#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007998 {
7999 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02008000 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01008001
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008002 if (channel == NULL)
8003 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
8004 else
8005 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01008006 "channel %d %s", channel->ch_id, status);
8007 return buf;
8008 }
8009#endif
8010 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008011 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008012 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008013 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008015 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016}
8017
8018/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008019 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
8020 * string() on Dict, List, etc.
8021 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02008022 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008023tv_stringify(typval_T *varp, char_u *buf)
8024{
8025 if (varp->v_type == VAR_LIST
8026 || varp->v_type == VAR_DICT
8027 || varp->v_type == VAR_FUNC
8028 || varp->v_type == VAR_PARTIAL
8029 || varp->v_type == VAR_FLOAT)
8030 {
8031 typval_T tmp;
8032
8033 f_string(varp, &tmp);
8034 tv_get_string_buf(&tmp, buf);
8035 clear_tv(varp);
8036 *varp = tmp;
8037 return tmp.vval.v_string;
8038 }
8039 return tv_get_string_buf(varp, buf);
8040}
8041
8042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 * Find variable "name" in the list of variables.
8044 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008045 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00008046 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008049 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008050find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008054 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055
Bram Moolenaara7043832005-01-21 11:56:39 +00008056 ht = find_var_ht(name, &varname);
8057 if (htp != NULL)
8058 *htp = ht;
8059 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008061 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
8062 if (ret != NULL)
8063 return ret;
8064
8065 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008066 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
8069/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02008070 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00008071 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008074find_var_in_ht(
8075 hashtab_T *ht,
8076 int htname,
8077 char_u *varname,
8078 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00008079{
Bram Moolenaar33570922005-01-25 22:26:29 +00008080 hashitem_T *hi;
8081
8082 if (*varname == NUL)
8083 {
8084 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02008085 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00008086 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008087 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00008088 case 'g': return &globvars_var;
8089 case 'v': return &vimvars_var;
8090 case 'b': return &curbuf->b_bufvar;
8091 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008092 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008093 case 'l': return get_funccal_local_var();
8094 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00008095 }
8096 return NULL;
8097 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008098
8099 hi = hash_find(ht, varname);
8100 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008101 {
8102 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008103 * worked find the variable again. Don't auto-load a script if it was
8104 * loaded already, otherwise it would be loaded every time when
8105 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008106 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008107 {
8108 /* Note: script_autoload() may make "hi" invalid. It must either
8109 * be obtained again or not used. */
8110 if (!script_autoload(varname, FALSE) || aborting())
8111 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008112 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008113 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008114 if (HASHITEM_EMPTY(hi))
8115 return NULL;
8116 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008118}
8119
8120/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02008122 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00008123 * Set "varname" to the start of name without ':'.
8124 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008125 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008126find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008128 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008129 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008130
Bram Moolenaar73627d02015-08-11 15:46:09 +02008131 if (name[0] == NUL)
8132 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 if (name[1] != ':')
8134 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008135 /* The name must not start with a colon or #. */
8136 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 return NULL;
8138 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00008139
Bram Moolenaard2e716e2019-04-20 14:39:52 +02008140 // "version" is "v:version" in all scopes if scriptversion < 3.
8141 // Same for a few other variables marked with VV_COMPAT.
8142 if (current_sctx.sc_version < 3)
8143 {
8144 hi = hash_find(&compat_hashtab, name);
8145 if (!HASHITEM_EMPTY(hi))
8146 return &compat_hashtab;
8147 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00008148
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008149 ht = get_funccal_local_ht();
8150 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008151 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008152 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
8154 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008155 if (*name == 'g') /* global variable */
8156 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02008157 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008158 if (vim_strchr(name + 2, ':') != NULL
8159 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008160 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008162 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008164 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008165 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008166 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 if (*name == 'v') /* v: variable */
8168 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008169 if (*name == 'a') /* a: function argument */
8170 return get_funccal_args_ht();
8171 if (*name == 'l') /* l: local function variable */
8172 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008174 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
8175 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 return NULL;
8177}
8178
8179/*
8180 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008181 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 * Returns NULL when it doesn't exist.
8183 */
8184 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008185get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008189 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 if (v == NULL)
8191 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008192 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193}
8194
8195/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008196 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 * sourcing this script and when executing functions defined in the script.
8198 */
8199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008200new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
Bram Moolenaara7043832005-01-21 11:56:39 +00008202 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 hashtab_T *ht;
8204 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008205
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8207 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008208 /* Re-allocating ga_data means that an ht_array pointing to
8209 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008211 for (i = 1; i <= ga_scripts.ga_len; ++i)
8212 {
8213 ht = &SCRIPT_VARS(i);
8214 if (ht->ht_mask == HT_INIT_SIZE - 1)
8215 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008216 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008217 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008218 }
8219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 while (ga_scripts.ga_len < id)
8221 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008222 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008223 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008224 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227 }
8228}
8229
8230/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8232 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 */
8234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008235init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
Bram Moolenaar33570922005-01-25 22:26:29 +00008237 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008238 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008239 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008240 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008241 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008242 dict_var->di_tv.vval.v_dict = dict;
8243 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008244 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008245 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8246 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
8248
8249/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008250 * Unreference a dictionary initialized by init_var_dict().
8251 */
8252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008253unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008254{
8255 /* Now the dict needs to be freed if no one else is using it, go back to
8256 * normal reference counting. */
8257 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8258 dict_unref(dict);
8259}
8260
8261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 * Frees all allocated variables and the value they contain.
8264 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 */
8266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008267vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008268{
8269 vars_clear_ext(ht, TRUE);
8270}
8271
8272/*
8273 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8274 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008276vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
Bram Moolenaara7043832005-01-21 11:56:39 +00008278 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008279 hashitem_T *hi;
8280 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008283 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008284 for (hi = ht->ht_array; todo > 0; ++hi)
8285 {
8286 if (!HASHITEM_EMPTY(hi))
8287 {
8288 --todo;
8289
Bram Moolenaar33570922005-01-25 22:26:29 +00008290 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008291 * ht_array might change then. hash_clear() takes care of it
8292 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 v = HI2DI(hi);
8294 if (free_val)
8295 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008296 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008298 }
8299 }
8300 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008301 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302}
8303
Bram Moolenaara7043832005-01-21 11:56:39 +00008304/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 * Delete a variable from hashtab "ht" at item "hi".
8306 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008309delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008312
8313 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 clear_tv(&di->di_tv);
8315 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316}
8317
8318/*
8319 * List the value of one internal variable.
8320 */
8321 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008322list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008324 char_u *tofree;
8325 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008326 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008327
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008328 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008330 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332}
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008335list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008336 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008337 char_u *name,
8338 int type,
8339 char_u *string,
8340 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341{
Bram Moolenaar31859182007-08-14 20:41:13 +00008342 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8343 msg_start();
8344 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008346 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 msg_putchar(' ');
8348 msg_advance(22);
8349 if (type == VAR_NUMBER)
8350 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008351 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 msg_putchar('*');
8353 else if (type == VAR_LIST)
8354 {
8355 msg_putchar('[');
8356 if (*string == '[')
8357 ++string;
8358 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008359 else if (type == VAR_DICT)
8360 {
8361 msg_putchar('{');
8362 if (*string == '{')
8363 ++string;
8364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 else
8366 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008367
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008369
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008370 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008371 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008372 if (*first)
8373 {
8374 msg_clr_eos();
8375 *first = FALSE;
8376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377}
8378
8379/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 * If the variable already exists, the value is updated.
8382 * Otherwise the variable is created.
8383 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008385set_var(
8386 char_u *name,
8387 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008388 int copy) // make copy of value in "tv"
8389{
8390 set_var_const(name, tv, copy, FALSE);
8391}
8392
8393/*
8394 * Set variable "name" to value in "tv".
8395 * If the variable already exists and "is_const" is FALSE the value is updated.
8396 * Otherwise the variable is created.
8397 */
8398 static void
8399set_var_const(
8400 char_u *name,
8401 typval_T *tv,
8402 int copy, // make copy of value in "tv"
8403 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaar33570922005-01-25 22:26:29 +00008405 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008409 ht = find_var_ht(name, &varname);
8410 if (ht == NULL || *varname == NUL)
8411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008412 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008413 return;
8414 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008415 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008416
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008417 /* Search in parent scope which is possible to reference from lambda */
8418 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008419 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008420
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008421 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8422 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008423 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424
Bram Moolenaar33570922005-01-25 22:26:29 +00008425 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008427 if (is_const)
8428 {
8429 emsg(_(e_cannot_mod));
8430 return;
8431 }
8432
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008434 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008435 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008437
8438 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008439 * Handle setting internal v: variables separately where needed to
8440 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 */
8442 if (ht == &vimvarht)
8443 {
8444 if (v->di_tv.v_type == VAR_STRING)
8445 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008446 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008447 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008448 {
8449 char_u *val = tv_get_string(tv);
8450
8451 // Careful: when assigning to v:errmsg and tv_get_string()
8452 // causes an error message the variable will alrady be set.
8453 if (v->di_tv.vval.v_string == NULL)
8454 v->di_tv.vval.v_string = vim_strsave(val);
8455 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008456 else
8457 {
8458 /* Take over the string to avoid an extra alloc/free. */
8459 v->di_tv.vval.v_string = tv->vval.v_string;
8460 tv->vval.v_string = NULL;
8461 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008462 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008464 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008465 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008466 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008467 if (STRCMP(varname, "searchforward") == 0)
8468 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008469#ifdef FEAT_SEARCH_EXTRA
8470 else if (STRCMP(varname, "hlsearch") == 0)
8471 {
8472 no_hlsearch = !v->di_tv.vval.v_number;
8473 redraw_all_later(SOME_VALID);
8474 }
8475#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008476 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008477 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008478 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008480 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008481 return;
8482 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 }
8484
8485 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 }
8487 else /* add a new variable */
8488 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008489 // Can't add "v:" or "a:" variable.
8490 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008492 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008493 return;
8494 }
8495
Bram Moolenaar31b81602019-02-10 22:14:27 +01008496 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008497 if (!valid_varname(varname))
8498 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008499
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008500 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008501 if (v == NULL)
8502 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008504 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008506 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 return;
8508 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008509 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008510 if (is_const)
8511 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008513
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008514 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008516 else
8517 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008518 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008519 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008521 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008522
8523 if (is_const)
8524 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525}
8526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008528 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 * Also give an error message.
8530 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008532var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008533{
8534 if (flags & DI_FLAGS_RO)
8535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008536 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 return TRUE;
8538 }
8539 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008541 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 return TRUE;
8543 }
8544 return FALSE;
8545}
8546
8547/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008548 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8549 * Also give an error message.
8550 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008552var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008553{
8554 if (flags & DI_FLAGS_FIX)
8555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008556 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008557 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008558 return TRUE;
8559 }
8560 return FALSE;
8561}
8562
8563/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008564 * Check if a funcref is assigned to a valid variable name.
8565 * Return TRUE and give an error if not.
8566 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008568var_check_func_name(
8569 char_u *name, /* points to start of variable name */
8570 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008571{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008572 /* Allow for w: b: s: and t:. */
8573 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008574 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8575 ? name[2] : name[0]))
8576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008577 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008578 name);
8579 return TRUE;
8580 }
8581 /* Don't allow hiding a function. When "v" is not NULL we might be
8582 * assigning another function to the same var, the type is checked
8583 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008584 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008586 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008587 name);
8588 return TRUE;
8589 }
8590 return FALSE;
8591}
8592
8593/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008594 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008595 * Also give an error message, using "name" or _("name") when use_gettext is
8596 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008597 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008598 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008599var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008600{
8601 if (lock & VAR_LOCKED)
8602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008603 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008604 name == NULL ? (char_u *)_("Unknown")
8605 : use_gettext ? (char_u *)_(name)
8606 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008607 return TRUE;
8608 }
8609 if (lock & VAR_FIXED)
8610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008611 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008612 name == NULL ? (char_u *)_("Unknown")
8613 : use_gettext ? (char_u *)_(name)
8614 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008615 return TRUE;
8616 }
8617 return FALSE;
8618}
8619
8620/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008621 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8622 * Also give an error message, using "name" or _("name") when use_gettext is
8623 * TRUE.
8624 */
8625 static int
8626tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8627{
8628 int lock = 0;
8629
8630 switch (tv->v_type)
8631 {
8632 case VAR_BLOB:
8633 if (tv->vval.v_blob != NULL)
8634 lock = tv->vval.v_blob->bv_lock;
8635 break;
8636 case VAR_LIST:
8637 if (tv->vval.v_list != NULL)
8638 lock = tv->vval.v_list->lv_lock;
8639 break;
8640 case VAR_DICT:
8641 if (tv->vval.v_dict != NULL)
8642 lock = tv->vval.v_dict->dv_lock;
8643 break;
8644 default:
8645 break;
8646 }
8647 return var_check_lock(tv->v_lock, name, use_gettext)
8648 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8649}
8650
8651/*
8652 * Check if a variable name is valid.
8653 * Return FALSE and give an error if not.
8654 */
8655 int
8656valid_varname(char_u *varname)
8657{
8658 char_u *p;
8659
8660 for (p = varname; *p != NUL; ++p)
8661 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8662 && *p != AUTOLOAD_CHAR)
8663 {
8664 semsg(_(e_illvar), varname);
8665 return FALSE;
8666 }
8667 return TRUE;
8668}
8669
8670/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008672 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008673 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008674 * It is OK for "from" and "to" to point to the same item. This is used to
8675 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008676 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008678copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008680 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008681 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008682 switch (from->v_type)
8683 {
8684 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008685 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008686 to->vval.v_number = from->vval.v_number;
8687 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008688 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008689#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008690 to->vval.v_float = from->vval.v_float;
8691 break;
8692#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008693 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008694#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008695 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008696 if (to->vval.v_job != NULL)
8697 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008698 break;
8699#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008700 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008701#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008702 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008703 if (to->vval.v_channel != NULL)
8704 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008705 break;
8706#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 case VAR_STRING:
8708 case VAR_FUNC:
8709 if (from->vval.v_string == NULL)
8710 to->vval.v_string = NULL;
8711 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008714 if (from->v_type == VAR_FUNC)
8715 func_ref(to->vval.v_string);
8716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008717 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008718 case VAR_PARTIAL:
8719 if (from->vval.v_partial == NULL)
8720 to->vval.v_partial = NULL;
8721 else
8722 {
8723 to->vval.v_partial = from->vval.v_partial;
8724 ++to->vval.v_partial->pt_refcount;
8725 }
8726 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008727 case VAR_BLOB:
8728 if (from->vval.v_blob == NULL)
8729 to->vval.v_blob = NULL;
8730 else
8731 {
8732 to->vval.v_blob = from->vval.v_blob;
8733 ++to->vval.v_blob->bv_refcount;
8734 }
8735 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008736 case VAR_LIST:
8737 if (from->vval.v_list == NULL)
8738 to->vval.v_list = NULL;
8739 else
8740 {
8741 to->vval.v_list = from->vval.v_list;
8742 ++to->vval.v_list->lv_refcount;
8743 }
8744 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008745 case VAR_DICT:
8746 if (from->vval.v_dict == NULL)
8747 to->vval.v_dict = NULL;
8748 else
8749 {
8750 to->vval.v_dict = from->vval.v_dict;
8751 ++to->vval.v_dict->dv_refcount;
8752 }
8753 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008754 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008755 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 break;
8757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758}
8759
8760/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761 * Make a copy of an item.
8762 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008763 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8764 * reference to an already copied list/dict can be used.
8765 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008768item_copy(
8769 typval_T *from,
8770 typval_T *to,
8771 int deep,
8772 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008773{
8774 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008775 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008776
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008779 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008780 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 }
8782 ++recurse;
8783
8784 switch (from->v_type)
8785 {
8786 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008787 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008788 case VAR_STRING:
8789 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008790 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008791 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008792 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008793 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008794 copy_tv(from, to);
8795 break;
8796 case VAR_LIST:
8797 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008798 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008799 if (from->vval.v_list == NULL)
8800 to->vval.v_list = NULL;
8801 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8802 {
8803 /* use the copy made earlier */
8804 to->vval.v_list = from->vval.v_list->lv_copylist;
8805 ++to->vval.v_list->lv_refcount;
8806 }
8807 else
8808 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8809 if (to->vval.v_list == NULL)
8810 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008811 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008812 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008813 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008814 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008815 case VAR_DICT:
8816 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008817 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008818 if (from->vval.v_dict == NULL)
8819 to->vval.v_dict = NULL;
8820 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8821 {
8822 /* use the copy made earlier */
8823 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8824 ++to->vval.v_dict->dv_refcount;
8825 }
8826 else
8827 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8828 if (to->vval.v_dict == NULL)
8829 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008830 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008831 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008832 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008834 }
8835 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008837}
8838
8839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 * This function is used by f_input() and f_inputdialog() functions. The third
8841 * argument to f_input() specifies the type of completion to use at the
8842 * prompt. The third argument to f_inputdialog() specifies the value to return
8843 * when the user cancels the prompt.
8844 */
8845 void
8846get_user_input(
8847 typval_T *argvars,
8848 typval_T *rettv,
8849 int inputdialog,
8850 int secret)
8851{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008852 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 char_u *p = NULL;
8854 int c;
8855 char_u buf[NUMBUFLEN];
8856 int cmd_silent_save = cmd_silent;
8857 char_u *defstr = (char_u *)"";
8858 int xp_type = EXPAND_NOTHING;
8859 char_u *xp_arg = NULL;
8860
8861 rettv->v_type = VAR_STRING;
8862 rettv->vval.v_string = NULL;
8863
8864#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008865 /* While starting up, there is no place to enter text. When running tests
8866 * with --not-a-term we assume feedkeys() will be used. */
8867 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868 return;
8869#endif
8870
8871 cmd_silent = FALSE; /* Want to see the prompt. */
8872 if (prompt != NULL)
8873 {
8874 /* Only the part of the message after the last NL is considered as
8875 * prompt for the command line */
8876 p = vim_strrchr(prompt, '\n');
8877 if (p == NULL)
8878 p = prompt;
8879 else
8880 {
8881 ++p;
8882 c = *p;
8883 *p = NUL;
8884 msg_start();
8885 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008886 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 msg_didout = FALSE;
8888 msg_starthere();
8889 *p = c;
8890 }
8891 cmdline_row = msg_row;
8892
8893 if (argvars[1].v_type != VAR_UNKNOWN)
8894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008895 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 if (defstr != NULL)
8897 stuffReadbuffSpec(defstr);
8898
8899 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8900 {
8901 char_u *xp_name;
8902 int xp_namelen;
8903 long argt;
8904
8905 /* input() with a third argument: completion */
8906 rettv->vval.v_string = NULL;
8907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008908 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 if (xp_name == NULL)
8910 return;
8911
8912 xp_namelen = (int)STRLEN(xp_name);
8913
8914 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8915 &xp_arg) == FAIL)
8916 return;
8917 }
8918 }
8919
8920 if (defstr != NULL)
8921 {
8922 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 ex_normal_busy = 0;
8925 rettv->vval.v_string =
8926 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8927 xp_type, xp_arg);
8928 ex_normal_busy = save_ex_normal_busy;
8929 }
8930 if (inputdialog && rettv->vval.v_string == NULL
8931 && argvars[1].v_type != VAR_UNKNOWN
8932 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008933 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 &argvars[2], buf));
8935
8936 vim_free(xp_arg);
8937
8938 /* since the user typed this, no need to wait for return */
8939 need_wait_return = FALSE;
8940 msg_didout = FALSE;
8941 }
8942 cmd_silent = cmd_silent_save;
8943}
8944
8945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 * ":echo expr1 ..." print each argument separated with a space, add a
8947 * newline at the end.
8948 * ":echon expr1 ..." print each argument plain.
8949 */
8950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008951ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008954 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008955 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 char_u *p;
8957 int needclr = TRUE;
8958 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008959 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008960 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008961 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962
8963 if (eap->skip)
8964 ++emsg_skip;
8965 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8966 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008967 /* If eval1() causes an error message the text from the command may
8968 * still need to be cleared. E.g., "echo 22,44". */
8969 need_clr_eos = needclr;
8970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 /*
8975 * Report the invalid expression unless the expression evaluation
8976 * has been cancelled due to an aborting error, an interrupt, or an
8977 * exception.
8978 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008979 if (!aborting() && did_emsg == did_emsg_before
8980 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008981 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008982 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 break;
8984 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008985 need_clr_eos = FALSE;
8986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 if (!eap->skip)
8988 {
8989 if (atstart)
8990 {
8991 atstart = FALSE;
8992 /* Call msg_start() after eval1(), evaluating the expression
8993 * may cause a message to appear. */
8994 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008995 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008996 /* Mark the saved text as finishing the line, so that what
8997 * follows is displayed on a new line when scrolling back
8998 * at the more prompt. */
8999 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01009001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 }
9003 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01009004 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01009005 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009006 if (p != NULL)
9007 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009009 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009011 if (*p != TAB && needclr)
9012 {
9013 /* remove any text still there from the command */
9014 msg_clr_eos();
9015 needclr = FALSE;
9016 }
9017 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 }
9019 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009020 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009021 if (has_mbyte)
9022 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009023 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009024
9025 (void)msg_outtrans_len_attr(p, i, echo_attr);
9026 p += i - 1;
9027 }
9028 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009029 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009032 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 arg = skipwhite(arg);
9036 }
9037 eap->nextcmd = check_nextcmd(arg);
9038
9039 if (eap->skip)
9040 --emsg_skip;
9041 else
9042 {
9043 /* remove text that may still be there from the command */
9044 if (needclr)
9045 msg_clr_eos();
9046 if (eap->cmdidx == CMD_echo)
9047 msg_end();
9048 }
9049}
9050
9051/*
9052 * ":echohl {name}".
9053 */
9054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009055ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02009057 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
9061 * ":execute expr1 ..." execute the result of an expression.
9062 * ":echomsg expr1 ..." Print a message
9063 * ":echoerr expr1 ..." Print an error
9064 * Each gets spaces around each argument and a newline at the end for
9065 * echo commands
9066 */
9067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00009071 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 int ret = OK;
9073 char_u *p;
9074 garray_T ga;
9075 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01009076 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
9078 ga_init2(&ga, 1, 80);
9079
9080 if (eap->skip)
9081 ++emsg_skip;
9082 while (*arg != NUL && *arg != '|' && *arg != '\n')
9083 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01009084 ret = eval1_emsg(&arg, &rettv, !eap->skip);
9085 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
9088 if (!eap->skip)
9089 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009090 char_u buf[NUMBUFLEN];
9091
9092 if (eap->cmdidx == CMD_execute)
9093 p = tv_get_string_buf(&rettv, buf);
9094 else
9095 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 len = (int)STRLEN(p);
9097 if (ga_grow(&ga, len + 2) == FAIL)
9098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 ret = FAIL;
9101 break;
9102 }
9103 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ga.ga_len += len;
9107 }
9108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 arg = skipwhite(arg);
9111 }
9112
9113 if (ret != FAIL && ga.ga_data != NULL)
9114 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009115 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
9116 {
9117 /* Mark the already saved text as finishing the line, so that what
9118 * follows is displayed on a new line when scrolling back at the
9119 * more prompt. */
9120 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009121 }
9122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009124 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009125 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009126 out_flush();
9127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 else if (eap->cmdidx == CMD_echoerr)
9129 {
9130 /* We don't want to abort following commands, restore did_emsg. */
9131 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009132 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 if (!force_abort)
9134 did_emsg = save_did_emsg;
9135 }
9136 else if (eap->cmdidx == CMD_execute)
9137 do_cmdline((char_u *)ga.ga_data,
9138 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9139 }
9140
9141 ga_clear(&ga);
9142
9143 if (eap->skip)
9144 --emsg_skip;
9145
9146 eap->nextcmd = check_nextcmd(arg);
9147}
9148
9149/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009150 * Find window specified by "vp" in tabpage "tp".
9151 */
9152 win_T *
9153find_win_by_nr(
9154 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009155 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009158 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009160 if (nr < 0)
9161 return NULL;
9162 if (nr == 0)
9163 return curwin;
9164
Bram Moolenaar29323592016-07-24 22:04:11 +02009165 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
9166 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167 if (nr >= LOWEST_WIN_ID)
9168 {
9169 if (wp->w_id == nr)
9170 return wp;
9171 }
9172 else if (--nr <= 0)
9173 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02009174 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009175 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009176 {
9177#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009178 // check tab-local popup windows
9179 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009180 if (wp->w_id == nr)
9181 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009182 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009183 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9184 if (wp->w_id == nr)
9185 return wp;
9186#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009188 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190}
9191
9192/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009193 * Find a window: When using a Window ID in any tab page, when using a number
9194 * in the current tab page.
9195 */
9196 win_T *
9197find_win_by_nr_or_id(typval_T *vp)
9198{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009199 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009200
9201 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009202 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009203 return find_win_by_nr(vp, NULL);
9204}
9205
9206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009207 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009208 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009209 */
9210 win_T *
9211find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009212 typval_T *wvp, // VAR_UNKNOWN for current window
9213 typval_T *tvp, // VAR_UNKNOWN for current tab page
9214 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215{
9216 win_T *wp = NULL;
9217 tabpage_T *tp = NULL;
9218 long n;
9219
9220 if (wvp->v_type != VAR_UNKNOWN)
9221 {
9222 if (tvp->v_type != VAR_UNKNOWN)
9223 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009224 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 if (n >= 0)
9226 tp = find_tabpage(n);
9227 }
9228 else
9229 tp = curtab;
9230
9231 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009232 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009234 if (wp == NULL && wvp->v_type == VAR_NUMBER
9235 && wvp->vval.v_number != -1)
9236 // A window with the specified number is not found
9237 tp = NULL;
9238 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239 }
9240 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009241 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009243 tp = curtab;
9244 }
9245
9246 if (ptp != NULL)
9247 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009248
9249 return wp;
9250}
9251
9252/*
9253 * getwinvar() and gettabwinvar()
9254 */
9255 void
9256getwinvar(
9257 typval_T *argvars,
9258 typval_T *rettv,
9259 int off) /* 1 for gettabwinvar() */
9260{
9261 win_T *win;
9262 char_u *varname;
9263 dictitem_T *v;
9264 tabpage_T *tp = NULL;
9265 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009266 win_T *oldcurwin;
9267 tabpage_T *oldtabpage;
9268 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009270 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009271 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 else
9273 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009275 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009276 ++emsg_off;
9277
9278 rettv->v_type = VAR_STRING;
9279 rettv->vval.v_string = NULL;
9280
9281 if (win != NULL && varname != NULL)
9282 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 /* Set curwin to be our win, temporarily. Also set the tabpage,
9284 * otherwise the window is not valid. Only do this when needed,
9285 * autocommands get blocked. */
9286 need_switch_win = !(tp == curtab && win == curwin);
9287 if (!need_switch_win
9288 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009290 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009292 if (varname[1] == NUL)
9293 {
9294 /* get all window-local options in a dict */
9295 dict_T *opts = get_winbuf_options(FALSE);
9296
9297 if (opts != NULL)
9298 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009299 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009300 done = TRUE;
9301 }
9302 }
9303 else if (get_option_tv(&varname, rettv, 1) == OK)
9304 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009305 done = TRUE;
9306 }
9307 else
9308 {
9309 /* Look up the variable. */
9310 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9311 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9312 varname, FALSE);
9313 if (v != NULL)
9314 {
9315 copy_tv(&v->di_tv, rettv);
9316 done = TRUE;
9317 }
9318 }
9319 }
9320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321 if (need_switch_win)
9322 /* restore previous notion of curwin */
9323 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324 }
9325
9326 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9327 /* use the default return value */
9328 copy_tv(&argvars[off + 2], rettv);
9329
9330 --emsg_off;
9331}
9332
9333/*
9334 * "setwinvar()" and "settabwinvar()" functions
9335 */
9336 void
9337setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9338{
9339 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 win_T *save_curwin;
9341 tabpage_T *save_curtab;
9342 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 char_u *varname, *winvarname;
9344 typval_T *varp;
9345 char_u nbuf[NUMBUFLEN];
9346 tabpage_T *tp = NULL;
9347
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009348 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009349 return;
9350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009352 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353 else
9354 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009356 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357 varp = &argvars[off + 2];
9358
9359 if (win != NULL && varname != NULL && varp != NULL)
9360 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361 need_switch_win = !(tp == curtab && win == curwin);
9362 if (!need_switch_win
9363 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 {
9365 if (*varname == '&')
9366 {
9367 long numval;
9368 char_u *strval;
9369 int error = FALSE;
9370
9371 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009372 numval = (long)tv_get_number_chk(varp, &error);
9373 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 if (!error && strval != NULL)
9375 set_option_value(varname, numval, strval, OPT_LOCAL);
9376 }
9377 else
9378 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009379 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380 if (winvarname != NULL)
9381 {
9382 STRCPY(winvarname, "w:");
9383 STRCPY(winvarname + 2, varname);
9384 set_var(winvarname, varp, TRUE);
9385 vim_free(winvarname);
9386 }
9387 }
9388 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 if (need_switch_win)
9390 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391 }
9392}
9393
9394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9396 * "arg" points to the "&" or '+' when called, to "option" when returning.
9397 * Returns NULL when no option name found. Otherwise pointer to the char
9398 * after the option name.
9399 */
9400 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009401find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403 char_u *p = *arg;
9404
9405 ++p;
9406 if (*p == 'g' && p[1] == ':')
9407 {
9408 *opt_flags = OPT_GLOBAL;
9409 p += 2;
9410 }
9411 else if (*p == 'l' && p[1] == ':')
9412 {
9413 *opt_flags = OPT_LOCAL;
9414 p += 2;
9415 }
9416 else
9417 *opt_flags = 0;
9418
9419 if (!ASCII_ISALPHA(*p))
9420 return NULL;
9421 *arg = p;
9422
9423 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9424 p += 4; /* termcap option */
9425 else
9426 while (ASCII_ISALPHA(*p))
9427 ++p;
9428 return p;
9429}
9430
9431/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009432 * Return the autoload script name for a function or variable name.
9433 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009434 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009436 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009437autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009438{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009439 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009440 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009441
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009442 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009443 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009444 if (scriptname == NULL)
Bram Moolenaar1058c9d2019-08-20 21:58:00 +02009445 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009446 STRCPY(scriptname, "autoload/");
9447 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009448 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9449 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009450 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009451 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009452 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009453}
9454
9455/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009456 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009457 * Return TRUE if a package was loaded.
9458 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009460script_autoload(
9461 char_u *name,
9462 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009463{
9464 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009465 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009466 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009467 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009468
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009469 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009470 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009471 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009472 return FALSE;
9473
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009474 tofree = scriptname = autoload_name(name);
Bram Moolenaar1058c9d2019-08-20 21:58:00 +02009475 if (scriptname == NULL)
9476 return FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009477
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009478 /* Find the name in the list of previously loaded package names. Skip
9479 * "autoload/", it's always the same. */
9480 for (i = 0; i < ga_loaded.ga_len; ++i)
9481 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9482 break;
9483 if (!reload && i < ga_loaded.ga_len)
9484 ret = FALSE; /* was loaded already */
9485 else
9486 {
9487 /* Remember the name if it wasn't loaded already. */
9488 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9489 {
9490 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9491 tofree = NULL;
9492 }
9493
9494 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009495 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009496 ret = TRUE;
9497 }
9498
9499 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009500 return ret;
9501}
9502
Bram Moolenaar661b1822005-07-28 22:36:45 +00009503/*
9504 * Display script name where an item was last set.
9505 * Should only be invoked when 'verbose' is non-zero.
9506 */
9507 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009508last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009509{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009510 char_u *p;
9511
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009512 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009513 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009514 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009515 if (p != NULL)
9516 {
9517 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009518 msg_puts(_("\n\tLast set from "));
9519 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009520 if (script_ctx.sc_lnum > 0)
9521 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009522 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009523 msg_outnum((long)script_ctx.sc_lnum);
9524 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009525 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009526 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009527 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009528 }
9529}
9530
Bram Moolenaar61be3762019-03-19 23:04:17 +01009531/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009532 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9533 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009534 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009537{
9538 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9539 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009540 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9541 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009542 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009543 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009544}
9545
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 * Add an assert error to v:errors.
9548 */
9549 void
9550assert_error(garray_T *gap)
9551{
9552 struct vimvar *vp = &vimvars[VV_ERRORS];
9553
9554 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9555 /* Make sure v:errors is a list. */
9556 set_vim_var_list(VV_ERRORS, list_alloc());
9557 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9558}
Bram Moolenaar31988702018-02-13 12:57:42 +01009559/*
9560 * Compare "typ1" and "typ2". Put the result in "typ1".
9561 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009562 int
9563typval_compare(
9564 typval_T *typ1, /* first operand */
9565 typval_T *typ2, /* second operand */
9566 exptype_T type, /* operator */
9567 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009568 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009569{
9570 int i;
9571 varnumber_T n1, n2;
9572 char_u *s1, *s2;
9573 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9574
Bram Moolenaar31988702018-02-13 12:57:42 +01009575 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009576 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009577 /* For "is" a different type always means FALSE, for "notis"
9578 * it means TRUE. */
9579 n1 = (type == TYPE_NEQUAL);
9580 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009581 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9582 {
9583 if (type_is)
9584 {
9585 n1 = (typ1->v_type == typ2->v_type
9586 && typ1->vval.v_blob == typ2->vval.v_blob);
9587 if (type == TYPE_NEQUAL)
9588 n1 = !n1;
9589 }
9590 else if (typ1->v_type != typ2->v_type
9591 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9592 {
9593 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009594 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009595 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009596 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009597 clear_tv(typ1);
9598 return FAIL;
9599 }
9600 else
9601 {
9602 // Compare two Blobs for being equal or unequal.
9603 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9604 if (type == TYPE_NEQUAL)
9605 n1 = !n1;
9606 }
9607 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009608 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9609 {
9610 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009611 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009612 n1 = (typ1->v_type == typ2->v_type
9613 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009614 if (type == TYPE_NEQUAL)
9615 n1 = !n1;
9616 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009617 else if (typ1->v_type != typ2->v_type
9618 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009619 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009620 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009621 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009622 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009623 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009624 clear_tv(typ1);
9625 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009626 }
9627 else
9628 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009629 /* Compare two Lists for being equal or unequal. */
9630 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9631 ic, FALSE);
9632 if (type == TYPE_NEQUAL)
9633 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009634 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009635 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009636
9637 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9638 {
9639 if (type_is)
9640 {
9641 n1 = (typ1->v_type == typ2->v_type
9642 && typ1->vval.v_dict == typ2->vval.v_dict);
9643 if (type == TYPE_NEQUAL)
9644 n1 = !n1;
9645 }
9646 else if (typ1->v_type != typ2->v_type
9647 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9648 {
9649 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009650 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009651 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009652 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009653 clear_tv(typ1);
9654 return FAIL;
9655 }
9656 else
9657 {
9658 /* Compare two Dictionaries for being equal or unequal. */
9659 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9660 ic, FALSE);
9661 if (type == TYPE_NEQUAL)
9662 n1 = !n1;
9663 }
9664 }
9665
9666 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9667 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9668 {
9669 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009671 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009672 clear_tv(typ1);
9673 return FAIL;
9674 }
9675 if ((typ1->v_type == VAR_PARTIAL
9676 && typ1->vval.v_partial == NULL)
9677 || (typ2->v_type == VAR_PARTIAL
9678 && typ2->vval.v_partial == NULL))
9679 /* when a partial is NULL assume not equal */
9680 n1 = FALSE;
9681 else if (type_is)
9682 {
9683 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9684 /* strings are considered the same if their value is
9685 * the same */
9686 n1 = tv_equal(typ1, typ2, ic, FALSE);
9687 else if (typ1->v_type == VAR_PARTIAL
9688 && typ2->v_type == VAR_PARTIAL)
9689 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9690 else
9691 n1 = FALSE;
9692 }
9693 else
9694 n1 = tv_equal(typ1, typ2, ic, FALSE);
9695 if (type == TYPE_NEQUAL)
9696 n1 = !n1;
9697 }
9698
9699#ifdef FEAT_FLOAT
9700 /*
9701 * If one of the two variables is a float, compare as a float.
9702 * When using "=~" or "!~", always compare as string.
9703 */
9704 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9705 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9706 {
9707 float_T f1, f2;
9708
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009709 f1 = tv_get_float(typ1);
9710 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009711 n1 = FALSE;
9712 switch (type)
9713 {
9714 case TYPE_EQUAL: n1 = (f1 == f2); break;
9715 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9716 case TYPE_GREATER: n1 = (f1 > f2); break;
9717 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9718 case TYPE_SMALLER: n1 = (f1 < f2); break;
9719 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9720 case TYPE_UNKNOWN:
9721 case TYPE_MATCH:
9722 case TYPE_NOMATCH: break; /* avoid gcc warning */
9723 }
9724 }
9725#endif
9726
9727 /*
9728 * If one of the two variables is a number, compare as a number.
9729 * When using "=~" or "!~", always compare as string.
9730 */
9731 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9732 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9733 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009734 n1 = tv_get_number(typ1);
9735 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009736 switch (type)
9737 {
9738 case TYPE_EQUAL: n1 = (n1 == n2); break;
9739 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9740 case TYPE_GREATER: n1 = (n1 > n2); break;
9741 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9742 case TYPE_SMALLER: n1 = (n1 < n2); break;
9743 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9744 case TYPE_UNKNOWN:
9745 case TYPE_MATCH:
9746 case TYPE_NOMATCH: break; /* avoid gcc warning */
9747 }
9748 }
9749 else
9750 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009751 s1 = tv_get_string_buf(typ1, buf1);
9752 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009753 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9754 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9755 else
9756 i = 0;
9757 n1 = FALSE;
9758 switch (type)
9759 {
9760 case TYPE_EQUAL: n1 = (i == 0); break;
9761 case TYPE_NEQUAL: n1 = (i != 0); break;
9762 case TYPE_GREATER: n1 = (i > 0); break;
9763 case TYPE_GEQUAL: n1 = (i >= 0); break;
9764 case TYPE_SMALLER: n1 = (i < 0); break;
9765 case TYPE_SEQUAL: n1 = (i <= 0); break;
9766
9767 case TYPE_MATCH:
9768 case TYPE_NOMATCH:
9769 n1 = pattern_match(s2, s1, ic);
9770 if (type == TYPE_NOMATCH)
9771 n1 = !n1;
9772 break;
9773
9774 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9775 }
9776 }
9777 clear_tv(typ1);
9778 typ1->v_type = VAR_NUMBER;
9779 typ1->vval.v_number = n1;
9780
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009781 return OK;
9782}
9783
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009784 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009785typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009786{
9787 char_u *tofree;
9788 char_u numbuf[NUMBUFLEN];
9789 char_u *ret = NULL;
9790
9791 if (arg == NULL)
9792 return vim_strsave((char_u *)"(does not exist)");
9793 ret = tv2string(arg, &tofree, numbuf, 0);
9794 /* Make a copy if we have a value but it's not in allocated memory. */
9795 if (ret != NULL && tofree == NULL)
9796 ret = vim_strsave(ret);
9797 return ret;
9798}
9799
9800 int
9801var_exists(char_u *var)
9802{
9803 char_u *name;
9804 char_u *tofree;
9805 typval_T tv;
9806 int len = 0;
9807 int n = FALSE;
9808
9809 /* get_name_len() takes care of expanding curly braces */
9810 name = var;
9811 len = get_name_len(&var, &tofree, TRUE, FALSE);
9812 if (len > 0)
9813 {
9814 if (tofree != NULL)
9815 name = tofree;
9816 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9817 if (n)
9818 {
9819 /* handle d.key, l[idx], f(expr) */
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02009820 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009821 if (n)
9822 clear_tv(&tv);
9823 }
9824 }
9825 if (*var != NUL)
9826 n = FALSE;
9827
9828 vim_free(tofree);
9829 return n;
9830}
9831
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832#endif /* FEAT_EVAL */
9833
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836
Bram Moolenaar4f974752019-02-17 17:44:42 +01009837#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838/*
9839 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841
9842/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009843 * Get the short path (8.3) for the filename in "fnamep".
9844 * Only works for a valid file name.
9845 * When the path gets longer "fnamep" is changed and the allocated buffer
9846 * is put in "bufp".
9847 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9848 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 */
9850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009853 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 char_u *newbuf;
9855
9856 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009857 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 if (l > len - 1)
9859 {
9860 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009861 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 newbuf = vim_strnsave(*fnamep, l);
9863 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009864 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
9866 vim_free(*bufp);
9867 *fnamep = *bufp = newbuf;
9868
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009869 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009870 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 }
9872
9873 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009874 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875}
9876
9877/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009878 * Get the short path (8.3) for the filename in "fname". The converted
9879 * path is returned in "bufp".
9880 *
9881 * Some of the directories specified in "fname" may not exist. This function
9882 * will shorten the existing directories at the beginning of the path and then
9883 * append the remaining non-existing path.
9884 *
9885 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009886 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009887 * bufp - Pointer to an allocated buffer for the filename.
9888 * fnamelen - Length of the filename pointed to by fname
9889 *
9890 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 */
9892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893shortpath_for_invalid_fname(
9894 char_u **fname,
9895 char_u **bufp,
9896 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009898 char_u *short_fname, *save_fname, *pbuf_unused;
9899 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009901 int old_len, len;
9902 int new_len, sfx_len;
9903 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
9905 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009906 old_len = *fnamelen;
9907 save_fname = vim_strnsave(*fname, old_len);
9908 pbuf_unused = NULL;
9909 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009911 endp = save_fname + old_len - 1; /* Find the end of the copy */
9912 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009914 /*
9915 * Try shortening the supplied path till it succeeds by removing one
9916 * directory at a time from the tail of the path.
9917 */
9918 len = 0;
9919 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009921 /* go back one path-separator */
9922 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9923 --endp;
9924 if (endp <= save_fname)
9925 break; /* processed the complete path */
9926
9927 /*
9928 * Replace the path separator with a NUL and try to shorten the
9929 * resulting path.
9930 */
9931 ch = *endp;
9932 *endp = 0;
9933 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009934 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009935 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9936 {
9937 retval = FAIL;
9938 goto theend;
9939 }
9940 *endp = ch; /* preserve the string */
9941
9942 if (len > 0)
9943 break; /* successfully shortened the path */
9944
9945 /* failed to shorten the path. Skip the path separator */
9946 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 }
9948
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009949 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009951 /*
9952 * Succeeded in shortening the path. Now concatenate the shortened
9953 * path with the remaining path at the tail.
9954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009956 /* Compute the length of the new path. */
9957 sfx_len = (int)(save_endp - endp) + 1;
9958 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009960 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009962 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009964 /* There is not enough space in the currently allocated string,
9965 * copy it to a buffer big enough. */
9966 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009968 {
9969 retval = FAIL;
9970 goto theend;
9971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
9973 else
9974 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009975 /* Transfer short_fname to the main buffer (it's big enough),
9976 * unless get_short_pathname() did its work in-place. */
9977 *fname = *bufp = save_fname;
9978 if (short_fname != save_fname)
9979 vim_strncpy(save_fname, short_fname, len);
9980 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009982
9983 /* concat the not-shortened part of the path */
9984 vim_strncpy(*fname + len, endp, sfx_len);
9985 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009987
9988theend:
9989 vim_free(pbuf_unused);
9990 vim_free(save_fname);
9991
9992 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993}
9994
9995/*
9996 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009997 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 */
9999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010000shortpath_for_partial(
10001 char_u **fnamep,
10002 char_u **bufp,
10003 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 int sepcount, len, tflen;
10006 char_u *p;
10007 char_u *pbuf, *tfname;
10008 int hasTilde;
10009
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010010 /* Count up the path separators from the RHS.. so we know which part
10011 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010013 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 if (vim_ispathsep(*p))
10015 ++sepcount;
10016
10017 /* Need full path first (use expand_env() to remove a "~/") */
10018 hasTilde = (**fnamep == '~');
10019 if (hasTilde)
10020 pbuf = tfname = expand_env_save(*fnamep);
10021 else
10022 pbuf = tfname = FullName_save(*fnamep, FALSE);
10023
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010024 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010026 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
10029 if (len == 0)
10030 {
10031 /* Don't have a valid filename, so shorten the rest of the
10032 * path if we can. This CAN give us invalid 8.3 filenames, but
10033 * there's not a lot of point in guessing what it might be.
10034 */
10035 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010036 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10037 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 }
10039
10040 /* Count the paths backward to find the beginning of the desired string. */
10041 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010042 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010043 if (has_mbyte)
10044 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 if (vim_ispathsep(*p))
10046 {
10047 if (sepcount == 0 || (hasTilde && sepcount == 1))
10048 break;
10049 else
10050 sepcount --;
10051 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 if (hasTilde)
10054 {
10055 --p;
10056 if (p >= tfname)
10057 *p = '~';
10058 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010059 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061 else
10062 ++p;
10063
10064 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10065 vim_free(*bufp);
10066 *fnamelen = (int)STRLEN(p);
10067 *bufp = pbuf;
10068 *fnamep = p;
10069
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010070 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010072#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
10074/*
10075 * Adjust a filename, according to a string of modifiers.
10076 * *fnamep must be NUL terminated when called. When returning, the length is
10077 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010078 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 * When there is an error, *fnamep is set to NULL.
10080 */
10081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010082modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010083 char_u *src, // string with modifiers
10084 int tilde_file, // "~" is a file name, not $HOME
10085 int *usedlen, // characters after src that are used
10086 char_u **fnamep, // file name so far
10087 char_u **bufp, // buffer for allocated file name or NULL
10088 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089{
10090 int valid = 0;
10091 char_u *tail;
10092 char_u *s, *p, *pbuf;
10093 char_u dirname[MAXPATHL];
10094 int c;
10095 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010096#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010097 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 int has_shortname = 0;
10099#endif
10100
10101repeat:
10102 /* ":p" - full path/file_name */
10103 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10104 {
10105 has_fullname = 1;
10106
10107 valid |= VALID_PATH;
10108 *usedlen += 2;
10109
10110 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10111 if ((*fnamep)[0] == '~'
10112#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10113 && ((*fnamep)[1] == '/'
10114# ifdef BACKSLASH_IN_FILENAME
10115 || (*fnamep)[1] == '\\'
10116# endif
10117 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010119 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 )
10121 {
10122 *fnamep = expand_env_save(*fnamep);
10123 vim_free(*bufp); /* free any allocated file name */
10124 *bufp = *fnamep;
10125 if (*fnamep == NULL)
10126 return -1;
10127 }
10128
10129 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010130 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 {
10132 if (vim_ispathsep(*p)
10133 && p[1] == '.'
10134 && (p[2] == NUL
10135 || vim_ispathsep(p[2])
10136 || (p[2] == '.'
10137 && (p[3] == NUL || vim_ispathsep(p[3])))))
10138 break;
10139 }
10140
10141 /* FullName_save() is slow, don't use it when not needed. */
10142 if (*p != NUL || !vim_isAbsName(*fnamep))
10143 {
10144 *fnamep = FullName_save(*fnamep, *p != NUL);
10145 vim_free(*bufp); /* free any allocated file name */
10146 *bufp = *fnamep;
10147 if (*fnamep == NULL)
10148 return -1;
10149 }
10150
Bram Moolenaar4f974752019-02-17 17:44:42 +010010151#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010152# if _WIN32_WINNT >= 0x0500
10153 if (vim_strchr(*fnamep, '~') != NULL)
10154 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010155 // Expand 8.3 filename to full path. Needed to make sure the same
10156 // file does not have two different names.
10157 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10158 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10159 WCHAR buf[_MAX_PATH];
10160
10161 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010162 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010163 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010164 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010165 char_u *p = utf16_to_enc(buf, NULL);
10166
10167 if (p != NULL)
10168 {
10169 vim_free(*bufp); // free any allocated file name
10170 *bufp = *fnamep = p;
10171 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010172 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010173 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010174 }
10175 }
10176# endif
10177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 /* Append a path separator to a directory. */
10179 if (mch_isdir(*fnamep))
10180 {
10181 /* Make room for one or two extra characters. */
10182 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10183 vim_free(*bufp); /* free any allocated file name */
10184 *bufp = *fnamep;
10185 if (*fnamep == NULL)
10186 return -1;
10187 add_pathsep(*fnamep);
10188 }
10189 }
10190
10191 /* ":." - path relative to the current directory */
10192 /* ":~" - path relative to the home directory */
10193 /* ":8" - shortname path - postponed till after */
10194 while (src[*usedlen] == ':'
10195 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10196 {
10197 *usedlen += 2;
10198 if (c == '8')
10199 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010200#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 has_shortname = 1; /* Postpone this. */
10202#endif
10203 continue;
10204 }
10205 pbuf = NULL;
10206 /* Need full path first (use expand_env() to remove a "~/") */
10207 if (!has_fullname)
10208 {
10209 if (c == '.' && **fnamep == '~')
10210 p = pbuf = expand_env_save(*fnamep);
10211 else
10212 p = pbuf = FullName_save(*fnamep, FALSE);
10213 }
10214 else
10215 p = *fnamep;
10216
10217 has_fullname = 0;
10218
10219 if (p != NULL)
10220 {
10221 if (c == '.')
10222 {
10223 mch_dirname(dirname, MAXPATHL);
10224 s = shorten_fname(p, dirname);
10225 if (s != NULL)
10226 {
10227 *fnamep = s;
10228 if (pbuf != NULL)
10229 {
10230 vim_free(*bufp); /* free any allocated file name */
10231 *bufp = pbuf;
10232 pbuf = NULL;
10233 }
10234 }
10235 }
10236 else
10237 {
10238 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10239 /* Only replace it when it starts with '~' */
10240 if (*dirname == '~')
10241 {
10242 s = vim_strsave(dirname);
10243 if (s != NULL)
10244 {
10245 *fnamep = s;
10246 vim_free(*bufp);
10247 *bufp = s;
10248 }
10249 }
10250 }
10251 vim_free(pbuf);
10252 }
10253 }
10254
10255 tail = gettail(*fnamep);
10256 *fnamelen = (int)STRLEN(*fnamep);
10257
10258 /* ":h" - head, remove "/file_name", can be repeated */
10259 /* Don't remove the first "/" or "c:\" */
10260 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10261 {
10262 valid |= VALID_HEAD;
10263 *usedlen += 2;
10264 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010265 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010266 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 *fnamelen = (int)(tail - *fnamep);
10268#ifdef VMS
10269 if (*fnamelen > 0)
10270 *fnamelen += 1; /* the path separator is part of the path */
10271#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010272 if (*fnamelen == 0)
10273 {
10274 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10275 p = vim_strsave((char_u *)".");
10276 if (p == NULL)
10277 return -1;
10278 vim_free(*bufp);
10279 *bufp = *fnamep = tail = p;
10280 *fnamelen = 1;
10281 }
10282 else
10283 {
10284 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010285 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 }
10288
10289 /* ":8" - shortname */
10290 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10291 {
10292 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010293#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 has_shortname = 1;
10295#endif
10296 }
10297
Bram Moolenaar4f974752019-02-17 17:44:42 +010010298#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010299 /*
10300 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 */
10302 if (has_shortname)
10303 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010304 /* Copy the string if it is shortened by :h and when it wasn't copied
10305 * yet, because we are going to change it in place. Avoids changing
10306 * the buffer name for "%:8". */
10307 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
10309 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010310 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 return -1;
10312 vim_free(*bufp);
10313 *bufp = *fnamep = p;
10314 }
10315
10316 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010317 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 if (!has_fullname && !vim_isAbsName(*fnamep))
10319 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010320 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 return -1;
10322 }
10323 else
10324 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010325 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
Bram Moolenaardc935552011-08-17 15:23:23 +020010327 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010329 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 return -1;
10331
10332 if (l == 0)
10333 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010334 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010336 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 return -1;
10338 }
10339 *fnamelen = l;
10340 }
10341 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010342#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343
10344 /* ":t" - tail, just the basename */
10345 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10346 {
10347 *usedlen += 2;
10348 *fnamelen -= (int)(tail - *fnamep);
10349 *fnamep = tail;
10350 }
10351
10352 /* ":e" - extension, can be repeated */
10353 /* ":r" - root, without extension, can be repeated */
10354 while (src[*usedlen] == ':'
10355 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10356 {
10357 /* find a '.' in the tail:
10358 * - for second :e: before the current fname
10359 * - otherwise: The last '.'
10360 */
10361 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10362 s = *fnamep - 2;
10363 else
10364 s = *fnamep + *fnamelen - 1;
10365 for ( ; s > tail; --s)
10366 if (s[0] == '.')
10367 break;
10368 if (src[*usedlen + 1] == 'e') /* :e */
10369 {
10370 if (s > tail)
10371 {
10372 *fnamelen += (int)(*fnamep - (s + 1));
10373 *fnamep = s + 1;
10374#ifdef VMS
10375 /* cut version from the extension */
10376 s = *fnamep + *fnamelen - 1;
10377 for ( ; s > *fnamep; --s)
10378 if (s[0] == ';')
10379 break;
10380 if (s > *fnamep)
10381 *fnamelen = s - *fnamep;
10382#endif
10383 }
10384 else if (*fnamep <= tail)
10385 *fnamelen = 0;
10386 }
10387 else /* :r */
10388 {
10389 if (s > tail) /* remove one extension */
10390 *fnamelen = (int)(s - *fnamep);
10391 }
10392 *usedlen += 2;
10393 }
10394
10395 /* ":s?pat?foo?" - substitute */
10396 /* ":gs?pat?foo?" - global substitute */
10397 if (src[*usedlen] == ':'
10398 && (src[*usedlen + 1] == 's'
10399 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10400 {
10401 char_u *str;
10402 char_u *pat;
10403 char_u *sub;
10404 int sep;
10405 char_u *flags;
10406 int didit = FALSE;
10407
10408 flags = (char_u *)"";
10409 s = src + *usedlen + 2;
10410 if (src[*usedlen + 1] == 'g')
10411 {
10412 flags = (char_u *)"g";
10413 ++s;
10414 }
10415
10416 sep = *s++;
10417 if (sep)
10418 {
10419 /* find end of pattern */
10420 p = vim_strchr(s, sep);
10421 if (p != NULL)
10422 {
10423 pat = vim_strnsave(s, (int)(p - s));
10424 if (pat != NULL)
10425 {
10426 s = p + 1;
10427 /* find end of substitution */
10428 p = vim_strchr(s, sep);
10429 if (p != NULL)
10430 {
10431 sub = vim_strnsave(s, (int)(p - s));
10432 str = vim_strnsave(*fnamep, *fnamelen);
10433 if (sub != NULL && str != NULL)
10434 {
10435 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010436 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 if (s != NULL)
10438 {
10439 *fnamep = s;
10440 *fnamelen = (int)STRLEN(s);
10441 vim_free(*bufp);
10442 *bufp = s;
10443 didit = TRUE;
10444 }
10445 }
10446 vim_free(sub);
10447 vim_free(str);
10448 }
10449 vim_free(pat);
10450 }
10451 }
10452 /* after using ":s", repeat all the modifiers */
10453 if (didit)
10454 goto repeat;
10455 }
10456 }
10457
Bram Moolenaar26df0922014-02-23 23:39:13 +010010458 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10459 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010460 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010461 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010462 if (c != NUL)
10463 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010464 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010465 if (c != NUL)
10466 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010467 if (p == NULL)
10468 return -1;
10469 vim_free(*bufp);
10470 *bufp = *fnamep = p;
10471 *fnamelen = (int)STRLEN(p);
10472 *usedlen += 2;
10473 }
10474
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 return valid;
10476}
10477
10478/*
10479 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010480 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 * "flags" can be "g" to do a global substitute.
10482 * Returns an allocated string, NULL for error.
10483 */
10484 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010485do_string_sub(
10486 char_u *str,
10487 char_u *pat,
10488 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010489 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010490 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 int sublen;
10493 regmatch_T regmatch;
10494 int i;
10495 int do_all;
10496 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010497 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 garray_T ga;
10499 char_u *ret;
10500 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010501 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502
10503 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10504 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010505 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
10507 ga_init2(&ga, 1, 200);
10508
10509 do_all = (flags[0] == 'g');
10510
10511 regmatch.rm_ic = p_ic;
10512 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10513 if (regmatch.regprog != NULL)
10514 {
10515 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010516 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10518 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010519 /* Skip empty match except for first match. */
10520 if (regmatch.startp[0] == regmatch.endp[0])
10521 {
10522 if (zero_width == regmatch.startp[0])
10523 {
10524 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010525 i = MB_PTR2LEN(tail);
10526 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10527 (size_t)i);
10528 ga.ga_len += i;
10529 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010530 continue;
10531 }
10532 zero_width = regmatch.startp[0];
10533 }
10534
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 /*
10536 * Get some space for a temporary buffer to do the substitution
10537 * into. It will contain:
10538 * - The text up to where the match is.
10539 * - The substituted text.
10540 * - The text after the match.
10541 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010542 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010543 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10545 {
10546 ga_clear(&ga);
10547 break;
10548 }
10549
10550 /* copy the text up to where the match is */
10551 i = (int)(regmatch.startp[0] - tail);
10552 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10553 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010554 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 + ga.ga_len + i, TRUE, TRUE, FALSE);
10556 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010557 tail = regmatch.endp[0];
10558 if (*tail == NUL)
10559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 if (!do_all)
10561 break;
10562 }
10563
10564 if (ga.ga_data != NULL)
10565 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10566
Bram Moolenaar473de612013-06-08 18:19:48 +020010567 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569
10570 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10571 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010572 if (p_cpo == empty_option)
10573 p_cpo = save_cpo;
10574 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010575 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010576 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577
10578 return ret;
10579}
10580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581 static int
10582filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10583{
10584 typval_T rettv;
10585 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010587
10588 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10589 argv[0] = vimvars[VV_KEY].vv_tv;
10590 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010591 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10592 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 if (map)
10594 {
10595 /* map(): replace the list item value */
10596 clear_tv(tv);
10597 rettv.v_lock = 0;
10598 *tv = rettv;
10599 }
10600 else
10601 {
10602 int error = FALSE;
10603
10604 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010605 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 clear_tv(&rettv);
10607 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010608 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 if (error)
10610 goto theend;
10611 }
10612 retval = OK;
10613theend:
10614 clear_tv(&vimvars[VV_VAL].vv_tv);
10615 return retval;
10616}
10617
10618
10619/*
10620 * Implementation of map() and filter().
10621 */
10622 void
10623filter_map(typval_T *argvars, typval_T *rettv, int map)
10624{
10625 typval_T *expr;
10626 listitem_T *li, *nli;
10627 list_T *l = NULL;
10628 dictitem_T *di;
10629 hashtab_T *ht;
10630 hashitem_T *hi;
10631 dict_T *d = NULL;
10632 typval_T save_val;
10633 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010634 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 int rem;
10636 int todo;
10637 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10638 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10639 : N_("filter() argument"));
10640 int save_did_emsg;
10641 int idx = 0;
10642
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010643 if (argvars[0].v_type == VAR_BLOB)
10644 {
10645 if ((b = argvars[0].vval.v_blob) == NULL)
10646 return;
10647 }
10648 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010649 {
10650 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010651 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 return;
10653 }
10654 else if (argvars[0].v_type == VAR_DICT)
10655 {
10656 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010657 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658 return;
10659 }
10660 else
10661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010662 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 return;
10664 }
10665
10666 expr = &argvars[1];
10667 /* On type errors, the preceding call has already displayed an error
10668 * message. Avoid a misleading error message for an empty string that
10669 * was not passed as argument. */
10670 if (expr->v_type != VAR_UNKNOWN)
10671 {
10672 prepare_vimvar(VV_VAL, &save_val);
10673
10674 /* We reset "did_emsg" to be able to detect whether an error
10675 * occurred during evaluation of the expression. */
10676 save_did_emsg = did_emsg;
10677 did_emsg = FALSE;
10678
10679 prepare_vimvar(VV_KEY, &save_key);
10680 if (argvars[0].v_type == VAR_DICT)
10681 {
10682 vimvars[VV_KEY].vv_type = VAR_STRING;
10683
10684 ht = &d->dv_hashtab;
10685 hash_lock(ht);
10686 todo = (int)ht->ht_used;
10687 for (hi = ht->ht_array; todo > 0; ++hi)
10688 {
10689 if (!HASHITEM_EMPTY(hi))
10690 {
10691 int r;
10692
10693 --todo;
10694 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010695 if (map && (var_check_lock(di->di_tv.v_lock,
10696 arg_errmsg, TRUE)
10697 || var_check_ro(di->di_flags,
10698 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010699 break;
10700 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10701 r = filter_map_one(&di->di_tv, expr, map, &rem);
10702 clear_tv(&vimvars[VV_KEY].vv_tv);
10703 if (r == FAIL || did_emsg)
10704 break;
10705 if (!map && rem)
10706 {
10707 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10708 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10709 break;
10710 dictitem_remove(d, di);
10711 }
10712 }
10713 }
10714 hash_unlock(ht);
10715 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010716 else if (argvars[0].v_type == VAR_BLOB)
10717 {
10718 int i;
10719 typval_T tv;
10720
10721 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10722 for (i = 0; i < b->bv_ga.ga_len; i++)
10723 {
10724 tv.v_type = VAR_NUMBER;
10725 tv.vval.v_number = blob_get(b, i);
10726 vimvars[VV_KEY].vv_nr = idx;
10727 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10728 break;
10729 if (tv.v_type != VAR_NUMBER)
10730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010731 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010732 return;
10733 }
10734 tv.v_type = VAR_NUMBER;
10735 blob_set(b, i, tv.vval.v_number);
10736 if (!map && rem)
10737 {
10738 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10739
10740 mch_memmove(p + idx, p + i + 1,
10741 (size_t)b->bv_ga.ga_len - i - 1);
10742 --b->bv_ga.ga_len;
10743 --i;
10744 }
10745 }
10746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747 else
10748 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010749 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10751
10752 for (li = l->lv_first; li != NULL; li = nli)
10753 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010754 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755 break;
10756 nli = li->li_next;
10757 vimvars[VV_KEY].vv_nr = idx;
10758 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10759 || did_emsg)
10760 break;
10761 if (!map && rem)
10762 listitem_remove(l, li);
10763 ++idx;
10764 }
10765 }
10766
10767 restore_vimvar(VV_KEY, &save_key);
10768 restore_vimvar(VV_VAL, &save_val);
10769
10770 did_emsg |= save_did_emsg;
10771 }
10772
10773 copy_tv(&argvars[0], rettv);
10774}
10775
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */