blob: b30f40490e34f24346340bc99ba46ddd860ebebd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +020035static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000036
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010037#define NAMESPACE_CHAR (char_u *)"abglstvw"
38
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020039static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000042 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them.
44 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000045static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000046
47/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000048 * When recursively copying lists and dicts we need to remember which ones we
49 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000050 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000051 */
52static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020053
Bram Moolenaard9fba312005-06-26 22:34:35 +000054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000055 * Array to hold the hashtab with variables local to each sourced script.
56 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000058typedef struct
59{
60 dictitem_T sv_var;
61 dict_T sv_dict;
62} scriptvar_T;
63
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020064static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
65#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
66#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68static int echo_attr = 0; /* attributes used for ":echo" */
69
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000071static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000074 * Info used by a ":for" loop.
75 */
Bram Moolenaar33570922005-01-25 22:26:29 +000076typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000077{
78 int fi_semicolon; /* TRUE if ending in '; var]' */
79 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000080 listwatch_T fi_lw; /* keep an eye on the item used. */
81 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 int fi_bi; /* index of blob */
83 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000084} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085
Bram Moolenaara7043832005-01-21 11:56:39 +000086
87/*
Bram Moolenaar33570922005-01-25 22:26:29 +000088 * Array to hold the value of v: variables.
89 * The value is in a dictitem, so that it can also be used in the v: scope.
90 * The reason to use this table anyway is for very quick access to the
91 * variables with the VV_ defines.
92 */
Bram Moolenaar33570922005-01-25 22:26:29 +000093
94/* values for vv_flags: */
95#define VV_COMPAT 1 /* compatible, also used without "v:" */
96#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000097#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000098
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010099#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000100
101static struct vimvar
102{
103 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100104 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000105 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
106} vimvars[VV_LEN] =
107{
108 /*
109 * The order here must match the VV_ defines in vim.h!
110 * Initializing a union does not work, leave tv.vval empty to get zero's.
111 */
112 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
113 {VV_NAME("count1", VAR_NUMBER), VV_RO},
114 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
115 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
116 {VV_NAME("warningmsg", VAR_STRING), 0},
117 {VV_NAME("statusmsg", VAR_STRING), 0},
118 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
119 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
120 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
121 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
122 {VV_NAME("termresponse", VAR_STRING), VV_RO},
123 {VV_NAME("fname", VAR_STRING), VV_RO},
124 {VV_NAME("lang", VAR_STRING), VV_RO},
125 {VV_NAME("lc_time", VAR_STRING), VV_RO},
126 {VV_NAME("ctype", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
128 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
129 {VV_NAME("fname_in", VAR_STRING), VV_RO},
130 {VV_NAME("fname_out", VAR_STRING), VV_RO},
131 {VV_NAME("fname_new", VAR_STRING), VV_RO},
132 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
133 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
134 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
136 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
137 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
138 {VV_NAME("progname", VAR_STRING), VV_RO},
139 {VV_NAME("servername", VAR_STRING), VV_RO},
140 {VV_NAME("dying", VAR_NUMBER), VV_RO},
141 {VV_NAME("exception", VAR_STRING), VV_RO},
142 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
143 {VV_NAME("register", VAR_STRING), VV_RO},
144 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
145 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000146 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
147 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000148 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000149 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
150 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200153 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000154 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
156 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000157 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000158 {VV_NAME("swapname", VAR_STRING), VV_RO},
159 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000160 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200161 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000162 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200163 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000164 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
165 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000166 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100168 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000169 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200171 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200172 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200173 {VV_NAME("option_new", VAR_STRING), VV_RO},
174 {VV_NAME("option_old", VAR_STRING), VV_RO},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200175 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
176 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
177 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200178 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100179 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100180 {VV_NAME("false", VAR_SPECIAL), VV_RO},
181 {VV_NAME("true", VAR_SPECIAL), VV_RO},
182 {VV_NAME("null", VAR_SPECIAL), VV_RO},
183 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100184 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200185 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200186 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
195 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100196 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200197 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
198 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200199 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200200 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
201 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
202 {VV_NAME("event", VAR_DICT), VV_RO},
203 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000204};
205
206/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000207#define vv_type vv_di.di_tv.v_type
208#define vv_nr vv_di.di_tv.vval.v_number
209#define vv_float vv_di.di_tv.vval.v_float
210#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000211#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200212#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100213#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000214#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000215
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200216static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define vimvarht vimvardict.dv_hashtab
218
Bram Moolenaar9937a052019-06-15 15:45:06 +0200219static void ex_let_const(exarg_T *eap, int is_const);
220static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
222static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void list_glob_vars(int *first);
224static void list_buf_vars(int *first);
225static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void list_vim_vars(int *first);
228static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200230static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
231static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
234static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
235static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
236static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int eval2(char_u **arg, typval_T *rettv, int evaluate);
239static int eval3(char_u **arg, typval_T *rettv, int evaluate);
240static int eval4(char_u **arg, typval_T *rettv, int evaluate);
241static int eval5(char_u **arg, typval_T *rettv, int evaluate);
242static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
243static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +0200244static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000245
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
247static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200252static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100253static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100254static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100255static void list_one_var(dictitem_T *v, char *prefix, int *first);
256static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200257static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100258static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100259static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200260
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261/* for VIM_VERSION_ defines */
262#include "version.h"
263
Bram Moolenaare21c1582019-03-02 11:57:09 +0100264/*
265 * Return "n1" divided by "n2", taking care of dividing by zero.
266 */
267 static varnumber_T
268num_divide(varnumber_T n1, varnumber_T n2)
269{
270 varnumber_T result;
271
272 if (n2 == 0) // give an error message?
273 {
274 if (n1 == 0)
275 result = VARNUM_MIN; // similar to NaN
276 else if (n1 < 0)
277 result = -VARNUM_MAX;
278 else
279 result = VARNUM_MAX;
280 }
281 else
282 result = n1 / n2;
283
284 return result;
285}
286
287/*
288 * Return "n1" modulus "n2", taking care of dividing by zero.
289 */
290 static varnumber_T
291num_modulus(varnumber_T n1, varnumber_T n2)
292{
293 // Give an error when n2 is 0?
294 return (n2 == 0) ? 0 : (n1 % n2);
295}
296
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100297
298#if defined(EBCDIC) || defined(PROTO)
299/*
300 * Compare struct fst by function name.
301 */
302 static int
303compare_func_name(const void *s1, const void *s2)
304{
305 struct fst *p1 = (struct fst *)s1;
306 struct fst *p2 = (struct fst *)s2;
307
308 return STRCMP(p1->f_name, p2->f_name);
309}
310
311/*
312 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100313 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100314 * On machines using EBCDIC we have to sort it.
315 */
316 static void
317sortFunctions(void)
318{
319 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
320
321 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
322}
323#endif
324
325
Bram Moolenaar33570922005-01-25 22:26:29 +0000326/*
327 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000328 */
329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000331{
Bram Moolenaar33570922005-01-25 22:26:29 +0000332 int i;
333 struct vimvar *p;
334
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200335 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
336 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200337 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000338 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200339 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000340
341 for (i = 0; i < VV_LEN; ++i)
342 {
343 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200344 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100346 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200347 getout(1);
348 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000349 STRCPY(p->vv_di.di_key, p->vv_name);
350 if (p->vv_flags & VV_RO)
351 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
352 else if (p->vv_flags & VV_RO_SBX)
353 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
354 else
355 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000356
357 /* add to v: scope dict, unless the value is not always available */
358 if (p->vv_type != VAR_UNKNOWN)
359 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000360 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000361 /* add to compat scope dict */
362 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000363 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100364 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200365 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100366
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100368 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100369 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100370 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100371 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100372
373 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
374 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
375 set_vim_var_nr(VV_NONE, VVAL_NONE);
376 set_vim_var_nr(VV_NULL, VVAL_NULL);
377
Bram Moolenaarf562e722016-07-19 17:25:25 +0200378 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
379 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
380 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
381 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
382 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
383 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
384 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
385 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
386 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
387 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100388 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200389
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200390 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200391
392#ifdef EBCDIC
393 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100394 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200395 */
396 sortFunctions();
397#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000398}
399
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400#if defined(EXITFREE) || defined(PROTO)
401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100402eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000403{
404 int i;
405 struct vimvar *p;
406
407 for (i = 0; i < VV_LEN; ++i)
408 {
409 p = &vimvars[i];
410 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100411 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000412 else if (p->vv_di.di_tv.v_type == VAR_LIST)
413 {
414 list_unref(p->vv_list);
415 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000416 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000417 }
418 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000419 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000420 hash_clear(&compat_hashtab);
421
Bram Moolenaard9fba312005-06-26 22:34:35 +0000422 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100423# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200424 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100425# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000426
427 /* global variables */
428 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000429
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000430 /* autoloaded script names */
431 ga_clear_strings(&ga_loaded);
432
Bram Moolenaarcca74132013-09-25 21:00:28 +0200433 /* Script-local variables. First clear all the variables and in a second
434 * loop free the scriptvar_T, because a variable in one script might hold
435 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200436 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200437 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200438 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200439 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200440 ga_clear(&ga_scripts);
441
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200442 // unreferenced lists and dicts
443 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200444
445 // functions not garbage collected
446 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000447}
448#endif
449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * Set an internal variable to a string value. Creates the variable if it does
453 * not already exist.
454 */
455 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100456set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000458 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000459 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461 val = vim_strsave(value);
462 if (val != NULL)
463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000464 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000465 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000467 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000468 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 }
470 }
471}
472
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200474#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000475static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000476static char_u *redir_endp = NULL;
477static char_u *redir_varname = NULL;
478
479/*
480 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100481 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000482 * Returns OK if successfully completed the setup. FAIL otherwise.
483 */
484 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100485var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000486{
487 int save_emsg;
488 int err;
489 typval_T tv;
490
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000491 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000492 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100494 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000495 return FAIL;
496 }
497
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000498 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000499 redir_varname = vim_strsave(name);
500 if (redir_varname == NULL)
501 return FAIL;
502
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200503 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000504 if (redir_lval == NULL)
505 {
506 var_redir_stop();
507 return FAIL;
508 }
509
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000510 /* The output is stored in growarray "redir_ga" until redirection ends. */
511 ga_init2(&redir_ga, (int)sizeof(char), 500);
512
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100514 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000515 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
517 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200518 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000519 if (redir_endp != NULL && *redir_endp != NUL)
520 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100521 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000522 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100523 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000524 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525 var_redir_stop();
526 return FAIL;
527 }
528
529 /* check if we can write to the variable: set it to or append an empty
530 * string */
531 save_emsg = did_emsg;
532 did_emsg = FALSE;
533 tv.v_type = VAR_STRING;
534 tv.vval.v_string = (char_u *)"";
535 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200536 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000537 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200538 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200539 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000541 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542 if (err)
543 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000544 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000545 var_redir_stop();
546 return FAIL;
547 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000548
549 return OK;
550}
551
552/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000553 * Append "value[value_len]" to the variable set by var_redir_start().
554 * The actual appending is postponed until redirection ends, because the value
555 * appended may in fact be the string we write to, changing it may cause freed
556 * memory to be used:
557 * :redir => foo
558 * :let foo
559 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000560 */
561 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100562var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000564 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000565
566 if (redir_lval == NULL)
567 return;
568
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000570 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000572 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000573
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000574 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000575 {
576 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000577 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000578 }
579 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000581}
582
583/*
584 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000585 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000586 */
587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100588var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000589{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000590 typval_T tv;
591
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200592 if (EVALCMD_BUSY)
593 {
594 redir_lval = NULL;
595 return;
596 }
597
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000598 if (redir_lval != NULL)
599 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000600 /* If there was no error: assign the text to the variable. */
601 if (redir_endp != NULL)
602 {
603 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
604 tv.v_type = VAR_STRING;
605 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200606 /* Call get_lval() again, if it's inside a Dict or List it may
607 * have changed. */
608 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100609 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200610 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200611 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
612 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200613 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000614 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000615
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000616 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000618
Bram Moolenaard23a8232018-02-10 18:45:26 +0100619 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100621 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000622}
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100625eval_charconvert(
626 char_u *enc_from,
627 char_u *enc_to,
628 char_u *fname_from,
629 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 int err = FALSE;
632
633 set_vim_var_string(VV_CC_FROM, enc_from, -1);
634 set_vim_var_string(VV_CC_TO, enc_to, -1);
635 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
636 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
637 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
638 err = TRUE;
639 set_vim_var_string(VV_CC_FROM, NULL, -1);
640 set_vim_var_string(VV_CC_TO, NULL, -1);
641 set_vim_var_string(VV_FNAME_IN, NULL, -1);
642 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
643
644 if (err)
645 return FAIL;
646 return OK;
647}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
650 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100651eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 int err = FALSE;
654
655 set_vim_var_string(VV_FNAME_IN, fname, -1);
656 set_vim_var_string(VV_CMDARG, args, -1);
657 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
658 err = TRUE;
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_CMDARG, NULL, -1);
661
662 if (err)
663 {
664 mch_remove(fname);
665 return FAIL;
666 }
667 return OK;
668}
669# endif
670
671# if defined(FEAT_DIFF) || defined(PROTO)
672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100673eval_diff(
674 char_u *origfile,
675 char_u *newfile,
676 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 int err = FALSE;
679
680 set_vim_var_string(VV_FNAME_IN, origfile, -1);
681 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
682 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
683 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
684 set_vim_var_string(VV_FNAME_IN, NULL, -1);
685 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
686 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
687}
688
689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100690eval_patch(
691 char_u *origfile,
692 char_u *difffile,
693 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
695 int err;
696
697 set_vim_var_string(VV_FNAME_IN, origfile, -1);
698 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
699 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
700 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
701 set_vim_var_string(VV_FNAME_IN, NULL, -1);
702 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
703 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
704}
705# endif
706
707/*
708 * Top level evaluation function, returning a boolean.
709 * Sets "error" to TRUE if there was an error.
710 * Return TRUE or FALSE.
711 */
712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100713eval_to_bool(
714 char_u *arg,
715 int *error,
716 char_u **nextcmd,
717 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
Bram Moolenaar33570922005-01-25 22:26:29 +0000719 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200720 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
722 if (skip)
723 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000724 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 else
727 {
728 *error = FALSE;
729 if (!skip)
730 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100731 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 }
735 if (skip)
736 --emsg_skip;
737
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200738 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739}
740
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100741/*
742 * Call eval1() and give an error message if not done at a lower level.
743 */
744 static int
745eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
746{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100747 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100748 int ret;
749 int did_emsg_before = did_emsg;
750 int called_emsg_before = called_emsg;
751
752 ret = eval1(arg, rettv, evaluate);
753 if (ret == FAIL)
754 {
755 // Report the invalid expression unless the expression evaluation has
756 // been cancelled due to an aborting error, an interrupt, or an
757 // exception, or we already gave a more specific error.
758 // Also check called_emsg for when using assert_fails().
759 if (!aborting() && did_emsg == did_emsg_before
760 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100761 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100762 }
763 return ret;
764}
765
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200766 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100767eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
768{
769 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100770 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200771 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100772
773 if (expr->v_type == VAR_FUNC)
774 {
775 s = expr->vval.v_string;
776 if (s == NULL || *s == NUL)
777 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200778 vim_memset(&funcexe, 0, sizeof(funcexe));
779 funcexe.evaluate = TRUE;
780 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100781 return FAIL;
782 }
783 else if (expr->v_type == VAR_PARTIAL)
784 {
785 partial_T *partial = expr->vval.v_partial;
786
787 s = partial_name(partial);
788 if (s == NULL || *s == NUL)
789 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200790 vim_memset(&funcexe, 0, sizeof(funcexe));
791 funcexe.evaluate = TRUE;
792 funcexe.partial = partial;
793 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100794 return FAIL;
795 }
796 else
797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100798 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100799 if (s == NULL)
800 return FAIL;
801 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100802 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100803 return FAIL;
804 if (*s != NUL) /* check for trailing chars after expr */
805 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200806 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100808 return FAIL;
809 }
810 }
811 return OK;
812}
813
814/*
815 * Like eval_to_bool() but using a typval_T instead of a string.
816 * Works for string, funcref and partial.
817 */
818 int
819eval_expr_to_bool(typval_T *expr, int *error)
820{
821 typval_T rettv;
822 int res;
823
824 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
825 {
826 *error = TRUE;
827 return FALSE;
828 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100829 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100830 clear_tv(&rettv);
831 return res;
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834/*
835 * Top level evaluation function, returning a string. If "skip" is TRUE,
836 * only parsing to "nextcmd" is done, without reporting errors. Return
837 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
838 */
839 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100840eval_to_string_skip(
841 char_u *arg,
842 char_u **nextcmd,
843 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaar33570922005-01-25 22:26:29 +0000845 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 char_u *retval;
847
848 if (skip)
849 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 retval = NULL;
852 else
853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100854 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 if (skip)
858 --emsg_skip;
859
860 return retval;
861}
862
863/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000864 * Skip over an expression at "*pp".
865 * Return FAIL for an error, OK otherwise.
866 */
867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100868skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000869{
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000871
872 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000874}
875
876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000878 * When "convert" is TRUE convert a List into a sequence of lines and convert
879 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * Return pointer to allocated memory, or NULL for failure.
881 */
882 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100883eval_to_string(
884 char_u *arg,
885 char_u **nextcmd,
886 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000890 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000891#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000892 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 retval = NULL;
897 else
898 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000899 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000900 {
901 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000902 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200903 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200904 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200905 if (tv.vval.v_list->lv_len > 0)
906 ga_append(&ga, NL);
907 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000908 ga_append(&ga, NUL);
909 retval = (char_u *)ga.ga_data;
910 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000911#ifdef FEAT_FLOAT
912 else if (convert && tv.v_type == VAR_FLOAT)
913 {
914 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
915 retval = vim_strsave(numbuf);
916 }
917#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000918 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100919 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000920 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922
923 return retval;
924}
925
926/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000927 * Call eval_to_string() without using current local variables and using
928 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 */
930 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100931eval_to_string_safe(
932 char_u *arg,
933 char_u **nextcmd,
934 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200937 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200939 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000940 if (use_sandbox)
941 ++sandbox;
942 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000943 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000944 if (use_sandbox)
945 --sandbox;
946 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200947 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 return retval;
949}
950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951/*
952 * Top level evaluation function, returning a number.
953 * Evaluates "expr" silently.
954 * Returns -1 for an error.
955 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200956 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100957eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200960 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000961 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
963 ++emsg_off;
964
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 retval = -1;
967 else
968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100969 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 --emsg_off;
973
974 return retval;
975}
976
Bram Moolenaara40058a2005-07-11 22:42:07 +0000977/*
978 * Prepare v: variable "idx" to be used.
979 * Save the current typeval in "save_tv".
980 * When not used yet add the variable to the v: hashtable.
981 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000984{
985 *save_tv = vimvars[idx].vv_tv;
986 if (vimvars[idx].vv_type == VAR_UNKNOWN)
987 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
988}
989
990/*
991 * Restore v: variable "idx" to typeval "save_tv".
992 * When no longer defined, remove the variable from the v: hashtable.
993 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100995restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000996{
997 hashitem_T *hi;
998
Bram Moolenaara40058a2005-07-11 22:42:07 +0000999 vimvars[idx].vv_tv = *save_tv;
1000 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1001 {
1002 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1003 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001004 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001005 else
1006 hash_remove(&vimvarht, hi);
1007 }
1008}
1009
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001010#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001011/*
1012 * Evaluate an expression to a list with suggestions.
1013 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001014 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001015 */
1016 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001017eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001018{
1019 typval_T save_val;
1020 typval_T rettv;
1021 list_T *list = NULL;
1022 char_u *p = skipwhite(expr);
1023
1024 /* Set "v:val" to the bad word. */
1025 prepare_vimvar(VV_VAL, &save_val);
1026 vimvars[VV_VAL].vv_type = VAR_STRING;
1027 vimvars[VV_VAL].vv_str = badword;
1028 if (p_verbose == 0)
1029 ++emsg_off;
1030
1031 if (eval1(&p, &rettv, TRUE) == OK)
1032 {
1033 if (rettv.v_type != VAR_LIST)
1034 clear_tv(&rettv);
1035 else
1036 list = rettv.vval.v_list;
1037 }
1038
1039 if (p_verbose == 0)
1040 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001041 restore_vimvar(VV_VAL, &save_val);
1042
1043 return list;
1044}
1045
1046/*
1047 * "list" is supposed to contain two items: a word and a number. Return the
1048 * word in "pp" and the number as the return value.
1049 * Return -1 if anything isn't right.
1050 * Used to get the good word and score from the eval_spell_expr() result.
1051 */
1052 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001053get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001054{
1055 listitem_T *li;
1056
1057 li = list->lv_first;
1058 if (li == NULL)
1059 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001060 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001061
1062 li = li->li_next;
1063 if (li == NULL)
1064 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001065 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001066}
1067#endif
1068
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001070 * Top level evaluation function.
1071 * Returns an allocated typval_T with the result.
1072 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001073 */
1074 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001075eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001076{
1077 typval_T *tv;
1078
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001079 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001080 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001081 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001082
1083 return tv;
1084}
1085
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001088 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001089 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1090 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001091 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001094call_vim_function(
1095 char_u *func,
1096 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001097 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001098 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001100 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001101 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001103 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001104 vim_memset(&funcexe, 0, sizeof(funcexe));
1105 funcexe.firstline = curwin->w_cursor.lnum;
1106 funcexe.lastline = curwin->w_cursor.lnum;
1107 funcexe.evaluate = TRUE;
1108 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001109 if (ret == FAIL)
1110 clear_tv(rettv);
1111
1112 return ret;
1113}
1114
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001115/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001116 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001117 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001118 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1119 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001120 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001121 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001122call_func_retnr(
1123 char_u *func,
1124 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001125 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001126{
1127 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001128 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001129
Bram Moolenaarded27a12018-08-01 19:06:03 +02001130 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001131 return -1;
1132
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001133 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001134 clear_tv(&rettv);
1135 return retval;
1136}
1137
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001138#if defined(FEAT_CMDL_COMPL) \
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001139 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1140
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001141# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001142/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001143 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001144 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001145 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1146 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147 */
1148 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001149call_func_retstr(
1150 char_u *func,
1151 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001152 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001153{
1154 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001155 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156
Bram Moolenaarded27a12018-08-01 19:06:03 +02001157 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158 return NULL;
1159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001160 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001161 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 return retval;
1163}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001164# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001165
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001166/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001167 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001168 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1169 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001170 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001171 */
1172 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001173call_func_retlist(
1174 char_u *func,
1175 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001176 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001177{
1178 typval_T rettv;
1179
Bram Moolenaarded27a12018-08-01 19:06:03 +02001180 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001181 return NULL;
1182
1183 if (rettv.v_type != VAR_LIST)
1184 {
1185 clear_tv(&rettv);
1186 return NULL;
1187 }
1188
1189 return rettv.vval.v_list;
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#endif
1192
Bram Moolenaar05159a02005-02-26 23:04:13 +00001193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef FEAT_FOLDING
1195/*
1196 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1197 * it in "*cp". Doesn't give error messages.
1198 */
1199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001200eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
Bram Moolenaar33570922005-01-25 22:26:29 +00001202 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001203 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001205 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1206 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
1208 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001209 if (use_sandbox)
1210 ++sandbox;
1211 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 retval = 0;
1215 else
1216 {
1217 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001218 if (tv.v_type == VAR_NUMBER)
1219 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001220 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 retval = 0;
1222 else
1223 {
1224 /* If the result is a string, check if there is a non-digit before
1225 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (!VIM_ISDIGIT(*s) && *s != '-')
1228 *cp = *s++;
1229 retval = atol((char *)s);
1230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001234 if (use_sandbox)
1235 --sandbox;
1236 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001238 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239}
1240#endif
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001243 * Get a list of lines from a HERE document. The here document is a list of
1244 * lines surrounded by a marker.
1245 * cmd << {marker}
1246 * {line1}
1247 * {line2}
1248 * ....
1249 * {marker}
1250 *
1251 * The {marker} is a string. If the optional 'trim' word is supplied before the
1252 * marker, then the leading indentation before the lines (matching the
1253 * indentation in the 'cmd' line) is stripped.
1254 * Returns a List with {lines} or NULL.
1255 */
1256 static list_T *
1257heredoc_get(exarg_T *eap, char_u *cmd)
1258{
1259 char_u *theline;
1260 char_u *marker;
1261 list_T *l;
1262 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001263 int marker_indent_len = 0;
1264 int text_indent_len = 0;
1265 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001266
1267 if (eap->getline == NULL)
1268 {
1269 emsg(_("E991: cannot use =<< here"));
1270 return NULL;
1271 }
1272
1273 // Check for the optional 'trim' word before the marker
1274 cmd = skipwhite(cmd);
1275 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1276 {
1277 cmd = skipwhite(cmd + 4);
1278
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001279 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001280 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001281 // the first line after the :let command line. To find the end marker
1282 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001283 p = *eap->cmdlinep;
1284 while (VIM_ISWHITE(*p))
1285 {
1286 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001287 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001288 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001289 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001290 }
1291
Bram Moolenaar24582002019-07-21 14:14:26 +02001292 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001293 if (*cmd != NUL && *cmd != '"')
1294 {
1295 marker = skipwhite(cmd);
1296 p = skiptowhite(marker);
1297 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1298 {
1299 emsg(_(e_trailing));
1300 return NULL;
1301 }
1302 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001303 if (vim_islower(*marker))
1304 {
1305 emsg(_("E221: Marker cannot start with lower case letter"));
1306 return NULL;
1307 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001308 }
1309 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001310 {
1311 emsg(_("E172: Missing marker"));
1312 return NULL;
1313 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001314
1315 l = list_alloc();
1316 if (l == NULL)
1317 return NULL;
1318
1319 for (;;)
1320 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001321 int mi = 0;
1322 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001323
Bram Moolenaare96a2492019-06-25 04:12:16 +02001324 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001325 if (theline == NULL)
1326 {
1327 semsg(_("E990: Missing end marker '%s'"), marker);
1328 break;
1329 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001330
1331 // with "trim": skip the indent matching the :let line to find the
1332 // marker
1333 if (marker_indent_len > 0
1334 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1335 mi = marker_indent_len;
1336 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001337 {
1338 vim_free(theline);
1339 break;
1340 }
1341
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001342 if (text_indent_len == -1 && *theline != NUL)
1343 {
1344 // set the text indent from the first line.
1345 p = theline;
1346 text_indent_len = 0;
1347 while (VIM_ISWHITE(*p))
1348 {
1349 p++;
1350 text_indent_len++;
1351 }
1352 text_indent = vim_strnsave(theline, text_indent_len);
1353 }
1354 // with "trim": skip the indent matching the first line
1355 if (text_indent != NULL)
1356 for (ti = 0; ti < text_indent_len; ++ti)
1357 if (theline[ti] != text_indent[ti])
1358 break;
1359
1360 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001361 break;
1362 vim_free(theline);
1363 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001364 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001365
1366 return l;
1367}
1368
1369/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 * ":let" list all variable values
1371 * ":let var1 var2" list variable values
1372 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 * ":let var += expr" assignment command.
1374 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001375 * ":let var *= expr" assignment command.
1376 * ":let var /= expr" assignment command.
1377 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001378 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001379 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001383ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001385 ex_let_const(eap, FALSE);
1386}
1387
1388/*
1389 * ":const" list all variable values
1390 * ":const var1 var2" list variable values
1391 * ":const var = expr" assignment command.
1392 * ":const [var1, var2] = expr" unpack list.
1393 */
1394 void
1395ex_const(exarg_T *eap)
1396{
1397 ex_let_const(eap, TRUE);
1398}
1399
1400 static void
1401ex_let_const(exarg_T *eap, int is_const)
1402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001405 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 int var_count = 0;
1408 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001409 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001410 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001411 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001412 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaardb552d602006-03-23 22:59:57 +00001414 argend = skip_var_list(arg, &var_count, &semicolon);
1415 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001417 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001418 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001419 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001420 concat = expr[0] == '.'
1421 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1422 || (expr[1] == '.' && expr[2] == '='));
1423 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1424 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001426 /*
1427 * ":let" without "=": list variables
1428 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001430 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001431 else if (expr[0] == '.')
1432 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001435 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001439 list_glob_vars(&first);
1440 list_buf_vars(&first);
1441 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001442 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001443 list_script_vars(&first);
1444 list_func_vars(&first);
1445 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 eap->nextcmd = check_nextcmd(arg);
1448 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001449 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1450 {
1451 list_T *l;
1452
1453 // HERE document
1454 l = heredoc_get(eap, expr + 3);
1455 if (l != NULL)
1456 {
1457 rettv_list_set(&rettv, l);
1458 op[0] = '=';
1459 op[1] = NUL;
1460 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001461 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001462 clear_tv(&rettv);
1463 }
1464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 else
1466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 op[0] = '=';
1468 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001469 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001470 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001471 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001472 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001473 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001474 if (expr[0] == '.' && expr[1] == '.') // ..=
1475 ++expr;
1476 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001477 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001479 else
1480 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (eap->skip)
1483 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (eap->skip)
1486 {
1487 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 --emsg_skip;
1490 }
1491 else if (i != FAIL)
1492 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001493 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001494 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
1497 }
1498}
1499
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500/*
1501 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1502 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001503 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1505 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 * Returns OK or FAIL;
1507 */
1508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509ex_let_vars(
1510 char_u *arg_start,
1511 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001512 int copy, // copy values from "tv", don't move
1513 int semicolon, // from skip_var_list()
1514 int var_count, // from skip_var_list()
1515 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001516 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517{
1518 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001519 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001521 listitem_T *item;
1522 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001523
1524 if (*arg != '[')
1525 {
1526 /*
1527 * ":let var = expr" or ":for var in list"
1528 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001529 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530 return FAIL;
1531 return OK;
1532 }
1533
1534 /*
1535 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1536 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001537 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001539 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001540 return FAIL;
1541 }
1542
1543 i = list_len(l);
1544 if (semicolon == 0 && var_count < i)
1545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001546 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 return FAIL;
1548 }
1549 if (var_count - semicolon > i)
1550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001552 return FAIL;
1553 }
1554
1555 item = l->lv_first;
1556 while (*arg != ']')
1557 {
1558 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001559 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001560 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 item = item->li_next;
1562 if (arg == NULL)
1563 return FAIL;
1564
1565 arg = skipwhite(arg);
1566 if (*arg == ';')
1567 {
1568 /* Put the rest of the list (may be empty) in the var after ';'.
1569 * Create a new list for this. */
1570 l = list_alloc();
1571 if (l == NULL)
1572 return FAIL;
1573 while (item != NULL)
1574 {
1575 list_append_tv(l, &item->li_tv);
1576 item = item->li_next;
1577 }
1578
1579 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001580 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001581 ltv.vval.v_list = l;
1582 l->lv_refcount = 1;
1583
Bram Moolenaar9937a052019-06-15 15:45:06 +02001584 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001585 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001586 clear_tv(&ltv);
1587 if (arg == NULL)
1588 return FAIL;
1589 break;
1590 }
1591 else if (*arg != ',' && *arg != ']')
1592 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001593 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 return FAIL;
1595 }
1596 }
1597
1598 return OK;
1599}
1600
1601/*
1602 * Skip over assignable variable "var" or list of variables "[var, var]".
1603 * Used for ":let varvar = expr" and ":for varvar in expr".
1604 * For "[var, var]" increment "*var_count" for each variable.
1605 * for "[var, var; var]" set "semicolon".
1606 * Return NULL for an error.
1607 */
1608 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609skip_var_list(
1610 char_u *arg,
1611 int *var_count,
1612 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613{
1614 char_u *p, *s;
1615
1616 if (*arg == '[')
1617 {
1618 /* "[var, var]": find the matching ']'. */
1619 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 {
1622 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1623 s = skip_var_one(p);
1624 if (s == p)
1625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001626 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 return NULL;
1628 }
1629 ++*var_count;
1630
1631 p = skipwhite(s);
1632 if (*p == ']')
1633 break;
1634 else if (*p == ';')
1635 {
1636 if (*semicolon == 1)
1637 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001638 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 return NULL;
1640 }
1641 *semicolon = 1;
1642 }
1643 else if (*p != ',')
1644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001645 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 return NULL;
1647 }
1648 }
1649 return p + 1;
1650 }
1651 else
1652 return skip_var_one(arg);
1653}
1654
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001656 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001657 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001658 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001662 if (*arg == '@' && arg[1] != NUL)
1663 return arg + 2;
1664 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1665 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666}
1667
Bram Moolenaara7043832005-01-21 11:56:39 +00001668/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001669 * List variables for hashtab "ht" with prefix "prefix".
1670 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001671 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001673list_hashtable_vars(
1674 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001675 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001676 int empty,
1677 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001678{
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 hashitem_T *hi;
1680 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001681 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001682 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001683
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001684 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001685 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1686 {
1687 if (!HASHITEM_EMPTY(hi))
1688 {
1689 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001690 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001691
1692 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001693 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1694 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001695 if (message_filtered(buf))
1696 continue;
1697
Bram Moolenaar33570922005-01-25 22:26:29 +00001698 if (empty || di->di_tv.v_type != VAR_STRING
1699 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001700 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001701 }
1702 }
1703}
1704
1705/*
1706 * List global variables.
1707 */
1708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001709list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001710{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001711 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001712}
1713
1714/*
1715 * List buffer variables.
1716 */
1717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001719{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001720 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001721}
1722
1723/*
1724 * List window variables.
1725 */
1726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001728{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001729 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001730}
1731
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001732/*
1733 * List tab page variables.
1734 */
1735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001736list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001737{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001738 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001739}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001740
Bram Moolenaara7043832005-01-21 11:56:39 +00001741/*
1742 * List Vim variables.
1743 */
1744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001745list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001747 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001748}
1749
1750/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001751 * List script-local variables, if there is a script.
1752 */
1753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001754list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001755{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001756 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1757 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001758 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001759}
1760
1761/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 * List variables in "arg".
1763 */
1764 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766{
1767 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001768 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001769 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001770 char_u *name_start;
1771 char_u *arg_subsc;
1772 char_u *tofree;
1773 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774
1775 while (!ends_excmd(*arg) && !got_int)
1776 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001777 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001779 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001780 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001781 {
1782 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001783 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001784 break;
1785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001786 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001789 /* get_name_len() takes care of expanding curly braces */
1790 name_start = name = arg;
1791 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1792 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001794 /* This is mainly to keep test 49 working: when expanding
1795 * curly braces fails overrule the exception error message. */
1796 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001798 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001799 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001800 break;
1801 }
1802 error = TRUE;
1803 }
1804 else
1805 {
1806 if (tofree != NULL)
1807 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001808 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 else
1811 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001812 /* handle d.key, l[idx], f(expr) */
1813 arg_subsc = arg;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02001814 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1815 name, &name) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001816 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001819 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001820 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001821 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001822 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001823 case 'g': list_glob_vars(first); break;
1824 case 'b': list_buf_vars(first); break;
1825 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001826 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001827 case 'v': list_vim_vars(first); break;
1828 case 's': list_script_vars(first); break;
1829 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001831 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001832 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001833 }
1834 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001835 {
1836 char_u numbuf[NUMBUFLEN];
1837 char_u *tf;
1838 int c;
1839 char_u *s;
1840
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001841 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001842 c = *arg;
1843 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001844 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001845 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001846 tv.v_type,
1847 s == NULL ? (char_u *)"" : s,
1848 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001849 *arg = c;
1850 vim_free(tf);
1851 }
1852 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 }
1855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001856
1857 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001859
1860 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 }
1862
1863 return arg;
1864}
1865
1866/*
1867 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1868 * Returns a pointer to the char just after the var name.
1869 * Returns NULL if there is an error.
1870 */
1871 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001872ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001873 char_u *arg, // points to variable name
1874 typval_T *tv, // value to assign to variable
1875 int copy, // copy value from "tv"
1876 int is_const, // lock variable for const
1877 char_u *endchars, // valid chars after variable name or NULL
1878 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879{
1880 int c1;
1881 char_u *name;
1882 char_u *p;
1883 char_u *arg_end = NULL;
1884 int len;
1885 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
1888 /*
1889 * ":let $VAR = expr": Set environment variable.
1890 */
1891 if (*arg == '$')
1892 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001893 if (is_const)
1894 {
1895 emsg(_("E996: Cannot lock an environment variable"));
1896 return NULL;
1897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 /* Find the end of the name. */
1899 ++arg;
1900 name = arg;
1901 len = get_env_len(&arg);
1902 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 else
1905 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001906 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001907 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001910 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001911 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 {
1913 c1 = name[len];
1914 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001915 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 {
1918 int mustfree = FALSE;
1919 char_u *s = vim_getenv(name, &mustfree);
1920
1921 if (s != NULL)
1922 {
1923 p = tofree = concat_str(s, p);
1924 if (mustfree)
1925 vim_free(s);
1926 }
1927 }
1928 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001930 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001931 if (STRICMP(name, "HOME") == 0)
1932 init_homedir();
1933 else if (didset_vim && STRICMP(name, "VIM") == 0)
1934 didset_vim = FALSE;
1935 else if (didset_vimruntime
1936 && STRICMP(name, "VIMRUNTIME") == 0)
1937 didset_vimruntime = FALSE;
1938 arg_end = arg;
1939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 }
1943 }
1944 }
1945
1946 /*
1947 * ":let &option = expr": Set option value.
1948 * ":let &l:option = expr": Set local option value.
1949 * ":let &g:option = expr": Set global option value.
1950 */
1951 else if (*arg == '&')
1952 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001953 if (is_const)
1954 {
1955 emsg(_("E996: Cannot lock an option"));
1956 return NULL;
1957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* Find the end of the name. */
1959 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 if (p == NULL || (endchars != NULL
1961 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001962 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 else
1964 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 long n;
1966 int opt_type;
1967 long numval;
1968 char_u *stringval = NULL;
1969 char_u *s;
1970
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 c1 = *p;
1972 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001974 n = (long)tv_get_number(tv);
1975 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001976 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 {
1978 opt_type = get_option_value(arg, &numval,
1979 &stringval, opt_flags);
1980 if ((opt_type == 1 && *op == '.')
1981 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001984 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001985 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 else
1987 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001988 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001990 switch (*op)
1991 {
1992 case '+': n = numval + n; break;
1993 case '-': n = numval - n; break;
1994 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001995 case '/': n = (long)num_divide(numval, n); break;
1996 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001997 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001999 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 {
2001 s = concat_str(stringval, s);
2002 vim_free(stringval);
2003 stringval = s;
2004 }
2005 }
2006 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 if (s != NULL)
2008 {
2009 set_option_value(arg, n, s, opt_flags);
2010 arg_end = p;
2011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 }
2015 }
2016
2017 /*
2018 * ":let @r = expr": Set register contents.
2019 */
2020 else if (*arg == '@')
2021 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002022 if (is_const)
2023 {
2024 emsg(_("E996: Cannot lock a register"));
2025 return NULL;
2026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002028 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 else
2034 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002035 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002036 char_u *s;
2037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002038 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002039 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002041 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 if (s != NULL)
2043 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002044 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045 vim_free(s);
2046 }
2047 }
2048 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002049 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002051 arg_end = arg + 1;
2052 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002053 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 }
2055 }
2056
2057 /*
2058 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002061 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002063 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002065 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002066 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002069 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 else
2071 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002072 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 arg_end = p;
2074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 }
2078
2079 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081
2082 return arg_end;
2083}
2084
2085/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 * Get an lval: variable, Dict item or List item that can be assigned a value
2087 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2088 * "name.key", "name.key[expr]" etc.
2089 * Indexing only works if "name" is an existing List or Dictionary.
2090 * "name" points to the start of the name.
2091 * If "rettv" is not NULL it points to the value to be assigned.
2092 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2093 * wrong; must end in space or cmd separator.
2094 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002095 * flags:
2096 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002097 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002098 * GLV_NO_AUTOLOAD: do not use script autoloading
2099 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002105get_lval(
2106 char_u *name,
2107 typval_T *rettv,
2108 lval_T *lp,
2109 int unlet,
2110 int skip,
2111 int flags, /* GLV_ values */
2112 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 char_u *expr_start, *expr_end;
2116 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 dictitem_T *v;
2118 typval_T var1;
2119 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002121 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002123 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002125 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002128 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129
2130 if (skip)
2131 {
2132 /* When skipping just find the end of the name. */
2133 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002134 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 }
2136
2137 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002138 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 if (expr_start != NULL)
2140 {
2141 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002142 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 && *p != '[' && *p != '.')
2144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 return NULL;
2147 }
2148
2149 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2150 if (lp->ll_exp_name == NULL)
2151 {
2152 /* Report an invalid expression in braces, unless the
2153 * expression evaluation has been cancelled due to an
2154 * aborting error, an interrupt, or an exception. */
2155 if (!aborting() && !quiet)
2156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002157 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 return NULL;
2160 }
2161 }
2162 lp->ll_name = lp->ll_exp_name;
2163 }
2164 else
2165 lp->ll_name = name;
2166
2167 /* Without [idx] or .key we are done. */
2168 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2169 return p;
2170
2171 cc = *p;
2172 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002173 /* Only pass &ht when we would write to the variable, it prevents autoload
2174 * as well. */
2175 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2176 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 if (v == NULL)
2181 return NULL;
2182
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 /*
2184 * Loop until no more [idx] or .key is following.
2185 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002187 var1.v_type = VAR_UNKNOWN;
2188 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2192 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002193 && lp->ll_tv->vval.v_dict != NULL)
2194 && !(lp->ll_tv->v_type == VAR_BLOB
2195 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002198 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002207
Bram Moolenaar8c711452005-01-14 21:53:12 +00002208 len = -1;
2209 if (*p == '.')
2210 {
2211 key = p + 1;
2212 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2213 ;
2214 if (len == 0)
2215 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002217 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 }
2220 p = key + len;
2221 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002222 else
2223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002224 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002225 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002226 if (*p == ':')
2227 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002228 else
2229 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002230 empty1 = FALSE;
2231 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002233 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002234 {
2235 /* not a number or string */
2236 clear_tv(&var1);
2237 return NULL;
2238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002239 }
2240
2241 /* Optionally get the second index [ :expr]. */
2242 if (*p == ':')
2243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002247 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002248 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002250 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002251 if (rettv != NULL
2252 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002253 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002254 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002255 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002258 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002259 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002261 }
2262 p = skipwhite(p + 1);
2263 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 else
2266 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002268 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2269 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002270 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002273 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 {
2275 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002276 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 clear_tv(&var2);
2278 return NULL;
2279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002285
Bram Moolenaar8c711452005-01-14 21:53:12 +00002286 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002287 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002289 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002290 clear_tv(&var1);
2291 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002292 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002293 }
2294
2295 /* Skip to past ']'. */
2296 ++p;
2297 }
2298
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002300 {
2301 if (len == -1)
2302 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002305 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002307 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002309 }
2310 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 lp->ll_list = NULL;
2312 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002313 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002314
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002315 /* When assigning to a scope dictionary check that a function and
2316 * variable name is valid (only variable name unless it is l: or
2317 * g: dictionary). Disallow overwriting a builtin function. */
2318 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002319 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002320 int prevval;
2321 int wrong;
2322
2323 if (len != -1)
2324 {
2325 prevval = key[len];
2326 key[len] = NUL;
2327 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002328 else
2329 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002330 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2331 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002332 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002333 || !valid_varname(key);
2334 if (len != -1)
2335 key[len] = prevval;
2336 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002337 return NULL;
2338 }
2339
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002342 // Can't add "v:" or "a:" variable.
2343 if (lp->ll_dict == &vimvardict
2344 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002346 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002347 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002348 return NULL;
2349 }
2350
Bram Moolenaar31b81602019-02-10 22:14:27 +01002351 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002355 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002356 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 }
2359 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002363 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 p = NULL;
2366 break;
2367 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002368 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002369 else if ((flags & GLV_READ_ONLY) == 0
2370 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002371 {
2372 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002373 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002374 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002375
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002376 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002379 else if (lp->ll_tv->v_type == VAR_BLOB)
2380 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002381 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2382
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002383 /*
2384 * Get the number and item for the only or first index of the List.
2385 */
2386 if (empty1)
2387 lp->ll_n1 = 0;
2388 else
2389 // is number or string
2390 lp->ll_n1 = (long)tv_get_number(&var1);
2391 clear_tv(&var1);
2392
2393 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002394 || lp->ll_n1 > bloblen
2395 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002396 {
2397 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002398 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002399 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002400 return NULL;
2401 }
2402 if (lp->ll_range && !lp->ll_empty2)
2403 {
2404 lp->ll_n2 = (long)tv_get_number(&var2);
2405 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002406 if (lp->ll_n2 < 0
2407 || lp->ll_n2 >= bloblen
2408 || lp->ll_n2 < lp->ll_n1)
2409 {
2410 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002411 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002412 return NULL;
2413 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002414 }
2415 lp->ll_blob = lp->ll_tv->vval.v_blob;
2416 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002417 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 else
2420 {
2421 /*
2422 * Get the number and item for the only or first index of the List.
2423 */
2424 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002427 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002428 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002429 clear_tv(&var1);
2430
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 lp->ll_list = lp->ll_tv->vval.v_list;
2433 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2434 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002436 if (lp->ll_n1 < 0)
2437 {
2438 lp->ll_n1 = 0;
2439 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2440 }
2441 }
2442 if (lp->ll_li == NULL)
2443 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002444 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002445 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002446 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 }
2449
2450 /*
2451 * May need to find the item or absolute index for the second
2452 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 * When no index given: "lp->ll_empty2" is TRUE.
2454 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002458 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002459 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002465 {
2466 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002467 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002469 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 }
2472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2474 if (lp->ll_n1 < 0)
2475 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2476 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002477 {
2478 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002481 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482 }
2483
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 }
2487
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return p;
2490}
2491
2492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002496clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497{
2498 vim_free(lp->ll_exp_name);
2499 vim_free(lp->ll_newkey);
2500}
2501
2502/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002503 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002505 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2506 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 */
2508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002509set_var_lval(
2510 lval_T *lp,
2511 char_u *endp,
2512 typval_T *rettv,
2513 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002514 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002515 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516{
2517 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 listitem_T *ri;
2519 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520
2521 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002523 cc = *endp;
2524 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002525 if (lp->ll_blob != NULL)
2526 {
2527 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002528
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002529 if (op != NULL && *op != '=')
2530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002531 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002532 return;
2533 }
2534
2535 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2536 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002537 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002538
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002539 if (lp->ll_empty2)
2540 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2541
2542 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002544 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002545 return;
2546 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002547 if (lp->ll_empty2)
2548 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002549
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002550 ir = 0;
2551 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2552 blob_set(lp->ll_blob, il,
2553 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002554 }
2555 else
2556 {
2557 val = (int)tv_get_number_chk(rettv, &error);
2558 if (!error)
2559 {
2560 garray_T *gap = &lp->ll_blob->bv_ga;
2561
2562 // Allow for appending a byte. Setting a byte beyond
2563 // the end is an error otherwise.
2564 if (lp->ll_n1 < gap->ga_len
2565 || (lp->ll_n1 == gap->ga_len
2566 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2567 {
2568 blob_set(lp->ll_blob, lp->ll_n1, val);
2569 if (lp->ll_n1 == gap->ga_len)
2570 ++gap->ga_len;
2571 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002572 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002573 }
2574 }
2575 }
2576 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002578 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579
Bram Moolenaar9937a052019-06-15 15:45:06 +02002580 if (is_const)
2581 {
2582 emsg(_(e_cannot_mod));
2583 *endp = cc;
2584 return;
2585 }
2586
Bram Moolenaarff697e62019-02-12 22:28:33 +01002587 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002588 di = NULL;
2589 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2590 &tv, &di, TRUE, FALSE) == OK)
2591 {
2592 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002593 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2594 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002595 && tv_op(&tv, rettv, op) == OK)
2596 set_var(lp->ll_name, &tv, FALSE);
2597 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002600 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002601 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002602 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002604 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002605 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002606 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002607 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 else if (lp->ll_range)
2609 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002610 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002611 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002612
Bram Moolenaar9937a052019-06-15 15:45:06 +02002613 if (is_const)
2614 {
2615 emsg(_("E996: Cannot lock a range"));
2616 return;
2617 }
2618
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002619 /*
2620 * Check whether any of the list items is locked
2621 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002622 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002623 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002624 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002625 return;
2626 ri = ri->li_next;
2627 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2628 break;
2629 ll_li = ll_li->li_next;
2630 ++ll_n1;
2631 }
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /*
2634 * Assign the List values to the list items.
2635 */
2636 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002638 if (op != NULL && *op != '=')
2639 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2640 else
2641 {
2642 clear_tv(&lp->ll_li->li_tv);
2643 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 ri = ri->li_next;
2646 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2647 break;
2648 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002651 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 ri = NULL;
2654 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 lp->ll_li = lp->ll_li->li_next;
2658 ++lp->ll_n1;
2659 }
2660 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002661 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 else if (lp->ll_empty2
2663 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002665 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 }
2667 else
2668 {
2669 /*
2670 * Assign to a List or Dictionary item.
2671 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002672 if (is_const)
2673 {
2674 emsg(_("E996: Cannot lock a list or dict"));
2675 return;
2676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (lp->ll_newkey != NULL)
2678 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 if (op != NULL && *op != '=')
2680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002681 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 return;
2683 }
2684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002686 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (di == NULL)
2688 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002689 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2690 {
2691 vim_free(di);
2692 return;
2693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 else if (op != NULL && *op != '=')
2697 {
2698 tv_op(lp->ll_tv, rettv, op);
2699 return;
2700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /*
2705 * Assign the value to the variable or list item.
2706 */
2707 if (copy)
2708 copy_tv(rettv, lp->ll_tv);
2709 else
2710 {
2711 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002712 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 }
2715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002716}
2717
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002718/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002719 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2720 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002721 * Returns OK or FAIL.
2722 */
2723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002724tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002726 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002727 char_u numbuf[NUMBUFLEN];
2728 char_u *s;
2729
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002730 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2731 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2732 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002733 {
2734 switch (tv1->v_type)
2735 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002736 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 case VAR_DICT:
2738 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002739 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002740 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002741 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002742 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 break;
2744
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002745 case VAR_BLOB:
2746 if (*op != '+' || tv2->v_type != VAR_BLOB)
2747 break;
2748 // BLOB += BLOB
2749 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2750 {
2751 blob_T *b1 = tv1->vval.v_blob;
2752 blob_T *b2 = tv2->vval.v_blob;
2753 int i, len = blob_len(b2);
2754 for (i = 0; i < len; i++)
2755 ga_append(&b1->bv_ga, blob_get(b2, i));
2756 }
2757 return OK;
2758
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 case VAR_LIST:
2760 if (*op != '+' || tv2->v_type != VAR_LIST)
2761 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002762 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002763 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2764 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2765 return OK;
2766
2767 case VAR_NUMBER:
2768 case VAR_STRING:
2769 if (tv2->v_type == VAR_LIST)
2770 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002771 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002773 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002774 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002775#ifdef FEAT_FLOAT
2776 if (tv2->v_type == VAR_FLOAT)
2777 {
2778 float_T f = n;
2779
Bram Moolenaarff697e62019-02-12 22:28:33 +01002780 if (*op == '%')
2781 break;
2782 switch (*op)
2783 {
2784 case '+': f += tv2->vval.v_float; break;
2785 case '-': f -= tv2->vval.v_float; break;
2786 case '*': f *= tv2->vval.v_float; break;
2787 case '/': f /= tv2->vval.v_float; break;
2788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002789 clear_tv(tv1);
2790 tv1->v_type = VAR_FLOAT;
2791 tv1->vval.v_float = f;
2792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002794#endif
2795 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002796 switch (*op)
2797 {
2798 case '+': n += tv_get_number(tv2); break;
2799 case '-': n -= tv_get_number(tv2); break;
2800 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002801 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2802 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002803 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002804 clear_tv(tv1);
2805 tv1->v_type = VAR_NUMBER;
2806 tv1->vval.v_number = n;
2807 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002808 }
2809 else
2810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002811 if (tv2->v_type == VAR_FLOAT)
2812 break;
2813
Bram Moolenaarff697e62019-02-12 22:28:33 +01002814 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002815 s = tv_get_string(tv1);
2816 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 clear_tv(tv1);
2818 tv1->v_type = VAR_STRING;
2819 tv1->vval.v_string = s;
2820 }
2821 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002822
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002823 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002824#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002825 {
2826 float_T f;
2827
Bram Moolenaarff697e62019-02-12 22:28:33 +01002828 if (*op == '%' || *op == '.'
2829 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002830 && tv2->v_type != VAR_NUMBER
2831 && tv2->v_type != VAR_STRING))
2832 break;
2833 if (tv2->v_type == VAR_FLOAT)
2834 f = tv2->vval.v_float;
2835 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002836 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002837 switch (*op)
2838 {
2839 case '+': tv1->vval.v_float += f; break;
2840 case '-': tv1->vval.v_float -= f; break;
2841 case '*': tv1->vval.v_float *= f; break;
2842 case '/': tv1->vval.v_float /= f; break;
2843 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002844 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002845#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002846 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 }
2848 }
2849
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002850 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002851 return FAIL;
2852}
2853
2854/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 * Evaluate the expression used in a ":for var in expr" command.
2856 * "arg" points to "var".
2857 * Set "*errp" to TRUE for an error, FALSE otherwise;
2858 * Return a pointer that holds the info. Null when there is an error.
2859 */
2860 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002861eval_for_line(
2862 char_u *arg,
2863 int *errp,
2864 char_u **nextcmdp,
2865 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002866{
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002868 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002869 typval_T tv;
2870 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871
2872 *errp = TRUE; /* default: there is an error */
2873
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002874 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875 if (fi == NULL)
2876 return NULL;
2877
2878 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2879 if (expr == NULL)
2880 return fi;
2881
2882 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002883 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002885 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 return fi;
2887 }
2888
2889 if (skip)
2890 ++emsg_skip;
2891 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2892 {
2893 *errp = FALSE;
2894 if (!skip)
2895 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002896 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002898 l = tv.vval.v_list;
2899 if (l == NULL)
2900 {
2901 // a null list is like an empty list: do nothing
2902 clear_tv(&tv);
2903 }
2904 else
2905 {
2906 // No need to increment the refcount, it's already set for
2907 // the list being used in "tv".
2908 fi->fi_list = l;
2909 list_add_watch(l, &fi->fi_lw);
2910 fi->fi_lw.lw_item = l->lv_first;
2911 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002912 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002913 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002914 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002915 fi->fi_bi = 0;
2916 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002917 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002918 typval_T btv;
2919
2920 // Make a copy, so that the iteration still works when the
2921 // blob is changed.
2922 blob_copy(&tv, &btv);
2923 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002924 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002925 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002926 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002927 else
2928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002929 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002930 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 }
2932 }
2933 }
2934 if (skip)
2935 --emsg_skip;
2936
2937 return fi;
2938}
2939
2940/*
2941 * Use the first item in a ":for" list. Advance to the next.
2942 * Assign the values to the variable (list). "arg" points to the first one.
2943 * Return TRUE when a valid item was found, FALSE when at end of list or
2944 * something wrong.
2945 */
2946 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002947next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002948{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002949 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002950 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002953 if (fi->fi_blob != NULL)
2954 {
2955 typval_T tv;
2956
2957 if (fi->fi_bi >= blob_len(fi->fi_blob))
2958 return FALSE;
2959 tv.v_type = VAR_NUMBER;
2960 tv.v_lock = VAR_FIXED;
2961 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2962 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002963 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2964 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002965 }
2966
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967 item = fi->fi_lw.lw_item;
2968 if (item == NULL)
2969 result = FALSE;
2970 else
2971 {
2972 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002973 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2974 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 }
2976 return result;
2977}
2978
2979/*
2980 * Free the structure used to store info used by ":for".
2981 */
2982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002983free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984{
Bram Moolenaar33570922005-01-25 22:26:29 +00002985 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002987 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002988 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002989 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 list_unref(fi->fi_list);
2991 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002992 if (fi != NULL && fi->fi_blob != NULL)
2993 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 vim_free(fi);
2995}
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2998
2999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000set_context_for_expression(
3001 expand_T *xp,
3002 char_u *arg,
3003 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 int got_eq = FALSE;
3006 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 if (cmdidx == CMD_let)
3010 {
3011 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003012 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 {
3014 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003015 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003016 {
3017 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003018 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003019 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003020 break;
3021 }
3022 return;
3023 }
3024 }
3025 else
3026 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3027 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 while ((xp->xp_pattern = vim_strpbrk(arg,
3029 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3030 {
3031 c = *xp->xp_pattern;
3032 if (c == '&')
3033 {
3034 c = xp->xp_pattern[1];
3035 if (c == '&')
3036 {
3037 ++xp->xp_pattern;
3038 xp->xp_context = cmdidx != CMD_let || got_eq
3039 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3040 }
3041 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3045 xp->xp_pattern += 2;
3046
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049 else if (c == '$')
3050 {
3051 /* environment variable */
3052 xp->xp_context = EXPAND_ENV_VARS;
3053 }
3054 else if (c == '=')
3055 {
3056 got_eq = TRUE;
3057 xp->xp_context = EXPAND_EXPRESSION;
3058 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003059 else if (c == '#'
3060 && xp->xp_context == EXPAND_EXPRESSION)
3061 {
3062 /* Autoload function/variable contains '#'. */
3063 break;
3064 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003065 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 && xp->xp_context == EXPAND_FUNCTIONS
3067 && vim_strchr(xp->xp_pattern, '(') == NULL)
3068 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003069 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 break;
3071 }
3072 else if (cmdidx != CMD_let || got_eq)
3073 {
3074 if (c == '"') /* string */
3075 {
3076 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3077 if (c == '\\' && xp->xp_pattern[1] != NUL)
3078 ++xp->xp_pattern;
3079 xp->xp_context = EXPAND_NOTHING;
3080 }
3081 else if (c == '\'') /* literal string */
3082 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003083 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3085 /* skip */ ;
3086 xp->xp_context = EXPAND_NOTHING;
3087 }
3088 else if (c == '|')
3089 {
3090 if (xp->xp_pattern[1] == '|')
3091 {
3092 ++xp->xp_pattern;
3093 xp->xp_context = EXPAND_EXPRESSION;
3094 }
3095 else
3096 xp->xp_context = EXPAND_COMMANDS;
3097 }
3098 else
3099 xp->xp_context = EXPAND_EXPRESSION;
3100 }
3101 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003102 /* Doesn't look like something valid, expand as an expression
3103 * anyway. */
3104 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 arg = xp->xp_pattern;
3106 if (*arg != NUL)
3107 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3108 /* skip */ ;
3109 }
3110 xp->xp_pattern = arg;
3111}
3112
3113#endif /* FEAT_CMDL_COMPL */
3114
3115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * ":unlet[!] var1 ... " command.
3117 */
3118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003119ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003121 ex_unletlock(eap, eap->arg, 0);
3122}
3123
3124/*
3125 * ":lockvar" and ":unlockvar" commands
3126 */
3127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003129{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003131 int deep = 2;
3132
3133 if (eap->forceit)
3134 deep = -1;
3135 else if (vim_isdigit(*arg))
3136 {
3137 deep = getdigits(&arg);
3138 arg = skipwhite(arg);
3139 }
3140
3141 ex_unletlock(eap, arg, deep);
3142}
3143
3144/*
3145 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3146 */
3147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003148ex_unletlock(
3149 exarg_T *eap,
3150 char_u *argstart,
3151 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003152{
3153 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003156 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 do
3159 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003160 if (*arg == '$')
3161 {
3162 char_u *name = ++arg;
3163
3164 if (get_env_len(&arg) == 0)
3165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003166 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003167 return;
3168 }
3169 vim_unsetenv(name);
3170 arg = skipwhite(arg);
3171 continue;
3172 }
3173
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003175 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003176 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 if (lv.ll_name == NULL)
3178 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003179 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 if (name_end != NULL)
3183 {
3184 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003185 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003186 }
3187 if (!(eap->skip || error))
3188 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
3190 }
3191
3192 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003193 {
3194 if (eap->cmdidx == CMD_unlet)
3195 {
3196 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3197 error = TRUE;
3198 }
3199 else
3200 {
3201 if (do_lock_var(&lv, name_end, deep,
3202 eap->cmdidx == CMD_lockvar) == FAIL)
3203 error = TRUE;
3204 }
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 if (!eap->skip)
3208 clear_lval(&lv);
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 arg = skipwhite(name_end);
3211 } while (!ends_excmd(*arg));
3212
3213 eap->nextcmd = check_nextcmd(arg);
3214}
3215
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003217do_unlet_var(
3218 lval_T *lp,
3219 char_u *name_end,
3220 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003221{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003222 int ret = OK;
3223 int cc;
3224
3225 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 cc = *name_end;
3228 *name_end = NUL;
3229
3230 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003231 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003234 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003235 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003236 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003237 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003238 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003239 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003240 else if (lp->ll_range)
3241 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003243 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003244 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003245
3246 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3247 {
3248 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003249 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003250 return FAIL;
3251 ll_li = li;
3252 ++ll_n1;
3253 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254
3255 /* Delete a range of List items. */
3256 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3257 {
3258 li = lp->ll_li->li_next;
3259 listitem_remove(lp->ll_list, lp->ll_li);
3260 lp->ll_li = li;
3261 ++lp->ll_n1;
3262 }
3263 }
3264 else
3265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003266 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003267 /* unlet a List item. */
3268 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003269 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003270 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003271 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003272 }
3273
3274 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275}
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277/*
3278 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 */
3281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 hashtab_T *ht;
3285 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003287 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003288 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 ht = find_var_ht(name, &varname);
3291 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003294 if (d == NULL)
3295 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 if (ht == &globvarht)
3297 d = &globvardict;
3298 else if (ht == &compat_hashtab)
3299 d = &vimvardict;
3300 else
3301 {
3302 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3303 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3304 }
3305 if (d == NULL)
3306 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003307 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 return FAIL;
3309 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003310 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003311 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003312 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003313 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003314 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003315 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003316 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003317 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003318 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003319 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003320 return FAIL;
3321
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003322 delete_var(ht, hi);
3323 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003326 if (forceit)
3327 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003328 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return FAIL;
3330}
3331
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003332/*
3333 * Lock or unlock variable indicated by "lp".
3334 * "deep" is the levels to go (-1 for unlimited);
3335 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3336 */
3337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338do_lock_var(
3339 lval_T *lp,
3340 char_u *name_end,
3341 int deep,
3342 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003343{
3344 int ret = OK;
3345 int cc;
3346 dictitem_T *di;
3347
3348 if (deep == 0) /* nothing to do */
3349 return OK;
3350
3351 if (lp->ll_tv == NULL)
3352 {
3353 cc = *name_end;
3354 *name_end = NUL;
3355
3356 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003357 di = find_var(lp->ll_name, NULL, TRUE);
3358 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003359 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003360 else if ((di->di_flags & DI_FLAGS_FIX)
3361 && di->di_tv.v_type != VAR_DICT
3362 && di->di_tv.v_type != VAR_LIST)
3363 /* For historic reasons this error is not given for a list or dict.
3364 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003365 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 else
3367 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003368 if (lock)
3369 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003371 di->di_flags &= ~DI_FLAGS_LOCK;
3372 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373 }
3374 *name_end = cc;
3375 }
3376 else if (lp->ll_range)
3377 {
3378 listitem_T *li = lp->ll_li;
3379
3380 /* (un)lock a range of List items. */
3381 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3382 {
3383 item_lock(&li->li_tv, deep, lock);
3384 li = li->li_next;
3385 ++lp->ll_n1;
3386 }
3387 }
3388 else if (lp->ll_list != NULL)
3389 /* (un)lock a List item. */
3390 item_lock(&lp->ll_li->li_tv, deep, lock);
3391 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003392 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003393 item_lock(&lp->ll_di->di_tv, deep, lock);
3394
3395 return ret;
3396}
3397
3398/*
3399 * Lock or unlock an item. "deep" is nr of levels to go.
3400 */
3401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003402item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003403{
3404 static int recurse = 0;
3405 list_T *l;
3406 listitem_T *li;
3407 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003408 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003409 hashitem_T *hi;
3410 int todo;
3411
3412 if (recurse >= DICT_MAXNEST)
3413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003414 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003415 return;
3416 }
3417 if (deep == 0)
3418 return;
3419 ++recurse;
3420
3421 /* lock/unlock the item itself */
3422 if (lock)
3423 tv->v_lock |= VAR_LOCKED;
3424 else
3425 tv->v_lock &= ~VAR_LOCKED;
3426
3427 switch (tv->v_type)
3428 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003429 case VAR_UNKNOWN:
3430 case VAR_NUMBER:
3431 case VAR_STRING:
3432 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003433 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003434 case VAR_FLOAT:
3435 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003436 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003437 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003438 break;
3439
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003440 case VAR_BLOB:
3441 if ((b = tv->vval.v_blob) != NULL)
3442 {
3443 if (lock)
3444 b->bv_lock |= VAR_LOCKED;
3445 else
3446 b->bv_lock &= ~VAR_LOCKED;
3447 }
3448 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003449 case VAR_LIST:
3450 if ((l = tv->vval.v_list) != NULL)
3451 {
3452 if (lock)
3453 l->lv_lock |= VAR_LOCKED;
3454 else
3455 l->lv_lock &= ~VAR_LOCKED;
3456 if (deep < 0 || deep > 1)
3457 /* recursive: lock/unlock the items the List contains */
3458 for (li = l->lv_first; li != NULL; li = li->li_next)
3459 item_lock(&li->li_tv, deep - 1, lock);
3460 }
3461 break;
3462 case VAR_DICT:
3463 if ((d = tv->vval.v_dict) != NULL)
3464 {
3465 if (lock)
3466 d->dv_lock |= VAR_LOCKED;
3467 else
3468 d->dv_lock &= ~VAR_LOCKED;
3469 if (deep < 0 || deep > 1)
3470 {
3471 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003473 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3474 {
3475 if (!HASHITEM_EMPTY(hi))
3476 {
3477 --todo;
3478 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3479 }
3480 }
3481 }
3482 }
3483 }
3484 --recurse;
3485}
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3488/*
3489 * Delete all "menutrans_" variables.
3490 */
3491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003492del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
Bram Moolenaar33570922005-01-25 22:26:29 +00003494 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003495 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaar33570922005-01-25 22:26:29 +00003497 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003499 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003500 {
3501 if (!HASHITEM_EMPTY(hi))
3502 {
3503 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3505 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003506 }
3507 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003508 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509}
3510#endif
3511
3512#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3513
3514/*
3515 * Local string buffer for the next two functions to store a variable name
3516 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3517 * get_user_var_name().
3518 */
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520static char_u *varnamebuf = NULL;
3521static int varnamebuflen = 0;
3522
3523/*
3524 * Function to concatenate a prefix and a variable name.
3525 */
3526 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003527cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 int len;
3530
3531 len = (int)STRLEN(name) + 3;
3532 if (len > varnamebuflen)
3533 {
3534 vim_free(varnamebuf);
3535 len += 10; /* some additional space */
3536 varnamebuf = alloc(len);
3537 if (varnamebuf == NULL)
3538 {
3539 varnamebuflen = 0;
3540 return NULL;
3541 }
3542 varnamebuflen = len;
3543 }
3544 *varnamebuf = prefix;
3545 varnamebuf[1] = ':';
3546 STRCPY(varnamebuf + 2, name);
3547 return varnamebuf;
3548}
3549
3550/*
3551 * Function given to ExpandGeneric() to obtain the list of user defined
3552 * (global/buffer/window/built-in) variable names.
3553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003557 static long_u gdone;
3558 static long_u bdone;
3559 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003561 static int vidx;
3562 static hashitem_T *hi;
3563 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003566 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003567 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003568 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003569 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003570
3571 /* Global variables */
3572 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003576 else
3577 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003578 while (HASHITEM_EMPTY(hi))
3579 ++hi;
3580 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3581 return cat_prefix_varname('g', hi->hi_key);
3582 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003584
3585 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003586 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003587 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003589 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003591 else
3592 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 while (HASHITEM_EMPTY(hi))
3594 ++hi;
3595 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003597
3598 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003599 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003602 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003603 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003604 else
3605 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003606 while (HASHITEM_EMPTY(hi))
3607 ++hi;
3608 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003610
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003611 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003612 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003613 if (tdone < ht->ht_used)
3614 {
3615 if (tdone++ == 0)
3616 hi = ht->ht_array;
3617 else
3618 ++hi;
3619 while (HASHITEM_EMPTY(hi))
3620 ++hi;
3621 return cat_prefix_varname('t', hi->hi_key);
3622 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003623
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 /* v: variables */
3625 if (vidx < VV_LEN)
3626 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
Bram Moolenaard23a8232018-02-10 18:45:26 +01003628 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 varnamebuflen = 0;
3630 return NULL;
3631}
3632
3633#endif /* FEAT_CMDL_COMPL */
3634
3635/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003636 * Return TRUE if "pat" matches "text".
3637 * Does not use 'cpo' and always uses 'magic'.
3638 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003639 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003640pattern_match(char_u *pat, char_u *text, int ic)
3641{
3642 int matches = FALSE;
3643 char_u *save_cpo;
3644 regmatch_T regmatch;
3645
3646 /* avoid 'l' flag in 'cpoptions' */
3647 save_cpo = p_cpo;
3648 p_cpo = (char_u *)"";
3649 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3650 if (regmatch.regprog != NULL)
3651 {
3652 regmatch.rm_ic = ic;
3653 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3654 vim_regfree(regmatch.regprog);
3655 }
3656 p_cpo = save_cpo;
3657 return matches;
3658}
3659
3660/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003661 * Handle a name followed by "(". Both for just "name(arg)" and for
3662 * "expr->name(arg)".
3663 * Returns OK or FAIL.
3664 */
3665 static int
3666eval_func(
3667 char_u **arg, // points to "(", will be advanced
3668 char_u *name,
3669 int name_len,
3670 typval_T *rettv,
3671 int evaluate,
3672 typval_T *basetv) // "expr" for "expr->name(arg)"
3673{
3674 char_u *s = name;
3675 int len = name_len;
3676 partial_T *partial;
3677 int ret = OK;
3678
3679 if (!evaluate)
3680 check_vars(s, len);
3681
3682 /* If "s" is the name of a variable of type VAR_FUNC
3683 * use its contents. */
3684 s = deref_func_name(s, &len, &partial, !evaluate);
3685
3686 /* Need to make a copy, in case evaluating the arguments makes
3687 * the name invalid. */
3688 s = vim_strsave(s);
3689 if (s == NULL)
3690 ret = FAIL;
3691 else
3692 {
3693 funcexe_T funcexe;
3694
3695 // Invoke the function.
3696 vim_memset(&funcexe, 0, sizeof(funcexe));
3697 funcexe.firstline = curwin->w_cursor.lnum;
3698 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003699 funcexe.evaluate = evaluate;
3700 funcexe.partial = partial;
3701 funcexe.basetv = basetv;
3702 ret = get_func_tv(s, len, rettv, arg, &funcexe);
3703 }
3704 vim_free(s);
3705
3706 /* If evaluate is FALSE rettv->v_type was not set in
3707 * get_func_tv, but it's needed in handle_subscript() to parse
3708 * what follows. So set it here. */
3709 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3710 {
3711 rettv->vval.v_string = NULL;
3712 rettv->v_type = VAR_FUNC;
3713 }
3714
3715 /* Stop the expression evaluation when immediately
3716 * aborting on error, or when an interrupt occurred or
3717 * an exception was thrown but not caught. */
3718 if (evaluate && aborting())
3719 {
3720 if (ret == OK)
3721 clear_tv(rettv);
3722 ret = FAIL;
3723 }
3724 return ret;
3725}
3726
3727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3731 */
3732
3733/*
3734 * Handle zero level expression.
3735 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003737 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * Return OK or FAIL.
3739 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003741eval0(
3742 char_u *arg,
3743 typval_T *rettv,
3744 char_u **nextcmd,
3745 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 int ret;
3748 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003749 int did_emsg_before = did_emsg;
3750 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 if (ret == FAIL || !ends_excmd(*p))
3755 {
3756 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 /*
3759 * Report the invalid expression unless the expression evaluation has
3760 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003761 * exception, or we already gave a more specific error.
3762 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003764 if (!aborting() && did_emsg == did_emsg_before
3765 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003766 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 ret = FAIL;
3768 }
3769 if (nextcmd != NULL)
3770 *nextcmd = check_nextcmd(p);
3771
3772 return ret;
3773}
3774
3775/*
3776 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003777 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 *
3779 * "arg" must point to the first non-white of the expression.
3780 * "arg" is advanced to the next non-white after the recognized expression.
3781 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003782 * Note: "rettv.v_lock" is not set.
3783 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 * Return OK or FAIL.
3785 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003786 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003787eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788{
3789 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /*
3793 * Get the first variable.
3794 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797
3798 if ((*arg)[0] == '?')
3799 {
3800 result = FALSE;
3801 if (evaluate)
3802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003803 int error = FALSE;
3804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003805 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003808 if (error)
3809 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812 /*
3813 * Get the second variable.
3814 */
3815 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return FAIL;
3818
3819 /*
3820 * Check for the ":".
3821 */
3822 if ((*arg)[0] != ':')
3823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003824 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 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
3830 /*
3831 * Get the third variable.
3832 */
3833 *arg = skipwhite(*arg + 1);
3834 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3835 {
3836 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 return FAIL;
3839 }
3840 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843
3844 return OK;
3845}
3846
3847/*
3848 * Handle first level expression:
3849 * expr2 || expr2 || expr2 logical OR
3850 *
3851 * "arg" must point to the first non-white of the expression.
3852 * "arg" is advanced to the next non-white after the recognized expression.
3853 *
3854 * Return OK or FAIL.
3855 */
3856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003857eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 long result;
3861 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /*
3865 * Get the first variable.
3866 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return FAIL;
3869
3870 /*
3871 * Repeat until there is no following "||".
3872 */
3873 first = TRUE;
3874 result = FALSE;
3875 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3876 {
3877 if (evaluate && first)
3878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003879 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882 if (error)
3883 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 first = FALSE;
3885 }
3886
3887 /*
3888 * Get the second variable.
3889 */
3890 *arg = skipwhite(*arg + 2);
3891 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3892 return FAIL;
3893
3894 /*
3895 * Compute the result.
3896 */
3897 if (evaluate && !result)
3898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003899 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 if (error)
3903 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 if (evaluate)
3906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 rettv->v_type = VAR_NUMBER;
3908 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 }
3911
3912 return OK;
3913}
3914
3915/*
3916 * Handle second level expression:
3917 * expr3 && expr3 && expr3 logical AND
3918 *
3919 * "arg" must point to the first non-white of the expression.
3920 * "arg" is advanced to the next non-white after the recognized expression.
3921 *
3922 * Return OK or FAIL.
3923 */
3924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003925eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 long result;
3929 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 /*
3933 * Get the first variable.
3934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return FAIL;
3937
3938 /*
3939 * Repeat until there is no following "&&".
3940 */
3941 first = TRUE;
3942 result = TRUE;
3943 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3944 {
3945 if (evaluate && first)
3946 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003947 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 if (error)
3951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 first = FALSE;
3953 }
3954
3955 /*
3956 * Get the second variable.
3957 */
3958 *arg = skipwhite(*arg + 2);
3959 if (eval4(arg, &var2, evaluate && result) == FAIL)
3960 return FAIL;
3961
3962 /*
3963 * Compute the result.
3964 */
3965 if (evaluate && result)
3966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003967 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003970 if (error)
3971 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 if (evaluate)
3974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 rettv->v_type = VAR_NUMBER;
3976 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 }
3979
3980 return OK;
3981}
3982
3983/*
3984 * Handle third level expression:
3985 * var1 == var2
3986 * var1 =~ var2
3987 * var1 != var2
3988 * var1 !~ var2
3989 * var1 > var2
3990 * var1 >= var2
3991 * var1 < var2
3992 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003993 * var1 is var2
3994 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 *
3996 * "arg" must point to the first non-white of the expression.
3997 * "arg" is advanced to the next non-white after the recognized expression.
3998 *
3999 * Return OK or FAIL.
4000 */
4001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004002eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u *p;
4006 int i;
4007 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004008 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
4012 /*
4013 * Get the first variable.
4014 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return FAIL;
4017
4018 p = *arg;
4019 switch (p[0])
4020 {
4021 case '=': if (p[1] == '=')
4022 type = TYPE_EQUAL;
4023 else if (p[1] == '~')
4024 type = TYPE_MATCH;
4025 break;
4026 case '!': if (p[1] == '=')
4027 type = TYPE_NEQUAL;
4028 else if (p[1] == '~')
4029 type = TYPE_NOMATCH;
4030 break;
4031 case '>': if (p[1] != '=')
4032 {
4033 type = TYPE_GREATER;
4034 len = 1;
4035 }
4036 else
4037 type = TYPE_GEQUAL;
4038 break;
4039 case '<': if (p[1] != '=')
4040 {
4041 type = TYPE_SMALLER;
4042 len = 1;
4043 }
4044 else
4045 type = TYPE_SEQUAL;
4046 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004047 case 'i': if (p[1] == 's')
4048 {
4049 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4050 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004051 i = p[len];
4052 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004053 {
4054 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4055 type_is = TRUE;
4056 }
4057 }
4058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 */
4064 if (type != TYPE_UNKNOWN)
4065 {
4066 /* extra question mark appended: ignore case */
4067 if (p[len] == '?')
4068 {
4069 ic = TRUE;
4070 ++len;
4071 }
4072 /* extra '#' appended: match case */
4073 else if (p[len] == '#')
4074 {
4075 ic = FALSE;
4076 ++len;
4077 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004078 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else
4080 ic = p_ic;
4081
4082 /*
4083 * Get the second variable.
4084 */
4085 *arg = skipwhite(p + len);
4086 if (eval5(arg, &var2, evaluate) == FAIL)
4087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return FAIL;
4090 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004091 if (evaluate)
4092 {
4093 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4094
4095 clear_tv(&var2);
4096 return ret;
4097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099
4100 return OK;
4101}
4102
4103/*
4104 * Handle fourth level expression:
4105 * + number addition
4106 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004107 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004108 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 *
4110 * "arg" must point to the first non-white of the expression.
4111 * "arg" is advanced to the next non-white after the recognized expression.
4112 *
4113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004116eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T var2;
4119 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004121 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004122#ifdef FEAT_FLOAT
4123 float_T f1 = 0, f2 = 0;
4124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 char_u *s1, *s2;
4126 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4127 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004128 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 /*
4131 * Get the first variable.
4132 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004133 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135
4136 /*
4137 * Repeat computing, until no '+', '-' or '.' is following.
4138 */
4139 for (;;)
4140 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004141 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004143 concat = op == '.'
4144 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4145 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 break;
4147
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004148 if ((op != '+' || (rettv->v_type != VAR_LIST
4149 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004150#ifdef FEAT_FLOAT
4151 && (op == '.' || rettv->v_type != VAR_FLOAT)
4152#endif
4153 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 {
4155 /* For "list + ...", an illegal use of the first operand as
4156 * a number cannot be determined before evaluating the 2nd
4157 * operand: if this is also a list, all is ok.
4158 * For "something . ...", "something - ..." or "non-list + ...",
4159 * we know that the first operand needs to be a string or number
4160 * without evaluating the 2nd operand. So check before to avoid
4161 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004162 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 {
4164 clear_tv(rettv);
4165 return FAIL;
4166 }
4167 }
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 /*
4170 * Get the second variable.
4171 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004172 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4173 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004175 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179 }
4180
4181 if (evaluate)
4182 {
4183 /*
4184 * Compute the result.
4185 */
4186 if (op == '.')
4187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004188 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4189 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (s2 == NULL) /* type error ? */
4191 {
4192 clear_tv(rettv);
4193 clear_tv(&var2);
4194 return FAIL;
4195 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004196 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
4198 rettv->v_type = VAR_STRING;
4199 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004201 else if (op == '+' && rettv->v_type == VAR_BLOB
4202 && var2.v_type == VAR_BLOB)
4203 {
4204 blob_T *b1 = rettv->vval.v_blob;
4205 blob_T *b2 = var2.vval.v_blob;
4206 blob_T *b = blob_alloc();
4207 int i;
4208
4209 if (b != NULL)
4210 {
4211 for (i = 0; i < blob_len(b1); i++)
4212 ga_append(&b->bv_ga, blob_get(b1, i));
4213 for (i = 0; i < blob_len(b2); i++)
4214 ga_append(&b->bv_ga, blob_get(b2, i));
4215
4216 clear_tv(rettv);
4217 rettv_blob_set(rettv, b);
4218 }
4219 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004220 else if (op == '+' && rettv->v_type == VAR_LIST
4221 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004222 {
4223 /* concatenate Lists */
4224 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4225 &var3) == FAIL)
4226 {
4227 clear_tv(rettv);
4228 clear_tv(&var2);
4229 return FAIL;
4230 }
4231 clear_tv(rettv);
4232 *rettv = var3;
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 else
4235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 int error = FALSE;
4237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004238#ifdef FEAT_FLOAT
4239 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004241 f1 = rettv->vval.v_float;
4242 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004244 else
4245#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004247 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004248 if (error)
4249 {
4250 /* This can only happen for "list + non-list". For
4251 * "non-list + ..." or "something - ...", we returned
4252 * before evaluating the 2nd operand. */
4253 clear_tv(rettv);
4254 return FAIL;
4255 }
4256#ifdef FEAT_FLOAT
4257 if (var2.v_type == VAR_FLOAT)
4258 f1 = n1;
4259#endif
4260 }
4261#ifdef FEAT_FLOAT
4262 if (var2.v_type == VAR_FLOAT)
4263 {
4264 f2 = var2.vval.v_float;
4265 n2 = 0;
4266 }
4267 else
4268#endif
4269 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004270 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004271 if (error)
4272 {
4273 clear_tv(rettv);
4274 clear_tv(&var2);
4275 return FAIL;
4276 }
4277#ifdef FEAT_FLOAT
4278 if (rettv->v_type == VAR_FLOAT)
4279 f2 = n2;
4280#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004283
4284#ifdef FEAT_FLOAT
4285 /* If there is a float on either side the result is a float. */
4286 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4287 {
4288 if (op == '+')
4289 f1 = f1 + f2;
4290 else
4291 f1 = f1 - f2;
4292 rettv->v_type = VAR_FLOAT;
4293 rettv->vval.v_float = f1;
4294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004296#endif
4297 {
4298 if (op == '+')
4299 n1 = n1 + n2;
4300 else
4301 n1 = n1 - n2;
4302 rettv->v_type = VAR_NUMBER;
4303 rettv->vval.v_number = n1;
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 }
4309 return OK;
4310}
4311
4312/*
4313 * Handle fifth level expression:
4314 * * number multiplication
4315 * / number division
4316 * % number modulo
4317 *
4318 * "arg" must point to the first non-white of the expression.
4319 * "arg" is advanced to the next non-white after the recognized expression.
4320 *
4321 * Return OK or FAIL.
4322 */
4323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004324eval6(
4325 char_u **arg,
4326 typval_T *rettv,
4327 int evaluate,
4328 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar33570922005-01-25 22:26:29 +00004330 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004332 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004333#ifdef FEAT_FLOAT
4334 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004335 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004336#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 /*
4340 * Get the first variable.
4341 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004342 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return FAIL;
4344
4345 /*
4346 * Repeat computing, until no '*', '/' or '%' is following.
4347 */
4348 for (;;)
4349 {
4350 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004351 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 break;
4353
4354 if (evaluate)
4355 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004356#ifdef FEAT_FLOAT
4357 if (rettv->v_type == VAR_FLOAT)
4358 {
4359 f1 = rettv->vval.v_float;
4360 use_float = TRUE;
4361 n1 = 0;
4362 }
4363 else
4364#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004365 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004367 if (error)
4368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 else
4371 n1 = 0;
4372
4373 /*
4374 * Get the second variable.
4375 */
4376 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004377 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 return FAIL;
4379
4380 if (evaluate)
4381 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004382#ifdef FEAT_FLOAT
4383 if (var2.v_type == VAR_FLOAT)
4384 {
4385 if (!use_float)
4386 {
4387 f1 = n1;
4388 use_float = TRUE;
4389 }
4390 f2 = var2.vval.v_float;
4391 n2 = 0;
4392 }
4393 else
4394#endif
4395 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004396 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 clear_tv(&var2);
4398 if (error)
4399 return FAIL;
4400#ifdef FEAT_FLOAT
4401 if (use_float)
4402 f2 = n2;
4403#endif
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /*
4407 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004410#ifdef FEAT_FLOAT
4411 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004413 if (op == '*')
4414 f1 = f1 * f2;
4415 else if (op == '/')
4416 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004417# ifdef VMS
4418 /* VMS crashes on divide by zero, work around it */
4419 if (f2 == 0.0)
4420 {
4421 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004422 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004423 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004424 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004425 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004426 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004427 }
4428 else
4429 f1 = f1 / f2;
4430# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431 /* We rely on the floating point library to handle divide
4432 * by zero to result in "inf" and not a crash. */
4433 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004434# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004438 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004439 return FAIL;
4440 }
4441 rettv->v_type = VAR_FLOAT;
4442 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004447 if (op == '*')
4448 n1 = n1 * n2;
4449 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004450 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004452 n1 = num_modulus(n1, n2);
4453
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454 rettv->v_type = VAR_NUMBER;
4455 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 }
4459
4460 return OK;
4461}
4462
4463/*
4464 * Handle sixth level expression:
4465 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004466 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004467 * "string" string constant
4468 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 * &option-name option value
4470 * @r register contents
4471 * identifier variable value
4472 * function() function call
4473 * $VAR environment variable
4474 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004475 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004476 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004477 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 *
4479 * Also handle:
4480 * ! in front logical NOT
4481 * - in front unary minus
4482 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 * trailing [] subscript in String or List
4484 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004485 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 *
4487 * "arg" must point to the first non-white of the expression.
4488 * "arg" is advanced to the next non-white after the recognized expression.
4489 *
4490 * Return OK or FAIL.
4491 */
4492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004493eval7(
4494 char_u **arg,
4495 typval_T *rettv,
4496 int evaluate,
4497 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004499 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 int len;
4501 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 char_u *start_leader, *end_leader;
4503 int ret = OK;
4504 char_u *alias;
4505
4506 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004507 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
4512 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004513 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
4515 start_leader = *arg;
4516 while (**arg == '!' || **arg == '-' || **arg == '+')
4517 *arg = skipwhite(*arg + 1);
4518 end_leader = *arg;
4519
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004520 if (**arg == '.' && (!isdigit(*(*arg + 1))
4521#ifdef FEAT_FLOAT
4522 || current_sctx.sc_version < 2
4523#endif
4524 ))
4525 {
4526 semsg(_(e_invexpr2), *arg);
4527 ++*arg;
4528 return FAIL;
4529 }
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 switch (**arg)
4532 {
4533 /*
4534 * Number constant.
4535 */
4536 case '0':
4537 case '1':
4538 case '2':
4539 case '3':
4540 case '4':
4541 case '5':
4542 case '6':
4543 case '7':
4544 case '8':
4545 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004546 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004547 {
4548#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004549 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004550 int get_float = FALSE;
4551
4552 /* We accept a float when the format matches
4553 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004554 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004555 * With script version 2 and later the leading digit can be
4556 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004557 * Don't look for a float after the "." operator, so that
4558 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004559 if (**arg == '.')
4560 p = *arg;
4561 else
4562 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004563 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004565 get_float = TRUE;
4566 p = skipdigits(p + 2);
4567 if (*p == 'e' || *p == 'E')
4568 {
4569 ++p;
4570 if (*p == '-' || *p == '+')
4571 ++p;
4572 if (!vim_isdigit(*p))
4573 get_float = FALSE;
4574 else
4575 p = skipdigits(p + 1);
4576 }
4577 if (ASCII_ISALPHA(*p) || *p == '.')
4578 get_float = FALSE;
4579 }
4580 if (get_float)
4581 {
4582 float_T f;
4583
4584 *arg += string2float(*arg, &f);
4585 if (evaluate)
4586 {
4587 rettv->v_type = VAR_FLOAT;
4588 rettv->vval.v_float = f;
4589 }
4590 }
4591 else
4592#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004593 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004594 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004595 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004596 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004597
4598 // Blob constant: 0z0123456789abcdef
4599 if (evaluate)
4600 blob = blob_alloc();
4601 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4602 {
4603 if (!vim_isxdigit(bp[1]))
4604 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004605 if (blob != NULL)
4606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004607 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004608 ga_clear(&blob->bv_ga);
4609 VIM_CLEAR(blob);
4610 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004611 ret = FAIL;
4612 break;
4613 }
4614 if (blob != NULL)
4615 ga_append(&blob->bv_ga,
4616 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004617 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4618 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004619 }
4620 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004621 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004622 *arg = bp;
4623 }
4624 else
4625 {
4626 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004627 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4628 if (len == 0)
4629 {
4630 semsg(_(e_invexpr2), *arg);
4631 ret = FAIL;
4632 break;
4633 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 *arg += len;
4635 if (evaluate)
4636 {
4637 rettv->v_type = VAR_NUMBER;
4638 rettv->vval.v_number = n;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 /*
4645 * String constant: "string".
4646 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 break;
4649
4650 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 break;
4655
4656 /*
4657 * List: [expr, expr]
4658 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 break;
4661
4662 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004663 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004664 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004665 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004666 {
4667 ++*arg;
4668 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4669 }
4670 else
4671 ret = NOTDONE;
4672 break;
4673
4674 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004675 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004676 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004678 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4679 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004680 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 break;
4682
4683 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004686 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 break;
4688
4689 /*
4690 * Environment variable: $VAR.
4691 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 break;
4694
4695 /*
4696 * Register contents: @r.
4697 */
4698 case '@': ++*arg;
4699 if (evaluate)
4700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004702 rettv->vval.v_string = get_reg_contents(**arg,
4703 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 if (**arg != NUL)
4706 ++*arg;
4707 break;
4708
4709 /*
4710 * nested expression: (expression).
4711 */
4712 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (**arg == ')')
4715 ++*arg;
4716 else if (ret == OK)
4717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004718 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 ret = FAIL;
4721 }
4722 break;
4723
Bram Moolenaar8c711452005-01-14 21:53:12 +00004724 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 break;
4726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727
4728 if (ret == NOTDONE)
4729 {
4730 /*
4731 * Must be a variable or function name.
4732 * Can also be a curly-braces kind of name: {expr}.
4733 */
4734 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004735 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 if (alias != NULL)
4737 s = alias;
4738
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004739 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004740 ret = FAIL;
4741 else
4742 {
4743 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004744 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004746 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004747 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004748 {
4749 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004750 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004753 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 }
4755
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 *arg = skipwhite(*arg);
4757
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004759 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004760 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004761 ret = handle_subscript(arg, rettv, evaluate, TRUE,
4762 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 /*
4765 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4766 */
4767 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004768 ret = eval7_leader(rettv, start_leader, &end_leader);
4769 return ret;
4770}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004772/*
4773 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
4774 * Adjusts "end_leaderp" until it is at "start_leader".
4775 */
4776 static int
4777eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
4778{
4779 char_u *end_leader = *end_leaderp;
4780 int ret = OK;
4781 int error = FALSE;
4782 varnumber_T val = 0;
4783#ifdef FEAT_FLOAT
4784 float_T f = 0.0;
4785
4786 if (rettv->v_type == VAR_FLOAT)
4787 f = rettv->vval.v_float;
4788 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004790 val = tv_get_number_chk(rettv, &error);
4791 if (error)
4792 {
4793 clear_tv(rettv);
4794 ret = FAIL;
4795 }
4796 else
4797 {
4798 while (end_leader > start_leader)
4799 {
4800 --end_leader;
4801 if (*end_leader == '!')
4802 {
4803#ifdef FEAT_FLOAT
4804 if (rettv->v_type == VAR_FLOAT)
4805 f = !f;
4806 else
4807#endif
4808 val = !val;
4809 }
4810 else if (*end_leader == '-')
4811 {
4812#ifdef FEAT_FLOAT
4813 if (rettv->v_type == VAR_FLOAT)
4814 f = -f;
4815 else
4816#endif
4817 val = -val;
4818 }
4819 }
4820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004824 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004829 clear_tv(rettv);
4830 rettv->v_type = VAR_NUMBER;
4831 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004834 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 return ret;
4836}
4837
4838/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004839 * Call the function referred to in "rettv".
4840 */
4841 static int
4842call_func_rettv(
4843 char_u **arg,
4844 typval_T *rettv,
4845 int evaluate,
4846 dict_T *selfdict,
4847 typval_T *basetv)
4848{
4849 partial_T *pt = NULL;
4850 funcexe_T funcexe;
4851 typval_T functv;
4852 char_u *s;
4853 int ret;
4854
4855 // need to copy the funcref so that we can clear rettv
4856 if (evaluate)
4857 {
4858 functv = *rettv;
4859 rettv->v_type = VAR_UNKNOWN;
4860
4861 /* Invoke the function. Recursive! */
4862 if (functv.v_type == VAR_PARTIAL)
4863 {
4864 pt = functv.vval.v_partial;
4865 s = partial_name(pt);
4866 }
4867 else
4868 s = functv.vval.v_string;
4869 }
4870 else
4871 s = (char_u *)"";
4872
4873 vim_memset(&funcexe, 0, sizeof(funcexe));
4874 funcexe.firstline = curwin->w_cursor.lnum;
4875 funcexe.lastline = curwin->w_cursor.lnum;
4876 funcexe.evaluate = evaluate;
4877 funcexe.partial = pt;
4878 funcexe.selfdict = selfdict;
4879 funcexe.basetv = basetv;
4880 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
4881
4882 /* Clear the funcref afterwards, so that deleting it while
4883 * evaluating the arguments is possible (see test55). */
4884 if (evaluate)
4885 clear_tv(&functv);
4886
4887 return ret;
4888}
4889
4890/*
4891 * Evaluate "->method()".
4892 * "*arg" points to the '-'.
4893 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4894 */
4895 static int
4896eval_lambda(
4897 char_u **arg,
4898 typval_T *rettv,
4899 int evaluate,
4900 int verbose) /* give error messages */
4901{
4902 typval_T base = *rettv;
4903 int ret;
4904
4905 // Skip over the ->.
4906 *arg += 2;
4907 rettv->v_type = VAR_UNKNOWN;
4908
4909 ret = get_lambda_tv(arg, rettv, evaluate);
4910 if (ret == NOTDONE)
4911 return FAIL;
4912 else if (**arg != '(')
4913 {
4914 if (verbose)
4915 {
4916 if (*skipwhite(*arg) == '(')
4917 semsg(_(e_nowhitespace));
4918 else
4919 semsg(_(e_missingparen), "lambda");
4920 }
4921 clear_tv(rettv);
4922 return FAIL;
4923 }
4924 return call_func_rettv(arg, rettv, evaluate, NULL, &base);
4925}
4926
4927/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004928 * Evaluate "->method()".
4929 * "*arg" points to the '-'.
4930 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4931 */
4932 static int
4933eval_method(
4934 char_u **arg,
4935 typval_T *rettv,
4936 int evaluate,
4937 int verbose) /* give error messages */
4938{
4939 char_u *name;
4940 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004941 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004942 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004943 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004944
4945 // Skip over the ->.
4946 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004947 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004948
Bram Moolenaarac92e252019-08-03 21:58:38 +02004949 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004950 len = get_name_len(arg, &alias, evaluate, TRUE);
4951 if (alias != NULL)
4952 name = alias;
4953
4954 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004955 {
4956 if (verbose)
4957 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004958 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004959 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004960 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004961 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004962 if (**arg != '(')
4963 {
4964 if (verbose)
4965 semsg(_(e_missingparen), name);
4966 ret = FAIL;
4967 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004968 else if (VIM_ISWHITE((*arg)[-1]))
4969 {
4970 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004971 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02004972 ret = FAIL;
4973 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004974 else
4975 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004976 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004977
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004978 // Clear the funcref afterwards, so that deleting it while
4979 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004980 if (evaluate)
4981 clear_tv(&base);
4982
Bram Moolenaarac92e252019-08-03 21:58:38 +02004983 return ret;
4984}
4985
4986/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004987 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4988 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4990 */
4991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004992eval_index(
4993 char_u **arg,
4994 typval_T *rettv,
4995 int evaluate,
4996 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004997{
4998 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004999 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005000 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 long len = -1;
5003 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005004 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005006
Bram Moolenaara03f2332016-02-06 18:09:59 +01005007 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005009 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005010 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005011 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005012 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005013 return FAIL;
5014 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005015#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005016 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005017 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005018 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005019#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005020 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005021 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005022 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005023 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005024 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005025 return FAIL;
5026 case VAR_UNKNOWN:
5027 if (evaluate)
5028 return FAIL;
5029 /* FALLTHROUGH */
5030
5031 case VAR_STRING:
5032 case VAR_NUMBER:
5033 case VAR_LIST:
5034 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005035 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005036 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005037 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005038
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005039 init_tv(&var1);
5040 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 /*
5044 * dict.name
5045 */
5046 key = *arg + 1;
5047 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5048 ;
5049 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005050 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005052 }
5053 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 /*
5056 * something[idx]
5057 *
5058 * Get the (first) variable from inside the [].
5059 */
5060 *arg = skipwhite(*arg + 1);
5061 if (**arg == ':')
5062 empty1 = TRUE;
5063 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5064 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005065 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005066 {
5067 /* not a number or string */
5068 clear_tv(&var1);
5069 return FAIL;
5070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071
5072 /*
5073 * Get the second variable from inside the [:].
5074 */
5075 if (**arg == ':')
5076 {
5077 range = TRUE;
5078 *arg = skipwhite(*arg + 1);
5079 if (**arg == ']')
5080 empty2 = TRUE;
5081 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005083 if (!empty1)
5084 clear_tv(&var1);
5085 return FAIL;
5086 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005087 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005088 {
5089 /* not a number or string */
5090 if (!empty1)
5091 clear_tv(&var1);
5092 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 return FAIL;
5094 }
5095 }
5096
5097 /* Check for the ']'. */
5098 if (**arg != ']')
5099 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005100 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005101 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 clear_tv(&var1);
5103 if (range)
5104 clear_tv(&var2);
5105 return FAIL;
5106 }
5107 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 }
5109
5110 if (evaluate)
5111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 n1 = 0;
5113 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005115 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 }
5118 if (range)
5119 {
5120 if (empty2)
5121 n2 = -1;
5122 else
5123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005124 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126 }
5127 }
5128
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005129 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005131 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005132 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005133 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005134 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005135 case VAR_SPECIAL:
5136 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005137 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005138 break; /* not evaluating, skipping over subscript */
5139
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 case VAR_NUMBER:
5141 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005142 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 len = (long)STRLEN(s);
5144 if (range)
5145 {
5146 /* The resulting variable is a substring. If the indexes
5147 * are out of range the result is empty. */
5148 if (n1 < 0)
5149 {
5150 n1 = len + n1;
5151 if (n1 < 0)
5152 n1 = 0;
5153 }
5154 if (n2 < 0)
5155 n2 = len + n2;
5156 else if (n2 >= len)
5157 n2 = len;
5158 if (n1 >= len || n2 < 0 || n1 > n2)
5159 s = NULL;
5160 else
5161 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5162 }
5163 else
5164 {
5165 /* The resulting variable is a string of a single
5166 * character. If the index is too big or negative the
5167 * result is empty. */
5168 if (n1 >= len || n1 < 0)
5169 s = NULL;
5170 else
5171 s = vim_strnsave(s + n1, 1);
5172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 clear_tv(rettv);
5174 rettv->v_type = VAR_STRING;
5175 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 break;
5177
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005178 case VAR_BLOB:
5179 len = blob_len(rettv->vval.v_blob);
5180 if (range)
5181 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005182 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005183 // are out of range the result is empty.
5184 if (n1 < 0)
5185 {
5186 n1 = len + n1;
5187 if (n1 < 0)
5188 n1 = 0;
5189 }
5190 if (n2 < 0)
5191 n2 = len + n2;
5192 else if (n2 >= len)
5193 n2 = len - 1;
5194 if (n1 >= len || n2 < 0 || n1 > n2)
5195 {
5196 clear_tv(rettv);
5197 rettv->v_type = VAR_BLOB;
5198 rettv->vval.v_blob = NULL;
5199 }
5200 else
5201 {
5202 blob_T *blob = blob_alloc();
5203
5204 if (blob != NULL)
5205 {
5206 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5207 {
5208 blob_free(blob);
5209 return FAIL;
5210 }
5211 blob->bv_ga.ga_len = n2 - n1 + 1;
5212 for (i = n1; i <= n2; i++)
5213 blob_set(blob, i - n1,
5214 blob_get(rettv->vval.v_blob, i));
5215
5216 clear_tv(rettv);
5217 rettv_blob_set(rettv, blob);
5218 }
5219 }
5220 }
5221 else
5222 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005223 // The resulting variable is a byte value.
5224 // If the index is too big or negative that is an error.
5225 if (n1 < 0)
5226 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005227 if (n1 < len && n1 >= 0)
5228 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005229 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005230
5231 clear_tv(rettv);
5232 rettv->v_type = VAR_NUMBER;
5233 rettv->vval.v_number = v;
5234 }
5235 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005236 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005237 }
5238 break;
5239
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005241 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 if (n1 < 0)
5243 n1 = len + n1;
5244 if (!empty1 && (n1 < 0 || n1 >= len))
5245 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005246 /* For a range we allow invalid values and return an empty
5247 * list. A list index out of range is an error. */
5248 if (!range)
5249 {
5250 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005251 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005252 return FAIL;
5253 }
5254 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 }
5256 if (range)
5257 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005258 list_T *l;
5259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260
5261 if (n2 < 0)
5262 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005263 else if (n2 >= len)
5264 n2 = len - 1;
5265 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005266 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 l = list_alloc();
5268 if (l == NULL)
5269 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 n1 <= n2; ++n1)
5272 {
5273 if (list_append_tv(l, &item->li_tv) == FAIL)
5274 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005275 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 return FAIL;
5277 }
5278 item = item->li_next;
5279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005281 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 }
5283 else
5284 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005285 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286 clear_tv(rettv);
5287 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 }
5289 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 case VAR_DICT:
5292 if (range)
5293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005294 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005295 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (len == -1)
5297 clear_tv(&var1);
5298 return FAIL;
5299 }
5300 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005301 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302
5303 if (len == -1)
5304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005305 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005306 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 clear_tv(&var1);
5309 return FAIL;
5310 }
5311 }
5312
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005313 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005315 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005316 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 if (len == -1)
5318 clear_tv(&var1);
5319 if (item == NULL)
5320 return FAIL;
5321
5322 copy_tv(&item->di_tv, &var1);
5323 clear_tv(rettv);
5324 *rettv = var1;
5325 }
5326 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 }
5328 }
5329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 return OK;
5331}
5332
5333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 * Get an option value.
5335 * "arg" points to the '&' or '+' before the option name.
5336 * "arg" is advanced to character after the option name.
5337 * Return OK or FAIL.
5338 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005340get_option_tv(
5341 char_u **arg,
5342 typval_T *rettv, /* when NULL, only check if option exists */
5343 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 char_u *option_end;
5346 long numval;
5347 char_u *stringval;
5348 int opt_type;
5349 int c;
5350 int working = (**arg == '+'); /* has("+option") */
5351 int ret = OK;
5352 int opt_flags;
5353
5354 /*
5355 * Isolate the option name and find its value.
5356 */
5357 option_end = find_option_end(arg, &opt_flags);
5358 if (option_end == NULL)
5359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005361 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 return FAIL;
5363 }
5364
5365 if (!evaluate)
5366 {
5367 *arg = option_end;
5368 return OK;
5369 }
5370
5371 c = *option_end;
5372 *option_end = NUL;
5373 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 if (opt_type == -3) /* invalid name */
5377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005379 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 ret = FAIL;
5381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
5384 if (opt_type == -2) /* hidden string option */
5385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 rettv->v_type = VAR_STRING;
5387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 else if (opt_type == -1) /* hidden number option */
5390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 rettv->v_type = VAR_NUMBER;
5392 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 else if (opt_type == 1) /* number option */
5395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 rettv->v_type = VAR_NUMBER;
5397 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 else /* string option */
5400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 rettv->v_type = VAR_STRING;
5402 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404 }
5405 else if (working && (opt_type == -2 || opt_type == -1))
5406 ret = FAIL;
5407
5408 *option_end = c; /* put back for error messages */
5409 *arg = option_end;
5410
5411 return ret;
5412}
5413
5414/*
5415 * Allocate a variable for a string constant.
5416 * Return OK or FAIL.
5417 */
5418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 char_u *p;
5422 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 int extra = 0;
5424
5425 /*
5426 * Find the end of the string, skipping backslashed characters.
5427 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005428 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 if (*p == '\\' && p[1] != NUL)
5431 {
5432 ++p;
5433 /* A "\<x>" form occupies at least 4 characters, and produces up
5434 * to 6 characters: reserve space for 2 extra */
5435 if (*p == '<')
5436 extra += 2;
5437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439
5440 if (*p != '"')
5441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005442 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 return FAIL;
5444 }
5445
5446 /* If only parsing, set *arg and return here */
5447 if (!evaluate)
5448 {
5449 *arg = p + 1;
5450 return OK;
5451 }
5452
5453 /*
5454 * Copy the string into allocated memory, handling backslashed
5455 * characters.
5456 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005457 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if (name == NULL)
5459 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 rettv->v_type = VAR_STRING;
5461 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
5465 if (*p == '\\')
5466 {
5467 switch (*++p)
5468 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 case 'b': *name++ = BS; ++p; break;
5470 case 'e': *name++ = ESC; ++p; break;
5471 case 'f': *name++ = FF; ++p; break;
5472 case 'n': *name++ = NL; ++p; break;
5473 case 'r': *name++ = CAR; ++p; break;
5474 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
5476 case 'X': /* hex: "\x1", "\x12" */
5477 case 'x':
5478 case 'u': /* Unicode: "\u0023" */
5479 case 'U':
5480 if (vim_isxdigit(p[1]))
5481 {
5482 int n, nr;
5483 int c = toupper(*p);
5484
5485 if (c == 'X')
5486 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005487 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005489 else
5490 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 nr = 0;
5492 while (--n >= 0 && vim_isxdigit(p[1]))
5493 {
5494 ++p;
5495 nr = (nr << 4) + hex2nr(*p);
5496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 /* For "\u" store the number according to
5499 * 'encoding'. */
5500 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 break;
5506
5507 /* octal: "\1", "\12", "\123" */
5508 case '0':
5509 case '1':
5510 case '2':
5511 case '3':
5512 case '4':
5513 case '5':
5514 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 case '7': *name = *p++ - '0';
5516 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 *name = (*name << 3) + *p++ - '0';
5519 if (*p >= '0' && *p <= '7')
5520 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 break;
5524
5525 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005526 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 if (extra != 0)
5528 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005529 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 break;
5531 }
5532 /* FALLTHROUGH */
5533
Bram Moolenaar8c711452005-01-14 21:53:12 +00005534 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 break;
5536 }
5537 }
5538 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005543 if (*p != NUL) /* just in case */
5544 ++p;
5545 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 return OK;
5548}
5549
5550/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * Return OK or FAIL.
5553 */
5554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005555get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005558 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005559 int reduce = 0;
5560
5561 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005563 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005564 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005569 break;
5570 ++reduce;
5571 ++p;
5572 }
5573 }
5574
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005577 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005578 return FAIL;
5579 }
5580
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005582 if (!evaluate)
5583 {
5584 *arg = p + 1;
5585 return OK;
5586 }
5587
5588 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005590 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005591 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 if (str == NULL)
5593 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 break;
5603 ++p;
5604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 *arg = p + 1;
5609
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005610 return OK;
5611}
5612
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005613/*
5614 * Return the function name of the partial.
5615 */
5616 char_u *
5617partial_name(partial_T *pt)
5618{
5619 if (pt->pt_name != NULL)
5620 return pt->pt_name;
5621 return pt->pt_func->uf_name;
5622}
5623
Bram Moolenaarddecc252016-04-06 22:59:37 +02005624 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005625partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005626{
5627 int i;
5628
5629 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005630 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005631 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005632 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005633 if (pt->pt_name != NULL)
5634 {
5635 func_unref(pt->pt_name);
5636 vim_free(pt->pt_name);
5637 }
5638 else
5639 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005640 vim_free(pt);
5641}
5642
5643/*
5644 * Unreference a closure: decrement the reference count and free it when it
5645 * becomes zero.
5646 */
5647 void
5648partial_unref(partial_T *pt)
5649{
5650 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005651 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005652}
5653
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005654static int tv_equal_recurse_limit;
5655
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005656 static int
5657func_equal(
5658 typval_T *tv1,
5659 typval_T *tv2,
5660 int ic) /* ignore case */
5661{
5662 char_u *s1, *s2;
5663 dict_T *d1, *d2;
5664 int a1, a2;
5665 int i;
5666
5667 /* empty and NULL function name considered the same */
5668 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005669 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005670 if (s1 != NULL && *s1 == NUL)
5671 s1 = NULL;
5672 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005673 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005674 if (s2 != NULL && *s2 == NUL)
5675 s2 = NULL;
5676 if (s1 == NULL || s2 == NULL)
5677 {
5678 if (s1 != s2)
5679 return FALSE;
5680 }
5681 else if (STRCMP(s1, s2) != 0)
5682 return FALSE;
5683
5684 /* empty dict and NULL dict is different */
5685 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5686 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5687 if (d1 == NULL || d2 == NULL)
5688 {
5689 if (d1 != d2)
5690 return FALSE;
5691 }
5692 else if (!dict_equal(d1, d2, ic, TRUE))
5693 return FALSE;
5694
5695 /* empty list and no list considered the same */
5696 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5697 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5698 if (a1 != a2)
5699 return FALSE;
5700 for (i = 0; i < a1; ++i)
5701 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5702 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5703 return FALSE;
5704
5705 return TRUE;
5706}
5707
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005708/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005709 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005710 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005711 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005712 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005714tv_equal(
5715 typval_T *tv1,
5716 typval_T *tv2,
5717 int ic, /* ignore case */
5718 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005719{
5720 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005721 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005722 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005723 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005724
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005725 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005726 * recursiveness to a limit. We guess they are equal then.
5727 * A fixed limit has the problem of still taking an awful long time.
5728 * Reduce the limit every time running into it. That should work fine for
5729 * deeply linked structures that are not recursively linked and catch
5730 * recursiveness quickly. */
5731 if (!recursive)
5732 tv_equal_recurse_limit = 1000;
5733 if (recursive_cnt >= tv_equal_recurse_limit)
5734 {
5735 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005736 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005737 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005738
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005739 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5740 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005741 if ((tv1->v_type == VAR_FUNC
5742 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5743 && (tv2->v_type == VAR_FUNC
5744 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5745 {
5746 ++recursive_cnt;
5747 r = func_equal(tv1, tv2, ic);
5748 --recursive_cnt;
5749 return r;
5750 }
5751
5752 if (tv1->v_type != tv2->v_type)
5753 return FALSE;
5754
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005755 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005756 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005757 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005758 ++recursive_cnt;
5759 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5760 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005761 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005762
5763 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005764 ++recursive_cnt;
5765 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5766 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005767 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005768
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005769 case VAR_BLOB:
5770 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5771
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005772 case VAR_NUMBER:
5773 return tv1->vval.v_number == tv2->vval.v_number;
5774
5775 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005776 s1 = tv_get_string_buf(tv1, buf1);
5777 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005778 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005779
5780 case VAR_SPECIAL:
5781 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005782
5783 case VAR_FLOAT:
5784#ifdef FEAT_FLOAT
5785 return tv1->vval.v_float == tv2->vval.v_float;
5786#endif
5787 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005788#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005789 return tv1->vval.v_job == tv2->vval.v_job;
5790#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005791 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005793 return tv1->vval.v_channel == tv2->vval.v_channel;
5794#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005795 case VAR_FUNC:
5796 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005797 case VAR_UNKNOWN:
5798 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005799 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005800
Bram Moolenaara03f2332016-02-06 18:09:59 +01005801 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5802 * does not equal anything, not even itself. */
5803 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005804}
5805
5806/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005807 * Return the next (unique) copy ID.
5808 * Used for serializing nested structures.
5809 */
5810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005811get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005812{
5813 current_copyID += COPYID_INC;
5814 return current_copyID;
5815}
5816
5817/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005818 * Garbage collection for lists and dictionaries.
5819 *
5820 * We use reference counts to be able to free most items right away when they
5821 * are no longer used. But for composite items it's possible that it becomes
5822 * unused while the reference count is > 0: When there is a recursive
5823 * reference. Example:
5824 * :let l = [1, 2, 3]
5825 * :let d = {9: l}
5826 * :let l[1] = d
5827 *
5828 * Since this is quite unusual we handle this with garbage collection: every
5829 * once in a while find out which lists and dicts are not referenced from any
5830 * variable.
5831 *
5832 * Here is a good reference text about garbage collection (refers to Python
5833 * but it applies to all reference-counting mechanisms):
5834 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005835 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005836
5837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005838 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005839 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005840 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005841 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005842 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005843garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005844{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005845 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005847 buf_T *buf;
5848 win_T *wp;
5849 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005850 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005851 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005852
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005853 if (!testing)
5854 {
5855 /* Only do this once. */
5856 want_garbage_collect = FALSE;
5857 may_garbage_collect = FALSE;
5858 garbage_collect_at_exit = FALSE;
5859 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005860
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005861 /* We advance by two because we add one for items referenced through
5862 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005863 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005864
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 /*
5866 * 1. Go through all accessible variables and mark all lists and dicts
5867 * with copyID.
5868 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005869
5870 /* Don't free variables in the previous_funccal list unless they are only
5871 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005872 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005875 /* script-local variables */
5876 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005877 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005878
5879 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005880 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005881 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5882 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005883
5884 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005885 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005886 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5887 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005888 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005889 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5890 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005891#ifdef FEAT_TEXT_PROP
5892 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5893 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5894 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005895 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005896 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005897 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5898 NULL, NULL);
5899#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005900
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005901 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005902 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005903 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5904 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005905 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005906 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005907
5908 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005909 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005910
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005911 /* named functions (matters for closures) */
5912 abort = abort || set_ref_in_functions(copyID);
5913
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005914 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005915 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005916
Bram Moolenaard812df62008-11-09 12:46:09 +00005917 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005918 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005919
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005920 // callbacks in buffers
5921 abort = abort || set_ref_in_buffers(copyID);
5922
Bram Moolenaar1dced572012-04-05 16:54:08 +02005923#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005924 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005925#endif
5926
Bram Moolenaardb913952012-06-29 12:54:53 +02005927#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005928 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005929#endif
5930
5931#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005932 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005933#endif
5934
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005935#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005936 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005937 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005938#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005939#ifdef FEAT_NETBEANS_INTG
5940 abort = abort || set_ref_in_nb_channel(copyID);
5941#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005942
Bram Moolenaare3188e22016-05-31 21:13:04 +02005943#ifdef FEAT_TIMERS
5944 abort = abort || set_ref_in_timer(copyID);
5945#endif
5946
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005947#ifdef FEAT_QUICKFIX
5948 abort = abort || set_ref_in_quickfix(copyID);
5949#endif
5950
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005951#ifdef FEAT_TERMINAL
5952 abort = abort || set_ref_in_term(copyID);
5953#endif
5954
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005955#ifdef FEAT_TEXT_PROP
5956 abort = abort || set_ref_in_popups(copyID);
5957#endif
5958
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005959 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005960 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005961 /*
5962 * 2. Free lists and dictionaries that are not referenced.
5963 */
5964 did_free = free_unref_items(copyID);
5965
5966 /*
5967 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005969 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005971 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005972 else if (p_verbose > 0)
5973 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005974 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005975 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005976
5977 return did_free;
5978}
5979
5980/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005981 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005982 */
5983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005985{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005986 int did_free = FALSE;
5987
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005988 /* Let all "free" functions know that we are here. This means no
5989 * dictionaries, lists, channels or jobs are to be freed, because we will
5990 * do that here. */
5991 in_free_unref_items = TRUE;
5992
5993 /*
5994 * PASS 1: free the contents of the items. We don't free the items
5995 * themselves yet, so that it is possible to decrement refcount counters
5996 */
5997
Bram Moolenaarcd524592016-07-17 14:57:05 +02005998 /* Go through the list of dicts and free items without the copyID. */
5999 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006000
Bram Moolenaarda861d62016-07-17 15:46:27 +02006001 /* Go through the list of lists and free items without the copyID. */
6002 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006003
6004#ifdef FEAT_JOB_CHANNEL
6005 /* Go through the list of jobs and free items without the copyID. This
6006 * must happen before doing channels, because jobs refer to channels, but
6007 * the reference from the channel to the job isn't tracked. */
6008 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
6009
6010 /* Go through the list of channels and free items without the copyID. */
6011 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
6012#endif
6013
6014 /*
6015 * PASS 2: free the items themselves.
6016 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006017 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02006018 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006019
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006020#ifdef FEAT_JOB_CHANNEL
6021 /* Go through the list of jobs and free items without the copyID. This
6022 * must happen before doing channels, because jobs refer to channels, but
6023 * the reference from the channel to the job isn't tracked. */
6024 free_unused_jobs(copyID, COPYID_MASK);
6025
6026 /* Go through the list of channels and free items without the copyID. */
6027 free_unused_channels(copyID, COPYID_MASK);
6028#endif
6029
6030 in_free_unref_items = FALSE;
6031
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032 return did_free;
6033}
6034
6035/*
6036 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006037 * "list_stack" is used to add lists to be marked. Can be NULL.
6038 *
6039 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006040 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006043{
6044 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006045 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006047 hashtab_T *cur_ht;
6048 ht_stack_T *ht_stack = NULL;
6049 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006050
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006051 cur_ht = ht;
6052 for (;;)
6053 {
6054 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006056 /* Mark each item in the hashtab. If the item contains a hashtab
6057 * it is added to ht_stack, if it contains a list it is added to
6058 * list_stack. */
6059 todo = (int)cur_ht->ht_used;
6060 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6061 if (!HASHITEM_EMPTY(hi))
6062 {
6063 --todo;
6064 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6065 &ht_stack, list_stack);
6066 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006067 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006068
6069 if (ht_stack == NULL)
6070 break;
6071
6072 /* take an item from the stack */
6073 cur_ht = ht_stack->ht;
6074 tempitem = ht_stack;
6075 ht_stack = ht_stack->prev;
6076 free(tempitem);
6077 }
6078
6079 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006080}
6081
6082/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006083 * Mark a dict and its items with "copyID".
6084 * Returns TRUE if setting references failed somehow.
6085 */
6086 int
6087set_ref_in_dict(dict_T *d, int copyID)
6088{
6089 if (d != NULL && d->dv_copyID != copyID)
6090 {
6091 d->dv_copyID = copyID;
6092 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6093 }
6094 return FALSE;
6095}
6096
6097/*
6098 * Mark a list and its items with "copyID".
6099 * Returns TRUE if setting references failed somehow.
6100 */
6101 int
6102set_ref_in_list(list_T *ll, int copyID)
6103{
6104 if (ll != NULL && ll->lv_copyID != copyID)
6105 {
6106 ll->lv_copyID = copyID;
6107 return set_ref_in_list_items(ll, copyID, NULL);
6108 }
6109 return FALSE;
6110}
6111
6112/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006114 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6115 *
6116 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006117 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006118 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006119set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006121 listitem_T *li;
6122 int abort = FALSE;
6123 list_T *cur_l;
6124 list_stack_T *list_stack = NULL;
6125 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006126
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006127 cur_l = l;
6128 for (;;)
6129 {
6130 if (!abort)
6131 /* Mark each item in the list. If the item contains a hashtab
6132 * it is added to ht_stack, if it contains a list it is added to
6133 * list_stack. */
6134 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6135 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6136 ht_stack, &list_stack);
6137 if (list_stack == NULL)
6138 break;
6139
6140 /* take an item from the stack */
6141 cur_l = list_stack->list;
6142 tempitem = list_stack;
6143 list_stack = list_stack->prev;
6144 free(tempitem);
6145 }
6146
6147 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148}
6149
6150/*
6151 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006152 * "list_stack" is used to add lists to be marked. Can be NULL.
6153 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6154 *
6155 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006157 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006158set_ref_in_item(
6159 typval_T *tv,
6160 int copyID,
6161 ht_stack_T **ht_stack,
6162 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006163{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006164 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006165
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006166 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006167 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006168 dict_T *dd = tv->vval.v_dict;
6169
Bram Moolenaara03f2332016-02-06 18:09:59 +01006170 if (dd != NULL && dd->dv_copyID != copyID)
6171 {
6172 /* Didn't see this dict yet. */
6173 dd->dv_copyID = copyID;
6174 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006175 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006176 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6177 }
6178 else
6179 {
6180 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
6181 if (newitem == NULL)
6182 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006183 else
6184 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006185 newitem->ht = &dd->dv_hashtab;
6186 newitem->prev = *ht_stack;
6187 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006188 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006189 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006190 }
6191 }
6192 else if (tv->v_type == VAR_LIST)
6193 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006194 list_T *ll = tv->vval.v_list;
6195
Bram Moolenaara03f2332016-02-06 18:09:59 +01006196 if (ll != NULL && ll->lv_copyID != copyID)
6197 {
6198 /* Didn't see this list yet. */
6199 ll->lv_copyID = copyID;
6200 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006201 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006202 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006203 }
6204 else
6205 {
6206 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006207 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006208 if (newitem == NULL)
6209 abort = TRUE;
6210 else
6211 {
6212 newitem->list = ll;
6213 newitem->prev = *list_stack;
6214 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006215 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006216 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006217 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006218 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006219 else if (tv->v_type == VAR_FUNC)
6220 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006221 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006222 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006223 else if (tv->v_type == VAR_PARTIAL)
6224 {
6225 partial_T *pt = tv->vval.v_partial;
6226 int i;
6227
6228 /* A partial does not have a copyID, because it cannot contain itself.
6229 */
6230 if (pt != NULL)
6231 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006232 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006233
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006234 if (pt->pt_dict != NULL)
6235 {
6236 typval_T dtv;
6237
6238 dtv.v_type = VAR_DICT;
6239 dtv.vval.v_dict = pt->pt_dict;
6240 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6241 }
6242
6243 for (i = 0; i < pt->pt_argc; ++i)
6244 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6245 ht_stack, list_stack);
6246 }
6247 }
6248#ifdef FEAT_JOB_CHANNEL
6249 else if (tv->v_type == VAR_JOB)
6250 {
6251 job_T *job = tv->vval.v_job;
6252 typval_T dtv;
6253
6254 if (job != NULL && job->jv_copyID != copyID)
6255 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006256 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006257 if (job->jv_channel != NULL)
6258 {
6259 dtv.v_type = VAR_CHANNEL;
6260 dtv.vval.v_channel = job->jv_channel;
6261 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6262 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006263 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006264 {
6265 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006266 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006267 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6268 }
6269 }
6270 }
6271 else if (tv->v_type == VAR_CHANNEL)
6272 {
6273 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006274 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006275 typval_T dtv;
6276 jsonq_T *jq;
6277 cbq_T *cq;
6278
6279 if (ch != NULL && ch->ch_copyID != copyID)
6280 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006281 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006282 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006283 {
6284 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6285 jq = jq->jq_next)
6286 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6287 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6288 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006289 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006290 {
6291 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006292 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006293 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6294 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006295 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006296 {
6297 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006298 dtv.vval.v_partial =
6299 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006300 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6301 }
6302 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006303 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006304 {
6305 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006306 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006307 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6308 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006309 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006310 {
6311 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006312 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006313 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6314 }
6315 }
6316 }
6317#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006318 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006319}
6320
Bram Moolenaar17a13432016-01-24 14:22:10 +01006321 static char *
6322get_var_special_name(int nr)
6323{
6324 switch (nr)
6325 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006326 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006327 case VVAL_TRUE: return "v:true";
6328 case VVAL_NONE: return "v:none";
6329 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006330 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006331 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006332 return "42";
6333}
6334
Bram Moolenaar8c711452005-01-14 21:53:12 +00006335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 * Return a string with the string representation of a variable.
6337 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006339 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006340 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6341 * stings as "string()", otherwise does not put quotes around strings, as
6342 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006343 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6344 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006345 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006347 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006348echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349 typval_T *tv,
6350 char_u **tofree,
6351 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006352 int copyID,
6353 int echo_style,
6354 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006355 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006357 static int recurse = 0;
6358 char_u *r = NULL;
6359
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006361 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006362 if (!did_echo_string_emsg)
6363 {
6364 /* Only give this message once for a recursive call to avoid
6365 * flooding the user with errors. And stop iterating over lists
6366 * and dicts. */
6367 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006368 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006370 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006371 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 }
6373 ++recurse;
6374
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 switch (tv->v_type)
6376 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006377 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006378 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006379 {
6380 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006381 r = tv->vval.v_string;
6382 if (r == NULL)
6383 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006384 }
6385 else
6386 {
6387 *tofree = string_quote(tv->vval.v_string, FALSE);
6388 r = *tofree;
6389 }
6390 break;
6391
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006393 if (echo_style)
6394 {
6395 *tofree = NULL;
6396 r = tv->vval.v_string;
6397 }
6398 else
6399 {
6400 *tofree = string_quote(tv->vval.v_string, TRUE);
6401 r = *tofree;
6402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006404
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006405 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006406 {
6407 partial_T *pt = tv->vval.v_partial;
6408 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006409 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006410 garray_T ga;
6411 int i;
6412 char_u *tf;
6413
6414 ga_init2(&ga, 1, 100);
6415 ga_concat(&ga, (char_u *)"function(");
6416 if (fname != NULL)
6417 {
6418 ga_concat(&ga, fname);
6419 vim_free(fname);
6420 }
6421 if (pt != NULL && pt->pt_argc > 0)
6422 {
6423 ga_concat(&ga, (char_u *)", [");
6424 for (i = 0; i < pt->pt_argc; ++i)
6425 {
6426 if (i > 0)
6427 ga_concat(&ga, (char_u *)", ");
6428 ga_concat(&ga,
6429 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6430 vim_free(tf);
6431 }
6432 ga_concat(&ga, (char_u *)"]");
6433 }
6434 if (pt != NULL && pt->pt_dict != NULL)
6435 {
6436 typval_T dtv;
6437
6438 ga_concat(&ga, (char_u *)", ");
6439 dtv.v_type = VAR_DICT;
6440 dtv.vval.v_dict = pt->pt_dict;
6441 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6442 vim_free(tf);
6443 }
6444 ga_concat(&ga, (char_u *)")");
6445
6446 *tofree = ga.ga_data;
6447 r = *tofree;
6448 break;
6449 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006450
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006451 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006452 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006453 break;
6454
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006456 if (tv->vval.v_list == NULL)
6457 {
6458 *tofree = NULL;
6459 r = NULL;
6460 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006461 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6462 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006463 {
6464 *tofree = NULL;
6465 r = (char_u *)"[...]";
6466 }
6467 else
6468 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006469 int old_copyID = tv->vval.v_list->lv_copyID;
6470
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006471 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006472 *tofree = list2string(tv, copyID, restore_copyID);
6473 if (restore_copyID)
6474 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006475 r = *tofree;
6476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006478
Bram Moolenaar8c711452005-01-14 21:53:12 +00006479 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006480 if (tv->vval.v_dict == NULL)
6481 {
6482 *tofree = NULL;
6483 r = NULL;
6484 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006485 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6486 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 {
6488 *tofree = NULL;
6489 r = (char_u *)"{...}";
6490 }
6491 else
6492 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006493 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006494 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006495 *tofree = dict2string(tv, copyID, restore_copyID);
6496 if (restore_copyID)
6497 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006498 r = *tofree;
6499 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006500 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006502 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006503 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006504 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006505 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006506 break;
6507
Bram Moolenaar835dc632016-02-07 14:27:38 +01006508 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006509 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006511 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006512 if (composite_val)
6513 {
6514 *tofree = string_quote(r, FALSE);
6515 r = *tofree;
6516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006518
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006519 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006520#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006521 *tofree = NULL;
6522 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6523 r = numbuf;
6524 break;
6525#endif
6526
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006527 case VAR_SPECIAL:
6528 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006529 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006530 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006531 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006532
Bram Moolenaar8502c702014-06-17 12:51:16 +02006533 if (--recurse == 0)
6534 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536}
6537
6538/*
6539 * Return a string with the string representation of a variable.
6540 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6541 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006542 * Does not put quotes around strings, as ":echo" displays values.
6543 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6544 * May return NULL.
6545 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006546 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006547echo_string(
6548 typval_T *tv,
6549 char_u **tofree,
6550 char_u *numbuf,
6551 int copyID)
6552{
6553 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6554}
6555
6556/*
6557 * Return a string with the string representation of a variable.
6558 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6559 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006560 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006561 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006562 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006563 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006564tv2string(
6565 typval_T *tv,
6566 char_u **tofree,
6567 char_u *numbuf,
6568 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006569{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006570 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571}
6572
6573/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 * Return string "str" in ' quotes, doubling ' characters.
6575 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006576 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006578 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582 char_u *p, *r, *s;
6583
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 len = (function ? 13 : 3);
6585 if (str != NULL)
6586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006587 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006588 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 if (*p == '\'')
6590 ++len;
6591 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006592 s = r = alloc(len);
6593 if (r != NULL)
6594 {
6595 if (function)
6596 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006598 r += 10;
6599 }
6600 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 if (str != NULL)
6603 for (p = str; *p != NUL; )
6604 {
6605 if (*p == '\'')
6606 *r++ = '\'';
6607 MB_COPY_CHAR(p, r);
6608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006609 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006610 if (function)
6611 *r++ = ')';
6612 *r++ = NUL;
6613 }
6614 return s;
6615}
6616
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006617#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006618/*
6619 * Convert the string "text" to a floating point number.
6620 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6621 * this always uses a decimal point.
6622 * Returns the length of the text that was consumed.
6623 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006625string2float(
6626 char_u *text,
6627 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006628{
6629 char *s = (char *)text;
6630 float_T f;
6631
Bram Moolenaar62473612017-01-08 19:25:40 +01006632 /* MS-Windows does not deal with "inf" and "nan" properly. */
6633 if (STRNICMP(text, "inf", 3) == 0)
6634 {
6635 *value = INFINITY;
6636 return 3;
6637 }
6638 if (STRNICMP(text, "-inf", 3) == 0)
6639 {
6640 *value = -INFINITY;
6641 return 4;
6642 }
6643 if (STRNICMP(text, "nan", 3) == 0)
6644 {
6645 *value = NAN;
6646 return 3;
6647 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006648 f = strtod(s, &s);
6649 *value = f;
6650 return (int)((char_u *)s - text);
6651}
6652#endif
6653
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 * Get the value of an environment variable.
6656 * "arg" is pointing to the '$'. It is advanced to after the name.
6657 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006658 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 */
6660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 char_u *string = NULL;
6664 int len;
6665 int cc;
6666 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006667 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 ++*arg;
6670 name = *arg;
6671 len = get_env_len(arg);
6672 if (evaluate)
6673 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006674 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006675 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006676
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006677 cc = name[len];
6678 name[len] = NUL;
6679 /* first try vim_getenv(), fast for normal environment vars */
6680 string = vim_getenv(name, &mustfree);
6681 if (string != NULL && *string != NUL)
6682 {
6683 if (!mustfree)
6684 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006686 else
6687 {
6688 if (mustfree)
6689 vim_free(string);
6690
6691 /* next try expanding things like $VIM and ${HOME} */
6692 string = expand_env_save(name - 1);
6693 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006694 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006695 }
6696 name[len] = cc;
6697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006698 rettv->v_type = VAR_STRING;
6699 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701
6702 return OK;
6703}
6704
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006705
6706
6707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006709 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006711 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712var2fpos(
6713 typval_T *varp,
6714 int dollar_lnum, /* TRUE when $ is last line */
6715 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006717 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006719 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
Bram Moolenaara5525202006-03-02 22:52:09 +00006721 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006722 if (varp->v_type == VAR_LIST)
6723 {
6724 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006725 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006726 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006727 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006728
6729 l = varp->vval.v_list;
6730 if (l == NULL)
6731 return NULL;
6732
6733 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006734 pos.lnum = list_find_nr(l, 0L, &error);
6735 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006736 return NULL; /* invalid line number */
6737
6738 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006739 pos.col = list_find_nr(l, 1L, &error);
6740 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006741 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006742 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006743
6744 /* We accept "$" for the column number: last column. */
6745 li = list_find(l, 1L);
6746 if (li != NULL && li->li_tv.v_type == VAR_STRING
6747 && li->li_tv.vval.v_string != NULL
6748 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6749 pos.col = len + 1;
6750
Bram Moolenaara5525202006-03-02 22:52:09 +00006751 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006752 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006753 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006754 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006755
Bram Moolenaara5525202006-03-02 22:52:09 +00006756 /* Get the virtual offset. Defaults to zero. */
6757 pos.coladd = list_find_nr(l, 2L, &error);
6758 if (error)
6759 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006760
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006761 return &pos;
6762 }
6763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006764 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006765 if (name == NULL)
6766 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006767 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006769 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6770 {
6771 if (VIsual_active)
6772 return &VIsual;
6773 return &curwin->w_cursor;
6774 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006775 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006777 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6779 return NULL;
6780 return pp;
6781 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006782
Bram Moolenaara5525202006-03-02 22:52:09 +00006783 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006784
Bram Moolenaar477933c2007-07-17 14:32:23 +00006785 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006786 {
6787 pos.col = 0;
6788 if (name[1] == '0') /* "w0": first visible line */
6789 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006790 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006791 /* In silent Ex mode topline is zero, but that's not a valid line
6792 * number; use one instead. */
6793 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006794 return &pos;
6795 }
6796 else if (name[1] == '$') /* "w$": last visible line */
6797 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006798 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006799 /* In silent Ex mode botline is zero, return zero then. */
6800 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006801 return &pos;
6802 }
6803 }
6804 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006806 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 pos.lnum = curbuf->b_ml.ml_line_count;
6809 pos.col = 0;
6810 }
6811 else
6812 {
6813 pos.lnum = curwin->w_cursor.lnum;
6814 pos.col = (colnr_T)STRLEN(ml_get_curline());
6815 }
6816 return &pos;
6817 }
6818 return NULL;
6819}
6820
6821/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006822 * Convert list in "arg" into a position and optional file number.
6823 * When "fnump" is NULL there is no file number, only 3 items.
6824 * Note that the column is passed on as-is, the caller may want to decrement
6825 * it to use 1 for the first column.
6826 * Return FAIL when conversion is not possible, doesn't check the position for
6827 * validity.
6828 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006830list2fpos(
6831 typval_T *arg,
6832 pos_T *posp,
6833 int *fnump,
6834 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006835{
6836 list_T *l = arg->vval.v_list;
6837 long i = 0;
6838 long n;
6839
Bram Moolenaar493c1782014-05-28 14:34:46 +02006840 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6841 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006842 if (arg->v_type != VAR_LIST
6843 || l == NULL
6844 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006845 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006846 return FAIL;
6847
6848 if (fnump != NULL)
6849 {
6850 n = list_find_nr(l, i++, NULL); /* fnum */
6851 if (n < 0)
6852 return FAIL;
6853 if (n == 0)
6854 n = curbuf->b_fnum; /* current buffer */
6855 *fnump = n;
6856 }
6857
6858 n = list_find_nr(l, i++, NULL); /* lnum */
6859 if (n < 0)
6860 return FAIL;
6861 posp->lnum = n;
6862
6863 n = list_find_nr(l, i++, NULL); /* col */
6864 if (n < 0)
6865 return FAIL;
6866 posp->col = n;
6867
Bram Moolenaar493c1782014-05-28 14:34:46 +02006868 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006869 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006870 posp->coladd = 0;
6871 else
6872 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006873
Bram Moolenaar493c1782014-05-28 14:34:46 +02006874 if (curswantp != NULL)
6875 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6876
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006877 return OK;
6878}
6879
6880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 * Get the length of an environment variable name.
6882 * Advance "arg" to the first character after the name.
6883 * Return 0 for error.
6884 */
6885 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 char_u *p;
6889 int len;
6890
6891 for (p = *arg; vim_isIDc(*p); ++p)
6892 ;
6893 if (p == *arg) /* no name found */
6894 return 0;
6895
6896 len = (int)(p - *arg);
6897 *arg = p;
6898 return len;
6899}
6900
6901/*
6902 * Get the length of the name of a function or internal variable.
6903 * "arg" is advanced to the first non-white character after the name.
6904 * Return 0 if something is wrong.
6905 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006907get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908{
6909 char_u *p;
6910 int len;
6911
6912 /* Find the end of the name. */
6913 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006914 {
6915 if (*p == ':')
6916 {
6917 /* "s:" is start of "s:var", but "n:" is not and can be used in
6918 * slice "[n:]". Also "xx:" is not a namespace. */
6919 len = (int)(p - *arg);
6920 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6921 || len > 1)
6922 break;
6923 }
6924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 if (p == *arg) /* no name found */
6926 return 0;
6927
6928 len = (int)(p - *arg);
6929 *arg = skipwhite(p);
6930
6931 return len;
6932}
6933
6934/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006935 * Get the length of the name of a variable or function.
6936 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006938 * Return -1 if curly braces expansion failed.
6939 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 * If the name contains 'magic' {}'s, expand them and return the
6941 * expanded name in an allocated string via 'alias' - caller must free.
6942 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006944get_name_len(
6945 char_u **arg,
6946 char_u **alias,
6947 int evaluate,
6948 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 char_u *p;
6952 char_u *expr_start;
6953 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 *alias = NULL; /* default to no alias */
6956
6957 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6958 && (*arg)[2] == (int)KE_SNR)
6959 {
6960 /* hard coded <SNR>, already translated */
6961 *arg += 3;
6962 return get_id_len(arg) + 3;
6963 }
6964 len = eval_fname_script(*arg);
6965 if (len > 0)
6966 {
6967 /* literal "<SID>", "s:" or "<SNR>" */
6968 *arg += len;
6969 }
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006972 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006974 p = find_name_end(*arg, &expr_start, &expr_end,
6975 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (expr_start != NULL)
6977 {
6978 char_u *temp_string;
6979
6980 if (!evaluate)
6981 {
6982 len += (int)(p - *arg);
6983 *arg = skipwhite(p);
6984 return len;
6985 }
6986
6987 /*
6988 * Include any <SID> etc in the expanded string:
6989 * Thus the -len here.
6990 */
6991 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6992 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006993 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 *alias = temp_string;
6995 *arg = skipwhite(p);
6996 return (int)STRLEN(temp_string);
6997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
6999 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01007000 // Only give an error when there is something, otherwise it will be
7001 // reported at a higher level.
7002 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007003 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 return len;
7006}
7007
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007008/*
7009 * Find the end of a variable or function name, taking care of magic braces.
7010 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
7011 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007012 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007013 * Return a pointer to just after the name. Equal to "arg" if there is no
7014 * valid name.
7015 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007017find_name_end(
7018 char_u *arg,
7019 char_u **expr_start,
7020 char_u **expr_end,
7021 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007023 int mb_nest = 0;
7024 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007026 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007028 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030 *expr_start = NULL;
7031 *expr_end = NULL;
7032 }
7033
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007034 /* Quick check for valid starting character. */
7035 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
7036 return arg;
7037
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 for (p = arg; *p != NUL
7039 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007041 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007042 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007043 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007044 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007045 if (*p == '\'')
7046 {
7047 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007048 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007049 ;
7050 if (*p == NUL)
7051 break;
7052 }
7053 else if (*p == '"')
7054 {
7055 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007056 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007057 if (*p == '\\' && p[1] != NUL)
7058 ++p;
7059 if (*p == NUL)
7060 break;
7061 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007062 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7063 {
7064 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007065 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007066 len = (int)(p - arg);
7067 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007068 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007069 break;
7070 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007071
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007074 if (*p == '[')
7075 ++br_nest;
7076 else if (*p == ']')
7077 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007080 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007082 if (*p == '{')
7083 {
7084 mb_nest++;
7085 if (expr_start != NULL && *expr_start == NULL)
7086 *expr_start = p;
7087 }
7088 else if (*p == '}')
7089 {
7090 mb_nest--;
7091 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7092 *expr_end = p;
7093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 }
7096
7097 return p;
7098}
7099
7100/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007101 * Expands out the 'magic' {}'s in a variable/function name.
7102 * Note that this can call itself recursively, to deal with
7103 * constructs like foo{bar}{baz}{bam}
7104 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7105 * "in_start" ^
7106 * "expr_start" ^
7107 * "expr_end" ^
7108 * "in_end" ^
7109 *
7110 * Returns a new allocated string, which the caller must free.
7111 * Returns NULL for failure.
7112 */
7113 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007114make_expanded_name(
7115 char_u *in_start,
7116 char_u *expr_start,
7117 char_u *expr_end,
7118 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007119{
7120 char_u c1;
7121 char_u *retval = NULL;
7122 char_u *temp_result;
7123 char_u *nextcmd = NULL;
7124
7125 if (expr_end == NULL || in_end == NULL)
7126 return NULL;
7127 *expr_start = NUL;
7128 *expr_end = NUL;
7129 c1 = *in_end;
7130 *in_end = NUL;
7131
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007132 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007133 if (temp_result != NULL && nextcmd == NULL)
7134 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007135 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7136 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007137 if (retval != NULL)
7138 {
7139 STRCPY(retval, in_start);
7140 STRCAT(retval, temp_result);
7141 STRCAT(retval, expr_end + 1);
7142 }
7143 }
7144 vim_free(temp_result);
7145
7146 *in_end = c1; /* put char back for error messages */
7147 *expr_start = '{';
7148 *expr_end = '}';
7149
7150 if (retval != NULL)
7151 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007152 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007153 if (expr_start != NULL)
7154 {
7155 /* Further expansion! */
7156 temp_result = make_expanded_name(retval, expr_start,
7157 expr_end, temp_result);
7158 vim_free(retval);
7159 retval = temp_result;
7160 }
7161 }
7162
7163 return retval;
7164}
7165
7166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007168 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007171eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007173 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
7174}
7175
7176/*
7177 * Return TRUE if character "c" can be used as the first character in a
7178 * variable or function name (excluding '{' and '}').
7179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007181eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007182{
7183 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184}
7185
7186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 * Set number v: variable to "val".
7188 */
7189 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007190set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007196 * Get typval_T v: variable value.
7197 */
7198 typval_T *
7199get_vim_var_tv(int idx)
7200{
7201 return &vimvars[idx].vv_tv;
7202}
7203
7204/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007205 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007207 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211}
7212
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007213/*
7214 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007215 * If the String variable has never been set, return an empty string.
7216 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007217 */
7218 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007220{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007221 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007222}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007225 * Get List v: variable value. Caller must take care of reference count when
7226 * needed.
7227 */
7228 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007229get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007230{
7231 return vimvars[idx].vv_list;
7232}
7233
7234/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007235 * Get Dict v: variable value. Caller must take care of reference count when
7236 * needed.
7237 */
7238 dict_T *
7239get_vim_var_dict(int idx)
7240{
7241 return vimvars[idx].vv_dict;
7242}
7243
7244/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007245 * Set v:char to character "c".
7246 */
7247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007249{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007250 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007251
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007252 if (has_mbyte)
7253 buf[(*mb_char2bytes)(c, buf)] = NUL;
7254 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007255 {
7256 buf[0] = c;
7257 buf[1] = NUL;
7258 }
7259 set_vim_var_string(VV_CHAR, buf, -1);
7260}
7261
7262/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007263 * Set v:count to "count" and v:count1 to "count1".
7264 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 */
7266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267set_vcount(
7268 long count,
7269 long count1,
7270 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007272 if (set_prevcount)
7273 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 vimvars[VV_COUNT].vv_nr = count;
7275 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007279 * Save variables that might be changed as a side effect. Used when executing
7280 * a timer callback.
7281 */
7282 void
7283save_vimvars(vimvars_save_T *vvsave)
7284{
7285 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7286 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7287 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7288}
7289
7290/*
7291 * Restore variables saved by save_vimvars().
7292 */
7293 void
7294restore_vimvars(vimvars_save_T *vvsave)
7295{
7296 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7297 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7298 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7299}
7300
7301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 * Set string v: variable to a copy of "val".
7303 */
7304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305set_vim_var_string(
7306 int idx,
7307 char_u *val,
7308 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
Bram Moolenaara542c682016-01-31 16:28:04 +01007310 clear_tv(&vimvars[idx].vv_di.di_tv);
7311 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
7320/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007321 * Set List v: variable to "val".
7322 */
7323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007324set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007325{
Bram Moolenaara542c682016-01-31 16:28:04 +01007326 clear_tv(&vimvars[idx].vv_di.di_tv);
7327 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007328 vimvars[idx].vv_list = val;
7329 if (val != NULL)
7330 ++val->lv_refcount;
7331}
7332
7333/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007334 * Set Dictionary v: variable to "val".
7335 */
7336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007337set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007338{
Bram Moolenaara542c682016-01-31 16:28:04 +01007339 clear_tv(&vimvars[idx].vv_di.di_tv);
7340 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007341 vimvars[idx].vv_dict = val;
7342 if (val != NULL)
7343 {
7344 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007345 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007346 }
7347}
7348
7349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 * Set v:register if needed.
7351 */
7352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
7355 char_u regname;
7356
7357 if (c == 0 || c == ' ')
7358 regname = '"';
7359 else
7360 regname = c;
7361 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 set_vim_var_string(VV_REG, &regname, 1);
7364}
7365
7366/*
7367 * Get or set v:exception. If "oldval" == NULL, return the current value.
7368 * Otherwise, restore the value to "oldval" and return NULL.
7369 * Must always be called in pairs to save and restore v:exception! Does not
7370 * take care of memory allocations.
7371 */
7372 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007373v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
7375 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 return NULL;
7380}
7381
7382/*
7383 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7384 * Otherwise, restore the value to "oldval" and return NULL.
7385 * Must always be called in pairs to save and restore v:throwpoint! Does not
7386 * take care of memory allocations.
7387 */
7388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 return NULL;
7396}
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398/*
7399 * Set v:cmdarg.
7400 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7401 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7402 * Must always be called in pairs!
7403 */
7404 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007405set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
7407 char_u *oldval;
7408 char_u *newval;
7409 unsigned len;
7410
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007412 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007414 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
7418
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007419 if (eap->force_bin == FORCE_BIN)
7420 len = 6;
7421 else if (eap->force_bin == FORCE_NOBIN)
7422 len = 8;
7423 else
7424 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007425
7426 if (eap->read_edit)
7427 len += 7;
7428
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007429 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007430 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007431 if (eap->force_enc != 0)
7432 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007433 if (eap->bad_char != 0)
7434 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007435
7436 newval = alloc(len + 1);
7437 if (newval == NULL)
7438 return NULL;
7439
7440 if (eap->force_bin == FORCE_BIN)
7441 sprintf((char *)newval, " ++bin");
7442 else if (eap->force_bin == FORCE_NOBIN)
7443 sprintf((char *)newval, " ++nobin");
7444 else
7445 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446
7447 if (eap->read_edit)
7448 STRCAT(newval, " ++edit");
7449
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007450 if (eap->force_ff != 0)
7451 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007452 eap->force_ff == 'u' ? "unix"
7453 : eap->force_ff == 'd' ? "dos"
7454 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007455 if (eap->force_enc != 0)
7456 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7457 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007458 if (eap->bad_char == BAD_KEEP)
7459 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7460 else if (eap->bad_char == BAD_DROP)
7461 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7462 else if (eap->bad_char != 0)
7463 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007465 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468/*
7469 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007470 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473get_var_tv(
7474 char_u *name,
7475 int len, /* length of "name" */
7476 typval_T *rettv, /* NULL when only checking existence */
7477 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7478 int verbose, /* may give error message */
7479 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486 /* truncate the name, so that we can use strcmp() */
7487 cc = name[len];
7488 name[len] = NUL;
7489
7490 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 * Check for user-defined variables.
7492 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007493 v = find_var(name, NULL, no_autoload);
7494 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007496 tv = &v->di_tv;
7497 if (dip != NULL)
7498 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007503 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 ret = FAIL;
7506 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
7510 name[len] = cc;
7511
7512 return ret;
7513}
7514
7515/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007516 * Check if variable "name[len]" is a local variable or an argument.
7517 * If so, "*eval_lavars_used" is set to TRUE.
7518 */
7519 static void
7520check_vars(char_u *name, int len)
7521{
7522 int cc;
7523 char_u *varname;
7524 hashtab_T *ht;
7525
7526 if (eval_lavars_used == NULL)
7527 return;
7528
7529 /* truncate the name, so that we can use strcmp() */
7530 cc = name[len];
7531 name[len] = NUL;
7532
7533 ht = find_var_ht(name, &varname);
7534 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7535 {
7536 if (find_var(name, NULL, TRUE) != NULL)
7537 *eval_lavars_used = TRUE;
7538 }
7539
7540 name[len] = cc;
7541}
7542
7543/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007544 * Handle:
7545 * - expr[expr], expr[expr:expr] subscript
7546 * - ".name" lookup
7547 * - function call with Funcref variable: func(expr)
7548 * - method call: var->method()
7549 *
7550 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007551 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007553handle_subscript(
7554 char_u **arg,
7555 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007556 int evaluate, // do more than finding the end
7557 int verbose, // give error messages
7558 char_u *start_leader, // start of '!' and '-' prefixes
7559 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007560{
7561 int ret = OK;
7562 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007563
Bram Moolenaar61343f02019-07-20 21:11:13 +02007564 // "." is ".name" lookup when we found a dict or when evaluating and
7565 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007566 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02007567 && (((**arg == '['
7568 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02007569 || (!evaluate
7570 && (*arg)[1] != '.'
7571 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007572 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007573 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007574 && !VIM_ISWHITE(*(*arg - 1)))
7575 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007576 {
7577 if (**arg == '(')
7578 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007579 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007580
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007581 // Stop the expression evaluation when immediately aborting on
7582 // error, or when an interrupt occurred or an exception was thrown
7583 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007584 if (aborting())
7585 {
7586 if (ret == OK)
7587 clear_tv(rettv);
7588 ret = FAIL;
7589 }
7590 dict_unref(selfdict);
7591 selfdict = NULL;
7592 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007593 else if (**arg == '-')
7594 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007595 // Expression "-1.0->method()" applies the leader "-" before
7596 // applying ->.
7597 if (evaluate && *end_leaderp > start_leader)
7598 ret = eval7_leader(rettv, start_leader, end_leaderp);
7599 if (ret == OK)
7600 {
7601 if ((*arg)[2] == '{')
7602 // expr->{lambda}()
7603 ret = eval_lambda(arg, rettv, evaluate, verbose);
7604 else
7605 // expr->name()
7606 ret = eval_method(arg, rettv, evaluate, verbose);
7607 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007609 else /* **arg == '[' || **arg == '.' */
7610 {
7611 dict_unref(selfdict);
7612 if (rettv->v_type == VAR_DICT)
7613 {
7614 selfdict = rettv->vval.v_dict;
7615 if (selfdict != NULL)
7616 ++selfdict->dv_refcount;
7617 }
7618 else
7619 selfdict = NULL;
7620 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7621 {
7622 clear_tv(rettv);
7623 ret = FAIL;
7624 }
7625 }
7626 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007627
Bram Moolenaar1d429612016-05-24 15:44:17 +02007628 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7629 * Don't do this when "Func" is already a partial that was bound
7630 * explicitly (pt_auto is FALSE). */
7631 if (selfdict != NULL
7632 && (rettv->v_type == VAR_FUNC
7633 || (rettv->v_type == VAR_PARTIAL
7634 && (rettv->vval.v_partial->pt_auto
7635 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007636 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007637
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007638 dict_unref(selfdict);
7639 return ret;
7640}
7641
7642/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007643 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 * value).
7645 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007646 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007647alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007648{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007649 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650}
7651
7652/*
7653 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 * The string "s" must have been allocated, it is consumed.
7655 * Return NULL for out of memory, the variable otherwise.
7656 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007658alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 rettv = alloc_tv();
7663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->v_type = VAR_STRING;
7666 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
7668 else
7669 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671}
7672
7673/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007677free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 if (varp != NULL)
7680 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007681 switch (varp->v_type)
7682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007684 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007685 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007686 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 vim_free(varp->vval.v_string);
7688 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007689 case VAR_PARTIAL:
7690 partial_unref(varp->vval.v_partial);
7691 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007692 case VAR_BLOB:
7693 blob_unref(varp->vval.v_blob);
7694 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 case VAR_LIST:
7696 list_unref(varp->vval.v_list);
7697 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007698 case VAR_DICT:
7699 dict_unref(varp->vval.v_dict);
7700 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007701 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007702#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007703 job_unref(varp->vval.v_job);
7704 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007705#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007706 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007707#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007708 channel_unref(varp->vval.v_channel);
7709 break;
7710#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007711 case VAR_NUMBER:
7712 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007713 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007714 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007715 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 vim_free(varp);
7718 }
7719}
7720
7721/*
7722 * Free the memory for a variable value and set the value to NULL or 0.
7723 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007724 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007725clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 if (varp != NULL)
7728 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007731 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007732 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007733 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007734 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007735 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007736 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007737 case VAR_PARTIAL:
7738 partial_unref(varp->vval.v_partial);
7739 varp->vval.v_partial = NULL;
7740 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007741 case VAR_BLOB:
7742 blob_unref(varp->vval.v_blob);
7743 varp->vval.v_blob = NULL;
7744 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 case VAR_LIST:
7746 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749 case VAR_DICT:
7750 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007754 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007755 varp->vval.v_number = 0;
7756 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007757 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007759 varp->vval.v_float = 0.0;
7760 break;
7761#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007762 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007763#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007764 job_unref(varp->vval.v_job);
7765 varp->vval.v_job = NULL;
7766#endif
7767 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007768 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007770 channel_unref(varp->vval.v_channel);
7771 varp->vval.v_channel = NULL;
7772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007773 case VAR_UNKNOWN:
7774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007776 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
7778}
7779
7780/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 * Set the value of a variable to NULL without freeing items.
7782 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007784init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785{
7786 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788}
7789
7790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 * Get the number value of a variable.
7792 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007793 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007794 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007795 * caller of incompatible types: it sets *denote to TRUE if "denote"
7796 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007798 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007799tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 int error = FALSE;
7802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007804}
7805
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007806 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007807tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007808{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007809 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007814 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007817 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007818 break;
7819#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007821 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007822 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823 break;
7824 case VAR_STRING:
7825 if (varp->vval.v_string != NULL)
7826 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007827 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007828 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007829 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007830 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007831 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007832 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007833 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007834 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007835 case VAR_SPECIAL:
7836 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7837 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007838 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007840 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007841 break;
7842#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007843 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007845 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007846 break;
7847#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007848 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007849 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007850 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007851 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007852 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 if (denote == NULL) /* useful for values that must be unsigned */
7856 n = -1;
7857 else
7858 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860}
7861
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007862#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007864tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007865{
7866 switch (varp->v_type)
7867 {
7868 case VAR_NUMBER:
7869 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007870 case VAR_FLOAT:
7871 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007872 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007873 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007874 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007875 break;
7876 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007877 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007878 break;
7879 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007880 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007881 break;
7882 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007883 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007884 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007885 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007886 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007887 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007888 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007889# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007890 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007891 break;
7892# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007893 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007894# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007895 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007896 break;
7897# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007898 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007899 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007900 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007901 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007902 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007903 break;
7904 }
7905 return 0;
7906}
7907#endif
7908
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 * Get the string value of a variable.
7911 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007912 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7913 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 * If the String variable has never been set, return an empty string.
7915 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007916 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007917 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007919 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007920tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
7922 static char_u mybuf[NUMBUFLEN];
7923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007924 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
7926
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007927 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007928tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007930 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007931
7932 return res != NULL ? res : (char_u *)"";
7933}
7934
Bram Moolenaar7d647822014-04-05 21:28:56 +02007935/*
7936 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7937 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007938 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007939tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007940{
7941 static char_u mybuf[NUMBUFLEN];
7942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007943 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944}
7945
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007946 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007948{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007952 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007953 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007954 return buf;
7955 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007956 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007957 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 break;
7959 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007960 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 break;
7962 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007963 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007966#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007967 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007968 break;
7969#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 case VAR_STRING:
7971 if (varp->vval.v_string != NULL)
7972 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007973 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007974 case VAR_SPECIAL:
7975 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7976 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007977 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007978 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007979 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007980 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007981#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007982 {
7983 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007984 char *status;
7985
7986 if (job == NULL)
7987 return (char_u *)"no process";
7988 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007989 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007990 : "run";
7991# ifdef UNIX
7992 vim_snprintf((char *)buf, NUMBUFLEN,
7993 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007994# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007995 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007996 "process %ld %s",
7997 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007998 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007999# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01008000 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01008001 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
8002# endif
8003 return buf;
8004 }
8005#endif
8006 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01008007 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008008#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008009 {
8010 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02008011 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01008012
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008013 if (channel == NULL)
8014 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
8015 else
8016 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01008017 "channel %d %s", channel->ch_id, status);
8018 return buf;
8019 }
8020#endif
8021 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008022 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008023 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027}
8028
8029/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008030 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
8031 * string() on Dict, List, etc.
8032 */
8033 char_u *
8034tv_stringify(typval_T *varp, char_u *buf)
8035{
8036 if (varp->v_type == VAR_LIST
8037 || varp->v_type == VAR_DICT
8038 || varp->v_type == VAR_FUNC
8039 || varp->v_type == VAR_PARTIAL
8040 || varp->v_type == VAR_FLOAT)
8041 {
8042 typval_T tmp;
8043
8044 f_string(varp, &tmp);
8045 tv_get_string_buf(&tmp, buf);
8046 clear_tv(varp);
8047 *varp = tmp;
8048 return tmp.vval.v_string;
8049 }
8050 return tv_get_string_buf(varp, buf);
8051}
8052
8053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 * Find variable "name" in the list of variables.
8055 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008056 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00008057 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008060 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008061find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008065 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
Bram Moolenaara7043832005-01-21 11:56:39 +00008067 ht = find_var_ht(name, &varname);
8068 if (htp != NULL)
8069 *htp = ht;
8070 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008072 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
8073 if (ret != NULL)
8074 return ret;
8075
8076 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008077 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078}
8079
8080/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02008081 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00008082 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085find_var_in_ht(
8086 hashtab_T *ht,
8087 int htname,
8088 char_u *varname,
8089 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00008090{
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 hashitem_T *hi;
8092
8093 if (*varname == NUL)
8094 {
8095 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02008096 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008098 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 case 'g': return &globvars_var;
8100 case 'v': return &vimvars_var;
8101 case 'b': return &curbuf->b_bufvar;
8102 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008103 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008104 case 'l': return get_funccal_local_var();
8105 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00008106 }
8107 return NULL;
8108 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008109
8110 hi = hash_find(ht, varname);
8111 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008112 {
8113 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008114 * worked find the variable again. Don't auto-load a script if it was
8115 * loaded already, otherwise it would be loaded every time when
8116 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008117 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008118 {
8119 /* Note: script_autoload() may make "hi" invalid. It must either
8120 * be obtained again or not used. */
8121 if (!script_autoload(varname, FALSE) || aborting())
8122 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008123 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008124 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008125 if (HASHITEM_EMPTY(hi))
8126 return NULL;
8127 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008129}
8130
8131/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02008133 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00008134 * Set "varname" to the start of name without ':'.
8135 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008136 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008137find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008139 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008140 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008141
Bram Moolenaar73627d02015-08-11 15:46:09 +02008142 if (name[0] == NUL)
8143 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 if (name[1] != ':')
8145 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008146 /* The name must not start with a colon or #. */
8147 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 return NULL;
8149 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00008150
Bram Moolenaard2e716e2019-04-20 14:39:52 +02008151 // "version" is "v:version" in all scopes if scriptversion < 3.
8152 // Same for a few other variables marked with VV_COMPAT.
8153 if (current_sctx.sc_version < 3)
8154 {
8155 hi = hash_find(&compat_hashtab, name);
8156 if (!HASHITEM_EMPTY(hi))
8157 return &compat_hashtab;
8158 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00008159
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008160 ht = get_funccal_local_ht();
8161 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008163 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008166 if (*name == 'g') /* global variable */
8167 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02008168 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008169 if (vim_strchr(name + 2, ':') != NULL
8170 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008171 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008173 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008175 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008176 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008177 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00008178 if (*name == 'v') /* v: variable */
8179 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008180 if (*name == 'a') /* a: function argument */
8181 return get_funccal_args_ht();
8182 if (*name == 'l') /* l: local function variable */
8183 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008185 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
8186 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 return NULL;
8188}
8189
8190/*
8191 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008192 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 * Returns NULL when it doesn't exist.
8194 */
8195 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008196get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008200 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (v == NULL)
8202 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008203 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205
8206/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 * sourcing this script and when executing functions defined in the script.
8209 */
8210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008211new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
Bram Moolenaara7043832005-01-21 11:56:39 +00008213 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008214 hashtab_T *ht;
8215 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8218 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008219 /* Re-allocating ga_data means that an ht_array pointing to
8220 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008222 for (i = 1; i <= ga_scripts.ga_len; ++i)
8223 {
8224 ht = &SCRIPT_VARS(i);
8225 if (ht->ht_mask == HT_INIT_SIZE - 1)
8226 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008227 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008229 }
8230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 while (ga_scripts.ga_len < id)
8232 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008233 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008234 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008235 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
8238 }
8239}
8240
8241/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008242 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8243 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008246init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
Bram Moolenaar33570922005-01-25 22:26:29 +00008248 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008249 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008250 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008251 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008252 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 dict_var->di_tv.vval.v_dict = dict;
8254 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008255 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8257 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258}
8259
8260/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008261 * Unreference a dictionary initialized by init_var_dict().
8262 */
8263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008264unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008265{
8266 /* Now the dict needs to be freed if no one else is using it, go back to
8267 * normal reference counting. */
8268 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8269 dict_unref(dict);
8270}
8271
8272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 * Frees all allocated variables and the value they contain.
8275 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 */
8277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008278vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008279{
8280 vars_clear_ext(ht, TRUE);
8281}
8282
8283/*
8284 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8285 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008287vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
Bram Moolenaara7043832005-01-21 11:56:39 +00008289 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008290 hashitem_T *hi;
8291 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008294 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008295 for (hi = ht->ht_array; todo > 0; ++hi)
8296 {
8297 if (!HASHITEM_EMPTY(hi))
8298 {
8299 --todo;
8300
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008302 * ht_array might change then. hash_clear() takes care of it
8303 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 v = HI2DI(hi);
8305 if (free_val)
8306 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008307 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008309 }
8310 }
8311 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008312 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
Bram Moolenaara7043832005-01-21 11:56:39 +00008315/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 * Delete a variable from hashtab "ht" at item "hi".
8317 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008320delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008323
8324 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 clear_tv(&di->di_tv);
8326 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327}
8328
8329/*
8330 * List the value of one internal variable.
8331 */
8332 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008333list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008335 char_u *tofree;
8336 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008338
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008339 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008341 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008342 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343}
8344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008346list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008347 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008348 char_u *name,
8349 int type,
8350 char_u *string,
8351 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
Bram Moolenaar31859182007-08-14 20:41:13 +00008353 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8354 msg_start();
8355 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008357 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 msg_putchar(' ');
8359 msg_advance(22);
8360 if (type == VAR_NUMBER)
8361 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008362 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 msg_putchar('*');
8364 else if (type == VAR_LIST)
8365 {
8366 msg_putchar('[');
8367 if (*string == '[')
8368 ++string;
8369 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008370 else if (type == VAR_DICT)
8371 {
8372 msg_putchar('{');
8373 if (*string == '{')
8374 ++string;
8375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 else
8377 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008380
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008381 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008382 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008383 if (*first)
8384 {
8385 msg_clr_eos();
8386 *first = FALSE;
8387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388}
8389
8390/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 * If the variable already exists, the value is updated.
8393 * Otherwise the variable is created.
8394 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008396set_var(
8397 char_u *name,
8398 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008399 int copy) // make copy of value in "tv"
8400{
8401 set_var_const(name, tv, copy, FALSE);
8402}
8403
8404/*
8405 * Set variable "name" to value in "tv".
8406 * If the variable already exists and "is_const" is FALSE the value is updated.
8407 * Otherwise the variable is created.
8408 */
8409 static void
8410set_var_const(
8411 char_u *name,
8412 typval_T *tv,
8413 int copy, // make copy of value in "tv"
8414 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008420 ht = find_var_ht(name, &varname);
8421 if (ht == NULL || *varname == NUL)
8422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008423 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008424 return;
8425 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008426 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008427
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008428 /* Search in parent scope which is possible to reference from lambda */
8429 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008430 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008431
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008432 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8433 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008434 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008435
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008438 if (is_const)
8439 {
8440 emsg(_(e_cannot_mod));
8441 return;
8442 }
8443
Bram Moolenaar33570922005-01-25 22:26:29 +00008444 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008445 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008446 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008447 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008448
8449 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008450 * Handle setting internal v: variables separately where needed to
8451 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 */
8453 if (ht == &vimvarht)
8454 {
8455 if (v->di_tv.v_type == VAR_STRING)
8456 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008457 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008459 {
8460 char_u *val = tv_get_string(tv);
8461
8462 // Careful: when assigning to v:errmsg and tv_get_string()
8463 // causes an error message the variable will alrady be set.
8464 if (v->di_tv.vval.v_string == NULL)
8465 v->di_tv.vval.v_string = vim_strsave(val);
8466 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008467 else
8468 {
8469 /* Take over the string to avoid an extra alloc/free. */
8470 v->di_tv.vval.v_string = tv->vval.v_string;
8471 tv->vval.v_string = NULL;
8472 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008473 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008475 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008476 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008477 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478 if (STRCMP(varname, "searchforward") == 0)
8479 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008480#ifdef FEAT_SEARCH_EXTRA
8481 else if (STRCMP(varname, "hlsearch") == 0)
8482 {
8483 no_hlsearch = !v->di_tv.vval.v_number;
8484 redraw_all_later(SOME_VALID);
8485 }
8486#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008487 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008489 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008491 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008492 return;
8493 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 }
8495
8496 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498 else /* add a new variable */
8499 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008500 // Can't add "v:" or "a:" variable.
8501 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008503 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008504 return;
8505 }
8506
Bram Moolenaar31b81602019-02-10 22:14:27 +01008507 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008508 if (!valid_varname(varname))
8509 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008510
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008511 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008512 if (v == NULL)
8513 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008517 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 return;
8519 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008520 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008521 if (is_const)
8522 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008524
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008525 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008526 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008527 else
8528 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008530 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008532 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008533
8534 if (is_const)
8535 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536}
8537
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008539 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 * Also give an error message.
8541 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008543var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008544{
8545 if (flags & DI_FLAGS_RO)
8546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008547 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 return TRUE;
8549 }
8550 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008552 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 return TRUE;
8554 }
8555 return FALSE;
8556}
8557
8558/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008559 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8560 * Also give an error message.
8561 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008563var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008564{
8565 if (flags & DI_FLAGS_FIX)
8566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008567 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008568 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008569 return TRUE;
8570 }
8571 return FALSE;
8572}
8573
8574/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008575 * Check if a funcref is assigned to a valid variable name.
8576 * Return TRUE and give an error if not.
8577 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008579var_check_func_name(
8580 char_u *name, /* points to start of variable name */
8581 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008582{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008583 /* Allow for w: b: s: and t:. */
8584 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008585 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8586 ? name[2] : name[0]))
8587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008588 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008589 name);
8590 return TRUE;
8591 }
8592 /* Don't allow hiding a function. When "v" is not NULL we might be
8593 * assigning another function to the same var, the type is checked
8594 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008595 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008597 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008598 name);
8599 return TRUE;
8600 }
8601 return FALSE;
8602}
8603
8604/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008605 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008606 * Also give an error message, using "name" or _("name") when use_gettext is
8607 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008608 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008609 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008610var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008611{
8612 if (lock & VAR_LOCKED)
8613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008614 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008615 name == NULL ? (char_u *)_("Unknown")
8616 : use_gettext ? (char_u *)_(name)
8617 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008618 return TRUE;
8619 }
8620 if (lock & VAR_FIXED)
8621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008622 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008623 name == NULL ? (char_u *)_("Unknown")
8624 : use_gettext ? (char_u *)_(name)
8625 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008626 return TRUE;
8627 }
8628 return FALSE;
8629}
8630
8631/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008632 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8633 * Also give an error message, using "name" or _("name") when use_gettext is
8634 * TRUE.
8635 */
8636 static int
8637tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8638{
8639 int lock = 0;
8640
8641 switch (tv->v_type)
8642 {
8643 case VAR_BLOB:
8644 if (tv->vval.v_blob != NULL)
8645 lock = tv->vval.v_blob->bv_lock;
8646 break;
8647 case VAR_LIST:
8648 if (tv->vval.v_list != NULL)
8649 lock = tv->vval.v_list->lv_lock;
8650 break;
8651 case VAR_DICT:
8652 if (tv->vval.v_dict != NULL)
8653 lock = tv->vval.v_dict->dv_lock;
8654 break;
8655 default:
8656 break;
8657 }
8658 return var_check_lock(tv->v_lock, name, use_gettext)
8659 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8660}
8661
8662/*
8663 * Check if a variable name is valid.
8664 * Return FALSE and give an error if not.
8665 */
8666 int
8667valid_varname(char_u *varname)
8668{
8669 char_u *p;
8670
8671 for (p = varname; *p != NUL; ++p)
8672 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8673 && *p != AUTOLOAD_CHAR)
8674 {
8675 semsg(_(e_illvar), varname);
8676 return FALSE;
8677 }
8678 return TRUE;
8679}
8680
8681/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008684 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008685 * It is OK for "from" and "to" to point to the same item. This is used to
8686 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008687 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008689copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008691 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008692 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008693 switch (from->v_type)
8694 {
8695 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008696 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008697 to->vval.v_number = from->vval.v_number;
8698 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008699 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008700#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008701 to->vval.v_float = from->vval.v_float;
8702 break;
8703#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008704 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008705#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008706 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008707 if (to->vval.v_job != NULL)
8708 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008709 break;
8710#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008711 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008712#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008713 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008714 if (to->vval.v_channel != NULL)
8715 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008716 break;
8717#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008718 case VAR_STRING:
8719 case VAR_FUNC:
8720 if (from->vval.v_string == NULL)
8721 to->vval.v_string = NULL;
8722 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008723 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008724 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008725 if (from->v_type == VAR_FUNC)
8726 func_ref(to->vval.v_string);
8727 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008728 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008729 case VAR_PARTIAL:
8730 if (from->vval.v_partial == NULL)
8731 to->vval.v_partial = NULL;
8732 else
8733 {
8734 to->vval.v_partial = from->vval.v_partial;
8735 ++to->vval.v_partial->pt_refcount;
8736 }
8737 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008738 case VAR_BLOB:
8739 if (from->vval.v_blob == NULL)
8740 to->vval.v_blob = NULL;
8741 else
8742 {
8743 to->vval.v_blob = from->vval.v_blob;
8744 ++to->vval.v_blob->bv_refcount;
8745 }
8746 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008747 case VAR_LIST:
8748 if (from->vval.v_list == NULL)
8749 to->vval.v_list = NULL;
8750 else
8751 {
8752 to->vval.v_list = from->vval.v_list;
8753 ++to->vval.v_list->lv_refcount;
8754 }
8755 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008756 case VAR_DICT:
8757 if (from->vval.v_dict == NULL)
8758 to->vval.v_dict = NULL;
8759 else
8760 {
8761 to->vval.v_dict = from->vval.v_dict;
8762 ++to->vval.v_dict->dv_refcount;
8763 }
8764 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008765 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008766 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008767 break;
8768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
8771/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008772 * Make a copy of an item.
8773 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008774 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8775 * reference to an already copied list/dict can be used.
8776 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008777 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008779item_copy(
8780 typval_T *from,
8781 typval_T *to,
8782 int deep,
8783 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784{
8785 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008786 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008787
Bram Moolenaar33570922005-01-25 22:26:29 +00008788 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008790 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008791 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008792 }
8793 ++recurse;
8794
8795 switch (from->v_type)
8796 {
8797 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008798 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008799 case VAR_STRING:
8800 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008801 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008802 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008803 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008804 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008805 copy_tv(from, to);
8806 break;
8807 case VAR_LIST:
8808 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008809 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008810 if (from->vval.v_list == NULL)
8811 to->vval.v_list = NULL;
8812 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8813 {
8814 /* use the copy made earlier */
8815 to->vval.v_list = from->vval.v_list->lv_copylist;
8816 ++to->vval.v_list->lv_refcount;
8817 }
8818 else
8819 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8820 if (to->vval.v_list == NULL)
8821 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008822 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008823 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008824 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008825 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008826 case VAR_DICT:
8827 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008828 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829 if (from->vval.v_dict == NULL)
8830 to->vval.v_dict = NULL;
8831 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8832 {
8833 /* use the copy made earlier */
8834 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8835 ++to->vval.v_dict->dv_refcount;
8836 }
8837 else
8838 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8839 if (to->vval.v_dict == NULL)
8840 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008842 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008843 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008844 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008845 }
8846 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008847 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848}
8849
8850/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 * This function is used by f_input() and f_inputdialog() functions. The third
8852 * argument to f_input() specifies the type of completion to use at the
8853 * prompt. The third argument to f_inputdialog() specifies the value to return
8854 * when the user cancels the prompt.
8855 */
8856 void
8857get_user_input(
8858 typval_T *argvars,
8859 typval_T *rettv,
8860 int inputdialog,
8861 int secret)
8862{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008863 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 char_u *p = NULL;
8865 int c;
8866 char_u buf[NUMBUFLEN];
8867 int cmd_silent_save = cmd_silent;
8868 char_u *defstr = (char_u *)"";
8869 int xp_type = EXPAND_NOTHING;
8870 char_u *xp_arg = NULL;
8871
8872 rettv->v_type = VAR_STRING;
8873 rettv->vval.v_string = NULL;
8874
8875#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008876 /* While starting up, there is no place to enter text. When running tests
8877 * with --not-a-term we assume feedkeys() will be used. */
8878 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 return;
8880#endif
8881
8882 cmd_silent = FALSE; /* Want to see the prompt. */
8883 if (prompt != NULL)
8884 {
8885 /* Only the part of the message after the last NL is considered as
8886 * prompt for the command line */
8887 p = vim_strrchr(prompt, '\n');
8888 if (p == NULL)
8889 p = prompt;
8890 else
8891 {
8892 ++p;
8893 c = *p;
8894 *p = NUL;
8895 msg_start();
8896 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008897 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008898 msg_didout = FALSE;
8899 msg_starthere();
8900 *p = c;
8901 }
8902 cmdline_row = msg_row;
8903
8904 if (argvars[1].v_type != VAR_UNKNOWN)
8905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008906 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 if (defstr != NULL)
8908 stuffReadbuffSpec(defstr);
8909
8910 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8911 {
8912 char_u *xp_name;
8913 int xp_namelen;
8914 long argt;
8915
8916 /* input() with a third argument: completion */
8917 rettv->vval.v_string = NULL;
8918
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008919 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920 if (xp_name == NULL)
8921 return;
8922
8923 xp_namelen = (int)STRLEN(xp_name);
8924
8925 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8926 &xp_arg) == FAIL)
8927 return;
8928 }
8929 }
8930
8931 if (defstr != NULL)
8932 {
8933 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 ex_normal_busy = 0;
8936 rettv->vval.v_string =
8937 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8938 xp_type, xp_arg);
8939 ex_normal_busy = save_ex_normal_busy;
8940 }
8941 if (inputdialog && rettv->vval.v_string == NULL
8942 && argvars[1].v_type != VAR_UNKNOWN
8943 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008944 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 &argvars[2], buf));
8946
8947 vim_free(xp_arg);
8948
8949 /* since the user typed this, no need to wait for return */
8950 need_wait_return = FALSE;
8951 msg_didout = FALSE;
8952 }
8953 cmd_silent = cmd_silent_save;
8954}
8955
8956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 * ":echo expr1 ..." print each argument separated with a space, add a
8958 * newline at the end.
8959 * ":echon expr1 ..." print each argument plain.
8960 */
8961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008962ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963{
8964 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008966 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 char_u *p;
8968 int needclr = TRUE;
8969 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008970 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008971 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008972 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973
8974 if (eap->skip)
8975 ++emsg_skip;
8976 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8977 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008978 /* If eval1() causes an error message the text from the command may
8979 * still need to be cleared. E.g., "echo 22,44". */
8980 need_clr_eos = needclr;
8981
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
8985 /*
8986 * Report the invalid expression unless the expression evaluation
8987 * has been cancelled due to an aborting error, an interrupt, or an
8988 * exception.
8989 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008990 if (!aborting() && did_emsg == did_emsg_before
8991 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008992 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008993 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 break;
8995 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008996 need_clr_eos = FALSE;
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 if (!eap->skip)
8999 {
9000 if (atstart)
9001 {
9002 atstart = FALSE;
9003 /* Call msg_start() after eval1(), evaluating the expression
9004 * may cause a message to appear. */
9005 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01009006 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02009007 /* Mark the saved text as finishing the line, so that what
9008 * follows is displayed on a new line when scrolling back
9009 * at the more prompt. */
9010 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01009012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01009015 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01009016 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009017 if (p != NULL)
9018 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009020 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009022 if (*p != TAB && needclr)
9023 {
9024 /* remove any text still there from the command */
9025 msg_clr_eos();
9026 needclr = FALSE;
9027 }
9028 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 }
9030 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009031 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009032 if (has_mbyte)
9033 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009034 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009035
9036 (void)msg_outtrans_len_attr(p, i, echo_attr);
9037 p += i - 1;
9038 }
9039 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009040 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009043 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 arg = skipwhite(arg);
9047 }
9048 eap->nextcmd = check_nextcmd(arg);
9049
9050 if (eap->skip)
9051 --emsg_skip;
9052 else
9053 {
9054 /* remove text that may still be there from the command */
9055 if (needclr)
9056 msg_clr_eos();
9057 if (eap->cmdidx == CMD_echo)
9058 msg_end();
9059 }
9060}
9061
9062/*
9063 * ":echohl {name}".
9064 */
9065 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009066ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02009068 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
9071/*
9072 * ":execute expr1 ..." execute the result of an expression.
9073 * ":echomsg expr1 ..." Print a message
9074 * ":echoerr expr1 ..." Print an error
9075 * Each gets spaces around each argument and a newline at the end for
9076 * echo commands
9077 */
9078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009079ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 int ret = OK;
9084 char_u *p;
9085 garray_T ga;
9086 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01009087 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088
9089 ga_init2(&ga, 1, 80);
9090
9091 if (eap->skip)
9092 ++emsg_skip;
9093 while (*arg != NUL && *arg != '|' && *arg != '\n')
9094 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01009095 ret = eval1_emsg(&arg, &rettv, !eap->skip);
9096 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098
9099 if (!eap->skip)
9100 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009101 char_u buf[NUMBUFLEN];
9102
9103 if (eap->cmdidx == CMD_execute)
9104 p = tv_get_string_buf(&rettv, buf);
9105 else
9106 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 len = (int)STRLEN(p);
9108 if (ga_grow(&ga, len + 2) == FAIL)
9109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 ret = FAIL;
9112 break;
9113 }
9114 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 ga.ga_len += len;
9118 }
9119
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 arg = skipwhite(arg);
9122 }
9123
9124 if (ret != FAIL && ga.ga_data != NULL)
9125 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009126 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
9127 {
9128 /* Mark the already saved text as finishing the line, so that what
9129 * follows is displayed on a new line when scrolling back at the
9130 * more prompt. */
9131 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009132 }
9133
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009135 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009136 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009137 out_flush();
9138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 else if (eap->cmdidx == CMD_echoerr)
9140 {
9141 /* We don't want to abort following commands, restore did_emsg. */
9142 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009143 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (!force_abort)
9145 did_emsg = save_did_emsg;
9146 }
9147 else if (eap->cmdidx == CMD_execute)
9148 do_cmdline((char_u *)ga.ga_data,
9149 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9150 }
9151
9152 ga_clear(&ga);
9153
9154 if (eap->skip)
9155 --emsg_skip;
9156
9157 eap->nextcmd = check_nextcmd(arg);
9158}
9159
9160/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 * Find window specified by "vp" in tabpage "tp".
9162 */
9163 win_T *
9164find_win_by_nr(
9165 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009166 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009169 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 if (nr < 0)
9172 return NULL;
9173 if (nr == 0)
9174 return curwin;
9175
Bram Moolenaar29323592016-07-24 22:04:11 +02009176 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
9177 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 if (nr >= LOWEST_WIN_ID)
9179 {
9180 if (wp->w_id == nr)
9181 return wp;
9182 }
9183 else if (--nr <= 0)
9184 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02009185 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009186 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009187 {
9188#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009189 // check tab-local popup windows
9190 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009191 if (wp->w_id == nr)
9192 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009193 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009194 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9195 if (wp->w_id == nr)
9196 return wp;
9197#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009199 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201}
9202
9203/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009204 * Find a window: When using a Window ID in any tab page, when using a number
9205 * in the current tab page.
9206 */
9207 win_T *
9208find_win_by_nr_or_id(typval_T *vp)
9209{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009210 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009211
9212 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009213 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009214 return find_win_by_nr(vp, NULL);
9215}
9216
9217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009219 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009220 */
9221 win_T *
9222find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009223 typval_T *wvp, // VAR_UNKNOWN for current window
9224 typval_T *tvp, // VAR_UNKNOWN for current tab page
9225 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226{
9227 win_T *wp = NULL;
9228 tabpage_T *tp = NULL;
9229 long n;
9230
9231 if (wvp->v_type != VAR_UNKNOWN)
9232 {
9233 if (tvp->v_type != VAR_UNKNOWN)
9234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009235 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009236 if (n >= 0)
9237 tp = find_tabpage(n);
9238 }
9239 else
9240 tp = curtab;
9241
9242 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009243 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009245 if (wp == NULL && wvp->v_type == VAR_NUMBER
9246 && wvp->vval.v_number != -1)
9247 // A window with the specified number is not found
9248 tp = NULL;
9249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250 }
9251 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009252 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009253 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009254 tp = curtab;
9255 }
9256
9257 if (ptp != NULL)
9258 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009259
9260 return wp;
9261}
9262
9263/*
9264 * getwinvar() and gettabwinvar()
9265 */
9266 void
9267getwinvar(
9268 typval_T *argvars,
9269 typval_T *rettv,
9270 int off) /* 1 for gettabwinvar() */
9271{
9272 win_T *win;
9273 char_u *varname;
9274 dictitem_T *v;
9275 tabpage_T *tp = NULL;
9276 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 win_T *oldcurwin;
9278 tabpage_T *oldtabpage;
9279 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009282 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 else
9284 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009286 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 ++emsg_off;
9288
9289 rettv->v_type = VAR_STRING;
9290 rettv->vval.v_string = NULL;
9291
9292 if (win != NULL && varname != NULL)
9293 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294 /* Set curwin to be our win, temporarily. Also set the tabpage,
9295 * otherwise the window is not valid. Only do this when needed,
9296 * autocommands get blocked. */
9297 need_switch_win = !(tp == curtab && win == curwin);
9298 if (!need_switch_win
9299 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009301 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009303 if (varname[1] == NUL)
9304 {
9305 /* get all window-local options in a dict */
9306 dict_T *opts = get_winbuf_options(FALSE);
9307
9308 if (opts != NULL)
9309 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009310 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009311 done = TRUE;
9312 }
9313 }
9314 else if (get_option_tv(&varname, rettv, 1) == OK)
9315 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316 done = TRUE;
9317 }
9318 else
9319 {
9320 /* Look up the variable. */
9321 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9322 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9323 varname, FALSE);
9324 if (v != NULL)
9325 {
9326 copy_tv(&v->di_tv, rettv);
9327 done = TRUE;
9328 }
9329 }
9330 }
9331
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332 if (need_switch_win)
9333 /* restore previous notion of curwin */
9334 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 }
9336
9337 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9338 /* use the default return value */
9339 copy_tv(&argvars[off + 2], rettv);
9340
9341 --emsg_off;
9342}
9343
9344/*
9345 * "setwinvar()" and "settabwinvar()" functions
9346 */
9347 void
9348setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9349{
9350 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351 win_T *save_curwin;
9352 tabpage_T *save_curtab;
9353 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354 char_u *varname, *winvarname;
9355 typval_T *varp;
9356 char_u nbuf[NUMBUFLEN];
9357 tabpage_T *tp = NULL;
9358
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009359 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009360 return;
9361
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 else
9365 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009367 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 varp = &argvars[off + 2];
9369
9370 if (win != NULL && varname != NULL && varp != NULL)
9371 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009372 need_switch_win = !(tp == curtab && win == curwin);
9373 if (!need_switch_win
9374 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 {
9376 if (*varname == '&')
9377 {
9378 long numval;
9379 char_u *strval;
9380 int error = FALSE;
9381
9382 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009383 numval = (long)tv_get_number_chk(varp, &error);
9384 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 if (!error && strval != NULL)
9386 set_option_value(varname, numval, strval, OPT_LOCAL);
9387 }
9388 else
9389 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009390 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391 if (winvarname != NULL)
9392 {
9393 STRCPY(winvarname, "w:");
9394 STRCPY(winvarname + 2, varname);
9395 set_var(winvarname, varp, TRUE);
9396 vim_free(winvarname);
9397 }
9398 }
9399 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400 if (need_switch_win)
9401 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 }
9403}
9404
9405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9407 * "arg" points to the "&" or '+' when called, to "option" when returning.
9408 * Returns NULL when no option name found. Otherwise pointer to the char
9409 * after the option name.
9410 */
9411 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009412find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414 char_u *p = *arg;
9415
9416 ++p;
9417 if (*p == 'g' && p[1] == ':')
9418 {
9419 *opt_flags = OPT_GLOBAL;
9420 p += 2;
9421 }
9422 else if (*p == 'l' && p[1] == ':')
9423 {
9424 *opt_flags = OPT_LOCAL;
9425 p += 2;
9426 }
9427 else
9428 *opt_flags = 0;
9429
9430 if (!ASCII_ISALPHA(*p))
9431 return NULL;
9432 *arg = p;
9433
9434 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9435 p += 4; /* termcap option */
9436 else
9437 while (ASCII_ISALPHA(*p))
9438 ++p;
9439 return p;
9440}
9441
9442/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009443 * Return the autoload script name for a function or variable name.
9444 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009445 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009447 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009448autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009449{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009450 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009451 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009452
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009453 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009454 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009455 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009456 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009457 STRCPY(scriptname, "autoload/");
9458 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009459 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9460 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009461 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009462 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009463 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009464}
9465
9466/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009467 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009468 * Return TRUE if a package was loaded.
9469 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471script_autoload(
9472 char_u *name,
9473 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009474{
9475 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009476 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009477 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009478 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009479
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009480 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009481 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009482 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009483 return FALSE;
9484
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009485 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009486
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009487 /* Find the name in the list of previously loaded package names. Skip
9488 * "autoload/", it's always the same. */
9489 for (i = 0; i < ga_loaded.ga_len; ++i)
9490 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9491 break;
9492 if (!reload && i < ga_loaded.ga_len)
9493 ret = FALSE; /* was loaded already */
9494 else
9495 {
9496 /* Remember the name if it wasn't loaded already. */
9497 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9498 {
9499 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9500 tofree = NULL;
9501 }
9502
9503 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009504 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009505 ret = TRUE;
9506 }
9507
9508 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009509 return ret;
9510}
9511
Bram Moolenaar661b1822005-07-28 22:36:45 +00009512/*
9513 * Display script name where an item was last set.
9514 * Should only be invoked when 'verbose' is non-zero.
9515 */
9516 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009517last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009518{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009519 char_u *p;
9520
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009521 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009522 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009523 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009524 if (p != NULL)
9525 {
9526 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009527 msg_puts(_("\n\tLast set from "));
9528 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009529 if (script_ctx.sc_lnum > 0)
9530 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009531 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009532 msg_outnum((long)script_ctx.sc_lnum);
9533 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009534 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009535 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009536 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009537 }
9538}
9539
Bram Moolenaar61be3762019-03-19 23:04:17 +01009540/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009541 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9542 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009543 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009545reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009546{
9547 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9548 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009549 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9550 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009551 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009552 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009553}
9554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556 * Add an assert error to v:errors.
9557 */
9558 void
9559assert_error(garray_T *gap)
9560{
9561 struct vimvar *vp = &vimvars[VV_ERRORS];
9562
9563 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9564 /* Make sure v:errors is a list. */
9565 set_vim_var_list(VV_ERRORS, list_alloc());
9566 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9567}
Bram Moolenaar31988702018-02-13 12:57:42 +01009568/*
9569 * Compare "typ1" and "typ2". Put the result in "typ1".
9570 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009571 int
9572typval_compare(
9573 typval_T *typ1, /* first operand */
9574 typval_T *typ2, /* second operand */
9575 exptype_T type, /* operator */
9576 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009577 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009578{
9579 int i;
9580 varnumber_T n1, n2;
9581 char_u *s1, *s2;
9582 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9583
Bram Moolenaar31988702018-02-13 12:57:42 +01009584 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009585 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009586 /* For "is" a different type always means FALSE, for "notis"
9587 * it means TRUE. */
9588 n1 = (type == TYPE_NEQUAL);
9589 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009590 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9591 {
9592 if (type_is)
9593 {
9594 n1 = (typ1->v_type == typ2->v_type
9595 && typ1->vval.v_blob == typ2->vval.v_blob);
9596 if (type == TYPE_NEQUAL)
9597 n1 = !n1;
9598 }
9599 else if (typ1->v_type != typ2->v_type
9600 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9601 {
9602 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009603 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009604 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009605 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009606 clear_tv(typ1);
9607 return FAIL;
9608 }
9609 else
9610 {
9611 // Compare two Blobs for being equal or unequal.
9612 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9613 if (type == TYPE_NEQUAL)
9614 n1 = !n1;
9615 }
9616 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009617 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9618 {
9619 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009620 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009621 n1 = (typ1->v_type == typ2->v_type
9622 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009623 if (type == TYPE_NEQUAL)
9624 n1 = !n1;
9625 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009626 else if (typ1->v_type != typ2->v_type
9627 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009628 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009629 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009630 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009631 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009632 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009633 clear_tv(typ1);
9634 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009635 }
9636 else
9637 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009638 /* Compare two Lists for being equal or unequal. */
9639 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9640 ic, FALSE);
9641 if (type == TYPE_NEQUAL)
9642 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009643 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009644 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009645
9646 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9647 {
9648 if (type_is)
9649 {
9650 n1 = (typ1->v_type == typ2->v_type
9651 && typ1->vval.v_dict == typ2->vval.v_dict);
9652 if (type == TYPE_NEQUAL)
9653 n1 = !n1;
9654 }
9655 else if (typ1->v_type != typ2->v_type
9656 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9657 {
9658 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009659 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009660 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009661 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009662 clear_tv(typ1);
9663 return FAIL;
9664 }
9665 else
9666 {
9667 /* Compare two Dictionaries for being equal or unequal. */
9668 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9669 ic, FALSE);
9670 if (type == TYPE_NEQUAL)
9671 n1 = !n1;
9672 }
9673 }
9674
9675 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9676 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9677 {
9678 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009680 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009681 clear_tv(typ1);
9682 return FAIL;
9683 }
9684 if ((typ1->v_type == VAR_PARTIAL
9685 && typ1->vval.v_partial == NULL)
9686 || (typ2->v_type == VAR_PARTIAL
9687 && typ2->vval.v_partial == NULL))
9688 /* when a partial is NULL assume not equal */
9689 n1 = FALSE;
9690 else if (type_is)
9691 {
9692 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9693 /* strings are considered the same if their value is
9694 * the same */
9695 n1 = tv_equal(typ1, typ2, ic, FALSE);
9696 else if (typ1->v_type == VAR_PARTIAL
9697 && typ2->v_type == VAR_PARTIAL)
9698 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9699 else
9700 n1 = FALSE;
9701 }
9702 else
9703 n1 = tv_equal(typ1, typ2, ic, FALSE);
9704 if (type == TYPE_NEQUAL)
9705 n1 = !n1;
9706 }
9707
9708#ifdef FEAT_FLOAT
9709 /*
9710 * If one of the two variables is a float, compare as a float.
9711 * When using "=~" or "!~", always compare as string.
9712 */
9713 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9714 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9715 {
9716 float_T f1, f2;
9717
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009718 f1 = tv_get_float(typ1);
9719 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009720 n1 = FALSE;
9721 switch (type)
9722 {
9723 case TYPE_EQUAL: n1 = (f1 == f2); break;
9724 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9725 case TYPE_GREATER: n1 = (f1 > f2); break;
9726 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9727 case TYPE_SMALLER: n1 = (f1 < f2); break;
9728 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9729 case TYPE_UNKNOWN:
9730 case TYPE_MATCH:
9731 case TYPE_NOMATCH: break; /* avoid gcc warning */
9732 }
9733 }
9734#endif
9735
9736 /*
9737 * If one of the two variables is a number, compare as a number.
9738 * When using "=~" or "!~", always compare as string.
9739 */
9740 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9741 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9742 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009743 n1 = tv_get_number(typ1);
9744 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009745 switch (type)
9746 {
9747 case TYPE_EQUAL: n1 = (n1 == n2); break;
9748 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9749 case TYPE_GREATER: n1 = (n1 > n2); break;
9750 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9751 case TYPE_SMALLER: n1 = (n1 < n2); break;
9752 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9753 case TYPE_UNKNOWN:
9754 case TYPE_MATCH:
9755 case TYPE_NOMATCH: break; /* avoid gcc warning */
9756 }
9757 }
9758 else
9759 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009760 s1 = tv_get_string_buf(typ1, buf1);
9761 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009762 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9763 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9764 else
9765 i = 0;
9766 n1 = FALSE;
9767 switch (type)
9768 {
9769 case TYPE_EQUAL: n1 = (i == 0); break;
9770 case TYPE_NEQUAL: n1 = (i != 0); break;
9771 case TYPE_GREATER: n1 = (i > 0); break;
9772 case TYPE_GEQUAL: n1 = (i >= 0); break;
9773 case TYPE_SMALLER: n1 = (i < 0); break;
9774 case TYPE_SEQUAL: n1 = (i <= 0); break;
9775
9776 case TYPE_MATCH:
9777 case TYPE_NOMATCH:
9778 n1 = pattern_match(s2, s1, ic);
9779 if (type == TYPE_NOMATCH)
9780 n1 = !n1;
9781 break;
9782
9783 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9784 }
9785 }
9786 clear_tv(typ1);
9787 typ1->v_type = VAR_NUMBER;
9788 typ1->vval.v_number = n1;
9789
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009790 return OK;
9791}
9792
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009793 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009794typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009795{
9796 char_u *tofree;
9797 char_u numbuf[NUMBUFLEN];
9798 char_u *ret = NULL;
9799
9800 if (arg == NULL)
9801 return vim_strsave((char_u *)"(does not exist)");
9802 ret = tv2string(arg, &tofree, numbuf, 0);
9803 /* Make a copy if we have a value but it's not in allocated memory. */
9804 if (ret != NULL && tofree == NULL)
9805 ret = vim_strsave(ret);
9806 return ret;
9807}
9808
9809 int
9810var_exists(char_u *var)
9811{
9812 char_u *name;
9813 char_u *tofree;
9814 typval_T tv;
9815 int len = 0;
9816 int n = FALSE;
9817
9818 /* get_name_len() takes care of expanding curly braces */
9819 name = var;
9820 len = get_name_len(&var, &tofree, TRUE, FALSE);
9821 if (len > 0)
9822 {
9823 if (tofree != NULL)
9824 name = tofree;
9825 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9826 if (n)
9827 {
9828 /* handle d.key, l[idx], f(expr) */
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02009829 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009830 if (n)
9831 clear_tv(&tv);
9832 }
9833 }
9834 if (*var != NUL)
9835 n = FALSE;
9836
9837 vim_free(tofree);
9838 return n;
9839}
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841#endif /* FEAT_EVAL */
9842
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009844#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845
Bram Moolenaar4f974752019-02-17 17:44:42 +01009846#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847/*
9848 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
9851/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009852 * Get the short path (8.3) for the filename in "fnamep".
9853 * Only works for a valid file name.
9854 * When the path gets longer "fnamep" is changed and the allocated buffer
9855 * is put in "bufp".
9856 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9857 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 */
9859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009860get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009862 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 char_u *newbuf;
9864
9865 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009866 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 if (l > len - 1)
9868 {
9869 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009870 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 newbuf = vim_strnsave(*fnamep, l);
9872 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874
9875 vim_free(*bufp);
9876 *fnamep = *bufp = newbuf;
9877
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009878 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009879 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881
9882 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009883 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884}
9885
9886/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009887 * Get the short path (8.3) for the filename in "fname". The converted
9888 * path is returned in "bufp".
9889 *
9890 * Some of the directories specified in "fname" may not exist. This function
9891 * will shorten the existing directories at the beginning of the path and then
9892 * append the remaining non-existing path.
9893 *
9894 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009895 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009896 * bufp - Pointer to an allocated buffer for the filename.
9897 * fnamelen - Length of the filename pointed to by fname
9898 *
9899 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 */
9901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009902shortpath_for_invalid_fname(
9903 char_u **fname,
9904 char_u **bufp,
9905 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009907 char_u *short_fname, *save_fname, *pbuf_unused;
9908 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009910 int old_len, len;
9911 int new_len, sfx_len;
9912 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009915 old_len = *fnamelen;
9916 save_fname = vim_strnsave(*fname, old_len);
9917 pbuf_unused = NULL;
9918 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009920 endp = save_fname + old_len - 1; /* Find the end of the copy */
9921 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009923 /*
9924 * Try shortening the supplied path till it succeeds by removing one
9925 * directory at a time from the tail of the path.
9926 */
9927 len = 0;
9928 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009930 /* go back one path-separator */
9931 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9932 --endp;
9933 if (endp <= save_fname)
9934 break; /* processed the complete path */
9935
9936 /*
9937 * Replace the path separator with a NUL and try to shorten the
9938 * resulting path.
9939 */
9940 ch = *endp;
9941 *endp = 0;
9942 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009943 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009944 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9945 {
9946 retval = FAIL;
9947 goto theend;
9948 }
9949 *endp = ch; /* preserve the string */
9950
9951 if (len > 0)
9952 break; /* successfully shortened the path */
9953
9954 /* failed to shorten the path. Skip the path separator */
9955 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 }
9957
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009958 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009960 /*
9961 * Succeeded in shortening the path. Now concatenate the shortened
9962 * path with the remaining path at the tail.
9963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009965 /* Compute the length of the new path. */
9966 sfx_len = (int)(save_endp - endp) + 1;
9967 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009969 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009971 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009973 /* There is not enough space in the currently allocated string,
9974 * copy it to a buffer big enough. */
9975 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009977 {
9978 retval = FAIL;
9979 goto theend;
9980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 }
9982 else
9983 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009984 /* Transfer short_fname to the main buffer (it's big enough),
9985 * unless get_short_pathname() did its work in-place. */
9986 *fname = *bufp = save_fname;
9987 if (short_fname != save_fname)
9988 vim_strncpy(save_fname, short_fname, len);
9989 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009991
9992 /* concat the not-shortened part of the path */
9993 vim_strncpy(*fname + len, endp, sfx_len);
9994 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009996
9997theend:
9998 vim_free(pbuf_unused);
9999 vim_free(save_fname);
10000
10001 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002}
10003
10004/*
10005 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010006 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 */
10008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010009shortpath_for_partial(
10010 char_u **fnamep,
10011 char_u **bufp,
10012 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 int sepcount, len, tflen;
10015 char_u *p;
10016 char_u *pbuf, *tfname;
10017 int hasTilde;
10018
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010019 /* Count up the path separators from the RHS.. so we know which part
10020 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010022 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 if (vim_ispathsep(*p))
10024 ++sepcount;
10025
10026 /* Need full path first (use expand_env() to remove a "~/") */
10027 hasTilde = (**fnamep == '~');
10028 if (hasTilde)
10029 pbuf = tfname = expand_env_save(*fnamep);
10030 else
10031 pbuf = tfname = FullName_save(*fnamep, FALSE);
10032
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010033 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010035 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10036 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
10038 if (len == 0)
10039 {
10040 /* Don't have a valid filename, so shorten the rest of the
10041 * path if we can. This CAN give us invalid 8.3 filenames, but
10042 * there's not a lot of point in guessing what it might be.
10043 */
10044 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010045 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
10048
10049 /* Count the paths backward to find the beginning of the desired string. */
10050 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010051 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010052 if (has_mbyte)
10053 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 if (vim_ispathsep(*p))
10055 {
10056 if (sepcount == 0 || (hasTilde && sepcount == 1))
10057 break;
10058 else
10059 sepcount --;
10060 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (hasTilde)
10063 {
10064 --p;
10065 if (p >= tfname)
10066 *p = '~';
10067 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 }
10070 else
10071 ++p;
10072
10073 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10074 vim_free(*bufp);
10075 *fnamelen = (int)STRLEN(p);
10076 *bufp = pbuf;
10077 *fnamep = p;
10078
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010079 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010081#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
10083/*
10084 * Adjust a filename, according to a string of modifiers.
10085 * *fnamep must be NUL terminated when called. When returning, the length is
10086 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010087 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 * When there is an error, *fnamep is set to NULL.
10089 */
10090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010091modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010092 char_u *src, // string with modifiers
10093 int tilde_file, // "~" is a file name, not $HOME
10094 int *usedlen, // characters after src that are used
10095 char_u **fnamep, // file name so far
10096 char_u **bufp, // buffer for allocated file name or NULL
10097 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
10099 int valid = 0;
10100 char_u *tail;
10101 char_u *s, *p, *pbuf;
10102 char_u dirname[MAXPATHL];
10103 int c;
10104 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010105#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010106 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 int has_shortname = 0;
10108#endif
10109
10110repeat:
10111 /* ":p" - full path/file_name */
10112 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10113 {
10114 has_fullname = 1;
10115
10116 valid |= VALID_PATH;
10117 *usedlen += 2;
10118
10119 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10120 if ((*fnamep)[0] == '~'
10121#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10122 && ((*fnamep)[1] == '/'
10123# ifdef BACKSLASH_IN_FILENAME
10124 || (*fnamep)[1] == '\\'
10125# endif
10126 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010128 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 )
10130 {
10131 *fnamep = expand_env_save(*fnamep);
10132 vim_free(*bufp); /* free any allocated file name */
10133 *bufp = *fnamep;
10134 if (*fnamep == NULL)
10135 return -1;
10136 }
10137
10138 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010139 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 {
10141 if (vim_ispathsep(*p)
10142 && p[1] == '.'
10143 && (p[2] == NUL
10144 || vim_ispathsep(p[2])
10145 || (p[2] == '.'
10146 && (p[3] == NUL || vim_ispathsep(p[3])))))
10147 break;
10148 }
10149
10150 /* FullName_save() is slow, don't use it when not needed. */
10151 if (*p != NUL || !vim_isAbsName(*fnamep))
10152 {
10153 *fnamep = FullName_save(*fnamep, *p != NUL);
10154 vim_free(*bufp); /* free any allocated file name */
10155 *bufp = *fnamep;
10156 if (*fnamep == NULL)
10157 return -1;
10158 }
10159
Bram Moolenaar4f974752019-02-17 17:44:42 +010010160#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010161# if _WIN32_WINNT >= 0x0500
10162 if (vim_strchr(*fnamep, '~') != NULL)
10163 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010164 // Expand 8.3 filename to full path. Needed to make sure the same
10165 // file does not have two different names.
10166 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10167 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10168 WCHAR buf[_MAX_PATH];
10169
10170 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010171 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010172 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010173 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010174 char_u *p = utf16_to_enc(buf, NULL);
10175
10176 if (p != NULL)
10177 {
10178 vim_free(*bufp); // free any allocated file name
10179 *bufp = *fnamep = p;
10180 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010181 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010182 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010183 }
10184 }
10185# endif
10186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 /* Append a path separator to a directory. */
10188 if (mch_isdir(*fnamep))
10189 {
10190 /* Make room for one or two extra characters. */
10191 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10192 vim_free(*bufp); /* free any allocated file name */
10193 *bufp = *fnamep;
10194 if (*fnamep == NULL)
10195 return -1;
10196 add_pathsep(*fnamep);
10197 }
10198 }
10199
10200 /* ":." - path relative to the current directory */
10201 /* ":~" - path relative to the home directory */
10202 /* ":8" - shortname path - postponed till after */
10203 while (src[*usedlen] == ':'
10204 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10205 {
10206 *usedlen += 2;
10207 if (c == '8')
10208 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010209#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 has_shortname = 1; /* Postpone this. */
10211#endif
10212 continue;
10213 }
10214 pbuf = NULL;
10215 /* Need full path first (use expand_env() to remove a "~/") */
10216 if (!has_fullname)
10217 {
10218 if (c == '.' && **fnamep == '~')
10219 p = pbuf = expand_env_save(*fnamep);
10220 else
10221 p = pbuf = FullName_save(*fnamep, FALSE);
10222 }
10223 else
10224 p = *fnamep;
10225
10226 has_fullname = 0;
10227
10228 if (p != NULL)
10229 {
10230 if (c == '.')
10231 {
10232 mch_dirname(dirname, MAXPATHL);
10233 s = shorten_fname(p, dirname);
10234 if (s != NULL)
10235 {
10236 *fnamep = s;
10237 if (pbuf != NULL)
10238 {
10239 vim_free(*bufp); /* free any allocated file name */
10240 *bufp = pbuf;
10241 pbuf = NULL;
10242 }
10243 }
10244 }
10245 else
10246 {
10247 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10248 /* Only replace it when it starts with '~' */
10249 if (*dirname == '~')
10250 {
10251 s = vim_strsave(dirname);
10252 if (s != NULL)
10253 {
10254 *fnamep = s;
10255 vim_free(*bufp);
10256 *bufp = s;
10257 }
10258 }
10259 }
10260 vim_free(pbuf);
10261 }
10262 }
10263
10264 tail = gettail(*fnamep);
10265 *fnamelen = (int)STRLEN(*fnamep);
10266
10267 /* ":h" - head, remove "/file_name", can be repeated */
10268 /* Don't remove the first "/" or "c:\" */
10269 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10270 {
10271 valid |= VALID_HEAD;
10272 *usedlen += 2;
10273 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010274 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010275 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 *fnamelen = (int)(tail - *fnamep);
10277#ifdef VMS
10278 if (*fnamelen > 0)
10279 *fnamelen += 1; /* the path separator is part of the path */
10280#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010281 if (*fnamelen == 0)
10282 {
10283 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10284 p = vim_strsave((char_u *)".");
10285 if (p == NULL)
10286 return -1;
10287 vim_free(*bufp);
10288 *bufp = *fnamep = tail = p;
10289 *fnamelen = 1;
10290 }
10291 else
10292 {
10293 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010294 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 }
10297
10298 /* ":8" - shortname */
10299 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10300 {
10301 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010302#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 has_shortname = 1;
10304#endif
10305 }
10306
Bram Moolenaar4f974752019-02-17 17:44:42 +010010307#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010308 /*
10309 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 */
10311 if (has_shortname)
10312 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010313 /* Copy the string if it is shortened by :h and when it wasn't copied
10314 * yet, because we are going to change it in place. Avoids changing
10315 * the buffer name for "%:8". */
10316 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 {
10318 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010319 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 return -1;
10321 vim_free(*bufp);
10322 *bufp = *fnamep = p;
10323 }
10324
10325 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010326 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 if (!has_fullname && !vim_isAbsName(*fnamep))
10328 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010329 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 return -1;
10331 }
10332 else
10333 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010334 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335
Bram Moolenaardc935552011-08-17 15:23:23 +020010336 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010338 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 return -1;
10340
10341 if (l == 0)
10342 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010343 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010345 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 return -1;
10347 }
10348 *fnamelen = l;
10349 }
10350 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010351#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352
10353 /* ":t" - tail, just the basename */
10354 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10355 {
10356 *usedlen += 2;
10357 *fnamelen -= (int)(tail - *fnamep);
10358 *fnamep = tail;
10359 }
10360
10361 /* ":e" - extension, can be repeated */
10362 /* ":r" - root, without extension, can be repeated */
10363 while (src[*usedlen] == ':'
10364 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10365 {
10366 /* find a '.' in the tail:
10367 * - for second :e: before the current fname
10368 * - otherwise: The last '.'
10369 */
10370 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10371 s = *fnamep - 2;
10372 else
10373 s = *fnamep + *fnamelen - 1;
10374 for ( ; s > tail; --s)
10375 if (s[0] == '.')
10376 break;
10377 if (src[*usedlen + 1] == 'e') /* :e */
10378 {
10379 if (s > tail)
10380 {
10381 *fnamelen += (int)(*fnamep - (s + 1));
10382 *fnamep = s + 1;
10383#ifdef VMS
10384 /* cut version from the extension */
10385 s = *fnamep + *fnamelen - 1;
10386 for ( ; s > *fnamep; --s)
10387 if (s[0] == ';')
10388 break;
10389 if (s > *fnamep)
10390 *fnamelen = s - *fnamep;
10391#endif
10392 }
10393 else if (*fnamep <= tail)
10394 *fnamelen = 0;
10395 }
10396 else /* :r */
10397 {
10398 if (s > tail) /* remove one extension */
10399 *fnamelen = (int)(s - *fnamep);
10400 }
10401 *usedlen += 2;
10402 }
10403
10404 /* ":s?pat?foo?" - substitute */
10405 /* ":gs?pat?foo?" - global substitute */
10406 if (src[*usedlen] == ':'
10407 && (src[*usedlen + 1] == 's'
10408 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10409 {
10410 char_u *str;
10411 char_u *pat;
10412 char_u *sub;
10413 int sep;
10414 char_u *flags;
10415 int didit = FALSE;
10416
10417 flags = (char_u *)"";
10418 s = src + *usedlen + 2;
10419 if (src[*usedlen + 1] == 'g')
10420 {
10421 flags = (char_u *)"g";
10422 ++s;
10423 }
10424
10425 sep = *s++;
10426 if (sep)
10427 {
10428 /* find end of pattern */
10429 p = vim_strchr(s, sep);
10430 if (p != NULL)
10431 {
10432 pat = vim_strnsave(s, (int)(p - s));
10433 if (pat != NULL)
10434 {
10435 s = p + 1;
10436 /* find end of substitution */
10437 p = vim_strchr(s, sep);
10438 if (p != NULL)
10439 {
10440 sub = vim_strnsave(s, (int)(p - s));
10441 str = vim_strnsave(*fnamep, *fnamelen);
10442 if (sub != NULL && str != NULL)
10443 {
10444 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010445 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (s != NULL)
10447 {
10448 *fnamep = s;
10449 *fnamelen = (int)STRLEN(s);
10450 vim_free(*bufp);
10451 *bufp = s;
10452 didit = TRUE;
10453 }
10454 }
10455 vim_free(sub);
10456 vim_free(str);
10457 }
10458 vim_free(pat);
10459 }
10460 }
10461 /* after using ":s", repeat all the modifiers */
10462 if (didit)
10463 goto repeat;
10464 }
10465 }
10466
Bram Moolenaar26df0922014-02-23 23:39:13 +010010467 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10468 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010469 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010470 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010471 if (c != NUL)
10472 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010473 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010474 if (c != NUL)
10475 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010476 if (p == NULL)
10477 return -1;
10478 vim_free(*bufp);
10479 *bufp = *fnamep = p;
10480 *fnamelen = (int)STRLEN(p);
10481 *usedlen += 2;
10482 }
10483
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 return valid;
10485}
10486
10487/*
10488 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010489 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 * "flags" can be "g" to do a global substitute.
10491 * Returns an allocated string, NULL for error.
10492 */
10493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010494do_string_sub(
10495 char_u *str,
10496 char_u *pat,
10497 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010498 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010499 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 int sublen;
10502 regmatch_T regmatch;
10503 int i;
10504 int do_all;
10505 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010506 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 garray_T ga;
10508 char_u *ret;
10509 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010510 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511
10512 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10513 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010514 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515
10516 ga_init2(&ga, 1, 200);
10517
10518 do_all = (flags[0] == 'g');
10519
10520 regmatch.rm_ic = p_ic;
10521 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10522 if (regmatch.regprog != NULL)
10523 {
10524 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010525 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10527 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010528 /* Skip empty match except for first match. */
10529 if (regmatch.startp[0] == regmatch.endp[0])
10530 {
10531 if (zero_width == regmatch.startp[0])
10532 {
10533 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010534 i = MB_PTR2LEN(tail);
10535 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10536 (size_t)i);
10537 ga.ga_len += i;
10538 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010539 continue;
10540 }
10541 zero_width = regmatch.startp[0];
10542 }
10543
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 /*
10545 * Get some space for a temporary buffer to do the substitution
10546 * into. It will contain:
10547 * - The text up to where the match is.
10548 * - The substituted text.
10549 * - The text after the match.
10550 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010551 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010552 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10554 {
10555 ga_clear(&ga);
10556 break;
10557 }
10558
10559 /* copy the text up to where the match is */
10560 i = (int)(regmatch.startp[0] - tail);
10561 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10562 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010563 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 + ga.ga_len + i, TRUE, TRUE, FALSE);
10565 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010566 tail = regmatch.endp[0];
10567 if (*tail == NUL)
10568 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 if (!do_all)
10570 break;
10571 }
10572
10573 if (ga.ga_data != NULL)
10574 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10575
Bram Moolenaar473de612013-06-08 18:19:48 +020010576 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 }
10578
10579 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10580 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010581 if (p_cpo == empty_option)
10582 p_cpo = save_cpo;
10583 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010584 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010585 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586
10587 return ret;
10588}
10589
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 static int
10591filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10592{
10593 typval_T rettv;
10594 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010595 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596
10597 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10598 argv[0] = vimvars[VV_KEY].vv_tv;
10599 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010600 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10601 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602 if (map)
10603 {
10604 /* map(): replace the list item value */
10605 clear_tv(tv);
10606 rettv.v_lock = 0;
10607 *tv = rettv;
10608 }
10609 else
10610 {
10611 int error = FALSE;
10612
10613 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010614 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 clear_tv(&rettv);
10616 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010617 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 if (error)
10619 goto theend;
10620 }
10621 retval = OK;
10622theend:
10623 clear_tv(&vimvars[VV_VAL].vv_tv);
10624 return retval;
10625}
10626
10627
10628/*
10629 * Implementation of map() and filter().
10630 */
10631 void
10632filter_map(typval_T *argvars, typval_T *rettv, int map)
10633{
10634 typval_T *expr;
10635 listitem_T *li, *nli;
10636 list_T *l = NULL;
10637 dictitem_T *di;
10638 hashtab_T *ht;
10639 hashitem_T *hi;
10640 dict_T *d = NULL;
10641 typval_T save_val;
10642 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010643 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010644 int rem;
10645 int todo;
10646 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10647 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10648 : N_("filter() argument"));
10649 int save_did_emsg;
10650 int idx = 0;
10651
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010652 if (argvars[0].v_type == VAR_BLOB)
10653 {
10654 if ((b = argvars[0].vval.v_blob) == NULL)
10655 return;
10656 }
10657 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658 {
10659 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010660 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010661 return;
10662 }
10663 else if (argvars[0].v_type == VAR_DICT)
10664 {
10665 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010666 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 return;
10668 }
10669 else
10670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010671 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672 return;
10673 }
10674
10675 expr = &argvars[1];
10676 /* On type errors, the preceding call has already displayed an error
10677 * message. Avoid a misleading error message for an empty string that
10678 * was not passed as argument. */
10679 if (expr->v_type != VAR_UNKNOWN)
10680 {
10681 prepare_vimvar(VV_VAL, &save_val);
10682
10683 /* We reset "did_emsg" to be able to detect whether an error
10684 * occurred during evaluation of the expression. */
10685 save_did_emsg = did_emsg;
10686 did_emsg = FALSE;
10687
10688 prepare_vimvar(VV_KEY, &save_key);
10689 if (argvars[0].v_type == VAR_DICT)
10690 {
10691 vimvars[VV_KEY].vv_type = VAR_STRING;
10692
10693 ht = &d->dv_hashtab;
10694 hash_lock(ht);
10695 todo = (int)ht->ht_used;
10696 for (hi = ht->ht_array; todo > 0; ++hi)
10697 {
10698 if (!HASHITEM_EMPTY(hi))
10699 {
10700 int r;
10701
10702 --todo;
10703 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010704 if (map && (var_check_lock(di->di_tv.v_lock,
10705 arg_errmsg, TRUE)
10706 || var_check_ro(di->di_flags,
10707 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 break;
10709 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10710 r = filter_map_one(&di->di_tv, expr, map, &rem);
10711 clear_tv(&vimvars[VV_KEY].vv_tv);
10712 if (r == FAIL || did_emsg)
10713 break;
10714 if (!map && rem)
10715 {
10716 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10717 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10718 break;
10719 dictitem_remove(d, di);
10720 }
10721 }
10722 }
10723 hash_unlock(ht);
10724 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010725 else if (argvars[0].v_type == VAR_BLOB)
10726 {
10727 int i;
10728 typval_T tv;
10729
10730 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10731 for (i = 0; i < b->bv_ga.ga_len; i++)
10732 {
10733 tv.v_type = VAR_NUMBER;
10734 tv.vval.v_number = blob_get(b, i);
10735 vimvars[VV_KEY].vv_nr = idx;
10736 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10737 break;
10738 if (tv.v_type != VAR_NUMBER)
10739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010740 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010741 return;
10742 }
10743 tv.v_type = VAR_NUMBER;
10744 blob_set(b, i, tv.vval.v_number);
10745 if (!map && rem)
10746 {
10747 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10748
10749 mch_memmove(p + idx, p + i + 1,
10750 (size_t)b->bv_ga.ga_len - i - 1);
10751 --b->bv_ga.ga_len;
10752 --i;
10753 }
10754 }
10755 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010756 else
10757 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010758 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10760
10761 for (li = l->lv_first; li != NULL; li = nli)
10762 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010763 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764 break;
10765 nli = li->li_next;
10766 vimvars[VV_KEY].vv_nr = idx;
10767 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10768 || did_emsg)
10769 break;
10770 if (!map && rem)
10771 listitem_remove(l, li);
10772 ++idx;
10773 }
10774 }
10775
10776 restore_vimvar(VV_KEY, &save_key);
10777 restore_vimvar(VV_VAL, &save_val);
10778
10779 did_emsg |= save_did_emsg;
10780 }
10781
10782 copy_tv(&argvars[0], rettv);
10783}
10784
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */